kernel/extp{read,write}v: Change ioccnt from u_int to int.
[dragonfly.git] / usr.bin / whois / whois.c
blobb919edaf0b4e562071e991f4343d52471e59aebf
1 /*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
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. 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.
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.
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)whois.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/whois/whois.c,v 1.15.2.11 2003/02/25 20:59:41 roberto Exp $
32 * $DragonFly: src/usr.bin/whois/whois.c,v 1.7 2005/02/17 11:53:33 liamfoy Exp $
35 #include <sys/types.h>
36 #include <sys/socket.h>
38 #include <arpa/inet.h>
39 #include <netinet/in.h>
41 #include <ctype.h>
42 #include <err.h>
43 #include <netdb.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sysexits.h>
49 #include <unistd.h>
51 #define ABUSEHOST "whois.abuse.net"
52 #define NICHOST "whois.crsnic.net"
53 #define INICHOST "whois.networksolutions.com"
54 #define DNICHOST "whois.nic.mil"
55 #define GNICHOST "whois.nic.gov"
56 #define ANICHOST "whois.arin.net"
57 #define LNICHOST "whois.lacnic.net"
58 #define KNICHOST "whois.krnic.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 IANAHOST "whois.iana.org"
66 #define GERMNICHOST "de.whois-servers.net"
67 #define BNICHOST "whois.registro.br"
68 #define WHOIS_SERVER_ID "Whois Server: "
69 #define WHOIS_ORG_SERVER_ID "Registrant Street1:Whois Server:"
71 #define DEFAULT_PORT "whois"
72 #define WHOIS_RECURSE 0x01
73 #define WHOIS_QUICK 0x02
75 #define ishost(h) (isalnum(h) || h == '.' || h == '-')
77 const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST, NULL };
78 const char *port = DEFAULT_PORT;
80 static char *choose_server(const char *);
81 static struct addrinfo *gethostinfo(const char *, int);
82 static void s_asprintf(char **, const char *, ...) __printflike(2, 3);
83 static void usage(void);
84 static void whois(const char *, const char *, int);
86 int
87 main(int argc, char *argv[])
89 const char *country, *host;
90 char *qnichost;
91 int ch, flags, use_qnichost;
93 #ifdef SOCKS
94 SOCKSinit(argv[0]);
95 #endif
97 country = host = qnichost = NULL;
98 flags = use_qnichost = 0;
99 while ((ch = getopt(argc, argv, "aAbc:dgh:ilkImp:QrR6")) != -1) {
100 switch (ch) {
101 case 'a':
102 host = ANICHOST;
103 break;
104 case 'A':
105 host = PNICHOST;
106 break;
107 case 'b':
108 host = ABUSEHOST;
109 break;
110 case 'c':
111 country = optarg;
112 break;
113 case 'd':
114 host = DNICHOST;
115 break;
116 case 'g':
117 host = GNICHOST;
118 break;
119 case 'h':
120 host = optarg;
121 break;
122 case 'i':
123 host = INICHOST;
124 break;
125 case 'I':
126 host = IANAHOST;
127 break;
128 case 'k':
129 host = KNICHOST;
130 break;
131 case 'l':
132 host = LNICHOST;
133 break;
134 case 'm':
135 host = MNICHOST;
136 break;
137 case 'p':
138 port = optarg;
139 break;
140 case 'Q':
141 flags |= WHOIS_QUICK;
142 break;
143 case 'r':
144 host = RNICHOST;
145 break;
146 case 'R':
147 host = RUNICHOST;
148 break;
149 case '6':
150 host = SNICHOST;
151 break;
152 case '?':
153 default:
154 usage();
155 /* NOTREACHED */
158 argc -= optind;
159 argv += optind;
161 if (!argc || (country != NULL && host != NULL))
162 usage();
165 * If no host or country is specified determine the top level domain
166 * from the query. If the TLD is a number, query ARIN. Otherwise, use
167 * TLD.whois-server.net. If the domain does not contain '.', fall
168 * back to NICHOST.
170 if (host == NULL && country == NULL) {
171 use_qnichost = 1;
172 host = NICHOST;
173 if (!(flags & WHOIS_QUICK))
174 flags |= WHOIS_RECURSE;
176 while (argc-- > 0) {
177 if (country != NULL) {
178 s_asprintf(&qnichost, "%s%s", country, QNICHOST_TAIL);
179 whois(*argv, qnichost, flags);
180 } else if (use_qnichost)
181 if ((qnichost = choose_server(*argv)) != NULL)
182 whois(*argv, qnichost, flags);
183 if (qnichost == NULL)
184 whois(*argv, host, flags);
185 free(qnichost);
186 qnichost = NULL;
187 argv++;
189 exit(0);
193 * This function will remove any trailing periods from domain, after which it
194 * returns a pointer to newly allocated memory containing the whois server to
195 * be queried, or a NULL if the correct server couldn't be determined. The
196 * caller must remember to free(3) the allocated memory.
198 static char *
199 choose_server(const char *domain)
201 char *pos, *retval;
203 for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.';)
204 *pos = '\0';
205 if (*domain == '\0')
206 errx(EX_USAGE, "can't search for a null string");
207 while (pos > domain && *pos != '.')
208 --pos;
209 if (pos <= domain)
210 return (NULL);
211 if (isdigit(*++pos))
212 s_asprintf(&retval, "%s", ANICHOST);
213 else
214 s_asprintf(&retval, "%s%s", pos, QNICHOST_TAIL);
215 return (retval);
218 static struct addrinfo *
219 gethostinfo(const char *host, int exit_on_error)
221 struct addrinfo hints, *res;
222 int error;
224 memset(&hints, 0, sizeof(hints));
225 hints.ai_flags = 0;
226 hints.ai_family = AF_UNSPEC;
227 hints.ai_socktype = SOCK_STREAM;
228 error = getaddrinfo(host, port, &hints, &res);
229 if (error) {
230 if (error == EAI_SERVICE)
231 warnx("bad port: %s", port);
232 else
233 warnx("%s: %s", host, gai_strerror(error));
234 if (exit_on_error)
235 exit(EX_NOHOST);
236 return (NULL);
238 return (res);
242 * Wrapper for asprintf(3) that exits on error.
244 static void
245 s_asprintf(char **ret, const char *format, ...)
248 va_list ap;
250 va_start(ap, format);
251 if (vasprintf(ret, format, ap) == -1) {
252 va_end(ap);
253 err(EX_OSERR, "vasprintf()");
255 va_end(ap);
258 static void
259 whois(const char *query, const char *hostname, int flags)
261 FILE *sfi, *sfo;
262 struct addrinfo *hostres, *res;
263 char *buf, *host, *nhost, *p;
264 int i, s = -1;
265 size_t c, len;
267 hostres = gethostinfo(hostname, 1);
268 for (res = hostres; res; res = res->ai_next) {
269 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
270 if (s < 0)
271 continue;
272 if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
273 break;
274 close(s);
276 freeaddrinfo(hostres);
277 if (res == NULL)
278 err(EX_OSERR, "connect()");
280 sfi = fdopen(s, "r");
281 sfo = fdopen(s, "w");
282 if (sfi == NULL || sfo == NULL)
283 err(EX_OSERR, "fdopen()");
284 if (strcmp(hostname, GERMNICHOST) == 0) {
285 fprintf(sfo, "-T dn,ace -C US-ASCII %s\r\n", query);
286 } else {
287 fprintf(sfo, "%s\r\n", query);
289 fflush(sfo);
290 nhost = NULL;
291 while ((buf = fgetln(sfi, &len)) != NULL) {
292 while (len > 0 && isspace(buf[len - 1]))
293 buf[--len] = '\0';
294 printf("%.*s\n", (int)len, buf);
296 if ((flags & WHOIS_RECURSE) && nhost == NULL) {
297 host = strnstr(buf, WHOIS_SERVER_ID, len);
298 if (host != NULL) {
299 host += sizeof(WHOIS_SERVER_ID) - 1;
300 for (p = host; p < buf + len; p++) {
301 if (!ishost(*p)) {
302 *p = '\0';
303 break;
306 s_asprintf(&nhost, "%.*s",
307 (int)(buf + len - host), host);
308 } else if ((host =
309 strnstr(buf, WHOIS_ORG_SERVER_ID, len)) != NULL) {
310 host += sizeof(WHOIS_ORG_SERVER_ID) - 1;
311 for (p = host; p < buf + len; p++) {
312 if (!ishost(*p)) {
313 *p = '\0';
314 break;
317 s_asprintf(&nhost, "%.*s",
318 (int)(buf + len - host), host);
319 } else if (strcmp(hostname, ANICHOST) == 0) {
320 for (c = 0; c <= len; c++)
321 buf[c] = tolower((int)buf[c]);
322 for (i = 0; ip_whois[i] != NULL; i++) {
323 if (strnstr(buf, ip_whois[i], len) !=
324 NULL) {
325 s_asprintf(&nhost, "%s",
326 ip_whois[i]);
327 break;
333 if (nhost != NULL) {
334 whois(query, nhost, 0);
335 free(nhost);
339 static void
340 usage(void)
342 fprintf(stderr,
343 "usage: whois [-aAbdgilkImQrR6] [-c country-code | -h hostname] "
344 "[-p port] name ...\n");
345 exit(EX_USAGE);