Home | History | Annotate | Line # | Download | only in whois
whois.c revision 1.12.4.2
      1 /*	$NetBSD: whois.c,v 1.12.4.2 2000/09/30 14:07:53 jhawk Exp $	*/
      2 
      3 /*
      4  * RIPE version marten (at) ripe.net
      5  * many changes & networkupdate by david (at) ripe.net
      6  * cosmetics by steven (at) dante.org.uk --	gcc stopped complaining mostly,
      7  *					code is still messy, though.
      8  *
      9  * 1.15 94/09/07
     10  *
     11  * 1.2  9705/02
     12  * "-v" option added; ambrose (at) ripe.net
     13  * "whois.ripe.net" replaced by "bsdbase.ripe.net";  ambrose (at) ripe.net
     14  * "bsdbase.ripe.net" replaced by "joshua.ripe.net"; marek (at) ripe.net
     15  * "joshua.ripe.net" replaced by "whois.ripe.net"; roman (at) ripe.net 981105
     16  *
     17  * Copyright (c) 1980 Regents of the University of California.
     18  * All rights reserved.
     19  *
     20  * Redistribution and use in source and binary forms, with or without
     21  * modification, are permitted provided that the following conditions
     22  * are met:
     23  * 1. Redistributions of source code must retain the above copyright
     24  *    notice, this list of conditions and the following disclaimer.
     25  * 2. Redistributions in binary form must reproduce the above copyright
     26  *    notice, this list of conditions and the following disclaimer in the
     27  *    documentation and/or other materials provided with the distribution.
     28  * 3. All advertising materials mentioning features or use of this software
     29  *    must display the following acknowledgement:
     30  *	This product includes software developed by the University of
     31  *	California, Berkeley and its contributors.
     32  * 4. Neither the name of the University nor the names of its contributors
     33  *    may be used to endorse or promote products derived from this software
     34  *    without specific prior written permission.
     35  *
     36  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     37  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     39  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     40  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     41  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     42  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     44  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     46  * SUCH DAMAGE.
     47  */
     48 
     49 #include <sys/cdefs.h>
     50 #if defined(sun) && defined(solaris)
     51 #define SYSV
     52 #endif
     53 
     54 #ifndef lint
     55 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
     56 	The Regents of the University of California.  All rights reserved.\n");
     57 #endif /* not lint */
     58 
     59 #ifndef RIPE
     60 #ifndef lint
     61 #if 0
     62 static char sccsid[] = "@(#)whois.c	8.1 (Berkeley) 6/6/93";
     63 #else
     64 <<<<<<< whois.c
     65 __RCSID("$NetBSD: whois.c,v 1.12.4.2 2000/09/30 14:07:53 jhawk Exp $");
     66 =======
     67 __RCSID("$NetBSD: whois.c,v 1.12.4.2 2000/09/30 14:07:53 jhawk Exp $");
     68 >>>>>>> 1.14
     69 #endif
     70 #endif /* not lint */
     71 #endif /* not RIPE */
     72 
     73 #ifdef RIPE
     74 #ifndef lint
     75 char sccsid[] =
     76     "@(#)whois.c 5.11 (Berkeley) 3/2/91 - RIPE 1.15 94/09/07 marten (at) ripe.net";
     77 #endif /* not lint */
     78 #endif /* RIPE */
     79 
     80 #include <sys/types.h>
     81 #include <sys/socket.h>
     82 #include <sys/param.h>
     83 #include <netinet/in.h>
     84 #include <err.h>
     85 #include <netdb.h>
     86 #include <stdio.h>
     87 #include <stdlib.h>
     88 #include <ctype.h>
     89 #include <string.h>
     90 #include <errno.h>
     91 #include <unistd.h>
     92 #include <pwd.h>
     93 #include <signal.h>
     94 
     95 #if defined(SYSV)
     96 #include	<crypt.h>
     97 #endif /* SYSV */
     98 
     99 #ifndef __NetBSD__
    100 #ifdef __STDC__
    101 extern int	getopt(int argc, char * const *argv, const char *optstring);
    102 extern int	kill(pid_t pid, int sig);
    103 extern FILE	*fdopen(int fildes, const char *type);
    104 extern int	gethostname(char *name, int namelen);
    105 #else /* !__STDC__ */
    106 extern int	gethostname();
    107 #endif /* __STDC__ */
    108 #endif /* __NetBSD__ */
    109 
    110 #if defined(SYSV) || defined(__STDC__)
    111 
    112 #define		index(s,c)		strchr((const char*)(s),(int)(c))
    113 #define		rindex(s,c)		strrchr((const char*)(s),(int)(c))
    114 #define		bzero(s,n)		memset((void*)s,0,(size_t)n)
    115 
    116 #ifdef HASMEMMOVE
    117 # define	bcopy(s,d,n)	memmove((void*)(d),(void*)(s),(size_t)(n))
    118 #else
    119 # define	bcopy(s,d,n)	memcpy((void*)(d),(void*)(s),(size_t)(n))
    120 #endif /* HASMEMMOVE */
    121 
    122 #endif /* SYSV || __STDC__ */
    123 
    124 #ifdef GLIBC
    125 typedef __u_short u_short;
    126 typedef __caddr_t caddr_t;
    127 #endif /* GLIBC */
    128 
    129 /*
    130 
    131 # the following defines can be used but are not fully functional anymore...
    132 #
    133 # CLEVER- Use a educated guess of the whereabouts of the nearest server
    134 #	  This is done by taking the top-level domain of the current
    135 #	  machine, and looking for a CNAME record for a server with name
    136 #	  <top-level-domain>-whois.ripe.net
    137 #	  If this machine does not exsist or the current machine's top-level
    138 #	  domain could not be found,it will fall back to whois.ripe.net
    139 #	  the default for this RIPE version of whois
    140 #	  The CLEVER option implies the RIPE option.
    141 
    142 # TOPDOMAIN=\"<top-level-domain>\"
    143 #	- This option will fix the default host to be
    144 #	  <top-level-domain>-whois.ripe.net, which may point to a secondary
    145 #	  server inside your top-level domain. If there is no such secondary
    146 #	  server, it will point to whois.ripe.net, the default. This option
    147 #	  overrules the CLEVER option.
    148 #	  The TOPDOMAIN option implies the RIPE option.
    149 
    150 */
    151 
    152 #if defined(TOPDOMAIN) || defined(CLEVER)
    153 #ifndef RIPE
    154 #define RIPE
    155 #endif /* !RIPE */
    156 #endif /* TOPDOMAIN || CLEVER */
    157 
    158 #if defined(RIPE) && !defined(__NetBSD__)
    159 #include <sys/param.h>
    160 #define NICHOST "whois.ripe.net"
    161 #else
    162 #define NICHOST "whois.internic.net"
    163 #endif
    164 
    165 int main __P((int, char **));
    166 static void usage __P((void));
    167 static void closesocket __P((int, int));
    168 static void termhandler __P((int));
    169 
    170 void usage()
    171 {
    172 #ifdef RIPE
    173 #ifdef NETWORKUPDATE
    174   (void)fprintf(stderr, "\nUsage: networkupdate [-46] [-h hostname] [-p port]");
    175 #else
    176   (void)fprintf(stderr, "\nUsage: whois [-46aFLmMrSvR] [-h hostname] [-s sources] [-T types] [-i attr] keys\n");
    177   (void)fprintf(stderr, "       whois -t type");
    178   (void)fprintf(stderr, "       whois -v type");
    179 #endif
    180 #else
    181   (void)fprintf(stderr, "\nUsage: whois [-46] [-h hostname] [-p port] name ...");
    182 #endif
    183   (void)fprintf(stderr, "\n\nWhere:\n\n");
    184   (void)fprintf(stderr, "-4                         Use IPv4 Only\n");
    185   (void)fprintf(stderr, "-6                         Use IPv6 Only\n");
    186 #ifdef RIPE
    187 #ifndef NETWORKUPDATE
    188   (void)fprintf(stderr, "-a                         search all databases\n");
    189   (void)fprintf(stderr, "-F                         fast raw output\n");
    190 #endif
    191 #endif
    192   (void)fprintf(stderr, "-h hostname                search alternate server\n");
    193 #ifdef RIPE
    194 #ifndef NETWORKUPDATE
    195   (void)fprintf(stderr, "-i [attr][[,attr] ... ]    do an inverse lookup for specified attributes\n");
    196   (void)fprintf(stderr, "-L                         find all Less specific matches\n");
    197   (void)fprintf(stderr, "-m                         find first level more specific matches\n");
    198   (void)fprintf(stderr, "-M                         find all More specific matches\n");
    199 #endif
    200 #endif
    201   (void)fprintf(stderr, "-p port                    port to connect to\n");
    202 #ifdef RIPE
    203 #ifndef NETWORKUPDATE
    204   (void)fprintf(stderr, "-r                         turn off recursive lookups\n");
    205   (void)fprintf(stderr, "-s source[[,source] ... ]  search databases with source 'source'\n");
    206   (void)fprintf(stderr, "-S                         tell server to leave out 'syntactic sugar'\n");
    207   (void)fprintf(stderr, "-t type                    requests template for object of type 'type'\n");
    208   (void)fprintf(stderr, "-v type                    requests verbose template for object of type 'type'\n");
    209   (void)fprintf(stderr, "-R                         force to show local copy of the domain object even if it contains referral\n");
    210   (void)fprintf(stderr, "-T type[[,type] ... ]      only look for objects of type 'type'\n\n");
    211   (void)fprintf(stderr, "Please note that most of these flags are NOT understood by\n");
    212   (void)fprintf(stderr, "non RIPE whois servers\n");
    213 #endif
    214 #endif
    215   (void)fprintf(stderr, "\n");
    216 
    217   exit(1);
    218 }
    219 
    220 int s;
    221 
    222 void closesocket(s, child)
    223 int s, child;
    224 {
    225   /* printf("close connection child=%i\n", child);  */
    226 
    227   close(s);
    228 
    229 #ifdef NETWORKUPDATE
    230   if (child==0) {
    231      kill(getppid(), SIGTERM);
    232   }
    233 #endif
    234 
    235   exit(0);
    236 
    237 }
    238 
    239 void termhandler(sig)
    240 int sig;
    241 {
    242   closesocket(s,1);
    243 }
    244 
    245 
    246 #ifdef RIPE
    247 #if defined(__STDC__) || defined(SYSV)
    248 #define occurs(str,pat)		((int) strstr((str),(pat)))
    249 #else /* !__STDC__ && !SYSV */
    250 int occurs(str, pat)
    251      char *str, *pat;
    252 {
    253   register char *point = str;
    254 
    255   while ((point=index(point, *pat)))
    256     {
    257       if (strncmp(point, pat, strlen(pat)) == 0)
    258 	return(1);
    259       point++;
    260     }
    261   return(0);
    262 }
    263 #endif
    264 #endif
    265 
    266 int main(argc, argv)
    267      int argc;
    268      char **argv;
    269 {
    270   extern char *optarg;
    271   extern int optind;
    272   FILE *sfi;
    273   FILE *sfo;
    274   int ch;
    275   struct addrinfo *dst, hints;
    276   int af=PF_UNSPEC;
    277   int error;
    278   char *host, *whoishost;
    279   int optp=0;
    280   char *optport="whois";
    281 #ifdef DEBUG
    282   int verb=1;
    283 #else /*DEBUG */
    284   int verb=0;
    285 #endif
    286 #ifdef RIPE
    287   int opthost=0;
    288 #ifndef NETWORKUPDATE
    289   /* normal whois client */
    290   char *string;
    291   int alldatabases=0;
    292   int optsource=0, optrecur=0, optfast=0, opttempl=0, optverbose=0;
    293   int optobjtype=0, optsugar=0, optinverselookup=0, optgetupdates=0;
    294   int optL=0, optm=0, optM=0, optchanged=0, optnonreferral=0;
    295   char	*source=NULL, *templ=NULL, *verbose=NULL, *objtype=NULL,
    296 	*inverselookup=NULL, *getupdates=NULL;
    297 #else /* NETWORKUPDATE */
    298   /* networkupdate client */
    299   int prev;
    300   char domainname[64]; /* that's what sys/param.h says */
    301   struct passwd *passwdentry;
    302   int child;
    303 #endif
    304 #ifdef CLEVER
    305   int  myerror;
    306   char *mytoplevel;
    307   char *myhost;
    308 #endif
    309 #endif
    310 
    311 #ifdef TOPDOMAIN
    312   host = strcat(TOPDOMAIN, "-whois.ripe.net");
    313 #else
    314   host = NICHOST;
    315 #endif
    316 
    317 #ifdef RIPE
    318 #ifdef NETWORKUPDATE
    319     while ((ch = getopt(argc, argv, "46h:p:")) != EOF)
    320 #else
    321   while ((ch = getopt(argc, argv, "46acFg:h:i:LmMp:rs:SRt:T:v:")) != EOF)
    322 #endif
    323 #else
    324     while ((ch = getopt(argc, argv, "46h:p:")) != EOF)
    325 #endif
    326       switch((char)ch) {
    327       case '4':
    328 	af = PF_INET;
    329 	break;
    330       case '6':
    331 	af = PF_INET6;
    332 	break;
    333       case 'h':
    334 	host = optarg;
    335 	opthost = 1;
    336 	break;
    337       case 'p':
    338 	optport=optarg;
    339         optp =1;
    340         break;
    341 #ifdef RIPE
    342 #ifndef NETWORKUPDATE
    343       case 'a':
    344 	alldatabases=1;
    345 	break;
    346       case 'c':
    347         optchanged=1;
    348         break;
    349       case 'F':
    350 	optfast = 1;
    351 	break;
    352       case 'g':
    353         getupdates=optarg;
    354 	optgetupdates=1;
    355 	break;
    356       case 'i':
    357         inverselookup=optarg;
    358 	optinverselookup = 1;
    359 	break;
    360       case 'L':
    361 	if (optM || optm) {
    362           fprintf(stderr, "Only one of -L, -m or -M allowed\n\n");
    363           usage();
    364         }
    365 	optL=1;
    366 	break;
    367       case 'm':
    368 	if (optM || optL) {
    369 	  fprintf(stderr, "Only one of -L, -m or -M allowed\n\n");
    370           usage();
    371 	}
    372 	optm=1;
    373 	break;
    374       case 'M':
    375 	if (optL || optm) {
    376 	  fprintf(stderr, "Only one of -L, -m or -M allowed\n\n");
    377 	  usage();
    378 	}
    379 	optM=1;
    380 	break;
    381 
    382       case 's':
    383 	source = optarg;
    384 	optsource=1;
    385 	break;
    386       case 'S':
    387 	optsugar=1;
    388 	break;
    389       case 'R':
    390 	optnonreferral=1;
    391 	break;
    392       case 'r':
    393 	optrecur=1;
    394 	break;
    395       case 't':
    396 	 templ=optarg;
    397 	 opttempl=1;
    398 	 break;
    399       case 'v':
    400 	 verbose=optarg;
    401 	 optverbose=1;
    402 	 break;
    403       case 'T':
    404 	objtype=optarg;
    405 	optobjtype=1;
    406 	break;
    407 
    408 #endif
    409 #endif
    410       case '?':
    411       default:
    412 	usage();
    413       }
    414   argc -= optind;
    415   argv += optind;
    416 
    417 #ifdef RIPE
    418 #ifdef NETWORKUPDATE
    419   if (argc>0)
    420     usage();
    421 #else
    422   if ((argc<=0) && !opttempl && !optverbose && !optgetupdates && (!(opttempl && optgetupdates)))
    423     usage();
    424 #endif
    425 #else
    426   if (argc<=0)
    427     usage();
    428 #endif
    429 
    430   if (!opthost) {
    431 
    432 #ifdef CLEVER
    433     whoishost=(char *)calloc(MAXHOSTNAMELEN, sizeof(char));
    434     myhost =(char *)calloc(MAXHOSTNAMELEN, sizeof(char));
    435     myerror = gethostname(myhost, MAXHOSTNAMELEN);
    436     if (myerror >= 0) {
    437       if (occurs(myhost, ".")) {
    438 	mytoplevel = rindex(myhost,'.');
    439 	mytoplevel++;
    440 	(void) sprintf(whoishost, "%s-whois.ripe.net", mytoplevel);
    441 	if (verb) fprintf(stderr, "Clever guess: %s\n", whoishost);
    442       }
    443     }
    444 
    445     memset(&hints, 0, sizeof(hints));
    446     hints.ai_flags = AI_CANONNAME;
    447     hints.ai_family = af;
    448     hints.ai_socktype = SOCK_STREAM;
    449     hints.ai_protocol = 0;
    450     error = getaddrinfo(host, optport, &hints, &dst);
    451     if ((error) && (verb))
    452       fprintf(stderr,"No such host: %s\n", whoishost);
    453     if (error) {
    454 #endif
    455 
    456       whoishost=NICHOST;
    457 
    458       if (verb)
    459 	fprintf(stderr, "Default host: %s\n\n", whoishost);
    460       memset(&hints, 0, sizeof(hints));
    461       hints.ai_flags = AI_CANONNAME;
    462       hints.ai_family = af;
    463       hints.ai_socktype = SOCK_STREAM;
    464       hints.ai_protocol = 0;
    465       error = getaddrinfo(host, optport , &hints, &dst);
    466       if (error) {
    467 	fprintf(stderr,"No such host: %s\n", whoishost);
    468 	if (verb) fprintf(stderr, "Now I give up ...\n");
    469 	perror("Unknown host");
    470 	exit(1);
    471       }
    472 
    473 #ifdef CLEVER
    474     }
    475 #endif
    476   }
    477   else {
    478     if (verb)
    479       fprintf(stderr, "Trying: %s\n\n", host);
    480     memset(&hints, 0, sizeof(hints));
    481     hints.ai_flags = AI_CANONNAME;
    482     hints.ai_family = af;
    483     hints.ai_socktype = SOCK_STREAM;
    484     hints.ai_protocol = 0;
    485     error = getaddrinfo(host, optport, &hints, &dst);
    486     if (error) {
    487       (void)fprintf(stderr, "whois: %s: ", host);
    488       perror("Unknown host");
    489       exit(1);
    490     }
    491   }
    492 
    493   for (/*nothing*/; dst; dst = dst->ai_next) {
    494     s = socket(dst->ai_family, dst->ai_socktype, dst->ai_protocol);
    495     if (s < 0)
    496       continue;
    497     if (connect(s, dst->ai_addr, dst->ai_addrlen) < 0) {
    498       close(s);
    499       if (verb) (void)fprintf(stderr, "whois: connect miss\n");
    500       continue;
    501     }
    502     /*okay*/
    503     break;
    504   }
    505   if (dst == NULL) {
    506     perror("whois: connect");
    507     exit(1);
    508   }
    509   if (verb) (void)fprintf(stderr, "whois: connect success\n");
    510 
    511 #ifndef NETWORKUPDATE
    512   sfi = fdopen(s, "r");
    513   sfo = fdopen(s, "w");
    514   if (sfi == NULL || sfo == NULL) {
    515     perror("whois: fdopen");
    516     (void)close(s);
    517     exit(1);
    518   }
    519 #endif
    520 
    521   signal(SIGTERM, termhandler);
    522 
    523 #ifdef RIPE
    524 #ifdef NETWORKUPDATE
    525 
    526   if ((child=fork())==0) {
    527 
    528      sfo = fdopen(s, "w");
    529      if (sfo == NULL) {
    530        perror("whois: fdopen");
    531        (void)close(s);
    532        exit(1);
    533      }
    534 
    535      if (gethostname(domainname, sizeof(domainname))) {
    536         fprintf(stderr, "error when doing gethostname()");
    537         exit(-1);
    538      }
    539 
    540      passwdentry=getpwuid(getuid());
    541 
    542      fprintf(sfo, "-Vnc2.0 -U %s %s\n", passwdentry->pw_name, domainname);
    543      fflush(sfo);
    544 
    545      prev='\0';
    546 
    547      while ((ch=getchar()) != EOF) {
    548 
    549         fputc(ch, sfo);
    550 
    551         if (ch=='\n') fflush(sfo);
    552         if (feof(sfo)) closesocket(s, child);
    553         if ((ch=='.') && (prev=='\n')) closesocket(s, child);
    554         if (!isspace(ch) || ((!isspace(prev)) && (ch=='\n'))) prev=ch;
    555      }
    556 
    557      closesocket(s, child);
    558 
    559   }
    560 
    561   sfi = fdopen(s, "r");
    562   if (sfi == NULL) {
    563        perror("whois: fdopen");
    564        (void)close(s);
    565        exit(1);
    566   }
    567 
    568 #else
    569 
    570   if (alldatabases)
    571     (void)fprintf(sfo, "-a ");
    572   if (optchanged)
    573     (void)fprintf(sfo, "-c ");
    574   if (optfast)
    575     (void)fprintf(sfo, "-F ");
    576   if (optgetupdates)
    577     (void)fprintf(sfo, "-g %s ", getupdates);
    578   if (optinverselookup)
    579     (void)fprintf(sfo, "-i %s ", inverselookup);
    580   if (optL)
    581     (void)fprintf(sfo, "-L ");
    582   if (optm)
    583     (void)fprintf(sfo, "-m ");
    584   if (optM)
    585     (void)fprintf(sfo, "-M ");
    586   if (optrecur)
    587     (void)fprintf(sfo, "-r ");
    588   if (optsource)
    589     (void)fprintf(sfo, "-s %s ", source);
    590   if (optsugar)
    591     (void)fprintf(sfo, "-S ");
    592   if (optnonreferral)
    593     (void)fprintf(sfo, "-R ");
    594   if (opttempl)
    595     (void)fprintf(sfo, "-t %s ", templ);
    596   if (optverbose)
    597     (void)fprintf(sfo, "-v %s ", verbose);
    598   if (optobjtype)
    599     (void)fprintf(sfo, "-T %s ", objtype);
    600 
    601   /* we can only send the -V when we are sure that we are dealing with
    602      a RIPE whois server :-( */
    603 
    604   whoishost=(char *)calloc(strlen(host)+1, sizeof(char));
    605   strcpy(whoishost, host);
    606   for (string=whoishost;(*string=(char)tolower(*string));string++);
    607 
    608   if (strstr(whoishost, "ripe.net") ||
    609       strstr(whoishost, "ra.net") ||
    610       strstr(whoishost, "apnic.net") ||
    611       strstr(whoishost, "mci.net") ||
    612       strstr(whoishost, "isi.edu") ||
    613       strstr(whoishost, "garr.it") ||
    614       strstr(whoishost, "ans.net") ||
    615       alldatabases || optfast || optgetupdates || optinverselookup ||
    616       optL || optm || optM || optrecur || optsugar || optsource ||
    617       opttempl || optverbose || optobjtype)
    618     (void)fprintf(sfo, "-VwC2.0 ");
    619 #endif
    620 #endif
    621 
    622 #ifndef NETWORKUPDATE
    623   while (argc-- > 1)
    624     (void)fprintf(sfo, "%s ", *argv++);
    625    if (*argv) (void)fputs(*argv, sfo);
    626    (void)fputs("\r\n", sfo);
    627   (void)fflush(sfo);
    628 #endif
    629 
    630   while ((ch = getc(sfi)) != EOF)
    631     putchar(ch);
    632 
    633   closesocket(s, 1);
    634 
    635   exit(0);
    636 
    637 }
    638