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