Home | History | Annotate | Line # | Download | only in identd
identd.c revision 1.15
      1  1.15   itojun /*	$NetBSD: identd.c,v 1.15 2002/09/23 03:32:35 itojun Exp $	*/
      2   1.8      mrg 
      3   1.1      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.9  msaitoh ** Last update: 7 Oct 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.9  msaitoh 
     14   1.9  msaitoh #if defined(IRIX) || defined(SVR4) || defined(NeXT) || (defined(sco) && sco >= 42) || defined(_AIX4) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(ultrix)
     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.9  msaitoh #ifdef NeXT31
     26   1.9  msaitoh #  include <libc.h>
     27   1.9  msaitoh #endif
     28   1.9  msaitoh 
     29   1.9  msaitoh #ifdef sco
     30   1.9  msaitoh #  define USE_SIGALARM
     31   1.9  msaitoh #endif
     32   1.9  msaitoh 
     33   1.9  msaitoh #include <stdio.h>
     34   1.9  msaitoh #include <ctype.h>
     35   1.9  msaitoh #include <errno.h>
     36   1.9  msaitoh #include <netdb.h>
     37   1.9  msaitoh #include <signal.h>
     38   1.9  msaitoh #include <fcntl.h>
     39  1.15   itojun #include <poll.h>
     40   1.9  msaitoh 
     41   1.1      cgd #include <sys/types.h>
     42   1.1      cgd #include <sys/param.h>
     43   1.1      cgd #include <sys/ioctl.h>
     44   1.1      cgd #include <sys/socket.h>
     45   1.1      cgd #ifndef _AUX_SOURCE
     46   1.1      cgd #  include <sys/file.h>
     47   1.1      cgd #endif
     48   1.1      cgd #include <sys/time.h>
     49   1.1      cgd #include <sys/wait.h>
     50   1.1      cgd 
     51   1.1      cgd #include <pwd.h>
     52   1.1      cgd #include <grp.h>
     53   1.1      cgd 
     54   1.1      cgd #include <netinet/in.h>
     55   1.1      cgd 
     56   1.1      cgd #ifndef HPUX7
     57   1.1      cgd #  include <arpa/inet.h>
     58   1.1      cgd #endif
     59   1.1      cgd 
     60   1.1      cgd #if defined(MIPS) || defined(BSD43)
     61   1.1      cgd extern int errno;
     62   1.1      cgd #endif
     63   1.1      cgd 
     64   1.9  msaitoh #if defined(SOLARIS) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(_AIX)
     65   1.9  msaitoh #  include <unistd.h>
     66   1.9  msaitoh #  include <stdlib.h>
     67   1.9  msaitoh #  include <string.h>
     68   1.9  msaitoh #endif
     69   1.9  msaitoh 
     70   1.1      cgd #include "identd.h"
     71   1.1      cgd #include "error.h"
     72   1.9  msaitoh #include "paths.h"
     73   1.9  msaitoh 
     74   1.1      cgd 
     75   1.1      cgd /* Antique unixes do not have these things defined... */
     76   1.1      cgd #ifndef FD_SETSIZE
     77   1.1      cgd #  define FD_SETSIZE 256
     78   1.1      cgd #endif
     79   1.1      cgd 
     80   1.1      cgd #ifndef FD_SET
     81   1.1      cgd #  ifndef NFDBITS
     82   1.1      cgd #    define NFDBITS   	(sizeof(int) * NBBY)  /* bits per mask */
     83   1.1      cgd #  endif
     84   1.1      cgd #  define FD_SET(n, p)  ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
     85   1.1      cgd #endif
     86   1.1      cgd 
     87   1.1      cgd #ifndef FD_ZERO
     88   1.1      cgd #  define FD_ZERO(p)        bzero((char *)(p), sizeof(*(p)))
     89   1.1      cgd #endif
     90   1.1      cgd 
     91   1.1      cgd 
     92   1.9  msaitoh char *path_unix = (char *) NULL;
     93   1.9  msaitoh char *path_kmem = (char *) NULL;
     94   1.1      cgd 
     95   1.1      cgd int verbose_flag = 0;
     96   1.1      cgd int debug_flag   = 0;
     97   1.1      cgd int syslog_flag  = 0;
     98   1.1      cgd int multi_flag   = 0;
     99   1.1      cgd int other_flag   = 0;
    100   1.1      cgd int unknown_flag = 0;
    101   1.1      cgd int noident_flag = 0;
    102   1.9  msaitoh int crypto_flag  = 0;
    103  1.10    jwise int liar_flag    = 0;
    104   1.1      cgd 
    105   1.1      cgd int lport = 0;
    106   1.1      cgd int fport = 0;
    107   1.1      cgd 
    108   1.9  msaitoh char *charset_name = (char *) NULL;
    109   1.9  msaitoh char *indirect_host = (char *) NULL;
    110   1.9  msaitoh char *indirect_password = (char *) NULL;
    111  1.10    jwise char *lie_string = (char *) NULL;
    112   1.9  msaitoh 
    113   1.9  msaitoh #ifdef ALLOW_FORMAT
    114   1.9  msaitoh     int format_flag = 0;
    115   1.9  msaitoh     char *format = "%u";
    116   1.9  msaitoh #endif
    117   1.1      cgd 
    118   1.1      cgd static int child_pid;
    119   1.1      cgd 
    120   1.1      cgd #ifdef LOG_DAEMON
    121   1.1      cgd static int syslog_facility = LOG_DAEMON;
    122   1.1      cgd #endif
    123   1.1      cgd 
    124   1.9  msaitoh static int comparemem __P((void *, void *, int));
    125   1.9  msaitoh char *clearmem __P((void *, int));
    126   1.9  msaitoh static SIGRETURN_TYPE child_handler __P((int));
    127   1.8      mrg int main __P((int, char *[]));
    128   1.8      mrg 
    129   1.1      cgd /*
    130   1.1      cgd ** The structure passing convention for GCC is incompatible with
    131   1.1      cgd ** Suns own C compiler, so we define our own inet_ntoa() function.
    132   1.1      cgd ** (This should only affect GCC version 1 I think, a well, this works
    133   1.1      cgd ** for version 2 also so why bother.. :-)
    134   1.1      cgd */
    135   1.9  msaitoh #if defined(__GNUC__) && defined(__sparc__) && !defined(NeXT)
    136   1.1      cgd 
    137   1.1      cgd #ifdef inet_ntoa
    138   1.1      cgd #undef inet_ntoa
    139   1.1      cgd #endif
    140   1.1      cgd 
    141   1.1      cgd char *inet_ntoa(ad)
    142   1.9  msaitoh     struct in_addr ad;
    143   1.1      cgd {
    144   1.9  msaitoh     unsigned long int s_ad;
    145   1.9  msaitoh     int a, b, c, d;
    146   1.9  msaitoh     static char addr[20];
    147   1.9  msaitoh 
    148   1.9  msaitoh     s_ad = ad.s_addr;
    149   1.9  msaitoh     d = s_ad % 256;
    150   1.9  msaitoh     s_ad /= 256;
    151   1.9  msaitoh     c = s_ad % 256;
    152   1.9  msaitoh     s_ad /= 256;
    153   1.9  msaitoh     b = s_ad % 256;
    154   1.9  msaitoh     a = s_ad / 256;
    155   1.9  msaitoh     sprintf(addr, "%d.%d.%d.%d", a, b, c, d);
    156   1.9  msaitoh 
    157   1.9  msaitoh     return addr;
    158   1.1      cgd }
    159   1.1      cgd #endif
    160   1.1      cgd 
    161   1.9  msaitoh static int comparemem(vp1, vp2, len)
    162   1.9  msaitoh      void *vp1;
    163   1.9  msaitoh      void *vp2;
    164   1.9  msaitoh      int len;
    165   1.9  msaitoh {
    166   1.9  msaitoh     unsigned char *p1 = (unsigned char *) vp1;
    167   1.9  msaitoh     unsigned char *p2 = (unsigned char *) vp2;
    168   1.9  msaitoh     int c;
    169   1.9  msaitoh 
    170   1.9  msaitoh     while (len-- > 0)
    171   1.9  msaitoh 	if ((c = (int) *p1++ - (int) *p2++) != 0)
    172   1.9  msaitoh 	    return c;
    173   1.9  msaitoh 
    174   1.9  msaitoh     return 0;
    175   1.9  msaitoh }
    176   1.1      cgd 
    177   1.1      cgd /*
    178   1.1      cgd ** Return the name of the connecting host, or the IP number as a string.
    179   1.1      cgd */
    180   1.1      cgd char *gethost(addr)
    181   1.9  msaitoh     struct in_addr *addr;
    182   1.1      cgd {
    183   1.9  msaitoh     int i;
    184   1.9  msaitoh     struct hostent *hp;
    185   1.1      cgd 
    186   1.1      cgd 
    187   1.9  msaitoh     hp = gethostbyaddr((char *) addr, sizeof(struct in_addr), AF_INET);
    188   1.9  msaitoh     if (hp)
    189   1.9  msaitoh     {
    190   1.9  msaitoh 	char *hname = strdup(hp->h_name);
    191   1.9  msaitoh 
    192   1.9  msaitoh 	if (! hname) {
    193   1.9  msaitoh 	    syslog(LOG_ERR, "strdup(%s): %m", hp->h_name);
    194   1.9  msaitoh 	    exit(1);
    195   1.9  msaitoh 	}
    196   1.9  msaitoh 	/* Found a IP -> Name match, now try the reverse for security reasons */
    197   1.9  msaitoh 	hp = gethostbyname(hname);
    198   1.9  msaitoh 	(void) free(hname);
    199   1.9  msaitoh 	if (hp)
    200   1.9  msaitoh #ifdef h_addr
    201   1.9  msaitoh 	    for (i = 0; hp->h_addr_list[i]; i++)
    202   1.9  msaitoh 		if (comparemem(hp->h_addr_list[i],
    203   1.9  msaitoh 			       (unsigned char *) addr,
    204   1.9  msaitoh 			       (int) sizeof(struct in_addr)) == 0)
    205   1.9  msaitoh 		    return (char *) hp->h_name;
    206   1.9  msaitoh #else
    207   1.9  msaitoh 	if (comparemem(hp->h_addr, addr, sizeof(struct in_addr)) == 0)
    208   1.9  msaitoh 	    return hp->h_name;
    209   1.9  msaitoh #endif
    210   1.9  msaitoh   }
    211   1.9  msaitoh 
    212   1.9  msaitoh   return inet_ntoa(*addr);
    213   1.1      cgd }
    214   1.1      cgd 
    215   1.9  msaitoh #ifdef USE_SIGALARM
    216   1.1      cgd /*
    217   1.1      cgd ** Exit cleanly after our time's up.
    218   1.1      cgd */
    219   1.1      cgd static SIGRETURN_TYPE
    220   1.9  msaitoh alarm_handler(int s)
    221   1.1      cgd {
    222   1.9  msaitoh     if (syslog_flag)
    223   1.9  msaitoh 	syslog(LOG_DEBUG, "SIGALRM triggered, exiting");
    224   1.9  msaitoh 
    225   1.9  msaitoh     exit(0);
    226   1.1      cgd }
    227   1.9  msaitoh #endif
    228   1.9  msaitoh 
    229   1.1      cgd 
    230   1.9  msaitoh #if !defined(hpux) && !defined(__hpux) && !defined(SVR4) && \
    231   1.9  msaitoh     !defined(_CRAY) && !defined(sco) && !defined(LINUX)
    232   1.1      cgd /*
    233   1.1      cgd ** This is used to clean up zombie child processes
    234   1.1      cgd ** if the -w or -b options are used.
    235   1.1      cgd */
    236   1.1      cgd static SIGRETURN_TYPE
    237   1.9  msaitoh child_handler(dummy)
    238   1.9  msaitoh 	int dummy;
    239   1.1      cgd {
    240   1.9  msaitoh #if defined(NeXT) || (defined(__sgi) && defined(__SVR3))
    241   1.9  msaitoh     union wait status;
    242   1.1      cgd #else
    243   1.9  msaitoh     int status;
    244   1.1      cgd #endif
    245   1.9  msaitoh     int saved_errno = errno;
    246   1.9  msaitoh 
    247   1.9  msaitoh     while (wait3(&status, WNOHANG, NULL) > 0)
    248   1.9  msaitoh 	;
    249   1.1      cgd 
    250   1.9  msaitoh     errno = saved_errno;
    251   1.9  msaitoh 
    252   1.1      cgd #ifndef SIGRETURN_TYPE_IS_VOID
    253   1.9  msaitoh     return 0;
    254   1.1      cgd #endif
    255   1.1      cgd }
    256   1.1      cgd #endif
    257   1.1      cgd 
    258   1.9  msaitoh 
    259   1.9  msaitoh char *clearmem(vbp, len)
    260   1.9  msaitoh      void *vbp;
    261   1.9  msaitoh      int len;
    262   1.1      cgd {
    263   1.9  msaitoh     char *bp = (char *) vbp;
    264   1.9  msaitoh     char *cp;
    265   1.1      cgd 
    266   1.9  msaitoh     cp = bp;
    267   1.9  msaitoh     while (len-- > 0)
    268   1.9  msaitoh 	*cp++ = 0;
    269   1.9  msaitoh 
    270   1.9  msaitoh     return bp;
    271   1.1      cgd }
    272   1.1      cgd 
    273   1.1      cgd 
    274   1.1      cgd /*
    275   1.1      cgd ** Main entry point into this daemon
    276   1.1      cgd */
    277   1.1      cgd int main(argc,argv)
    278   1.9  msaitoh     int argc;
    279   1.9  msaitoh     char *argv[];
    280   1.1      cgd {
    281   1.9  msaitoh     int i, len;
    282   1.9  msaitoh     struct sockaddr_in sin;
    283   1.9  msaitoh     struct in_addr laddr, faddr;
    284   1.9  msaitoh     int one = 1;
    285   1.9  msaitoh 
    286   1.9  msaitoh     int background_flag = 0;
    287   1.9  msaitoh     int timeout = 0;
    288   1.9  msaitoh     char *portno = "113";
    289   1.9  msaitoh     char *bind_address = (char *) NULL;
    290   1.9  msaitoh     int set_uid = 0;
    291   1.9  msaitoh     int set_gid = 0;
    292   1.9  msaitoh     int inhibit_default_config = 0;
    293   1.9  msaitoh     int opt_count = 0;		/* Count of option flags */
    294   1.1      cgd 
    295   1.1      cgd #ifdef __convex__
    296   1.9  msaitoh     argc--;    /* get rid of extra argument passed by inetd */
    297   1.1      cgd #endif
    298   1.1      cgd 
    299   1.9  msaitoh 
    300   1.9  msaitoh     if (isatty(0))
    301   1.9  msaitoh 	background_flag = 1;
    302   1.9  msaitoh 
    303   1.9  msaitoh     /*
    304   1.9  msaitoh     ** Prescan the arguments for "-f<config-file>" switches
    305   1.9  msaitoh     */
    306   1.9  msaitoh     inhibit_default_config = 0;
    307   1.9  msaitoh     for (i = 1; i < argc && argv[i][0] == '-'; i++)
    308   1.9  msaitoh 	if (argv[i][1] == 'f')
    309   1.9  msaitoh 	    inhibit_default_config = 1;
    310   1.9  msaitoh 
    311   1.9  msaitoh     /*
    312   1.9  msaitoh     ** Parse the default config file - if it exists
    313   1.9  msaitoh     */
    314   1.9  msaitoh     if (!inhibit_default_config)
    315   1.9  msaitoh 	parse_config(NULL, 1);
    316   1.1      cgd 
    317   1.9  msaitoh     /*
    318   1.9  msaitoh     ** Parse the command line arguments
    319   1.9  msaitoh     */
    320   1.9  msaitoh     for (i = 1; i < argc && argv[i][0] == '-'; i++) {
    321   1.9  msaitoh 	opt_count++;
    322   1.9  msaitoh 	switch (argv[i][1])
    323   1.9  msaitoh 	{
    324   1.9  msaitoh 	  case 'b':    /* Start as standalone daemon */
    325   1.9  msaitoh 	    background_flag = 1;
    326   1.9  msaitoh 	    break;
    327   1.9  msaitoh 
    328   1.9  msaitoh 	  case 'w':    /* Start from Inetd, wait mode */
    329   1.9  msaitoh 	    background_flag = 2;
    330   1.9  msaitoh 	    break;
    331   1.9  msaitoh 
    332   1.9  msaitoh 	  case 'i':    /* Start from Inetd, nowait mode */
    333   1.9  msaitoh 	    background_flag = 0;
    334   1.9  msaitoh 	    break;
    335   1.9  msaitoh 
    336   1.9  msaitoh 	  case 't':
    337   1.9  msaitoh 	    timeout = atoi(argv[i]+2);
    338   1.9  msaitoh 	    break;
    339   1.9  msaitoh 
    340   1.9  msaitoh 	  case 'p':
    341   1.9  msaitoh 	    portno = argv[i]+2;
    342   1.9  msaitoh 	    break;
    343   1.9  msaitoh 
    344   1.9  msaitoh 	  case 'a':
    345   1.9  msaitoh 	    bind_address = argv[i]+2;
    346   1.9  msaitoh 	    break;
    347   1.9  msaitoh 
    348   1.9  msaitoh 	  case 'u':
    349   1.9  msaitoh 	    if (isdigit(argv[i][2]))
    350   1.9  msaitoh 		set_uid = atoi(argv[i]+2);
    351   1.9  msaitoh 	    else
    352   1.9  msaitoh 	    {
    353   1.9  msaitoh 		struct passwd *pwd;
    354   1.9  msaitoh 
    355   1.9  msaitoh 		pwd = getpwnam(argv[i]+2);
    356   1.9  msaitoh 		if (!pwd)
    357   1.9  msaitoh 		    ERROR1("no such user (%s) for -u option", argv[i]+2);
    358   1.9  msaitoh 		else
    359   1.9  msaitoh 		{
    360   1.9  msaitoh 		    set_uid = pwd->pw_uid;
    361   1.9  msaitoh 		    set_gid = pwd->pw_gid;
    362   1.9  msaitoh 		}
    363   1.9  msaitoh 	    }
    364   1.9  msaitoh 	    break;
    365   1.9  msaitoh 
    366   1.9  msaitoh 	  case 'g':
    367   1.9  msaitoh 	    if (isdigit(argv[i][2]))
    368   1.9  msaitoh 		set_gid = atoi(argv[i]+2);
    369   1.9  msaitoh 	    else
    370   1.9  msaitoh 	    {
    371   1.9  msaitoh 		struct group *grp;
    372   1.9  msaitoh 
    373   1.9  msaitoh 		grp = getgrnam(argv[i]+2);
    374   1.9  msaitoh 		if (!grp)
    375   1.9  msaitoh 		    ERROR1("no such group (%s) for -g option", argv[i]+2);
    376   1.9  msaitoh 		else
    377   1.9  msaitoh 		    set_gid = grp->gr_gid;
    378   1.9  msaitoh 	    }
    379   1.9  msaitoh 	    break;
    380   1.9  msaitoh 
    381   1.9  msaitoh 	  case 'c':
    382   1.9  msaitoh 	    charset_name = argv[i]+2;
    383   1.9  msaitoh 	    break;
    384   1.9  msaitoh 
    385   1.9  msaitoh 	  case 'r':
    386   1.9  msaitoh 	    indirect_host = argv[i]+2;
    387   1.9  msaitoh 	    break;
    388   1.9  msaitoh 
    389   1.9  msaitoh 	  case 'l':    /* Use the Syslog daemon for logging */
    390   1.9  msaitoh 	    syslog_flag++;
    391   1.9  msaitoh #ifdef LOG_DAEMON
    392   1.9  msaitoh 	    openlog("identd", LOG_PID, syslog_facility);
    393   1.9  msaitoh #else
    394   1.9  msaitoh 	    openlog("identd", LOG_PID);
    395   1.9  msaitoh #endif
    396   1.9  msaitoh 	    break;
    397   1.9  msaitoh 
    398   1.9  msaitoh 	  case 'o':
    399   1.9  msaitoh 	    other_flag = 1;
    400   1.9  msaitoh 	    break;
    401   1.9  msaitoh 
    402   1.9  msaitoh 	  case 'e':
    403   1.9  msaitoh 	    unknown_flag = 1;
    404   1.9  msaitoh 	    break;
    405   1.9  msaitoh 
    406   1.9  msaitoh 	  case 'V':    /* Give version of this daemon */
    407   1.9  msaitoh 	    printf("[in.identd, version %s]\r\n", version);
    408   1.9  msaitoh 	    exit(0);
    409   1.9  msaitoh 	    break;
    410   1.9  msaitoh 
    411   1.9  msaitoh 	  case 'v':    /* Be verbose */
    412   1.9  msaitoh 	    verbose_flag++;
    413   1.9  msaitoh 	    break;
    414   1.9  msaitoh 
    415   1.9  msaitoh 	  case 'd':    /* Enable debugging */
    416   1.9  msaitoh 	    debug_flag++;
    417   1.9  msaitoh 	    break;
    418   1.9  msaitoh 
    419   1.9  msaitoh 	  case 'm':    /* Enable multiline queries */
    420   1.9  msaitoh 	    multi_flag++;
    421   1.9  msaitoh 	    break;
    422   1.9  msaitoh 
    423   1.9  msaitoh 	  case 'N':    /* Enable users ".noident" files */
    424   1.9  msaitoh 	    noident_flag++;
    425   1.9  msaitoh 	    break;
    426   1.9  msaitoh 
    427   1.9  msaitoh #ifdef INCLUDE_CRYPT
    428   1.9  msaitoh           case 'C':    /* Enable encryption. */
    429   1.9  msaitoh 	    {
    430   1.9  msaitoh 		FILE *keyfile;
    431   1.9  msaitoh 
    432   1.9  msaitoh 		if (argv[i][2])
    433   1.9  msaitoh 		    keyfile = fopen(argv[i]+2, "r");
    434   1.9  msaitoh 		else
    435   1.9  msaitoh 		    keyfile = fopen(PATH_DESKEY, "r");
    436   1.9  msaitoh 
    437   1.9  msaitoh 		if (keyfile == NULL)
    438   1.9  msaitoh 		{
    439   1.9  msaitoh 		    ERROR("cannot open key file for option -C");
    440   1.9  msaitoh 		}
    441   1.9  msaitoh 		else
    442   1.9  msaitoh 		{
    443   1.9  msaitoh 		    char buf[1024];
    444   1.9  msaitoh 
    445   1.9  msaitoh 		    if (fgets(buf, 1024, keyfile) == NULL)
    446   1.9  msaitoh 		    {
    447   1.9  msaitoh 			ERROR("cannot read key file for option -C");
    448   1.9  msaitoh 		    }
    449   1.9  msaitoh 		    else
    450   1.9  msaitoh 		    {
    451   1.9  msaitoh 			init_encryption(buf);
    452   1.9  msaitoh 			crypto_flag++;
    453   1.9  msaitoh 		    }
    454   1.9  msaitoh 		    fclose(keyfile);
    455   1.9  msaitoh 		}
    456   1.9  msaitoh 	    }
    457   1.9  msaitoh             break;
    458   1.9  msaitoh #endif
    459   1.9  msaitoh 
    460   1.9  msaitoh #ifdef ALLOW_FORMAT
    461   1.9  msaitoh 	  case 'n': /* Compatibility flag - just send the user number */
    462   1.9  msaitoh 	    format_flag = 1;
    463   1.9  msaitoh 	    format = "%U";
    464   1.9  msaitoh 	    break;
    465   1.9  msaitoh 
    466   1.9  msaitoh           case 'F':    /* Output format */
    467   1.9  msaitoh 	    format_flag = 1;
    468   1.9  msaitoh 	    format = argv[i]+2;
    469   1.9  msaitoh 	    break;
    470   1.9  msaitoh #endif
    471  1.10    jwise 
    472  1.10    jwise 	  case 'L':	/* lie brazenly */
    473  1.10    jwise 	    liar_flag = 1;
    474  1.10    jwise 	    if (*(argv[i]+2) != '\0')
    475  1.10    jwise 		lie_string = argv[i]+2;
    476  1.10    jwise 	    else
    477  1.10    jwise #ifdef DEFAULT_LIE_USER
    478  1.10    jwise 		lie_string = DEFAULT_LIE_USER;
    479  1.10    jwise #else
    480  1.10    jwise 		ERROR("-L specified with no user name");
    481  1.10    jwise #endif
    482  1.10    jwise 	    break;
    483   1.9  msaitoh 
    484   1.9  msaitoh 	  default:
    485   1.9  msaitoh 	    ERROR1("Bad option %s", argv[i]);
    486   1.9  msaitoh 	    break;
    487   1.9  msaitoh 	}
    488   1.9  msaitoh     }
    489   1.9  msaitoh 
    490   1.9  msaitoh #if defined(_AUX_SOURCE) || defined (SUNOS35)
    491   1.9  msaitoh     /* A/UX 2.0* & SunOS 3.5 calls us with an argument XXXXXXXX.YYYY
    492   1.9  msaitoh     ** where XXXXXXXXX is the hexadecimal version of the callers
    493   1.9  msaitoh     ** IP number, and YYYY is the port/socket or something.
    494   1.9  msaitoh     ** It seems to be impossible to pass arguments to a daemon started
    495   1.9  msaitoh     ** by inetd.
    496   1.9  msaitoh     **
    497   1.9  msaitoh     ** Just in case it is started from something else, then we only
    498   1.9  msaitoh     ** skip the argument if no option flags have been seen.
    499   1.9  msaitoh     */
    500   1.9  msaitoh     if (opt_count == 0)
    501   1.9  msaitoh 	argc--;
    502   1.9  msaitoh #endif
    503   1.9  msaitoh 
    504   1.9  msaitoh     /*
    505   1.9  msaitoh     ** Path to kernel namelist file specified on command line
    506   1.9  msaitoh     */
    507   1.9  msaitoh     if (i < argc)
    508   1.9  msaitoh 	path_unix = argv[i++];
    509   1.9  msaitoh 
    510   1.9  msaitoh     /*
    511   1.9  msaitoh     ** Path to kernel memory device specified on command line
    512   1.9  msaitoh     */
    513   1.9  msaitoh     if (i < argc)
    514   1.9  msaitoh 	path_kmem = argv[i++];
    515   1.9  msaitoh 
    516   1.9  msaitoh 
    517   1.9  msaitoh     if (i < argc)
    518   1.9  msaitoh 	ERROR1("Too many arguments: ignored from %s", argv[i]);
    519   1.9  msaitoh 
    520   1.9  msaitoh 
    521   1.9  msaitoh     /*
    522   1.9  msaitoh     ** We used to call k_open here. But then the file descriptor
    523   1.9  msaitoh     ** kd->fd open on /dev/kmem is shared by all child processes.
    524   1.9  msaitoh     ** From the fork(2) man page:
    525   1.9  msaitoh     **      o  The child process has its own copy of the parent's descriptors.  These
    526   1.9  msaitoh     **         descriptors reference the same underlying objects.  For instance, file
    527   1.9  msaitoh     **         pointers in file objects are shared between the child and the parent
    528   1.9  msaitoh     **         so that an lseek(2) on a descriptor in the child process can affect a
    529   1.9  msaitoh     **         subsequent read(2) or write(2) by the parent.
    530   1.9  msaitoh     ** Thus with concurrent (simultaneous) identd client processes,
    531   1.9  msaitoh     ** they step on each other's toes when they use kvm_read.
    532   1.9  msaitoh     **
    533   1.9  msaitoh     ** Calling k_open here was a mistake for another reason too: we
    534   1.9  msaitoh     ** did not yet honor -u and -g options. Presumably we are
    535   1.9  msaitoh     ** running as root (unless the in.identd file is setuid), and
    536   1.9  msaitoh     ** then we can open kmem regardless of -u and -g values.
    537   1.9  msaitoh     **
    538   1.9  msaitoh     **
    539   1.9  msaitoh     ** Open the kernel memory device and read the nlist table
    540   1.9  msaitoh     **
    541   1.9  msaitoh     **     if (k_open() < 0)
    542   1.9  msaitoh     ** 		ERROR("main: k_open");
    543   1.9  msaitoh     */
    544   1.9  msaitoh 
    545   1.9  msaitoh     /*
    546   1.9  msaitoh     ** Do the special handling needed for the "-b" flag
    547   1.9  msaitoh     */
    548   1.9  msaitoh     if (background_flag == 1)
    549   1.1      cgd     {
    550   1.9  msaitoh 	struct sockaddr_in addr;
    551   1.9  msaitoh 	struct servent *sp;
    552   1.9  msaitoh 	int fd;
    553   1.9  msaitoh 
    554   1.9  msaitoh 
    555   1.9  msaitoh 	if (!debug_flag)
    556   1.9  msaitoh 	{
    557   1.9  msaitoh 	    if (fork())
    558   1.9  msaitoh 		exit(0);
    559   1.9  msaitoh 
    560   1.9  msaitoh 	    close(0);
    561   1.9  msaitoh 	    close(1);
    562   1.9  msaitoh 	    close(2);
    563   1.9  msaitoh 
    564   1.9  msaitoh 	    if (fork())
    565   1.9  msaitoh 		exit(0);
    566   1.9  msaitoh 	}
    567   1.9  msaitoh 
    568   1.9  msaitoh 	fd = socket(AF_INET, SOCK_STREAM, 0);
    569   1.9  msaitoh 	if (fd == -1)
    570   1.9  msaitoh 	    ERROR("main: socket");
    571   1.9  msaitoh 
    572   1.9  msaitoh 	if (fd != 0)
    573   1.9  msaitoh 	    dup2(fd, 0);
    574   1.9  msaitoh 
    575   1.9  msaitoh 	clearmem((void *) &addr, (int) sizeof(addr));
    576   1.9  msaitoh 
    577   1.9  msaitoh 	addr.sin_family = AF_INET;
    578   1.9  msaitoh 	if (bind_address == (char *) NULL)
    579   1.9  msaitoh 	    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    580   1.1      cgd 	else
    581   1.9  msaitoh 	{
    582   1.9  msaitoh 	    if (isdigit(bind_address[0]))
    583   1.9  msaitoh 		addr.sin_addr.s_addr = inet_addr(bind_address);
    584   1.9  msaitoh 	    else
    585   1.9  msaitoh 	    {
    586   1.9  msaitoh 		struct hostent *hp;
    587   1.9  msaitoh 
    588   1.9  msaitoh 		hp = gethostbyname(bind_address);
    589   1.9  msaitoh 		if (!hp)
    590   1.9  msaitoh 		    ERROR1("no such address (%s) for -a switch", bind_address);
    591   1.9  msaitoh 
    592   1.9  msaitoh 		/* This is ugly, should use memcpy() or bcopy() but... */
    593   1.9  msaitoh 		addr.sin_addr.s_addr = * (unsigned long *) (hp->h_addr);
    594   1.9  msaitoh 	    }
    595   1.1      cgd 	}
    596   1.1      cgd 
    597   1.9  msaitoh 	if (isdigit(portno[0]))
    598   1.9  msaitoh 	    addr.sin_port = htons(atoi(portno));
    599   1.1      cgd 	else
    600   1.9  msaitoh 	{
    601   1.9  msaitoh 	    sp = getservbyname(portno, "tcp");
    602   1.9  msaitoh 	    if (sp == (struct servent *) NULL)
    603   1.9  msaitoh 		ERROR1("main: getservbyname: %s", portno);
    604   1.9  msaitoh 	    addr.sin_port = sp->s_port;
    605   1.9  msaitoh 	}
    606   1.1      cgd 
    607   1.9  msaitoh #ifdef SO_REUSEADDR
    608   1.9  msaitoh 	setsockopt(0, SOL_SOCKET, SO_REUSEADDR, (void *) &one, sizeof(one));
    609   1.9  msaitoh #endif
    610   1.1      cgd 
    611   1.9  msaitoh 	if (bind(0, (struct sockaddr *) &addr, sizeof(addr)) < 0)
    612   1.9  msaitoh 	    ERROR("main: bind");
    613   1.1      cgd     }
    614   1.9  msaitoh 
    615   1.9  msaitoh     if (background_flag)
    616   1.9  msaitoh     {
    617   1.9  msaitoh       if (listen(0, 3) < 0)
    618   1.9  msaitoh 	ERROR("main: listen");
    619   1.9  msaitoh     }
    620   1.9  msaitoh 
    621   1.9  msaitoh     if (set_gid)
    622   1.1      cgd     {
    623   1.9  msaitoh 	if (setgid(set_gid) == -1)
    624   1.9  msaitoh 	    ERROR("main: setgid");
    625   1.9  msaitoh 	/* Call me paranoid... PSz */
    626   1.9  msaitoh 	if (getgid() != set_gid)
    627   1.9  msaitoh 	    ERROR2("main: setgid failed: wanted %d, got GID %d", set_gid, getgid());
    628   1.9  msaitoh 	if (getegid() != set_gid)
    629   1.9  msaitoh 	    ERROR2("main: setgid failed: wanted %d, got EGID %d", set_gid, getegid());
    630   1.1      cgd     }
    631   1.9  msaitoh 
    632   1.9  msaitoh     if (set_uid)
    633   1.1      cgd     {
    634   1.9  msaitoh 	if (setuid(set_uid) == -1)
    635   1.9  msaitoh 	    ERROR("main: setuid");
    636   1.9  msaitoh 	/* Call me paranoid... PSz */
    637   1.9  msaitoh 	if (getuid() != set_uid)
    638   1.9  msaitoh 	    ERROR2("main: setuid failed: wanted %d, got UID %d", set_uid, getuid());
    639   1.9  msaitoh 	if (geteuid() != set_uid)
    640   1.9  msaitoh 	    ERROR2("main: setuid failed: wanted %d, got EUID %d", set_uid, geteuid());
    641   1.1      cgd     }
    642   1.1      cgd 
    643   1.1      cgd     /*
    644   1.9  msaitoh     ** Do some special handling if the "-b" or "-w" flags are used
    645   1.9  msaitoh     */
    646   1.9  msaitoh     if (background_flag)
    647   1.9  msaitoh     {
    648   1.9  msaitoh 	int nfds, fd;
    649  1.13  mycroft 	struct pollfd set[1];
    650   1.9  msaitoh 	struct sockaddr sad;
    651   1.9  msaitoh 	int sadlen;
    652   1.9  msaitoh 
    653   1.9  msaitoh 
    654   1.9  msaitoh 	/*
    655   1.9  msaitoh 	** Set up the SIGCHLD signal child termination handler so
    656   1.9  msaitoh 	** that we can avoid zombie processes hanging around and
    657   1.9  msaitoh 	** handle childs terminating before being able to complete the
    658   1.9  msaitoh 	** handshake.
    659   1.9  msaitoh 	*/
    660   1.9  msaitoh #if (defined(SVR4) || defined(hpux) || defined(__hpux) || defined(IRIX) || \
    661   1.9  msaitoh      defined(_CRAY) || defined(_AUX_SOURCE) || defined(sco) || \
    662   1.9  msaitoh 	 defined(LINUX))
    663   1.9  msaitoh 	signal(SIGCHLD, SIG_IGN);
    664   1.1      cgd #else
    665   1.9  msaitoh 	signal(SIGCHLD, child_handler);
    666   1.1      cgd #endif
    667   1.1      cgd 
    668  1.13  mycroft 	set[0].fd = 0;
    669  1.13  mycroft 	set[0].events = POLLIN;
    670  1.13  mycroft 
    671   1.9  msaitoh 	/*
    672   1.9  msaitoh 	** Loop and dispatch client handling processes
    673   1.9  msaitoh 	*/
    674   1.9  msaitoh 	do
    675   1.9  msaitoh 	{
    676   1.9  msaitoh #ifdef USE_SIGALARM
    677   1.9  msaitoh 	    /*
    678   1.9  msaitoh 	    ** Terminate if we've been idle for 'timeout' seconds
    679   1.9  msaitoh 	    */
    680   1.9  msaitoh 	    if (background_flag == 2 && timeout)
    681   1.9  msaitoh 	    {
    682   1.9  msaitoh 		signal(SIGALRM, alarm_handler);
    683   1.9  msaitoh 		alarm(timeout);
    684   1.9  msaitoh 	    }
    685   1.9  msaitoh #endif
    686   1.1      cgd 
    687   1.9  msaitoh 	    /*
    688   1.9  msaitoh 	    ** Wait for a connection request to occur.
    689   1.9  msaitoh 	    ** Ignore EINTR (Interrupted System Call).
    690   1.9  msaitoh 	    */
    691   1.9  msaitoh 	    do
    692   1.9  msaitoh 	    {
    693   1.9  msaitoh #ifndef USE_SIGALARM
    694   1.9  msaitoh 		if (timeout)
    695  1.13  mycroft 		    nfds = poll(set, 1, timeout * 1000);
    696   1.9  msaitoh 		else
    697   1.9  msaitoh #endif
    698  1.13  mycroft 		nfds = poll(set, 1, INFTIM);
    699   1.9  msaitoh 	    } while (nfds < 0  && errno == EINTR);
    700   1.9  msaitoh 
    701   1.9  msaitoh 	    /*
    702  1.11      wiz 	    ** An error occurred in select? Just die
    703   1.9  msaitoh 	    */
    704   1.9  msaitoh 	    if (nfds < 0)
    705  1.14  mycroft 		ERROR("main: poll");
    706   1.9  msaitoh 
    707   1.9  msaitoh 	    /*
    708   1.9  msaitoh 	    ** Timeout limit reached. Exit nicely
    709   1.9  msaitoh 	    */
    710   1.9  msaitoh 	    if (nfds == 0)
    711   1.9  msaitoh 		exit(0);
    712   1.1      cgd 
    713   1.9  msaitoh #ifdef USE_SIGALARM
    714   1.9  msaitoh 	    /*
    715   1.9  msaitoh 	    ** Disable the alarm timeout
    716   1.9  msaitoh 	    */
    717   1.9  msaitoh 	    alarm(0);
    718   1.9  msaitoh #endif
    719   1.1      cgd 
    720   1.9  msaitoh 	    /*
    721   1.9  msaitoh 	    ** Accept the new client
    722   1.9  msaitoh 	    */
    723   1.9  msaitoh 	    sadlen = sizeof(sad);
    724   1.9  msaitoh 	    errno = 0;
    725   1.9  msaitoh 	    fd = accept(0, &sad, &sadlen);
    726   1.9  msaitoh 	    if (fd == -1)
    727   1.9  msaitoh 		ERROR1("main: accept. errno = %d", errno);
    728   1.1      cgd 
    729   1.9  msaitoh 	    /*
    730   1.9  msaitoh 	    ** And fork, then close the fd if we are the parent.
    731   1.9  msaitoh 	    */
    732   1.9  msaitoh 	    child_pid = fork();
    733   1.9  msaitoh 	} while (child_pid && (close(fd), 1));
    734   1.9  msaitoh 
    735   1.9  msaitoh 	/*
    736   1.9  msaitoh 	** We are now in child, the parent has returned to "do" above.
    737   1.9  msaitoh 	*/
    738   1.9  msaitoh 	if (dup2(fd, 0) == -1)
    739   1.9  msaitoh 	    ERROR("main: dup2: failed fd 0");
    740   1.9  msaitoh 
    741   1.9  msaitoh 	if (dup2(fd, 1) == -1)
    742   1.9  msaitoh 	    ERROR("main: dup2: failed fd 1");
    743   1.9  msaitoh 
    744   1.9  msaitoh 	if (dup2(fd, 2) == -1)
    745   1.9  msaitoh 	    ERROR("main: dup2: failed fd 2");
    746   1.9  msaitoh     }
    747   1.9  msaitoh 
    748   1.1      cgd     /*
    749   1.9  msaitoh     ** Get foreign internet address
    750   1.1      cgd     */
    751   1.9  msaitoh     len = sizeof(sin);
    752   1.9  msaitoh     if (getpeername(0, (struct sockaddr *) &sin, &len) == -1)
    753   1.9  msaitoh     {
    754   1.9  msaitoh 	/*
    755   1.9  msaitoh 	** A user has tried to start us from the command line or
    756   1.9  msaitoh 	** the network link died, in which case this message won't
    757   1.9  msaitoh 	** reach to other end anyway, so lets give the poor user some
    758   1.9  msaitoh 	** errors.
    759   1.9  msaitoh 	*/
    760   1.9  msaitoh 	perror("in.identd: getpeername()");
    761   1.9  msaitoh 	exit(1);
    762   1.9  msaitoh     }
    763   1.9  msaitoh 
    764   1.9  msaitoh     faddr = sin.sin_addr;
    765   1.1      cgd 
    766   1.1      cgd 
    767   1.9  msaitoh #ifdef STRONG_LOG
    768   1.9  msaitoh     if (syslog_flag)
    769   1.9  msaitoh 	syslog(LOG_INFO, "Connection from %s", gethost(&faddr));
    770   1.9  msaitoh #endif
    771   1.1      cgd 
    772   1.9  msaitoh 
    773   1.9  msaitoh     /*
    774   1.9  msaitoh     ** Get local internet address
    775   1.1      cgd     */
    776   1.9  msaitoh     len = sizeof(sin);
    777   1.9  msaitoh #ifdef ATTSVR4
    778   1.9  msaitoh     if (t_getsockname(0, (struct sockaddr *) &sin, &len) == -1)
    779   1.1      cgd #else
    780   1.9  msaitoh     if (getsockname(0, (struct sockaddr *) &sin, &len) == -1)
    781   1.1      cgd #endif
    782   1.9  msaitoh     {
    783   1.9  msaitoh 	/*
    784   1.9  msaitoh 	** We can just die here, because if this fails then the
    785   1.9  msaitoh 	** network has died and we haven't got anyone to return
    786   1.9  msaitoh 	** errors to.
    787   1.9  msaitoh 	*/
    788   1.9  msaitoh 	exit(1);
    789   1.9  msaitoh     }
    790   1.9  msaitoh     laddr = sin.sin_addr;
    791   1.1      cgd 
    792   1.1      cgd 
    793   1.1      cgd     /*
    794   1.9  msaitoh     ** Get the local/foreign port pair from the luser
    795   1.1      cgd     */
    796   1.9  msaitoh     parse(stdin, &laddr, &faddr);
    797   1.1      cgd 
    798   1.9  msaitoh     exit(0);
    799   1.1      cgd }
    800