1 /*      $NetBSD: whois.c,v 1.38 2020/03/01 02:26:16 sevan Exp $    */
2 /*      $OpenBSD: whois.c,v 1.58 2018/06/19 11:28:11 jca Exp $   */
3 
4 /*
5  * Copyright (c) 1980, 1993
6  *        The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 
35 #ifndef lint
36 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
37  The Regents of the University of California.  All rights reserved.");
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static const char sccsid[] = "@(#)whois.c         8.1 (Berkeley) 6/6/93";
43 #else
44 __RCSID("$NetBSD: whois.c,v 1.38 2020/03/01 02:26:16 sevan Exp $");
45 #endif
46 #endif /* not lint */
47 
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <netdb.h>
54 
55 #include <ctype.h>
56 #include <err.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <errno.h>
62 
63 #define   ANICHOST  "whois.arin.net"
64 #define   BNICHOST  "whois.registro.br"
65 #define   CNICHOST  "whois.corenic.net"
66 #define   DNICHOST  "whois.nic.mil"
67 #define   FNICHOST  "whois.afrinic.net"
68 #define   GNICHOST  "whois.nic.gov"
69 #define   IANAHOST  "whois.iana.org"
70 #define   INICHOST  "whois.networksolutions.com"
71 #define   LNICHOST  "whois.lacnic.net"
72 #define   MNICHOST  "whois.ra.net"
73 #define   NICHOST             "whois.crsnic.net"
74 #define   PDBHOST             "whois.peeringdb.com"
75 #define   PNICHOST  "whois.apnic.net"
76 #define   QNICHOST_TAIL       ".whois-servers.net"
77 #define   RNICHOST  "whois.ripe.net"
78 #define   RUNICHOST "whois.ripn.net"
79 
80 #define   WHOIS_PORT          "whois"
81 #define   WHOIS_SERVER_ID     "Registrar WHOIS Server:"
82 
83 #define WHOIS_RECURSE                   0x01
84 #define WHOIS_QUICK           0x02
85 
86 static const char *port_whois = WHOIS_PORT;
87 static const char *ip_whois[] =
88     { LNICHOST, RNICHOST, PNICHOST, FNICHOST, BNICHOST, NULL };
89 
90 static void usage(void) __dead;
91 static int whois(const char *, const char *, const char *, int);
92 static const char *choose_server(const char *, const char *, char **);
93 
94 int
main(int argc,char * argv[])95 main(int argc, char *argv[])
96 {
97           int ch, flags, rval;
98           const char *host, *name, *country;
99 
100           country = host = NULL;
101           flags = rval = 0;
102           while ((ch = getopt(argc, argv, "Aac:dfgh:Iilmp:PqQRr")) != -1)
103                     switch (ch) {
104                     case 'a':
105                               host = ANICHOST;
106                               break;
107                     case 'A':
108                               host = PNICHOST;
109                               break;
110                     case 'c':
111                               country = optarg;
112                               break;
113                     case 'd':
114                               host = DNICHOST;
115                               break;
116                     case 'f':
117                               host = FNICHOST;
118                               break;
119                     case 'g':
120                               host = GNICHOST;
121                               break;
122                     case 'h':
123                               host = optarg;
124                               break;
125                     case 'i':
126                               host = INICHOST;
127                               break;
128                     case 'I':
129                               host = IANAHOST;
130                               break;
131                     case 'l':
132                               host = LNICHOST;
133                               break;
134                     case 'm':
135                               host = MNICHOST;
136                               break;
137                     case 'p':
138                               port_whois = optarg;
139                               break;
140                     case 'P':
141                               host = PDBHOST;
142                               break;
143                     case 'q':
144                               /* deprecated, now the default */
145                               break;
146                     case 'Q':
147                               flags |= WHOIS_QUICK;
148                               break;
149                     case 'r':
150                               host = RNICHOST;
151                               break;
152                     case 'R':
153                               host = RUNICHOST;
154                               break;
155                     default:
156                               usage();
157                     }
158           argc -= optind;
159           argv += optind;
160 
161           if (!argc || (country != NULL && host != NULL))
162                     usage();
163 
164           if (host == NULL && country == NULL && !(flags & WHOIS_QUICK))
165                     flags |= WHOIS_RECURSE;
166           for (name = *argv; (name = *argv) != NULL; argv++) {
167                     char *tofree = NULL;
168                     const char *server =
169                         host ? host : choose_server(name, country, &tofree);
170                     rval += whois(name, server, port_whois, flags);
171                     free(tofree);
172           }
173           return (rval);
174 }
175 
176 static int
whois(const char * query,const char * server,const char * port,int flags)177 whois(const char *query, const char *server, const char *port, int flags)
178 {
179           FILE *fp;
180           char *buf, *p, *nhost, *nbuf = NULL;
181           size_t len;
182           int i, s, error;
183           const char *reason = NULL, *fmt;
184           struct addrinfo hints, *res, *ai;
185 
186           (void)memset(&hints, 0, sizeof(hints));
187           hints.ai_flags = 0;
188           hints.ai_family = AF_UNSPEC;
189           hints.ai_socktype = SOCK_STREAM;
190           error = getaddrinfo(server, port, &hints, &res);
191           if (error) {
192                     if (error == EAI_SERVICE)
193                               warnx("%s: bad port", port);
194                     else
195                               warnx("%s: %s", server, gai_strerror(error));
196                     return (1);
197           }
198 
199           for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) {
200                     s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
201                     if (s < 0) {
202                               error = errno;
203                               reason = "socket";
204                               continue;
205                     }
206                     if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
207                               error = errno;
208                               reason = "connect";
209                               close(s);
210                               s = -1;
211                               continue;
212                     }
213                     break;    /*okay*/
214           }
215           freeaddrinfo(res);
216           if (s < 0) {
217                     if (reason) {
218                               errno = error;
219                               warn("%s: %s", server, reason);
220                     } else
221                               warnx("Unknown error in connection attempt");
222                     return (1);
223           }
224 
225           if (strcmp(server, "whois.denic.de") == 0 ||
226               strcmp(server, "de" QNICHOST_TAIL) == 0)
227                     fmt = "-T dn,ace -C ISO-8859-1 ";
228           else if (strcmp(server, "whois.dk-hostmaster.dk") == 0 ||
229               strcmp(server, "dk" QNICHOST_TAIL) == 0)
230                     fmt = "--show-handles ";
231           else
232                     fmt = "";
233 
234           fp = fdopen(s, "r+");
235           if (fp == NULL)
236                     err(1, "fdopen");
237           (void)fprintf(fp, "%s%s\r\n", fmt, query);
238           (void)fflush(fp);
239           nhost = NULL;
240           while ((buf = fgetln(fp, &len)) != NULL) {
241                     p = buf + len - 1;
242                     if (isspace((unsigned char)*p)) {
243                               do
244                                         *p = '\0';
245                               while (p > buf && isspace((unsigned char)*--p));
246                     } else {
247                               if ((nbuf = malloc(len + 1)) == NULL)
248                                         err(1, "malloc");
249                               (void)memcpy(nbuf, buf, len);
250                               nbuf[len] = '\0';
251                               buf = nbuf;
252                     }
253                     (void)puts(buf);
254 
255                     if (nhost != NULL || !(flags & WHOIS_RECURSE))
256                               continue;
257 
258                     if ((p = strstr(buf, WHOIS_SERVER_ID))) {
259                               p += sizeof(WHOIS_SERVER_ID) - 1;
260                               while (isblank((unsigned char)*p))
261                                         p++;
262                               if ((len = strcspn(p, " \t\n\r"))) {
263                                         if ((nhost = malloc(len + 1)) == NULL)
264                                                   err(1, "malloc");
265                                         (void)memcpy(nhost, p, len);
266                                         nhost[len] = '\0';
267                               }
268                     } else if (strcmp(server, ANICHOST) == 0) {
269                               for (p = buf; *p != '\0'; p++)
270                                         *p = tolower((unsigned char)*p);
271                               for (i = 0; ip_whois[i] != NULL; i++) {
272                                         if (strstr(buf, ip_whois[i]) != NULL) {
273                                                   nhost = strdup(ip_whois[i]);
274                                                   if (nhost == NULL)
275                                                             err(1, "strdup");
276                                                   break;
277                                         }
278                               }
279                     }
280           }
281           fclose(fp);
282           free(nbuf);
283 
284           if (nhost != NULL) {
285                     error = whois(query, nhost, port, 0);
286                     free(nhost);
287           }
288 
289           return (error);
290 }
291 
292 /*
293  * If no country is specified determine the top level domain from the query.
294  * If the TLD is a number, query ARIN, otherwise, use TLD.whois-server.net.
295  * If the domain does not contain '.', check to see if it is an NSI handle
296  * (starts with '!') or a CORE handle (COCO-[0-9]+ or COHO-[0-9]+) or an
297  * ASN (starts with AS) or IPv6 address (contains ':'). Fall back to
298  * NICHOST for the non-handle and non-IPv6 case.
299  */
300 static const char *
choose_server(const char * name,const char * country,char ** tofree)301 choose_server(const char *name, const char *country, char **tofree)
302 {
303           static char *server;
304           const char *qhead;
305           char *ep;
306           struct addrinfo hints, *res = NULL;
307 
308           (void)memset(&hints, 0, sizeof(hints));
309           hints.ai_flags = 0;
310           hints.ai_family = AF_UNSPEC;
311           hints.ai_socktype = SOCK_STREAM;
312 
313           if (country != NULL)
314                     qhead = country;
315           else if ((qhead = strrchr(name, '.')) == NULL) {
316                     if (*name == '!')
317                               return (INICHOST);
318                     else if ((strncasecmp(name, "COCO-", 5) == 0 ||
319                         strncasecmp(name, "COHO-", 5) == 0) &&
320                         strtol(name + 5, &ep, 10) > 0 && *ep == '\0')
321                               return (CNICHOST);
322                     else if ((strncasecmp(name, "AS", 2) == 0) &&
323                         strtol(name + 2, &ep, 10) > 0 && *ep == '\0')
324                               return (MNICHOST);
325                     else if (strchr(name, ':') != NULL) /* IPv6 address */
326                               return (ANICHOST);
327                     else
328                               return (NICHOST);
329           } else if (isdigit((unsigned char)*(++qhead)))
330                     return (ANICHOST);
331 
332           /*
333            * Post-2003 ("new") gTLDs are all supposed to have "whois.nic.domain"
334            * (per registry agreement), some older gTLDs also support this...
335            */
336           if (asprintf(&server, "whois.nic.%s", qhead) == -1)
337                     err(1, NULL);
338 
339           /* most ccTLDs don't do this, but QNICHOST/whois-servers mostly works */
340           if ((strlen(qhead) == 2 ||
341               /* and is required for most of the <=2003 TLDs/gTLDs */
342               strcasecmp(qhead, "org") == 0 ||
343               strcasecmp(qhead, "com") == 0 ||
344               strcasecmp(qhead, "net") == 0 ||
345               strcasecmp(qhead, "cat") == 0 ||
346               strcasecmp(qhead, "pro") == 0 ||
347               strcasecmp(qhead, "info") == 0 ||
348               strcasecmp(qhead, "aero") == 0 ||
349               strcasecmp(qhead, "jobs") == 0 ||
350               strcasecmp(qhead, "mobi") == 0 ||
351               strcasecmp(qhead, "museum") == 0 ||
352                /* for others, if whois.nic.TLD doesn't exist, try whois-servers */
353               getaddrinfo(server, NULL, &hints, &res) != 0)) {
354                     free(server);
355                     if (asprintf(&server, "%s%s", qhead, QNICHOST_TAIL) == -1)
356                               err(1, NULL);
357           }
358           if (res != NULL)
359                     freeaddrinfo(res);
360 
361           *tofree = server;
362           return (server);
363 }
364 
365 static void
usage(void)366 usage(void)
367 {
368           (void)fprintf(stderr,
369               "usage: %s [-AadfgIilmPQRr] [-c country-code | -h host] "
370                     "[-p port] name ...\n", getprogname());
371           exit(1);
372 }
373