2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
7 * Copyright (c) 2013 Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
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
34 #include <sys/types.h>
35 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
50 #define ABUSEHOST "whois.abuse.net"
51 #define NICHOST "whois.crsnic.net"
52 #define INICHOST "whois.networksolutions.com"
53 #define GNICHOST "whois.nic.gov"
54 #define ANICHOST "whois.arin.net"
55 #define LNICHOST "whois.lacnic.net"
56 #define KNICHOST "whois.krnic.net"
57 #define RNICHOST "whois.ripe.net"
58 #define PNICHOST "whois.apnic.net"
59 #define MNICHOST "whois.ra.net"
60 #define QNICHOST_TAIL ".whois-servers.net"
61 #define BNICHOST "whois.registro.br"
62 #define NORIDHOST "whois.norid.no"
63 #define IANAHOST "whois.iana.org"
64 #define GERMNICHOST "de.whois-servers.net"
65 #define FNICHOST "whois.afrinic.net"
66 #define DEFAULT_PORT "whois"
67 #define WHOIS_SERVER_ID "Whois Server: "
68 #define WHOIS_ORG_SERVER_ID "Registrant Street1:Whois Server:"
70 #define WHOIS_RECURSE 0x01
71 #define WHOIS_QUICK 0x02
73 #define ishost(h) (isalnum((unsigned char)h) || h == '.' || h == '-')
75 const char *ip_whois
[] = { LNICHOST
, RNICHOST
, PNICHOST
, BNICHOST
,
77 const char *port
= DEFAULT_PORT
;
79 static char *choose_server(char *);
80 static struct addrinfo
*gethostinfo(char const *host
, int exit_on_error
);
81 static void s_asprintf(char **ret
, const char *format
, ...);
82 static void usage(void);
83 static void whois(const char *, const char *, int);
86 main(int argc
, char *argv
[])
88 const char *country
, *host
;
90 int ch
, flags
, use_qnichost
;
92 country
= host
= qnichost
= NULL
;
93 flags
= use_qnichost
= 0;
94 while ((ch
= getopt(argc
, argv
, "aAbc:fgh:iIklmp:Qr")) != -1) {
136 flags
|= WHOIS_QUICK
;
150 if (!argc
|| (country
!= NULL
&& host
!= NULL
))
154 * If no host or country is specified determine the top level domain
155 * from the query. If the TLD is a number, query ARIN. Otherwise, use
156 * TLD.whois-server.net. If the domain does not contain '.', fall
159 if (host
== NULL
&& country
== NULL
) {
162 if (!(flags
& WHOIS_QUICK
))
163 flags
|= WHOIS_RECURSE
;
166 if (country
!= NULL
) {
167 s_asprintf(&qnichost
, "%s%s", country
, QNICHOST_TAIL
);
168 whois(*argv
, qnichost
, flags
);
169 } else if (use_qnichost
)
170 if ((qnichost
= choose_server(*argv
)) != NULL
)
171 whois(*argv
, qnichost
, flags
);
172 if (qnichost
== NULL
)
173 whois(*argv
, host
, flags
);
183 * This function will remove any trailing periods from domain, after which it
184 * returns a pointer to newly allocated memory containing the whois server to
185 * be queried, or a NULL if the correct server couldn't be determined. The
186 * caller must remember to free(3) the allocated memory.
189 choose_server(char *domain
)
193 if (strchr(domain
, ':')) {
194 s_asprintf(&retval
, "%s", ANICHOST
);
197 for (pos
= strchr(domain
, '\0'); pos
> domain
&& *--pos
== '.'; )
200 errx(EX_USAGE
, "can't search for a null string");
201 if (strlen(domain
) > sizeof ("-NORID")-1 &&
202 strcasecmp(domain
+ strlen(domain
) - sizeof ("-NORID") + 1,
204 s_asprintf(&retval
, "%s", NORIDHOST
);
207 while (pos
> domain
&& *pos
!= '.')
211 if (isdigit((unsigned char)*++pos
))
212 s_asprintf(&retval
, "%s", ANICHOST
);
214 s_asprintf(&retval
, "%s%s", pos
, QNICHOST_TAIL
);
218 static struct addrinfo
*
219 gethostinfo(char const *host
, int exit_on_error
)
221 struct addrinfo hints
, *res
;
224 (void) memset(&hints
, 0, sizeof (hints
));
226 hints
.ai_family
= AF_UNSPEC
;
227 hints
.ai_socktype
= SOCK_STREAM
;
228 error
= getaddrinfo(host
, port
, &hints
, &res
);
230 warnx("%s: %s", host
, gai_strerror(error
));
239 * Wrapper for asprintf(3) that exits on error.
243 s_asprintf(char **ret
, const char *format
, ...)
247 va_start(ap
, format
);
248 if (vasprintf(ret
, format
, ap
) == -1) {
250 err(EX_OSERR
, "vasprintf()");
256 whois(const char *query
, const char *hostname
, int flags
)
259 struct addrinfo
*hostres
, *res
;
261 * The variables buf and buflen are static so the buffer for
262 * getline() is retained across calls.
264 static char *buf
= NULL
;
265 static size_t buflen
= 0;
266 char *host
, *nhost
, *p
;
271 hostres
= gethostinfo(hostname
, 1);
272 for (res
= hostres
; res
; res
= res
->ai_next
) {
273 s
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
276 if (connect(s
, res
->ai_addr
, res
->ai_addrlen
) == 0)
280 freeaddrinfo(hostres
);
282 err(EX_OSERR
, "connect()");
284 sfi
= fdopen(s
, "r");
285 sfo
= fdopen(s
, "w");
286 if (sfi
== NULL
|| sfo
== NULL
)
287 err(EX_OSERR
, "fdopen()");
288 if (strcmp(hostname
, GERMNICHOST
) == 0) {
289 (void) fprintf(sfo
, "-T dn,ace -C US-ASCII %s\r\n", query
);
290 } else if (strcmp(hostname
, "dk" QNICHOST_TAIL
) == 0) {
291 (void) fprintf(sfo
, "--show-handles %s\r\n", query
);
293 (void) fprintf(sfo
, "%s\r\n", query
);
297 while ((len
= getline(&buf
, &buflen
, sfi
)) != -1) {
298 while (len
> 0 && isspace((unsigned char)buf
[len
- 1]))
300 (void) printf("%.*s\n", (int)len
, buf
);
302 if ((flags
& WHOIS_RECURSE
) && nhost
== NULL
) {
303 host
= strnstr(buf
, WHOIS_SERVER_ID
, len
);
305 host
+= sizeof (WHOIS_SERVER_ID
) - 1;
306 for (p
= host
; p
< buf
+ len
; p
++) {
312 s_asprintf(&nhost
, "%.*s",
313 (int)(buf
+ len
- host
), host
);
315 strnstr(buf
, WHOIS_ORG_SERVER_ID
, len
)) != NULL
) {
316 host
+= sizeof (WHOIS_ORG_SERVER_ID
) - 1;
317 for (p
= host
; p
< buf
+ len
; p
++) {
323 s_asprintf(&nhost
, "%.*s",
324 (int)(buf
+ len
- host
), host
);
325 } else if (strcmp(hostname
, ANICHOST
) == 0) {
326 for (c
= 0; c
<= len
; c
++)
327 buf
[c
] = tolower((unsigned char)buf
[c
]);
328 for (i
= 0; ip_whois
[i
] != NULL
; i
++) {
329 if (strnstr(buf
, ip_whois
[i
], len
) !=
331 s_asprintf(&nhost
, "%s",
340 whois(query
, nhost
, 0);
348 (void) fprintf(stderr
,
349 "usage: whois [-aAbfgiIklmQr] [-c country-code | -h hostname] "
350 "[-p port] name ...\n");