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