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