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
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.
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
35 static const char copyright
[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid
[] = "@(#)whois.c 8.1 (Berkeley) 6/6/93";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
63 #define ABUSEHOST "whois.abuse.net"
64 #define NICHOST "whois.crsnic.net"
65 #define INICHOST "whois.networksolutions.com"
66 #define DNICHOST "whois.nic.mil"
67 #define GNICHOST "whois.nic.gov"
68 #define ANICHOST "whois.arin.net"
69 #define LNICHOST "whois.lacnic.net"
70 #define KNICHOST "whois.krnic.net"
71 #define RNICHOST "whois.ripe.net"
72 #define PNICHOST "whois.apnic.net"
73 #define MNICHOST "whois.ra.net"
74 #define QNICHOST_TAIL ".whois-servers.net"
75 #define SNICHOST "whois.6bone.net"
76 #define BNICHOST "whois.registro.br"
77 #define NORIDHOST "whois.norid.no"
78 #define IANAHOST "whois.iana.org"
79 #define GERMNICHOST "de.whois-servers.net"
80 #define FNICHOST "whois.afrinic.net"
81 #define DEFAULT_PORT "whois"
82 #define WHOIS_SERVER_ID "Whois Server: "
83 #define WHOIS_ORG_SERVER_ID "Registrant Street1:Whois Server:"
85 #define WHOIS_RECURSE 0x01
86 #define WHOIS_QUICK 0x02
88 #define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
90 const char *ip_whois
[] = { LNICHOST
, RNICHOST
, PNICHOST
, BNICHOST
,
92 const char *port
= DEFAULT_PORT
;
94 static char *choose_server(char *);
95 static struct addrinfo
*gethostinfo(char const *host
, int exit_on_error
);
96 static void s_asprintf(char **ret
, const char *format
, ...) __printflike(2, 3);
97 static void usage(void);
98 static void whois(const char *, const char *, int);
101 main(int argc
, char *argv
[])
103 const char *country
, *host
;
105 int ch
, flags
, use_qnichost
;
111 country
= host
= qnichost
= NULL
;
112 flags
= use_qnichost
= 0;
113 while ((ch
= getopt(argc
, argv
, "aAbc:dfgh:iIklmp:QrR6")) != -1) {
158 flags
|= WHOIS_QUICK
;
164 warnx("-R is deprecated; use '-c ru' instead");
179 if (!argc
|| (country
!= NULL
&& host
!= NULL
))
183 * If no host or country is specified determine the top level domain
184 * from the query. If the TLD is a number, query ARIN. Otherwise, use
185 * TLD.whois-server.net. If the domain does not contain '.', fall
188 if (host
== NULL
&& country
== NULL
) {
191 if (!(flags
& WHOIS_QUICK
))
192 flags
|= WHOIS_RECURSE
;
195 if (country
!= NULL
) {
196 s_asprintf(&qnichost
, "%s%s", country
, QNICHOST_TAIL
);
197 whois(*argv
, qnichost
, flags
);
198 } else if (use_qnichost
)
199 if ((qnichost
= choose_server(*argv
)) != NULL
)
200 whois(*argv
, qnichost
, flags
);
201 if (qnichost
== NULL
)
202 whois(*argv
, host
, flags
);
211 * This function will remove any trailing periods from domain, after which it
212 * returns a pointer to newly allocated memory containing the whois server to
213 * be queried, or a NULL if the correct server couldn't be determined. The
214 * caller must remember to free(3) the allocated memory.
217 choose_server(char *domain
)
221 for (pos
= strchr(domain
, '\0'); pos
> domain
&& *--pos
== '.';)
224 errx(EX_USAGE
, "can't search for a null string");
225 if (strlen(domain
) > sizeof("-NORID")-1 &&
226 strcasecmp(domain
+ strlen(domain
) - sizeof("-NORID") + 1,
228 s_asprintf(&retval
, "%s", NORIDHOST
);
231 while (pos
> domain
&& *pos
!= '.')
235 if (isdigit((unsigned char)*++pos
))
236 s_asprintf(&retval
, "%s", ANICHOST
);
238 s_asprintf(&retval
, "%s%s", pos
, QNICHOST_TAIL
);
242 static struct addrinfo
*
243 gethostinfo(char const *host
, int exit_on_error
)
245 struct addrinfo hints
, *res
;
248 memset(&hints
, 0, sizeof(hints
));
250 hints
.ai_family
= AF_UNSPEC
;
251 hints
.ai_socktype
= SOCK_STREAM
;
252 error
= getaddrinfo(host
, port
, &hints
, &res
);
254 warnx("%s: %s", host
, gai_strerror(error
));
263 * Wrapper for asprintf(3) that exits on error.
266 s_asprintf(char **ret
, const char *format
, ...)
270 va_start(ap
, format
);
271 if (vasprintf(ret
, format
, ap
) == -1) {
273 err(EX_OSERR
, "vasprintf()");
279 whois(const char *query
, const char *hostname
, int flags
)
282 struct addrinfo
*hostres
, *res
;
283 char *buf
, *host
, *nhost
, *p
;
288 hostres
= gethostinfo(hostname
, 1);
289 for (res
= hostres
; res
; res
= res
->ai_next
) {
290 s
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
293 if (connect(s
, res
->ai_addr
, res
->ai_addrlen
) == 0)
297 freeaddrinfo(hostres
);
299 err(EX_OSERR
, "connect()");
301 sfi
= fdopen(s
, "r");
302 sfo
= fdopen(s
, "w");
303 if (sfi
== NULL
|| sfo
== NULL
)
304 err(EX_OSERR
, "fdopen()");
305 if (strcmp(hostname
, GERMNICHOST
) == 0) {
306 fprintf(sfo
, "-T dn,ace -C US-ASCII %s\r\n", query
);
307 } else if (strcmp(hostname
, "dk" QNICHOST_TAIL
) == 0) {
308 fprintf(sfo
, "--show-handles %s\r\n", query
);
310 fprintf(sfo
, "%s\r\n", query
);
314 while ((buf
= fgetln(sfi
, &len
)) != NULL
) {
315 while (len
> 0 && isspace((unsigned char)buf
[len
- 1]))
317 printf("%.*s\n", (int)len
, buf
);
319 if ((flags
& WHOIS_RECURSE
) && nhost
== NULL
) {
320 host
= strnstr(buf
, WHOIS_SERVER_ID
, len
);
322 host
+= sizeof(WHOIS_SERVER_ID
) - 1;
323 for (p
= host
; p
< buf
+ len
; p
++) {
329 s_asprintf(&nhost
, "%.*s",
330 (int)(buf
+ len
- host
), host
);
332 strnstr(buf
, WHOIS_ORG_SERVER_ID
, len
)) != NULL
) {
333 host
+= sizeof(WHOIS_ORG_SERVER_ID
) - 1;
334 for (p
= host
; p
< buf
+ len
; p
++) {
340 s_asprintf(&nhost
, "%.*s",
341 (int)(buf
+ len
- host
), host
);
342 } else if (strcmp(hostname
, ANICHOST
) == 0) {
343 for (c
= 0; c
<= len
; c
++)
344 buf
[c
] = tolower((unsigned char)buf
[c
]);
345 for (i
= 0; ip_whois
[i
] != NULL
; i
++) {
346 if (strnstr(buf
, ip_whois
[i
], len
) !=
348 s_asprintf(&nhost
, "%s",
357 whois(query
, nhost
, 0);
366 "usage: whois [-aAbdfgiIklmQrR6] [-c country-code | -h hostname] "
367 "[-p port] name ...\n");