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