Merge from vendor branch LIBPCAP:
[dragonfly.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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1980, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)whois.c  8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.bin/whois/whois.c,v 1.15.2.11 2003/02/25 20:59:41 roberto Exp $
36  * $DragonFly: src/usr.bin/whois/whois.c,v 1.2 2003/06/17 04:29:33 dillon Exp $
37  */
38
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #include <ctype.h>
44 #include <err.h>
45 #include <netdb.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <sysexits.h>
51 #include <unistd.h>
52
53 #define NICHOST         "whois.crsnic.net"
54 #define INICHOST        "whois.networksolutions.com"
55 #define DNICHOST        "whois.nic.mil"
56 #define GNICHOST        "whois.nic.gov"
57 #define ANICHOST        "whois.arin.net"
58 #define LNICHOST        "whois.lacnic.net"
59 #define RNICHOST        "whois.ripe.net"
60 #define PNICHOST        "whois.apnic.net"
61 #define RUNICHOST       "whois.ripn.net"
62 #define MNICHOST        "whois.ra.net"
63 #define QNICHOST_TAIL   ".whois-servers.net"
64 #define SNICHOST        "whois.6bone.net"
65 #define BNICHOST        "whois.registro.br"
66 #define WHOIS_SERVER_ID "Whois Server: "
67 #define WHOIS_ORG_SERVER_ID     "Registrant Street1:Whois Server:"
68
69 #define WHOIS_RECURSE           0x01
70 #define WHOIS_QUICK             0x02
71
72 #define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
73
74 const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST, NULL };
75
76 static char *choose_server(char *);
77 static struct addrinfo *gethostinfo(char const *host, int exit_on_error);
78 static void s_asprintf(char **ret, const char *format, ...);
79 static void usage(void);
80 static void whois(const char *, const char *, int);
81
82 int
83 main(int argc, char *argv[])
84 {
85         const char *country, *host;
86         char *qnichost;
87         int ch, flags, use_qnichost;
88
89 #ifdef  SOCKS
90         SOCKSinit(argv[0]);
91 #endif
92
93         country = host = qnichost = NULL;
94         flags = use_qnichost = 0;
95         while ((ch = getopt(argc, argv, "ac:dgh:ilmpQrR6")) != -1) {
96                 switch (ch) {
97                 case 'a':
98                         host = ANICHOST;
99                         break;
100                 case 'c':
101                         country = optarg;
102                         break;
103                 case 'd':
104                         host = DNICHOST;
105                         break;
106                 case 'g':
107                         host = GNICHOST;
108                         break;
109                 case 'h':
110                         host = optarg;
111                         break;
112                 case 'i':
113                         host = INICHOST;
114                         break;
115                 case 'l':
116                         host = LNICHOST;
117                         break;
118                 case 'm':
119                         host = MNICHOST;
120                         break;
121                 case 'p':
122                         host = PNICHOST;
123                         break;
124                 case 'Q':
125                         flags |= WHOIS_QUICK;
126                         break;
127                 case 'r':
128                         host = RNICHOST;
129                         break;
130                 case 'R':
131                         host = RUNICHOST;
132                         break;
133                 case '6':
134                         host = SNICHOST;
135                         break;
136                 case '?':
137                 default:
138                         usage();
139                         /* NOTREACHED */
140                 }
141         }
142         argc -= optind;
143         argv += optind;
144
145         if (!argc || (country != NULL && host != NULL))
146                 usage();
147
148         /*
149          * If no host or country is specified determine the top level domain
150          * from the query.  If the TLD is a number, query ARIN.  Otherwise, use
151          * TLD.whois-server.net.  If the domain does not contain '.', fall
152          * back to NICHOST.
153          */
154         if (host == NULL && country == NULL) {
155                 use_qnichost = 1;
156                 host = NICHOST;
157                 if (!(flags & WHOIS_QUICK))
158                         flags |= WHOIS_RECURSE;
159         }
160         while (argc-- > 0) {
161                 if (country != NULL) {
162                         s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
163                         whois(*argv, qnichost, flags);
164                 } else if (use_qnichost)
165                         if ((qnichost = choose_server(*argv)) != NULL)
166                                 whois(*argv, qnichost, flags);
167                 if (qnichost == NULL)
168                         whois(*argv, host, flags);
169                 free(qnichost);
170                 qnichost = NULL;
171                 argv++;
172         }
173         exit(0);
174 }
175
176 /*
177  * This function will remove any trailing periods from domain, after which it
178  * returns a pointer to newly allocated memory containing the whois server to
179  * be queried, or a NULL if the correct server couldn't be determined.  The
180  * caller must remember to free(3) the allocated memory.
181  */
182 static char *
183 choose_server(char *domain)
184 {
185         char *pos, *retval;
186
187         for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.';)
188                 *pos = '\0';
189         if (*domain == '\0')
190                 errx(EX_USAGE, "can't search for a null string");
191         while (pos > domain && *pos != '.')
192                 --pos;
193         if (pos <= domain)
194                 return (NULL);
195         if (isdigit((unsigned char)*++pos))
196                 s_asprintf(&retval, "%s", ANICHOST);
197         else
198                 s_asprintf(&retval, "%s%s", pos, QNICHOST_TAIL);
199         return (retval);
200 }
201
202 static struct addrinfo *
203 gethostinfo(char const *host, int exit_on_error)
204 {
205         struct addrinfo hints, *res;
206         int error;
207
208         memset(&hints, 0, sizeof(hints));
209         hints.ai_flags = 0;
210         hints.ai_family = AF_UNSPEC;
211         hints.ai_socktype = SOCK_STREAM;
212         error = getaddrinfo(host, "whois", &hints, &res);
213         if (error) {
214                 warnx("%s: %s", host, gai_strerror(error));
215                 if (exit_on_error)
216                         exit(EX_NOHOST);
217                 return (NULL);
218         }
219         return (res);
220 }
221
222 /*
223  * Wrapper for asprintf(3) that exits on error.
224  */
225 static void
226 s_asprintf(char **ret, const char *format, ...)
227 {
228         va_list ap;
229
230         va_start(ap, format);
231         if (vasprintf(ret, format, ap) == -1) {
232                 va_end(ap);
233                 err(EX_OSERR, "vasprintf()");
234         }
235         va_end(ap);
236 }
237
238 static void
239 whois(const char *query, const char *hostname, int flags)
240 {
241         FILE *sfi, *sfo;
242         struct addrinfo *hostres, *res;
243         char *buf, *host, *nhost, *p;
244         int i, s;
245         size_t c, len;
246
247         hostres = gethostinfo(hostname, 1);
248         for (res = hostres; res; res = res->ai_next) {
249                 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
250                 if (s < 0)
251                         continue;
252                 if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
253                         break;
254                 close(s);
255         }
256         freeaddrinfo(hostres);
257         if (res == NULL)
258                 err(EX_OSERR, "connect()");
259
260         sfi = fdopen(s, "r");
261         sfo = fdopen(s, "w");
262         if (sfi == NULL || sfo == NULL)
263                 err(EX_OSERR, "fdopen()");
264         fprintf(sfo, "%s\r\n", query);
265         fflush(sfo);
266         nhost = NULL;
267         while ((buf = fgetln(sfi, &len)) != NULL) {
268                 while (len > 0 && isspace((unsigned char)buf[len - 1]))
269                         buf[--len] = '\0';
270                 printf("%.*s\n", (int)len, buf);
271
272                 if ((flags & WHOIS_RECURSE) && nhost == NULL) {
273                         host = strnstr(buf, WHOIS_SERVER_ID, len);
274                         if (host != NULL) {
275                                 host += sizeof(WHOIS_SERVER_ID) - 1;
276                                 for (p = host; p < buf + len; p++) {
277                                         if (!ishost(*p)) {
278                                                 *p = '\0';
279                                                 break;
280                                         }
281                                 }
282                                 s_asprintf(&nhost, "%.*s",
283                                      (int)(buf + len - host), host);
284                         } else if ((host =
285                             strnstr(buf, WHOIS_ORG_SERVER_ID, len)) != NULL) {
286                                 host += sizeof(WHOIS_ORG_SERVER_ID) - 1;
287                                 for (p = host; p < buf + len; p++) {
288                                         if (!ishost(*p)) {
289                                                 *p = '\0';
290                                                 break;
291                                         }
292                                 }
293                                 s_asprintf(&nhost, "%.*s",
294                                     (int)(buf + len - host), host);
295                         } else if (strcmp(hostname, ANICHOST) == 0) {
296                                 for (c = 0; c <= len; c++)
297                                         buf[c] = tolower((int)buf[c]);
298                                 for (i = 0; ip_whois[i] != NULL; i++) {
299                                         if (strnstr(buf, ip_whois[i], len) !=
300                                             NULL) {
301                                                 s_asprintf(&nhost, "%s",
302                                                     ip_whois[i]);
303                                                 break;
304                                         }
305                                 }
306                         }
307                 }
308         }
309         if (nhost != NULL) {
310                 whois(query, nhost, 0);
311                 free(nhost);
312         }
313 }
314
315 static void
316 usage(void)
317 {
318         fprintf(stderr,
319             "usage: whois [-adgilmpQrR6] [-c country-code | -h hostname] "
320             "name ...\n");
321         exit(EX_USAGE);
322 }