]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/whois/whois.c
Merge bmake-20160512
[FreeBSD/FreeBSD.git] / usr.bin / whois / whois.c
1 /*-
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1993\n\
33         The Regents of the University of California.  All rights reserved.\n";
34 #endif /* not lint */
35
36 #if 0
37 #ifndef lint
38 static char sccsid[] = "@(#)whois.c     8.1 (Berkeley) 6/6/93";
39 #endif /* not lint */
40 #endif
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/poll.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <ctype.h>
51 #include <err.h>
52 #include <netdb.h>
53 #include <stdarg.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <sysexits.h>
58 #include <unistd.h>
59 #include <fcntl.h>
60 #include <errno.h>
61
62 #define ABUSEHOST       "whois.abuse.net"
63 #define ANICHOST        "whois.arin.net"
64 #define DENICHOST       "whois.denic.de"
65 #define DKNICHOST       "whois.dk-hostmaster.dk"
66 #define FNICHOST        "whois.afrinic.net"
67 #define GNICHOST        "whois.nic.gov"
68 #define IANAHOST        "whois.iana.org"
69 #define INICHOST        "whois.internic.net"
70 #define KNICHOST        "whois.krnic.net"
71 #define LNICHOST        "whois.lacnic.net"
72 #define MNICHOST        "whois.ra.net"
73 #define PDBHOST         "whois.peeringdb.com"
74 #define PNICHOST        "whois.apnic.net"
75 #define QNICHOST_TAIL   ".whois-servers.net"
76 #define RNICHOST        "whois.ripe.net"
77 #define VNICHOST        "whois.verisign-grs.com"
78
79 #define DEFAULT_PORT    "whois"
80
81 #define WHOIS_RECURSE   0x01
82 #define WHOIS_QUICK     0x02
83 #define WHOIS_SPAM_ME   0x04
84
85 #define CHOPSPAM        ">>> Last update of WHOIS database:"
86
87 #define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
88
89 #define SCAN(p, end, check)                                     \
90         while ((p) < (end))                                     \
91                 if (check) ++(p);                               \
92                 else break
93
94 static struct {
95         const char *suffix, *server;
96 } whoiswhere[] = {
97         /* Various handles */
98         { "-ARIN", ANICHOST },
99         { "-NICAT", "at" QNICHOST_TAIL },
100         { "-NORID", "no" QNICHOST_TAIL },
101         { "-RIPE", RNICHOST },
102         /* Nominet's whois server doesn't return referrals to JANET */
103         { ".ac.uk", "ac.uk" QNICHOST_TAIL },
104         { "", IANAHOST }, /* default */
105         { NULL, NULL } /* safety belt */
106 };
107
108 #define WHOIS_REFERRAL(s) { s, sizeof(s) - 1 }
109 static struct {
110         const char *prefix;
111         size_t len;
112 } whois_referral[] = {
113         WHOIS_REFERRAL("whois:"), /* IANA */
114         WHOIS_REFERRAL("Whois Server:"),
115         WHOIS_REFERRAL("Registrar WHOIS Server:"), /* corporatedomains.com */
116         WHOIS_REFERRAL("ReferralServer:  whois://"), /* ARIN */
117         WHOIS_REFERRAL("descr:          region. Please query"), /* AfriNIC */
118         { NULL, 0 }
119 };
120
121 static const char *actually_arin[] = {
122         "netname:        ERX-NETBLOCK\n", /* APNIC */
123         "netname:        NON-RIPE-NCC-MANAGED-ADDRESS-BLOCK\n",
124         NULL
125 };
126
127 static const char *port = DEFAULT_PORT;
128
129 static const char *choose_server(char *);
130 static struct addrinfo *gethostinfo(char const *host, int exitnoname);
131 static void s_asprintf(char **ret, const char *format, ...) __printflike(2, 3);
132 static void usage(void);
133 static void whois(const char *, const char *, int);
134
135 int
136 main(int argc, char *argv[])
137 {
138         const char *country, *host;
139         int ch, flags;
140
141 #ifdef  SOCKS
142         SOCKSinit(argv[0]);
143 #endif
144
145         country = host = NULL;
146         flags = 0;
147         while ((ch = getopt(argc, argv, "aAbc:fgh:iIklmp:PQrRS")) != -1) {
148                 switch (ch) {
149                 case 'a':
150                         host = ANICHOST;
151                         break;
152                 case 'A':
153                         host = PNICHOST;
154                         break;
155                 case 'b':
156                         host = ABUSEHOST;
157                         break;
158                 case 'c':
159                         country = optarg;
160                         break;
161                 case 'f':
162                         host = FNICHOST;
163                         break;
164                 case 'g':
165                         host = GNICHOST;
166                         break;
167                 case 'h':
168                         host = optarg;
169                         break;
170                 case 'i':
171                         host = INICHOST;
172                         break;
173                 case 'I':
174                         host = IANAHOST;
175                         break;
176                 case 'k':
177                         host = KNICHOST;
178                         break;
179                 case 'l':
180                         host = LNICHOST;
181                         break;
182                 case 'm':
183                         host = MNICHOST;
184                         break;
185                 case 'p':
186                         port = optarg;
187                         break;
188                 case 'P':
189                         host = PDBHOST;
190                         break;
191                 case 'Q':
192                         flags |= WHOIS_QUICK;
193                         break;
194                 case 'r':
195                         host = RNICHOST;
196                         break;
197                 case 'R':
198                         flags |= WHOIS_RECURSE;
199                         break;
200                 case 'S':
201                         flags |= WHOIS_SPAM_ME;
202                         break;
203                 case '?':
204                 default:
205                         usage();
206                         /* NOTREACHED */
207                 }
208         }
209         argc -= optind;
210         argv += optind;
211
212         if (!argc || (country != NULL && host != NULL))
213                 usage();
214
215         /*
216          * If no host or country is specified, rely on referrals from IANA.
217          */
218         if (host == NULL && country == NULL) {
219                 if ((host = getenv("WHOIS_SERVER")) == NULL &&
220                     (host = getenv("RA_SERVER")) == NULL) {
221                         if (!(flags & WHOIS_QUICK))
222                                 flags |= WHOIS_RECURSE;
223                 }
224         }
225         while (argc-- > 0) {
226                 if (country != NULL) {
227                         char *qnichost;
228                         s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
229                         whois(*argv, qnichost, flags);
230                         free(qnichost);
231                 } else
232                         whois(*argv, host != NULL ? host :
233                               choose_server(*argv), flags);
234                 argv++;
235         }
236         exit(0);
237 }
238
239 static const char *
240 choose_server(char *domain)
241 {
242         size_t len = strlen(domain);
243         int i;
244
245         for (i = 0; whoiswhere[i].suffix != NULL; i++) {
246                 size_t suffix_len = strlen(whoiswhere[i].suffix);
247                 if (len > suffix_len &&
248                     strcasecmp(domain + len - suffix_len,
249                                whoiswhere[i].suffix) == 0)
250                         return (whoiswhere[i].server);
251         }
252         errx(EX_SOFTWARE, "no default whois server");
253 }
254
255 static struct addrinfo *
256 gethostinfo(char const *host, int exit_on_noname)
257 {
258         struct addrinfo hints, *res;
259         int error;
260
261         memset(&hints, 0, sizeof(hints));
262         hints.ai_flags = AI_CANONNAME;
263         hints.ai_family = AF_UNSPEC;
264         hints.ai_socktype = SOCK_STREAM;
265         res = NULL;
266         error = getaddrinfo(host, port, &hints, &res);
267         if (error && (exit_on_noname || error != EAI_NONAME))
268                 err(EX_NOHOST, "%s: %s", host, gai_strerror(error));
269         return (res);
270 }
271
272 /*
273  * Wrapper for asprintf(3) that exits on error.
274  */
275 static void
276 s_asprintf(char **ret, const char *format, ...)
277 {
278         va_list ap;
279
280         va_start(ap, format);
281         if (vasprintf(ret, format, ap) == -1) {
282                 va_end(ap);
283                 err(EX_OSERR, "vasprintf()");
284         }
285         va_end(ap);
286 }
287
288 static int
289 connect_to_any_host(struct addrinfo *hostres)
290 {
291         struct addrinfo *res;
292         nfds_t i, j;
293         size_t count;
294         struct pollfd *fds;
295         int timeout = 180, s = -1;
296
297         for (res = hostres, count = 0; res; res = res->ai_next)
298                 count++;
299         fds = calloc(count, sizeof(*fds));
300         if (fds == NULL)
301                 err(EX_OSERR, "calloc()");
302
303         /*
304          * Traverse the result list elements and make non-block
305          * connection attempts.
306          */
307         count = i = 0;
308         for (res = hostres; res != NULL; res = res->ai_next) {
309                 s = socket(res->ai_family, res->ai_socktype | SOCK_NONBLOCK,
310                     res->ai_protocol);
311                 if (s < 0)
312                         continue;
313                 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
314                         if (errno == EINPROGRESS) {
315                                 /* Add the socket to poll list */
316                                 fds[i].fd = s;
317                                 fds[i].events = POLLERR | POLLHUP |
318                                                 POLLIN | POLLOUT;
319                                 /*
320                                  * From here until a socket connects, the
321                                  * socket fd is owned by the fds[] poll array.
322                                  */
323                                 s = -1;
324                                 count++;
325                                 i++;
326                         } else {
327                                 close(s);
328                                 s = -1;
329
330                                 /*
331                                  * Poll only if we have something to poll,
332                                  * otherwise just go ahead and try next
333                                  * address
334                                  */
335                                 if (count == 0)
336                                         continue;
337                         }
338                 } else
339                         goto done;
340
341                 /*
342                  * If we are at the last address, poll until a connection is
343                  * established or we failed all connection attempts.
344                  */
345                 if (res->ai_next == NULL)
346                         timeout = INFTIM;
347
348                 /*
349                  * Poll the watched descriptors for successful connections:
350                  * if we still have more untried resolved addresses, poll only
351                  * once; otherwise, poll until all descriptors have errors,
352                  * which will be considered as ETIMEDOUT later.
353                  */
354                 do {
355                         int n;
356
357                         n = poll(fds, i, timeout);
358                         if (n == 0) {
359                                 /*
360                                  * No event reported in time.  Try with a
361                                  * smaller timeout (but cap at 2-3ms)
362                                  * after a new host have been added.
363                                  */
364                                 if (timeout >= 3)
365                                         timeout >>= 1;
366
367                                 break;
368                         } else if (n < 0) {
369                                 /*
370                                  * errno here can only be EINTR which we would
371                                  * want to clean up and bail out.
372                                  */
373                                 s = -1;
374                                 goto done;
375                         }
376
377                         /*
378                          * Check for the event(s) we have seen.
379                          */
380                         for (j = 0; j < i; j++) {
381                                 if (fds[j].fd == -1 || fds[j].events == 0 ||
382                                     fds[j].revents == 0)
383                                         continue;
384                                 if (fds[j].revents & ~(POLLIN | POLLOUT)) {
385                                         close(fds[j].fd);
386                                         fds[j].fd = -1;
387                                         fds[j].events = 0;
388                                         count--;
389                                         continue;
390                                 } else if (fds[j].revents & (POLLIN | POLLOUT)) {
391                                         /* Connect succeeded. */
392                                         s = fds[j].fd;
393                                         fds[j].fd = -1;
394
395                                         goto done;
396                                 }
397
398                         }
399                 } while (timeout == INFTIM && count != 0);
400         }
401
402         /* All attempts were failed */
403         s = -1;
404         if (count == 0)
405                 errno = ETIMEDOUT;
406
407 done:
408         /* Close all watched fds except the succeeded one */
409         for (j = 0; j < i; j++)
410                 if (fds[j].fd != -1)
411                         close(fds[j].fd);
412         free(fds);
413         return (s);
414 }
415
416 static void
417 whois(const char *query, const char *hostname, int flags)
418 {
419         FILE *fp;
420         struct addrinfo *hostres;
421         char *buf, *host, *nhost, *p;
422         int s, f;
423         size_t len, i;
424
425         hostres = gethostinfo(hostname, 1);
426         s = connect_to_any_host(hostres);
427         if (s == -1)
428                 err(EX_OSERR, "connect()");
429
430         /* Restore default blocking behavior.  */
431         if ((f = fcntl(s, F_GETFL)) == -1)
432                 err(EX_OSERR, "fcntl()");
433         f &= ~O_NONBLOCK;
434         if (fcntl(s, F_SETFL, f) == -1)
435                 err(EX_OSERR, "fcntl()");
436
437         fp = fdopen(s, "r+");
438         if (fp == NULL)
439                 err(EX_OSERR, "fdopen()");
440
441         if (!(flags & WHOIS_SPAM_ME) &&
442             (strcasecmp(hostname, DENICHOST) == 0 ||
443              strcasecmp(hostname, "de" QNICHOST_TAIL) == 0)) {
444                 const char *q;
445                 int idn = 0;
446                 for (q = query; *q != '\0'; q++)
447                         if (!isascii(*q))
448                                 idn = 1;
449                 fprintf(fp, "-T dn%s %s\r\n", idn ? "" : ",ace", query);
450         } else if (!(flags & WHOIS_SPAM_ME) &&
451                    (strcasecmp(hostname, DKNICHOST) == 0 ||
452                     strcasecmp(hostname, "dk" QNICHOST_TAIL) == 0))
453                 fprintf(fp, "--show-handles %s\r\n", query);
454         else if ((flags & WHOIS_SPAM_ME) ||
455                  strchr(query, ' ') != NULL)
456                 fprintf(fp, "%s\r\n", query);
457         else if (strcasecmp(hostname, ANICHOST) == 0) {
458                 if (strncasecmp(query, "AS", 2) == 0 &&
459                     strspn(query+2, "0123456789") == strlen(query+2))
460                         fprintf(fp, "+ a %s\r\n", query+2);
461                 else
462                         fprintf(fp, "+ %s\r\n", query);
463         } else if (strcasecmp(hostres->ai_canonname, VNICHOST) == 0)
464                 fprintf(fp, "domain %s\r\n", query);
465         else
466                 fprintf(fp, "%s\r\n", query);
467         fflush(fp);
468
469         nhost = NULL;
470         while ((buf = fgetln(fp, &len)) != NULL) {
471                 /* Nominet */
472                 if (!(flags & WHOIS_SPAM_ME) &&
473                     len == 5 && strncmp(buf, "-- \r\n", 5) == 0)
474                         break;
475
476                 printf("%.*s", (int)len, buf);
477
478                 if ((flags & WHOIS_RECURSE) && nhost == NULL) {
479                         for (i = 0; whois_referral[i].prefix != NULL; i++) {
480                                 p = buf;
481                                 SCAN(p, buf+len, *p == ' ');
482                                 if (strncasecmp(p, whois_referral[i].prefix,
483                                                    whois_referral[i].len) != 0)
484                                         continue;
485                                 p += whois_referral[i].len;
486                                 SCAN(p, buf+len, *p == ' ');
487                                 host = p;
488                                 SCAN(p, buf+len, ishost(*p));
489                                 /* avoid loops */
490                                 if (strncmp(hostname, host, p - host) != 0)
491                                         s_asprintf(&nhost, "%.*s",
492                                                    (int)(p - host), host);
493                                 break;
494                         }
495                         for (i = 0; actually_arin[i] != NULL; i++) {
496                                 if (strncmp(buf, actually_arin[i], len) == 0) {
497                                         s_asprintf(&nhost, "%s", ANICHOST);
498                                         break;
499                                 }
500                         }
501                 }
502                 /* Verisign etc. */
503                 if (!(flags & WHOIS_SPAM_ME) &&
504                     len >= sizeof(CHOPSPAM)-1 &&
505                     (strncasecmp(buf, CHOPSPAM, sizeof(CHOPSPAM)-1) == 0 ||
506                      strncasecmp(buf, CHOPSPAM+4, sizeof(CHOPSPAM)-5) == 0)) {
507                         printf("\n");
508                         break;
509                 }
510         }
511         fclose(fp);
512         freeaddrinfo(hostres);
513         if (nhost != NULL) {
514                 whois(query, nhost, flags);
515                 free(nhost);
516         }
517 }
518
519 static void
520 usage(void)
521 {
522         fprintf(stderr,
523             "usage: whois [-aAbfgiIklmPQrRS] [-c country-code | -h hostname] "
524             "[-p port] name ...\n");
525         exit(EX_USAGE);
526 }