[BACK]Return to whois.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / whois

Annotation of src/usr.bin/whois/whois.c, Revision 1.32

1.32    ! henning     1: /*      $NetBSD: whois.c,v 1.27 2005/06/22 12:17:56 christos Exp $   */
1.1       deraadt     2:
                      3: /*
                      4:  * Copyright (c) 1980, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.26      millert    15:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
                     32: #ifndef lint
1.17      millert    33: static const char copyright[] =
1.1       deraadt    34: "@(#) Copyright (c) 1980, 1993\n\
                     35:        The Regents of the University of California.  All rights reserved.\n";
                     36: #endif /* not lint */
                     37:
                     38: #ifndef lint
                     39: #if 0
1.17      millert    40: static const char sccsid[] = "@(#)whois.c      8.1 (Berkeley) 6/6/93";
1.9       millert    41: #else
1.31      henning    42: static const char rcsid[] = "$OpenBSD: whois.c,v 1.30 2003/10/12 13:26:09 jmc Exp $";
1.1       deraadt    43: #endif
                     44: #endif /* not lint */
                     45:
                     46: #include <sys/types.h>
                     47: #include <sys/socket.h>
1.17      millert    48:
1.1       deraadt    49: #include <netinet/in.h>
1.5       art        50: #include <arpa/inet.h>
1.1       deraadt    51: #include <netdb.h>
1.17      millert    52:
1.18      millert    53: #include <ctype.h>
1.7       millert    54: #include <err.h>
1.1       deraadt    55: #include <stdio.h>
                     56: #include <stdlib.h>
                     57: #include <string.h>
                     58: #include <unistd.h>
                     59:
1.7       millert    60: #define        NICHOST         "whois.crsnic.net"
1.22      millert    61: #define        INICHOST        "whois.networksolutions.com"
1.19      millert    62: #define        CNICHOST        "whois.corenic.net"
1.7       millert    63: #define        DNICHOST        "whois.nic.mil"
                     64: #define        GNICHOST        "whois.nic.gov"
1.4       deraadt    65: #define        ANICHOST        "whois.arin.net"
                     66: #define        RNICHOST        "whois.ripe.net"
                     67: #define        PNICHOST        "whois.apnic.net"
1.7       millert    68: #define        RUNICHOST       "whois.ripn.net"
1.6       deraadt    69: #define        MNICHOST        "whois.ra.net"
1.16      fgsch      70: #define LNICHOST       "whois.lacnic.net"
1.18      millert    71: #define SNICHOST       "whois.6bone.net"
1.22      millert    72: #define BNICHOST       "whois.registro.br"
1.6       deraadt    73: #define        QNICHOST_TAIL   ".whois-servers.net"
1.22      millert    74:
                     75: #define        WHOIS_PORT      "whois"
1.24      millert    76: #define        WHOIS_SERVER_ID "Whois Server:"
1.1       deraadt    77:
1.8       millert    78: #define WHOIS_RECURSE          0x01
1.22      millert    79: #define WHOIS_QUICK            0x02
                     80:
                     81: const char *port = WHOIS_PORT;
                     82: const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST, NULL };
1.8       millert    83:
1.17      millert    84: static __dead void usage(void);
1.22      millert    85: static int whois(const char *, const char *, const char *, int);
1.18      millert    86: static char *choose_server(const char *, const char *);
1.1       deraadt    87:
                     88: int
1.27      deraadt    89: main(int argc, char *argv[])
1.1       deraadt    90: {
1.18      millert    91:        int ch, flags, rval;
                     92:        char *host, *name, *country, *server;
1.1       deraadt    93:
1.9       millert    94: #ifdef SOCKS
                     95:        SOCKSinit(argv[0]);
                     96: #endif
1.18      millert    97:        country = host = server = NULL;
                     98:        flags = rval = 0;
1.22      millert    99:        while ((ch = getopt(argc, argv, "aAc:dgh:ilmp:qQrR6")) != -1)
1.23      millert   100:                switch(ch) {
1.4       deraadt   101:                case 'a':
                    102:                        host = ANICHOST;
                    103:                        break;
1.22      millert   104:                case 'A':
                    105:                        host = PNICHOST;
                    106:                        break;
1.18      millert   107:                case 'c':
                    108:                        country = optarg;
                    109:                        break;
1.4       deraadt   110:                case 'd':
                    111:                        host = DNICHOST;
                    112:                        break;
1.7       millert   113:                case 'g':
                    114:                        host = GNICHOST;
                    115:                        break;
1.1       deraadt   116:                case 'h':
                    117:                        host = optarg;
                    118:                        break;
1.7       millert   119:                case 'i':
                    120:                        host = INICHOST;
                    121:                        break;
1.16      fgsch     122:                case 'l':
                    123:                        host = LNICHOST;
                    124:                        break;
1.6       deraadt   125:                case 'm':
                    126:                        host = MNICHOST;
                    127:                        break;
1.4       deraadt   128:                case 'p':
1.22      millert   129:                        port = optarg;
1.4       deraadt   130:                        break;
1.6       deraadt   131:                case 'q':
1.22      millert   132:                        /* deprecated, now the default */
1.8       millert   133:                        break;
                    134:                case 'Q':
                    135:                        flags |= WHOIS_QUICK;
1.6       deraadt   136:                        break;
1.4       deraadt   137:                case 'r':
                    138:                        host = RNICHOST;
                    139:                        break;
1.7       millert   140:                case 'R':
                    141:                        host = RUNICHOST;
                    142:                        break;
1.18      millert   143:                case '6':
                    144:                        host = SNICHOST;
                    145:                        break;
1.1       deraadt   146:                default:
                    147:                        usage();
                    148:                }
                    149:        argc -= optind;
                    150:        argv += optind;
                    151:
1.18      millert   152:        if (!argc || (country != NULL && host != NULL))
1.1       deraadt   153:                usage();
                    154:
1.18      millert   155:        if (host == NULL && country == NULL && !(flags & WHOIS_QUICK))
1.22      millert   156:                flags |= WHOIS_RECURSE;
1.18      millert   157:        for (name = *argv; (name = *argv) != NULL; argv++)
1.22      millert   158:                rval += whois(name, host ? host : choose_server(name, country),
                    159:                    port, flags);
1.17      millert   160:        exit(rval);
1.8       millert   161: }
                    162:
1.17      millert   163: static int
1.22      millert   164: whois(const char *query, const char *server, const char *port, int flags)
1.8       millert   165: {
                    166:        FILE *sfi, *sfo;
1.32    ! henning   167:        char *buf, *p, *nhost, *nbuf = NULL;
1.8       millert   168:        size_t len;
1.22      millert   169:        int i, s, error;
1.32    ! henning   170:        const char *reason = NULL, *fmt;
1.20      millert   171:        struct addrinfo hints, *res, *ai;
1.18      millert   172:
                    173:        memset(&hints, 0, sizeof(hints));
                    174:        hints.ai_flags = 0;
                    175:        hints.ai_family = AF_UNSPEC;
                    176:        hints.ai_socktype = SOCK_STREAM;
1.22      millert   177:        error = getaddrinfo(server, port, &hints, &res);
1.18      millert   178:        if (error) {
1.29      deraadt   179:                if (error == EAI_SERVICE)
                    180:                        warnx("%s: bad port", port);
                    181:                else
                    182:                        warnx("%s: %s", server, gai_strerror(error));
1.18      millert   183:                return (1);
                    184:        }
1.6       deraadt   185:
1.20      millert   186:        for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) {
                    187:                s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.11      itojun    188:                if (s < 0) {
                    189:                        reason = "socket";
                    190:                        continue;
                    191:                }
1.20      millert   192:                if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
1.11      itojun    193:                        reason = "connect";
                    194:                        close(s);
                    195:                        s = -1;
                    196:                        continue;
                    197:                }
                    198:                break;  /*okay*/
                    199:        }
                    200:        if (s < 0) {
                    201:                if (reason)
1.24      millert   202:                        warn("%s: %s", server, reason);
1.11      itojun    203:                else
1.17      millert   204:                        warn("unknown error in connection attempt");
1.18      millert   205:                freeaddrinfo(res);
                    206:                return (1);
1.11      itojun    207:        }
1.4       deraadt   208:
1.32    ! henning   209:        if (strcmp(server, "whois.denic.de") == 0 ||
        !           210:            strcmp(server, "de.whois-servers.net") == 0)
        !           211:                fmt = "-T dn %s\r\n";
        !           212:        else
        !           213:                fmt = "%s\r\n";
1.31      henning   214:
1.1       deraadt   215:        sfi = fdopen(s, "r");
                    216:        sfo = fdopen(s, "w");
1.4       deraadt   217:        if (sfi == NULL || sfo == NULL)
1.18      millert   218:                err(1, "fdopen");
1.32    ! henning   219:        (void)fprintf(sfo, fmt, query);
1.1       deraadt   220:        (void)fflush(sfo);
1.8       millert   221:        nhost = NULL;
1.22      millert   222:        while ((buf = fgetln(sfi, &len)) != NULL) {
                    223:                p = buf + len - 1;
                    224:                if (isspace((unsigned char)*p)) {
                    225:                        do
1.28      fgsch     226:                                *p = '\0';
                    227:                        while (p > buf && isspace((unsigned char)*--p));
1.22      millert   228:                } else {
1.17      millert   229:                        if ((nbuf = malloc(len + 1)) == NULL)
                    230:                                err(1, "malloc");
1.12      millert   231:                        memcpy(nbuf, buf, len);
                    232:                        nbuf[len] = '\0';
                    233:                        buf = nbuf;
                    234:                }
1.22      millert   235:                (void)puts(buf);
1.8       millert   236:
1.22      millert   237:                if (nhost != NULL || !(flags & WHOIS_RECURSE))
                    238:                        continue;
                    239:
                    240:                if ((p = strstr(buf, WHOIS_SERVER_ID))) {
                    241:                        p += sizeof(WHOIS_SERVER_ID) - 1;
1.25      millert   242:                        while (isblank(*p))
                    243:                                p++;
1.8       millert   244:                        if ((len = strcspn(p, " \t\n\r"))) {
                    245:                                if ((nhost = malloc(len + 1)) == NULL)
                    246:                                        err(1, "malloc");
                    247:                                memcpy(nhost, p, len);
                    248:                                nhost[len] = '\0';
                    249:                        }
1.22      millert   250:                } else if (strcmp(server, ANICHOST) == 0) {
                    251:                        for (p = buf; *p != '\0'; p++)
                    252:                                *p = tolower((unsigned char)*p);
                    253:                        for (i = 0; ip_whois[i] != NULL; i++) {
                    254:                                if (strstr(buf, ip_whois[i]) != NULL) {
                    255:                                        nhost = strdup(ip_whois[i]);
1.23      millert   256:                                        if (nhost == NULL)
                    257:                                                err(1, "strdup");
1.22      millert   258:                                        break;
                    259:                                }
                    260:                        }
1.8       millert   261:                }
                    262:        }
1.32    ! henning   263:        if (nbuf != NULL)
        !           264:                free(nbuf);
1.8       millert   265:
1.22      millert   266:        if (nhost != NULL) {
                    267:                error = whois(query, nhost, port, 0);
                    268:                free(nhost);
1.8       millert   269:        }
1.18      millert   270:        freeaddrinfo(res);
                    271:        return (error);
                    272: }
                    273:
                    274: /*
                    275:  * If no country is specified determine the top level domain from the query.
1.19      millert   276:  * If the TLD is a number, query ARIN, otherwise, use TLD.whois-server.net.
                    277:  * If the domain does not contain '.', check to see if it is an NSI handle
                    278:  * (starts with '!') or a CORE handle (COCO-[0-9]+ or COHO-[0-9]+).
                    279:  * Fall back to NICHOST for the non-handle case.
1.18      millert   280:  */
                    281: static char *
                    282: choose_server(const char *name, const char *country)
                    283: {
                    284:        static char *server;
                    285:        const char *qhead;
1.19      millert   286:        char *ep;
1.18      millert   287:        size_t len;
                    288:
                    289:        if (country != NULL)
                    290:                qhead = country;
1.19      millert   291:        else if ((qhead = strrchr(name, '.')) == NULL) {
                    292:                if (*name == '!')
1.22      millert   293:                        return (INICHOST);
1.19      millert   294:                else if ((strncasecmp(name, "COCO-", 5) == 0 ||
                    295:                    strncasecmp(name, "COHO-", 5) == 0) &&
                    296:                    strtol(name + 5, &ep, 10) > 0 && *ep == '\0')
                    297:                        return (CNICHOST);
                    298:                else
                    299:                        return (NICHOST);
                    300:        } else if (isdigit(*(++qhead)))
1.18      millert   301:                return (ANICHOST);
                    302:        len = strlen(qhead) + sizeof(QNICHOST_TAIL);
                    303:        if ((server = realloc(server, len)) == NULL)
                    304:                err(1, "realloc");
                    305:        strlcpy(server, qhead, len);
                    306:        strlcat(server, QNICHOST_TAIL, len);
                    307:        return (server);
1.1       deraadt   308: }
                    309:
1.17      millert   310: static __dead void
                    311: usage(void)
1.1       deraadt   312: {
1.18      millert   313:        extern char *__progname;
1.7       millert   314:
                    315:        (void)fprintf(stderr,
1.30      jmc       316:            "usage: %s [-6AadgilmQRr] [-c country-code | -h hostname] "
1.22      millert   317:                "[-p port] name ...\n", __progname);
1.18      millert   318:        exit(1);
1.1       deraadt   319: }