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