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