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