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

1.64    ! op          1: /*      $OpenBSD: whois.c,v 1.63 2024/03/25 15:52:39 millert 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.59      millert    48: #define        INICHOST        "whois.internic.net"
1.7       millert    49: #define        DNICHOST        "whois.nic.mil"
                     50: #define        GNICHOST        "whois.nic.gov"
1.4       deraadt    51: #define        ANICHOST        "whois.arin.net"
                     52: #define        RNICHOST        "whois.ripe.net"
                     53: #define        PNICHOST        "whois.apnic.net"
1.7       millert    54: #define        RUNICHOST       "whois.ripn.net"
1.6       deraadt    55: #define        MNICHOST        "whois.ra.net"
1.16      fgsch      56: #define LNICHOST       "whois.lacnic.net"
1.41      henning    57: #define        AFNICHOST       "whois.afrinic.net"
1.22      millert    58: #define BNICHOST       "whois.registro.br"
1.44      sthen      59: #define        PDBHOST         "whois.peeringdb.com"
1.52      sthen      60: #define        IANAHOST        "whois.iana.org"
1.6       deraadt    61: #define        QNICHOST_TAIL   ".whois-servers.net"
1.22      millert    62:
                     63: #define        WHOIS_PORT      "whois"
1.56      sthen      64: #define        WHOIS_SERVER_ID "Registrar WHOIS Server:"
1.1       deraadt    65:
1.60      millert    66: #define WHOIS_RECURSE  0x01
                     67: #define WHOIS_QUICK    0x02
                     68: #define WHOIS_SPAM_ME  0x04
                     69:
                     70: #define CHOPSPAM       ">>> Last update of WHOIS database:"
1.22      millert    71:
1.35      henning    72: const char *port_whois = WHOIS_PORT;
1.41      henning    73: const char *ip_whois[] = { LNICHOST, RNICHOST, PNICHOST, BNICHOST,
                     74:     AFNICHOST, NULL };
1.8       millert    75:
1.34      henning    76: __dead void usage(void);
                     77: int whois(const char *, const char *, const char *, int);
1.55      millert    78: char *choose_server(const char *, const char *, char **);
1.1       deraadt    79:
                     80: int
1.27      deraadt    81: main(int argc, char *argv[])
1.1       deraadt    82: {
1.18      millert    83:        int ch, flags, rval;
1.55      millert    84:        char *host, *name, *country;
1.1       deraadt    85:
1.39      millert    86:        country = host = NULL;
1.18      millert    87:        flags = rval = 0;
1.60      millert    88:        while ((ch = getopt(argc, argv, "aAc:dgh:iIlmp:PqQrRS")) != -1)
1.34      henning    89:                switch (ch) {
1.4       deraadt    90:                case 'a':
                     91:                        host = ANICHOST;
                     92:                        break;
1.22      millert    93:                case 'A':
                     94:                        host = PNICHOST;
                     95:                        break;
1.18      millert    96:                case 'c':
                     97:                        country = optarg;
                     98:                        break;
1.4       deraadt    99:                case 'd':
                    100:                        host = DNICHOST;
                    101:                        break;
1.7       millert   102:                case 'g':
                    103:                        host = GNICHOST;
                    104:                        break;
1.1       deraadt   105:                case 'h':
                    106:                        host = optarg;
                    107:                        break;
1.7       millert   108:                case 'i':
                    109:                        host = INICHOST;
                    110:                        break;
1.52      sthen     111:                case 'I':
                    112:                        host = IANAHOST;
                    113:                        break;
1.16      fgsch     114:                case 'l':
                    115:                        host = LNICHOST;
                    116:                        break;
1.6       deraadt   117:                case 'm':
                    118:                        host = MNICHOST;
                    119:                        break;
1.4       deraadt   120:                case 'p':
1.35      henning   121:                        port_whois = optarg;
1.4       deraadt   122:                        break;
1.44      sthen     123:                case 'P':
                    124:                        host = PDBHOST;
                    125:                        break;
1.6       deraadt   126:                case 'q':
1.22      millert   127:                        /* deprecated, now the default */
1.8       millert   128:                        break;
                    129:                case 'Q':
                    130:                        flags |= WHOIS_QUICK;
1.6       deraadt   131:                        break;
1.4       deraadt   132:                case 'r':
                    133:                        host = RNICHOST;
                    134:                        break;
1.7       millert   135:                case 'R':
                    136:                        host = RUNICHOST;
1.18      millert   137:                        break;
1.60      millert   138:                case 'S':
                    139:                        flags |= WHOIS_SPAM_ME;
                    140:                        break;
1.1       deraadt   141:                default:
                    142:                        usage();
                    143:                }
                    144:        argc -= optind;
                    145:        argv += optind;
                    146:
1.18      millert   147:        if (!argc || (country != NULL && host != NULL))
1.1       deraadt   148:                usage();
1.49      deraadt   149:
1.50      deraadt   150:        if (pledge("stdio dns inet", NULL) == -1)
                    151:                err(1, "pledge");
1.1       deraadt   152:
1.18      millert   153:        if (host == NULL && country == NULL && !(flags & WHOIS_QUICK))
1.22      millert   154:                flags |= WHOIS_RECURSE;
1.54      millert   155:        for (name = *argv; (name = *argv) != NULL; argv++) {
1.55      millert   156:                char *tofree = NULL;
                    157:                const char *server =
                    158:                    host ? host : choose_server(name, country, &tofree);
1.54      millert   159:                rval += whois(name, server, port_whois, flags);
1.55      millert   160:                free(tofree);
1.54      millert   161:        }
                    162:        return (rval);
1.8       millert   163: }
                    164:
1.34      henning   165: int
1.22      millert   166: whois(const char *query, const char *server, const char *port, int flags)
1.8       millert   167: {
1.46      millert   168:        FILE *fp;
1.63      millert   169:        char *p, *nhost, *buf = NULL;
                    170:        size_t len, bufsize = 0;
1.22      millert   171:        int i, s, error;
1.32      henning   172:        const char *reason = NULL, *fmt;
1.20      millert   173:        struct addrinfo hints, *res, *ai;
1.18      millert   174:
                    175:        memset(&hints, 0, sizeof(hints));
                    176:        hints.ai_flags = 0;
                    177:        hints.ai_family = AF_UNSPEC;
                    178:        hints.ai_socktype = SOCK_STREAM;
1.22      millert   179:        error = getaddrinfo(server, port, &hints, &res);
1.18      millert   180:        if (error) {
1.29      deraadt   181:                if (error == EAI_SERVICE)
                    182:                        warnx("%s: bad port", port);
                    183:                else
                    184:                        warnx("%s: %s", server, gai_strerror(error));
1.18      millert   185:                return (1);
                    186:        }
1.6       deraadt   187:
1.20      millert   188:        for (s = -1, ai = res; ai != NULL; ai = ai->ai_next) {
                    189:                s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.34      henning   190:                if (s == -1) {
1.35      henning   191:                        error = errno;
1.11      itojun    192:                        reason = "socket";
                    193:                        continue;
                    194:                }
1.34      henning   195:                if (connect(s, ai->ai_addr, ai->ai_addrlen) == -1) {
1.35      henning   196:                        error = errno;
1.11      itojun    197:                        reason = "connect";
                    198:                        close(s);
                    199:                        s = -1;
                    200:                        continue;
                    201:                }
                    202:                break;  /*okay*/
                    203:        }
1.58      jca       204:        freeaddrinfo(res);
1.34      henning   205:        if (s == -1) {
1.35      henning   206:                if (reason) {
                    207:                        errno = error;
1.24      millert   208:                        warn("%s: %s", server, reason);
1.35      henning   209:                } else
1.17      millert   210:                        warn("unknown error in connection attempt");
1.18      millert   211:                return (1);
1.11      itojun    212:        }
1.4       deraadt   213:
1.60      millert   214:        if (!(flags & WHOIS_SPAM_ME) &&
                    215:            (strcmp(server, "whois.denic.de") == 0 ||
                    216:            strcmp(server, "de" QNICHOST_TAIL) == 0))
1.36      henning   217:                fmt = "-T dn,ace -C ISO-8859-1 %s\r\n";
1.60      millert   218:        else if (!(flags & WHOIS_SPAM_ME) &&
                    219:            (strcmp(server, "whois.dk-hostmaster.dk") == 0 ||
                    220:            strcmp(server, "dk" QNICHOST_TAIL) == 0))
1.37      mk        221:                fmt = "--show-handles %s\r\n";
1.32      henning   222:        else
                    223:                fmt = "%s\r\n";
1.31      henning   224:
1.46      millert   225:        fp = fdopen(s, "r+");
                    226:        if (fp == NULL)
1.18      millert   227:                err(1, "fdopen");
1.46      millert   228:        fprintf(fp, fmt, query);
                    229:        fflush(fp);
1.8       millert   230:        nhost = NULL;
1.63      millert   231:        while ((len = getline(&buf, &bufsize, fp)) != (size_t)-1) {
1.60      millert   232:                /* Nominet */
                    233:                if (!(flags & WHOIS_SPAM_ME) &&
                    234:                    len == 5 && strncmp(buf, "-- \r\n", 5) == 0)
                    235:                        break;
                    236:
1.22      millert   237:                p = buf + len - 1;
1.63      millert   238:                while (p > buf && isspace((unsigned char)*p))
                    239:                        *p-- = '\0';
1.34      henning   240:                puts(buf);
1.8       millert   241:
1.60      millert   242:                if (nhost == NULL && (flags & WHOIS_RECURSE)) {
                    243:                        if ((p = strstr(buf, WHOIS_SERVER_ID))) {
                    244:                                p += sizeof(WHOIS_SERVER_ID) - 1;
                    245:                                while (isblank((unsigned char)*p))
                    246:                                        p++;
                    247:                                if ((len = strcspn(p, " \t\n\r"))) {
1.63      millert   248:                                        if ((nhost = strndup(p, len)) == NULL)
                    249:                                                err(1, "strndup");
1.60      millert   250:                                }
                    251:                        } else if (strcmp(server, ANICHOST) == 0) {
                    252:                                for (p = buf; *p != '\0'; p++)
                    253:                                        *p = tolower((unsigned char)*p);
                    254:                                for (i = 0; ip_whois[i] != NULL; i++) {
                    255:                                        if (strstr(buf, ip_whois[i]) != NULL) {
                    256:                                                nhost = strdup(ip_whois[i]);
                    257:                                                if (nhost == NULL)
                    258:                                                        err(1, "strdup");
                    259:                                                break;
                    260:                                        }
1.22      millert   261:                                }
                    262:                        }
1.60      millert   263:                }
                    264:
                    265:                /* Verisign etc. */
                    266:                if (!(flags & WHOIS_SPAM_ME) &&
                    267:                    (strncasecmp(buf, CHOPSPAM, sizeof(CHOPSPAM)-1) == 0 ||
                    268:                     strncasecmp(buf, &CHOPSPAM[4], sizeof(CHOPSPAM)-5) == 0)) {
                    269:                        printf("\n");
                    270:                        break;
1.8       millert   271:                }
                    272:        }
1.46      millert   273:        fclose(fp);
1.63      millert   274:        free(buf);
1.8       millert   275:
1.22      millert   276:        if (nhost != NULL) {
                    277:                error = whois(query, nhost, port, 0);
                    278:                free(nhost);
1.8       millert   279:        }
1.58      jca       280:
1.18      millert   281:        return (error);
                    282: }
                    283:
                    284: /*
                    285:  * If no country is specified determine the top level domain from the query.
1.19      millert   286:  * If the TLD is a number, query ARIN, otherwise, use TLD.whois-server.net.
1.62      millert   287:  * If the domain does not contain '.', check to see if it is an ASN (starts
                    288:  * with AS) or IPv6 address (contains ':').
                    289:  * Fall back to NICHOST for the non-handle and non-IPv6 case.
1.18      millert   290:  */
1.34      henning   291: char *
1.55      millert   292: choose_server(const char *name, const char *country, char **tofree)
1.18      millert   293: {
1.54      millert   294:        char *server;
1.18      millert   295:        const char *qhead;
1.19      millert   296:        char *ep;
1.58      jca       297:        struct addrinfo hints, *res = NULL;
1.47      sthen     298:
                    299:        memset(&hints, 0, sizeof(hints));
                    300:        hints.ai_flags = 0;
                    301:        hints.ai_family = AF_UNSPEC;
                    302:        hints.ai_socktype = SOCK_STREAM;
1.18      millert   303:
                    304:        if (country != NULL)
                    305:                qhead = country;
1.19      millert   306:        else if ((qhead = strrchr(name, '.')) == NULL) {
1.62      millert   307:                if ((strncasecmp(name, "AS", 2) == 0) &&
1.38      henning   308:                    strtol(name + 2, &ep, 10) > 0 && *ep == '\0')
                    309:                        return (MNICHOST);
1.57      florian   310:                else if (strchr(name, ':') != NULL) /* IPv6 address */
                    311:                        return (ANICHOST);
1.19      millert   312:                else
                    313:                        return (NICHOST);
1.51      mmcc      314:        } else if (isdigit((unsigned char)*(++qhead)))
1.18      millert   315:                return (ANICHOST);
1.47      sthen     316:
                    317:        /*
                    318:         * Post-2003 ("new") gTLDs are all supposed to have "whois.nic.domain"
                    319:         * (per registry agreement), some older gTLDs also support this...
                    320:         */
1.54      millert   321:        if (asprintf(&server, "whois.nic.%s", qhead) == -1)
                    322:                err(1, NULL);
1.47      sthen     323:
                    324:        /* most ccTLDs don't do this, but QNICHOST/whois-servers mostly works */
                    325:        if ((strlen(qhead) == 2 ||
                    326:            /* and is required for most of the <=2003 TLDs/gTLDs */
1.48      sthen     327:            strcasecmp(qhead, "org") == 0 ||
                    328:            strcasecmp(qhead, "com") == 0 ||
                    329:            strcasecmp(qhead, "net") == 0 ||
                    330:            strcasecmp(qhead, "cat") == 0 ||
                    331:            strcasecmp(qhead, "pro") == 0 ||
                    332:            strcasecmp(qhead, "info") == 0 ||
                    333:            strcasecmp(qhead, "aero") == 0 ||
                    334:            strcasecmp(qhead, "jobs") == 0 ||
                    335:            strcasecmp(qhead, "mobi") == 0 ||
                    336:            strcasecmp(qhead, "museum") == 0 ||
1.47      sthen     337:             /* for others, if whois.nic.TLD doesn't exist, try whois-servers */
                    338:            getaddrinfo(server, NULL, &hints, &res) != 0)) {
1.54      millert   339:                free(server);
                    340:                if (asprintf(&server, "%s%s", qhead, QNICHOST_TAIL) == -1)
                    341:                        err(1, NULL);
1.47      sthen     342:        }
1.58      jca       343:        if (res != NULL)
                    344:                freeaddrinfo(res);
1.47      sthen     345:
1.55      millert   346:        *tofree = server;
1.18      millert   347:        return (server);
1.1       deraadt   348: }
                    349:
1.34      henning   350: __dead void
1.17      millert   351: usage(void)
1.1       deraadt   352: {
1.18      millert   353:        extern char *__progname;
1.7       millert   354:
1.34      henning   355:        fprintf(stderr,
1.61      jmc       356:            "usage: %s [-AadgIilmPQRrS] [-c country-code | -h host] "
1.22      millert   357:                "[-p port] name ...\n", __progname);
1.18      millert   358:        exit(1);
1.1       deraadt   359: }