Home | History | Annotate | Line # | Download | only in identd
identd.c revision 1.4
      1  1.1      cgd /*
      2  1.4  mycroft **	$Id: identd.c,v 1.4 1995/06/03 22:46:26 mycroft Exp $
      3  1.2      cgd **
      4  1.1      cgd ** identd.c                       A TCP/IP link identification protocol server
      5  1.1      cgd **
      6  1.1      cgd ** This program is in the public domain and may be used freely by anyone
      7  1.1      cgd ** who wants to.
      8  1.1      cgd **
      9  1.1      cgd ** Last update: 22 April 1993
     10  1.1      cgd **
     11  1.1      cgd ** Please send bug fixes/bug reports to: Peter Eriksson <pen (at) lysator.liu.se>
     12  1.1      cgd */
     13  1.1      cgd 
     14  1.2      cgd #if defined(IRIX) || defined(SVR4) || defined(NeXT) || defined(__NetBSD__)
     15  1.1      cgd #  define SIGRETURN_TYPE void
     16  1.1      cgd #  define SIGRETURN_TYPE_IS_VOID
     17  1.1      cgd #else
     18  1.1      cgd #  define SIGRETURN_TYPE int
     19  1.1      cgd #endif
     20  1.1      cgd 
     21  1.1      cgd #ifdef SVR4
     22  1.1      cgd #  define STRNET
     23  1.1      cgd #endif
     24  1.1      cgd 
     25  1.1      cgd #include <stdio.h>
     26  1.1      cgd #include <ctype.h>
     27  1.1      cgd #include <errno.h>
     28  1.1      cgd #include <netdb.h>
     29  1.1      cgd #include <signal.h>
     30  1.1      cgd #include <fcntl.h>
     31  1.1      cgd 
     32  1.1      cgd #include <sys/types.h>
     33  1.1      cgd #include <sys/param.h>
     34  1.1      cgd #include <sys/ioctl.h>
     35  1.1      cgd #include <sys/socket.h>
     36  1.1      cgd #ifndef _AUX_SOURCE
     37  1.1      cgd #  include <sys/file.h>
     38  1.1      cgd #endif
     39  1.1      cgd #include <sys/time.h>
     40  1.1      cgd #include <sys/wait.h>
     41  1.1      cgd 
     42  1.1      cgd #include <pwd.h>
     43  1.1      cgd #include <grp.h>
     44  1.1      cgd 
     45  1.1      cgd #include <netinet/in.h>
     46  1.1      cgd 
     47  1.1      cgd #ifndef HPUX7
     48  1.1      cgd #  include <arpa/inet.h>
     49  1.1      cgd #endif
     50  1.1      cgd 
     51  1.1      cgd #if defined(MIPS) || defined(BSD43)
     52  1.1      cgd extern int errno;
     53  1.1      cgd #endif
     54  1.1      cgd 
     55  1.1      cgd #include "identd.h"
     56  1.1      cgd #include "error.h"
     57  1.1      cgd 
     58  1.1      cgd /* Antique unixes do not have these things defined... */
     59  1.1      cgd #ifndef FD_SETSIZE
     60  1.1      cgd #  define FD_SETSIZE 256
     61  1.1      cgd #endif
     62  1.1      cgd 
     63  1.1      cgd #ifndef FD_SET
     64  1.1      cgd #  ifndef NFDBITS
     65  1.1      cgd #    define NFDBITS   	(sizeof(int) * NBBY)  /* bits per mask */
     66  1.1      cgd #  endif
     67  1.1      cgd #  define FD_SET(n, p)  ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
     68  1.1      cgd #endif
     69  1.1      cgd 
     70  1.1      cgd #ifndef FD_ZERO
     71  1.1      cgd #  define FD_ZERO(p)        bzero((char *)(p), sizeof(*(p)))
     72  1.1      cgd #endif
     73  1.1      cgd 
     74  1.1      cgd extern char *version;
     75  1.1      cgd 
     76  1.1      cgd extern void *calloc();
     77  1.1      cgd extern void *malloc();
     78  1.1      cgd 
     79  1.1      cgd 
     80  1.1      cgd char *path_unix = NULL;
     81  1.1      cgd char *path_kmem = NULL;
     82  1.1      cgd 
     83  1.1      cgd int verbose_flag = 0;
     84  1.1      cgd int debug_flag   = 0;
     85  1.1      cgd int syslog_flag  = 0;
     86  1.1      cgd int multi_flag   = 0;
     87  1.1      cgd int other_flag   = 0;
     88  1.1      cgd int unknown_flag = 0;
     89  1.1      cgd int number_flag  = 0;
     90  1.1      cgd int noident_flag = 0;
     91  1.1      cgd 
     92  1.1      cgd int lport = 0;
     93  1.1      cgd int fport = 0;
     94  1.1      cgd 
     95  1.1      cgd char *charset_name = NULL;
     96  1.1      cgd char *indirect_host = NULL;
     97  1.1      cgd char *indirect_password = NULL;
     98  1.1      cgd 
     99  1.1      cgd static int child_pid;
    100  1.1      cgd 
    101  1.1      cgd #ifdef LOG_DAEMON
    102  1.1      cgd static int syslog_facility = LOG_DAEMON;
    103  1.1      cgd #endif
    104  1.1      cgd 
    105  1.1      cgd /*
    106  1.1      cgd ** The structure passing convention for GCC is incompatible with
    107  1.1      cgd ** Suns own C compiler, so we define our own inet_ntoa() function.
    108  1.1      cgd ** (This should only affect GCC version 1 I think, a well, this works
    109  1.1      cgd ** for version 2 also so why bother.. :-)
    110  1.1      cgd */
    111  1.1      cgd #if defined(__GNUC__) && defined(__sparc__)
    112  1.1      cgd 
    113  1.1      cgd #ifdef inet_ntoa
    114  1.1      cgd #undef inet_ntoa
    115  1.1      cgd #endif
    116  1.1      cgd 
    117  1.1      cgd char *inet_ntoa(ad)
    118  1.1      cgd   struct in_addr ad;
    119  1.1      cgd {
    120  1.1      cgd   unsigned long int s_ad;
    121  1.1      cgd   int a, b, c, d;
    122  1.1      cgd   static char addr[20];
    123  1.1      cgd 
    124  1.1      cgd   s_ad = ad.s_addr;
    125  1.1      cgd   d = s_ad % 256;
    126  1.1      cgd   s_ad /= 256;
    127  1.1      cgd   c = s_ad % 256;
    128  1.1      cgd   s_ad /= 256;
    129  1.1      cgd   b = s_ad % 256;
    130  1.1      cgd   a = s_ad / 256;
    131  1.1      cgd   sprintf(addr, "%d.%d.%d.%d", a, b, c, d);
    132  1.1      cgd 
    133  1.1      cgd   return addr;
    134  1.1      cgd }
    135  1.1      cgd #endif
    136  1.1      cgd 
    137  1.1      cgd 
    138  1.1      cgd /*
    139  1.1      cgd ** Return the name of the connecting host, or the IP number as a string.
    140  1.1      cgd */
    141  1.1      cgd char *gethost(addr)
    142  1.1      cgd   struct in_addr *addr;
    143  1.1      cgd {
    144  1.1      cgd   struct hostent *hp;
    145  1.1      cgd 
    146  1.1      cgd 
    147  1.1      cgd   hp = gethostbyaddr((char *) addr, sizeof(struct in_addr), AF_INET);
    148  1.1      cgd   if (hp)
    149  1.1      cgd     return hp->h_name;
    150  1.1      cgd   else
    151  1.1      cgd     return inet_ntoa(*addr);
    152  1.1      cgd }
    153  1.1      cgd 
    154  1.1      cgd /*
    155  1.1      cgd ** Exit cleanly after our time's up.
    156  1.1      cgd */
    157  1.1      cgd static SIGRETURN_TYPE
    158  1.1      cgd alarm_handler()
    159  1.1      cgd {
    160  1.1      cgd   if (syslog_flag)
    161  1.1      cgd     syslog(LOG_DEBUG, "SIGALRM triggered, exiting");
    162  1.1      cgd 
    163  1.1      cgd   exit(0);
    164  1.1      cgd }
    165  1.1      cgd 
    166  1.1      cgd #if !defined(hpux) && !defined(__hpux) && !defined(SVR4) || defined(_CRAY)
    167  1.1      cgd /*
    168  1.1      cgd ** This is used to clean up zombie child processes
    169  1.1      cgd ** if the -w or -b options are used.
    170  1.1      cgd */
    171  1.1      cgd static SIGRETURN_TYPE
    172  1.1      cgd child_handler()
    173  1.1      cgd {
    174  1.1      cgd #if defined(IRIX) || defined(NeXT)
    175  1.1      cgd   union wait status;
    176  1.1      cgd #else
    177  1.1      cgd   int status;
    178  1.1      cgd #endif
    179  1.1      cgd 
    180  1.1      cgd   while (wait3(&status, WNOHANG, NULL) > 0)
    181  1.1      cgd     ;
    182  1.1      cgd 
    183  1.1      cgd #ifndef SIGRETURN_TYPE_IS_VOID
    184  1.1      cgd   return 0;
    185  1.1      cgd #endif
    186  1.1      cgd }
    187  1.1      cgd #endif
    188  1.1      cgd 
    189  1.1      cgd 
    190  1.1      cgd char *clearmem(bp, len)
    191  1.1      cgd   char *bp;
    192  1.1      cgd   int len;
    193  1.1      cgd {
    194  1.1      cgd   char *cp;
    195  1.1      cgd 
    196  1.1      cgd   cp = bp;
    197  1.1      cgd   while (len-- > 0)
    198  1.1      cgd     *cp++ = 0;
    199  1.1      cgd 
    200  1.1      cgd   return bp;
    201  1.1      cgd }
    202  1.1      cgd 
    203  1.1      cgd 
    204  1.1      cgd /*
    205  1.1      cgd ** Main entry point into this daemon
    206  1.1      cgd */
    207  1.1      cgd int main(argc,argv)
    208  1.1      cgd   int argc;
    209  1.1      cgd   char *argv[];
    210  1.1      cgd {
    211  1.1      cgd   int i, len;
    212  1.1      cgd   struct sockaddr_in sin;
    213  1.1      cgd   struct in_addr laddr, faddr;
    214  1.1      cgd   struct timeval tv;
    215  1.1      cgd 
    216  1.1      cgd   int background_flag = 0;
    217  1.1      cgd   int timeout = 0;
    218  1.1      cgd   char *portno = "113";
    219  1.1      cgd   char *bind_address = NULL;
    220  1.1      cgd   int set_uid = 0;
    221  1.1      cgd   int set_gid = 0;
    222  1.1      cgd   int inhibit_default_config = 0;
    223  1.1      cgd   int opt_count = 0;		/* Count of option flags */
    224  1.1      cgd 
    225  1.1      cgd #ifdef __convex__
    226  1.1      cgd   argc--;    /* get rid of extra argument passed by inetd */
    227  1.1      cgd #endif
    228  1.1      cgd 
    229  1.1      cgd   /*
    230  1.1      cgd   ** Prescan the arguments for "-f<config-file>" switches
    231  1.1      cgd   */
    232  1.1      cgd   inhibit_default_config = 0;
    233  1.1      cgd   for (i = 1; i < argc && argv[i][0] == '-'; i++)
    234  1.1      cgd     if (argv[i][1] == 'f')
    235  1.1      cgd       inhibit_default_config = 1;
    236  1.1      cgd 
    237  1.1      cgd   /*
    238  1.1      cgd   ** Parse the default config file - if it exists
    239  1.1      cgd   */
    240  1.1      cgd   if (!inhibit_default_config)
    241  1.1      cgd     parse_config(NULL, 1);
    242  1.1      cgd 
    243  1.1      cgd   /*
    244  1.1      cgd   ** Parse the command line arguments
    245  1.1      cgd   */
    246  1.1      cgd   for (i = 1; i < argc && argv[i][0] == '-'; i++) {
    247  1.1      cgd     opt_count++;
    248  1.1      cgd     switch (argv[i][1])
    249  1.1      cgd     {
    250  1.1      cgd       case 'b':    /* Start as standalone daemon */
    251  1.1      cgd         background_flag = 1;
    252  1.1      cgd 	break;
    253  1.1      cgd 
    254  1.1      cgd       case 'w':    /* Start from Inetd, wait mode */
    255  1.1      cgd 	background_flag = 2;
    256  1.1      cgd 	break;
    257  1.1      cgd 
    258  1.1      cgd       case 'i':    /* Start from Inetd, nowait mode */
    259  1.1      cgd 	background_flag = 0;
    260  1.1      cgd 	break;
    261  1.1      cgd 
    262  1.1      cgd       case 't':
    263  1.1      cgd 	timeout = atoi(argv[i]+2);
    264  1.1      cgd 	break;
    265  1.1      cgd 
    266  1.1      cgd       case 'p':
    267  1.1      cgd 	portno = argv[i]+2;
    268  1.1      cgd 	break;
    269  1.1      cgd 
    270  1.1      cgd       case 'a':
    271  1.1      cgd 	bind_address = argv[i]+2;
    272  1.1      cgd 	break;
    273  1.1      cgd 
    274  1.1      cgd       case 'u':
    275  1.1      cgd 	if (isdigit(argv[i][2]))
    276  1.1      cgd 	  set_uid = atoi(argv[i]+2);
    277  1.1      cgd 	else
    278  1.1      cgd  	{
    279  1.1      cgd 	  struct passwd *pwd;
    280  1.1      cgd 
    281  1.1      cgd 	  pwd = getpwnam(argv[i]+2);
    282  1.1      cgd 	  if (!pwd)
    283  1.1      cgd 	    ERROR1("no such user (%s) for -u option", argv[i]+2);
    284  1.1      cgd 	  else
    285  1.1      cgd 	  {
    286  1.1      cgd 	    set_uid = pwd->pw_uid;
    287  1.1      cgd 	    set_gid = pwd->pw_gid;
    288  1.1      cgd 	  }
    289  1.1      cgd 	}
    290  1.1      cgd 	break;
    291  1.1      cgd 
    292  1.1      cgd       case 'g':
    293  1.1      cgd 	if (isdigit(argv[i][2]))
    294  1.1      cgd 	  set_gid = atoi(argv[i]+2);
    295  1.1      cgd 	else
    296  1.1      cgd  	{
    297  1.1      cgd 	  struct group *grp;
    298  1.1      cgd 
    299  1.1      cgd 	  grp = getgrnam(argv[i]+2);
    300  1.1      cgd 	  if (!grp)
    301  1.1      cgd 	    ERROR1("no such group (%s) for -g option", argv[i]+2);
    302  1.1      cgd 	  else
    303  1.1      cgd 	    set_gid = grp->gr_gid;
    304  1.1      cgd 	}
    305  1.1      cgd 	break;
    306  1.1      cgd 
    307  1.1      cgd       case 'c':
    308  1.1      cgd 	charset_name = argv[i]+2;
    309  1.1      cgd 	break;
    310  1.1      cgd 
    311  1.1      cgd       case 'r':
    312  1.1      cgd 	indirect_host = argv[i]+2;
    313  1.1      cgd 	break;
    314  1.1      cgd 
    315  1.1      cgd       case 'l':    /* Use the Syslog daemon for logging */
    316  1.1      cgd 	syslog_flag++;
    317  1.1      cgd 	break;
    318  1.1      cgd 
    319  1.1      cgd       case 'o':
    320  1.1      cgd 	other_flag = 1;
    321  1.1      cgd 	break;
    322  1.1      cgd 
    323  1.1      cgd       case 'e':
    324  1.1      cgd 	unknown_flag = 1;
    325  1.1      cgd 	break;
    326  1.1      cgd 
    327  1.1      cgd       case 'n':
    328  1.1      cgd 	number_flag = 1;
    329  1.1      cgd 	break;
    330  1.1      cgd 
    331  1.1      cgd       case 'V':    /* Give version of this daemon */
    332  1.1      cgd 	printf("[in.identd, version %s]\r\n", version);
    333  1.1      cgd 	exit(0);
    334  1.1      cgd 	break;
    335  1.1      cgd 
    336  1.1      cgd       case 'v':    /* Be verbose */
    337  1.1      cgd 	verbose_flag++;
    338  1.1      cgd 	break;
    339  1.1      cgd 
    340  1.1      cgd       case 'd':    /* Enable debugging */
    341  1.1      cgd 	debug_flag++;
    342  1.1      cgd 	break;
    343  1.1      cgd 
    344  1.1      cgd       case 'm':    /* Enable multiline queries */
    345  1.1      cgd 	multi_flag++;
    346  1.1      cgd 	break;
    347  1.1      cgd 
    348  1.1      cgd       case 'N':    /* Enable users ".noident" files */
    349  1.1      cgd 	noident_flag++;
    350  1.1      cgd 	break;
    351  1.1      cgd     }
    352  1.1      cgd   }
    353  1.1      cgd 
    354  1.1      cgd #if defined(_AUX_SOURCE) || defined (SUNOS35)
    355  1.1      cgd   /* A/UX 2.0* & SunOS 3.5 calls us with an argument XXXXXXXX.YYYY
    356  1.1      cgd   ** where XXXXXXXXX is the hexadecimal version of the callers
    357  1.1      cgd   ** IP number, and YYYY is the port/socket or something.
    358  1.1      cgd   ** It seems to be impossible to pass arguments to a daemon started
    359  1.1      cgd   ** by inetd.
    360  1.1      cgd   **
    361  1.1      cgd   ** Just in case it is started from something else, then we only
    362  1.1      cgd   ** skip the argument if no option flags have been seen.
    363  1.1      cgd   */
    364  1.1      cgd   if (opt_count == 0)
    365  1.1      cgd     argc--;
    366  1.1      cgd #endif
    367  1.1      cgd 
    368  1.1      cgd   /*
    369  1.1      cgd   ** Path to kernel namelist file specified on command line
    370  1.1      cgd   */
    371  1.1      cgd   if (i < argc)
    372  1.1      cgd     path_unix = argv[i++];
    373  1.1      cgd 
    374  1.1      cgd   /*
    375  1.1      cgd   ** Path to kernel memory device specified on command line
    376  1.1      cgd   */
    377  1.1      cgd   if (i < argc)
    378  1.1      cgd     path_kmem = argv[i++];
    379  1.1      cgd 
    380  1.1      cgd 
    381  1.1      cgd   /*
    382  1.1      cgd   ** Open the kernel memory device and read the nlist table
    383  1.1      cgd   */
    384  1.1      cgd   if (k_open() < 0)
    385  1.1      cgd       ERROR("main: k_open");
    386  1.1      cgd 
    387  1.1      cgd   /*
    388  1.1      cgd   ** Do the special handling needed for the "-b" flag
    389  1.1      cgd   */
    390  1.1      cgd   if (background_flag == 1)
    391  1.1      cgd   {
    392  1.1      cgd     struct sockaddr_in addr;
    393  1.1      cgd     struct servent *sp;
    394  1.1      cgd     int fd;
    395  1.1      cgd 
    396  1.1      cgd 
    397  1.1      cgd     if (fork())
    398  1.1      cgd       exit(0);
    399  1.1      cgd 
    400  1.1      cgd     close(0);
    401  1.1      cgd     close(1);
    402  1.1      cgd     close(2);
    403  1.1      cgd 
    404  1.1      cgd     if (fork())
    405  1.1      cgd       exit(0);
    406  1.1      cgd 
    407  1.1      cgd     fd = socket(AF_INET, SOCK_STREAM, 0);
    408  1.1      cgd     if (fd == -1)
    409  1.1      cgd       ERROR("main: socket");
    410  1.1      cgd 
    411  1.1      cgd     if (fd != 0)
    412  1.1      cgd       dup2(fd, 0);
    413  1.1      cgd 
    414  1.1      cgd     clearmem(&addr, sizeof(addr));
    415  1.1      cgd 
    416  1.4  mycroft     addr.sin_len = sizeof(struct sockaddr_in);
    417  1.1      cgd     addr.sin_family = AF_INET;
    418  1.1      cgd     if (bind_address == NULL)
    419  1.1      cgd       addr.sin_addr.s_addr = htonl(INADDR_ANY);
    420  1.1      cgd     else
    421  1.1      cgd     {
    422  1.3  mycroft       if (inet_aton(bind_address, &addr.sin_addr) == 0)
    423  1.1      cgd       {
    424  1.1      cgd 	struct hostent *hp;
    425  1.1      cgd 
    426  1.1      cgd 	hp = gethostbyname(bind_address);
    427  1.1      cgd 	if (!hp)
    428  1.1      cgd 	  ERROR1("no such address (%s) for -a switch", bind_address);
    429  1.1      cgd 
    430  1.3  mycroft 	memcpy(&addr.sin_addr, hp->h_addr, sizeof(addr.sin_addr));
    431  1.1      cgd       }
    432  1.1      cgd     }
    433  1.1      cgd 
    434  1.1      cgd     if (isdigit(portno[0]))
    435  1.1      cgd       addr.sin_port = htons(atoi(portno));
    436  1.1      cgd     else
    437  1.1      cgd     {
    438  1.1      cgd       sp = getservbyname(portno, "tcp");
    439  1.1      cgd       if (sp == NULL)
    440  1.1      cgd 	ERROR1("main: getservbyname: %s", portno);
    441  1.1      cgd       addr.sin_port = sp->s_port;
    442  1.1      cgd     }
    443  1.1      cgd 
    444  1.1      cgd     if (bind(0, (struct sockaddr *) &addr, sizeof(addr)) < 0)
    445  1.1      cgd       ERROR("main: bind");
    446  1.1      cgd 
    447  1.1      cgd     if (listen(0, 3) < 0)
    448  1.1      cgd       ERROR("main: listen");
    449  1.1      cgd   }
    450  1.1      cgd 
    451  1.1      cgd   if (set_gid)
    452  1.1      cgd     if (setgid(set_gid) == -1)
    453  1.1      cgd       ERROR("main: setgid");
    454  1.1      cgd 
    455  1.1      cgd   if (set_uid)
    456  1.1      cgd     if (setuid(set_uid) == -1)
    457  1.1      cgd       ERROR("main: setuid");
    458  1.1      cgd 
    459  1.1      cgd   /*
    460  1.1      cgd   ** Do some special handling if the "-b" or "-w" flags are used
    461  1.1      cgd   */
    462  1.1      cgd   if (background_flag)
    463  1.1      cgd   {
    464  1.1      cgd     int nfds, fd;
    465  1.1      cgd     fd_set read_set;
    466  1.1      cgd 
    467  1.1      cgd 
    468  1.1      cgd     /*
    469  1.1      cgd     ** Set up the SIGCHLD signal child termination handler so
    470  1.1      cgd     ** that we can avoid zombie processes hanging around and
    471  1.1      cgd     ** handle childs terminating before being able to complete the
    472  1.1      cgd     ** handshake.
    473  1.1      cgd     */
    474  1.1      cgd #if (defined(SVR4) || defined(hpux) || defined(__hpux) || \
    475  1.1      cgd      defined(_CRAY) || defined(_AUX_SOURCE))
    476  1.1      cgd     signal(SIGCHLD, SIG_IGN);
    477  1.1      cgd #else
    478  1.1      cgd     signal(SIGCHLD, (SIGRETURN_TYPE (*)()) child_handler);
    479  1.1      cgd #endif
    480  1.1      cgd 
    481  1.1      cgd     /*
    482  1.1      cgd     ** Loop and dispatch client handling processes
    483  1.1      cgd     */
    484  1.1      cgd     do
    485  1.1      cgd     {
    486  1.1      cgd       /*
    487  1.1      cgd       ** Terminate if we've been idle for 'timeout' seconds
    488  1.1      cgd       */
    489  1.1      cgd       if (background_flag == 2 && timeout)
    490  1.1      cgd       {
    491  1.1      cgd 	signal(SIGALRM, alarm_handler);
    492  1.1      cgd 	alarm(timeout);
    493  1.1      cgd       }
    494  1.1      cgd 
    495  1.1      cgd       /*
    496  1.1      cgd       ** Wait for a connection request to occur.
    497  1.1      cgd       ** Ignore EINTR (Interrupted System Call).
    498  1.1      cgd       */
    499  1.1      cgd       do
    500  1.1      cgd       {
    501  1.1      cgd 	FD_ZERO(&read_set);
    502  1.1      cgd 	FD_SET(0, &read_set);
    503  1.1      cgd 
    504  1.1      cgd 	if (timeout)
    505  1.1      cgd 	{
    506  1.1      cgd 	  tv.tv_sec = timeout;
    507  1.1      cgd 	  tv.tv_usec = 0;
    508  1.1      cgd 	  nfds = select(FD_SETSIZE, &read_set, NULL, NULL, &tv);
    509  1.1      cgd 	}
    510  1.1      cgd 	else
    511  1.1      cgd 
    512  1.1      cgd 	nfds = select(FD_SETSIZE, &read_set, NULL, NULL, NULL);
    513  1.1      cgd       } while (nfds < 0  && errno == EINTR);
    514  1.1      cgd 
    515  1.1      cgd       /*
    516  1.1      cgd       ** An error occured in select? Just die
    517  1.1      cgd       */
    518  1.1      cgd       if (nfds < 0)
    519  1.1      cgd 	ERROR("main: select");
    520  1.1      cgd 
    521  1.1      cgd       /*
    522  1.1      cgd       ** Timeout limit reached. Exit nicely
    523  1.1      cgd       */
    524  1.1      cgd       if (nfds == 0)
    525  1.1      cgd 	exit(0);
    526  1.1      cgd 
    527  1.1      cgd       /*
    528  1.1      cgd       ** Disable the alarm timeout
    529  1.1      cgd       */
    530  1.1      cgd       alarm(0);
    531  1.1      cgd 
    532  1.1      cgd       /*
    533  1.1      cgd       ** Accept the new client
    534  1.1      cgd       */
    535  1.1      cgd       fd = accept(0, NULL, NULL);
    536  1.1      cgd       if (fd == -1)
    537  1.1      cgd 	ERROR1("main: accept. errno = %d", errno);
    538  1.1      cgd 
    539  1.1      cgd       /*
    540  1.1      cgd       ** And fork, then close the fd if we are the parent.
    541  1.1      cgd       */
    542  1.1      cgd       child_pid = fork();
    543  1.1      cgd     } while (child_pid && (close(fd), 1));
    544  1.1      cgd 
    545  1.1      cgd     /*
    546  1.1      cgd     ** We are now in child, the parent has returned to "do" above.
    547  1.1      cgd     */
    548  1.1      cgd     if (dup2(fd, 0) == -1)
    549  1.1      cgd       ERROR("main: dup2: failed fd 0");
    550  1.1      cgd 
    551  1.1      cgd     if (dup2(fd, 1) == -1)
    552  1.1      cgd       ERROR("main: dup2: failed fd 1");
    553  1.1      cgd 
    554  1.1      cgd     if (dup2(fd, 2) == -1)
    555  1.1      cgd       ERROR("main: dup2: failed fd 2");
    556  1.1      cgd   }
    557  1.1      cgd 
    558  1.1      cgd   /*
    559  1.1      cgd   ** Get foreign internet address
    560  1.1      cgd   */
    561  1.1      cgd   len = sizeof(sin);
    562  1.1      cgd   if (getpeername(0, (struct sockaddr *) &sin, &len) == -1)
    563  1.1      cgd   {
    564  1.1      cgd     /*
    565  1.1      cgd     ** A user has tried to start us from the command line or
    566  1.1      cgd     ** the network link died, in which case this message won't
    567  1.1      cgd     ** reach to other end anyway, so lets give the poor user some
    568  1.1      cgd     ** errors.
    569  1.1      cgd     */
    570  1.1      cgd     perror("in.identd: getpeername()");
    571  1.1      cgd     exit(1);
    572  1.1      cgd   }
    573  1.1      cgd 
    574  1.1      cgd   faddr = sin.sin_addr;
    575  1.1      cgd 
    576  1.1      cgd 
    577  1.1      cgd   /*
    578  1.1      cgd   ** Open the connection to the Syslog daemon if requested
    579  1.1      cgd   */
    580  1.1      cgd   if (syslog_flag)
    581  1.1      cgd   {
    582  1.1      cgd #ifdef LOG_DAEMON
    583  1.1      cgd     openlog("identd", LOG_PID, syslog_facility);
    584  1.1      cgd #else
    585  1.1      cgd     openlog("identd", LOG_PID);
    586  1.1      cgd #endif
    587  1.1      cgd 
    588  1.1      cgd     syslog(LOG_INFO, "Connection from %s", gethost(&faddr));
    589  1.1      cgd   }
    590  1.1      cgd 
    591  1.1      cgd 
    592  1.1      cgd   /*
    593  1.1      cgd   ** Get local internet address
    594  1.1      cgd   */
    595  1.1      cgd   len = sizeof(sin);
    596  1.1      cgd #ifdef ATTSVR4
    597  1.1      cgd   if (t_getsockname(0, (struct sockaddr *) &sin, &len) == -1)
    598  1.1      cgd #else
    599  1.1      cgd   if (getsockname(0, (struct sockaddr *) &sin, &len) == -1)
    600  1.1      cgd #endif
    601  1.1      cgd   {
    602  1.1      cgd     /*
    603  1.1      cgd     ** We can just die here, because if this fails then the
    604  1.1      cgd     ** network has died and we haven't got anyone to return
    605  1.1      cgd     ** errors to.
    606  1.1      cgd     */
    607  1.1      cgd     exit(1);
    608  1.1      cgd   }
    609  1.1      cgd   laddr = sin.sin_addr;
    610  1.1      cgd 
    611  1.1      cgd 
    612  1.1      cgd   /*
    613  1.1      cgd   ** Get the local/foreign port pair from the luser
    614  1.1      cgd   */
    615  1.1      cgd   parse(stdin, &laddr, &faddr);
    616  1.1      cgd 
    617  1.1      cgd   exit(0);
    618  1.1      cgd }
    619