xref: /freebsd/usr.bin/whois/whois.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)whois.c	8.1 (Berkeley) 6/6/93";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <err.h>
53 #include <netdb.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <sysexits.h>
58 #include <unistd.h>
59 
60 #define	NICHOST		"whois.crsnic.net"
61 #define	INICHOST	"whois.networksolutions.com"
62 #define	DNICHOST	"whois.nic.mil"
63 #define	GNICHOST	"whois.nic.gov"
64 #define	ANICHOST	"whois.arin.net"
65 #define	RNICHOST	"whois.ripe.net"
66 #define	PNICHOST	"whois.apnic.net"
67 #define	RUNICHOST	"whois.ripn.net"
68 #define	MNICHOST	"whois.ra.net"
69 #define	QNICHOST_TAIL	".whois-servers.net"
70 #define	SNICHOST	"whois.6bone.net"
71 #define	WHOIS_PORT	43
72 
73 #define WHOIS_RECURSE		0x01
74 #define WHOIS_INIC_FALLBACK	0x02
75 #define WHOIS_QUICK		0x04
76 
77 static void usage __P((void));
78 static void whois __P((char *, struct addrinfo *, int));
79 
80 int
81 main(argc, argv)
82 	int argc;
83 	char **argv;
84 {
85 	int ch, i, j, error;
86 	int use_qnichost, flags;
87 	char *host;
88 	char *qnichost;
89 	struct addrinfo hints, *res;
90 
91 #ifdef	SOCKS
92 	SOCKSinit(argv[0]);
93 #endif
94 
95 	host = NULL;
96 	qnichost = NULL;
97 	flags = 0;
98 	use_qnichost = 0;
99 	while ((ch = getopt(argc, argv, "adgh:impQrR6")) != -1) {
100 		switch((char)ch) {
101 		case 'a':
102 			host = ANICHOST;
103 			break;
104 		case 'd':
105 			host = DNICHOST;
106 			break;
107 		case 'g':
108 			host = GNICHOST;
109 			break;
110 		case 'h':
111 			host = optarg;
112 			break;
113 		case 'i':
114 			host = INICHOST;
115 			break;
116 		case 'm':
117 			host = MNICHOST;
118 			break;
119 		case 'p':
120 			host = PNICHOST;
121 			break;
122 		case 'Q':
123 			flags |= WHOIS_QUICK;
124 			break;
125 		case 'r':
126 			host = RNICHOST;
127 			break;
128 		case 'R':
129 			host = RUNICHOST;
130 			break;
131 		case '6':
132 			host = SNICHOST;
133 			break;
134 		case '?':
135 		default:
136 			usage();
137 		}
138 	}
139 	argc -= optind;
140 	argv += optind;
141 
142 	if (!argc) {
143 		usage();
144 	}
145 
146 	/*
147 	 * If no nic host is specified, use whois-servers.net
148 	 * if there is a '.' in the name, else fall back to NICHOST.
149 	 */
150 	if (host == NULL) {
151 		use_qnichost = 1;
152 		host = NICHOST;
153 		if (!(flags & WHOIS_QUICK)) {
154 			flags |= WHOIS_INIC_FALLBACK | WHOIS_RECURSE;
155 		}
156 	}
157 	while (argc--) {
158 		if (use_qnichost) {
159 			if (qnichost) {
160 				free(qnichost);
161 				qnichost = NULL;
162 			}
163 			for (i = j = 0; (*argv)[i]; i++) {
164 				if ((*argv)[i] == '.') {
165 					j = i;
166 				}
167 			}
168 			if (j != 0) {
169 				qnichost = (char *) calloc(i - j + 1 +
170 				    strlen(QNICHOST_TAIL), sizeof(char));
171 				if (!qnichost) {
172 					err(1, "calloc");
173 				}
174 				strcpy(qnichost, *argv + j + 1);
175 				strcat(qnichost, QNICHOST_TAIL);
176 
177 				memset(&hints, 0, sizeof(hints));
178 				hints.ai_flags = 0;
179 				hints.ai_family = AF_UNSPEC;
180 				hints.ai_socktype = SOCK_STREAM;
181 				error = getaddrinfo(qnichost, "whois",
182 						&hints, &res);
183 				if (error != 0)
184 					errx(EX_NOHOST, "%s: %s", qnichost,
185 						gai_strerror(error));
186 			}
187 		}
188 		if (!qnichost) {
189 			memset(&hints, 0, sizeof(hints));
190 			hints.ai_flags = 0;
191 			hints.ai_family = AF_UNSPEC;
192 			hints.ai_socktype = SOCK_STREAM;
193 			error = getaddrinfo(host, "whois", &hints, &res);
194 			if (error != 0)
195 				errx(EX_NOHOST, "%s: %s", host,
196 					gai_strerror(error));
197 		}
198 
199 		whois(*argv++, res, flags);
200 		freeaddrinfo(res);
201 	}
202 	exit(0);
203 }
204 
205 static void
206 whois(name, res, flags)
207 	char *name;
208 	struct addrinfo *res;
209 	int flags;
210 {
211 	FILE *sfi, *sfo;
212 	char *buf, *p, *nhost;
213 	size_t len;
214 	int s, nomatch;
215 
216 	s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
217 	if (s < 0) {
218 		err(EX_OSERR, "socket");
219 	}
220 
221 	if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
222 		err(EX_OSERR, "connect");
223 	}
224 
225 	sfi = fdopen(s, "r");
226 	sfo = fdopen(s, "w");
227 	if (sfi == NULL || sfo == NULL) {
228 		err(EX_OSERR, "fdopen");
229 	}
230 	(void)fprintf(sfo, "%s\r\n", name);
231 	(void)fflush(sfo);
232 	nhost = NULL;
233 	nomatch = 0;
234 	while ((buf = fgetln(sfi, &len))) {
235 		if (buf[len - 2] == '\r') {
236 			buf[len - 2] = '\0';
237 		} else {
238 			buf[len - 1] = '\0';
239 		}
240 
241 		if ((flags & WHOIS_RECURSE) && !nhost &&
242 		    (p = strstr(buf, "Whois Server: "))) {
243 			p += sizeof("Whois Server: ") - 1;
244 			if ((len = strcspn(p, " \t\n\r"))) {
245 				if ((nhost = malloc(len + 1)) == NULL) {
246 					err(1, "malloc");
247 				}
248 				memcpy(nhost, p, len);
249 				nhost[len] = '\0';
250 			}
251 		}
252 		if ((flags & WHOIS_INIC_FALLBACK) && !nhost && !nomatch &&
253 		    (p = strstr(buf, "No match for \""))) {
254 			p += sizeof("No match for \"") - 1;
255 			if ((len = strcspn(p, "\"")) &&
256 			    strncasecmp(name, p, len) == 0 &&
257 			    name[len] == '\0' &&
258 			    strchr(name, '.') == NULL) {
259 				nomatch = 1;
260 			}
261 		}
262 		(void)puts(buf);
263 	}
264 
265 	/* Do second lookup as needed */
266 	if (nomatch && !nhost) {
267 		(void)printf("Looking up %s at %s.\n\n", name, INICHOST);
268 		nhost = INICHOST;
269 	}
270 	if (nhost) {
271 		struct addrinfo hints, *res2;
272 		int error;
273 
274 		memset(&hints, 0, sizeof(hints));
275 		hints.ai_flags = 0;
276 		hints.ai_family = AF_UNSPEC;
277 		hints.ai_socktype = SOCK_STREAM;
278 		error = getaddrinfo(nhost, "whois", &hints, &res2);
279 		if (error != 0) {
280 			warnx("%s: %s", nhost, gai_strerror(error));
281 			return;
282 		}
283 		if (!nomatch) {
284 			free(nhost);
285 		}
286 		whois(name, res2, 0);
287 		freeaddrinfo(res2);
288 	}
289 }
290 
291 static void
292 usage()
293 {
294 	(void)fprintf(stderr,
295 	    "usage: whois [-adgimpQrR6] [-h hostname] name ...\n");
296 	exit(EX_USAGE);
297 }
298