]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/whois/whois.c
This commit was generated by cvs2svn to compensate for changes in r102644,
[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  * 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
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #if 0
41 #ifndef lint
42 static char sccsid[] = "@(#)whois.c     8.1 (Berkeley) 6/6/93";
43 #endif /* not lint */
44 #endif
45
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <ctype.h>
54 #include <err.h>
55 #include <netdb.h>
56 #include <stdarg.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <sysexits.h>
61 #include <unistd.h>
62
63 #define NICHOST         "whois.crsnic.net"
64 #define INICHOST        "whois.networksolutions.com"
65 #define DNICHOST        "whois.nic.mil"
66 #define GNICHOST        "whois.nic.gov"
67 #define ANICHOST        "whois.arin.net"
68 #define RNICHOST        "whois.ripe.net"
69 #define PNICHOST        "whois.apnic.net"
70 #define MNICHOST        "whois.ra.net"
71 #define QNICHOST_TAIL   ".whois-servers.net"
72 #define SNICHOST        "whois.6bone.net"
73 #define BNICHOST        "whois.registro.br"
74 #define DEFAULT_PORT    "whois"
75 #define WHOIS_SERVER_ID "Whois Server: "
76
77 #define WHOIS_RECURSE           0x01
78 #define WHOIS_QUICK             0x02
79
80 #define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
81
82 const char *ip_whois[] = { RNICHOST, PNICHOST, BNICHOST, NULL };
83 const char *port = DEFAULT_PORT;
84
85 static char *choose_server(char *);
86 static struct addrinfo *gethostinfo(char const *host, int exit_on_error);
87 static void s_asprintf(char **ret, const char *format, ...) __printflike(2, 3);
88 static void usage(void);
89 static void whois(const char *, const char *, int);
90
91 int
92 main(int argc, char *argv[])
93 {
94         const char *country, *host;
95         char *qnichost;
96         int ch, flags, use_qnichost;
97
98 #ifdef  SOCKS
99         SOCKSinit(argv[0]);
100 #endif
101
102         country = host = qnichost = NULL;
103         flags = use_qnichost = 0;
104         while ((ch = getopt(argc, argv, "aAc:dgh:imp:QrR6")) != -1) {
105                 switch (ch) {
106                 case 'a':
107                         host = ANICHOST;
108                         break;
109                 case 'A':
110                         host = PNICHOST;
111                         break;
112                 case 'c':
113                         country = optarg;
114                         break;
115                 case 'd':
116                         host = DNICHOST;
117                         break;
118                 case 'g':
119                         host = GNICHOST;
120                         break;
121                 case 'h':
122                         host = optarg;
123                         break;
124                 case 'i':
125                         host = INICHOST;
126                         break;
127                 case 'm':
128                         host = MNICHOST;
129                         break;
130                 case 'p':
131                         port = optarg;
132                         break;
133                 case 'Q':
134                         flags |= WHOIS_QUICK;
135                         break;
136                 case 'r':
137                         host = RNICHOST;
138                         break;
139                 case 'R':
140                         warnx("-R is deprecated; use '-c ru' instead");
141                         country = "ru";
142                         break;
143                 case '6':
144                         host = SNICHOST;
145                         break;
146                 case '?':
147                 default:
148                         usage();
149                         /* NOTREACHED */
150                 }
151         }
152         argc -= optind;
153         argv += optind;
154
155         if (!argc || (country != NULL && host != NULL))
156                 usage();
157
158         /*
159          * If no host or country is specified determine the top level domain
160          * from the query.  If the TLD is a number, query ARIN.  Otherwise, use 
161          * TLD.whois-server.net.  If the domain does not contain '.', fall
162          * back to NICHOST.
163          */
164         if (host == NULL && country == NULL) {
165                 use_qnichost = 1;
166                 host = NICHOST;
167                 if (!(flags & WHOIS_QUICK))
168                         flags |= WHOIS_RECURSE;
169         }
170         while (argc-- > 0) {
171                 if (country != NULL) {
172                         s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
173                         whois(*argv, qnichost, flags);
174                 } else if (use_qnichost)
175                         if ((qnichost = choose_server(*argv)) != NULL)
176                                 whois(*argv, qnichost, flags);
177                 if (qnichost == NULL)
178                         whois(*argv, host, flags);
179                 free(qnichost);
180                 qnichost = NULL;
181                 argv++;
182         }
183         exit(0);
184 }
185
186 /*
187  * This function will remove any trailing periods from domain, after which it
188  * returns a pointer to newly allocated memory containing the whois server to
189  * be queried, or a NULL if the correct server couldn't be determined.  The
190  * caller must remember to free(3) the allocated memory.
191  */
192 static char *
193 choose_server(char *domain)
194 {
195         char *pos, *retval;
196
197         for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.';)
198                 *pos = '\0';
199         if (*domain == '\0')
200                 errx(EX_USAGE, "can't search for a null string");
201         while (pos > domain && *pos != '.')
202                 --pos;
203         if (pos <= domain)
204                 return (NULL);
205         if (isdigit((unsigned char)*++pos))
206                 s_asprintf(&retval, "%s", ANICHOST);
207         else
208                 s_asprintf(&retval, "%s%s", pos, QNICHOST_TAIL);
209         return (retval);
210 }
211
212 static struct addrinfo *
213 gethostinfo(char const *host, int exit_on_error)
214 {
215         struct addrinfo hints, *res;
216         int error;
217
218         memset(&hints, 0, sizeof(hints));
219         hints.ai_flags = 0;
220         hints.ai_family = AF_UNSPEC;
221         hints.ai_socktype = SOCK_STREAM;
222         error = getaddrinfo(host, port, &hints, &res);
223         if (error) {
224                 warnx("%s: %s", host, gai_strerror(error));
225                 if (exit_on_error)
226                         exit(EX_NOHOST);
227                 return (NULL);
228         }
229         return (res);
230 }
231
232 /*
233  * Wrapper for asprintf(3) that exits on error.
234  */
235 static void
236 s_asprintf(char **ret, const char *format, ...)
237 {
238         va_list ap;
239
240         va_start(ap, format);
241         if (vasprintf(ret, format, ap) == -1) {
242                 va_end(ap);
243                 err(EX_OSERR, "vasprintf()");
244         }
245         va_end(ap);
246 }
247
248 static void
249 whois(const char *query, const char *hostname, int flags)
250 {
251         FILE *sfi, *sfo;
252         struct addrinfo *hostres, *res;
253         char *buf, *host, *nhost, *p;
254         int i, s;
255         size_t len;
256
257         hostres = gethostinfo(hostname, 1);
258         for (res = hostres; res; res = res->ai_next) {
259                 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
260                 if (s < 0)
261                         continue;
262                 if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
263                         break;
264                 close(s);
265         }
266         freeaddrinfo(hostres);
267         if (res == NULL)
268                 err(EX_OSERR, "connect()");
269
270         sfi = fdopen(s, "r");
271         sfo = fdopen(s, "w");
272         if (sfi == NULL || sfo == NULL)
273                 err(EX_OSERR, "fdopen()");
274         fprintf(sfo, "%s\r\n", query);
275         fflush(sfo);
276         nhost = NULL;
277         while ((buf = fgetln(sfi, &len)) != NULL) {
278                 while (len > 0 && isspace((unsigned char)buf[len - 1]))
279                         buf[--len] = '\0';
280                 printf("%.*s\n", (int)len, buf);
281
282                 if ((flags & WHOIS_RECURSE) && nhost == NULL) {
283                         host = strnstr(buf, WHOIS_SERVER_ID, len);
284                         if (host != NULL) {
285                                 host += sizeof(WHOIS_SERVER_ID) - 1;
286                                 for (p = host; p < buf + len; p++) {
287                                         if (!ishost(*p)) {
288                                                 *p = '\0';
289                                                 break;
290                                         }
291                                 }
292                                 s_asprintf(&nhost, "%.*s",
293                                      (int)(buf + len - host), host);
294                         } else if (strcmp(hostname, ANICHOST) == 0) {
295                                 for (i = 0; ip_whois[i] != NULL; i++) {
296                                         if (strnstr(buf, ip_whois[i], len) !=
297                                             NULL) {
298                                                 s_asprintf(&nhost, "%s",
299                                                     ip_whois[i]);
300                                                 break;
301                                         }
302                                 }
303                         }
304                 }
305         }
306         if (nhost != NULL) {
307                 whois(query, nhost, 0);
308                 free(nhost);
309         }
310 }
311
312 static void
313 usage(void)
314 {
315         fprintf(stderr,
316             "usage: whois [-aAdgimQrR6] [-c country-code | -h hostname] "
317             "[-p port] name ...\n");
318         exit(EX_USAGE);
319 }