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

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