Home | History | Annotate | Line # | Download | only in identd
identd.c revision 1.3
      1 /*
      2 **	$Id: identd.c,v 1.3 1995/05/21 00:39:11 mycroft 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 (inet_aton(bind_address, &addr.sin_addr) == 0)
    422       {
    423 	struct hostent *hp;
    424 
    425 	hp = gethostbyname(bind_address);
    426 	if (!hp)
    427 	  ERROR1("no such address (%s) for -a switch", bind_address);
    428 
    429 	memcpy(&addr.sin_addr, hp->h_addr, sizeof(addr.sin_addr));
    430       }
    431     }
    432 
    433     if (isdigit(portno[0]))
    434       addr.sin_port = htons(atoi(portno));
    435     else
    436     {
    437       sp = getservbyname(portno, "tcp");
    438       if (sp == NULL)
    439 	ERROR1("main: getservbyname: %s", portno);
    440       addr.sin_port = sp->s_port;
    441     }
    442 
    443     if (bind(0, (struct sockaddr *) &addr, sizeof(addr)) < 0)
    444       ERROR("main: bind");
    445 
    446     if (listen(0, 3) < 0)
    447       ERROR("main: listen");
    448   }
    449 
    450   if (set_gid)
    451     if (setgid(set_gid) == -1)
    452       ERROR("main: setgid");
    453 
    454   if (set_uid)
    455     if (setuid(set_uid) == -1)
    456       ERROR("main: setuid");
    457 
    458   /*
    459   ** Do some special handling if the "-b" or "-w" flags are used
    460   */
    461   if (background_flag)
    462   {
    463     int nfds, fd;
    464     fd_set read_set;
    465 
    466 
    467     /*
    468     ** Set up the SIGCHLD signal child termination handler so
    469     ** that we can avoid zombie processes hanging around and
    470     ** handle childs terminating before being able to complete the
    471     ** handshake.
    472     */
    473 #if (defined(SVR4) || defined(hpux) || defined(__hpux) || \
    474      defined(_CRAY) || defined(_AUX_SOURCE))
    475     signal(SIGCHLD, SIG_IGN);
    476 #else
    477     signal(SIGCHLD, (SIGRETURN_TYPE (*)()) child_handler);
    478 #endif
    479 
    480     /*
    481     ** Loop and dispatch client handling processes
    482     */
    483     do
    484     {
    485       /*
    486       ** Terminate if we've been idle for 'timeout' seconds
    487       */
    488       if (background_flag == 2 && timeout)
    489       {
    490 	signal(SIGALRM, alarm_handler);
    491 	alarm(timeout);
    492       }
    493 
    494       /*
    495       ** Wait for a connection request to occur.
    496       ** Ignore EINTR (Interrupted System Call).
    497       */
    498       do
    499       {
    500 	FD_ZERO(&read_set);
    501 	FD_SET(0, &read_set);
    502 
    503 	if (timeout)
    504 	{
    505 	  tv.tv_sec = timeout;
    506 	  tv.tv_usec = 0;
    507 	  nfds = select(FD_SETSIZE, &read_set, NULL, NULL, &tv);
    508 	}
    509 	else
    510 
    511 	nfds = select(FD_SETSIZE, &read_set, NULL, NULL, NULL);
    512       } while (nfds < 0  && errno == EINTR);
    513 
    514       /*
    515       ** An error occured in select? Just die
    516       */
    517       if (nfds < 0)
    518 	ERROR("main: select");
    519 
    520       /*
    521       ** Timeout limit reached. Exit nicely
    522       */
    523       if (nfds == 0)
    524 	exit(0);
    525 
    526       /*
    527       ** Disable the alarm timeout
    528       */
    529       alarm(0);
    530 
    531       /*
    532       ** Accept the new client
    533       */
    534       fd = accept(0, NULL, NULL);
    535       if (fd == -1)
    536 	ERROR1("main: accept. errno = %d", errno);
    537 
    538       /*
    539       ** And fork, then close the fd if we are the parent.
    540       */
    541       child_pid = fork();
    542     } while (child_pid && (close(fd), 1));
    543 
    544     /*
    545     ** We are now in child, the parent has returned to "do" above.
    546     */
    547     if (dup2(fd, 0) == -1)
    548       ERROR("main: dup2: failed fd 0");
    549 
    550     if (dup2(fd, 1) == -1)
    551       ERROR("main: dup2: failed fd 1");
    552 
    553     if (dup2(fd, 2) == -1)
    554       ERROR("main: dup2: failed fd 2");
    555   }
    556 
    557   /*
    558   ** Get foreign internet address
    559   */
    560   len = sizeof(sin);
    561   if (getpeername(0, (struct sockaddr *) &sin, &len) == -1)
    562   {
    563     /*
    564     ** A user has tried to start us from the command line or
    565     ** the network link died, in which case this message won't
    566     ** reach to other end anyway, so lets give the poor user some
    567     ** errors.
    568     */
    569     perror("in.identd: getpeername()");
    570     exit(1);
    571   }
    572 
    573   faddr = sin.sin_addr;
    574 
    575 
    576   /*
    577   ** Open the connection to the Syslog daemon if requested
    578   */
    579   if (syslog_flag)
    580   {
    581 #ifdef LOG_DAEMON
    582     openlog("identd", LOG_PID, syslog_facility);
    583 #else
    584     openlog("identd", LOG_PID);
    585 #endif
    586 
    587     syslog(LOG_INFO, "Connection from %s", gethost(&faddr));
    588   }
    589 
    590 
    591   /*
    592   ** Get local internet address
    593   */
    594   len = sizeof(sin);
    595 #ifdef ATTSVR4
    596   if (t_getsockname(0, (struct sockaddr *) &sin, &len) == -1)
    597 #else
    598   if (getsockname(0, (struct sockaddr *) &sin, &len) == -1)
    599 #endif
    600   {
    601     /*
    602     ** We can just die here, because if this fails then the
    603     ** network has died and we haven't got anyone to return
    604     ** errors to.
    605     */
    606     exit(1);
    607   }
    608   laddr = sin.sin_addr;
    609 
    610 
    611   /*
    612   ** Get the local/foreign port pair from the luser
    613   */
    614   parse(stdin, &laddr, &faddr);
    615 
    616   exit(0);
    617 }
    618