| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | # This client is inspired by rdapper: | ||
| 4 | # https://metacpan.org/source/GBROWN/rdapper-0.3 | ||
| 5 | # | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use NOLookup::RDAP::RDAPLookup qw / $sepln %RDAP_FIELDSETS %RDAP_REGISTERED_LAYERS/; | ||
| 10 | use NOLookup::RDAP::RDAPLookup::Whois; | ||
| 11 | use Encode; | ||
| 12 | use Getopt::Long qw(:config no_ignore_case bundling); | ||
| 13 | use Pod::Usage; | ||
| 14 | use Term::ANSIColor; | ||
| 15 | use JSON; | ||
| 16 | |||
| 17 | use Data::Dumper; | ||
| 18 | $Data::Dumper::Indent=1; | ||
| 19 | |||
| 20 | my ($service_url, $query, $check, $nameservers, $rawtype, $fieldset, | ||
| 21 | $cursor, $nopages, $entity, $help, $debug, $verbose, $expand, | ||
| 22 | $short, $use_cache, $header_secret, $header_proxy, $referral_ip, | ||
| 23 | $whois_fmt, $force_ipv, $bauth_username, $bauth_password, $color, | ||
| 24 | @access_layers); | ||
| 25 | |||
| 26 | |||
| 27 | my $BG_COL_GREY = 'on_grey23'; | ||
| 28 | my $FG_COL_YELLOW = 'yellow'; | ||
| 29 | my $FG_COL_RED = 'red'; | ||
| 30 | |||
| 31 | # Reset any term colors | ||
| 32 | print color('reset'), "\n"; | ||
| 33 | |||
| 34 | ## | ||
| 35 | # Default test values unless overrided by their parameters | ||
| 36 | my $use_test_values = 1; | ||
| 37 | my $test_service_url = $ENV{RDAP_SERVICE_URL} || 'https://rdap.test.norid.no'; | ||
| 38 | my $test_secret = $ENV{RDAP_GDPR_LAYER_ACCESS_TOKEN} || ''; | ||
| 39 | my $test_proxy = $ENV{RDAP_GDPR_NORID_PROXY} || ''; | ||
| 40 | my $test_referral_ip = 0; # or an ip, like '1.2.3.4'; | ||
| 41 | my $test_expand = 0; | ||
| 42 | |||
| 43 | GetOptions( | ||
| 44 | 'service_url|s:s' => \$service_url, | ||
| 45 | 'query|q:s' => \$query, | ||
| 46 | 'check|c' => \$check, | ||
| 47 | 'nameservers|n:i' => \$nameservers, | ||
| 48 | 'entity|e' => \$entity, | ||
| 49 | 'header_proxy|y:i' => \$header_proxy, | ||
| 50 | 'header_secret|z:s' => \$header_secret, | ||
| 51 | 'referral_ip|I:s' => \$referral_ip, | ||
| 52 | 'expand|x' => \$expand, | ||
| 53 | 'short|o' => \$short, | ||
| 54 | 'use_cache|a' => \$use_cache, | ||
| 55 | 'debug|d:i' => \$debug, | ||
| 56 | 'help|h' => \$help, | ||
| 57 | 'verbose|v' => \$verbose, | ||
| 58 | 'whois_fmt|w' => \$whois_fmt, | ||
| 59 | 'force_ipv|f:i' => \$force_ipv, | ||
| 60 | 'bauth_username|U:s' => \$bauth_username, | ||
| 61 | 'bauth_password|P:s' => \$bauth_password, | ||
| 62 | 'color|l' => \$color, | ||
| 63 | 'rawtype|r' => \$rawtype, | ||
| 64 | 'fieldset|F:s' => \$fieldset, | ||
| 65 | 'cursor|C:s' => \$cursor, | ||
| 66 | 'nopages|p:i' => \$nopages, | ||
| 67 | 'access_layers|a:s@' => \@access_layers, | ||
| 68 | |||
| 69 | ) or pod2usage('-verbose' => 99, '-sections' => [qw(NAME DESCRIPTION USAGE)]); | ||
| 70 | |||
| 71 | |||
| 72 | pod2usage('-verbose' => 99, '-sections' => [qw(NAME DESCRIPTION USAGE)]) if ($help); | ||
| 73 | |||
| 74 | unless ($query) { | ||
| 75 | pod2usage("A query (-q) being a domainname, identity, nameserver name or a handle must be specified!\n"); | ||
| 76 | } | ||
| 77 | |||
| 78 | if ($use_test_values) { | ||
| 79 | $service_url = $test_service_url unless $service_url; | ||
| 80 | $header_secret = $test_secret unless defined($header_secret); | ||
| 81 | $header_proxy = $test_proxy unless defined($header_proxy); | ||
| 82 | $referral_ip = $test_referral_ip unless defined($referral_ip); | ||
| 83 | $expand = $test_expand unless $expand; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub print_warnings { | ||
| 87 | my (@params) = @_; | ||
| 88 | |||
| 89 | print STDERR color($FG_COL_YELLOW) if $color; | ||
| 90 | print STDERR "Warnings:\n"; | ||
| 91 | |||
| 92 | foreach my $el (@params) { | ||
| 93 | next unless $el; | ||
| 94 | my $str = encode('UTF-8', sprintf(" %s", $el)); | ||
| 95 | print STDERR $str, "\n"; | ||
| 96 | } | ||
| 97 | print color('reset'), "\n" if $color; | ||
| 98 | |||
| 99 | } | ||
| 100 | |||
| 101 | sub print_errors { | ||
| 102 | my (@params) = @_; | ||
| 103 | |||
| 104 | print STDERR color($FG_COL_RED) if $color; | ||
| 105 | print STDERR "Errors:\n"; | ||
| 106 | |||
| 107 | foreach my $el (@params) { | ||
| 108 | next unless $el; | ||
| 109 | my $str = encode('UTF-8', sprintf(" %s", $el)); | ||
| 110 | print STDERR $str, "\n"; | ||
| 111 | } | ||
| 112 | print color('reset'), "\n" if $color; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub print_verbose { | ||
| 116 | my $ro = shift; | ||
| 117 | |||
| 118 | if ($verbose) { | ||
| 119 | unless ($check) { | ||
| 120 | print $sepln; | ||
| 121 | print "\n--\nJSON raw data structure pretty: '", $ro->raw_json_decoded, "'\n--\n"; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | my $ro; | ||
| 127 | |||
| 128 | my %OPTIONS = ( | ||
| 129 | service_url => $service_url , | ||
| 130 | debug => $debug || 0, | ||
| 131 | use_cache => $use_cache, | ||
| 132 | norid_header_secret => $header_secret, | ||
| 133 | norid_header_proxy => $header_proxy, | ||
| 134 | norid_referral_ip => $referral_ip, | ||
| 135 | bauth_username => $bauth_username, | ||
| 136 | bauth_password => $bauth_password, | ||
| 137 | rawtype => $rawtype, | ||
| 138 | fieldset => $fieldset, | ||
| 139 | cursor => $cursor, | ||
| 140 | nopages => $nopages, | ||
| 141 | force_ipv => $force_ipv, | ||
| 142 | ); | ||
| 143 | |||
| 144 | # Default access layers are those known by NOLookup lib. | ||
| 145 | # Application can override by passing other values. | ||
| 146 | if (@access_layers) { | ||
| 147 | # Use passed access layers | ||
| 148 | my %hal = map {$_ => 1} sort @access_layers; | ||
| 149 | $OPTIONS{access_layers} = \%hal; | ||
| 150 | } | ||
| 151 | |||
| 152 | #print STDERR "OPTIONS: ", Dumper \%OPTIONS; | ||
| 153 | |||
| 154 | if ($force_ipv) { | ||
| 155 | # Try to connect on the requested protocol, if possible | ||
| 156 | unless ($force_ipv == 4 || $force_ipv == 6) { | ||
| 157 | pod2usage("The -f option must specify 4 or 6!\n"); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | if ($whois_fmt) { | ||
| 162 | $ro = NOLookup::RDAP::RDAPLookup::Whois->new({ %OPTIONS }); | ||
| 163 | } else { | ||
| 164 | $ro = NOLookup::RDAP::RDAPLookup->new({ %OPTIONS }); | ||
| 165 | } | ||
| 166 | |||
| 167 | ## | ||
| 168 | # Validation and analyzing what type of query is done by the lookup | ||
| 169 | # | ||
| 170 | |||
| 171 | $ro->lookup($query, $check, $nameservers, $entity); | ||
| 172 | |||
| 173 | if ($ro->error) { | ||
| 174 | print_verbose($ro); | ||
| 175 | print_errors($ro->error, $ro->status, $ro->description); | ||
| 176 | exit 1; | ||
| 177 | } | ||
| 178 | |||
| 179 | if ($ro->warning) { | ||
| 180 | print_verbose($ro); | ||
| 181 | print_warnings($ro->warning, $ro->status, $ro->description); | ||
| 182 | } else { | ||
| 183 | print_verbose($ro); | ||
| 184 | } | ||
| 185 | |||
| 186 | if ($debug) { | ||
| 187 | print STDERR $sepln; | ||
| 188 | print STDERR "$0:\n"; | ||
| 189 | print STDERR " (connecting over ipv $force_ipv since force_ipv option is set)\n" if ($force_ipv); | ||
| 190 | print STDERR " Looked up : ", $ro->_method, "/ ", $ro->_full_url, "\n"; | ||
| 191 | print STDERR " Acccess layer set to : ", $ro->access_layer, "\n"; | ||
| 192 | print STDERR " Size of returned data : ", $ro->size || 0, "\n"; | ||
| 193 | |||
| 194 | # paging data | ||
| 195 | if ($ro->is_a_search) { | ||
| 196 | # Cursors to pages | ||
| 197 | print STDERR " Is a search, page info :\n"; | ||
| 198 | print STDERR " page_size : ", $ro->page_size || '-', "\n"; | ||
| 199 | print STDERR " first_page: ", $ro->first_page || '-', "\n"; | ||
| 200 | print STDERR " cur_page : ", $ro->cur_page || '-', "\n"; | ||
| 201 | print STDERR " prev_page : ", $ro->prev_page || '-', "\n"; | ||
| 202 | print STDERR " next_page : ", $ro->next_page || '-', "\n"; | ||
| 203 | print STDERR " size : ", $ro->size || '-', "\n"; | ||
| 204 | print STDERR " total_size: ", $ro->total_size || '-', "\n"; | ||
| 205 | |||
| 206 | } | ||
| 207 | print STDERR $sepln; | ||
| 208 | } | ||
| 209 | |||
| 210 | if ($check) { | ||
| 211 | print "\n-- HEAD (check) operation OK, query '$query' found --\n"; | ||
| 212 | exit 0; | ||
| 213 | } | ||
| 214 | |||
| 215 | if ($debug) { | ||
| 216 | print $sepln; | ||
| 217 | print " GET (lookup) operation OK, query '$query' found\n"; | ||
| 218 | print " Use the -v option to see the raw JSON content\n" unless ($verbose); | ||
| 219 | } | ||
| 220 | |||
| 221 | my $result = $ro->result; | ||
| 222 | #print STDERR "ro result: ", Dumper $result; | ||
| 223 | #print "lookup up class: ", $result->class, "\n" if ($result->class); | ||
| 224 | ## Print structured output | ||
| 225 | |||
| 226 | my ($rs, $errs); | ||
| 227 | |||
| 228 | print $sepln if ($debug); | ||
| 229 | |||
| 230 | if ($whois_fmt) { | ||
| 231 | # Make a whois string of the rdap result | ||
| 232 | $ro->{insert_page_info} = 1; | ||
| 233 | |||
| 234 | ($rs, $errs) = $ro->result_as_norid_whois_string($check, $nameservers, $entity, $expand); | ||
| 235 | |||
| 236 | if ($rs) { | ||
| 237 | print color($BG_COL_GREY) if $color; | ||
| 238 | print "\n"; | ||
| 239 | print uc("Result in whois text format:\n\n"); | ||
| 240 | print encode('UTF-8', "$rs\n\n"); | ||
| 241 | |||
| 242 | #print STDERR " from result: ", Dumper $result; | ||
| 243 | |||
| 244 | ### | ||
| 245 | # DEBUG: Make whois objects of the whois string | ||
| 246 | #my ($wh, $do, $ho) = $ro->norid_whois_parse($rs); | ||
| 247 | |||
| 248 | #print "no_rdap.pl, wh: ", Dumper $wh if ($wh); | ||
| 249 | #print "no_rdap.pl, do: ", Dumper $do if ($do); | ||
| 250 | #print "no_rdap.pl, ho: ", Dumper $ho if ($ho); | ||
| 251 | |||
| 252 | } | ||
| 253 | |||
| 254 | } else { | ||
| 255 | ($rs, $errs) = $ro->result_as_rdap_string($check, $nameservers, $entity, $short, $expand); | ||
| 256 | |||
| 257 | if ($rs) { | ||
| 258 | print color($BG_COL_GREY) if $color; | ||
| 259 | print "\n"; | ||
| 260 | print uc("Result in RDAP text format:\n\n"); | ||
| 261 | print encode('UTF-8', "$rs\n\n") if ($rs); | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | print color('reset'), "\n" if $color; | ||
| 266 | |||
| 267 | print $sepln if ($debug); | ||
| 268 | |||
| 269 | if ($errs && @$errs) { | ||
| 270 | print_warnings(@$errs); | ||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | =pod | ||
| 275 | |||
| 276 | =head1 NAME | ||
| 277 | |||
| 278 | no_rdap.pl | ||
| 279 | |||
| 280 | =head1 DESCRIPTION | ||
| 281 | |||
| 282 | Default behaviour: | ||
| 283 | Uses NOLookup::RDAP::RDAPLookup to perform RDAP lookup on domain | ||
| 284 | related queries and fetch and print the matching information in a | ||
| 285 | textual format inspired by rdapper. | ||
| 286 | |||
| 287 | With the -w option: | ||
| 288 | Uses NOLookup::RDAP::RDAPLookup::Whois to perform RDAP lookup on | ||
| 289 | domain related queries and fetch and print the matching information in | ||
| 290 | Norid old school whois format (rdap2whois behaviour). | ||
| 291 | |||
| 292 | =head1 USAGE | ||
| 293 | |||
| 294 | The lookup arguments can be specified by the user, and passed as a | ||
| 295 | raw argument. A raw query must be indicated with the -r option. | ||
| 296 | |||
| 297 | Otherwise the default behavious kicks in. The query type is | ||
| 298 | automagically guessed by analyzing the query, combined with | ||
| 299 | -n or -e options to resole some ambiguous cases. | ||
| 300 | |||
| 301 | perl no_rdap.pl -q <query> | ||
| 302 | |||
| 303 | Examples: | ||
| 304 | |||
| 305 | * Lookup a single object: | ||
| 306 | |||
| 307 | - A domain: | ||
| 308 | no_rdap.pl -q norid.no | ||
| 309 | |||
| 310 | - A nameserver: | ||
| 311 | no_rdap.pl -q nn.uninett.no -n 1 | ||
| 312 | |||
| 313 | - A role/host/registrar/registrant entity object: | ||
| 314 | no_rdap.pl -q UH9R-NORID | ||
| 315 | no_rdap.pl -q NN14H-NORID | ||
| 316 | no_rdap.pl -q reg2-NORID -w | ||
| 317 | no_rdap.pl -q UNA78O-NORID -e | ||
| 318 | no_rdap.pl -q XX18091P-NORID -e | ||
| 319 | |||
| 320 | * Search for matching objects: | ||
| 321 | |||
| 322 | - List of matching domains from a domain name or wildcard: | ||
| 323 | no_rdap.pl -q winter.no (same as lookup of one domain) | ||
| 324 | no_rdap.pl -q winter*.no | ||
| 325 | no_rdap.pl -q *apartment.no | ||
| 326 | |||
| 327 | - List of matching domains from a registrant identity: | ||
| 328 | no_rdap.pl -q 985821585 | ||
| 329 | no_rdap.pl -q N.PRI.1234567 | ||
| 330 | no_rdap.pl -q N.ORG.1234567 | ||
| 331 | no_rdap.pl -q N.LEG.1234567 | ||
| 332 | |||
| 333 | - List of matching domains from a registrant handle: | ||
| 334 | no_rdap.pl -q UNA78O-NORID | ||
| 335 | no_rdap.pl -q XX18091P-NORID | ||
| 336 | |||
| 337 | - List of matching domains from a nameserver name | ||
| 338 | or wildcard: | ||
| 339 | no_rdap.pl -q nn.uninett.no -n 2 | ||
| 340 | no_rdap.pl -q nn.u*.no -n 2 | ||
| 341 | no_rdap.pl -q *inett.no -n 2 | ||
| 342 | |||
| 343 | - List of matching domains from a nameserver ip: | ||
| 344 | no_rdap.pl -q 158.38.0.181 | ||
| 345 | no_rdap.pl -q 2001:700:0:503::aa:5302 | ||
| 346 | |||
| 347 | - List of matching nameservers from a nameserver name | ||
| 348 | wildcard: | ||
| 349 | no_rdap.pl -q nn.uninett.no -n 1 (same as lookup of one | ||
| 350 | nameserver) | ||
| 351 | no_rdap.pl -q nn.u*.no -n 1 | ||
| 352 | no_rdap.pl -q *inett.no -n 1 | ||
| 353 | |||
| 354 | - List of matching nameservers from a nameserver ip: | ||
| 355 | no_rdap.pl -q 158.38.0.181 -n 1 | ||
| 356 | no_rdap.pl -q 2001:700:0:503::aa:5302 -n 1 | ||
| 357 | |||
| 358 | - List of matching entities from a registrant identity: | ||
| 359 | no_rdap.pl -q 985821585 -e | ||
| 360 | no_rdap.pl -q N.PRI.1234567 -e | ||
| 361 | no_rdap.pl -q N.ORG.1234567 -e | ||
| 362 | no_rdap.pl -q N.LEG.1234567 -e | ||
| 363 | |||
| 364 | - List of matching entities from a contact's full name or | ||
| 365 | wildcard: | ||
| 366 | no_rdap.pl -q 'trond haugen' | ||
| 367 | no_rdap.pl -q 'haugen' | ||
| 368 | no_rdap.pl -q 'trond hau*' | ||
| 369 | no_rdap.pl -q '* haugen' | ||
| 370 | |||
| 371 | |||
| 372 | Mandatory arguments: | ||
| 373 | |||
| 374 | -q: query, one of: | ||
| 375 | - domain name or wildcard | ||
| 376 | - nameserver name or wildcard (see use of -n) | ||
| 377 | - handle, if query matches a handle (P/O/R/H/REG (D not | ||
| 378 | supported, use domain name)) | ||
| 379 | - identity: if query is a holder identity [ 985821585 | N.{PRI|ORG|LEG}.xxxxx ], a search is | ||
| 380 | performed to find matching domains. | ||
| 381 | - identity: if query is a O/P holder handle, a search is | ||
| 382 | performed to find matching domains | ||
| 383 | if -e is set, the handle object is looked up instead | ||
| 384 | - ip-address: lookup domains with nameservers using this ip, | ||
| 385 | or lookup nameservers using this ip if -n is set. | ||
| 386 | |||
| 387 | -r: The query is raw: | ||
| 388 | A raw query is complete, and shall be used as is towards the | ||
| 389 | RDAP backend. The http(s) part must not be included. | ||
| 390 | |||
| 391 | |||
| 392 | Optional arguments: | ||
| 393 | |||
| 394 | Query options: | ||
| 395 | -n: integer, one of: | ||
| 396 | 1: The query is a nameserver name, and a nameservers | ||
| 397 | by nameserver name search shall be performed | ||
| 398 | 2: The query is a nameserver name, and a domains | ||
| 399 | by nameserver name search shall be performed | ||
| 400 | -s: The full http(s)-address (URL) of the RDAP service (default is | ||
| 401 | https://rdap.test.norid.no) | ||
| 402 | -c: Do a HEAD instead of the default GET. HEAD returns no data, and | ||
| 403 | can be used to check existence of domain etc. | ||
| 404 | |||
| 405 | Authentication options: | ||
| 406 | Grants access to access layer with more functionality and data | ||
| 407 | (default is basic layer) | ||
| 408 | -U: Basic authentication username | ||
| 409 | For a Norid registrar RDAP user where: | ||
| 410 | - RDAP username is : 'rdapuser', | ||
| 411 | - Norid registrar id is: 'reg1234' | ||
| 412 | The basic username must be set as follows: 'rdapuser@reg1234'. | ||
| 413 | -P: Basic authentication password | ||
| 414 | |||
| 415 | Authentication options (for Norid usage only): | ||
| 416 | Grants access to access layer with more functionality and data | ||
| 417 | (default is basic layer) | ||
| 418 | -z: Secret for access layer with higher amount of visible data | ||
| 419 | -I: The ip address of the client UA for proper referral rate | ||
| 420 | limiting (default is none) | ||
| 421 | -y: Act as a Norid proxy | ||
| 422 | |||
| 423 | Page control for searches: | ||
| 424 | -F: Fieldset value, undef or one of the valid fieldSets | ||
| 425 | -C: Cursor to be used in first page lookup for a search, undef for | ||
| 426 | first page | ||
| 427 | |||
| 428 | -p: max number of pages from cursor (1..x, default | ||
| 429 | $RDAP_ENTITY_QUOTA_PAGES in rdap lib). | ||
| 430 | Note on page size: | ||
| 431 | The 'pageSize' parameter in RDAP lookup result tells the number of | ||
| 432 | hits per page and cannot be controlled by a client. | ||
| 433 | |||
| 434 | Various: | ||
| 435 | -e: lookup entity instead of doing a domain search | ||
| 436 | -o: present short result ( only when -w not set) | ||
| 437 | -x: expand result, do additional lookups if data is truncated | ||
| 438 | -f: force ipv4 (-f 4) or ipv6 (-f 6) connection, else use what your system picks | ||
| 439 | Note: option added to force mode if you experience DNS/connect problems | ||
| 440 | -l: Use terminal colors | ||
| 441 | |||
| 442 | Format for output of the result: | ||
| 443 | |||
| 444 | -w: dump result in old style Norid whois format (default is a | ||
| 445 | rudimentary text output inspired by rdapper) | ||
| 446 | |||
| 447 | Other: | ||
| 448 | -d: debug | ||
| 449 | 0 : debug off | ||
| 450 | 1 : debug output from this module | ||
| 451 | 2 : headers output from LWP UA callback | ||
| 452 | 3 : Debug output from Net::RDAP::UA (headers and contents) | ||
| 453 | 4-8: Debug output using LWP::ConsoleLogger::Easy, | ||
| 454 | which then must be installed | ||
| 455 | -a: activate Net::RDAP library caching | ||
| 456 | -v: verbose: dump JSON result received from RDAP | ||
| 457 | -h: help | ||
| 458 | |||
| 459 | =cut | ||
| 460 | |||
| 461 | 1; | ||
| 462 | |||
| 463 |