[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.35

1.35    ! henning     1: /*      $OpenBSD: whois.c,v 1.34 2005/06/25 14:27:36 henning 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: #include <sys/types.h>
                     33: #include <sys/socket.h>
1.17      millert    34:
1.1       deraadt    35: #include <netinet/in.h>
1.5       art        36: #include <arpa/inet.h>
1.1       deraadt    37: #include <netdb.h>
1.17      millert    38:
1.18      millert    39: #include <ctype.h>
1.7       millert    40: #include <err.h>
1.35    ! henning    41: #include <errno.h>
1.1       deraadt    42: #include <stdio.h>
                     43: #include <stdlib.h>
                     44: #include <string.h>
                     45: #include <unistd.h>
                     46:
1.7       millert    47: #define        NICHOST         "whois.crsnic.net"
1.22      millert    48: #define        INICHOST        "whois.networksolutions.com"
1.19      millert    49: #define        CNICHOST        "whois.corenic.net"
1.7       millert    50: #define        DNICHOST        "whois.nic.mil"
                     51: #define        GNICHOST        "whois.nic.gov"
1.4       deraadt    52: #define        ANICHOST        "whois.arin.net"
                     53: #define        RNICHOST        "whois.ripe.net"
                     54: #define        PNICHOST        "whois.apnic.net"
1.7       millert    55: #define        RUNICHOST       "whois.ripn.net"
1.6       deraadt    56: #define        MNICHOST        "whois.ra.net"
1.16      fgsch      57: #define LNICHOST       "whois.lacnic.net"
1.18      millert    58: #define SNICHOST       "whois.6bone.net"
1.22      millert    59: #define BNICHOST       "whois.registro.br"
1.6       deraadt    60: #define        QNICHOST_TAIL   ".whois-servers.net"
1.22      millert    61:
                     62: #define        WHOIS_PORT      "whois"
1.24      millert    63: #define        WHOIS_SERVER_ID "Whois Server:"
1.1       deraadt    64:
1.8       millert    65: #define WHOIS_RECURSE          0x01
1.22      millert    66: #define WHOIS_QUICK            0x02
                     67:
1.35    ! henning    68: const char *port_whois = WHOIS_PORT;
1.22      millert    69: const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST, NULL };
1.8       millert    70:
1.34      henning    71: __dead void usage(void);
                     72: int whois(const char *, const char *, const char *, int);
                     73: char *choose_server(const char *, const char *);
1.1       deraadt    74:
                     75: int
1.27      deraadt    76: main(int argc, char *argv[])
1.1       deraadt    77: {
1.18      millert    78:        int ch, flags, rval;
                     79:        char *host, *name, *country, *server;
1.1       deraadt    80:
1.18      millert    81:        country = host = server = NULL;
                     82:        flags = rval = 0;
1.22      millert    83:        while ((ch = getopt(argc, argv, "aAc:dgh:ilmp:qQrR6")) != -1)
1.34      henning    84:                switch (ch) {
1.4       deraadt    85:                case 'a':
                     86:                        host = ANICHOST;
                     87:                        break;
1.22      millert    88:                case 'A':
                     89:                        host = PNICHOST;
                     90:                        break;
1.18      millert    91:                case 'c':
                     92:                        country = optarg;
                     93:                        break;
1.4       deraadt    94:                case 'd':
                     95:                        host = DNICHOST;
                     96:                        break;
1.7       millert    97:                case 'g':
                     98:                        host = GNICHOST;
                     99:                        break;
1.1       deraadt   100:                case 'h':
                    101:                        host = optarg;
                    102:                        break;
1.7       millert   103:                case 'i':
                    104:                        host = INICHOST;
                    105:                        break;
1.16      fgsch     106:                case 'l':
                    107:                        host = LNICHOST;
                    108:                        break;
1.6       deraadt   109:                case 'm':
                    110:                        host = MNICHOST;
                    111:                        break;
1.4       deraadt   112:                case 'p':
1.35    ! henning   113:                        port_whois = optarg;
1.4       deraadt   114:                        break;
1.6       deraadt   115:                case 'q':
1.22      millert   116:                        /* deprecated, now the default */
1.8       millert   117:                        break;
                    118:                case 'Q':
                    119:                        flags |= WHOIS_QUICK;
1.6       deraadt   120:                        break;
1.4       deraadt   121:                case 'r':
                    122:                        host = RNICHOST;
                    123:                        break;
1.7       millert   124:                case 'R':
                    125:                        host = RUNICHOST;
                    126:                        break;
1.18      millert   127:                case '6':
                    128:                        host = SNICHOST;
                    129:                        break;
1.1       deraadt   130:                default:
                    131:                        usage();
                    132:                }
                    133:        argc -= optind;
                    134:        argv += optind;
                    135:
1.18      millert   136:        if (!argc || (country != NULL && host != NULL))
1.1       deraadt   137:                usage();
                    138:
1.18      millert   139:        if (host == NULL && country == NULL && !(flags & WHOIS_QUICK))
1.22      millert   140:                flags |= WHOIS_RECURSE;
1.18      millert   141:        for (name = *argv; (name = *argv) != NULL; argv++)
1.22      millert   142:                rval += whois(name, host ? host : choose_server(name, country),
1.35    ! henning   143:                    port_whois, flags);
1.17      millert   144:        exit(rval);
1.8       millert   145: }
                    146:
1.34      henning   147: int
1.22      millert   148: whois(const char *query, const char *server, const char *port, int flags)
1.8       millert   149: {
                    150:        FILE *sfi, *sfo;
1.32      henning   151:        char *buf, *p, *nhost, *nbuf = NULL;
1.8       millert   152:        size_t len;
1.22      millert   153:        int i, s, error;
1.32      henning   154:        const char *reason = NULL, *fmt;
1.20      millert   155:        struct addrinfo hints, *res, *ai;
1.18      millert   156:
                    157:        memset(&hints, 0, sizeof(hints));
                    158:        hints.ai_flags = 0;
                    159:        hints.ai_family = AF_UNSPEC;
                    160:        hints.ai_socktype = SOCK_STREAM;
1.22      millert   161:        error = getaddrinfo(server, port, &hints, &res);
1.18      millert   162:        if (error) {
1.29      deraadt   163:                if (error == EAI_SERVICE)
                    164:                        warnx("%s: bad port", port);
                    165:                else
                    166:                        warnx("%s: %s", server, gai_strerror(error));
1.18      millert   167:                return (1);
                    168:        }
1.6       deraadt   169:
1.20      millert   170:        for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) {
                    171:                s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.34      henning   172:                if (s == -1) {
1.35    ! henning   173:                        error = errno;
1.11      itojun    174:                        reason = "socket";
                    175:                        continue;
                    176:                }
1.34      henning   177:                if (connect(s, ai->ai_addr, ai->ai_addrlen) == -1) {
1.35    ! henning   178:                        error = errno;
1.11      itojun    179:                        reason = "connect";
                    180:                        close(s);
                    181:                        s = -1;
                    182:                        continue;
                    183:                }
                    184:                break;  /*okay*/
                    185:        }
1.34      henning   186:        if (s == -1) {
1.35    ! henning   187:                if (reason) {
        !           188:                        errno = error;
1.24      millert   189:                        warn("%s: %s", server, reason);
1.35    ! henning   190:                } else
1.17      millert   191:                        warn("unknown error in connection attempt");
1.18      millert   192:                freeaddrinfo(res);
                    193:                return (1);
1.11      itojun    194:        }
1.4       deraadt   195:
1.32      henning   196:        if (strcmp(server, "whois.denic.de") == 0 ||
                    197:            strcmp(server, "de.whois-servers.net") == 0)
                    198:                fmt = "-T dn %s\r\n";
                    199:        else
                    200:                fmt = "%s\r\n";
1.31      henning   201:
1.1       deraadt   202:        sfi = fdopen(s, "r");
                    203:        sfo = fdopen(s, "w");
1.4       deraadt   204:        if (sfi == NULL || sfo == NULL)
1.18      millert   205:                err(1, "fdopen");
1.34      henning   206:        fprintf(sfo, fmt, query);
                    207:        fflush(sfo);
1.8       millert   208:        nhost = NULL;
1.22      millert   209:        while ((buf = fgetln(sfi, &len)) != NULL) {
                    210:                p = buf + len - 1;
                    211:                if (isspace((unsigned char)*p)) {
                    212:                        do
1.28      fgsch     213:                                *p = '\0';
                    214:                        while (p > buf && isspace((unsigned char)*--p));
1.22      millert   215:                } else {
1.17      millert   216:                        if ((nbuf = malloc(len + 1)) == NULL)
                    217:                                err(1, "malloc");
1.12      millert   218:                        memcpy(nbuf, buf, len);
                    219:                        nbuf[len] = '\0';
                    220:                        buf = nbuf;
                    221:                }
1.34      henning   222:                puts(buf);
1.8       millert   223:
1.22      millert   224:                if (nhost != NULL || !(flags & WHOIS_RECURSE))
                    225:                        continue;
                    226:
                    227:                if ((p = strstr(buf, WHOIS_SERVER_ID))) {
                    228:                        p += sizeof(WHOIS_SERVER_ID) - 1;
1.25      millert   229:                        while (isblank(*p))
                    230:                                p++;
1.8       millert   231:                        if ((len = strcspn(p, " \t\n\r"))) {
                    232:                                if ((nhost = malloc(len + 1)) == NULL)
                    233:                                        err(1, "malloc");
                    234:                                memcpy(nhost, p, len);
                    235:                                nhost[len] = '\0';
                    236:                        }
1.22      millert   237:                } else if (strcmp(server, ANICHOST) == 0) {
                    238:                        for (p = buf; *p != '\0'; p++)
                    239:                                *p = tolower((unsigned char)*p);
                    240:                        for (i = 0; ip_whois[i] != NULL; i++) {
                    241:                                if (strstr(buf, ip_whois[i]) != NULL) {
                    242:                                        nhost = strdup(ip_whois[i]);
1.23      millert   243:                                        if (nhost == NULL)
                    244:                                                err(1, "strdup");
1.22      millert   245:                                        break;
                    246:                                }
                    247:                        }
1.8       millert   248:                }
                    249:        }
1.32      henning   250:        if (nbuf != NULL)
                    251:                free(nbuf);
1.8       millert   252:
1.22      millert   253:        if (nhost != NULL) {
                    254:                error = whois(query, nhost, port, 0);
                    255:                free(nhost);
1.8       millert   256:        }
1.18      millert   257:        freeaddrinfo(res);
                    258:        return (error);
                    259: }
                    260:
                    261: /*
                    262:  * If no country is specified determine the top level domain from the query.
1.19      millert   263:  * If the TLD is a number, query ARIN, otherwise, use TLD.whois-server.net.
                    264:  * If the domain does not contain '.', check to see if it is an NSI handle
                    265:  * (starts with '!') or a CORE handle (COCO-[0-9]+ or COHO-[0-9]+).
                    266:  * Fall back to NICHOST for the non-handle case.
1.18      millert   267:  */
1.34      henning   268: char *
1.18      millert   269: choose_server(const char *name, const char *country)
                    270: {
                    271:        static char *server;
                    272:        const char *qhead;
1.35    ! henning   273:        char *nserver;
1.19      millert   274:        char *ep;
1.18      millert   275:        size_t len;
                    276:
                    277:        if (country != NULL)
                    278:                qhead = country;
1.19      millert   279:        else if ((qhead = strrchr(name, '.')) == NULL) {
                    280:                if (*name == '!')
1.22      millert   281:                        return (INICHOST);
1.19      millert   282:                else if ((strncasecmp(name, "COCO-", 5) == 0 ||
                    283:                    strncasecmp(name, "COHO-", 5) == 0) &&
                    284:                    strtol(name + 5, &ep, 10) > 0 && *ep == '\0')
1.34      henning   285:                        return (CNICHOST);
1.19      millert   286:                else
                    287:                        return (NICHOST);
                    288:        } else if (isdigit(*(++qhead)))
1.18      millert   289:                return (ANICHOST);
                    290:        len = strlen(qhead) + sizeof(QNICHOST_TAIL);
1.35    ! henning   291:        if ((nserver = realloc(server, len)) == NULL)
1.18      millert   292:                err(1, "realloc");
1.35    ! henning   293:        server = nserver;
1.18      millert   294:        strlcpy(server, qhead, len);
                    295:        strlcat(server, QNICHOST_TAIL, len);
                    296:        return (server);
1.1       deraadt   297: }
                    298:
1.34      henning   299: __dead void
1.17      millert   300: usage(void)
1.1       deraadt   301: {
1.18      millert   302:        extern char *__progname;
1.7       millert   303:
1.34      henning   304:        fprintf(stderr,
1.30      jmc       305:            "usage: %s [-6AadgilmQRr] [-c country-code | -h hostname] "
1.22      millert   306:                "[-p port] name ...\n", __progname);
1.18      millert   307:        exit(1);
1.1       deraadt   308: }