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