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