Home | History | Annotate | Line # | Download | only in inetd
inetd.c revision 1.23
      1 /*	$NetBSD: inetd.c,v 1.23 1997/03/13 17:22:23 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1991, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 static char copyright[] =
     38 "@(#) Copyright (c) 1983, 1991, 1993, 1994\n\
     39 	The Regents of the University of California.  All rights reserved.\n";
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)inetd.c	8.4 (Berkeley) 4/13/94";
     45 #else
     46 static char rcsid[] = "$NetBSD: inetd.c,v 1.23 1997/03/13 17:22:23 mycroft Exp $";
     47 #endif
     48 #endif /* not lint */
     49 
     50 /*
     51  * Inetd - Internet super-server
     52  *
     53  * This program invokes all internet services as needed.  Connection-oriented
     54  * services are invoked each time a connection is made, by creating a process.
     55  * This process is passed the connection as file descriptor 0 and is expected
     56  * to do a getpeername to find out the source host and port.
     57  *
     58  * Datagram oriented services are invoked when a datagram
     59  * arrives; a process is created and passed a pending message
     60  * on file descriptor 0.  Datagram servers may either connect
     61  * to their peer, freeing up the original socket for inetd
     62  * to receive further messages on, or ``take over the socket'',
     63  * processing all arriving datagrams and, eventually, timing
     64  * out.	 The first type of server is said to be ``multi-threaded'';
     65  * the second type of server ``single-threaded''.
     66  *
     67  * Inetd uses a configuration file which is read at startup
     68  * and, possibly, at some later time in response to a hangup signal.
     69  * The configuration file is ``free format'' with fields given in the
     70  * order shown below.  Continuation lines for an entry must being with
     71  * a space or tab.  All fields must be present in each entry.
     72  *
     73  *	service name			must be in /etc/services or must
     74  *					name a tcpmux service
     75  *	socket type			stream/dgram/raw/rdm/seqpacket
     76  *	protocol			must be in /etc/protocols
     77  *	wait/nowait[.max]		single-threaded/multi-threaded, max #
     78  *	user[.group]			user/group to run daemon as
     79  *	server program			full path name
     80  *	server program arguments	maximum of MAXARGS (20)
     81  *
     82  * For RPC services
     83  *      service name/version            must be in /etc/rpc
     84  *	socket type			stream/dgram/raw/rdm/seqpacket
     85  *	protocol			must be in /etc/protocols
     86  *	wait/nowait[.max]		single-threaded/multi-threaded
     87  *	user[.group]			user to run daemon as
     88  *	server program			full path name
     89  *	server program arguments	maximum of MAXARGS (20)
     90  *
     91  * For non-RPC services, the "service name" can be of the form
     92  * hostaddress:servicename, in which case the hostaddress is used
     93  * as the host portion of the address to listen on.  If hostaddress
     94  * consists of a single `*' character, INADDR_ANY is used.
     95  *
     96  * A line can also consist of just
     97  *	hostaddress:
     98  * where hostaddress is as in the preceding paragraph.  Such a line must
     99  * have no further fields; the specified hostaddress is remembered and
    100  * used for all further lines that have no hostaddress specified,
    101  * until the next such line (or EOF).  (This is why * is provided to
    102  * allow explicit specification of INADDR_ANY.)  A line
    103  *	*:
    104  * is implicitly in effect at the beginning of the file.
    105  *
    106  * The hostaddress specifier may (and often will) contain dots;
    107  * the service name must not.
    108  *
    109  * For RPC services, host-address specifiers are accepted and will
    110  * work to some extent; however, because of limitations in the
    111  * portmapper interface, it will not work to try to give more than
    112  * one line for any given RPC service, even if the host-address
    113  * specifiers are different.
    114  *
    115  * TCP services without official port numbers are handled with the
    116  * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for
    117  * requests. When a connection is made from a foreign host, the service
    118  * requested is passed to tcpmux, which looks it up in the servtab list
    119  * and returns the proper entry for the service. Tcpmux returns a
    120  * negative reply if the service doesn't exist, otherwise the invoked
    121  * server is expected to return the positive reply if the service type in
    122  * inetd.conf file has the prefix "tcpmux/". If the service type has the
    123  * prefix "tcpmux/+", tcpmux will return the positive reply for the
    124  * process; this is for compatibility with older server code, and also
    125  * allows you to invoke programs that use stdin/stdout without putting any
    126  * special server code in them. Services that use tcpmux are "nowait"
    127  * because they do not have a well-known port and hence cannot listen
    128  * for new requests.
    129  *
    130  * Comment lines are indicated by a `#' in column 1.
    131  */
    132 
    133 /*
    134  * Here's the scoop concerning the user.group feature:
    135  *
    136  * 1) set-group-option off.
    137  *
    138  * 	a) user = root:	NO setuid() or setgid() is done
    139  *
    140  * 	b) other:	setuid()
    141  * 			setgid(primary group as found in passwd)
    142  * 			initgroups(name, primary group)
    143  *
    144  * 2) set-group-option on.
    145  *
    146  * 	a) user = root:	NO setuid()
    147  * 			setgid(specified group)
    148  * 			NO initgroups()
    149  *
    150  * 	b) other:	setuid()
    151  * 			setgid(specified group)
    152  * 			initgroups(name, specified group)
    153  *
    154  */
    155 
    156 #include <sys/param.h>
    157 #include <sys/stat.h>
    158 #include <sys/ioctl.h>
    159 #include <sys/socket.h>
    160 #include <sys/un.h>
    161 #include <sys/wait.h>
    162 #include <sys/time.h>
    163 #include <sys/resource.h>
    164 
    165 #ifndef RLIMIT_NOFILE
    166 #define RLIMIT_NOFILE	RLIMIT_OFILE
    167 #endif
    168 
    169 #define RPC
    170 
    171 #include <netinet/in.h>
    172 #include <arpa/inet.h>
    173 #ifdef RPC
    174 #include <rpc/rpc.h>
    175 #endif
    176 
    177 #include <errno.h>
    178 #include <grp.h>
    179 #include <netdb.h>
    180 #include <pwd.h>
    181 #include <signal.h>
    182 #include <stdio.h>
    183 #include <stdlib.h>
    184 #include <string.h>
    185 #include <syslog.h>
    186 #include <unistd.h>
    187 
    188 #include "pathnames.h"
    189 
    190 #ifdef LIBWRAP
    191 # include <tcpd.h>
    192 #ifndef LIBWRAP_ALLOW_FACILITY
    193 # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
    194 #endif
    195 #ifndef LIBWRAP_ALLOW_SEVERITY
    196 # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
    197 #endif
    198 #ifndef LIBWRAP_DENY_FACILITY
    199 # define LIBWRAP_DENY_FACILITY LOG_AUTH
    200 #endif
    201 #ifndef LIBWRAP_DENY_SEVERITY
    202 # define LIBWRAP_DENY_SEVERITY LOG_WARNING
    203 #endif
    204 int allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
    205 int deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
    206 #endif
    207 
    208 #define	TOOMANY		40		/* don't start more than TOOMANY */
    209 #define	CNT_INTVL	60		/* servers in CNT_INTVL sec. */
    210 #define	RETRYTIME	(60*10)		/* retry after bind or server fail */
    211 
    212 #define	SIGBLOCK	(sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM))
    213 
    214 int	debug;
    215 #ifdef LIBWRAP
    216 int	lflag;
    217 #endif
    218 int	nsock, maxsock;
    219 fd_set	allsock;
    220 int	options;
    221 int	timingout;
    222 struct	servent *sp;
    223 char	*curdom;
    224 
    225 #ifndef OPEN_MAX
    226 #define OPEN_MAX	64
    227 #endif
    228 
    229 /* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */
    230 #define FD_MARGIN	(8)
    231 typeof(((struct rlimit *)0)->rlim_cur)	rlim_ofile_cur = OPEN_MAX;
    232 
    233 #ifdef RLIMIT_NOFILE
    234 struct rlimit	rlim_ofile;
    235 #endif
    236 
    237 struct	servtab {
    238 	char	*se_hostaddr;		/* host address to listen on */
    239 	char	*se_service;		/* name of service */
    240 	int	se_socktype;		/* type of socket to use */
    241 	int	se_family;		/* address family */
    242 	char	*se_proto;		/* protocol used */
    243 	int	se_rpcprog;		/* rpc program number */
    244 	int	se_rpcversl;		/* rpc program lowest version */
    245 	int	se_rpcversh;		/* rpc program highest version */
    246 #define isrpcservice(sep)	((sep)->se_rpcversl != 0)
    247 	short	se_wait;		/* single threaded server */
    248 	short	se_checked;		/* looked at during merge */
    249 	char	*se_user;		/* user name to run as */
    250 	char	*se_group;		/* group name to run as */
    251 	struct	biltin *se_bi;		/* if built-in, description */
    252 	char	*se_server;		/* server program */
    253 #define	MAXARGV 20
    254 	char	*se_argv[MAXARGV+1];	/* program arguments */
    255 	int	se_fd;			/* open descriptor */
    256 	int	se_type;		/* type */
    257 	union {
    258 		struct	sockaddr se_un_ctrladdr;
    259 		struct	sockaddr_in se_un_ctrladdr_in;
    260 		struct	sockaddr_un se_un_ctrladdr_un;
    261 	} se_un;			/* bound address */
    262 #define se_ctrladdr	se_un.se_un_ctrladdr
    263 #define se_ctrladdr_in	se_un.se_un_ctrladdr_in
    264 #define se_ctrladdr_un	se_un.se_un_ctrladdr_un
    265 	int	se_ctrladdr_size;
    266 	int	se_max;			/* max # of instances of this service */
    267 	int	se_count;		/* number started since se_time */
    268 	struct	timeval se_time;	/* start of se_count */
    269 #ifdef MULOG
    270 	int	se_log;
    271 #define MULOG_RFC931	0x40000000
    272 #endif
    273 	struct	servtab *se_next;
    274 } *servtab;
    275 
    276 #define NORM_TYPE	0
    277 #define MUX_TYPE	1
    278 #define MUXPLUS_TYPE	2
    279 #define ISMUX(sep)	(((sep)->se_type == MUX_TYPE) || \
    280 			 ((sep)->se_type == MUXPLUS_TYPE))
    281 #define ISMUXPLUS(sep)	((sep)->se_type == MUXPLUS_TYPE)
    282 
    283 
    284 void		chargen_dg __P((int, struct servtab *));
    285 void		chargen_stream __P((int, struct servtab *));
    286 void		close_sep __P((struct servtab *));
    287 void		config __P((int));
    288 void		daytime_dg __P((int, struct servtab *));
    289 void		daytime_stream __P((int, struct servtab *));
    290 void		discard_dg __P((int, struct servtab *));
    291 void		discard_stream __P((int, struct servtab *));
    292 void		echo_dg __P((int, struct servtab *));
    293 void		echo_stream __P((int, struct servtab *));
    294 void		endconfig __P((void));
    295 struct servtab *enter __P((struct servtab *));
    296 void		freeconfig __P((struct servtab *));
    297 struct servtab *getconfigent __P((void));
    298 void		goaway __P((int));
    299 void		machtime_dg __P((int, struct servtab *));
    300 void		machtime_stream __P((int, struct servtab *));
    301 char	       *newstr __P((char *));
    302 char	       *nextline __P((FILE *));
    303 void		print_service __P((char *, struct servtab *));
    304 void		reapchild __P((int));
    305 void		retry __P((int));
    306 int		setconfig __P((void));
    307 void		setup __P((struct servtab *));
    308 char	       *sskip __P((char **));
    309 char	       *skip __P((char **));
    310 struct servtab *tcpmux __P((int));
    311 void		usage __P((void));
    312 
    313 struct biltin {
    314 	char	*bi_service;		/* internally provided service name */
    315 	int	bi_socktype;		/* type of socket supported */
    316 	short	bi_fork;		/* 1 if should fork before call */
    317 	short	bi_wait;		/* 1 if should wait for child */
    318 	void	(*bi_fn)();		/* function which performs it */
    319 } biltins[] = {
    320 	/* Echo received data */
    321 	{ "echo",	SOCK_STREAM,	1, 0,	echo_stream },
    322 	{ "echo",	SOCK_DGRAM,	0, 0,	echo_dg },
    323 
    324 	/* Internet /dev/null */
    325 	{ "discard",	SOCK_STREAM,	1, 0,	discard_stream },
    326 	{ "discard",	SOCK_DGRAM,	0, 0,	discard_dg },
    327 
    328 	/* Return 32 bit time since 1900 */
    329 	{ "time",	SOCK_STREAM,	0, 0,	machtime_stream },
    330 	{ "time",	SOCK_DGRAM,	0, 0,	machtime_dg },
    331 
    332 	/* Return human-readable time */
    333 	{ "daytime",	SOCK_STREAM,	0, 0,	daytime_stream },
    334 	{ "daytime",	SOCK_DGRAM,	0, 0,	daytime_dg },
    335 
    336 	/* Familiar character generator */
    337 	{ "chargen",	SOCK_STREAM,	1, 0,	chargen_stream },
    338 	{ "chargen",	SOCK_DGRAM,	0, 0,	chargen_dg },
    339 
    340 	{ "tcpmux",	SOCK_STREAM,	1, 0,	(void (*)())tcpmux },
    341 
    342 	{ NULL }
    343 };
    344 
    345 #define NUMINT	(sizeof(intab) / sizeof(struct inent))
    346 char	*CONFIG = _PATH_INETDCONF;
    347 char	**Argv;
    348 char 	*LastArg;
    349 extern char	*__progname;
    350 
    351 #ifdef sun
    352 /*
    353  * Sun's RPC library caches the result of `dtablesize()'
    354  * This is incompatible with our "bumping" of file descriptors "on demand"
    355  */
    356 int
    357 _rpc_dtablesize()
    358 {
    359 	return rlim_ofile_cur;
    360 }
    361 #endif
    362 
    363 main(argc, argv, envp)
    364 	int argc;
    365 	char *argv[], *envp[];
    366 {
    367 	struct servtab *sep, *nsep;
    368 	struct passwd *pwd;
    369 	struct group *grp;
    370 	struct sigvec sv;
    371 	int tmpint, ch, dofork;
    372 	pid_t pid;
    373 	char buf[50];
    374 #ifdef LIBWRAP
    375 	struct request_info req;
    376 	int denied;
    377 	char *service;
    378 #endif
    379 
    380 	Argv = argv;
    381 	if (envp == 0 || *envp == 0)
    382 		envp = argv;
    383 	while (*envp)
    384 		envp++;
    385 	LastArg = envp[-1] + strlen(envp[-1]);
    386 
    387 	while ((ch = getopt(argc, argv,
    388 #ifdef LIBWRAP
    389 					"dl"
    390 #else
    391 					"d"
    392 #endif
    393 					   )) != EOF)
    394 		switch(ch) {
    395 		case 'd':
    396 			debug = 1;
    397 			options |= SO_DEBUG;
    398 			break;
    399 #ifdef LIBWRAP
    400 		case 'l':
    401 			lflag = 1;
    402 			break;
    403 #endif
    404 		case '?':
    405 		default:
    406 			usage();
    407 		}
    408 	argc -= optind;
    409 	argv += optind;
    410 
    411 	if (argc > 0)
    412 		CONFIG = argv[0];
    413 
    414 	if (debug == 0)
    415 		daemon(0, 0);
    416 	openlog(__progname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
    417 	logpid();
    418 
    419 #ifdef RLIMIT_NOFILE
    420 	if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
    421 		syslog(LOG_ERR, "getrlimit: %m");
    422 	} else {
    423 		rlim_ofile_cur = rlim_ofile.rlim_cur;
    424 		if (rlim_ofile_cur == RLIM_INFINITY)	/* ! */
    425 			rlim_ofile_cur = OPEN_MAX;
    426 	}
    427 #endif
    428 
    429 	memset(&sv, 0, sizeof(sv));
    430 	sv.sv_mask = SIGBLOCK;
    431 	sv.sv_handler = retry;
    432 	sigvec(SIGALRM, &sv, (struct sigvec *)0);
    433 	config(SIGHUP);
    434 	sv.sv_handler = config;
    435 	sigvec(SIGHUP, &sv, (struct sigvec *)0);
    436 	sv.sv_handler = reapchild;
    437 	sigvec(SIGCHLD, &sv, (struct sigvec *)0);
    438 	sv.sv_handler = goaway;
    439 	sigvec(SIGTERM, &sv, (struct sigvec *)0);
    440 	sv.sv_handler = goaway;
    441 	sigvec(SIGINT, &sv, (struct sigvec *)0);
    442 
    443 	{
    444 		/* space for daemons to overwrite environment for ps */
    445 #define	DUMMYSIZE	100
    446 		char dummy[DUMMYSIZE];
    447 
    448 		(void)memset(dummy, 'x', DUMMYSIZE - 1);
    449 		dummy[DUMMYSIZE - 1] = '\0';
    450 
    451 		(void)setenv("inetd_dummy", dummy, 1);
    452 	}
    453 
    454 	for (;;) {
    455 	    int n, ctrl;
    456 	    fd_set readable;
    457 
    458 	    if (nsock == 0) {
    459 		(void) sigblock(SIGBLOCK);
    460 		while (nsock == 0)
    461 		    sigpause(0L);
    462 		(void) sigsetmask(0L);
    463 	    }
    464 	    readable = allsock;
    465 	    if ((n = select(maxsock + 1, &readable, (fd_set *)0,
    466 		(fd_set *)0, (struct timeval *)0)) <= 0) {
    467 		    if (n < 0 && errno != EINTR)
    468 			syslog(LOG_WARNING, "select: %m");
    469 		    sleep(1);
    470 		    continue;
    471 	    }
    472 	    for (sep = servtab; n && sep; sep = nsep) {
    473 	    nsep = sep->se_next;
    474 	    if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
    475 		n--;
    476 		if (debug)
    477 			fprintf(stderr, "someone wants %s\n", sep->se_service);
    478 		if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
    479 			/* XXX here do the libwrap check-before-accept */
    480 			ctrl = accept(sep->se_fd, (struct sockaddr *)0,
    481 			    (int *)0);
    482 			if (debug)
    483 				fprintf(stderr, "accept, ctrl %d\n", ctrl);
    484 			if (ctrl < 0) {
    485 				if (errno != EINTR)
    486 					syslog(LOG_WARNING,
    487 					    "accept (for %s): %m",
    488 					    sep->se_service);
    489 				continue;
    490 			}
    491 			/*
    492 			 * Call tcpmux to find the real service to exec.
    493 			 */
    494 			if (sep->se_bi &&
    495 			    sep->se_bi->bi_fn == (void (*)()) tcpmux) {
    496 				sep = tcpmux(ctrl);
    497 				if (sep == NULL) {
    498 					close(ctrl);
    499 					continue;
    500 				}
    501 			}
    502 		} else
    503 			ctrl = sep->se_fd;
    504 		(void) sigblock(SIGBLOCK);
    505 		pid = 0;
    506 		dofork = (sep->se_bi == 0 || sep->se_bi->bi_fork);
    507 		if (dofork) {
    508 			if (sep->se_count++ == 0)
    509 			    (void)gettimeofday(&sep->se_time,
    510 			        (struct timezone *)0);
    511 			else if (sep->se_count >= sep->se_max) {
    512 				struct timeval now;
    513 
    514 				(void)gettimeofday(&now, (struct timezone *)0);
    515 				if (now.tv_sec - sep->se_time.tv_sec >
    516 				    CNT_INTVL) {
    517 					sep->se_time = now;
    518 					sep->se_count = 1;
    519 				} else {
    520 					syslog(LOG_ERR,
    521 			"%s/%s server failing (looping), service terminated\n",
    522 					    sep->se_service, sep->se_proto);
    523 					close_sep(sep);
    524 					sigsetmask(0L);
    525 					if (!timingout) {
    526 						timingout = 1;
    527 						alarm(RETRYTIME);
    528 					}
    529 					continue;
    530 				}
    531 			}
    532 			pid = fork();
    533 		}
    534 		if (pid < 0) {
    535 			syslog(LOG_ERR, "fork: %m");
    536 			if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
    537 				close(ctrl);
    538 			sigsetmask(0L);
    539 			sleep(1);
    540 			continue;
    541 		}
    542 		if (pid && sep->se_wait) {
    543 			sep->se_wait = pid;
    544 			FD_CLR(sep->se_fd, &allsock);
    545 			nsock--;
    546 		}
    547 		sigsetmask(0L);
    548 		if (pid == 0) {
    549 			if (debug && dofork)
    550 				setsid();
    551 #ifdef LIBWRAP
    552 			request_init(&req, RQ_DAEMON, sep->se_argv[0] ?
    553 			    sep->se_argv[0] : sep->se_service, RQ_FILE, ctrl,
    554 			    NULL);
    555 			fromhost(&req);
    556 			denied = !hosts_access(&req);
    557 			if (denied || lflag) {
    558 				sp = getservbyport(sep->se_ctrladdr_in.sin_port,
    559 				    sep->se_proto);
    560 				if (sp == NULL) {
    561 					(void)snprintf(buf, sizeof buf, "%d",
    562 					    ntohs(sep->se_ctrladdr_in.sin_port));
    563 					service = buf;
    564 				} else
    565 					service = sp->s_name;
    566 			}
    567 			if (denied) {
    568 				syslog(deny_severity, "refused "
    569 				    "connection from %.500s, service %s (%s)",
    570 				    eval_client(&req), service, sep->se_proto);
    571 				goto reject;
    572 			}
    573 			if (lflag) {
    574 				syslog(allow_severity,
    575 				    "connection from %.500s, service %s (%s)",
    576 				    eval_client(&req), service, sep->se_proto);
    577 			}
    578 #endif /* LIBWRAP */
    579 			if (sep->se_bi)
    580 				(*sep->se_bi->bi_fn)(ctrl, sep);
    581 			else {
    582 				if ((pwd = getpwnam(sep->se_user)) == NULL) {
    583 					syslog(LOG_ERR,
    584 					    "%s/%s: %s: No such user",
    585 					    sep->se_service, sep->se_proto,
    586 					    sep->se_user);
    587 					goto reject;
    588 				}
    589 				if (sep->se_group &&
    590 				    (grp = getgrnam(sep->se_group)) == NULL) {
    591 					syslog(LOG_ERR,
    592 					    "%s/%s: %s: No such group",
    593 					    sep->se_service, sep->se_proto,
    594 					    sep->se_group);
    595 					goto reject;
    596 				}
    597 				if (pwd->pw_uid) {
    598 					if (sep->se_group)
    599 						pwd->pw_gid = grp->gr_gid;
    600 					if (setgid(pwd->pw_gid) < 0) {
    601 						syslog(LOG_ERR,
    602 						 "%s/%s: can't set gid %d: %m",
    603 						    sep->se_service,
    604 						    sep->se_proto, pwd->pw_gid);
    605 						goto reject;
    606 					}
    607 					(void) initgroups(pwd->pw_name,
    608 					    pwd->pw_gid);
    609 					if (setuid(pwd->pw_uid) < 0) {
    610 						syslog(LOG_ERR,
    611 						 "%s/%s: can't set uid %d: %m",
    612 						    sep->se_service,
    613 						    sep->se_proto, pwd->pw_uid);
    614 						goto reject;
    615 					}
    616 				} else if (sep->se_group) {
    617 					(void) setgid((gid_t)grp->gr_gid);
    618 				}
    619 				if (debug)
    620 					fprintf(stderr, "%d execl %s\n",
    621 					    getpid(), sep->se_server);
    622 #ifdef MULOG
    623 				if (sep->se_log)
    624 					dolog(sep, ctrl);
    625 #endif
    626 				if (ctrl != 0) {
    627 					dup2(ctrl, 0);
    628 					close(ctrl);
    629 					ctrl = 0;
    630 				}
    631 				dup2(0, 1);
    632 				dup2(0, 2);
    633 #ifdef RLIMIT_NOFILE
    634 				if (rlim_ofile.rlim_cur != rlim_ofile_cur) {
    635 					if (setrlimit(RLIMIT_NOFILE,
    636 							&rlim_ofile) < 0)
    637 						syslog(LOG_ERR,
    638 						    "setrlimit: %m");
    639 				}
    640 #endif
    641 				for (tmpint = rlim_ofile_cur-1; --tmpint > 2; )
    642 					(void)close(tmpint);
    643 				execv(sep->se_server, sep->se_argv);
    644 				syslog(LOG_ERR,
    645 				    "cannot execute %s: %m", sep->se_server);
    646 			reject:
    647 				if (sep->se_socktype != SOCK_STREAM)
    648 					recv(ctrl, buf, sizeof (buf), 0);
    649 				_exit(1);
    650 			}
    651 		}
    652 		if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
    653 			close(ctrl);
    654 	    }
    655 	    }
    656 	}
    657 }
    658 
    659 void
    660 reapchild(signo)
    661 	int signo;
    662 {
    663 	int status;
    664 	pid_t pid;
    665 	struct servtab *sep;
    666 
    667 	for (;;) {
    668 		pid = wait3(&status, WNOHANG, (struct rusage *)0);
    669 		if (pid <= 0)
    670 			break;
    671 		if (debug)
    672 			fprintf(stderr, "%d reaped, status %#x\n",
    673 			    pid, status);
    674 		for (sep = servtab; sep; sep = sep->se_next)
    675 			if (sep->se_wait == pid) {
    676 				if (WIFEXITED(status) && WEXITSTATUS(status))
    677 					syslog(LOG_WARNING,
    678 					    "%s: exit status 0x%x",
    679 					    sep->se_server, WEXITSTATUS(status));
    680 				else if (WIFSIGNALED(status))
    681 					syslog(LOG_WARNING,
    682 					    "%s: exit signal 0x%x",
    683 					    sep->se_server, WTERMSIG(status));
    684 				sep->se_wait = 1;
    685 				FD_SET(sep->se_fd, &allsock);
    686 				nsock++;
    687 				if (debug)
    688 					fprintf(stderr, "restored %s, fd %d\n",
    689 					    sep->se_service, sep->se_fd);
    690 			}
    691 	}
    692 }
    693 
    694 void
    695 config(signo)
    696 	int signo;
    697 {
    698 	struct servtab *sep, *cp, **sepp;
    699 	struct passwd *pwd;
    700 	long omask;
    701 	int n;
    702 
    703 	if (!setconfig()) {
    704 		syslog(LOG_ERR, "%s: %m", CONFIG);
    705 		return;
    706 	}
    707 	for (sep = servtab; sep; sep = sep->se_next)
    708 		sep->se_checked = 0;
    709 	while (cp = getconfigent()) {
    710 		for (sep = servtab; sep; sep = sep->se_next)
    711 			if (strcmp(sep->se_service, cp->se_service) == 0 &&
    712 			    strcmp(sep->se_hostaddr, cp->se_hostaddr) == 0 &&
    713 			    strcmp(sep->se_proto, cp->se_proto) == 0 &&
    714 			    ISMUX(sep) == ISMUX(cp))
    715 				break;
    716 		if (sep != 0) {
    717 			int i;
    718 
    719 #define SWAP(type, a, b) {type c=(type)a; (type)a=(type)b; (type)b=(type)c;}
    720 
    721 			omask = sigblock(SIGBLOCK);
    722 			/*
    723 			 * sep->se_wait may be holding the pid of a daemon
    724 			 * that we're waiting for.  If so, don't overwrite
    725 			 * it unless the config file explicitly says don't
    726 			 * wait.
    727 			 */
    728 			if (cp->se_bi == 0 &&
    729 			    (sep->se_wait == 1 || cp->se_wait == 0))
    730 				sep->se_wait = cp->se_wait;
    731 			SWAP(int, cp->se_max, sep->se_max);
    732 			SWAP(char *, sep->se_user, cp->se_user);
    733 			SWAP(char *, sep->se_group, cp->se_group);
    734 			SWAP(char *, sep->se_server, cp->se_server);
    735 			for (i = 0; i < MAXARGV; i++)
    736 				SWAP(char *, sep->se_argv[i], cp->se_argv[i]);
    737 #undef SWAP
    738 			if (isrpcservice(sep))
    739 				unregister_rpc(sep);
    740 			sep->se_rpcversl = cp->se_rpcversl;
    741 			sep->se_rpcversh = cp->se_rpcversh;
    742 			sigsetmask(omask);
    743 			freeconfig(cp);
    744 			if (debug)
    745 				print_service("REDO", sep);
    746 		} else {
    747 			sep = enter(cp);
    748 			if (debug)
    749 				print_service("ADD ", sep);
    750 		}
    751 		sep->se_checked = 1;
    752 
    753 		switch (sep->se_family) {
    754 		case AF_UNIX:
    755 			if (sep->se_fd != -1)
    756 				break;
    757 			(void)unlink(sep->se_service);
    758 			n = strlen(sep->se_service);
    759 			if (n > sizeof(sep->se_ctrladdr_un.sun_path) - 1)
    760 				n = sizeof(sep->se_ctrladdr_un.sun_path) - 1;
    761 			strncpy(sep->se_ctrladdr_un.sun_path,
    762 			    sep->se_service, n);
    763 			sep->se_ctrladdr_un.sun_family = AF_UNIX;
    764 			sep->se_ctrladdr_size = n +
    765 			    sizeof(sep->se_ctrladdr_un) -
    766 			    sizeof(sep->se_ctrladdr_un.sun_path);
    767 			if (!ISMUX(sep))
    768 				setup(sep);
    769 			break;
    770 		case AF_INET:
    771 			sep->se_ctrladdr_in.sin_family = AF_INET;
    772 			if (!strcmp(sep->se_hostaddr,"*"))
    773 				sep->se_ctrladdr_in.sin_addr.s_addr =
    774 				    INADDR_ANY;
    775 			else if (!inet_aton(sep->se_hostaddr,
    776 			    &sep->se_ctrladdr_in.sin_addr)) {
    777 				/* Do we really want to support hostname lookups here? */
    778 				struct hostent *hp;
    779 				hp = gethostbyname(sep->se_hostaddr);
    780 				if (hp == 0) {
    781 					syslog(LOG_ERR, "%s: unknown host",
    782 					    sep->se_hostaddr);
    783 					sep->se_checked = 0;
    784 					continue;
    785 				} else if (hp->h_addrtype != AF_INET) {
    786 					syslog(LOG_ERR,
    787 				       "%s: address isn't an Internet address",
    788 					    sep->se_hostaddr);
    789 					sep->se_checked = 0;
    790 					continue;
    791 				} else if (hp->h_length != sizeof(struct in_addr)) {
    792 					syslog(LOG_ERR,
    793 		       "%s: address size wrong (under DNS corruption attack?)",
    794 					    sep->se_hostaddr);
    795 					sep->se_checked = 0;
    796 					continue;
    797 				} else {
    798 					memcpy(&sep->se_ctrladdr_in.sin_addr,
    799 					    hp->h_addr_list[0],
    800 					    sizeof(struct in_addr));
    801 				}
    802 			}
    803 			if (ISMUX(sep)) {
    804 				sep->se_fd = -1;
    805 				continue;
    806 			}
    807 			sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr_in);
    808 			if (isrpcservice(sep)) {
    809 				struct rpcent *rp;
    810 
    811 				sep->se_rpcprog = atoi(sep->se_service);
    812 				if (sep->se_rpcprog == 0) {
    813 					rp = getrpcbyname(sep->se_service);
    814 					if (rp == 0) {
    815 						syslog(LOG_ERR,
    816 						    "%s/%s: unknown service",
    817 						    sep->se_service,
    818 						    sep->se_proto);
    819 						sep->se_checked = 0;
    820 						continue;
    821 					}
    822 					sep->se_rpcprog = rp->r_number;
    823 				}
    824 				if (sep->se_fd == -1 && !ISMUX(sep))
    825 					setup(sep);
    826 				if (sep->se_fd != -1)
    827 					register_rpc(sep);
    828 			} else {
    829 				u_short port = htons(atoi(sep->se_service));
    830 
    831 				if (!port) {
    832 					sp = getservbyname(sep->se_service,
    833 					    sep->se_proto);
    834 					if (sp == 0) {
    835 						syslog(LOG_ERR,
    836 						    "%s/%s: unknown service",
    837 						    sep->se_service,
    838 						    sep->se_proto);
    839 						sep->se_checked = 0;
    840 						continue;
    841 					}
    842 					port = sp->s_port;
    843 				}
    844 				if (port != sep->se_ctrladdr_in.sin_port) {
    845 					sep->se_ctrladdr_in.sin_port = port;
    846 					if (sep->se_fd >= 0)
    847 						close_sep(sep);
    848 				}
    849 				if (sep->se_fd == -1 && !ISMUX(sep))
    850 					setup(sep);
    851 			}
    852 		}
    853 	}
    854 	endconfig();
    855 	/*
    856 	 * Purge anything not looked at above.
    857 	 */
    858 	omask = sigblock(SIGBLOCK);
    859 	sepp = &servtab;
    860 	while (sep = *sepp) {
    861 		if (sep->se_checked) {
    862 			sepp = &sep->se_next;
    863 			continue;
    864 		}
    865 		*sepp = sep->se_next;
    866 		if (sep->se_fd >= 0)
    867 			close_sep(sep);
    868 		if (isrpcservice(sep))
    869 			unregister_rpc(sep);
    870 		if (sep->se_family == AF_UNIX)
    871 			(void)unlink(sep->se_service);
    872 		if (debug)
    873 			print_service("FREE", sep);
    874 		freeconfig(sep);
    875 		free((char *)sep);
    876 	}
    877 	(void) sigsetmask(omask);
    878 }
    879 
    880 void
    881 retry(signo)
    882 	int signo;
    883 {
    884 	struct servtab *sep;
    885 
    886 	timingout = 0;
    887 	for (sep = servtab; sep; sep = sep->se_next) {
    888 		if (sep->se_fd == -1 && !ISMUX(sep)) {
    889 			switch (sep->se_family) {
    890 			case AF_UNIX:
    891 			case AF_INET:
    892 				setup(sep);
    893 				if (sep->se_fd != -1 && isrpcservice(sep))
    894 					register_rpc(sep);
    895 				break;
    896 			}
    897 		}
    898 	}
    899 }
    900 
    901 void
    902 goaway(signo)
    903 	int signo;
    904 {
    905 	register struct servtab *sep;
    906 
    907 	for (sep = servtab; sep; sep = sep->se_next) {
    908 		if (sep->se_fd == -1)
    909 			continue;
    910 
    911 		switch (sep->se_family) {
    912 		case AF_UNIX:
    913 			(void)unlink(sep->se_service);
    914 			break;
    915 		case AF_INET:
    916 			if (sep->se_wait == 1 && isrpcservice(sep))
    917 				unregister_rpc(sep);
    918 			break;
    919 		}
    920 		(void)close(sep->se_fd);
    921 	}
    922 	(void)unlink(_PATH_INETDPID);
    923 	exit(0);
    924 }
    925 
    926 void
    927 setup(sep)
    928 	struct servtab *sep;
    929 {
    930 	int on = 1;
    931 
    932 	if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) {
    933 		if (debug)
    934 			fprintf(stderr, "socket failed on %s/%s: %s\n",
    935 			    sep->se_service, sep->se_proto, strerror(errno));
    936 		syslog(LOG_ERR, "%s/%s: socket: %m",
    937 		    sep->se_service, sep->se_proto);
    938 		return;
    939 	}
    940 #define	turnon(fd, opt) \
    941 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
    942 	if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) &&
    943 	    turnon(sep->se_fd, SO_DEBUG) < 0)
    944 		syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
    945 	if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
    946 		syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
    947 #undef turnon
    948 	if (bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size) < 0) {
    949 		if (debug)
    950 			fprintf(stderr, "bind failed on %s/%s: %s\n",
    951 			    sep->se_service, sep->se_proto, strerror(errno));
    952 		syslog(LOG_ERR, "%s/%s: bind: %m",
    953 		    sep->se_service, sep->se_proto);
    954 		(void) close(sep->se_fd);
    955 		sep->se_fd = -1;
    956 		if (!timingout) {
    957 			timingout = 1;
    958 			alarm(RETRYTIME);
    959 		}
    960 		return;
    961 	}
    962 	if (sep->se_socktype == SOCK_STREAM)
    963 		listen(sep->se_fd, 10);
    964 
    965 	FD_SET(sep->se_fd, &allsock);
    966 	nsock++;
    967 	if (sep->se_fd > maxsock) {
    968 		maxsock = sep->se_fd;
    969 		if (maxsock > rlim_ofile_cur - FD_MARGIN)
    970 			bump_nofile();
    971 	}
    972 	if (debug)
    973 		fprintf(stderr, "registered %s on %d\n",
    974 		    sep->se_server, sep->se_fd);
    975 }
    976 
    977 /*
    978  * Finish with a service and its socket.
    979  */
    980 void
    981 close_sep(sep)
    982 	struct servtab *sep;
    983 {
    984 	if (sep->se_fd >= 0) {
    985 		nsock--;
    986 		FD_CLR(sep->se_fd, &allsock);
    987 		(void) close(sep->se_fd);
    988 		sep->se_fd = -1;
    989 	}
    990 	sep->se_count = 0;
    991 	/*
    992 	 * Don't keep the pid of this running deamon: when reapchild()
    993 	 * reaps this pid, it would erroneously increment nsock.
    994 	 */
    995 	if (sep->se_wait > 1)
    996 		sep->se_wait = 1;
    997 }
    998 
    999 register_rpc(sep)
   1000 	register struct servtab *sep;
   1001 {
   1002 #ifdef RPC
   1003 	int n;
   1004 	struct sockaddr_in sin;
   1005 	struct protoent *pp;
   1006 
   1007 	if ((pp = getprotobyname(sep->se_proto+4)) == NULL) {
   1008 		syslog(LOG_ERR, "%s: getproto: %m",
   1009 		    sep->se_proto);
   1010 		return;
   1011 	}
   1012 	n = sizeof sin;
   1013 	if (getsockname(sep->se_fd, (struct sockaddr *)&sin, &n) < 0) {
   1014 		syslog(LOG_ERR, "%s/%s: getsockname: %m",
   1015 		    sep->se_service, sep->se_proto);
   1016 		return;
   1017 	}
   1018 
   1019 	for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
   1020 		if (debug)
   1021 			fprintf(stderr, "pmap_set: %u %u %u %u\n",
   1022 			    sep->se_rpcprog, n, pp->p_proto,
   1023 			    ntohs(sin.sin_port));
   1024 		(void)pmap_unset(sep->se_rpcprog, n);
   1025 		if (!pmap_set(sep->se_rpcprog, n, pp->p_proto, ntohs(sin.sin_port)))
   1026 			syslog(LOG_ERR, "pmap_set: %u %u %u %u: %m",
   1027 			    sep->se_rpcprog, n, pp->p_proto,
   1028 			    ntohs(sin.sin_port));
   1029 	}
   1030 #endif /* RPC */
   1031 }
   1032 
   1033 unregister_rpc(sep)
   1034 	register struct servtab *sep;
   1035 {
   1036 #ifdef RPC
   1037 	int n;
   1038 
   1039 	for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
   1040 		if (debug)
   1041 			fprintf(stderr, "pmap_unset(%u, %u)\n",
   1042 			    sep->se_rpcprog, n);
   1043 		if (!pmap_unset(sep->se_rpcprog, n))
   1044 			syslog(LOG_ERR, "pmap_unset(%u, %u)\n",
   1045 			    sep->se_rpcprog, n);
   1046 	}
   1047 #endif /* RPC */
   1048 }
   1049 
   1050 
   1051 struct servtab *
   1052 enter(cp)
   1053 	struct servtab *cp;
   1054 {
   1055 	struct servtab *sep;
   1056 	long omask;
   1057 
   1058 	sep = (struct servtab *)malloc(sizeof (*sep));
   1059 	if (sep == (struct servtab *)0) {
   1060 		syslog(LOG_ERR, "Out of memory.");
   1061 		exit(-1);
   1062 	}
   1063 	*sep = *cp;
   1064 	sep->se_fd = -1;
   1065 	sep->se_rpcprog = -1;
   1066 	omask = sigblock(SIGBLOCK);
   1067 	sep->se_next = servtab;
   1068 	servtab = sep;
   1069 	sigsetmask(omask);
   1070 	return (sep);
   1071 }
   1072 
   1073 FILE	*fconfig = NULL;
   1074 struct	servtab serv;
   1075 char	line[LINE_MAX];
   1076 char    *defhost;
   1077 
   1078 int
   1079 setconfig()
   1080 {
   1081 	if (defhost) free(defhost);
   1082 	defhost = newstr("*");
   1083 	if (fconfig != NULL) {
   1084 		fseek(fconfig, 0L, SEEK_SET);
   1085 		return (1);
   1086 	}
   1087 	fconfig = fopen(CONFIG, "r");
   1088 	return (fconfig != NULL);
   1089 }
   1090 
   1091 void
   1092 endconfig()
   1093 {
   1094 	if (fconfig) {
   1095 		(void) fclose(fconfig);
   1096 		fconfig = NULL;
   1097 	}
   1098 	if (defhost) {
   1099 		free(defhost);
   1100 		defhost = 0;
   1101 	}
   1102 }
   1103 
   1104 struct servtab *
   1105 getconfigent()
   1106 {
   1107 	struct servtab *sep = &serv;
   1108 	int argc;
   1109 	char *cp, *arg;
   1110 	static char TCPMUX_TOKEN[] = "tcpmux/";
   1111 #define MUX_LEN		(sizeof(TCPMUX_TOKEN)-1)
   1112 	char *hostdelim;
   1113 
   1114 more:
   1115 #ifdef MULOG
   1116 	while ((cp = nextline(fconfig)) && (*cp == '#' || *cp == '\0')) {
   1117 		/* Avoid use of `skip' if there is a danger of it looking
   1118 		 * at continuation lines.
   1119 		 */
   1120 		do {
   1121 			cp++;
   1122 		} while (*cp == ' ' || *cp == '\t');
   1123 		if (*cp == '\0')
   1124 			continue;
   1125 		if ((arg = skip(&cp)) == NULL)
   1126 			continue;
   1127 		if (strcmp(arg, "DOMAIN"))
   1128 			continue;
   1129 		if (curdom)
   1130 			free(curdom);
   1131 		curdom = NULL;
   1132 		while (*cp == ' ' || *cp == '\t')
   1133 			cp++;
   1134 		if (*cp == '\0')
   1135 			continue;
   1136 		arg = cp;
   1137 		while (*cp && *cp != ' ' && *cp != '\t')
   1138 			cp++;
   1139 		if (*cp != '\0')
   1140 			*cp++ = '\0';
   1141 		curdom = newstr(arg);
   1142 	}
   1143 #else
   1144 	while ((cp = nextline(fconfig)) && (*cp == '#' || *cp == '\0'))
   1145 		;
   1146 #endif
   1147 	if (cp == NULL)
   1148 		return ((struct servtab *)0);
   1149 	/*
   1150 	 * clear the static buffer, since some fields (se_ctrladdr,
   1151 	 * for example) don't get initialized here.
   1152 	 */
   1153 	memset((caddr_t)sep, 0, sizeof *sep);
   1154 	arg = skip(&cp);
   1155 	if (cp == NULL) {
   1156 		/* got an empty line containing just blanks/tabs. */
   1157 		goto more;
   1158 	}
   1159 	/* Check for a host name. */
   1160 	hostdelim = strrchr(arg, ':');
   1161 	if (hostdelim) {
   1162 		*hostdelim = '\0';
   1163 		sep->se_hostaddr = newstr(arg);
   1164 		arg = hostdelim + 1;
   1165 		/*
   1166 		 * If the line is of the form `host:', then just change the
   1167 		 * default host for the following lines.
   1168 		 */
   1169 		if (*arg == '\0') {
   1170 			arg = skip(&cp);
   1171 			if (cp == NULL) {
   1172 				free(defhost);
   1173 				defhost = sep->se_hostaddr;
   1174 				goto more;
   1175 			}
   1176 		}
   1177 	} else
   1178 		sep->se_hostaddr = newstr(defhost);
   1179 	if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) {
   1180 		char *c = arg + MUX_LEN;
   1181 		if (*c == '+') {
   1182 			sep->se_type = MUXPLUS_TYPE;
   1183 			c++;
   1184 		} else
   1185 			sep->se_type = MUX_TYPE;
   1186 		sep->se_service = newstr(c);
   1187 	} else {
   1188 		sep->se_service = newstr(arg);
   1189 		sep->se_type = NORM_TYPE;
   1190 	}
   1191 
   1192 	arg = sskip(&cp);
   1193 	if (strcmp(arg, "stream") == 0)
   1194 		sep->se_socktype = SOCK_STREAM;
   1195 	else if (strcmp(arg, "dgram") == 0)
   1196 		sep->se_socktype = SOCK_DGRAM;
   1197 	else if (strcmp(arg, "rdm") == 0)
   1198 		sep->se_socktype = SOCK_RDM;
   1199 	else if (strcmp(arg, "seqpacket") == 0)
   1200 		sep->se_socktype = SOCK_SEQPACKET;
   1201 	else if (strcmp(arg, "raw") == 0)
   1202 		sep->se_socktype = SOCK_RAW;
   1203 	else
   1204 		sep->se_socktype = -1;
   1205 
   1206 	sep->se_proto = newstr(sskip(&cp));
   1207 	if (strcmp(sep->se_proto, "unix") == 0) {
   1208 		sep->se_family = AF_UNIX;
   1209 	} else {
   1210 		sep->se_family = AF_INET;
   1211 		if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
   1212 #ifdef RPC
   1213 			char *cp, *ccp;
   1214 			cp = strchr(sep->se_service, '/');
   1215 			if (cp == 0) {
   1216 				syslog(LOG_ERR, "%s: no rpc version",
   1217 				    sep->se_service);
   1218 				goto more;
   1219 			}
   1220 			*cp++ = '\0';
   1221 			sep->se_rpcversl = sep->se_rpcversh =
   1222 			    strtol(cp, &ccp, 0);
   1223 			if (ccp == cp) {
   1224 		badafterall:
   1225 				syslog(LOG_ERR, "%s/%s: bad rpc version",
   1226 				    sep->se_service, cp);
   1227 				goto more;
   1228 			}
   1229 			if (*ccp == '-') {
   1230 				cp = ccp + 1;
   1231 				sep->se_rpcversh = strtol(cp, &ccp, 0);
   1232 				if (ccp == cp)
   1233 					goto badafterall;
   1234 			}
   1235 #else
   1236 			syslog(LOG_ERR, "%s: rpc services not suported",
   1237 			    sep->se_service);
   1238 			goto more;
   1239 #endif /* RPC */
   1240 		}
   1241 	}
   1242 	arg = sskip(&cp);
   1243 	{
   1244 		char *cp;
   1245 		cp = strchr(arg, '.');
   1246 		if (cp) {
   1247 			*cp++ = '\0';
   1248 			sep->se_max = atoi(cp);
   1249 		} else
   1250 			sep->se_max = TOOMANY;
   1251 	}
   1252 	sep->se_wait = strcmp(arg, "wait") == 0;
   1253 	if (ISMUX(sep)) {
   1254 		/*
   1255 		 * Silently enforce "nowait" for TCPMUX services since
   1256 		 * they don't have an assigned port to listen on.
   1257 		 */
   1258 		sep->se_wait = 0;
   1259 
   1260 		if (strcmp(sep->se_proto, "tcp")) {
   1261 			syslog(LOG_ERR,
   1262 			    "%s: bad protocol for tcpmux service %s",
   1263 			    CONFIG, sep->se_service);
   1264 			goto more;
   1265 		}
   1266 		if (sep->se_socktype != SOCK_STREAM) {
   1267 			syslog(LOG_ERR,
   1268 			    "%s: bad socket type for tcpmux service %s",
   1269 			    CONFIG, sep->se_service);
   1270 			goto more;
   1271 		}
   1272 	}
   1273 	sep->se_user = newstr(sskip(&cp));
   1274 	if (sep->se_group = strchr(sep->se_user, '.'))
   1275 		*sep->se_group++ = '\0';
   1276 	sep->se_server = newstr(sskip(&cp));
   1277 	if (strcmp(sep->se_server, "internal") == 0) {
   1278 		struct biltin *bi;
   1279 
   1280 		for (bi = biltins; bi->bi_service; bi++)
   1281 			if (bi->bi_socktype == sep->se_socktype &&
   1282 			    strcmp(bi->bi_service, sep->se_service) == 0)
   1283 				break;
   1284 		if (bi->bi_service == 0) {
   1285 			syslog(LOG_ERR, "internal service %s unknown",
   1286 			    sep->se_service);
   1287 			goto more;
   1288 		}
   1289 		sep->se_bi = bi;
   1290 		sep->se_wait = bi->bi_wait;
   1291 	} else
   1292 		sep->se_bi = NULL;
   1293 	argc = 0;
   1294 	for (arg = skip(&cp); cp; arg = skip(&cp)) {
   1295 #if MULOG
   1296 		char *colon;
   1297 
   1298 		if (argc == 0 && (colon = strrchr(arg, ':'))) {
   1299 			while (arg < colon) {
   1300 				int	x;
   1301 				char	*ccp;
   1302 
   1303 				switch (*arg++) {
   1304 				case 'l':
   1305 					x = 1;
   1306 					if (isdigit(*arg)) {
   1307 						x = strtol(arg, &ccp, 0);
   1308 						if (ccp == arg)
   1309 							break;
   1310 						arg = ccp;
   1311 					}
   1312 					sep->se_log &= ~MULOG_RFC931;
   1313 					sep->se_log |= x;
   1314 					break;
   1315 				case 'a':
   1316 					sep->se_log |= MULOG_RFC931;
   1317 					break;
   1318 				default:
   1319 					break;
   1320 				}
   1321 			}
   1322 			arg = colon + 1;
   1323 		}
   1324 #endif
   1325 		if (argc < MAXARGV)
   1326 			sep->se_argv[argc++] = newstr(arg);
   1327 	}
   1328 	while (argc <= MAXARGV)
   1329 		sep->se_argv[argc++] = NULL;
   1330 	return (sep);
   1331 }
   1332 
   1333 void
   1334 freeconfig(cp)
   1335 	struct servtab *cp;
   1336 {
   1337 	int i;
   1338 
   1339 	if (cp->se_hostaddr)
   1340 		free(cp->se_hostaddr);
   1341 	if (cp->se_service)
   1342 		free(cp->se_service);
   1343 	if (cp->se_proto)
   1344 		free(cp->se_proto);
   1345 	if (cp->se_user)
   1346 		free(cp->se_user);
   1347 	/* Note: se_group is part of the newstr'ed se_user */
   1348 	if (cp->se_server)
   1349 		free(cp->se_server);
   1350 	for (i = 0; i < MAXARGV; i++)
   1351 		if (cp->se_argv[i])
   1352 			free(cp->se_argv[i]);
   1353 }
   1354 
   1355 
   1356 /*
   1357  * Safe skip - if skip returns null, log a syntax error in the
   1358  * configuration file and exit.
   1359  */
   1360 char *
   1361 sskip(cpp)
   1362 	char **cpp;
   1363 {
   1364 	char *cp;
   1365 
   1366 	cp = skip(cpp);
   1367 	if (cp == NULL) {
   1368 		syslog(LOG_ERR, "%s: syntax error", CONFIG);
   1369 		exit(-1);
   1370 	}
   1371 	return (cp);
   1372 }
   1373 
   1374 char *
   1375 skip(cpp)
   1376 	char **cpp;
   1377 {
   1378 	char *cp = *cpp;
   1379 	char *start;
   1380 
   1381 	if (*cpp == NULL)
   1382 		return ((char *)0);
   1383 
   1384 again:
   1385 	while (*cp == ' ' || *cp == '\t')
   1386 		cp++;
   1387 	if (*cp == '\0') {
   1388 		int c;
   1389 
   1390 		c = getc(fconfig);
   1391 		(void) ungetc(c, fconfig);
   1392 		if (c == ' ' || c == '\t')
   1393 			if (cp = nextline(fconfig))
   1394 				goto again;
   1395 		*cpp = (char *)0;
   1396 		return ((char *)0);
   1397 	}
   1398 	start = cp;
   1399 	while (*cp && *cp != ' ' && *cp != '\t')
   1400 		cp++;
   1401 	if (*cp != '\0')
   1402 		*cp++ = '\0';
   1403 	*cpp = cp;
   1404 	return (start);
   1405 }
   1406 
   1407 char *
   1408 nextline(fd)
   1409 	FILE *fd;
   1410 {
   1411 	char *cp;
   1412 
   1413 	if (fgets(line, sizeof (line), fd) == NULL)
   1414 		return ((char *)0);
   1415 	cp = strchr(line, '\n');
   1416 	if (cp)
   1417 		*cp = '\0';
   1418 	return (line);
   1419 }
   1420 
   1421 char *
   1422 newstr(cp)
   1423 	char *cp;
   1424 {
   1425 	if (cp = strdup(cp ? cp : ""))
   1426 		return (cp);
   1427 	syslog(LOG_ERR, "strdup: %m");
   1428 	exit(-1);
   1429 }
   1430 
   1431 void
   1432 inetd_setproctitle(a, s)
   1433 	char *a;
   1434 	int s;
   1435 {
   1436 	int size;
   1437 	char *cp;
   1438 	struct sockaddr_in sin;
   1439 	char buf[80];
   1440 
   1441 	cp = Argv[0];
   1442 	size = sizeof(sin);
   1443 	if (getpeername(s, (struct sockaddr *)&sin, &size) == 0)
   1444 		(void)snprintf(buf, sizeof buf, "-%s [%s]", a,
   1445 		    inet_ntoa(sin.sin_addr));
   1446 	else
   1447 		(void)snprintf(buf, sizeof buf, "-%s", a);
   1448 	strncpy(cp, buf, LastArg - cp);
   1449 	cp += strlen(cp);
   1450 	while (cp < LastArg)
   1451 		*cp++ = ' ';
   1452 }
   1453 
   1454 logpid()
   1455 {
   1456 	FILE *fp;
   1457 
   1458 	if ((fp = fopen(_PATH_INETDPID, "w")) != NULL) {
   1459 		fprintf(fp, "%u\n", getpid());
   1460 		(void)fclose(fp);
   1461 	}
   1462 }
   1463 
   1464 bump_nofile()
   1465 {
   1466 #ifdef RLIMIT_NOFILE
   1467 
   1468 #define FD_CHUNK	32
   1469 
   1470 	struct rlimit rl;
   1471 
   1472 	if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
   1473 		syslog(LOG_ERR, "getrlimit: %m");
   1474 		return -1;
   1475 	}
   1476 	rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK);
   1477 	if (rl.rlim_cur <= rlim_ofile_cur) {
   1478 		syslog(LOG_ERR,
   1479 		    "bump_nofile: cannot extend file limit, max = %d",
   1480 		    rl.rlim_cur);
   1481 		return -1;
   1482 	}
   1483 
   1484 	if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
   1485 		syslog(LOG_ERR, "setrlimit: %m");
   1486 		return -1;
   1487 	}
   1488 
   1489 	rlim_ofile_cur = rl.rlim_cur;
   1490 	return 0;
   1491 
   1492 #else
   1493 	syslog(LOG_ERR, "bump_nofile: cannot extend file limit");
   1494 	return -1;
   1495 #endif
   1496 }
   1497 
   1498 /*
   1499  * Internet services provided internally by inetd:
   1500  */
   1501 #define	BUFSIZE	4096
   1502 
   1503 /* ARGSUSED */
   1504 void
   1505 echo_stream(s, sep)		/* Echo service -- echo data back */
   1506 	int s;
   1507 	struct servtab *sep;
   1508 {
   1509 	char buffer[BUFSIZE];
   1510 	int i;
   1511 
   1512 	inetd_setproctitle(sep->se_service, s);
   1513 	while ((i = read(s, buffer, sizeof(buffer))) > 0 &&
   1514 	    write(s, buffer, i) > 0)
   1515 		;
   1516 	exit(0);
   1517 }
   1518 
   1519 /* ARGSUSED */
   1520 void
   1521 echo_dg(s, sep)			/* Echo service -- echo data back */
   1522 	int s;
   1523 	struct servtab *sep;
   1524 {
   1525 	char buffer[BUFSIZE];
   1526 	int i, size;
   1527 	struct sockaddr sa;
   1528 
   1529 	size = sizeof(sa);
   1530 	if ((i = recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size)) < 0)
   1531 		return;
   1532 	(void) sendto(s, buffer, i, 0, &sa, sizeof(sa));
   1533 }
   1534 
   1535 /* ARGSUSED */
   1536 void
   1537 discard_stream(s, sep)		/* Discard service -- ignore data */
   1538 	int s;
   1539 	struct servtab *sep;
   1540 {
   1541 	char buffer[BUFSIZE];
   1542 
   1543 	inetd_setproctitle(sep->se_service, s);
   1544 	while ((errno = 0, read(s, buffer, sizeof(buffer)) > 0) ||
   1545 			errno == EINTR)
   1546 		;
   1547 	exit(0);
   1548 }
   1549 
   1550 /* ARGSUSED */
   1551 void
   1552 discard_dg(s, sep)		/* Discard service -- ignore data */
   1553 	int s;
   1554 	struct servtab *sep;
   1555 {
   1556 	char buffer[BUFSIZE];
   1557 
   1558 	(void) read(s, buffer, sizeof(buffer));
   1559 }
   1560 
   1561 #include <ctype.h>
   1562 #define LINESIZ 72
   1563 char ring[128];
   1564 char *endring;
   1565 
   1566 void
   1567 initring()
   1568 {
   1569 	int i;
   1570 
   1571 	endring = ring;
   1572 
   1573 	for (i = 0; i <= 128; ++i)
   1574 		if (isprint(i))
   1575 			*endring++ = i;
   1576 }
   1577 
   1578 /* ARGSUSED */
   1579 void
   1580 chargen_stream(s, sep)		/* Character generator */
   1581 	int s;
   1582 	struct servtab *sep;
   1583 {
   1584 	int len;
   1585 	char *rs, text[LINESIZ+2];
   1586 
   1587 	inetd_setproctitle(sep->se_service, s);
   1588 
   1589 	if (!endring) {
   1590 		initring();
   1591 		rs = ring;
   1592 	}
   1593 
   1594 	text[LINESIZ] = '\r';
   1595 	text[LINESIZ + 1] = '\n';
   1596 	for (rs = ring;;) {
   1597 		if ((len = endring - rs) >= LINESIZ)
   1598 			memmove(text, rs, LINESIZ);
   1599 		else {
   1600 			memmove(text, rs, len);
   1601 			memmove(text + len, ring, LINESIZ - len);
   1602 		}
   1603 		if (++rs == endring)
   1604 			rs = ring;
   1605 		if (write(s, text, sizeof(text)) != sizeof(text))
   1606 			break;
   1607 	}
   1608 	exit(0);
   1609 }
   1610 
   1611 /* ARGSUSED */
   1612 void
   1613 chargen_dg(s, sep)		/* Character generator */
   1614 	int s;
   1615 	struct servtab *sep;
   1616 {
   1617 	struct sockaddr sa;
   1618 	static char *rs;
   1619 	int len, size;
   1620 	char text[LINESIZ+2];
   1621 
   1622 	if (endring == 0) {
   1623 		initring();
   1624 		rs = ring;
   1625 	}
   1626 
   1627 	size = sizeof(sa);
   1628 	if (recvfrom(s, text, sizeof(text), 0, &sa, &size) < 0)
   1629 		return;
   1630 
   1631 	if ((len = endring - rs) >= LINESIZ)
   1632 		memmove(text, rs, LINESIZ);
   1633 	else {
   1634 		memmove(text, rs, len);
   1635 		memmove(text + len, ring, LINESIZ - len);
   1636 	}
   1637 	if (++rs == endring)
   1638 		rs = ring;
   1639 	text[LINESIZ] = '\r';
   1640 	text[LINESIZ + 1] = '\n';
   1641 	(void) sendto(s, text, sizeof(text), 0, &sa, sizeof(sa));
   1642 }
   1643 
   1644 /*
   1645  * Return a machine readable date and time, in the form of the
   1646  * number of seconds since midnight, Jan 1, 1900.  Since gettimeofday
   1647  * returns the number of seconds since midnight, Jan 1, 1970,
   1648  * we must add 2208988800 seconds to this figure to make up for
   1649  * some seventy years Bell Labs was asleep.
   1650  */
   1651 
   1652 long
   1653 machtime()
   1654 {
   1655 	struct timeval tv;
   1656 
   1657 	if (gettimeofday(&tv, (struct timezone *)0) < 0) {
   1658 		if (debug)
   1659 			fprintf(stderr, "Unable to get time of day\n");
   1660 		return (0L);
   1661 	}
   1662 #define	OFFSET ((u_long)25567 * 24*60*60)
   1663 	return (htonl((long)(tv.tv_sec + OFFSET)));
   1664 #undef OFFSET
   1665 }
   1666 
   1667 /* ARGSUSED */
   1668 void
   1669 machtime_stream(s, sep)
   1670 	int s;
   1671 	struct servtab *sep;
   1672 {
   1673 	long result;
   1674 
   1675 	result = machtime();
   1676 	(void) write(s, (char *) &result, sizeof(result));
   1677 }
   1678 
   1679 /* ARGSUSED */
   1680 void
   1681 machtime_dg(s, sep)
   1682 	int s;
   1683 	struct servtab *sep;
   1684 {
   1685 	long result;
   1686 	struct sockaddr sa;
   1687 	int size;
   1688 
   1689 	size = sizeof(sa);
   1690 	if (recvfrom(s, (char *)&result, sizeof(result), 0, &sa, &size) < 0)
   1691 		return;
   1692 	result = machtime();
   1693 	(void) sendto(s, (char *) &result, sizeof(result), 0, &sa, sizeof(sa));
   1694 }
   1695 
   1696 /* ARGSUSED */
   1697 void
   1698 daytime_stream(s, sep)		/* Return human-readable time of day */
   1699 	int s;
   1700 	struct servtab *sep;
   1701 {
   1702 	char buffer[256];
   1703 	time_t clock;
   1704 	int len;
   1705 
   1706 	clock = time((time_t *) 0);
   1707 
   1708 	len = snprintf(buffer, sizeof buffer, "%.24s\r\n", ctime(&clock));
   1709 	(void) write(s, buffer, len);
   1710 }
   1711 
   1712 /* ARGSUSED */
   1713 void
   1714 daytime_dg(s, sep)		/* Return human-readable time of day */
   1715 	int s;
   1716 	struct servtab *sep;
   1717 {
   1718 	char buffer[256];
   1719 	time_t clock;
   1720 	struct sockaddr sa;
   1721 	int size, len;
   1722 
   1723 	clock = time((time_t *) 0);
   1724 
   1725 	size = sizeof(sa);
   1726 	if (recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size) < 0)
   1727 		return;
   1728 	len = snprintf(buffer, sizeof buffer, "%.24s\r\n", ctime(&clock));
   1729 	(void) sendto(s, buffer, len, 0, &sa, sizeof(sa));
   1730 }
   1731 
   1732 /*
   1733  * print_service:
   1734  *	Dump relevant information to stderr
   1735  */
   1736 void
   1737 print_service(action, sep)
   1738 	char *action;
   1739 	struct servtab *sep;
   1740 {
   1741 	if (isrpcservice(sep))
   1742 		fprintf(stderr,
   1743 		    "%s: %s rpcprog=%d, rpcvers = %d/%d, proto=%s, wait.max=%d.%d, user.group=%s.%s builtin=%lx server=%s\n",
   1744 		    action, sep->se_service,
   1745 		    sep->se_rpcprog, sep->se_rpcversh, sep->se_rpcversl, sep->se_proto,
   1746 		    sep->se_wait, sep->se_max, sep->se_user, sep->se_group,
   1747 		    (long)sep->se_bi, sep->se_server);
   1748 	else
   1749 		fprintf(stderr,
   1750 		    "%s: %s proto=%s, wait.max=%d.%d, user.group=%s.%s builtin=%lx server=%s\n",
   1751 		    action, sep->se_service, sep->se_proto,
   1752 		    sep->se_wait, sep->se_max, sep->se_user, sep->se_group,
   1753 		    (long)sep->se_bi, sep->se_server);
   1754 }
   1755 
   1756 void
   1757 usage()
   1758 {
   1759 
   1760 #ifdef LIBWRAP
   1761 	(void)fprintf(stderr, "usage: %s [-dl] [conf]\n", __progname);
   1762 #else
   1763 	(void)fprintf(stderr, "usage: %s [-d] [conf]\n", __progname);
   1764 #endif
   1765 	exit(1);
   1766 }
   1767 
   1768 
   1769 /*
   1770  *  Based on TCPMUX.C by Mark K. Lottor November 1988
   1771  *  sri-nic::ps:<mkl>tcpmux.c
   1772  */
   1773 
   1774 static int		/* # of characters upto \r,\n or \0 */
   1775 getline(fd, buf, len)
   1776 	int fd;
   1777 	char *buf;
   1778 	int len;
   1779 {
   1780 	int count = 0, n;
   1781 
   1782 	do {
   1783 		n = read(fd, buf, len-count);
   1784 		if (n == 0)
   1785 			return (count);
   1786 		if (n < 0)
   1787 			return (-1);
   1788 		while (--n >= 0) {
   1789 			if (*buf == '\r' || *buf == '\n' || *buf == '\0')
   1790 				return (count);
   1791 			count++;
   1792 			buf++;
   1793 		}
   1794 	} while (count < len);
   1795 	return (count);
   1796 }
   1797 
   1798 #define MAX_SERV_LEN	(256+2)		/* 2 bytes for \r\n */
   1799 
   1800 #define strwrite(fd, buf)	(void) write(fd, buf, sizeof(buf)-1)
   1801 
   1802 struct servtab *
   1803 tcpmux(s)
   1804 	int s;
   1805 {
   1806 	struct servtab *sep;
   1807 	char service[MAX_SERV_LEN+1];
   1808 	int len;
   1809 
   1810 	/* Get requested service name */
   1811 	if ((len = getline(s, service, MAX_SERV_LEN)) < 0) {
   1812 		strwrite(s, "-Error reading service name\r\n");
   1813 		return (NULL);
   1814 	}
   1815 	service[len] = '\0';
   1816 
   1817 	if (debug)
   1818 		fprintf(stderr, "tcpmux: someone wants %s\n", service);
   1819 
   1820 	/*
   1821 	 * Help is a required command, and lists available services,
   1822 	 * one per line.
   1823 	 */
   1824 	if (!strcasecmp(service, "help")) {
   1825 		for (sep = servtab; sep; sep = sep->se_next) {
   1826 			if (!ISMUX(sep))
   1827 				continue;
   1828 			(void)write(s,sep->se_service,strlen(sep->se_service));
   1829 			strwrite(s, "\r\n");
   1830 		}
   1831 		return (NULL);
   1832 	}
   1833 
   1834 	/* Try matching a service in inetd.conf with the request */
   1835 	for (sep = servtab; sep; sep = sep->se_next) {
   1836 		if (!ISMUX(sep))
   1837 			continue;
   1838 		if (!strcasecmp(service, sep->se_service)) {
   1839 			if (ISMUXPLUS(sep)) {
   1840 				strwrite(s, "+Go\r\n");
   1841 			}
   1842 			return (sep);
   1843 		}
   1844 	}
   1845 	strwrite(s, "-Service not available\r\n");
   1846 	return (NULL);
   1847 }
   1848 
   1849 
   1850 #ifdef MULOG
   1851 dolog(sep, ctrl)
   1852 	struct servtab *sep;
   1853 	int		ctrl;
   1854 {
   1855 	struct sockaddr		sa;
   1856 	struct sockaddr_in	*sin = (struct sockaddr_in *)&sa;
   1857 	int			len = sizeof(sa);
   1858 	struct hostent		*hp;
   1859 	char			*host, *dp, buf[BUFSIZ], *rfc931_name();
   1860 	int			connected = 1;
   1861 
   1862 	if (sep->se_family != AF_INET)
   1863 		return;
   1864 
   1865 	if (getpeername(ctrl, &sa, &len) < 0) {
   1866 		if (errno != ENOTCONN) {
   1867 			syslog(LOG_ERR, "getpeername: %m");
   1868 			return;
   1869 		}
   1870 		if (recvfrom(ctrl, buf, sizeof(buf), MSG_PEEK, &sa, &len) < 0) {
   1871 			syslog(LOG_ERR, "recvfrom: %m");
   1872 			return;
   1873 		}
   1874 		connected = 0;
   1875 	}
   1876 	if (sa.sa_family != AF_INET) {
   1877 		syslog(LOG_ERR, "unexpected address family %u", sa.sa_family);
   1878 		return;
   1879 	}
   1880 
   1881 	hp = gethostbyaddr((char *) &sin->sin_addr.s_addr,
   1882 				sizeof (sin->sin_addr.s_addr), AF_INET);
   1883 
   1884 	host = hp?hp->h_name:inet_ntoa(sin->sin_addr);
   1885 
   1886 	switch (sep->se_log & ~MULOG_RFC931) {
   1887 	case 0:
   1888 		return;
   1889 	case 1:
   1890 		if (curdom == NULL || *curdom == '\0')
   1891 			break;
   1892 		dp = host + strlen(host) - strlen(curdom);
   1893 		if (dp < host)
   1894 			break;
   1895 		if (debug)
   1896 			fprintf(stderr, "check \"%s\" against curdom \"%s\"\n",
   1897 			    host, curdom);
   1898 		if (strcasecmp(dp, curdom) == 0)
   1899 			return;
   1900 		break;
   1901 	case 2:
   1902 	default:
   1903 		break;
   1904 	}
   1905 
   1906 	openlog("", LOG_NOWAIT, MULOG);
   1907 
   1908 	if (connected && (sep->se_log & MULOG_RFC931))
   1909 		syslog(LOG_INFO, "%s@%s wants %s",
   1910 		    rfc931_name(sin, ctrl), host, sep->se_service);
   1911 	else
   1912 		syslog(LOG_INFO, "%s wants %s",
   1913 		    host, sep->se_service);
   1914 }
   1915 
   1916 /*
   1917  * From tcp_log by
   1918  *  Wietse Venema, Eindhoven University of Technology, The Netherlands.
   1919  */
   1920 #if 0
   1921 static char sccsid[] = "@(#) rfc931.c 1.3 92/08/31 22:54:46";
   1922 #endif
   1923 
   1924 #include <setjmp.h>
   1925 
   1926 #define	RFC931_PORT	113		/* Semi-well-known port */
   1927 #define	TIMEOUT		4
   1928 #define	TIMEOUT2	10
   1929 
   1930 static jmp_buf timebuf;
   1931 
   1932 /* timeout - handle timeouts */
   1933 
   1934 static void timeout(sig)
   1935 int     sig;
   1936 {
   1937 	longjmp(timebuf, sig);
   1938 }
   1939 
   1940 /* rfc931_name - return remote user name */
   1941 
   1942 char *
   1943 rfc931_name(there, ctrl)
   1944 struct sockaddr_in *there;		/* remote link information */
   1945 int	ctrl;
   1946 {
   1947 	struct sockaddr_in here;	/* local link information */
   1948 	struct sockaddr_in sin;		/* for talking to RFC931 daemon */
   1949 	int		length;
   1950 	int		s;
   1951 	unsigned	remote;
   1952 	unsigned	local;
   1953 	static char	user[256];		/* XXX */
   1954 	char		buf[256];
   1955 	char		*cp;
   1956 	char		*result = "USER_UNKNOWN";
   1957 	int		len;
   1958 
   1959 	/* Find out local port number of our stdin. */
   1960 
   1961 	length = sizeof(here);
   1962 	if (getsockname(ctrl, (struct sockaddr *) &here, &length) == -1) {
   1963 		syslog(LOG_ERR, "getsockname: %m");
   1964 		return (result);
   1965 	}
   1966 	/* Set up timer so we won't get stuck. */
   1967 
   1968 	if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
   1969 		syslog(LOG_ERR, "socket: %m");
   1970 		return (result);
   1971 	}
   1972 
   1973 	sin = here;
   1974 	sin.sin_port = htons(0);
   1975 	if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
   1976 		syslog(LOG_ERR, "bind: %m");
   1977 		return (result);
   1978 	}
   1979 
   1980 	signal(SIGALRM, timeout);
   1981 	if (setjmp(timebuf)) {
   1982 		close(s);			/* not: fclose(fp) */
   1983 		return (result);
   1984 	}
   1985 	alarm(TIMEOUT);
   1986 
   1987 	/* Connect to the RFC931 daemon. */
   1988 
   1989 	sin = *there;
   1990 	sin.sin_port = htons(RFC931_PORT);
   1991 	if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
   1992 		close(s);
   1993 		alarm(0);
   1994 		return (result);
   1995 	}
   1996 
   1997 	/* Query the RFC 931 server. Would 13-byte writes ever be broken up? */
   1998 	(void)snprintf(buf, sizeof buf, "%u,%u\r\n", ntohs(there->sin_port),
   1999 	    ntohs(here.sin_port));
   2000 
   2001 
   2002 	for (len = 0, cp = buf; len < strlen(buf); ) {
   2003 		int	n;
   2004 
   2005 		if ((n = write(s, cp, strlen(buf) - len)) == -1) {
   2006 			close(s);
   2007 			alarm(0);
   2008 			return (result);
   2009 		}
   2010 		cp += n;
   2011 		len += n;
   2012 	}
   2013 
   2014 	/* Read response */
   2015 	for (cp = buf; cp < buf + sizeof(buf) - 1; ) {
   2016 		char	c;
   2017 		if (read(s, &c, 1) != 1) {
   2018 			close(s);
   2019 			alarm(0);
   2020 			return (result);
   2021 		}
   2022 		if (c == '\n')
   2023 			break;
   2024 		*cp++ = c;
   2025 	}
   2026 	*cp = '\0';
   2027 
   2028 	if (sscanf(buf, "%u , %u : USERID :%*[^:]:%255s", &remote, &local, user) == 3
   2029 		&& ntohs(there->sin_port) == remote
   2030 		&& ntohs(here.sin_port) == local) {
   2031 
   2032 		/* Strip trailing carriage return. */
   2033 		if (cp = strchr(user, '\r'))
   2034 			*cp = 0;
   2035 		result = user;
   2036 	}
   2037 
   2038 	alarm(0);
   2039 	close(s);
   2040 	return (result);
   2041 }
   2042 #endif
   2043