| 1 |
/* |
| 2 |
Copyright (C) 1998-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* Written by Marco d'Itri. */ |
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
/* System library */ |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <stdarg.h> |
| 26 |
#include <unistd.h> |
| 27 |
#include <string.h> |
| 28 |
#include <ctype.h> |
| 29 |
#include <sys/types.h> |
| 30 |
#include <sys/socket.h> |
| 31 |
#include <netinet/in.h> |
| 32 |
#include <arpa/inet.h> |
| 33 |
#include <netdb.h> |
| 34 |
#include <errno.h> |
| 35 |
#include <signal.h> |
| 36 |
#include <progname.h> |
| 37 |
#include <argp.h> |
| 38 |
#define obstack_chunk_alloc malloc |
| 39 |
#define obstack_chunk_free free |
| 40 |
#include <obstack.h> |
| 41 |
#include <error.h> |
| 42 |
#include <attribute.h> |
| 43 |
#include <libinetutils.h> |
| 44 |
|
| 45 |
/* Application-specific */ |
| 46 |
#include <data.h> |
| 47 |
#include <whois.h> |
| 48 |
#include "xalloc.h" |
| 49 |
|
| 50 |
/* Global variables */ |
| 51 |
int sockfd, verb = 0; |
| 52 |
struct obstack query_stk; |
| 53 |
|
| 54 |
#ifdef ALWAYS_HIDE_DISCL |
| 55 |
int hide_discl = 0; |
| 56 |
#else |
| 57 |
int hide_discl = 2; |
| 58 |
#endif |
| 59 |
const char *server = NULL; |
| 60 |
const char *port = NULL; |
| 61 |
int nopar = 0; |
| 62 |
|
| 63 |
static struct argp_option ripe_argp_options[] = { |
| 64 |
#define GRP 10 |
| 65 |
{NULL, 'a', NULL, 0, |
| 66 |
"search all databases", GRP}, |
| 67 |
{NULL, 'F', NULL, 0, |
| 68 |
"fast raw output (implies -r)", GRP}, |
| 69 |
{NULL, 'g', "SOURCE:FIRST-LAST", 0, |
| 70 |
"find updates from SOURCE from serial FIRST to LAST", GRP}, |
| 71 |
{NULL, 'i', "ATTR[,ATTR]...", 0, |
| 72 |
"do an inverse lookup for specified ATTRibutes", GRP}, |
| 73 |
{NULL, 'l', NULL, 0, |
| 74 |
"one level less specific lookup (RPSL only)", GRP}, |
| 75 |
{NULL, 'L', NULL, 0, |
| 76 |
"find all Less specific matches", GRP}, |
| 77 |
{NULL, 'M', NULL, 0, |
| 78 |
"find all More specific matches", GRP}, |
| 79 |
{NULL, 'm', NULL, 0, |
| 80 |
"find first level more specific matches", GRP}, |
| 81 |
{NULL, 'r', NULL, 0, |
| 82 |
"turn off recursive lookups", GRP}, |
| 83 |
{NULL, 'R', NULL, 0, |
| 84 |
"force to show local copy of the domain object even " |
| 85 |
"if it contains referral", GRP}, |
| 86 |
{NULL, 'S', NULL, 0, |
| 87 |
"tell server to leave out syntactic sugar", GRP}, |
| 88 |
{NULL, 's', "SOURCE[,SOURCE]...", 0, |
| 89 |
"search the database from SOURCE", GRP}, |
| 90 |
{NULL, 'T', "TYPE[,TYPE]...", 0, |
| 91 |
"only look for objects of TYPE", GRP}, |
| 92 |
{NULL, 'q', "version|sources", 0, |
| 93 |
"query specified server info (RPSL only)", GRP}, |
| 94 |
{NULL, 't', "TYPE", 0, |
| 95 |
"requests template for object of TYPE ('all' for a list)", GRP}, |
| 96 |
{NULL, 'x', NULL, 0, |
| 97 |
"exact match only (RPSL only)", GRP}, |
| 98 |
#undef GRP |
| 99 |
{NULL, 0, NULL, 0, NULL, 0} |
| 100 |
}; |
| 101 |
|
| 102 |
static error_t |
| 103 |
ripe_argp_parser (int key, char *arg, struct argp_state *state MAYBE_UNUSED) |
| 104 |
{ |
| 105 |
if (key > 0 && (unsigned) key < 128) |
| 106 |
{ |
| 107 |
if (key == 't' || key == 'v' || key == 'q') |
| 108 |
nopar = 1; |
| 109 |
|
| 110 |
obstack_1grow (&query_stk, '-'); |
| 111 |
obstack_1grow (&query_stk, key); |
| 112 |
if (arg) |
| 113 |
{ |
| 114 |
obstack_1grow (&query_stk, ' '); |
| 115 |
obstack_grow (&query_stk, arg, strlen (arg)); |
| 116 |
} |
| 117 |
return 0; |
| 118 |
} |
| 119 |
return ARGP_ERR_UNKNOWN; |
| 120 |
} |
| 121 |
|
| 122 |
static struct argp ripe_argp = |
| 123 |
{ ripe_argp_options, ripe_argp_parser, NULL, NULL, NULL, NULL, NULL }; |
| 124 |
|
| 125 |
static struct argp_option gwhois_argp_options[] = { |
| 126 |
#define GRP 20 |
| 127 |
{NULL, 0, NULL, 0, "General options", GRP}, |
| 128 |
{"verbose", 'V', NULL, 0, |
| 129 |
"explain what is being done", GRP}, |
| 130 |
{"server", 'h', "HOST", 0, |
| 131 |
"connect to server HOST", GRP}, |
| 132 |
{"port", 'p', "PORT", 0, |
| 133 |
"connect to PORT", GRP}, |
| 134 |
{NULL, 'H', NULL, 0, |
| 135 |
"hide legal disclaimers", GRP}, |
| 136 |
#undef GRP |
| 137 |
{NULL, 0, NULL, 0, NULL, 0} |
| 138 |
}; |
| 139 |
|
| 140 |
static error_t |
| 141 |
gwhois_argp_parser (int key, char *arg, struct argp_state *state MAYBE_UNUSED) |
| 142 |
{ |
| 143 |
char *p, *q; |
| 144 |
|
| 145 |
switch (key) |
| 146 |
{ |
| 147 |
case 'h': |
| 148 |
server = q = xmalloc (strlen (arg) + 1); |
| 149 |
for (p = arg; *p != '\0' && *p != ':'; *q++ = tolower (*p++)); |
| 150 |
if (*p == ':') |
| 151 |
port = p + 1; |
| 152 |
*q = '\0'; |
| 153 |
break; |
| 154 |
|
| 155 |
case 'H': |
| 156 |
hide_discl = 0; /* enable disclaimers hiding */ |
| 157 |
break; |
| 158 |
|
| 159 |
case 'p': |
| 160 |
port = arg; |
| 161 |
break; |
| 162 |
|
| 163 |
case 'V': |
| 164 |
verb = 1; |
| 165 |
break; |
| 166 |
|
| 167 |
default: |
| 168 |
return ARGP_ERR_UNKNOWN; |
| 169 |
} |
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
struct argp_child gwhois_argp_children[] = { |
| 174 |
{&ripe_argp, |
| 175 |
0, |
| 176 |
"RIPE-specific options", |
| 177 |
0}, |
| 178 |
{NULL, 0, NULL, 0} |
| 179 |
}; |
| 180 |
|
| 181 |
static struct argp gwhois_argp = { |
| 182 |
gwhois_argp_options, |
| 183 |
gwhois_argp_parser, |
| 184 |
"OBJECT...", |
| 185 |
"client for the whois directory service", |
| 186 |
gwhois_argp_children, |
| 187 |
NULL, |
| 188 |
NULL |
| 189 |
}; |
| 190 |
|
| 191 |
const char *program_authors[] = { |
| 192 |
"Marco d'Itri", |
| 193 |
NULL |
| 194 |
}; |
| 195 |
|
| 196 |
int |
| 197 |
main (int argc, char *argv[]) |
| 198 |
{ |
| 199 |
int index; |
| 200 |
char *fstring; |
| 201 |
char *qstring; |
| 202 |
char *p; |
| 203 |
|
| 204 |
set_program_name (argv[0]); |
| 205 |
|
| 206 |
#ifdef ENABLE_NLS |
| 207 |
setlocale (LC_MESSAGES, ""); |
| 208 |
bindtextdomain (NLS_CAT_NAME, LOCALEDIR); |
| 209 |
textdomain (NLS_CAT_NAME); |
| 210 |
#endif |
| 211 |
|
| 212 |
obstack_init (&query_stk); |
| 213 |
iu_argp_init ("whois", program_authors); |
| 214 |
argp_parse (&gwhois_argp, argc, argv, ARGP_IN_ORDER, &index, NULL); |
| 215 |
obstack_1grow (&query_stk, 0); |
| 216 |
fstring = obstack_finish (&query_stk); |
| 217 |
argc -= index; |
| 218 |
argv += index; |
| 219 |
|
| 220 |
if (argc == 0 && !nopar) /* there is no parameter */ |
| 221 |
error (EXIT_FAILURE, 0, "not enough arguments"); |
| 222 |
|
| 223 |
/* parse other parameters, if any */ |
| 224 |
if (!nopar) |
| 225 |
{ |
| 226 |
while (argc--) |
| 227 |
{ |
| 228 |
const char *arg = *argv++; |
| 229 |
obstack_grow (&query_stk, arg, strlen (arg)); |
| 230 |
if (argc) |
| 231 |
obstack_1grow (&query_stk, ' '); |
| 232 |
} |
| 233 |
} |
| 234 |
obstack_1grow (&query_stk, 0); |
| 235 |
qstring = obstack_finish (&query_stk); |
| 236 |
|
| 237 |
if (!server && domfind (qstring, gtlds)) |
| 238 |
{ |
| 239 |
if (verb) |
| 240 |
puts (_("Connecting to whois.internic.net.")); |
| 241 |
sockfd = openconn ("whois.internic.net", NULL); |
| 242 |
server = query_crsnic (sockfd, qstring); |
| 243 |
closeconn (sockfd); |
| 244 |
if (!server) |
| 245 |
exit (EXIT_SUCCESS); |
| 246 |
printf (_("\nFound InterNIC referral to %s.\n\n"), server); |
| 247 |
} |
| 248 |
|
| 249 |
if (!server) |
| 250 |
{ |
| 251 |
server = whichwhois (qstring); |
| 252 |
switch (server[0]) |
| 253 |
{ |
| 254 |
case 0: |
| 255 |
if (!(server = getenv ("WHOIS_SERVER"))) |
| 256 |
server = DEFAULTSERVER; |
| 257 |
if (verb) |
| 258 |
printf (_("Using default server %s.\n"), server); |
| 259 |
break; |
| 260 |
case 1: |
| 261 |
puts (_("This TLD has no whois server, but you can access the " |
| 262 |
"whois database at")); |
| 263 |
/* FALLTHROUGH */ |
| 264 |
case 2: |
| 265 |
puts (server + 1); |
| 266 |
exit (EXIT_SUCCESS); |
| 267 |
case 3: |
| 268 |
puts (_("This TLD has no whois server.")); |
| 269 |
exit (EXIT_SUCCESS); |
| 270 |
default: |
| 271 |
if (verb) |
| 272 |
printf (_("Using server %s.\n"), server); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
if (getenv ("WHOIS_HIDE")) |
| 277 |
hide_discl = 0; |
| 278 |
|
| 279 |
p = queryformat (server, fstring, qstring); |
| 280 |
if (verb) |
| 281 |
printf (_("Query string: \"%s\"\n\n"), p); |
| 282 |
strcat (p, "\r\n"); |
| 283 |
|
| 284 |
signal (SIGTERM, sighandler); |
| 285 |
signal (SIGINT, sighandler); |
| 286 |
|
| 287 |
sockfd = openconn (server, port); |
| 288 |
do_query (sockfd, p); |
| 289 |
closeconn (sockfd); |
| 290 |
|
| 291 |
exit (EXIT_SUCCESS); |
| 292 |
} |
| 293 |
|
| 294 |
const char * |
| 295 |
whichwhois (const char *s) |
| 296 |
{ |
| 297 |
unsigned long ip; |
| 298 |
unsigned int i; |
| 299 |
|
| 300 |
/* Step over any single glyph preamble, as it must be some |
| 301 |
* kind of informed instructions for an anticipated server. |
| 302 |
*/ |
| 303 |
while ((*s != '\0') && (*(s + 1) != '\0') && (*(s + 1) == ' ')) |
| 304 |
s += 2; |
| 305 |
|
| 306 |
/* -v or -t has been used */ |
| 307 |
if (*s == '\0') |
| 308 |
return "whois.ripe.net"; |
| 309 |
|
| 310 |
/* email address */ |
| 311 |
if (strchr (s, '@')) |
| 312 |
return ""; |
| 313 |
|
| 314 |
/* no dot, no colon, and no hyphen means it's an NSI NIC handle or ASN (?) */ |
| 315 |
if (!strpbrk (s, ".-:")) |
| 316 |
{ |
| 317 |
const char *p; |
| 318 |
|
| 319 |
for (p = s; *p != '\0'; p++); /* go to the end of s */ |
| 320 |
if (strncasecmp (s, "as", 2) == 0 && /* it's an AS */ |
| 321 |
((s[2] >= '0' && s[2] <= '9') || s[2] == ' ')) |
| 322 |
return whereas (atoi (s + 2), as_assign); |
| 323 |
else if (strncasecmp (p - 2, "jp", 2) == 0) /* JP NIC handle */ |
| 324 |
return "whois.nic.ad.jp"; |
| 325 |
if (*p == '!') /* NSI NIC handle */ |
| 326 |
return "whois.networksolutions.com"; |
| 327 |
else /* it's a NSI NIC handle or something we don't know about */ |
| 328 |
return ""; |
| 329 |
} |
| 330 |
|
| 331 |
/* IPv6 address */ |
| 332 |
if (strchr (s, ':')) |
| 333 |
{ |
| 334 |
int a = 0, b = 0; |
| 335 |
|
| 336 |
/* Only the first two double-octets are ever scanned. |
| 337 |
* At least one is required. |
| 338 |
*/ |
| 339 |
if (sscanf (s, "%x:%x:", &a, &b) == 0) |
| 340 |
return ""; |
| 341 |
|
| 342 |
/* Mask off any non-significant bits and generate prefix. */ |
| 343 |
ip = (a & 0xffff) << 16 | (b & 0xffff); |
| 344 |
|
| 345 |
for (i = 0; ip6_assign[i].serv; i++) |
| 346 |
if ((ip & ip6_assign[i].mask) == ip6_assign[i].net) |
| 347 |
return ip6_assign[i].serv; |
| 348 |
|
| 349 |
if (verb) |
| 350 |
puts (_("Delegation server of this address is not known.\n" |
| 351 |
"The fallback is ARIN, hoping for success.")); |
| 352 |
|
| 353 |
return "whois.arin.net"; |
| 354 |
} |
| 355 |
|
| 356 |
/* smells like an IP? */ |
| 357 |
if ((ip = myinet_aton (s))) |
| 358 |
{ |
| 359 |
for (i = 0; ip_assign[i].serv; i++) |
| 360 |
if ((ip & ip_assign[i].mask) == ip_assign[i].net) |
| 361 |
return ip_assign[i].serv; |
| 362 |
if (verb) |
| 363 |
puts (_("I don't know where this IP has been delegated.\n" |
| 364 |
"I'll try ARIN and hope for the best...")); |
| 365 |
return "whois.arin.net"; |
| 366 |
} |
| 367 |
|
| 368 |
/* check TLD list */ |
| 369 |
for (i = 0; tld_serv[i]; i += 2) |
| 370 |
if (domcmp (s, tld_serv[i])) |
| 371 |
return tld_serv[i + 1]; |
| 372 |
|
| 373 |
/* no dot but hyphen */ |
| 374 |
if (!strchr (s, '.')) |
| 375 |
{ |
| 376 |
/* search for strings at the start of the word */ |
| 377 |
for (i = 0; nic_handles[i]; i += 2) |
| 378 |
if (strncasecmp (s, nic_handles[i], strlen (nic_handles[i])) == 0) |
| 379 |
return nic_handles[i + 1]; |
| 380 |
if (verb) |
| 381 |
puts (_("I guess it's a netblock name but I don't know where to" |
| 382 |
" look it up.")); |
| 383 |
return "whois.arin.net"; |
| 384 |
} |
| 385 |
|
| 386 |
/* has dot and hyphen and it's not in tld_serv[], WTF is it? */ |
| 387 |
if (verb) |
| 388 |
puts (_("I guess it's a domain but I don't know where to look it" |
| 389 |
" up.")); |
| 390 |
return ""; |
| 391 |
} |
| 392 |
|
| 393 |
const char * |
| 394 |
whereas (int asn, struct as_del aslist[]) |
| 395 |
{ |
| 396 |
int i; |
| 397 |
|
| 398 |
if (asn > 398287) |
| 399 |
puts (_("Unknown AS number. Please upgrade this program.")); |
| 400 |
for (i = 0; aslist[i].serv; i++) |
| 401 |
if (asn >= aslist[i].first && asn <= aslist[i].last) |
| 402 |
return aslist[i].serv; |
| 403 |
return "whois.arin.net"; |
| 404 |
} |
| 405 |
|
| 406 |
int |
| 407 |
is_ripe_server (const char *const *srvtab, const char *name) |
| 408 |
{ |
| 409 |
struct in_addr addr; |
| 410 |
int isip = 0; |
| 411 |
|
| 412 |
isip = inet_aton (name, &addr); |
| 413 |
|
| 414 |
for (; *srvtab; ++srvtab) |
| 415 |
{ |
| 416 |
const char *server = *srvtab; |
| 417 |
struct hostent *hp; |
| 418 |
|
| 419 |
if (strcmp (server, name) == 0) |
| 420 |
return 1; |
| 421 |
if (isip && (hp = gethostbyname (server)) != NULL) |
| 422 |
{ |
| 423 |
char **pa; |
| 424 |
for (pa = hp->h_addr_list; *pa; pa++) |
| 425 |
if (*(unsigned long *) *pa == addr.s_addr) |
| 426 |
return 1; |
| 427 |
} |
| 428 |
} |
| 429 |
return 0; |
| 430 |
} |
| 431 |
|
| 432 |
char * |
| 433 |
queryformat (const char *server, const char *flags, const char *query) |
| 434 |
{ |
| 435 |
char *buf; |
| 436 |
int isripe = 0; |
| 437 |
|
| 438 |
/* +10 for CORE; +2 for \r\n; +1 for NULL */ |
| 439 |
buf = xmalloc (strlen (flags) + strlen (query) + 10 + 2 + 1); |
| 440 |
*buf = '\0'; |
| 441 |
|
| 442 |
isripe = is_ripe_server (ripe_servers, server) |
| 443 |
|| is_ripe_server (ripe_servers_old, server); |
| 444 |
if (isripe) |
| 445 |
strcat (buf, "-V" IDSTRING " "); |
| 446 |
|
| 447 |
if (*flags != '\0') |
| 448 |
{ |
| 449 |
if (!isripe && strcmp (server, "whois.corenic.net") != 0) |
| 450 |
puts (_("Warning: RIPE flags ignored for a traditional server.")); |
| 451 |
else |
| 452 |
strcat (buf, flags); |
| 453 |
} |
| 454 |
if (!isripe && |
| 455 |
(strcmp (server, "whois.arin.net") == 0 || |
| 456 |
strcmp (server, "whois.nic.mil") == 0) && |
| 457 |
strncasecmp (query, "AS", 2) == 0 && query[2] >= '0' && query[2] <= '9') |
| 458 |
sprintf (buf, "a %s", query + 2); /* fix query for ARIN */ |
| 459 |
else if (!isripe && strcmp (server, "whois.corenic.net") == 0) |
| 460 |
sprintf (buf, "--machine %s", query); /* machine readable output */ |
| 461 |
else if (!isripe && strcmp (server, "whois.ncst.ernet.in") == 0 && |
| 462 |
!strchr (query, ' ')) |
| 463 |
sprintf (buf, "domain %s", query); /* ask for a domain */ |
| 464 |
else if (!isripe && strcmp (server, "whois.nic.ad.jp") == 0) |
| 465 |
{ |
| 466 |
char *lang = getenv ("LANG"); /* not a perfect check, but... */ |
| 467 |
if (!lang || (strncmp (lang, "ja", 2) != 0)) |
| 468 |
sprintf (buf, "%s/e", query); /* ask for english text */ |
| 469 |
else |
| 470 |
strcat (buf, query); |
| 471 |
} |
| 472 |
else |
| 473 |
strcat (buf, query); |
| 474 |
return buf; |
| 475 |
} |
| 476 |
|
| 477 |
void |
| 478 |
do_query (const int sock, const char *query) |
| 479 |
{ |
| 480 |
char buf[200], *p; |
| 481 |
FILE *fi; |
| 482 |
int i = 0, hide = hide_discl; |
| 483 |
|
| 484 |
fi = fdopen (sock, "r"); |
| 485 |
if (write (sock, query, strlen (query)) < 0) |
| 486 |
err_sys ("write"); |
| 487 |
|
| 488 |
while (fgets (buf, 200, fi)) |
| 489 |
{ /* XXX errors? */ |
| 490 |
if (hide == 1) |
| 491 |
{ |
| 492 |
if (strncmp (buf, hide_strings[i + 1], strlen (hide_strings[i + 1])) |
| 493 |
== 0) |
| 494 |
hide = 0; /* go into testing mode */ |
| 495 |
continue; /* hide this line */ |
| 496 |
} |
| 497 |
if (hide == 0) |
| 498 |
{ |
| 499 |
for (i = 0; hide_strings[i] != NULL; i += 2) |
| 500 |
{ |
| 501 |
if (strncmp (buf, hide_strings[i], strlen (hide_strings[i])) == |
| 502 |
0) |
| 503 |
{ |
| 504 |
hide = 1; /* start hiding */ |
| 505 |
break; |
| 506 |
} |
| 507 |
} |
| 508 |
if (hide == 1) |
| 509 |
continue; /* hide the first line */ |
| 510 |
} |
| 511 |
#ifdef EXT_6BONE |
| 512 |
/* % referto: whois -h whois.arin.net -p 43 as 1 */ |
| 513 |
if (strncmp (buf, "% referto:", 10) == 0) |
| 514 |
{ |
| 515 |
char nh[256], np[16], nq[1024]; |
| 516 |
|
| 517 |
if (sscanf (buf, REFERTO_FORMAT, nh, np, nq) == 3) |
| 518 |
{ |
| 519 |
int fd; |
| 520 |
|
| 521 |
if (verb) |
| 522 |
printf (_("Detected referral to %s on %s.\n"), nq, nh); |
| 523 |
strcat (nq, "\r\n"); |
| 524 |
fd = openconn (nh, np); |
| 525 |
do_query (fd, nq); |
| 526 |
closeconn (fd); |
| 527 |
continue; |
| 528 |
} |
| 529 |
} |
| 530 |
#endif |
| 531 |
for (p = buf; *p && *p != '\r' && *p != '\n'; p++); |
| 532 |
*p = '\0'; |
| 533 |
fprintf (stdout, "%s\n", buf); |
| 534 |
} |
| 535 |
if (ferror (fi)) |
| 536 |
err_sys ("fgets"); |
| 537 |
|
| 538 |
if (hide == 1) |
| 539 |
err_quit (_("Catastrophic error: disclaimer text has been changed.\n" |
| 540 |
"Please upgrade this program.\n")); |
| 541 |
} |
| 542 |
|
| 543 |
const char * |
| 544 |
query_crsnic (const int sock, const char *query) |
| 545 |
{ |
| 546 |
char *temp, buf[100], *ret = NULL; |
| 547 |
FILE *fi; |
| 548 |
|
| 549 |
temp = xmalloc (strlen (query) + 1 + 2 + 1); |
| 550 |
*temp = '='; |
| 551 |
strcpy (temp + 1, query); |
| 552 |
strcat (temp, "\r\n"); |
| 553 |
|
| 554 |
fi = fdopen (sock, "r"); |
| 555 |
if (write (sock, temp, strlen (temp)) < 0) |
| 556 |
err_sys ("write"); |
| 557 |
|
| 558 |
while (fgets (buf, 100, fi)) |
| 559 |
{ |
| 560 |
/* If there are multiple matches only the server of the first record |
| 561 |
is queried */ |
| 562 |
if (strncmp (buf, " Whois Server:", 16) == 0 && !ret) |
| 563 |
{ |
| 564 |
char *p, *q; |
| 565 |
|
| 566 |
for (p = buf; *p != ':'; p++); /* skip until colon */ |
| 567 |
for (p++; *p == ' '; p++); /* skip colon and spaces */ |
| 568 |
ret = xmalloc (strlen (p) + 1); |
| 569 |
for (q = ret; *p != '\n' && *p != '\r'; *q++ = *p++); /*copy data */ |
| 570 |
*q = '\0'; |
| 571 |
} |
| 572 |
fputs (buf, stdout); |
| 573 |
} |
| 574 |
if (ferror (fi)) |
| 575 |
err_sys ("fgets"); |
| 576 |
|
| 577 |
free (temp); |
| 578 |
return ret; |
| 579 |
} |
| 580 |
|
| 581 |
int |
| 582 |
openconn (const char *server, const char *port) |
| 583 |
{ |
| 584 |
int fd; |
| 585 |
int i; |
| 586 |
struct addrinfo hints, *res, *ressave; |
| 587 |
|
| 588 |
memset (&hints, 0, sizeof (struct addrinfo)); |
| 589 |
hints.ai_family = AF_UNSPEC; |
| 590 |
hints.ai_socktype = SOCK_STREAM; |
| 591 |
|
| 592 |
/* The well-known service name is no longer universally acknowledged, |
| 593 |
* because IANA has tampered with aliasing, making `Who' and `Is' |
| 594 |
* into legal and functional aliases. |
| 595 |
*/ |
| 596 |
if ((i = getaddrinfo (server, port ? port : "whois", &hints, &res)) != 0) |
| 597 |
if ((i = |
| 598 |
getaddrinfo (server, port ? port : "nicname", &hints, &res)) != 0) |
| 599 |
err_quit ("getaddrinfo: %s", gai_strerror (i)); |
| 600 |
|
| 601 |
for (ressave = res; res; res = res->ai_next) |
| 602 |
{ |
| 603 |
if ((fd = |
| 604 |
socket (res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) |
| 605 |
continue; /* ignore */ |
| 606 |
if (connect (fd, res->ai_addr, res->ai_addrlen) == 0) |
| 607 |
break; /* success */ |
| 608 |
close (fd); |
| 609 |
} |
| 610 |
|
| 611 |
if (!res) |
| 612 |
err_sys ("connect"); |
| 613 |
freeaddrinfo (ressave); |
| 614 |
return (fd); |
| 615 |
} |
| 616 |
|
| 617 |
void |
| 618 |
closeconn (const int fd) |
| 619 |
{ |
| 620 |
close (fd); |
| 621 |
} |
| 622 |
|
| 623 |
void |
| 624 |
sighandler (int signum) |
| 625 |
{ |
| 626 |
closeconn (sockfd); |
| 627 |
err_quit (_("Interrupted by signal %d..."), signum); |
| 628 |
} |
| 629 |
|
| 630 |
/* check if dom ends with tld */ |
| 631 |
int |
| 632 |
domcmp (const char *dom, const char *tld) |
| 633 |
{ |
| 634 |
const char *p, *q; |
| 635 |
|
| 636 |
for (p = dom; *p != '\0'; p++); |
| 637 |
p--; /* move to the last char */ |
| 638 |
for (q = tld; *q != '\0'; q++); |
| 639 |
q--; |
| 640 |
while (p >= dom && q >= tld && tolower (*p) == *q) |
| 641 |
{ /* compare backwards */ |
| 642 |
if (q == tld) /* start of the second word? */ |
| 643 |
return 1; |
| 644 |
p--; |
| 645 |
q--; |
| 646 |
} |
| 647 |
return 0; |
| 648 |
} |
| 649 |
|
| 650 |
/* check if dom ends with an element of tldlist[] */ |
| 651 |
int |
| 652 |
domfind (const char *dom, const char *tldlist[]) |
| 653 |
{ |
| 654 |
int i; |
| 655 |
|
| 656 |
for (i = 0; tldlist[i]; i++) |
| 657 |
if (domcmp (dom, tldlist[i])) |
| 658 |
return 1; |
| 659 |
return 0; |
| 660 |
} |
| 661 |
|
| 662 |
unsigned long |
| 663 |
myinet_aton (const char *s) |
| 664 |
{ |
| 665 |
int a, b, c, d; |
| 666 |
|
| 667 |
if (!s) |
| 668 |
return 0; |
| 669 |
if (sscanf (s, "%d.%d.%d.%d", &a, &b, &c, &d) != 4) |
| 670 |
return 0; |
| 671 |
return (a << 24) + (b << 16) + (c << 8) + d; |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
/* Error routines */ |
| 676 |
void |
| 677 |
err_sys (const char *fmt, ...) |
| 678 |
{ |
| 679 |
va_list ap; |
| 680 |
|
| 681 |
va_start (ap, fmt); |
| 682 |
vfprintf (stderr, fmt, ap); |
| 683 |
fprintf (stderr, ": %s\n", strerror (errno)); |
| 684 |
va_end (ap); |
| 685 |
exit (EXIT_FAILURE); |
| 686 |
} |
| 687 |
|
| 688 |
void |
| 689 |
err_quit (const char *fmt, ...) |
| 690 |
{ |
| 691 |
va_list ap; |
| 692 |
|
| 693 |
va_start (ap, fmt); |
| 694 |
vfprintf (stderr, fmt, ap); |
| 695 |
fputs ("\n", stderr); |
| 696 |
va_end (ap); |
| 697 |
exit (EXIT_FAILURE); |
| 698 |
} |