Home | History | Annotate | Line # | Download | only in whois
whois.c revision 1.20
      1 /*	$NetBSD: whois.c,v 1.20 2003/07/14 09:21:03 itojun 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 
     51 #ifndef lint
     52 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
     53 	The Regents of the University of California.  All rights reserved.\n");
     54 #endif /* not lint */
     55 
     56 #ifndef RIPE
     57 #ifndef lint
     58 #if 0
     59 static char sccsid[] = "@(#)whois.c	8.1 (Berkeley) 6/6/93";
     60 #else
     61 __RCSID("$NetBSD: whois.c,v 1.20 2003/07/14 09:21:03 itojun Exp $");
     62 #endif
     63 #endif /* not lint */
     64 #endif /* not RIPE */
     65 
     66 #ifdef RIPE
     67 #ifndef lint
     68 char sccsid[] =
     69     "@(#)whois.c 5.11 (Berkeley) 3/2/91 - RIPE 1.15 94/09/07 marten (at) ripe.net";
     70 #endif /* not lint */
     71 #ifndef lint
     72 __RCSID("$NetBSD: whois.c,v 1.20 2003/07/14 09:21:03 itojun Exp $");
     73 #endif
     74 
     75 #endif /* RIPE */
     76 
     77 #include <sys/types.h>
     78 #include <sys/socket.h>
     79 #include <sys/param.h>
     80 #include <netinet/in.h>
     81 #include <err.h>
     82 #include <netdb.h>
     83 #include <stdio.h>
     84 #include <stdlib.h>
     85 #include <ctype.h>
     86 #include <string.h>
     87 #include <errno.h>
     88 #include <unistd.h>
     89 #include <pwd.h>
     90 #include <signal.h>
     91 
     92 #define		index(s,c)	strchr((const char*)(s),(int)(c))
     93 #define		rindex(s,c)	strrchr((const char*)(s),(int)(c))
     94 #define		bzero(s,n)	memset((void*)s,0,(size_t)n)
     95 
     96 #ifdef HASMEMMOVE
     97 # define	bcopy(s,d,n)	memmove((void*)(d),(void*)(s),(size_t)(n))
     98 #else
     99 # define	bcopy(s,d,n)	memcpy((void*)(d),(void*)(s),(size_t)(n))
    100 #endif /* HASMEMMOVE */
    101 
    102 /*
    103 
    104 # the following defines can be used but are not fully functional anymore...
    105 #
    106 # CLEVER- Use a educated guess of the whereabouts of the nearest server
    107 #	  This is done by taking the top-level domain of the current
    108 #	  machine, and looking for a CNAME record for a server with name
    109 #	  <top-level-domain>-whois.ripe.net
    110 #	  If this machine does not exsist or the current machine's top-level
    111 #	  domain could not be found,it will fall back to whois.ripe.net
    112 #	  the default for this RIPE version of whois
    113 #	  The CLEVER option implies the RIPE option.
    114 
    115 # TOPDOMAIN=\"<top-level-domain>\"
    116 #	- This option will fix the default host to be
    117 #	  <top-level-domain>-whois.ripe.net, which may point to a secondary
    118 #	  server inside your top-level domain. If there is no such secondary
    119 #	  server, it will point to whois.ripe.net, the default. This option
    120 #	  overrules the CLEVER option.
    121 #	  The TOPDOMAIN option implies the RIPE option.
    122 
    123 */
    124 
    125 #if defined(TOPDOMAIN) || defined(CLEVER)
    126 #ifndef RIPE
    127 #define RIPE
    128 #endif /* !RIPE */
    129 #endif /* TOPDOMAIN || CLEVER */
    130 
    131 #define NICHOST "whois.internic.net"
    132 
    133 int main(int, char **);
    134 static void usage(void);
    135 static void closesocket(int, int);
    136 static void termhandler(int);
    137 
    138 void
    139 usage(void)
    140 {
    141 #ifdef RIPE
    142 #ifdef NETWORKUPDATE
    143   (void)fprintf(stderr, "\nUsage: networkupdate [-46] [-h hostname] [-p port]");
    144 #else
    145   (void)fprintf(stderr, "\nUsage: whois [-46aFLmMrSvR] [-h hostname] [-s sources] [-T types] [-i attr] keys\n");
    146   (void)fprintf(stderr, "       whois -t type");
    147   (void)fprintf(stderr, "       whois -v type");
    148 #endif
    149 #else
    150   (void)fprintf(stderr, "\nUsage: whois [-46] [-h hostname] [-p port] name ...");
    151 #endif
    152   (void)fprintf(stderr, "\n\nWhere:\n\n");
    153   (void)fprintf(stderr, "-4                         Use IPv4 Only\n");
    154   (void)fprintf(stderr, "-6                         Use IPv6 Only\n");
    155 #ifdef RIPE
    156 #ifndef NETWORKUPDATE
    157   (void)fprintf(stderr, "-a                         search all databases\n");
    158   (void)fprintf(stderr, "-F                         fast raw output\n");
    159 #endif
    160 #endif
    161   (void)fprintf(stderr, "-h hostname                search alternate server\n");
    162 #ifdef RIPE
    163 #ifndef NETWORKUPDATE
    164   (void)fprintf(stderr, "-i [attr][[,attr] ... ]    do an inverse lookup for specified attributes\n");
    165   (void)fprintf(stderr, "-L                         find all Less specific matches\n");
    166   (void)fprintf(stderr, "-m                         find first level more specific matches\n");
    167   (void)fprintf(stderr, "-M                         find all More specific matches\n");
    168 #endif
    169 #endif
    170   (void)fprintf(stderr, "-p port                    port to connect to\n");
    171 #ifdef RIPE
    172 #ifndef NETWORKUPDATE
    173   (void)fprintf(stderr, "-r                         turn off recursive lookups\n");
    174   (void)fprintf(stderr, "-s source[[,source] ... ]  search databases with source 'source'\n");
    175   (void)fprintf(stderr, "-S                         tell server to leave out 'syntactic sugar'\n");
    176   (void)fprintf(stderr, "-t type                    requests template for object of type 'type'\n");
    177   (void)fprintf(stderr, "-v type                    requests verbose template for object of type 'type'\n");
    178   (void)fprintf(stderr, "-R                         force to show local copy of the domain object even if it contains referral\n");
    179   (void)fprintf(stderr, "-T type[[,type] ... ]      only look for objects of type 'type'\n\n");
    180   (void)fprintf(stderr, "Please note that most of these flags are NOT understood by\n");
    181   (void)fprintf(stderr, "non RIPE whois servers\n");
    182 #endif
    183 #endif
    184   (void)fprintf(stderr, "\n");
    185 
    186   exit(1);
    187 }
    188 
    189 int s;
    190 
    191 void
    192 closesocket(int s, int child)
    193 {
    194   /* printf("close connection child=%i\n", child);  */
    195 
    196   close(s);
    197 
    198 #ifdef NETWORKUPDATE
    199   if (child==0) {
    200      kill(getppid(), SIGTERM);
    201   }
    202 #endif
    203 
    204   exit(0);
    205 
    206 }
    207 
    208 void
    209 termhandler(int sig)
    210 {
    211   closesocket(s,1);
    212 }
    213 
    214 
    215 #ifdef RIPE
    216 #define occurs(str,pat)		((int) strstr((str),(pat)))
    217 #endif
    218 
    219 int
    220 main(int argc, char *argv[])
    221 {
    222   extern char *optarg;
    223   extern int optind;
    224   FILE *sfi;
    225   FILE *sfo;
    226   int ch;
    227   struct addrinfo *dst, hints;
    228   int af=PF_UNSPEC;
    229   int error;
    230   char *host, *whoishost;
    231   int optp=0;
    232   char *optport="whois";
    233 #ifdef DEBUG
    234   int verb=1;
    235 #else /*DEBUG */
    236   int verb=0;
    237 #endif
    238 #ifdef RIPE
    239   int opthost=0;
    240 #ifndef NETWORKUPDATE
    241   /* normal whois client */
    242   char *string;
    243   int alldatabases=0;
    244   int optsource=0, optrecur=0, optfast=0, opttempl=0, optverbose=0;
    245   int optobjtype=0, optsugar=0, optinverselookup=0, optgetupdates=0;
    246   int optL=0, optm=0, optM=0, optchanged=0, optnonreferral=0;
    247   char	*source=NULL, *templ=NULL, *verbose=NULL, *objtype=NULL,
    248 	*inverselookup=NULL, *getupdates=NULL;
    249 #else /* NETWORKUPDATE */
    250   /* networkupdate client */
    251   int prev;
    252   char domainname[64]; /* that's what sys/param.h says */
    253   struct passwd *passwdentry;
    254   int child;
    255 #endif
    256 #ifdef CLEVER
    257   int  myerror;
    258   char *mytoplevel;
    259   char *myhost;
    260 #endif
    261 #endif
    262 
    263 #ifdef TOPDOMAIN
    264   char topdomain[] = TOPDOMAIN "-whois.ripe.net";
    265   host = topdomain;
    266 #else
    267   host = NICHOST;
    268 #endif
    269 
    270 #ifdef RIPE
    271 #ifdef NETWORKUPDATE
    272     while ((ch = getopt(argc, argv, "46h:p:")) != -1)
    273 #else
    274   while ((ch = getopt(argc, argv, "46acFg:h:i:LmMp:rs:SRt:T:v:")) != -1)
    275 #endif
    276 #else
    277     while ((ch = getopt(argc, argv, "46h:p:")) != -1)
    278 #endif
    279       switch((char)ch) {
    280       case '4':
    281 	af = PF_INET;
    282 	break;
    283       case '6':
    284 	af = PF_INET6;
    285 	break;
    286       case 'h':
    287 	host = optarg;
    288 	opthost = 1;
    289 	break;
    290       case 'p':
    291 	optport=optarg;
    292         optp =1;
    293         break;
    294 #ifdef RIPE
    295 #ifndef NETWORKUPDATE
    296       case 'a':
    297 	alldatabases=1;
    298 	break;
    299       case 'c':
    300         optchanged=1;
    301         break;
    302       case 'F':
    303 	optfast = 1;
    304 	break;
    305       case 'g':
    306         getupdates=optarg;
    307 	optgetupdates=1;
    308 	break;
    309       case 'i':
    310         inverselookup=optarg;
    311 	optinverselookup = 1;
    312 	break;
    313       case 'L':
    314 	if (optM || optm) {
    315           fprintf(stderr, "Only one of -L, -m or -M allowed\n\n");
    316           usage();
    317         }
    318 	optL=1;
    319 	break;
    320       case 'm':
    321 	if (optM || optL) {
    322 	  fprintf(stderr, "Only one of -L, -m or -M allowed\n\n");
    323           usage();
    324 	}
    325 	optm=1;
    326 	break;
    327       case 'M':
    328 	if (optL || optm) {
    329 	  fprintf(stderr, "Only one of -L, -m or -M allowed\n\n");
    330 	  usage();
    331 	}
    332 	optM=1;
    333 	break;
    334 
    335       case 's':
    336 	source = optarg;
    337 	optsource=1;
    338 	break;
    339       case 'S':
    340 	optsugar=1;
    341 	break;
    342       case 'R':
    343 	optnonreferral=1;
    344 	break;
    345       case 'r':
    346 	optrecur=1;
    347 	break;
    348       case 't':
    349 	 templ=optarg;
    350 	 opttempl=1;
    351 	 break;
    352       case 'v':
    353 	 verbose=optarg;
    354 	 optverbose=1;
    355 	 break;
    356       case 'T':
    357 	objtype=optarg;
    358 	optobjtype=1;
    359 	break;
    360 
    361 #endif
    362 #endif
    363       case '?':
    364       default:
    365 	usage();
    366       }
    367   argc -= optind;
    368   argv += optind;
    369 
    370 #ifdef RIPE
    371 #ifdef NETWORKUPDATE
    372   if (argc>0)
    373     usage();
    374 #else
    375   if ((argc<=0) && !opttempl && !optverbose && !optgetupdates && (!(opttempl && optgetupdates)))
    376     usage();
    377 #endif
    378 #else
    379   if (argc<=0)
    380     usage();
    381 #endif
    382 
    383   if (!opthost) {
    384 
    385 #ifdef CLEVER
    386     whoishost=(char *)calloc(MAXHOSTNAMELEN, sizeof(char));
    387     if (!whoishost)
    388       err(1, "malloc");
    389     myhost =(char *)calloc(MAXHOSTNAMELEN, sizeof(char));
    390     if (!myhost)
    391       err(1, "malloc");
    392     myerror = gethostname(myhost, MAXHOSTNAMELEN);
    393     if (myerror >= 0) {
    394       if (occurs(myhost, ".")) {
    395 	mytoplevel = rindex(myhost,'.');
    396 	mytoplevel++;
    397 	(void) snprintf(whoishost, MAXHOSTNAMELEN, "%s-whois.ripe.net",
    398 	    mytoplevel);
    399 	if (verb) fprintf(stderr, "Clever guess: %s\n", whoishost);
    400       }
    401     }
    402 
    403     memset(&hints, 0, sizeof(hints));
    404     hints.ai_flags = AI_CANONNAME;
    405     hints.ai_family = af;
    406     hints.ai_socktype = SOCK_STREAM;
    407     hints.ai_protocol = 0;
    408     error = getaddrinfo(host, optport, &hints, &dst);
    409     if ((error) && (verb))
    410       fprintf(stderr,"No such host: %s\n", whoishost);
    411     if (error) {
    412 #endif
    413 
    414       whoishost=NICHOST;
    415 
    416       if (verb)
    417 	fprintf(stderr, "Default host: %s\n\n", whoishost);
    418       memset(&hints, 0, sizeof(hints));
    419       hints.ai_flags = AI_CANONNAME;
    420       hints.ai_family = af;
    421       hints.ai_socktype = SOCK_STREAM;
    422       hints.ai_protocol = 0;
    423       error = getaddrinfo(host, optport , &hints, &dst);
    424       if (error) {
    425 	fprintf(stderr,"No such host: %s\n", whoishost);
    426 	if (verb) fprintf(stderr, "Now I give up ...\n");
    427 	perror("Unknown host");
    428 	exit(1);
    429       }
    430 
    431 #ifdef CLEVER
    432     }
    433 #endif
    434   }
    435   else {
    436     if (verb)
    437       fprintf(stderr, "Trying: %s\n\n", host);
    438     memset(&hints, 0, sizeof(hints));
    439     hints.ai_flags = AI_CANONNAME;
    440     hints.ai_family = af;
    441     hints.ai_socktype = SOCK_STREAM;
    442     hints.ai_protocol = 0;
    443     error = getaddrinfo(host, optport, &hints, &dst);
    444     if (error) {
    445       (void)fprintf(stderr, "whois: %s: ", host);
    446       perror("Unknown host");
    447       exit(1);
    448     }
    449   }
    450 
    451   for (/*nothing*/; dst; dst = dst->ai_next) {
    452     s = socket(dst->ai_family, dst->ai_socktype, dst->ai_protocol);
    453     if (s < 0)
    454       continue;
    455     if (connect(s, dst->ai_addr, dst->ai_addrlen) < 0) {
    456       close(s);
    457       if (verb) (void)fprintf(stderr, "whois: connect miss\n");
    458       continue;
    459     }
    460     /*okay*/
    461     break;
    462   }
    463   if (dst == NULL) {
    464     perror("whois: connect");
    465     exit(1);
    466   }
    467   if (verb) (void)fprintf(stderr, "whois: connect success\n");
    468 
    469 #ifndef NETWORKUPDATE
    470   sfi = fdopen(s, "r");
    471   sfo = fdopen(s, "w");
    472   if (sfi == NULL || sfo == NULL) {
    473     perror("whois: fdopen");
    474     (void)close(s);
    475     exit(1);
    476   }
    477 #endif
    478 
    479   signal(SIGTERM, termhandler);
    480 
    481 #ifdef RIPE
    482 #ifdef NETWORKUPDATE
    483 
    484   if ((child=fork())==0) {
    485 
    486      sfo = fdopen(s, "w");
    487      if (sfo == NULL) {
    488        perror("whois: fdopen");
    489        (void)close(s);
    490        exit(1);
    491      }
    492 
    493      if (gethostname(domainname, sizeof(domainname))) {
    494         fprintf(stderr, "error when doing gethostname()");
    495         exit(1);
    496      }
    497 
    498      passwdentry=getpwuid(getuid());
    499 
    500      fprintf(sfo, "-Vnc2.0 -U %s %s\n", passwdentry->pw_name, domainname);
    501      fflush(sfo);
    502 
    503      prev='\0';
    504 
    505      while ((ch=getchar()) != EOF) {
    506 
    507         fputc(ch, sfo);
    508 
    509         if (ch=='\n') fflush(sfo);
    510         if (feof(sfo)) closesocket(s, child);
    511         if ((ch=='.') && (prev=='\n')) closesocket(s, child);
    512         if (!isspace(ch) || ((!isspace(prev)) && (ch=='\n'))) prev=ch;
    513      }
    514 
    515      closesocket(s, child);
    516 
    517   }
    518 
    519   sfi = fdopen(s, "r");
    520   if (sfi == NULL) {
    521        perror("whois: fdopen");
    522        (void)close(s);
    523        exit(1);
    524   }
    525 
    526 #else
    527 
    528   if (alldatabases)
    529     (void)fprintf(sfo, "-a ");
    530   if (optchanged)
    531     (void)fprintf(sfo, "-c ");
    532   if (optfast)
    533     (void)fprintf(sfo, "-F ");
    534   if (optgetupdates)
    535     (void)fprintf(sfo, "-g %s ", getupdates);
    536   if (optinverselookup)
    537     (void)fprintf(sfo, "-i %s ", inverselookup);
    538   if (optL)
    539     (void)fprintf(sfo, "-L ");
    540   if (optm)
    541     (void)fprintf(sfo, "-m ");
    542   if (optM)
    543     (void)fprintf(sfo, "-M ");
    544   if (optrecur)
    545     (void)fprintf(sfo, "-r ");
    546   if (optsource)
    547     (void)fprintf(sfo, "-s %s ", source);
    548   if (optsugar)
    549     (void)fprintf(sfo, "-S ");
    550   if (optnonreferral)
    551     (void)fprintf(sfo, "-R ");
    552   if (opttempl)
    553     (void)fprintf(sfo, "-t %s ", templ);
    554   if (optverbose)
    555     (void)fprintf(sfo, "-v %s ", verbose);
    556   if (optobjtype)
    557     (void)fprintf(sfo, "-T %s ", objtype);
    558 
    559   /* we can only send the -V when we are sure that we are dealing with
    560      a RIPE whois server :-( */
    561 
    562   whoishost = strdup(host);
    563   if (!whoishost)
    564     err(1, "malloc");
    565   for (string=whoishost;(*string=(char)tolower(*string));string++);
    566 
    567   if (strstr(whoishost, "ripe.net") ||
    568       strstr(whoishost, "ra.net") ||
    569       strstr(whoishost, "apnic.net") ||
    570       strstr(whoishost, "mci.net") ||
    571       strstr(whoishost, "isi.edu") ||
    572       strstr(whoishost, "garr.it") ||
    573       strstr(whoishost, "ans.net") ||
    574       alldatabases || optfast || optgetupdates || optinverselookup ||
    575       optL || optm || optM || optrecur || optsugar || optsource ||
    576       opttempl || optverbose || optobjtype)
    577     (void)fprintf(sfo, "-VwC2.0 ");
    578 #endif
    579 #endif
    580 
    581 #ifndef NETWORKUPDATE
    582   while (argc-- > 1)
    583     (void)fprintf(sfo, "%s ", *argv++);
    584    if (*argv) (void)fputs(*argv, sfo);
    585    (void)fputs("\r\n", sfo);
    586   (void)fflush(sfo);
    587 #endif
    588 
    589   while ((ch = getc(sfi)) != EOF)
    590     putchar(ch);
    591 
    592   closesocket(s, 1);
    593 
    594   exit(0);
    595 }
    596