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