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

1.8     ! millert     1: /*     $OpenBSD: whois.c,v 1.7 1999/11/15 01:46:41 millert Exp $       */
1.1       deraadt     2: /*     $NetBSD: whois.c,v 1.5 1994/11/14 05:13:25 jtc Exp $    */
                      3:
                      4: /*
                      5:  * Copyright (c) 1980, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: static char copyright[] =
                     39: "@(#) Copyright (c) 1980, 1993\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
                     45: static char sccsid[] = "@(#)whois.c    8.1 (Berkeley) 6/6/93";
                     46: #endif
1.8     ! millert    47: static char rcsid[] = "$OpenBSD: whois.c,v 1.7 1999/11/15 01:46:41 millert Exp $";
1.1       deraadt    48: #endif /* not lint */
                     49:
                     50: #include <sys/types.h>
                     51: #include <sys/socket.h>
                     52: #include <netinet/in.h>
1.5       art        53: #include <arpa/inet.h>
1.1       deraadt    54: #include <netdb.h>
1.7       millert    55: #include <err.h>
1.1       deraadt    56: #include <stdio.h>
                     57: #include <stdlib.h>
                     58: #include <string.h>
1.4       deraadt    59: #include <sysexits.h>
1.1       deraadt    60: #include <unistd.h>
                     61:
1.7       millert    62: #define        NICHOST         "whois.crsnic.net"
                     63: #define        INICHOST        "whois.internic.net"
                     64: #define        DNICHOST        "whois.nic.mil"
                     65: #define        GNICHOST        "whois.nic.gov"
1.4       deraadt    66: #define        ANICHOST        "whois.arin.net"
                     67: #define        RNICHOST        "whois.ripe.net"
                     68: #define        PNICHOST        "whois.apnic.net"
1.7       millert    69: #define        RUNICHOST       "whois.ripn.net"
1.6       deraadt    70: #define        MNICHOST        "whois.ra.net"
                     71: #define        QNICHOST_TAIL   ".whois-servers.net"
1.4       deraadt    72: #define        WHOIS_PORT      43
1.1       deraadt    73:
1.8     ! millert    74: #define WHOIS_RECURSE          0x01
        !            75: #define WHOIS_INIC_FALLBACK    0x02
        !            76: #define WHOIS_QUICK            0x04
        !            77:
        !            78: static void usage      __P((void));
        !            79: static void whois      __P((char *, struct sockaddr_in *, int));
1.1       deraadt    80:
                     81: int
                     82: main(argc, argv)
                     83:        int argc;
                     84:        char **argv;
                     85: {
1.8     ! millert    86:        int ch, i, j;
        !            87:        int use_qnichost, flags;
1.1       deraadt    88:        char *host;
1.6       deraadt    89:        char *qnichost;
1.8     ! millert    90:        struct servent *sp;
        !            91:        struct hostent *hp;
        !            92:        struct sockaddr_in sin;
1.1       deraadt    93:
1.8     ! millert    94:        host = NULL;
        !            95:        qnichost = NULL;
        !            96:        flags = 0;
1.6       deraadt    97:        use_qnichost = 0;
1.8     ! millert    98:        while ((ch = getopt(argc, argv, "adgh:impqQrR")) != -1)
1.1       deraadt    99:                switch((char)ch) {
1.4       deraadt   100:                case 'a':
                    101:                        host = ANICHOST;
                    102:                        break;
                    103:                case 'd':
                    104:                        host = DNICHOST;
                    105:                        break;
1.7       millert   106:                case 'g':
                    107:                        host = GNICHOST;
                    108:                        break;
1.1       deraadt   109:                case 'h':
                    110:                        host = optarg;
                    111:                        break;
1.7       millert   112:                case 'i':
                    113:                        host = INICHOST;
                    114:                        break;
1.6       deraadt   115:                case 'm':
                    116:                        host = MNICHOST;
                    117:                        break;
1.4       deraadt   118:                case 'p':
                    119:                        host = PNICHOST;
                    120:                        break;
1.6       deraadt   121:                case 'q':
1.8     ! millert   122:                        /* default */
        !           123:                        break;
        !           124:                case 'Q':
        !           125:                        flags |= WHOIS_QUICK;
1.6       deraadt   126:                        break;
1.4       deraadt   127:                case 'r':
                    128:                        host = RNICHOST;
                    129:                        break;
1.7       millert   130:                case 'R':
                    131:                        host = RUNICHOST;
                    132:                        break;
1.1       deraadt   133:                case '?':
                    134:                default:
                    135:                        usage();
                    136:                }
                    137:        argc -= optind;
                    138:        argv += optind;
                    139:
                    140:        if (!argc)
                    141:                usage();
                    142:
1.8     ! millert   143:        memset(&sin, 0, sizeof sin);
        !           144:        sin.sin_len = sizeof(sin);
        !           145:        sin.sin_family = AF_INET;
        !           146:        sp = getservbyname("whois", "tcp");
        !           147:        if (sp == NULL)
        !           148:                sin.sin_port = htons(WHOIS_PORT);
        !           149:        else
        !           150:                sin.sin_port = sp->s_port;
        !           151:
        !           152:        /*
        !           153:         * If no nic host is specified, use whois-servers.net
        !           154:         * if there is a '.' in the name, else fall back to NICHOST.
        !           155:         */
        !           156:        if (host == NULL) {
        !           157:                use_qnichost = 1;
        !           158:                host = NICHOST;
        !           159:                if (!(flags & WHOIS_QUICK))
        !           160:                        flags |= WHOIS_INIC_FALLBACK | WHOIS_RECURSE;
        !           161:        }
        !           162:        while (argc--) {
        !           163:                if (use_qnichost) {
        !           164:                        if (qnichost) {
        !           165:                                free(qnichost);
        !           166:                                qnichost = NULL;
        !           167:                        }
1.6       deraadt   168:                        for (i = j = 0; (*argv)[i]; i++)
1.8     ! millert   169:                                if ((*argv)[i] == '.')
        !           170:                                        j = i;
1.6       deraadt   171:                        if (j != 0) {
1.8     ! millert   172:                                qnichost = (char *) calloc(i - j + 1 +
1.6       deraadt   173:                                    strlen(QNICHOST_TAIL), sizeof(char));
                    174:                                if (!qnichost)
                    175:                                        err(1, "malloc");
                    176:                                strcpy(qnichost, *argv + j + 1);
                    177:                                strcat(qnichost, QNICHOST_TAIL);
1.8     ! millert   178:
        !           179:                                if (inet_aton(qnichost, &sin.sin_addr) == 0) {
        !           180:                                        hp = gethostbyname2(qnichost, AF_INET);
        !           181:                                        if (hp == NULL) {
        !           182:                                                free(qnichost);
        !           183:                                                qnichost = NULL;
        !           184:                                        } else {
        !           185:                                                sin.sin_addr = *(struct in_addr *)hp->h_addr_list[0];
        !           186:                                        }
        !           187:                                }
1.6       deraadt   188:                        }
                    189:                }
1.8     ! millert   190:                if (!qnichost && inet_aton(host, &sin.sin_addr) == 0) {
        !           191:                        hp = gethostbyname2(host, AF_INET);
        !           192:                        if (hp == NULL)
        !           193:                                errx(EX_NOHOST, "%s: %s", host,
        !           194:                                    hstrerror(h_errno));
        !           195:                        host = hp->h_name;
        !           196:                        sin.sin_addr = *(struct in_addr *)hp->h_addr_list[0];
        !           197:                }
        !           198:
        !           199:                whois(*argv++, &sin, flags);
1.6       deraadt   200:        }
1.8     ! millert   201:        exit(0);
        !           202: }
        !           203:
        !           204: static void
        !           205: whois(name, sinp, flags)
        !           206:        char *name;
        !           207:        struct sockaddr_in *sinp;
        !           208:        int flags;
        !           209: {
        !           210:        FILE *sfi, *sfo;
        !           211:        char *buf, *p, *nhost;
        !           212:        size_t len;
        !           213:        int s, nomatch;
1.6       deraadt   214:
1.4       deraadt   215:        s = socket(PF_INET, SOCK_STREAM, 0);
                    216:        if (s < 0)
                    217:                err(EX_OSERR, "socket");
                    218:
1.8     ! millert   219:        if (connect(s, (struct sockaddr *)sinp, sizeof(*sinp)) < 0)
1.4       deraadt   220:                err(EX_OSERR, "connect");
                    221:
1.1       deraadt   222:        sfi = fdopen(s, "r");
                    223:        sfo = fdopen(s, "w");
1.4       deraadt   224:        if (sfi == NULL || sfo == NULL)
                    225:                err(EX_OSERR, "fdopen");
1.8     ! millert   226:        (void)fprintf(sfo, "%s\r\n", name);
1.1       deraadt   227:        (void)fflush(sfo);
1.8     ! millert   228:        nhost = NULL;
        !           229:        nomatch = 0;
        !           230:        while ((buf = fgetln(sfi, &len))) {
        !           231:                if (buf[len - 2] == '\r')
        !           232:                        buf[len - 2] = '\0';
        !           233:                else
        !           234:                        buf[len - 1] = '\0';
        !           235:
        !           236:                if ((flags & WHOIS_RECURSE) && !nhost &&
        !           237:                    (p = strstr(buf, "Whois Server: "))) {
        !           238:                        p += sizeof("Whois Server: ") - 1;
        !           239:                        if ((len = strcspn(p, " \t\n\r"))) {
        !           240:                                if ((nhost = malloc(len + 1)) == NULL)
        !           241:                                        err(1, "malloc");
        !           242:                                memcpy(nhost, p, len);
        !           243:                                nhost[len] = '\0';
        !           244:                        }
        !           245:                }
        !           246:                if ((flags & WHOIS_INIC_FALLBACK) && !nhost && !nomatch &&
        !           247:                    (p = strstr(buf, "No match for \""))) {
        !           248:                        p += sizeof("No match for \"") - 1;
        !           249:                        if ((len = strcspn(p, "\"")) &&
        !           250:                            strncasecmp(name, p, len) == 0)
        !           251:                                nomatch = 1;
        !           252:                }
        !           253:                (void)puts(buf);
        !           254:        }
        !           255:
        !           256:        /* Do second lookup as needed */
        !           257:        if (nomatch && !nhost) {
        !           258:                (void)printf("Looking up %s at %s.\n\n", name, INICHOST);
        !           259:                nhost = INICHOST;
        !           260:        }
        !           261:        if (nhost) {
        !           262:                if (inet_aton(nhost, &sinp->sin_addr) == 0) {
        !           263:                        struct hostent *hp = gethostbyname2(nhost, AF_INET);
        !           264:                        if (hp == NULL)
        !           265:                                return;
        !           266:                        sinp->sin_addr = *(struct in_addr *)hp->h_addr_list[0];
        !           267:                }
        !           268:                if (!nomatch)
        !           269:                        free(nhost);
        !           270:                whois(name, sinp, 0);
        !           271:        }
1.1       deraadt   272: }
                    273:
                    274: static void
                    275: usage()
                    276: {
1.7       millert   277:
                    278:        (void)fprintf(stderr,
                    279:            "usage: whois [-adgimpqrR] [-h hostname] name ...\n");
1.4       deraadt   280:        exit(EX_USAGE);
1.1       deraadt   281: }