]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/whois/whois.c
MFhead @ r281974
[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 NICHOST         "whois.crsnic.net"
64 #define INICHOST        "whois.networksolutions.com"
65 #define GNICHOST        "whois.nic.gov"
66 #define ANICHOST        "whois.arin.net"
67 #define LNICHOST        "whois.lacnic.net"
68 #define KNICHOST        "whois.krnic.net"
69 #define RNICHOST        "whois.ripe.net"
70 #define PNICHOST        "whois.apnic.net"
71 #define MNICHOST        "whois.ra.net"
72 #define QNICHOST_TAIL   ".whois-servers.net"
73 #define BNICHOST        "whois.registro.br"
74 #define NORIDHOST       "whois.norid.no"
75 #define IANAHOST        "whois.iana.org"
76 #define GERMNICHOST     "de.whois-servers.net"
77 #define FNICHOST        "whois.afrinic.net"
78 #define DEFAULT_PORT    "whois"
79 #define WHOIS_SERVER_ID "Whois Server: "
80 #define WHOIS_ORG_SERVER_ID     "Registrant Street1:Whois Server:"
81
82 #define WHOIS_RECURSE           0x01
83 #define WHOIS_QUICK             0x02
84
85 #define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
86
87 static const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST,
88                                   FNICHOST, NULL };
89 static const char *port = DEFAULT_PORT;
90
91 static char *choose_server(char *);
92 static struct addrinfo *gethostinfo(char const *host, int exit_on_error);
93 static void s_asprintf(char **ret, const char *format, ...) __printflike(2, 3);
94 static void usage(void);
95 static void whois(const char *, const char *, int);
96
97 int
98 main(int argc, char *argv[])
99 {
100         const char *country, *host;
101         char *qnichost;
102         int ch, flags, use_qnichost;
103
104 #ifdef  SOCKS
105         SOCKSinit(argv[0]);
106 #endif
107
108         country = host = qnichost = NULL;
109         flags = use_qnichost = 0;
110         while ((ch = getopt(argc, argv, "aAbc:fgh:iIklmp:QrR6")) != -1) {
111                 switch (ch) {
112                 case 'a':
113                         host = ANICHOST;
114                         break;
115                 case 'A':
116                         host = PNICHOST;
117                         break;
118                 case 'b':
119                         host = ABUSEHOST;
120                         break;
121                 case 'c':
122                         country = optarg;
123                         break;
124                 case 'f':
125                         host = FNICHOST;
126                         break;
127                 case 'g':
128                         host = GNICHOST;
129                         break;
130                 case 'h':
131                         host = optarg;
132                         break;
133                 case 'i':
134                         host = INICHOST;
135                         break;
136                 case 'I':
137                         host = IANAHOST;
138                         break;
139                 case 'k':
140                         host = KNICHOST;
141                         break;
142                 case 'l':
143                         host = LNICHOST;
144                         break;
145                 case 'm':
146                         host = MNICHOST;
147                         break;
148                 case 'p':
149                         port = optarg;
150                         break;
151                 case 'Q':
152                         flags |= WHOIS_QUICK;
153                         break;
154                 case 'r':
155                         host = RNICHOST;
156                         break;
157                 case 'R':
158                         warnx("-R is deprecated; use '-c ru' instead");
159                         country = "ru";
160                         break;
161                 /* Remove in FreeBSD 10 */
162                 case '6':
163                         errx(EX_USAGE,
164                                 "-6 is deprecated; use -[aAflr] instead");
165                         break;
166                 case '?':
167                 default:
168                         usage();
169                         /* NOTREACHED */
170                 }
171         }
172         argc -= optind;
173         argv += optind;
174
175         if (!argc || (country != NULL && host != NULL))
176                 usage();
177
178         /*
179          * If no host or country is specified determine the top level domain
180          * from the query.  If the TLD is a number, query ARIN.  Otherwise, use 
181          * TLD.whois-server.net.  If the domain does not contain '.', fall
182          * back to NICHOST.
183          */
184         if (host == NULL && country == NULL) {
185                 if ((host = getenv("RA_SERVER")) == NULL) {
186                         use_qnichost = 1;
187                         host = NICHOST;
188                         if (!(flags & WHOIS_QUICK))
189                                 flags |= WHOIS_RECURSE;
190                 }
191         }
192         while (argc-- > 0) {
193                 if (country != NULL) {
194                         s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
195                         whois(*argv, qnichost, flags);
196                 } else if (use_qnichost)
197                         if ((qnichost = choose_server(*argv)) != NULL)
198                                 whois(*argv, qnichost, flags);
199                 if (qnichost == NULL)
200                         whois(*argv, host, flags);
201                 free(qnichost);
202                 qnichost = NULL;
203                 argv++;
204         }
205         exit(0);
206 }
207
208 /*
209  * This function will remove any trailing periods from domain, after which it
210  * returns a pointer to newly allocated memory containing the whois server to
211  * be queried, or a NULL if the correct server couldn't be determined.  The
212  * caller must remember to free(3) the allocated memory.
213  */
214 static char *
215 choose_server(char *domain)
216 {
217         char *pos, *retval;
218
219         if (strchr(domain, ':')) {
220                 s_asprintf(&retval, "%s", ANICHOST);
221                 return (retval);
222         }
223         for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.';)
224                 *pos = '\0';
225         if (*domain == '\0')
226                 errx(EX_USAGE, "can't search for a null string");
227         if (strlen(domain) > sizeof("-NORID")-1 &&
228             strcasecmp(domain + strlen(domain) - sizeof("-NORID") + 1,
229                 "-NORID") == 0) {
230                 s_asprintf(&retval, "%s", NORIDHOST);
231                 return (retval);
232         }
233         while (pos > domain && *pos != '.')
234                 --pos;
235         if (pos <= domain)
236                 return (NULL);
237         if (isdigit((unsigned char)*++pos))
238                 s_asprintf(&retval, "%s", ANICHOST);
239         else
240                 s_asprintf(&retval, "%s%s", pos, QNICHOST_TAIL);
241         return (retval);
242 }
243
244 static struct addrinfo *
245 gethostinfo(char const *host, int exit_on_error)
246 {
247         struct addrinfo hints, *res;
248         int error;
249
250         memset(&hints, 0, sizeof(hints));
251         hints.ai_flags = 0;
252         hints.ai_family = AF_UNSPEC;
253         hints.ai_socktype = SOCK_STREAM;
254         error = getaddrinfo(host, port, &hints, &res);
255         if (error) {
256                 warnx("%s: %s", host, gai_strerror(error));
257                 if (exit_on_error)
258                         exit(EX_NOHOST);
259                 return (NULL);
260         }
261         return (res);
262 }
263
264 /*
265  * Wrapper for asprintf(3) that exits on error.
266  */
267 static void
268 s_asprintf(char **ret, const char *format, ...)
269 {
270         va_list ap;
271
272         va_start(ap, format);
273         if (vasprintf(ret, format, ap) == -1) {
274                 va_end(ap);
275                 err(EX_OSERR, "vasprintf()");
276         }
277         va_end(ap);
278 }
279
280 static void
281 whois(const char *query, const char *hostname, int flags)
282 {
283         FILE *fp;
284         struct addrinfo *hostres, *res;
285         char *buf, *host, *nhost, *p;
286         int i, j, s = -1, count;
287         size_t c, len;
288         struct pollfd *fds;
289         int timeout = 180;
290
291         hostres = gethostinfo(hostname, 1);
292         for (res = hostres, count = 0; res; res = res->ai_next)
293                 count++;
294
295         fds = calloc(count, sizeof(*fds));
296         if (fds == NULL)
297                 err(EX_OSERR, "calloc()");
298
299         /*
300          * Traverse the result list elements and make non-block
301          * connection attempts.
302          */
303         count = i = 0;
304         for (res = hostres; res != NULL; res = res->ai_next) {
305                 s = socket(res->ai_family, res->ai_socktype | SOCK_NONBLOCK,
306                     res->ai_protocol);
307                 if (s < 0)
308                         continue;
309                 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
310                         if (errno == EINPROGRESS) {
311                                 /* Add the socket to poll list */
312                                 fds[i].fd = s;
313                                 fds[i].events = POLLERR | POLLHUP |
314                                                 POLLIN | POLLOUT;
315                                 count++;
316                                 i++;
317                         } else {
318                                 close(s);
319                                 s = -1;
320
321                                 /*
322                                  * Poll only if we have something to poll,
323                                  * otherwise just go ahead and try next
324                                  * address
325                                  */
326                                 if (count == 0)
327                                         continue;
328                         }
329                 } else
330                         goto done;
331
332                 /*
333                  * If we are at the last address, poll until a connection is
334                  * established or we failed all connection attempts.
335                  */
336                 if (res->ai_next == NULL)
337                         timeout = INFTIM;
338
339                 /*
340                  * Poll the watched descriptors for successful connections:
341                  * if we still have more untried resolved addresses, poll only
342                  * once; otherwise, poll until all descriptors have errors,
343                  * which will be considered as ETIMEDOUT later.
344                  */
345                 do {
346                         int n;
347
348                         n = poll(fds, i, timeout);
349                         if (n == 0) {
350                                 /*
351                                  * No event reported in time.  Try with a
352                                  * smaller timeout (but cap at 2-3ms)
353                                  * after a new host have been added.
354                                  */
355                                 if (timeout >= 3)
356                                         timeout <<= 1;
357
358                                 break;
359                         } else if (n < 0) {
360                                 /*
361                                  * errno here can only be EINTR which we would want
362                                  * to clean up and bail out.
363                                  */
364                                 s = -1;
365                                 goto done;
366                         }
367
368                         /*
369                          * Check for the event(s) we have seen.
370                          */
371                         for (j = 0; j < i; j++) {
372                                 if (fds[j].fd == -1 || fds[j].events == 0 ||
373                                     fds[j].revents == 0)
374                                         continue;
375                                 if (fds[j].revents & ~(POLLIN | POLLOUT)) {
376                                         close(s);
377                                         fds[j].fd = -1;
378                                         fds[j].events = 0;
379                                         count--;
380                                         continue;
381                                 } else if (fds[j].revents & (POLLIN | POLLOUT)) {
382                                         /* Connect succeeded. */
383                                         s = fds[j].fd;
384
385                                         goto done;
386                                 }
387
388                         }
389                 } while (timeout == INFTIM && count != 0);
390         }
391
392         /* All attempts were failed */
393         s = -1;
394         if (count == 0)
395                 errno = ETIMEDOUT;
396
397 done:
398         /* Close all watched fds except the succeeded one */
399         for (j = 0; j < i; j++)
400                 if (fds[j].fd != s && fds[j].fd != -1)
401                         close(fds[j].fd);
402
403         if (s != -1) {
404                 /* Restore default blocking behavior.  */
405                 if ((flags = fcntl(s, F_GETFL)) != -1) {
406                         flags &= ~O_NONBLOCK;
407                         if (fcntl(s, F_SETFL, flags) == -1)
408                                 err(EX_OSERR, "fcntl()");
409                 } else
410                         err(EX_OSERR, "fcntl()");
411         }
412
413         free(fds);
414         freeaddrinfo(hostres);
415         if (s == -1)
416                 err(EX_OSERR, "connect()");
417
418         fp = fdopen(s, "r+");
419         if (fp == NULL)
420                 err(EX_OSERR, "fdopen()");
421         if (strcmp(hostname, GERMNICHOST) == 0) {
422                 fprintf(fp, "-T dn,ace -C US-ASCII %s\r\n", query);
423         } else if (strcmp(hostname, "dk" QNICHOST_TAIL) == 0) {
424                 fprintf(fp, "--show-handles %s\r\n", query);
425         } else {
426                 fprintf(fp, "%s\r\n", query);
427         }
428         fflush(fp);
429         nhost = NULL;
430         while ((buf = fgetln(fp, &len)) != NULL) {
431                 while (len > 0 && isspace((unsigned char)buf[len - 1]))
432                         buf[--len] = '\0';
433                 printf("%.*s\n", (int)len, buf);
434
435                 if ((flags & WHOIS_RECURSE) && nhost == NULL) {
436                         host = strnstr(buf, WHOIS_SERVER_ID, len);
437                         if (host != NULL) {
438                                 host += sizeof(WHOIS_SERVER_ID) - 1;
439                                 for (p = host; p < buf + len; p++) {
440                                         if (!ishost(*p)) {
441                                                 *p = '\0';
442                                                 break;
443                                         }
444                                 }
445                                 s_asprintf(&nhost, "%.*s",
446                                      (int)(buf + len - host), host);
447                         } else if ((host =
448                             strnstr(buf, WHOIS_ORG_SERVER_ID, len)) != NULL) {
449                                 host += sizeof(WHOIS_ORG_SERVER_ID) - 1;
450                                 for (p = host; p < buf + len; p++) {
451                                         if (!ishost(*p)) {
452                                                 *p = '\0';
453                                                 break;
454                                         }
455                                 }
456                                 s_asprintf(&nhost, "%.*s",
457                                     (int)(buf + len - host), host);
458                         } else if (strcmp(hostname, ANICHOST) == 0) {
459                                 for (c = 0; c <= len; c++)
460                                         buf[c] = tolower((unsigned char)buf[c]);
461                                 for (i = 0; ip_whois[i] != NULL; i++) {
462                                         if (strnstr(buf, ip_whois[i], len) !=
463                                             NULL) {
464                                                 s_asprintf(&nhost, "%s",
465                                                     ip_whois[i]);
466                                                 break;
467                                         }
468                                 }
469                         }
470                 }
471         }
472         fclose(fp);
473         if (nhost != NULL) {
474                 whois(query, nhost, 0);
475                 free(nhost);
476         }
477 }
478
479 static void
480 usage(void)
481 {
482         fprintf(stderr,
483             "usage: whois [-aAbfgiIklmQrR6] [-c country-code | -h hostname] "
484             "[-p port] name ...\n");
485         exit(EX_USAGE);
486 }