Radix cross Linux

The main Radix cross Linux repository contains the build scripts of packages, which have the most complete and common functionality for desktop machines

452 Commits   2 Branches   1 Tag
     5         kx /*	$Slackware: inetd.c 1.79s 2001/02/06 13:18:00 volkerdi Exp $	*/
     5         kx /*	$OpenBSD: inetd.c,v 1.79 2001/01/30 08:30:57 deraadt Exp $	*/
     5         kx /*	$NetBSD: inetd.c,v 1.11 1996/02/22 11:14:41 mycroft Exp $	*/
     5         kx /*
     5         kx  * Copyright (c) 1983,1991 The Regents of the University of California.
     5         kx  * All rights reserved.
     5         kx  *
     5         kx  * Redistribution and use in source and binary forms, with or without
     5         kx  * modification, are permitted provided that the following conditions
     5         kx  * are met:
     5         kx  * 1. Redistributions of source code must retain the above copyright
     5         kx  *    notice, this list of conditions and the following disclaimer.
     5         kx  * 2. Redistributions in binary form must reproduce the above copyright
     5         kx  *    notice, this list of conditions and the following disclaimer in the
     5         kx  *    documentation and/or other materials provided with the distribution.
     5         kx  * 3. All advertising materials mentioning features or use of this software
     5         kx  *    must display the following acknowledgement:
     5         kx  *	This product includes software developed by the University of
     5         kx  *	California, Berkeley and its contributors.
     5         kx  * 4. Neither the name of the University nor the names of its contributors
     5         kx  *    may be used to endorse or promote products derived from this software
     5         kx  *    without specific prior written permission.
     5         kx  *
     5         kx  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     5         kx  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     5         kx  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     5         kx  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     5         kx  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     5         kx  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     5         kx  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     5         kx  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     5         kx  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     5         kx  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     5         kx  * SUCH DAMAGE.
     5         kx  */
     5         kx 
     5         kx #ifndef lint
     5         kx char copyright[] =
     5         kx "@(#) Copyright (c) 1983 Regents of the University of California.\n\
     5         kx  All rights reserved.\n";
     5         kx #endif /* not lint */
     5         kx 
     5         kx #ifndef lint
     5         kx static char sccsid[] = "from: @(#)inetd.c	5.30 (Berkeley) 6/3/91";
     5         kx static char rcsid[] = "$OpenBSD: inetd.c,v 1.79 2001/01/30 08:30:57 deraadt Exp $";
     5         kx static char xtraid[] = "$Slackware: inetd.c 1.79s 2001/02/06 13:18:00 volkerdi Exp $";
     5         kx #endif /* not lint */
     5         kx 
     5         kx /*
     5         kx  * Inetd - Internet super-server
     5         kx  *
     5         kx  * This program invokes all internet services as needed.
     5         kx  * connection-oriented services are invoked each time a
     5         kx  * connection is made, by creating a process.  This process
     5         kx  * is passed the connection as file descriptor 0 and is
     5         kx  * expected to do a getpeername to find out the source host
     5         kx  * and port.
     5         kx  *
     5         kx  * Datagram oriented services are invoked when a datagram
     5         kx  * arrives; a process is created and passed a pending message
     5         kx  * on file descriptor 0.  Datagram servers may either connect
     5         kx  * to their peer, freeing up the original socket for inetd
     5         kx  * to receive further messages on, or ``take over the socket'',
     5         kx  * processing all arriving datagrams and, eventually, timing
     5         kx  * out.	 The first type of server is said to be ``multi-threaded'';
     5         kx  * the second type of server ``single-threaded''. 
     5         kx  *
     5         kx  * Inetd uses a configuration file which is read at startup
     5         kx  * and, possibly, at some later time in response to a hangup signal.
     5         kx  * The configuration file is ``free format'' with fields given in the
     5         kx  * order shown below.  Continuation lines for an entry must begin with
     5         kx  * a space or tab.  All fields must be present in each entry.
     5         kx  *
     5         kx  *	service name			must be in /etc/services
     5         kx  *	socket type			stream/dgram/raw/rdm/seqpacket
     5         kx  *	protocol			must be in /etc/protocols
     5         kx  *	wait/nowait[.max]		single-threaded/multi-threaded, max #
     5         kx  *	user[.group] or user[:group]	user/group to run daemon as
     5         kx  *	server program			full path name
     5         kx  *	server program arguments	maximum of MAXARGS (20)
     5         kx  *
     5         kx  * For RPC services
     5         kx  *      service name/version            must be in /etc/rpc
     5         kx  *	socket type			stream/dgram/raw/rdm/seqpacket
     5         kx  *	protocol			must be in /etc/protocols
     5         kx  *	wait/nowait[.max]		single-threaded/multi-threaded
     5         kx  *	user[.group] or user[:group]	user to run daemon as
     5         kx  *	server program			full path name
     5         kx  *	server program arguments	maximum of MAXARGS (20)
     5         kx  *
     5         kx  * For non-RPC services, the "service name" can be of the form
     5         kx  * hostaddress:servicename, in which case the hostaddress is used
     5         kx  * as the host portion of the address to listen on.  If hostaddress
     5         kx  * consists of a single `*' character, INADDR_ANY is used.
     5         kx  *
     5         kx  * A line can also consist of just
     5         kx  *      hostaddress:
     5         kx  * where hostaddress is as in the preceding paragraph.  Such a line must
     5         kx  * have no further fields; the specified hostaddress is remembered and
     5         kx  * used for all further lines that have no hostaddress specified,
     5         kx  * until the next such line (or EOF).  (This is why * is provided to
     5         kx  * allow explicit specification of INADDR_ANY.)  A line
     5         kx  *      *:
     5         kx  * is implicitly in effect at the beginning of the file.
     5         kx  *
     5         kx  * The hostaddress specifier may (and often will) contain dots;
     5         kx  * the service name must not.
     5         kx  *
     5         kx  * For RPC services, host-address specifiers are accepted and will
     5         kx  * work to some extent; however, because of limitations in the
     5         kx  * portmapper interface, it will not work to try to give more than
     5         kx  * one line for any given RPC service, even if the host-address
     5         kx  * specifiers are different.
     5         kx  *
     5         kx  * Comment lines are indicated by a `#' in column 1.
     5         kx  */
     5         kx 
     5         kx /*
     5         kx  * Here's the scoop concerning the user[.:]group feature:
     5         kx  *
     5         kx  * 1) set-group-option off.
     5         kx  * 
     5         kx  * 	a) user = root:	NO setuid() or setgid() is done
     5         kx  * 
     5         kx  * 	b) other:	setgid(primary group as found in passwd)
     5         kx  * 			initgroups(name, primary group)
     5         kx  * 			setuid()
     5         kx  *
     5         kx  * 2) set-group-option on.
     5         kx  * 
     5         kx  * 	a) user = root:	setgid(specified group)
     5         kx  * 			NO initgroups()
     5         kx  * 			NO setuid()
     5         kx  *
     5         kx  * 	b) other:	setgid(specified group)
     5         kx  * 			initgroups(name, specified group)
     5         kx  * 			setuid()
     5         kx  * 
     5         kx  */
     5         kx 
     5         kx #include <sys/param.h>
     5         kx #include <sys/stat.h>
     5         kx #include <sys/ioctl.h>
     5         kx #include <sys/socket.h>
     5         kx #include <sys/un.h>
     5         kx #include <sys/file.h>
     5         kx #include <sys/wait.h>
     5         kx #include <sys/time.h>
     5         kx #include <sys/resource.h>
     5         kx 
     5         kx #ifndef RLIMIT_NOFILE
     5         kx #define RLIMIT_NOFILE	RLIMIT_OFILE
     5         kx #endif
     5         kx 
     5         kx #include <netinet/in.h>
     5         kx #include <arpa/inet.h>
     5         kx 
     5         kx #include <errno.h>
     5         kx #include <signal.h>
     5         kx #include <netdb.h>
     5         kx #include <syslog.h>
     5         kx #include <pwd.h>
     5         kx #include <grp.h>
     5         kx #include <ctype.h>
     5         kx #include <stdio.h>
     5         kx #include <stdlib.h>
     5         kx #include <unistd.h>
     5         kx #include <string.h>
     5         kx #include <time.h>
     5         kx /* #include <login_cap.h> */
     5         kx #include <rpc/rpc.h>
     5         kx #include <rpc/pmap_clnt.h>
     5         kx #include <rpcsvc/nfs_prot.h>
     5         kx #include "pathnames.h"
     5         kx 
     5         kx #define	TOOMANY		0		/* don't start more than TOOMANY */
     5         kx 					/* zero disables this stupid "feature" */
     5         kx #define	CNT_INTVL	60		/* servers in CNT_INTVL sec. */
     5         kx #define	RETRYTIME	(60*10)		/* retry after bind or server fail */
     5         kx 
     5         kx #define	SIGBLOCK	(sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM))
     5         kx 
     5         kx void	config __P((int));
     5         kx /* void	doconfig __P((void)); */
     5         kx /* void	reap __P((int)); */
     5         kx /* void	doreap __P((void)); */
     5         kx void reapchild __P ((int));
     5         kx void	retry __P((int));
     5         kx /* void	doretry __P((void)); */
     5         kx void	goaway __P((int));
     5         kx 
     5         kx static int debug = 0;
     5         kx static int global_queuelen = 128;
     5         kx int	nsock, maxsock;
     5         kx /* fd_set	*allsockp; */
     5         kx fd_set	allsock;
     5         kx /* int	allsockn; */
     5         kx int	toomany = TOOMANY;
     5         kx int	options;
     5         kx int	timingout;
     5         kx struct	servent *sp;
     5         kx char	*curdom;
     5         kx uid_t	uid;
     5         kx 
     5         kx #ifndef OPEN_MAX
     5         kx #define OPEN_MAX	64
     5         kx #endif
     5         kx 
     5         kx /* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */
     5         kx #define FD_MARGIN	(8)
     5         kx typeof(((struct rlimit *)0)->rlim_cur)	rlim_ofile_cur = OPEN_MAX;
     5         kx 
     5         kx #ifdef RLIMIT_NOFILE
     5         kx struct rlimit	rlim_ofile;
     5         kx #endif
     5         kx 
     5         kx struct	servtab {
     5         kx 	char	*se_hostaddr;		/* host address to listen on */
     5         kx 	char	*se_service;		/* name of service */
     5         kx 	int	se_socktype;		/* type of socket to use */
     5         kx 	int	se_family;		/* address family */
     5         kx 	char	*se_proto;		/* protocol used */
     5         kx 	int	se_rpcprog;		/* rpc program number */
     5         kx 	int	se_rpcversl;		/* rpc program lowest version */
     5         kx 	int	se_rpcversh;		/* rpc program highest version */
     5         kx #define isrpcservice(sep)	((sep)->se_rpcversl != 0)
     5         kx 	pid_t	se_wait;		/* single threaded server */
     5         kx 	short	se_checked;		/* looked at during merge */
     5         kx 	char	*se_user;		/* user name to run as */
     5         kx 	char	*se_group;		/* group name to run as */
     5         kx 	struct	biltin *se_bi;		/* if built-in, description */
     5         kx 	char	*se_server;		/* server program */
     5         kx #define	MAXARGV 20
     5         kx 	char	*se_argv[MAXARGV+1];	/* program arguments */
     5         kx 	int	se_fd;			/* open descriptor */
     5         kx 	union {
     5         kx 		struct	sockaddr se_un_ctrladdr;
     5         kx 		struct	sockaddr_in se_un_ctrladdr_in;
     5         kx 		struct	sockaddr_in6 se_un_ctrladdr_in6;
     5         kx 		struct	sockaddr_un se_un_ctrladdr_un;
     5         kx 		/* struct	sockaddr_storage se_un_ctrladdr_storage; */
     5         kx 	} se_un;			/* bound address */
     5         kx #define se_ctrladdr	se_un.se_un_ctrladdr
     5         kx #define se_ctrladdr_in	se_un.se_un_ctrladdr_in
     5         kx #define se_ctrladdr_in6	se_un.se_un_ctrladdr_in6
     5         kx #define se_ctrladdr_un	se_un.se_un_ctrladdr_un
     5         kx /* #define se_ctrladdr_storage	se_un.se_un_ctrladdr_storage */
     5         kx 	int	se_ctrladdr_size;
     5         kx 	int	se_max;			/* max # of instances of this service */
     5         kx 	int	se_count;		/* number started since se_time */
     5         kx 	struct	timeval se_time;	/* start of se_count */
     5         kx 	struct	servtab *se_next;
     5         kx } *servtab;
     5         kx 
     5         kx void echo_stream __P((int, struct servtab *));
     5         kx void discard_stream __P((int, struct servtab *));
     5         kx void machtime_stream __P((int, struct servtab *));
     5         kx void daytime_stream __P((int, struct servtab *));
     5         kx void chargen_stream __P((int, struct servtab *));
     5         kx void echo_dg __P((int, struct servtab *));
     5         kx void discard_dg __P((int, struct servtab *));
     5         kx void machtime_dg __P((int, struct servtab *));
     5         kx void daytime_dg __P((int, struct servtab *));
     5         kx void chargen_dg __P((int, struct servtab *));
     5         kx 
     5         kx struct biltin {
     5         kx 	char	*bi_service;		/* internally provided service name */
     5         kx 	int	bi_socktype;		/* type of socket supported */
     5         kx 	short	bi_fork;		/* 1 if should fork before call */
     5         kx 	short	bi_wait;		/* 1 if should wait for child */
     5         kx 	void	(*bi_fn) __P((int, struct servtab *));
     5         kx } biltins[] = {
     5         kx 	/* Echo received data */
     5         kx 	{ "echo",	SOCK_STREAM,	1, 0,	echo_stream },
     5         kx 	{ "echo",	SOCK_DGRAM,	0, 0,	echo_dg },
     5         kx 
     5         kx 	/* Internet /dev/null */
     5         kx 	{ "discard",	SOCK_STREAM,	1, 0,	discard_stream },
     5         kx 	{ "discard",	SOCK_DGRAM,	0, 0,	discard_dg },
     5         kx 
     5         kx 	/* Return 32 bit time since 1900 */
     5         kx 	{ "time",	SOCK_STREAM,	0, 0,	machtime_stream },
     5         kx 	{ "time",	SOCK_DGRAM,	0, 0,	machtime_dg },
     5         kx 
     5         kx 	/* Return human-readable time */
     5         kx 	{ "daytime",	SOCK_STREAM,	0, 0,	daytime_stream },
     5         kx 	{ "daytime",	SOCK_DGRAM,	0, 0,	daytime_dg },
     5         kx 
     5         kx 	/* Familiar character generator */
     5         kx 	{ "chargen",	SOCK_STREAM,	1, 0,	chargen_stream },
     5         kx 	{ "chargen",	SOCK_DGRAM,	0, 0,	chargen_dg },
     5         kx 
     5         kx 	{ NULL, 0, 0, 0, NULL }
     5         kx };
     5         kx 
     5         kx /* sig_atomic_t wantretry; */
     5         kx /* sig_atomic_t wantconfig; */
     5         kx /* sig_atomic_t wantreap; */
     5         kx 
     5         kx #define NUMINT	(sizeof(intab) / sizeof(struct inent))
     5         kx char	*CONFIG = _PATH_INETDCONF;
     5         kx char	**Argv;
     5         kx char 	*LastArg;
     5         kx char	*progname;
     5         kx 
     5         kx void logpid __P((void));
     5         kx 
     5         kx /* This function is unused in the Linux port */
     5         kx #ifdef OpenBSD
     5         kx void
     5         kx fd_grow(fd_set **fdsp, int *bytes, int fd)
     5         kx {
     5         kx 	caddr_t new;
     5         kx 	int newbytes;
     5         kx 
     5         kx 	newbytes = howmany(fd+1, NFDBITS) * sizeof(fd_mask);
     5         kx 	if (newbytes > *bytes) {
     5         kx 		newbytes *= 2;			/* optimism */
     5         kx 		new = realloc(*fdsp, newbytes);
     5         kx 		if (new == NULL) {
     5         kx 			syslog(LOG_ERR, "Out of memory.");
     5         kx 			exit(1);
     5         kx 		}
     5         kx 		memset(new + *bytes, 0, newbytes - *bytes);
     5         kx 		*fdsp = (fd_set *)new;
     5         kx 		*bytes = newbytes;
     5         kx 	}
     5         kx }
     5         kx #endif
     5         kx 
     5         kx int
     5         kx main(argc, argv, envp)
     5         kx 	int argc;
     5         kx 	char *argv[], *envp[];
     5         kx {
     5         kx 	/* extern char *optarg; */
     5         kx 	/* extern int optind; */
     5         kx 	register struct servtab *sep;
     5         kx 	register struct passwd *pwd;
     5         kx 	register struct group *grp = NULL;
     5         kx 	register int tmpint;
     5         kx 	struct sigaction sa, sapipe;
     5         kx 	int ch, dofork;
     5         kx 	int nodaemon = 0;
     5         kx 	pid_t pid;
     5         kx 	char buf[50];
     5         kx 	/* fd_set *readablep = NULL; */
     5         kx 	/* int readablen = 0; */
     5         kx 
     5         kx 	Argv = argv;
     5         kx 	if (envp == 0 || *envp == 0)
     5         kx 		envp = argv;
     5         kx 	while (*envp)
     5         kx 		envp++;
     5         kx 	LastArg = envp[-1] + strlen(envp[-1]);
     5         kx 
     5         kx 	progname = strrchr(argv[0], '/');
     5         kx 	progname = progname ? progname + 1 : argv[0];
     5         kx 
     5         kx 	while ((ch = getopt(argc, argv, "dR:")) != -1)
     5         kx 		switch(ch) {
     5         kx 		case 'd':
     5         kx 			debug = 1;
     5         kx 			options |= SO_DEBUG;
     5         kx 			break;
     5         kx 		case 'R': {	/* invocation rate */
     5         kx 			char *p;
     5         kx 			int val;
     5         kx 
     5         kx 			val = strtoul(optarg, &p, 0);
     5         kx 			if (val >= 0 && *p == (char) 0) {
     5         kx 				toomany = val;
     5         kx 				break;
     5         kx 			}
     5         kx 			syslog(LOG_ERR,
     5         kx 			    "-R %s: bad value for service invocation rate",
     5         kx 			    optarg);
     5         kx 			break;
     5         kx 		}
     5         kx 		case '?':
     5         kx 		default:
     5         kx 			fprintf(stderr, "usage: %s [-R rate] [-d] [conf]\n",
     5         kx 			    progname);
     5         kx 			exit(1);
     5         kx 		}
     5         kx 	argc -= optind;
     5         kx 	argv += optind;
     5         kx 
     5         kx 	uid = getuid();
     5         kx 	if (uid != 0)
     5         kx 		CONFIG = NULL;
     5         kx 	if (argc > 0)
     5         kx 		CONFIG = argv[0];
     5         kx 	if (CONFIG == NULL) {
     5         kx 		fprintf(stderr, "%s: non-root must specify a config file\n",
     5         kx 		    progname);
     5         kx 		exit(1);
     5         kx 	}
     5         kx 
     5         kx 	if (nodaemon == 0)
     5         kx 		daemon (0, 0);
     5         kx 	else if (debug == 0) {
     5         kx 		/* daemon(0, 0); */
     5         kx 		/* if (uid == 0) */
     5         kx 			/* (void) setlogin(""); */
     5         kx 		setsid ();
     5         kx 	}
     5         kx 
     5         kx 	if (uid == 0) {
     5         kx 		gid_t gid = getgid();
     5         kx 
     5         kx 		/* If run by hand, ensure groups vector gets trashed */
     5         kx 		setgroups(1, &gid);
     5         kx 	}
     5         kx 
     5         kx 	openlog(progname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
     5         kx 	logpid();
     5         kx 
     5         kx #ifdef RLIMIT_NOFILE
     5         kx 	if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
     5         kx 		syslog(LOG_ERR, "getrlimit: %m");
     5         kx 	} else {
     5         kx 		rlim_ofile_cur = rlim_ofile.rlim_cur;
     5         kx 		if (rlim_ofile_cur == RLIM_INFINITY)	/* ! */
     5         kx 			rlim_ofile_cur = OPEN_MAX;
     5         kx 	}
     5         kx #endif
     5         kx 
     5         kx 	memset((char *)&sa, 0, sizeof(sa));
     5         kx 	sigemptyset(&sa.sa_mask);	
     5         kx 	sigaddset(&sa.sa_mask, SIGALRM);
     5         kx 	sigaddset(&sa.sa_mask, SIGCHLD);
     5         kx 	sigaddset(&sa.sa_mask, SIGHUP);
     5         kx 	sa.sa_handler = retry;
     5         kx 	sigaction(SIGALRM, &sa, NULL);
     5         kx 	/* doconfig(); */
     5         kx 	config (SIGHUP);
     5         kx 	sa.sa_handler = config;
     5         kx 	sigaction(SIGHUP, &sa, NULL);
     5         kx 	/* sa.sa_handler = reap; */
     5         kx 	sa.sa_handler = reapchild;
     5         kx 	sigaction(SIGCHLD, &sa, NULL);
     5         kx 	sa.sa_handler = goaway;
     5         kx 	sigaction(SIGTERM, &sa, NULL);
     5         kx 	sa.sa_handler = goaway;
     5         kx 	sigaction(SIGINT, &sa, NULL);
     5         kx 	sa.sa_handler = SIG_IGN;
     5         kx 	sigaction(SIGPIPE, &sa, &sapipe);
     5         kx 
     5         kx 	{
     5         kx 		/* space for daemons to overwrite environment for ps */
     5         kx #define	DUMMYSIZE	100
     5         kx 		char dummy[DUMMYSIZE];
     5         kx 
     5         kx 		(void)memset(dummy, 'x', DUMMYSIZE - 1);
     5         kx 		dummy[DUMMYSIZE - 1] = '\0';
     5         kx 
     5         kx 		(void)setenv("inetd_dummy", dummy, 1);
     5         kx 	}
     5         kx 
     5         kx 	for (;;) {
     5         kx 	    int n, ctrl = -1;
     5         kx 	    fd_set readable;
     5         kx 
     5         kx 	    if (nsock == 0) {
     5         kx 		(void) sigblock(SIGBLOCK);
     5         kx 		while (nsock == 0)
     5         kx 		    sigpause(0L);
     5         kx 		(void) sigsetmask(0L);
     5         kx 	    }
     5         kx 
     5         kx 	    /* Well, this all looks like cruft to me ;^) */
     5         kx #ifdef OpenBSD
     5         kx 	    if (readablen != allsockn) {
     5         kx 		if (readablep)
     5         kx 		    free(readablep);
     5         kx 		readablep = (fd_set *)calloc(allsockn, 1);
     5         kx 		if (readablep == NULL) {
     5         kx 		    syslog(LOG_ERR, "Out of memory.");
     5         kx 		    exit(1);
     5         kx 		}
     5         kx 		readablen = allsockn;
     5         kx 	    }
     5         kx 	    bcopy(allsockp, readablep, allsockn);
     5         kx 
     5         kx 	    if (wantretry) {
     5         kx 		doretry();
     5         kx 		wantretry = 0;
     5         kx 		continue;
     5         kx 	    }
     5         kx 	    if (wantconfig) {
     5         kx 		doconfig();
     5         kx 		wantconfig = 0;
     5         kx 		continue;
     5         kx 	    }
     5         kx 	    if (wantreap) {
     5         kx 		doreap();
     5         kx 		wantreap = 0;
     5         kx 		continue;
     5         kx 	    }
     5         kx #endif
     5         kx 
     5         kx 	    readable = allsock;
     5         kx 	    if ((n = select(maxsock + 1, &readable, NULL, NULL, NULL)) <= 0) {
     5         kx 		    if (n < 0 && errno != EINTR) {
     5         kx 			syslog(LOG_WARNING, "select: %m");
     5         kx 			sleep(1);
     5         kx 		    }
     5         kx 		    continue;
     5         kx 	    }
     5         kx 	    for (sep = servtab; n && sep; sep = sep->se_next)
     5         kx 	    if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
     5         kx 		n--;
     5         kx 		if (debug)
     5         kx 			fprintf(stderr, "someone wants %s\n", sep->se_service);
     5         kx 		if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
     5         kx 			ctrl = accept(sep->se_fd, NULL, NULL);
     5         kx 			if (debug)
     5         kx 				fprintf(stderr, "accept, ctrl %d\n", ctrl);
     5         kx 			if (ctrl < 0) {
     5         kx 				if (errno == EINTR)
     5         kx 					continue;
     5         kx 				syslog(LOG_WARNING, "accept (for %s): %m",
     5         kx 				    sep->se_service);
     5         kx 				continue;
     5         kx 			}
     5         kx 			if (sep->se_family == AF_INET &&
     5         kx 			    sep->se_socktype == SOCK_STREAM) {
     5         kx 				struct sockaddr_in peer;
     5         kx 				int plen = sizeof(peer);
     5         kx 
     5         kx 				if (getpeername(ctrl, (struct sockaddr *)&peer,
     5         kx 				    &plen) < 0) {
     5         kx 					syslog(LOG_WARNING, "could not getpeername");
     5         kx 					close(ctrl);
     5         kx 					continue;
     5         kx 				}
     5         kx 				if (ntohs(peer.sin_port) == 20) {
     5         kx 					/* XXX ftp bounce */
     5         kx 					close(ctrl);
     5         kx 					continue;
     5         kx 				}
     5         kx 			}
     5         kx 		} else
     5         kx 			ctrl = sep->se_fd;
     5         kx 		(void) sigblock(SIGBLOCK);
     5         kx 		pid = 0;
     5         kx 		dofork = (sep->se_bi == 0 || sep->se_bi->bi_fork);
     5         kx 		if (dofork) {
     5         kx 			if (sep->se_count++ == 0)
     5         kx 			    (void)gettimeofday(&sep->se_time, NULL);
     5         kx 			else if (toomany > 0 && sep->se_count >= sep->se_max) {
     5         kx 				struct timeval now;
     5         kx 
     5         kx 				(void)gettimeofday(&now, NULL);
     5         kx 				if (now.tv_sec - sep->se_time.tv_sec >
     5         kx 				    CNT_INTVL) {
     5         kx 					sep->se_time = now;
     5         kx 					sep->se_count = 1;
     5         kx 				} else {
     5         kx 					if (!sep->se_wait &&
     5         kx 					    sep->se_socktype == SOCK_STREAM)
     5         kx 						close(ctrl);
     5         kx 					if (sep->se_family == AF_INET &&
     5         kx 					    ntohs(sep->se_ctrladdr_in.sin_port) >=
     5         kx 					    IPPORT_RESERVED) {
     5         kx 						/*
     5         kx 						 * Cannot close it -- there are
     5         kx 						 * thieves on the system.
     5         kx 						 * Simply ignore the connection.
     5         kx 						 */
     5         kx 						--sep->se_count;
     5         kx 						continue;
     5         kx 					}
     5         kx 					syslog(LOG_ERR,
     5         kx 			"%s/%s server failing (looping), service terminated",
     5         kx 					    sep->se_service, sep->se_proto);
     5         kx 					if (!sep->se_wait &&
     5         kx 					    sep->se_socktype == SOCK_STREAM)
     5         kx 						close(ctrl);
     5         kx 					FD_CLR(sep->se_fd, &allsock);
     5         kx 					(void) close(sep->se_fd);
     5         kx 					sep->se_fd = -1;
     5         kx 					sep->se_count = 0;
     5         kx 					nsock--;
     5         kx 					sigsetmask(0L);
     5         kx 					if (!timingout) {
     5         kx 						timingout = 1;
     5         kx 						alarm(RETRYTIME);
     5         kx 					}
     5         kx 					continue;
     5         kx 				}
     5         kx 			}
     5         kx 			pid = fork();
     5         kx 		}
     5         kx 		if (pid < 0) {
     5         kx 			syslog(LOG_ERR, "fork: %m");
     5         kx 			if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
     5         kx 				close(ctrl);
     5         kx 			sigsetmask(0L);
     5         kx 			sleep(1);
     5         kx 			continue;
     5         kx 		}
     5         kx 		if (pid && sep->se_wait) {
     5         kx 			sep->se_wait = pid;
     5         kx 			FD_CLR(sep->se_fd, &allsock);
     5         kx 			nsock--;
     5         kx 		}
     5         kx 		sigsetmask(0L);
     5         kx 		if (pid == 0) {
     5         kx 			if (sep->se_bi)
     5         kx 				(*sep->se_bi->bi_fn)(ctrl, sep);
     5         kx 			else {
     5         kx 				if ((pwd = getpwnam(sep->se_user)) == NULL) {
     5         kx 					syslog(LOG_ERR,
     5         kx 					    "getpwnam: %s: No such user",
     5         kx 					    sep->se_user);
     5         kx 					if (sep->se_socktype != SOCK_STREAM)
     5         kx 						recv(0, buf, sizeof (buf), 0);
     5         kx 					_exit(1);
     5         kx 				}
     5         kx 				if (setsid() <0)
     5         kx 					syslog(LOG_ERR, "%s: setsid: %m",
     5         kx 					    sep->se_service);
     5         kx 				if (sep->se_group &&
     5         kx 				    (grp = getgrnam(sep->se_group)) == NULL) {
     5         kx 					syslog(LOG_ERR,
     5         kx 					    "getgrnam: %s: No such group",
     5         kx 					    sep->se_group);
     5         kx 					if (sep->se_socktype != SOCK_STREAM)
     5         kx 						recv(0, buf, sizeof (buf), 0);
     5         kx 					_exit(1);
     5         kx 				}
     5         kx 				if (uid != 0) {
     5         kx 					/* a user running private inetd */
     5         kx 					if (uid != pwd->pw_uid)
     5         kx 						_exit(1);
     5         kx 				} else if (pwd->pw_uid) {
     5         kx 					/* tmpint = LOGIN_SETALL & */
     5         kx 					    /* ~(LOGIN_SETGROUP|LOGIN_SETLOGIN); */
     5         kx 					/* if (pwd->pw_uid) */
     5         kx 						/* tmpint |= LOGIN_SETGROUP|LOGIN_SETLOGIN; */
     5         kx 					if (sep->se_group) {
     5         kx 						pwd->pw_gid = grp->gr_gid;
     5         kx 						/* tmpint |= LOGIN_SETGROUP; */
     5         kx 					}
     5         kx 					setgid ((gid_t) pwd->pw_gid);
     5         kx 					initgroups (pwd->pw_name, pwd->pw_gid);
     5         kx 					setuid ((uid_t) pwd->pw_uid);
     5         kx 					/* if (setusercontext(0, pwd, pwd->pw_uid, */
     5         kx 					    /* tmpint) < 0) */
     5         kx 						/* syslog(LOG_ERR, */
     5         kx 						    /* "%s/%s: setusercontext: %m", */
     5         kx 						    /* sep->se_service, */
     5         kx 						    /* sep->se_proto); */
     5         kx 				} else if (sep->se_group) {
     5         kx 					setgid (grp->gr_gid);
     5         kx 					setgroups (1, &grp->gr_gid);
     5         kx 				}
     5         kx 				if (debug)
     5         kx 					fprintf(stderr, "%d execl %s\n",
     5         kx 					    getpid(), sep->se_server);
     5         kx 				dup2(ctrl, 0);
     5         kx 				close(ctrl);
     5         kx 				dup2(0, 1);
     5         kx 				dup2(0, 2);
     5         kx 				if (rlim_ofile.rlim_cur != rlim_ofile_cur)
     5         kx 					if (setrlimit (RLIMIT_NOFILE, &rlim_ofile) < 0)
     5         kx 						syslog (LOG_ERR, "setrlimit: %m");
     5         kx 				closelog();
     5         kx 				for (tmpint = rlim_ofile_cur-1; --tmpint > 2; )
     5         kx 					(void)close(tmpint);
     5         kx 				sigaction(SIGPIPE, &sapipe, NULL);
     5         kx 				execv(sep->se_server, sep->se_argv);
     5         kx 				if (sep->se_socktype != SOCK_STREAM)
     5         kx 					recv(0, buf, sizeof (buf), 0);
     5         kx 				syslog(LOG_ERR, "execv %s: %m", sep->se_server);
     5         kx 				_exit(1);
     5         kx 			}
     5         kx 		}
     5         kx 		if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
     5         kx 			close(ctrl);
     5         kx 	    }
     5         kx 	}
     5         kx }
     5         kx 
     5         kx int
     5         kx dg_badinput(sin)
     5         kx 	struct sockaddr_in *sin;
     5         kx {
     5         kx 	struct in_addr in;
     5         kx #ifdef INET6
     5         kx 	struct in6_addr *in6;
     5         kx #endif
     5         kx #ifdef OpenBSD
     5         kx 	u_int16_t port;
     5         kx 	int i;
     5         kx 
     5         kx 	switch (sa->sa_family) {
     5         kx 	case AF_INET:
     5         kx 		in.s_addr = ntohl(((struct sockaddr_in *)sa)->sin_addr.s_addr);
     5         kx 		port = ntohs(((struct sockaddr_in *)sa)->sin_port);
     5         kx 	v4chk:
     5         kx 		if (IN_MULTICAST(in.s_addr))
     5         kx 			goto bad;
     5         kx 		switch ((in.s_addr & 0xff000000) >> 24) {
     5         kx 		case 0: case 127: case 255:
     5         kx 			goto bad;
     5         kx 		}
     5         kx 		/* XXX check for subnet broadcast using getifaddrs(3) */
     5         kx 		break;
     5         kx #ifdef INET6
     5         kx 	case AF_INET6:
     5         kx 		in6 = &((struct sockaddr_in6 *)sa)->sin6_addr;
     5         kx 		port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
     5         kx 		if (IN6_IS_ADDR_MULTICAST(in6) || IN6_IS_ADDR_UNSPECIFIED(in6))
     5         kx 			goto bad;
     5         kx 		/*
     5         kx 		 * OpenBSD does not support IPv4 mapped adderss (RFC2553
     5         kx 		 * inbound behavior) at all.  We should drop it.
     5         kx 		 */
     5         kx 		if (IN6_IS_ADDR_V4MAPPED(in6))
     5         kx 			goto bad;
     5         kx 		if (IN6_IS_ADDR_V4COMPAT(in6)) {
     5         kx 			memcpy(&in, &in6->s6_addr[12], sizeof(in));
     5         kx 			in.s_addr = ntohl(in.s_addr);
     5         kx 			goto v4chk;
     5         kx 		}
     5         kx 		break;
     5         kx #endif
     5         kx 	default:
     5         kx 		/* XXX unsupported af, is it safe to assume it to be safe? */
     5         kx 		return 0;
     5         kx 	}
     5         kx 
     5         kx 	if (port < IPPORT_RESERVED || port == NFS_PORT)
     5         kx 		goto bad;
     5         kx #endif
     5         kx 
     5         kx 	if (ntohs (sin->sin_port) < IPPORT_RESERVED)
     5         kx 		return (1);
     5         kx 	if (sin->sin_addr.s_addr == htonl (INADDR_BROADCAST))
     5         kx 		return (1);
     5         kx 	/* XXX compare against broadcast addresses in SIOCGIFCONF list? */
     5         kx 	return (0);
     5         kx 
     5         kx #ifdef OpenBSD
     5         kx bad:
     5         kx 	return (1);
     5         kx #endif
     5         kx }
     5         kx 
     5         kx #ifdef OpenBSD
     5         kx void
     5         kx reap(int sig)
     5         kx {
     5         kx 	wantreap = 1;
     5         kx }
     5         kx #endif
     5         kx 
     5         kx void
     5         kx reapchild(int sig)
     5         kx {
     5         kx 	pid_t pid;
     5         kx 	int save_errno = errno, status;
     5         kx 	register struct servtab *sep;
     5         kx 
     5         kx #ifdef OpenBSD
     5         kx 	if (debug)
     5         kx 		fprintf(stderr, "reaping asked for\n");
     5         kx #endif
     5         kx 
     5         kx 	for (;;) {
     5         kx 		pid = wait3(&status, WNOHANG, NULL);
     5         kx 		if (pid <= 0)
     5         kx 			break;
     5         kx 		if (debug)
     5         kx 			fprintf(stderr, "%d reaped, status %x\n", pid, status);
     5         kx 		for (sep = servtab; sep; sep = sep->se_next)
     5         kx 			if (sep->se_wait == pid) {
     5         kx 				if (WIFEXITED(status) && WEXITSTATUS(status))
     5         kx 					syslog(LOG_WARNING,
     5         kx 					    "%s: exit status 0x%x",
     5         kx 					    sep->se_server, WEXITSTATUS(status));
     5         kx 				else if (WIFSIGNALED(status))
     5         kx 					syslog(LOG_WARNING,
     5         kx 					    "%s: exit signal 0x%x",
     5         kx 					    sep->se_server, WTERMSIG(status));
     5         kx 				sep->se_wait = 1;
     5         kx 				/* fd_grow(&allsockp, &allsockn, sep->se_fd); */
     5         kx 				FD_SET(sep->se_fd, &allsock);
     5         kx 				nsock++;
     5         kx 				if (debug)
     5         kx 					fprintf(stderr, "restored %s, fd %d\n",
     5         kx 					    sep->se_service, sep->se_fd);
     5         kx 			}
     5         kx 	}
     5         kx 	errno = save_errno;
     5         kx }
     5         kx 
     5         kx int setconfig __P((void));
     5         kx void endconfig __P((void));
     5         kx 
     5         kx void register_rpc __P((struct servtab *));
     5         kx void unregister_rpc __P((struct servtab *));
     5         kx void freeconfig __P((struct servtab *));
     5         kx void print_service __P((char *, struct servtab *));
     5         kx void setup __P((struct servtab *));
     5         kx struct servtab *getconfigent __P((void));
     5         kx struct servtab *enter __P((struct servtab *));
     5         kx int matchconf __P((struct servtab *, struct servtab *));
     5         kx 
     5         kx #ifdef OpenBSD
     5         kx void
     5         kx config(int sig)
     5         kx {
     5         kx 	wantconfig = 1;
     5         kx }
     5         kx #endif
     5         kx 
     5         kx void
     5         kx config(int sig)
     5         kx {
     5         kx 	register struct servtab *sep, *cp, **sepp;
     5         kx 	int omask;
     5         kx 	int n, add;
     5         kx 	char protoname[10];
     5         kx 
     5         kx 	if (!setconfig()) {
     5         kx 		syslog(LOG_ERR, "%s: %m", CONFIG);
     5         kx 		return;
     5         kx 	}
     5         kx 	for (sep = servtab; sep; sep = sep->se_next)
     5         kx 		sep->se_checked = 0;
     5         kx 	cp = getconfigent();
     5         kx 	while (cp != NULL) {
     5         kx 		for (sep = servtab; sep; sep = sep->se_next)
     5         kx 			if (matchconf(sep, cp))
     5         kx 				break;
     5         kx 		add = 0;
     5         kx 		if (sep != 0) {
     5         kx 			int i;
     5         kx 
     5         kx #define SWAP(type, a, b) {type c=(type)a; a=(type)b; b=(type)c;}
     5         kx 
     5         kx 			omask = sigblock(SIGBLOCK);
     5         kx 			/*
     5         kx 			 * sep->se_wait may be holding the pid of a daemon
     5         kx 			 * that we're waiting for.  If so, don't overwrite
     5         kx 			 * it unless the config file explicitly says don't 
     5         kx 			 * wait.
     5         kx 			 */
     5         kx 			if (cp->se_bi == 0 && 
     5         kx 			    (sep->se_wait == 1 || cp->se_wait == 0))
     5         kx 				sep->se_wait = cp->se_wait;
     5         kx 			SWAP(int, cp->se_max, sep->se_max);
     5         kx 			SWAP(char *, sep->se_user, cp->se_user);
     5         kx 			SWAP(char *, sep->se_group, cp->se_group);
     5         kx 			SWAP(char *, sep->se_server, cp->se_server);
     5         kx 			for (i = 0; i < MAXARGV; i++)
     5         kx 				SWAP(char *, sep->se_argv[i], cp->se_argv[i]);
     5         kx #undef SWAP
     5         kx 			if (isrpcservice(sep))
     5         kx 				unregister_rpc(sep);
     5         kx 			sep->se_rpcversl = cp->se_rpcversl;
     5         kx 			sep->se_rpcversh = cp->se_rpcversh;
     5         kx 			sigsetmask(omask);
     5         kx 			freeconfig(cp);
     5         kx 			add = 1;
     5         kx 		} else {
     5         kx 			sep = enter(cp);
     5         kx 		}
     5         kx 		sep->se_checked = 1;
     5         kx 
     5         kx 		switch (sep->se_family) {
     5         kx 		case AF_UNIX:
     5         kx 			if (sep->se_fd != -1)
     5         kx 				break;
     5         kx 			(void)unlink(sep->se_service);
     5         kx 			n = strlen(sep->se_service);
     5         kx 			if (n > sizeof sep->se_ctrladdr_un.sun_path - 1) 
     5         kx 				n = sizeof sep->se_ctrladdr_un.sun_path - 1;
     5         kx 			strncpy(sep->se_ctrladdr_un.sun_path,
     5         kx 			    sep->se_service, n);
     5         kx 			sep->se_ctrladdr_un.sun_path[n] = '\0';
     5         kx 			sep->se_ctrladdr_un.sun_family = AF_UNIX;
     5         kx 			sep->se_ctrladdr_size = n +
     5         kx 			    sizeof sep->se_ctrladdr_un.sun_family;
     5         kx 			setup(sep);
     5         kx 			break;
     5         kx 		case AF_INET:
     5         kx 			sep->se_ctrladdr_in.sin_family = AF_INET;
     5         kx 			/* se_ctrladdr_in was set in getconfigent */
     5         kx 			sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in;
     5         kx 
     5         kx 			if (isrpcservice(sep)) {
     5         kx 				struct rpcent *rp;
     5         kx 
     5         kx 				sep->se_rpcprog = atoi(sep->se_service);
     5         kx 				if (sep->se_rpcprog == 0) {
     5         kx 					rp = getrpcbyname(sep->se_service);
     5         kx 					if (rp == 0) {
     5         kx 						syslog(LOG_ERR,
     5         kx 						    "%s: unknown rpc service",
     5         kx 						    sep->se_service);
     5         kx 						goto serv_unknown;
     5         kx 					}
     5         kx 					sep->se_rpcprog = rp->r_number;
     5         kx 				}
     5         kx 				if (sep->se_fd == -1)
     5         kx 					setup(sep);
     5         kx 				if (sep->se_fd != -1)
     5         kx 					register_rpc(sep);
     5         kx 			} else {
     5         kx 				u_short port = htons(atoi(sep->se_service));
     5         kx 
     5         kx 				if (!port) {
     5         kx 					/*XXX*/
     5         kx 					strncpy(protoname, sep->se_proto,
     5         kx 						sizeof(protoname));
     5         kx 					if (isdigit(protoname[strlen(protoname) - 1]))
     5         kx 						protoname[strlen(protoname) - 1] = '\0';
     5         kx 					sp = getservbyname(sep->se_service,
     5         kx 					    protoname);
     5         kx 					if (sp == 0) {
     5         kx 						syslog(LOG_ERR,
     5         kx 						    "%s/%s: unknown service",
     5         kx 						    sep->se_service, sep->se_proto);
     5         kx 						goto serv_unknown;
     5         kx 					}
     5         kx 					port = sp->s_port;
     5         kx 				}
     5         kx 				if (port != sep->se_ctrladdr_in.sin_port) {
     5         kx 					sep->se_ctrladdr_in.sin_port = port;
     5         kx 					if (sep->se_fd != -1) {
     5         kx 						FD_CLR(sep->se_fd, &allsock);
     5         kx 						nsock--;
     5         kx 						(void) close(sep->se_fd);
     5         kx 					}
     5         kx 					sep->se_fd = -1;
     5         kx 				}
     5         kx 				if (sep->se_fd == -1)
     5         kx 					setup(sep);
     5         kx 			}
     5         kx 			break;
     5         kx 		case AF_INET6:
     5         kx 			sep->se_ctrladdr_in6.sin6_family = AF_INET6;
     5         kx 			/* se_ctrladdr_in was set in getconfigent */
     5         kx 			sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in6;
     5         kx 
     5         kx 			if (isrpcservice(sep)) {
     5         kx 				struct rpcent *rp;
     5         kx 
     5         kx 				sep->se_rpcprog = atoi(sep->se_service);
     5         kx 				if (sep->se_rpcprog == 0) {
     5         kx 					rp = getrpcbyname(sep->se_service);
     5         kx 					if (rp == 0) {
     5         kx 						syslog(LOG_ERR,
     5         kx 						    "%s: unknown rpc service",
     5         kx 						    sep->se_service);
     5         kx 						goto serv_unknown;
     5         kx 					}
     5         kx 					sep->se_rpcprog = rp->r_number;
     5         kx 				}
     5         kx 				if (sep->se_fd == -1)
     5         kx 					setup(sep);
     5         kx 				if (sep->se_fd != -1)
     5         kx 					register_rpc(sep);
     5         kx 			} else {
     5         kx 				u_short port = htons(atoi(sep->se_service));
     5         kx 
     5         kx 				if (!port) {
     5         kx 					/*XXX*/
     5         kx 					strncpy(protoname, sep->se_proto,
     5         kx 						sizeof(protoname));
     5         kx 					if (isdigit(protoname[strlen(protoname) - 1]))
     5         kx 						protoname[strlen(protoname) - 1] = '\0';
     5         kx 					sp = getservbyname(sep->se_service,
     5         kx 					    protoname);
     5         kx 					if (sp == 0) {
     5         kx 						syslog(LOG_ERR,
     5         kx 						    "%s/%s: unknown service",
     5         kx 						    sep->se_service, sep->se_proto);
     5         kx 						goto serv_unknown;
     5         kx 					}
     5         kx 					port = sp->s_port;
     5         kx 				}
     5         kx 				if (port != sep->se_ctrladdr_in6.sin6_port) {
     5         kx 					sep->se_ctrladdr_in6.sin6_port = port;
     5         kx 					if (sep->se_fd != -1) {
     5         kx 						FD_CLR(sep->se_fd, &allsock);
     5         kx 						nsock--;
     5         kx 						(void) close(sep->se_fd);
     5         kx 					}
     5         kx 					sep->se_fd = -1;
     5         kx 				}
     5         kx 				if (sep->se_fd == -1)
     5         kx 					setup(sep);
     5         kx 			}
     5         kx 			break;
     5         kx 		}
     5         kx 	serv_unknown:
     5         kx 		if (cp->se_next != NULL) {
     5         kx 			struct servtab *tmp = cp;
     5         kx 
     5         kx 			cp = cp->se_next;
     5         kx 			free(tmp);
     5         kx 		} else {
     5         kx 			free(cp);
     5         kx 			cp = getconfigent();
     5         kx 		}
     5         kx 		if (debug)
     5         kx 			print_service(add ? "REDO" : "ADD", sep);
     5         kx 	}
     5         kx 	endconfig();
     5         kx 	/*
     5         kx 	 * Purge anything not looked at above.
     5         kx 	 */
     5         kx 	omask = sigblock(SIGBLOCK);
     5         kx 	sepp = &servtab;
     5         kx 	while ((sep = *sepp)) {
     5         kx 		if (sep->se_checked) {
     5         kx 			sepp = &sep->se_next;
     5         kx 			continue;
     5         kx 		}
     5         kx 		*sepp = sep->se_next;
     5         kx 		if (sep->se_fd != -1) {
     5         kx 			FD_CLR(sep->se_fd, &allsock);
     5         kx 			nsock--;
     5         kx 			(void) close(sep->se_fd);
     5         kx 		}
     5         kx 		if (isrpcservice(sep))
     5         kx 			unregister_rpc(sep);
     5         kx 		if (sep->se_family == AF_UNIX)
     5         kx 			(void)unlink(sep->se_service);
     5         kx 		if (debug)
     5         kx 			print_service("FREE", sep);
     5         kx 		freeconfig(sep);
     5         kx 		free((char *)sep);
     5         kx 	}
     5         kx 	(void) sigsetmask(omask);
     5         kx }
     5         kx 
     5         kx #ifdef OpenBSD
     5         kx void
     5         kx retry(int sig)
     5         kx {
     5         kx 	wantretry = 1;
     5         kx }
     5         kx #endif
     5         kx 
     5         kx void
     5         kx retry(int sig)
     5         kx {
     5         kx 	register struct servtab *sep;
     5         kx 
     5         kx 	timingout = 0;
     5         kx 	for (sep = servtab; sep; sep = sep->se_next) {
     5         kx 		if (sep->se_fd == -1) {
     5         kx 			switch (sep->se_family) {
     5         kx 			case AF_UNIX:
     5         kx 			case AF_INET:
     5         kx 			case AF_INET6:
     5         kx 				setup(sep);
     5         kx 				if (sep->se_fd != -1 && isrpcservice(sep))
     5         kx 					register_rpc(sep);
     5         kx 				break;
     5         kx 			}
     5         kx 		}
     5         kx 	}
     5         kx }
     5         kx 
     5         kx void
     5         kx goaway(sig)
     5         kx 	int sig;
     5         kx {
     5         kx 	register struct servtab *sep;
     5         kx 
     5         kx 	/* XXX signal race walking sep list */
     5         kx 	for (sep = servtab; sep; sep = sep->se_next) {
     5         kx 		if (sep->se_fd == -1)
     5         kx 			continue;
     5         kx 
     5         kx 		switch (sep->se_family) {
     5         kx 		case AF_UNIX:
     5         kx 			(void)unlink(sep->se_service);
     5         kx 			break;
     5         kx 		case AF_INET:
     5         kx 		case AF_INET6:
     5         kx 			if (sep->se_wait == 1 && isrpcservice(sep))
     5         kx 				unregister_rpc(sep);	/* XXX signal race */
     5         kx 			break;
     5         kx 		}
     5         kx 		(void)close(sep->se_fd);
     5         kx 	}
     5         kx 	(void)unlink(_PATH_INETDPID);
     5         kx 	exit(0);
     5         kx }
     5         kx 
     5         kx int bump_nofile __P((void));
     5         kx 
     5         kx void
     5         kx setup(sep)
     5         kx 	register struct servtab *sep;
     5         kx {
     5         kx 	int on = 1;
     5         kx 	int r;
     5         kx 
     5         kx 	if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) {
     5         kx 		syslog(LOG_ERR, "%s/%s: socket: %m",
     5         kx 		    sep->se_service, sep->se_proto);
     5         kx 		return;
     5         kx 	}
     5         kx #define	turnon(fd, opt) \
     5         kx setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
     5         kx 	if (strncmp(sep->se_proto, "tcp", 3) == 0 && (options & SO_DEBUG) &&
     5         kx 	    turnon(sep->se_fd, SO_DEBUG) < 0)
     5         kx 		syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
     5         kx 	if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
     5         kx 		syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
     5         kx #undef turnon
     5         kx 	if (isrpcservice(sep)) {
     5         kx 		struct passwd *pwd;
     5         kx 
     5         kx 		/*
     5         kx 		 * for RPC services, attempt to use a reserved port
     5         kx 		 * if they are going to be running as root.
     5         kx 		 *
     5         kx 		 * Also, zero out the port for all RPC services; let bind()
     5         kx 		 * find one.
     5         kx 		 */
     5         kx 		sep->se_ctrladdr_in.sin_port = 0;
     5         kx 		if (sep->se_user && (pwd = getpwnam(sep->se_user)) &&
     5         kx 		    pwd->pw_uid == 0 && uid == 0)
     5         kx 			r = bindresvport(sep->se_fd, &sep->se_ctrladdr_in);
     5         kx 		else {
     5         kx 			r = bind(sep->se_fd, &sep->se_ctrladdr,
     5         kx 			    sep->se_ctrladdr_size);
     5         kx 			if (r == 0) {
     5         kx 				int len = sep->se_ctrladdr_size;
     5         kx 				int saveerrno = errno;
     5         kx 
     5         kx 				/* update se_ctrladdr_in.sin_port */
     5         kx 				r = getsockname(sep->se_fd, &sep->se_ctrladdr,
     5         kx 				    &len);
     5         kx 				if (r <= 0)
     5         kx 					errno = saveerrno;
     5         kx 			}
     5         kx 		}
     5         kx 	} else
     5         kx 		r = bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size);
     5         kx 	if (r < 0) {
     5         kx 		syslog(LOG_ERR, "%s/%s (%d): bind: %m",
     5         kx 		    sep->se_service, sep->se_proto, sep->se_ctrladdr.sa_family);
     5         kx 		close(sep->se_fd);
     5         kx 		sep->se_fd = -1;
     5         kx 		if (!timingout) {
     5         kx 			timingout = 1;
     5         kx 			alarm(RETRYTIME);
     5         kx 		}
     5         kx 		return;
     5         kx 	}
     5         kx 	if (sep->se_socktype == SOCK_STREAM)
     5         kx 		listen(sep->se_fd, global_queuelen);
     5         kx 
     5         kx 	/* fd_grow(&allsockp, &allsockn, sep->se_fd); */
     5         kx 	FD_SET(sep->se_fd, &allsock);
     5         kx 	nsock++;
     5         kx 	if (sep->se_fd > maxsock) {
     5         kx 		maxsock = sep->se_fd;
     5         kx 		if (maxsock > rlim_ofile_cur - FD_MARGIN)
     5         kx 			bump_nofile();
     5         kx 	}
     5         kx }
     5         kx 
     5         kx void
     5         kx register_rpc(sep)
     5         kx 	register struct servtab *sep;
     5         kx {
     5         kx 	int n;
     5         kx 	struct sockaddr_in sin;
     5         kx 	struct protoent *pp;
     5         kx 
     5         kx 	if ((pp = getprotobyname(sep->se_proto+4)) == NULL) {
     5         kx 		syslog(LOG_ERR, "%s: getproto: %m",
     5         kx 		    sep->se_proto);
     5         kx 		return;
     5         kx 	}
     5         kx 	n = sizeof sin;
     5         kx 	if (getsockname(sep->se_fd, (struct sockaddr *)&sin, &n) < 0) {
     5         kx 		syslog(LOG_ERR, "%s/%s: getsockname: %m",
     5         kx 		    sep->se_service, sep->se_proto);
     5         kx 		return;
     5         kx 	}
     5         kx 
     5         kx 	for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
     5         kx 		if (debug)
     5         kx 			fprintf(stderr, "pmap_set: %u %u %u %u\n",
     5         kx 			    sep->se_rpcprog, n, pp->p_proto,
     5         kx 			    ntohs(sin.sin_port));
     5         kx 		(void)pmap_unset(sep->se_rpcprog, n);
     5         kx 		if (!pmap_set(sep->se_rpcprog, n, pp->p_proto, ntohs(sin.sin_port)))
     5         kx 			syslog(LOG_ERR, "%s %s: pmap_set: %u %u %u %u: %m",
     5         kx 			    sep->se_service, sep->se_proto,
     5         kx 			    sep->se_rpcprog, n, pp->p_proto,
     5         kx 			    ntohs(sin.sin_port));
     5         kx 	}
     5         kx }
     5         kx 
     5         kx void
     5         kx unregister_rpc(sep)
     5         kx 	register struct servtab *sep;
     5         kx {
     5         kx 	int n;
     5         kx 
     5         kx 	for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
     5         kx 		if (debug)
     5         kx 			fprintf(stderr, "pmap_unset(%u, %u)\n",
     5         kx 			    sep->se_rpcprog, n);
     5         kx 		if (!pmap_unset(sep->se_rpcprog, n))
     5         kx 			syslog(LOG_ERR, "pmap_unset(%u, %u)",
     5         kx 			    sep->se_rpcprog, n);
     5         kx 	}
     5         kx }
     5         kx 
     5         kx 
     5         kx struct servtab *
     5         kx enter(cp)
     5         kx 	struct servtab *cp;
     5         kx {
     5         kx 	register struct servtab *sep;
     5         kx 	int omask;
     5         kx 
     5         kx 	sep = (struct servtab *)malloc(sizeof (*sep));
     5         kx 	if (sep == NULL) {
     5         kx 		syslog(LOG_ERR, "Out of memory.");
     5         kx 		exit(1);
     5         kx 	}
     5         kx 	*sep = *cp;
     5         kx 	sep->se_fd = -1;
     5         kx 	sep->se_rpcprog = -1;
     5         kx 	omask = sigblock(SIGBLOCK);
     5         kx 	sep->se_next = servtab;
     5         kx 	servtab = sep;
     5         kx 	sigsetmask(omask);
     5         kx 	return (sep);
     5         kx }
     5         kx 
     5         kx int
     5         kx matchconf (old, new)
     5         kx 	struct servtab	*old;
     5         kx 	struct servtab	*new;
     5         kx {
     5         kx 	if (strcmp(old->se_service, new->se_service) != 0)
     5         kx 		return (0);
     5         kx 
     5         kx 	if (strcmp(old->se_hostaddr, new->se_hostaddr) != 0)
     5         kx 		return (0);
     5         kx 
     5         kx 	if (strcmp(old->se_proto, new->se_proto) != 0)
     5         kx 		return (0);
     5         kx 
     5         kx 	/*
     5         kx 	 * If the new servtab is bound to a specific address, check that the
     5         kx 	 * old servtab is bound to the same entry. If the new service is not
     5         kx 	 * bound to a specific address then the check of se_hostaddr above
     5         kx 	 * is sufficient.
     5         kx 	 */
     5         kx 
     5         kx 	if (old->se_family == AF_INET && new->se_family == AF_INET &&
     5         kx 	    bcmp(&old->se_ctrladdr_in.sin_addr,
     5         kx 	    &new->se_ctrladdr_in.sin_addr,
     5         kx 	    sizeof(new->se_ctrladdr_in.sin_addr)) != 0)
     5         kx 		return (0);
     5         kx 
     5         kx 	if (old->se_family == AF_INET6 && new->se_family == AF_INET6 &&
     5         kx 	    bcmp(&old->se_ctrladdr_in6.sin6_addr,
     5         kx 	    &new->se_ctrladdr_in6.sin6_addr,
     5         kx 	    sizeof(new->se_ctrladdr_in6.sin6_addr)) != 0)
     5         kx 		return (0);
     5         kx #ifdef OpenBSD
     5         kx 	if (old->se_family == AF_INET6 && new->se_family == AF_INET6 &&
     5         kx 	    old->se_ctrladdr_in6.sin6_scope_id !=
     5         kx 	    new->se_ctrladdr_in6.sin6_scope_id)
     5         kx 		return (0);
     5         kx #endif
     5         kx 
     5         kx 	return (1);
     5         kx }
     5         kx 
     5         kx FILE		*fconfig = NULL;
     5         kx char		line[1024];
     5         kx char		*defhost;
     5         kx char		*skip __P((char **));
     5         kx char		*nextline __P((FILE *));
     5         kx char		*newstr __P((char *));
     5         kx struct servtab	*dupconfig __P((struct servtab *));
     5         kx 
     5         kx int
     5         kx setconfig()
     5         kx {
     5         kx 	if (defhost) free(defhost);
     5         kx 	defhost = newstr("*");
     5         kx 	if (fconfig != NULL) {
     5         kx 		fseek(fconfig, 0L, SEEK_SET);
     5         kx 		return (1);
     5         kx 	}
     5         kx 	fconfig = fopen(CONFIG, "r");
     5         kx 	return (fconfig != NULL);
     5         kx }
     5         kx 
     5         kx void
     5         kx endconfig()
     5         kx {
     5         kx 	if (fconfig) {
     5         kx 		(void) fclose(fconfig);
     5         kx 		fconfig = NULL;
     5         kx 	}
     5         kx 	if (defhost) {
     5         kx 		free(defhost);
     5         kx 		defhost = 0;
     5         kx 	}
     5         kx }
     5         kx 
     5         kx struct servtab *
     5         kx getconfigent()
     5         kx {
     5         kx 	register struct servtab *sep;
     5         kx 	int argc;
     5         kx 	char *cp, *arg;
     5         kx 	char *hostdelim;
     5         kx 	struct servtab *nsep;
     5         kx 	struct servtab *psep;
     5         kx 
     5         kx 	sep = (struct servtab *) malloc(sizeof(struct servtab));
     5         kx 	if (sep == NULL) {
     5         kx 		syslog(LOG_ERR, "malloc: %m");
     5         kx 		exit(1);
     5         kx 	}
     5         kx 
     5         kx 	/* memset(sep, 0, sizeof *sep); */
     5         kx more:
     5         kx 	/* freeconfig(sep); */
     5         kx 	
     5         kx 	while ((cp = nextline(fconfig)) && *cp == '#')
     5         kx 		;
     5         kx 	if (cp == NULL) {
     5         kx 		/* free(sep); */
     5         kx 		return (NULL);
     5         kx 	}
     5         kx 	
     5         kx 	memset((char *)sep, 0, sizeof *sep);
     5         kx 	arg = skip(&cp);
     5         kx 	if (arg == NULL) {
     5         kx 		/* A blank line. */
     5         kx 		goto more;
     5         kx 	}
     5         kx 
     5         kx 	/* Check for a host name. */
     5         kx 	hostdelim = strrchr(arg, ':');
     5         kx 	if (hostdelim) {
     5         kx 		*hostdelim = '\0';
     5         kx #ifdef OpenBSD
     5         kx 		if (arg[0] == '[' && hostdelim > arg && hostdelim[-1] == ']') {
     5         kx 			hostdelim[-1] = '\0';
     5         kx 			sep->se_hostaddr = newstr(arg + 1);
     5         kx 		} else
     5         kx #endif
     5         kx 			sep->se_hostaddr = newstr(arg);
     5         kx 		arg = hostdelim + 1;
     5         kx 		/*
     5         kx 		 * If the line is of the form `host:', then just change the
     5         kx 		 * default host for the following lines.
     5         kx 		 */
     5         kx 		if (*arg == '\0') {
     5         kx 			arg = skip(&cp);
     5         kx 			if (cp == NULL) {
     5         kx 				free(defhost);
     5         kx 				defhost = sep->se_hostaddr;
     5         kx 				goto more;
     5         kx 			}
     5         kx 		}
     5         kx 	} else
     5         kx 		sep->se_hostaddr = newstr(defhost);
     5         kx 
     5         kx 	sep->se_service = newstr(arg);
     5         kx #ifdef OpenBSD
     5         kx 	if ((arg = skip(&cp, 1)) == NULL)
     5         kx 		goto more;
     5         kx #endif
     5         kx 	arg = skip (&cp);
     5         kx 
     5         kx 	if (strcmp(arg, "stream") == 0)
     5         kx 		sep->se_socktype = SOCK_STREAM;
     5         kx 	else if (strcmp(arg, "dgram") == 0)
     5         kx 		sep->se_socktype = SOCK_DGRAM;
     5         kx 	else if (strcmp(arg, "rdm") == 0)
     5         kx 		sep->se_socktype = SOCK_RDM;
     5         kx 	else if (strcmp(arg, "seqpacket") == 0)
     5         kx 		sep->se_socktype = SOCK_SEQPACKET;
     5         kx 	else if (strcmp(arg, "raw") == 0)
     5         kx 		sep->se_socktype = SOCK_RAW;
     5         kx 	else
     5         kx 		sep->se_socktype = -1;
     5         kx 
     5         kx #ifdef OpenBSD
     5         kx 	if ((arg = skip(&cp, 1)) == NULL)
     5         kx 		goto more;
     5         kx #endif
     5         kx 	
     5         kx 	sep->se_proto = newstr(skip (&cp));
     5         kx 
     5         kx 	if (strcmp(sep->se_proto, "unix") == 0) {
     5         kx 		sep->se_family = AF_UNIX;
     5         kx 	} else {
     5         kx 		sep->se_family = AF_INET;
     5         kx 		if (sep->se_proto[strlen(sep->se_proto) - 1] == '6')
     5         kx 			sep->se_family = AF_INET6;
     5         kx 		if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
     5         kx 			char *cp, *ccp;
     5         kx 			long l;
     5         kx 
     5         kx 			cp = strchr(sep->se_service, '/');
     5         kx 			if (cp == 0) {
     5         kx 				syslog(LOG_ERR, "%s: no rpc version",
     5         kx 				    sep->se_service);
     5         kx 				goto more;
     5         kx 			}
     5         kx 			*cp++ = '\0';
     5         kx 			l = strtol(cp, &ccp, 0);
     5         kx 			if (ccp == cp || l < 0 || l > INT_MAX) {
     5         kx 		badafterall:
     5         kx 				syslog(LOG_ERR, "%s/%s: bad rpc version",
     5         kx 				    sep->se_service, cp);
     5         kx 				goto more;
     5         kx 			}
     5         kx 			sep->se_rpcversl = sep->se_rpcversh = l;
     5         kx 			if (*ccp == '-') {
     5         kx 				cp = ccp + 1;
     5         kx 				l = strtol(cp, &ccp, 0); 
     5         kx 				if (ccp == cp || l < 0 || l > INT_MAX ||
     5         kx 				    l < sep->se_rpcversl || *ccp)
     5         kx 					goto badafterall;
     5         kx 				sep->se_rpcversh = l;
     5         kx 			} else if (*ccp != '\0')
     5         kx 				goto badafterall;
     5         kx 		}
     5         kx 	}
     5         kx 	arg = skip(&cp);
     5         kx 	if (arg == NULL)
     5         kx 		goto more;
     5         kx 
     5         kx #ifdef OpenBSD
     5         kx 	s = strchr(arg, '.');
     5         kx 	if (s) {
     5         kx 		char *p;
     5         kx 
     5         kx 		*s++ = '\0';
     5         kx 		sep->se_max = strtoul(s, &p, 0);
     5         kx 		if (sep->se_max < 1 || *p) {
     5         kx 			syslog(LOG_ERR, "%s: illegal max field \"%s\", setting to %d",
     5         kx 			    sep->se_service, s, toomany);
     5         kx 			sep->se_max = toomany;
     5         kx 		}
     5         kx 	} else
     5         kx 		sep->se_max = toomany;
     5         kx #endif
     5         kx 
     5         kx 	{
     5         kx 		char *s = strchr (arg, '.');
     5         kx 		if (s) {
     5         kx 			*s++ = '\0';
     5         kx 			sep->se_max = atoi (s);
     5         kx 		} else
     5         kx 			sep->se_max = toomany;
     5         kx 	}
     5         kx 	sep->se_wait = strcmp(arg, "wait") == 0;
     5         kx 	/* if ((arg = skip(&cp, 1)) == NULL) */
     5         kx 		/* goto more; */
     5         kx 	sep->se_user = newstr(skip (&cp));
     5         kx 	arg = strchr(sep->se_user, '.');
     5         kx 	if (arg == NULL)
     5         kx 		arg = strchr(sep->se_user, ':');
     5         kx 	if (arg) {
     5         kx 		*arg++ = '\0';
     5         kx 		sep->se_group = newstr(arg);
     5         kx 	}
     5         kx 	/* if ((arg = skip(&cp, 1)) == NULL) */
     5         kx 		/* goto more; */
     5         kx 	
     5         kx 	sep->se_server = newstr(skip (&cp));
     5         kx 	if (strcmp(sep->se_server, "internal") == 0) {
     5         kx 		register struct biltin *bi;
     5         kx 
     5         kx 		for (bi = biltins; bi->bi_service; bi++)
     5         kx 			if (bi->bi_socktype == sep->se_socktype &&
     5         kx 			    strcmp(bi->bi_service, sep->se_service) == 0)
     5         kx 				break;
     5         kx 		if (bi->bi_service == 0) {
     5         kx 			syslog(LOG_ERR, "internal service %s unknown",
     5         kx 			    sep->se_service);
     5         kx 			goto more;
     5         kx 		}
     5         kx 		sep->se_bi = bi;
     5         kx 		sep->se_wait = bi->bi_wait;
     5         kx 	} else
     5         kx 		sep->se_bi = NULL;
     5         kx 	argc = 0;
     5         kx 	for (arg = skip(&cp); cp; arg = skip(&cp)) {
     5         kx 		if (argc < MAXARGV)
     5         kx 			sep->se_argv[argc++] = newstr(arg);
     5         kx 	}
     5         kx 	while (argc <= MAXARGV)
     5         kx 		sep->se_argv[argc++] = NULL;
     5         kx 
     5         kx 	/*
     5         kx 	 * Now that we've processed the entire line, check if the hostname
     5         kx 	 * specifier was a comma separated list of hostnames. If so
     5         kx 	 * we'll make new entries for each address.
     5         kx 	 */
     5         kx 	while ((hostdelim = strrchr(sep->se_hostaddr, ',')) != NULL) {
     5         kx 		nsep = dupconfig(sep);
     5         kx 
     5         kx 		/*
     5         kx 		 * NULL terminate the hostname field of the existing entry,
     5         kx 		 * and make a dup for the new entry.
     5         kx 		 */
     5         kx 		*hostdelim++ = '\0';
     5         kx 		nsep->se_hostaddr = newstr(hostdelim);
     5         kx 
     5         kx 		nsep->se_next = sep->se_next;
     5         kx 		sep->se_next = nsep;
     5         kx 	}
     5         kx 
     5         kx 	nsep = sep;
     5         kx 	while (nsep != NULL) {
     5         kx 		nsep->se_checked = 1;
     5         kx 		/* switch (nsep->se_family) { */
     5         kx 		if (nsep->se_family == AF_INET) {
     5         kx #ifdef OpenBSD
     5         kx 		case AF_INET:
     5         kx 		case AF_INET6:
     5         kx 		    {
     5         kx 			struct addrinfo hints, *res0, *res;
     5         kx 			char *host, *port;
     5         kx 			int error;
     5         kx 			int s;
     5         kx 
     5         kx 			/* check if the family is supported */
     5         kx 			s = socket(nsep->se_family, SOCK_DGRAM, 0);
     5         kx 			if (s < 0) {
     5         kx 				syslog(LOG_WARNING,
     5         kx "%s/%s: %s: the address family is not supported by the kernel",
     5         kx 				    nsep->se_service, nsep->se_proto,
     5         kx 				    nsep->se_hostaddr);
     5         kx 				nsep->se_checked = 0;
     5         kx 				goto skip;
     5         kx 			}
     5         kx 			close(s);
     5         kx #endif
     5         kx 			if (!strcmp (nsep->se_hostaddr, "*"))
     5         kx 				nsep->se_ctrladdr_in.sin_addr.s_addr = INADDR_ANY;
     5         kx 			else if (!inet_aton (nsep->se_hostaddr, &nsep->se_ctrladdr_in.sin_addr)) {
     5         kx 				struct hostent *hp;
     5         kx 
     5         kx #ifdef OpenBSD
     5         kx 			memset(&hints, 0, sizeof(hints));
     5         kx 			hints.ai_family = nsep->se_family;
     5         kx 			hints.ai_socktype = nsep->se_socktype;
     5         kx 			hints.ai_flags = AI_PASSIVE;
     5         kx 			if (!strcmp(nsep->se_hostaddr, "*"))
     5         kx 				host = NULL;
     5         kx 			else
     5         kx 				host = nsep->se_hostaddr;
     5         kx 			port = "0";
     5         kx 			/* XXX shortened IPv4 syntax is now forbidden */
     5         kx 			error = getaddrinfo(host, port, &hints, &res0);
     5         kx 			if (error) {
     5         kx 				syslog(LOG_ERR, "%s/%s: %s: %s",
     5         kx 				    nsep->se_service, nsep->se_proto,
     5         kx 				    nsep->se_hostaddr,
     5         kx 				    gai_strerror(error));
     5         kx 				nsep->se_checked = 0;
     5         kx 				goto skip;
     5         kx 			}
     5         kx 			for (res = res0; res; res = res->ai_next) {
     5         kx 				if (res->ai_addrlen >
     5         kx 				    sizeof(nsep->se_ctrladdr_storage))
     5         kx 					continue;
     5         kx 				if (res == res0) {
     5         kx 					memcpy(&nsep->se_ctrladdr_storage,
     5         kx 					    res->ai_addr, res->ai_addrlen);
     5         kx 					continue;
     5         kx 				}
     5         kx #endif
     5         kx 				hp = gethostbyname (nsep->se_hostaddr);
     5         kx 				if (hp == 0) {
     5         kx 					syslog (LOG_ERR, "%s: unknown host", nsep->se_hostaddr);
     5         kx 					nsep->se_checked = 0;
     5         kx 					goto skip;
     5         kx 				} else if (hp->h_addrtype != AF_INET) {
     5         kx 					syslog (LOG_ERR,
     5         kx 					"%s: address isn't an Internet "
     5         kx 					"address", nsep->se_hostaddr);
     5         kx 					nsep->se_checked = 0;
     5         kx 					goto skip;
     5         kx 				} else {
     5         kx 					int i = 1;
     5         kx 
     5         kx #ifdef OpenBSD
     5         kx 				psep = dupconfig(nsep);
     5         kx 				psep->se_hostaddr = newstr(nsep->se_hostaddr);
     5         kx 				psep->se_checked = 1;
     5         kx 				memcpy(&psep->se_ctrladdr_storage, res->ai_addr,
     5         kx 				    res->ai_addrlen);
     5         kx 				psep->se_ctrladdr_size = res->ai_addrlen;
     5         kx 
     5         kx 				/*
     5         kx 				 * Prepend to list, don't want to look up its
     5         kx 				 * hostname again.
     5         kx 				 */
     5         kx 				psep->se_next = sep;
     5         kx 				sep = psep;
     5         kx 			}
     5         kx 			freeaddrinfo(res0);
     5         kx 			break;
     5         kx 		    }
     5         kx #endif
     5         kx 					memmove (&nsep->se_ctrladdr_in.sin_addr,
     5         kx 					hp->h_addr_list[0], sizeof (struct in_addr));
     5         kx 					while (hp->h_addr_list[i] != NULL) {
     5         kx 						psep = dupconfig (nsep);
     5         kx 						psep->se_hostaddr = newstr (nsep->se_hostaddr);
     5         kx 						psep->se_checked = 1;
     5         kx 						memmove (&psep->se_ctrladdr_in.sin_addr,
     5         kx 						hp->h_addr_list[i], sizeof (struct in_addr));
     5         kx 						psep->se_ctrladdr_size = sizeof (psep->se_ctrladdr_in);
     5         kx 						i++;
     5         kx 						/* Prepend to list, don't want to look up its */
     5         kx 						/* hostname again. */
     5         kx 						psep->se_next = sep;
     5         kx 						sep = psep;
     5         kx 					}
     5         kx 				}
     5         kx 			}
     5         kx 		}
     5         kx /* XXX BUG?: is this skip: label supposed to remain? */
     5         kx skip:
     5         kx 		nsep = nsep->se_next;
     5         kx 	}
     5         kx 
     5         kx 	/*
     5         kx 	 * Finally, free any entries which failed the gethostbyname
     5         kx 	 * check.
     5         kx 	 */
     5         kx 	psep = NULL;
     5         kx 	nsep = sep;
     5         kx 	while (nsep != NULL) {
     5         kx 		struct servtab *tsep;
     5         kx 
     5         kx 		if (nsep->se_checked == 0) {
     5         kx 			tsep = nsep;
     5         kx 			if (psep == NULL) {
     5         kx 				sep = nsep->se_next;
     5         kx 				nsep = sep;
     5         kx 			} else {
     5         kx 				nsep = nsep->se_next;
     5         kx 				psep->se_next = nsep;
     5         kx 			}
     5         kx 			freeconfig(tsep);
     5         kx 		} else {
     5         kx 			nsep->se_checked = 0;
     5         kx 			psep = nsep;
     5         kx 			nsep = nsep->se_next;
     5         kx 		}
     5         kx 	}
     5         kx 
     5         kx 	return (sep);
     5         kx }
     5         kx 
     5         kx void
     5         kx freeconfig(cp)
     5         kx 	register struct servtab *cp;
     5         kx {
     5         kx 	int i;
     5         kx 
     5         kx 	if (cp->se_hostaddr)
     5         kx 		free(cp->se_hostaddr);
     5         kx 	if (cp->se_service)
     5         kx 		free(cp->se_service);
     5         kx 	if (cp->se_proto)
     5         kx 		free(cp->se_proto);
     5         kx 	if (cp->se_user)
     5         kx 		free(cp->se_user);
     5         kx 	if (cp->se_group)
     5         kx 		free(cp->se_group);
     5         kx 	if (cp->se_server)
     5         kx 		free(cp->se_server);
     5         kx 	for (i = 0; i < MAXARGV; i++)
     5         kx 		if (cp->se_argv[i])
     5         kx 			free(cp->se_argv[i]);
     5         kx }
     5         kx 
     5         kx char *
     5         kx skip(cpp)
     5         kx 	char **cpp;
     5         kx 	/* int report; */
     5         kx {
     5         kx 	register char *cp = *cpp;
     5         kx 	char *start;
     5         kx 
     5         kx /* erp: */
     5         kx 	if (*cpp == NULL) {
     5         kx 		/* if (report) */
     5         kx 			/* syslog(LOG_ERR, "syntax error in inetd config file"); */
     5         kx 		return (NULL);
     5         kx 	}
     5         kx 	
     5         kx again:
     5         kx 	while (*cp == ' ' || *cp == '\t')
     5         kx 		cp++;
     5         kx 	if (*cp == '\0') {
     5         kx 		int c;
     5         kx 
     5         kx 		c = getc(fconfig);
     5         kx 		(void) ungetc(c, fconfig);
     5         kx 		if (c == ' ' || c == '\t')
     5         kx 			if ((cp = nextline(fconfig)))
     5         kx 				goto again;
     5         kx 		*cpp = NULL;
     5         kx 		/* goto erp; */
     5         kx 		return (NULL);
     5         kx 	}
     5         kx 	start = cp;
     5         kx 	while (*cp && *cp != ' ' && *cp != '\t')
     5         kx 		cp++;
     5         kx 	if (*cp != '\0')
     5         kx 		*cp++ = '\0';
     5         kx 	/* if ((*cpp = cp) == NULL) */
     5         kx 		/* goto erp; */
     5         kx 
     5         kx 	*cpp = cp;
     5         kx 	return (start);
     5         kx }
     5         kx 
     5         kx char *
     5         kx nextline(fd)
     5         kx 	FILE *fd;
     5         kx {
     5         kx 	char *cp;
     5         kx 
     5         kx 	if (fgets(line, sizeof (line), fd) == NULL)
     5         kx 		return (NULL);
     5         kx 	cp = strchr(line, '\n');
     5         kx 	if (cp)
     5         kx 		*cp = '\0';
     5         kx 	return (line);
     5         kx }
     5         kx 
     5         kx char *
     5         kx newstr(cp)
     5         kx 	char *cp;
     5         kx {
     5         kx 	if ((cp = strdup(cp ? cp : "")))
     5         kx 		return(cp);
     5         kx 	syslog(LOG_ERR, "strdup: %m");
     5         kx 	exit(1);
     5         kx }
     5         kx 
     5         kx struct servtab *
     5         kx dupconfig(sep)
     5         kx 	struct servtab *sep;
     5         kx {
     5         kx 	struct servtab *newtab;
     5         kx 	int argc;
     5         kx 
     5         kx 	newtab = (struct servtab *) malloc(sizeof(struct servtab));
     5         kx 
     5         kx 	if (newtab == NULL) {
     5         kx 		syslog(LOG_ERR, "malloc: %m");
     5         kx 		exit(1);
     5         kx 	}
     5         kx 
     5         kx 	memset((char *)newtab, 0, sizeof(struct servtab));
     5         kx 
     5         kx 	newtab->se_service = sep->se_service ? newstr(sep->se_service) : NULL;
     5         kx 	newtab->se_socktype = sep->se_socktype;
     5         kx 	newtab->se_family = sep->se_family;
     5         kx 	newtab->se_proto = sep->se_proto ? newstr(sep->se_proto) : NULL;
     5         kx 	newtab->se_rpcprog = sep->se_rpcprog;
     5         kx 	newtab->se_rpcversl = sep->se_rpcversl;
     5         kx 	newtab->se_rpcversh = sep->se_rpcversh;
     5         kx 	newtab->se_wait = sep->se_wait;
     5         kx 	newtab->se_user = sep->se_user ? newstr(sep->se_user) : NULL;
     5         kx 	newtab->se_group = sep->se_group ? newstr(sep->se_group) : NULL;
     5         kx 	newtab->se_bi = sep->se_bi;
     5         kx 	newtab->se_server = sep->se_server ? newstr(sep->se_server) : 0;
     5         kx 
     5         kx 	for (argc = 0; argc <= MAXARGV; argc++)
     5         kx 		newtab->se_argv[argc] = sep->se_argv[argc] ?
     5         kx 		    newstr(sep->se_argv[argc]) : NULL;
     5         kx 	newtab->se_max = sep->se_max;
     5         kx 
     5         kx 	return (newtab);
     5         kx }
     5         kx 
     5         kx void
     5         kx inetd_setproctitle(a, s)
     5         kx 	char *a;
     5         kx 	int s;
     5         kx {
     5         kx 	int size;
     5         kx 	register char *cp;
     5         kx 	struct sockaddr_in sin;
     5         kx 	char buf[80];
     5         kx 
     5         kx 	cp = Argv[0];
     5         kx 	size = sizeof(sin);
     5         kx 	(void) snprintf(buf, sizeof buf, "-%s", a);
     5         kx 	if (getpeername(s, (struct sockaddr *)&sin, &size) == 0) {
     5         kx 		char *s = inet_ntoa(sin.sin_addr);
     5         kx 
     5         kx 		buf[sizeof(buf) - 1 - strlen(s) - 3] = '\0';
     5         kx 		strcat(buf, " [");
     5         kx 		strcat(buf, s);
     5         kx 		strcat(buf, "]");
     5         kx 	}
     5         kx 	strncpy(cp, buf, LastArg - cp);
     5         kx 	cp += strlen(cp);
     5         kx 	while (cp < LastArg)
     5         kx 		*cp++ = ' ';
     5         kx }
     5         kx 
     5         kx void
     5         kx logpid()
     5         kx {
     5         kx 	FILE *fp;
     5         kx 
     5         kx 	if ((fp = fopen(_PATH_INETDPID, "w")) != NULL) {
     5         kx 		fprintf(fp, "%u\n", getpid());
     5         kx 		(void)fclose(fp);
     5         kx 	}
     5         kx }
     5         kx 
     5         kx int
     5         kx bump_nofile()
     5         kx {
     5         kx #ifdef RLIMIT_NOFILE
     5         kx 
     5         kx #define FD_CHUNK	32
     5         kx 
     5         kx 	struct rlimit rl;
     5         kx 
     5         kx 	if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
     5         kx 		syslog(LOG_ERR, "getrlimit: %m");
     5         kx 		return -1;
     5         kx 	}
     5         kx 	rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK);
     5         kx 	rl.rlim_cur = MIN(FD_SETSIZE, rl.rlim_cur + FD_CHUNK);
     5         kx 	if (rl.rlim_cur <= rlim_ofile_cur) {
     5         kx 		syslog(LOG_ERR,
     5         kx 		    "bump_nofile: cannot extend file limit, max = %d",
     5         kx 		    (int)rl.rlim_cur);
     5         kx 		return -1;
     5         kx 	}
     5         kx 
     5         kx 	if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
     5         kx 		syslog(LOG_ERR, "setrlimit: %m");
     5         kx 		return -1;
     5         kx 	}
     5         kx 
     5         kx 	rlim_ofile_cur = rl.rlim_cur;
     5         kx 	return 0;
     5         kx 
     5         kx #else
     5         kx 	syslog(LOG_ERR, "bump_nofile: cannot extend file limit");
     5         kx 	return -1;
     5         kx #endif
     5         kx }
     5         kx 
     5         kx /*
     5         kx  * Internet services provided internally by inetd:
     5         kx  */
     5         kx #define	BUFSIZE	4096
     5         kx 
     5         kx /* ARGSUSED */
     5         kx void
     5         kx echo_stream(s, sep)		/* Echo service -- echo data back */
     5         kx 	int s;
     5         kx 	struct servtab *sep;
     5         kx {
     5         kx 	char buffer[BUFSIZE];
     5         kx 	int i;
     5         kx 
     5         kx 	inetd_setproctitle(sep->se_service, s);
     5         kx 	while ((i = read(s, buffer, sizeof(buffer))) > 0 &&
     5         kx 	    write(s, buffer, i) > 0)
     5         kx 		;
     5         kx 	exit(0);
     5         kx }
     5         kx 
     5         kx /* ARGSUSED */
     5         kx void
     5         kx echo_dg(s, sep)			/* Echo service -- echo data back */
     5         kx 	int s;
     5         kx 	struct servtab *sep;
     5         kx {
     5         kx 	char buffer[BUFSIZE];
     5         kx 	int i, size;
     5         kx 	/* struct sockaddr_storage ss; */
     5         kx 	struct sockaddr sa;
     5         kx 
     5         kx 	size = sizeof(sa);
     5         kx 	if ((i = recvfrom(s, buffer, sizeof(buffer), 0, &sa,
     5         kx 	    &size)) < 0)
     5         kx 		return;
     5         kx 	if (dg_badinput((struct sockaddr_in *)&sa))
     5         kx 		return;
     5         kx 	(void) sendto(s, buffer, i, 0, &sa, sizeof(sa));
     5         kx }
     5         kx 
     5         kx /* ARGSUSED */
     5         kx void
     5         kx discard_stream(s, sep)		/* Discard service -- ignore data */
     5         kx 	int s;
     5         kx 	struct servtab *sep;
     5         kx {
     5         kx 	char buffer[BUFSIZE];
     5         kx 
     5         kx 	inetd_setproctitle(sep->se_service, s);
     5         kx 	while ((errno = 0, read(s, buffer, sizeof(buffer)) > 0) ||
     5         kx 	    errno == EINTR)
     5         kx 		;
     5         kx 	exit(0);
     5         kx }
     5         kx 
     5         kx /* ARGSUSED */
     5         kx void
     5         kx discard_dg(s, sep)		/* Discard service -- ignore data */
     5         kx 	int s;
     5         kx 	struct servtab *sep;
     5         kx {
     5         kx 	char buffer[BUFSIZE];
     5         kx 
     5         kx 	(void) read(s, buffer, sizeof(buffer));
     5         kx }
     5         kx 
     5         kx #include <ctype.h>
     5         kx #define LINESIZ 72
     5         kx char ring[128];
     5         kx char *endring;
     5         kx 
     5         kx void
     5         kx initring()
     5         kx {
     5         kx 	register int i;
     5         kx 
     5         kx 	endring = ring;
     5         kx 
     5         kx 	for (i = 0; i <= 128; ++i)
     5         kx 		if (isprint(i))
     5         kx 			*endring++ = i;
     5         kx }
     5         kx 
     5         kx /* ARGSUSED */
     5         kx void
     5         kx chargen_stream(s, sep)		/* Character generator */
     5         kx 	int s;
     5         kx 	struct servtab *sep;
     5         kx {
     5         kx 	register char *rs;
     5         kx 	int len;
     5         kx 	char text[LINESIZ+2];
     5         kx 
     5         kx 	inetd_setproctitle(sep->se_service, s);
     5         kx 
     5         kx 	if (!endring) {
     5         kx 		initring();
     5         kx 		rs = ring;
     5         kx 	}
     5         kx 
     5         kx 	text[LINESIZ] = '\r';
     5         kx 	text[LINESIZ + 1] = '\n';
     5         kx 	for (rs = ring;;) {
     5         kx 		if ((len = endring - rs) >= LINESIZ)
     5         kx 			memmove(text, rs, LINESIZ);
     5         kx 		else {
     5         kx 			memmove(text, rs, len);
     5         kx 			memmove(text + len, ring, LINESIZ - len);
     5         kx 		}
     5         kx 		if (++rs == endring)
     5         kx 			rs = ring;
     5         kx 		if (write(s, text, sizeof(text)) != sizeof(text))
     5         kx 			break;
     5         kx 	}
     5         kx 	exit(0);
     5         kx }
     5         kx 
     5         kx /* ARGSUSED */
     5         kx void
     5         kx chargen_dg(s, sep)		/* Character generator */
     5         kx 	int s;
     5         kx 	struct servtab *sep;
     5         kx {
     5         kx 	/* struct sockaddr_storage ss; */
     5         kx 	struct sockaddr sa;
     5         kx 	static char *rs;
     5         kx 	int len, size;
     5         kx 	char text[LINESIZ+2];
     5         kx 
     5         kx 	if (endring == 0) {
     5         kx 		initring();
     5         kx 		rs = ring;
     5         kx 	}
     5         kx 
     5         kx 	size = sizeof(sa);
     5         kx 	if (recvfrom(s, text, sizeof(text), 0, &sa,
     5         kx 	    &size) < 0)
     5         kx 		return;
     5         kx 	if (dg_badinput((struct sockaddr_in *)&sa))
     5         kx 		return;
     5         kx 
     5         kx 	if ((len = endring - rs) >= LINESIZ)
     5         kx 		memmove(text, rs, LINESIZ);
     5         kx 	else {
     5         kx 		memmove(text, rs, len);
     5         kx 		memmove(text + len, ring, LINESIZ - len);
     5         kx 	}
     5         kx 	if (++rs == endring)
     5         kx 		rs = ring;
     5         kx 	text[LINESIZ] = '\r';
     5         kx 	text[LINESIZ + 1] = '\n';
     5         kx 	(void) sendto(s, text, sizeof(text), 0, &sa, sizeof(sa));
     5         kx }
     5         kx 
     5         kx /*
     5         kx  * Return a machine readable date and time, in the form of the
     5         kx  * number of seconds since midnight, Jan 1, 1900.  Since gettimeofday
     5         kx  * returns the number of seconds since midnight, Jan 1, 1970,
     5         kx  * we must add 2208988800 seconds to this figure to make up for
     5         kx  * some seventy years Bell Labs was asleep.
     5         kx  */
     5         kx 
     5         kx u_int
     5         kx machtime()
     5         kx {
     5         kx 	struct timeval tv;
     5         kx 
     5         kx 	if (gettimeofday(&tv, NULL) < 0) {
     5         kx 		fprintf(stderr, "Unable to get time of day\n");
     5         kx 		return (0L);
     5         kx 	}
     5         kx 	return (htonl((u_int)tv.tv_sec + 2208988800UL));
     5         kx }
     5         kx 
     5         kx /* ARGSUSED */
     5         kx void
     5         kx machtime_stream(s, sep)
     5         kx 	int s;
     5         kx 	struct servtab *sep;
     5         kx {
     5         kx 	u_int result;
     5         kx 
     5         kx 	result = machtime();
     5         kx 	(void) write(s, (char *) &result, sizeof(result));
     5         kx }
     5         kx 
     5         kx /* ARGSUSED */
     5         kx void
     5         kx machtime_dg(s, sep)
     5         kx 	int s;
     5         kx 	struct servtab *sep;
     5         kx {
     5         kx 	u_int result;
     5         kx 	/* struct sockaddr_storage ss; */
     5         kx 	struct sockaddr sa;
     5         kx 	struct sockaddr_in *sin;
     5         kx 	int size;
     5         kx 
     5         kx 	size = sizeof(sa);
     5         kx 	if (recvfrom(s, (char *)&result, sizeof(result), 0,
     5         kx 	    &sa, &size) < 0)
     5         kx 		return;
     5         kx 	/* if (dg_badinput((struct sockaddr *)&ss)) */
     5         kx 	sin = (struct sockaddr_in *) &sa;
     5         kx 	if (sin->sin_addr.s_addr == htonl (INADDR_BROADCAST) ||
     5         kx 	ntohs (sin->sin_port) < IPPORT_RESERVED / 2)
     5         kx 		return;
     5         kx 	result = machtime();
     5         kx 	(void) sendto(s, (char *) &result, sizeof(result), 0,
     5         kx 	    &sa, sizeof(sa));
     5         kx }
     5         kx 
     5         kx /* ARGSUSED */
     5         kx void
     5         kx daytime_stream(s, sep)		/* Return human-readable time of day */
     5         kx 	int s;
     5         kx 	struct servtab *sep;
     5         kx {
     5         kx 	char buffer[256];
     5         kx 	time_t clock;
     5         kx 
     5         kx 	clock = time(NULL);
     5         kx 
     5         kx 	(void) sprintf(buffer, "%.24s\r\n", ctime(&clock));
     5         kx 	(void) write(s, buffer, strlen(buffer));
     5         kx }
     5         kx 
     5         kx /* ARGSUSED */
     5         kx void
     5         kx daytime_dg(s, sep)		/* Return human-readable time of day */
     5         kx 	int s;
     5         kx 	struct servtab *sep;
     5         kx {
     5         kx 	char buffer[256];
     5         kx 	time_t clock;
     5         kx 	/* struct sockaddr_storage ss; */
     5         kx 	struct sockaddr sa;
     5         kx 	int size;
     5         kx 
     5         kx 	clock = time((time_t *) 0);
     5         kx 
     5         kx 	size = sizeof(sa);
     5         kx 	if (recvfrom(s, buffer, sizeof(buffer), 0, &sa,
     5         kx 	    &size) < 0)
     5         kx 		return;
     5         kx 	if (dg_badinput((struct sockaddr_in *)&sa))
     5         kx 		return;
     5         kx 	(void) sprintf(buffer, "%.24s\r\n", ctime(&clock));
     5         kx 	(void) sendto(s, buffer, strlen(buffer), 0, &sa,
     5         kx 	    sizeof(sa));
     5         kx }
     5         kx 
     5         kx /*
     5         kx  * print_service:
     5         kx  *	Dump relevant information to stderr
     5         kx  */
     5         kx void
     5         kx print_service(action, sep)
     5         kx 	char *action;
     5         kx 	struct servtab *sep;
     5         kx {
     5         kx 	if (strcmp(sep->se_hostaddr, "*") == 0)
     5         kx 		fprintf(stderr, "%s: %s ", action, sep->se_service);
     5         kx 	else
     5         kx 		fprintf(stderr, "%s: %s:%s ", action, sep->se_hostaddr,
     5         kx 		    sep->se_service);
     5         kx 
     5         kx 	if (isrpcservice(sep))
     5         kx 		fprintf(stderr, "rpcprog=%d, rpcvers=%d/%d, proto=%s,",
     5         kx 		    sep->se_rpcprog, sep->se_rpcversh,
     5         kx 		    sep->se_rpcversl, sep->se_proto);
     5         kx 	else
     5         kx 		fprintf(stderr, "proto=%s,", sep->se_proto);
     5         kx 
     5         kx 	fprintf(stderr,
     5         kx 	    " wait.max=%hd.%d user:group=%s.%s builtin=%lx server=%s\n",
     5         kx 	    sep->se_wait, sep->se_max, sep->se_user,
     5         kx 	    sep->se_group ? sep->se_group : "wheel",
     5         kx 	    (long)sep->se_bi, sep->se_server);
     5         kx }