Home | History | Annotate | Line # | Download | only in inetd
inetd.c revision 1.140
      1  1.140  dholland /*	$NetBSD: inetd.c,v 1.140 2022/08/10 03:35:38 dholland 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.140  dholland __RCSID("$NetBSD: inetd.c,v 1.140 2022/08/10 03:35:38 dholland 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.125    martin  *	server program arguments	maximum of MAXARGV (64)
    105    1.1       cgd  *
    106    1.2    brezak  * For RPC services
    107  1.133    rillig  *	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.125    martin  *	server program arguments	maximum of MAXARGV (64)
    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/wait.h>
    191    1.5        pk #include <sys/resource.h>
    192   1.85      tron #include <sys/event.h>
    193  1.127  christos #include <sys/socket.h>
    194  1.137  christos #include <sys/queue.h>
    195  1.127  christos 
    196   1.82    itojun #include <net/if.h>
    197   1.82    itojun 
    198   1.23   mycroft #ifdef RPC
    199   1.23   mycroft #include <rpc/rpc.h>
    200   1.62      fvdl #include <rpc/rpcb_clnt.h>
    201   1.62      fvdl #include <netconfig.h>
    202   1.23   mycroft #endif
    203    1.1       cgd 
    204   1.42   thorpej #include <ctype.h>
    205  1.127  christos #include <err.h>
    206    1.1       cgd #include <errno.h>
    207   1.30   mycroft #include <fcntl.h>
    208  1.127  christos #include <glob.h>
    209   1.23   mycroft #include <grp.h>
    210  1.127  christos #include <libgen.h>
    211    1.1       cgd #include <pwd.h>
    212   1.23   mycroft #include <signal.h>
    213    1.1       cgd #include <stdio.h>
    214    1.9       cgd #include <stdlib.h>
    215    1.1       cgd #include <string.h>
    216   1.23   mycroft #include <syslog.h>
    217   1.23   mycroft #include <unistd.h>
    218   1.48   thorpej #include <util.h>
    219   1.82    itojun #include <ifaddrs.h>
    220   1.23   mycroft 
    221  1.127  christos #include "inetd.h"
    222   1.49    itojun 
    223   1.12       mrg #ifdef LIBWRAP
    224   1.12       mrg # include <tcpd.h>
    225   1.13       mrg #ifndef LIBWRAP_ALLOW_FACILITY
    226   1.17     mouse # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
    227   1.13       mrg #endif
    228   1.13       mrg #ifndef LIBWRAP_ALLOW_SEVERITY
    229   1.13       mrg # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
    230   1.13       mrg #endif
    231   1.13       mrg #ifndef LIBWRAP_DENY_FACILITY
    232   1.17     mouse # define LIBWRAP_DENY_FACILITY LOG_AUTH
    233   1.13       mrg #endif
    234   1.13       mrg #ifndef LIBWRAP_DENY_SEVERITY
    235   1.13       mrg # define LIBWRAP_DENY_SEVERITY LOG_WARNING
    236   1.13       mrg #endif
    237   1.13       mrg int allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
    238   1.13       mrg int deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
    239   1.12       mrg #endif
    240   1.12       mrg 
    241  1.137  christos static bool foreground;
    242   1.12       mrg int	debug;
    243   1.12       mrg #ifdef LIBWRAP
    244   1.12       mrg int	lflag;
    245   1.12       mrg #endif
    246   1.88      tron int	maxsock;
    247   1.85      tron int	kq;
    248    1.1       cgd int	options;
    249    1.1       cgd int	timingout;
    250   1.57    itojun const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
    251    1.5        pk 
    252    1.5        pk #ifndef OPEN_MAX
    253    1.5        pk #define OPEN_MAX	64
    254    1.5        pk #endif
    255    1.5        pk 
    256    1.5        pk /* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */
    257    1.5        pk #define FD_MARGIN	(8)
    258   1.70   mycroft rlim_t		rlim_ofile_cur = OPEN_MAX;
    259    1.5        pk 
    260    1.5        pk struct rlimit	rlim_ofile;
    261    1.1       cgd 
    262   1.87      tron struct kevent	changebuf[64];
    263   1.87      tron size_t		changes;
    264   1.87      tron 
    265  1.127  christos struct servtab *servtab;
    266   1.23   mycroft 
    267  1.139       ryo static ssize_t	recvfromto(int, void * restrict, size_t, int,
    268  1.139       ryo     struct sockaddr * restrict, socklen_t * restrict,
    269  1.139       ryo     struct sockaddr * restrict, socklen_t * restrict);
    270  1.139       ryo static ssize_t	sendfromto(int, const void *, size_t, int,
    271  1.139       ryo     const struct sockaddr *, socklen_t, const struct sockaddr *, socklen_t);
    272   1.87      tron static void	chargen_dg(int, struct servtab *);
    273   1.87      tron static void	chargen_stream(int, struct servtab *);
    274   1.87      tron static void	daytime_dg(int, struct servtab *);
    275   1.87      tron static void	daytime_stream(int, struct servtab *);
    276   1.87      tron static void	discard_dg(int, struct servtab *);
    277   1.87      tron static void	discard_stream(int, struct servtab *);
    278   1.87      tron static void	echo_dg(int, struct servtab *);
    279   1.87      tron static void	echo_stream(int, struct servtab *);
    280  1.119     joerg __dead static void	goaway(void);
    281   1.87      tron static void	machtime_dg(int, struct servtab *);
    282   1.87      tron static void	machtime_stream(int, struct servtab *);
    283   1.87      tron static void	reapchild(void);
    284   1.87      tron static void	retry(void);
    285  1.105  dholland static void	run_service(int, struct servtab *, int);
    286   1.87      tron static void	tcpmux(int, struct servtab *);
    287  1.119     joerg __dead static void	usage(void);
    288   1.87      tron static void	bump_nofile(void);
    289   1.87      tron static void	inetd_setproctitle(char *, int);
    290   1.87      tron static void	initring(void);
    291   1.87      tron static uint32_t	machtime(void);
    292   1.87      tron static int	port_good_dg(struct sockaddr *);
    293  1.127  christos static int	dg_broadcast(struct in_addr *);
    294  1.127  christos static int	my_kevent(const struct kevent *, size_t, struct kevent *, size_t);
    295  1.127  christos static struct kevent	*allocchange(void);
    296  1.113       roy static int	get_line(int, char *, int);
    297   1.87      tron static void	spawn(struct servtab *, int);
    298    1.1       cgd 
    299    1.1       cgd struct biltin {
    300  1.110  christos 	const char *bi_service;		/* internally provided service name */
    301    1.1       cgd 	int	bi_socktype;		/* type of socket supported */
    302    1.1       cgd 	short	bi_fork;		/* 1 if should fork before call */
    303    1.1       cgd 	short	bi_wait;		/* 1 if should wait for child */
    304   1.86      tron 	void	(*bi_fn)(int, struct servtab *);
    305   1.36       mrg 					/* function which performs it */
    306    1.1       cgd } biltins[] = {
    307    1.1       cgd 	/* Echo received data */
    308  1.134    rillig 	{ "echo",	SOCK_STREAM,	true, false,	echo_stream },
    309  1.134    rillig 	{ "echo",	SOCK_DGRAM,	false, false,	echo_dg },
    310    1.1       cgd 
    311    1.1       cgd 	/* Internet /dev/null */
    312  1.134    rillig 	{ "discard",	SOCK_STREAM,	true, false,	discard_stream },
    313  1.134    rillig 	{ "discard",	SOCK_DGRAM,	false, false,	discard_dg },
    314    1.1       cgd 
    315   1.35       mrg 	/* Return 32 bit time since 1970 */
    316  1.134    rillig 	{ "time",	SOCK_STREAM,	false, false,	machtime_stream },
    317  1.134    rillig 	{ "time",	SOCK_DGRAM,	false, false,	machtime_dg },
    318    1.1       cgd 
    319    1.1       cgd 	/* Return human-readable time */
    320  1.134    rillig 	{ "daytime",	SOCK_STREAM,	false, false,	daytime_stream },
    321  1.134    rillig 	{ "daytime",	SOCK_DGRAM,	false, false,	daytime_dg },
    322    1.1       cgd 
    323    1.1       cgd 	/* Familiar character generator */
    324  1.134    rillig 	{ "chargen",	SOCK_STREAM,	true, false,	chargen_stream },
    325  1.134    rillig 	{ "chargen",	SOCK_DGRAM,	false, false,	chargen_dg },
    326   1.23   mycroft 
    327  1.137  christos 	{ "tcpmux",	SOCK_STREAM,	true, false,	tcpmux }
    328    1.1       cgd };
    329    1.1       cgd 
    330   1.47       hwr /* list of "bad" ports. I.e. ports that are most obviously used for
    331   1.47       hwr  * "cycling packets" denial of service attacks. See /etc/services.
    332   1.47       hwr  * List must end with port number "0".
    333   1.47       hwr  */
    334   1.47       hwr 
    335   1.79    itojun u_int16_t bad_ports[] =  { 7, 9, 13, 19, 37, 0 };
    336   1.47       hwr 
    337   1.47       hwr 
    338    1.1       cgd #define NUMINT	(sizeof(intab) / sizeof(struct inent))
    339  1.110  christos const char	*CONFIG = _PATH_INETDCONF;
    340    1.5        pk 
    341   1.88      tron static int my_signals[] =
    342   1.88      tron     { SIGALRM, SIGHUP, SIGCHLD, SIGTERM, SIGINT, SIGPIPE };
    343   1.87      tron 
    344   1.36       mrg int
    345   1.87      tron main(int argc, char *argv[])
    346    1.1       cgd {
    347   1.87      tron 	int		ch, n, reload = 1;
    348    1.1       cgd 
    349   1.12       mrg 	while ((ch = getopt(argc, argv,
    350   1.12       mrg #ifdef LIBWRAP
    351  1.137  christos 					"dfl"
    352   1.12       mrg #else
    353  1.137  christos 					"df"
    354   1.12       mrg #endif
    355   1.38     lukem 					   )) != -1)
    356    1.1       cgd 		switch(ch) {
    357    1.1       cgd 		case 'd':
    358  1.137  christos 			foreground = true;
    359  1.134    rillig 			debug = true;
    360    1.1       cgd 			options |= SO_DEBUG;
    361    1.1       cgd 			break;
    362  1.137  christos 		case 'f':
    363  1.137  christos 			foreground = true;
    364  1.137  christos 			break;
    365   1.12       mrg #ifdef LIBWRAP
    366   1.12       mrg 		case 'l':
    367  1.134    rillig 			lflag = true;
    368   1.12       mrg 			break;
    369   1.12       mrg #endif
    370    1.1       cgd 		case '?':
    371    1.1       cgd 		default:
    372   1.23   mycroft 			usage();
    373    1.1       cgd 		}
    374    1.1       cgd 	argc -= optind;
    375    1.1       cgd 	argv += optind;
    376    1.1       cgd 
    377    1.1       cgd 	if (argc > 0)
    378    1.1       cgd 		CONFIG = argv[0];
    379    1.5        pk 
    380  1.137  christos 	if (!foreground)
    381    1.1       cgd 		daemon(0, 0);
    382   1.72     lukem 	openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
    383   1.48   thorpej 	pidfile(NULL);
    384    1.5        pk 
    385   1.85      tron 	kq = kqueue();
    386   1.85      tron 	if (kq < 0) {
    387   1.85      tron 		syslog(LOG_ERR, "kqueue: %m");
    388   1.85      tron 		return (EXIT_FAILURE);
    389   1.85      tron 	}
    390   1.85      tron 
    391    1.6        pk 	if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
    392    1.5        pk 		syslog(LOG_ERR, "getrlimit: %m");
    393    1.5        pk 	} else {
    394    1.5        pk 		rlim_ofile_cur = rlim_ofile.rlim_cur;
    395    1.6        pk 		if (rlim_ofile_cur == RLIM_INFINITY)	/* ! */
    396    1.6        pk 			rlim_ofile_cur = OPEN_MAX;
    397    1.5        pk 	}
    398    1.5        pk 
    399  1.127  christos 	for (n = 0; n < (int)__arraycount(my_signals); n++) {
    400   1.88      tron 		int	signum;
    401   1.88      tron 
    402   1.88      tron 		signum = my_signals[n];
    403  1.100  christos 		if (signum != SIGCHLD)
    404  1.100  christos 			(void) signal(signum, SIG_IGN);
    405   1.88      tron 
    406   1.88      tron 		if (signum != SIGPIPE) {
    407   1.88      tron 			struct kevent	*ev;
    408   1.88      tron 
    409   1.88      tron 			ev = allocchange();
    410   1.88      tron 			EV_SET(ev, signum, EVFILT_SIGNAL, EV_ADD | EV_ENABLE,
    411   1.94      fvdl 			    0, 0, 0);
    412   1.88      tron 		}
    413   1.87      tron 	}
    414    1.1       cgd 
    415    1.1       cgd 	for (;;) {
    416   1.87      tron 		int		ctrl;
    417   1.88      tron 		struct kevent	eventbuf[64], *ev;
    418   1.87      tron 		struct servtab	*sep;
    419   1.87      tron 
    420   1.87      tron 		if (reload) {
    421  1.134    rillig 			reload = false;
    422  1.127  christos 			config_root();
    423   1.87      tron 		}
    424    1.1       cgd 
    425  1.127  christos 		n = my_kevent(changebuf, changes, eventbuf, __arraycount(eventbuf));
    426   1.87      tron 		changes = 0;
    427   1.87      tron 
    428   1.87      tron 		for (ev = eventbuf; n > 0; ev++, n--) {
    429   1.87      tron 			if (ev->filter == EVFILT_SIGNAL) {
    430   1.87      tron 				switch (ev->ident) {
    431   1.87      tron 				case SIGALRM:
    432   1.87      tron 					retry();
    433   1.87      tron 					break;
    434   1.87      tron 				case SIGCHLD:
    435   1.87      tron 					reapchild();
    436   1.87      tron 					break;
    437   1.87      tron 				case SIGTERM:
    438   1.87      tron 				case SIGINT:
    439   1.87      tron 					goaway();
    440   1.87      tron 					break;
    441   1.87      tron 				case SIGHUP:
    442  1.134    rillig 					reload = true;
    443   1.87      tron 					break;
    444   1.87      tron 				}
    445   1.85      tron 				continue;
    446   1.87      tron 			}
    447   1.87      tron 			if (ev->filter != EVFILT_READ)
    448   1.87      tron 				continue;
    449   1.87      tron 			sep = (struct servtab *)ev->udata;
    450   1.85      tron 			/* Paranoia */
    451  1.102    rillig 			if ((int)ev->ident != sep->se_fd)
    452   1.79    itojun 				continue;
    453  1.127  christos 			DPRINTF(SERV_FMT ": service requested" , SERV_PARAMS(sep));
    454  1.134    rillig 			if (sep->se_wait == 0 && sep->se_socktype == SOCK_STREAM) {
    455   1.96    rumble 				/* XXX here do the libwrap check-before-accept*/
    456   1.85      tron 				ctrl = accept(sep->se_fd, NULL, NULL);
    457  1.130    rillig 				DPRINTF(SERV_FMT ": accept, ctrl fd %d",
    458  1.127  christos 				    SERV_PARAMS(sep), ctrl);
    459   1.79    itojun 				if (ctrl < 0) {
    460   1.79    itojun 					if (errno != EINTR)
    461   1.79    itojun 						syslog(LOG_WARNING,
    462  1.127  christos 						    SERV_FMT ": accept: %m",
    463  1.127  christos 						    SERV_PARAMS(sep));
    464   1.79    itojun 					continue;
    465   1.79    itojun 				}
    466   1.79    itojun 			} else
    467   1.79    itojun 				ctrl = sep->se_fd;
    468   1.79    itojun 			spawn(sep, ctrl);
    469   1.79    itojun 		}
    470   1.79    itojun 	}
    471   1.79    itojun }
    472   1.79    itojun 
    473   1.87      tron static void
    474   1.87      tron spawn(struct servtab *sep, int ctrl)
    475   1.79    itojun {
    476   1.79    itojun 	int dofork;
    477   1.79    itojun 	pid_t pid;
    478   1.79    itojun 
    479   1.79    itojun 	pid = 0;
    480   1.28   mycroft #ifdef LIBWRAP_INTERNAL
    481  1.134    rillig 	dofork = true;
    482   1.26   mycroft #else
    483  1.134    rillig 	dofork = (sep->se_bi == NULL || sep->se_bi->bi_fork);
    484   1.26   mycroft #endif
    485   1.79    itojun 	if (dofork) {
    486  1.127  christos 		if (rl_process(sep, ctrl)) {
    487  1.127  christos 			return;
    488    1.5        pk 		}
    489   1.79    itojun 		pid = fork();
    490   1.79    itojun 		if (pid < 0) {
    491   1.79    itojun 			syslog(LOG_ERR, "fork: %m");
    492  1.134    rillig 			if (sep->se_wait == 0 && sep->se_socktype == SOCK_STREAM)
    493   1.79    itojun 				close(ctrl);
    494   1.79    itojun 			sleep(1);
    495   1.79    itojun 			return;
    496   1.79    itojun 		}
    497  1.134    rillig 		if (pid != 0 && sep->se_wait != 0) {
    498   1.87      tron 			struct kevent	*ev;
    499   1.85      tron 
    500   1.79    itojun 			sep->se_wait = pid;
    501   1.87      tron 			ev = allocchange();
    502   1.87      tron 			EV_SET(ev, sep->se_fd, EVFILT_READ,
    503   1.85      tron 			    EV_DELETE, 0, 0, 0);
    504   1.79    itojun 		}
    505    1.5        pk 		if (pid == 0) {
    506  1.102    rillig 			size_t	n;
    507   1.88      tron 
    508  1.127  christos 			for (n = 0; n < __arraycount(my_signals); n++)
    509   1.88      tron 				(void) signal(my_signals[n], SIG_DFL);
    510  1.137  christos 			/* Don't put services in terminal session */
    511  1.137  christos 			if (foreground)
    512   1.79    itojun 				setsid();
    513   1.30   mycroft 		}
    514   1.30   mycroft 	}
    515   1.79    itojun 	if (pid == 0) {
    516  1.105  dholland 		run_service(ctrl, sep, dofork);
    517   1.79    itojun 		if (dofork)
    518  1.137  christos 			exit(EXIT_SUCCESS);
    519   1.79    itojun 	}
    520  1.134    rillig 	if (sep->se_wait == 0 && sep->se_socktype == SOCK_STREAM)
    521   1.79    itojun 		close(ctrl);
    522   1.30   mycroft }
    523   1.30   mycroft 
    524   1.87      tron static void
    525  1.105  dholland run_service(int ctrl, struct servtab *sep, int didfork)
    526   1.30   mycroft {
    527   1.30   mycroft 	struct passwd *pwd;
    528   1.36       mrg 	struct group *grp = NULL;	/* XXX gcc */
    529   1.57    itojun 	char buf[NI_MAXSERV];
    530  1.105  dholland 	struct servtab *s;
    531   1.30   mycroft #ifdef LIBWRAP
    532  1.109  christos 	char abuf[BUFSIZ];
    533   1.30   mycroft 	struct request_info req;
    534   1.30   mycroft 	int denied;
    535   1.40  christos 	char *service = NULL;	/* XXX gcc */
    536   1.30   mycroft #endif
    537   1.30   mycroft 
    538   1.20   mycroft #ifdef LIBWRAP
    539   1.28   mycroft #ifndef LIBWRAP_INTERNAL
    540   1.30   mycroft 	if (sep->se_bi == 0)
    541   1.28   mycroft #endif
    542  1.134    rillig 	if (sep->se_wait == 0 && sep->se_socktype == SOCK_STREAM) {
    543  1.134    rillig 		request_init(&req, RQ_DAEMON, sep->se_argv[0] != NULL ?
    544   1.30   mycroft 		    sep->se_argv[0] : sep->se_service, RQ_FILE, ctrl, NULL);
    545   1.30   mycroft 		fromhost(&req);
    546  1.134    rillig 		denied = hosts_access(&req) == 0;
    547   1.30   mycroft 		if (denied || lflag) {
    548   1.52    itojun 			if (getnameinfo(&sep->se_ctrladdr,
    549  1.110  christos 			    (socklen_t)sep->se_ctrladdr.sa_len, NULL, 0,
    550  1.110  christos 			    buf, sizeof(buf), 0) != 0) {
    551   1.52    itojun 				/* shouldn't happen */
    552   1.30   mycroft 				(void)snprintf(buf, sizeof buf, "%d",
    553   1.30   mycroft 				    ntohs(sep->se_ctrladdr_in.sin_port));
    554   1.52    itojun 			}
    555   1.52    itojun 			service = buf;
    556  1.134    rillig 			if (req.client->sin != NULL) {
    557  1.116  dholland 				sockaddr_snprintf(abuf, sizeof(abuf), "%a",
    558  1.116  dholland 				    req.client->sin);
    559  1.116  dholland 			} else {
    560  1.116  dholland 				strcpy(abuf, "(null)");
    561  1.116  dholland 			}
    562   1.30   mycroft 		}
    563   1.30   mycroft 		if (denied) {
    564   1.30   mycroft 			syslog(deny_severity,
    565  1.109  christos 			    "refused connection from %.500s(%s), service %s (%s)",
    566  1.109  christos 			    eval_client(&req), abuf, service, sep->se_proto);
    567   1.30   mycroft 			goto reject;
    568   1.30   mycroft 		}
    569   1.30   mycroft 		if (lflag) {
    570   1.30   mycroft 			syslog(allow_severity,
    571  1.109  christos 			    "connection from %.500s(%s), service %s (%s)",
    572  1.109  christos 			    eval_client(&req), abuf, service, sep->se_proto);
    573   1.30   mycroft 		}
    574   1.30   mycroft 	}
    575   1.30   mycroft #endif /* LIBWRAP */
    576   1.30   mycroft 
    577  1.134    rillig 	if (sep->se_bi != NULL) {
    578  1.105  dholland 		if (didfork) {
    579  1.134    rillig 			for (s = servtab; s != NULL; s = s->se_next)
    580  1.110  christos 				if (s->se_fd != -1 && s->se_fd != ctrl) {
    581  1.105  dholland 					close(s->se_fd);
    582  1.110  christos 					s->se_fd = -1;
    583  1.110  christos 				}
    584  1.105  dholland 		}
    585   1.30   mycroft 		(*sep->se_bi->bi_fn)(ctrl, sep);
    586   1.30   mycroft 	} else {
    587   1.30   mycroft 		if ((pwd = getpwnam(sep->se_user)) == NULL) {
    588   1.30   mycroft 			syslog(LOG_ERR, "%s/%s: %s: No such user",
    589   1.30   mycroft 			    sep->se_service, sep->se_proto, sep->se_user);
    590   1.30   mycroft 			goto reject;
    591   1.30   mycroft 		}
    592  1.134    rillig 		if (sep->se_group != NULL &&
    593   1.30   mycroft 		    (grp = getgrnam(sep->se_group)) == NULL) {
    594   1.30   mycroft 			syslog(LOG_ERR, "%s/%s: %s: No such group",
    595   1.30   mycroft 			    sep->se_service, sep->se_proto, sep->se_group);
    596   1.30   mycroft 			goto reject;
    597   1.30   mycroft 		}
    598  1.134    rillig 		if (pwd->pw_uid != 0) {
    599  1.134    rillig 			if (sep->se_group != NULL)
    600   1.30   mycroft 				pwd->pw_gid = grp->gr_gid;
    601   1.30   mycroft 			if (setgid(pwd->pw_gid) < 0) {
    602   1.30   mycroft 				syslog(LOG_ERR,
    603   1.30   mycroft 				 "%s/%s: can't set gid %d: %m", sep->se_service,
    604   1.30   mycroft 				    sep->se_proto, pwd->pw_gid);
    605   1.30   mycroft 				goto reject;
    606   1.20   mycroft 			}
    607   1.30   mycroft 			(void) initgroups(pwd->pw_name,
    608   1.30   mycroft 			    pwd->pw_gid);
    609   1.30   mycroft 			if (setuid(pwd->pw_uid) < 0) {
    610   1.30   mycroft 				syslog(LOG_ERR,
    611   1.30   mycroft 				 "%s/%s: can't set uid %d: %m", sep->se_service,
    612   1.30   mycroft 				    sep->se_proto, pwd->pw_uid);
    613   1.22   mycroft 				goto reject;
    614   1.20   mycroft 			}
    615  1.134    rillig 		} else if (sep->se_group != NULL) {
    616   1.30   mycroft 			(void) setgid((gid_t)grp->gr_gid);
    617   1.30   mycroft 		}
    618  1.130    rillig 		DPRINTF("%d execl %s",
    619  1.127  christos 		    getpid(), sep->se_server);
    620   1.30   mycroft 		/* Set our control descriptor to not close-on-exec... */
    621   1.30   mycroft 		if (fcntl(ctrl, F_SETFD, 0) < 0)
    622  1.110  christos 			syslog(LOG_ERR, "fcntl (%d, F_SETFD, 0): %m", ctrl);
    623   1.30   mycroft 		/* ...and dup it to stdin, stdout, and stderr. */
    624   1.30   mycroft 		if (ctrl != 0) {
    625   1.30   mycroft 			dup2(ctrl, 0);
    626   1.30   mycroft 			close(ctrl);
    627   1.30   mycroft 			ctrl = 0;
    628   1.30   mycroft 		}
    629   1.30   mycroft 		dup2(0, 1);
    630   1.30   mycroft 		dup2(0, 2);
    631   1.30   mycroft 		if (rlim_ofile.rlim_cur != rlim_ofile_cur &&
    632   1.30   mycroft 		    setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0)
    633   1.30   mycroft 			syslog(LOG_ERR, "setrlimit: %m");
    634   1.30   mycroft 		execv(sep->se_server, sep->se_argv);
    635   1.30   mycroft 		syslog(LOG_ERR, "cannot execute %s: %m", sep->se_server);
    636   1.30   mycroft 	reject:
    637   1.30   mycroft 		if (sep->se_socktype != SOCK_STREAM)
    638   1.30   mycroft 			recv(ctrl, buf, sizeof (buf), 0);
    639  1.127  christos 		_exit(EXIT_FAILURE);
    640    1.1       cgd 	}
    641    1.1       cgd }
    642    1.1       cgd 
    643   1.87      tron static void
    644   1.87      tron reapchild(void)
    645    1.1       cgd {
    646    1.1       cgd 	int status;
    647   1.23   mycroft 	pid_t pid;
    648   1.23   mycroft 	struct servtab *sep;
    649    1.1       cgd 
    650    1.1       cgd 	for (;;) {
    651   1.85      tron 		pid = wait3(&status, WNOHANG, NULL);
    652    1.1       cgd 		if (pid <= 0)
    653    1.1       cgd 			break;
    654  1.127  christos 		DPRINTF("%d reaped, status %#x", pid, status);
    655   1.85      tron 		for (sep = servtab; sep != NULL; sep = sep->se_next)
    656    1.1       cgd 			if (sep->se_wait == pid) {
    657   1.87      tron 				struct kevent	*ev;
    658   1.85      tron 
    659    1.5        pk 				if (WIFEXITED(status) && WEXITSTATUS(status))
    660    1.1       cgd 					syslog(LOG_WARNING,
    661  1.122   khorben 					    "%s: exit status %u",
    662    1.5        pk 					    sep->se_server, WEXITSTATUS(status));
    663    1.5        pk 				else if (WIFSIGNALED(status))
    664    1.5        pk 					syslog(LOG_WARNING,
    665  1.122   khorben 					    "%s: exit signal %u",
    666    1.5        pk 					    sep->se_server, WTERMSIG(status));
    667    1.5        pk 				sep->se_wait = 1;
    668   1.87      tron 				ev = allocchange();
    669   1.87      tron 				EV_SET(ev, sep->se_fd, EVFILT_READ,
    670   1.85      tron 				    EV_ADD | EV_ENABLE, 0, 0, (intptr_t)sep);
    671  1.127  christos 				DPRINTF("restored %s, fd %d",
    672  1.127  christos 				    sep->se_service, sep->se_fd);
    673    1.1       cgd 			}
    674    1.1       cgd 	}
    675    1.1       cgd }
    676    1.1       cgd 
    677   1.87      tron static void
    678   1.87      tron retry(void)
    679    1.5        pk {
    680   1.23   mycroft 	struct servtab *sep;
    681    1.2    brezak 
    682  1.134    rillig 	timingout = false;
    683   1.85      tron 	for (sep = servtab; sep != NULL; sep = sep->se_next) {
    684   1.23   mycroft 		if (sep->se_fd == -1 && !ISMUX(sep)) {
    685    1.5        pk 			switch (sep->se_family) {
    686   1.44     lukem 			case AF_LOCAL:
    687    1.5        pk 			case AF_INET:
    688   1.58    itojun #ifdef INET6
    689   1.49    itojun 			case AF_INET6:
    690   1.58    itojun #endif
    691    1.5        pk 				setup(sep);
    692  1.110  christos 				if (sep->se_fd >= 0 && isrpcservice(sep))
    693    1.5        pk 					register_rpc(sep);
    694    1.5        pk 				break;
    695    1.5        pk 			}
    696    1.5        pk 		}
    697    1.5        pk 	}
    698    1.2    brezak }
    699    1.2    brezak 
    700   1.87      tron static void
    701   1.87      tron goaway(void)
    702    1.1       cgd {
    703   1.36       mrg 	struct servtab *sep;
    704    1.1       cgd 
    705   1.85      tron 	for (sep = servtab; sep != NULL; sep = sep->se_next) {
    706    1.1       cgd 		if (sep->se_fd == -1)
    707    1.5        pk 			continue;
    708  1.130    rillig 
    709    1.5        pk 		switch (sep->se_family) {
    710   1.44     lukem 		case AF_LOCAL:
    711    1.5        pk 			(void)unlink(sep->se_service);
    712    1.5        pk 			break;
    713    1.5        pk 		case AF_INET:
    714   1.58    itojun #ifdef INET6
    715   1.49    itojun 		case AF_INET6:
    716   1.58    itojun #endif
    717    1.5        pk 			if (sep->se_wait == 1 && isrpcservice(sep))
    718    1.5        pk 				unregister_rpc(sep);
    719    1.5        pk 			break;
    720    1.5        pk 		}
    721    1.5        pk 		(void)close(sep->se_fd);
    722  1.110  christos 		sep->se_fd = -1;
    723    1.5        pk 	}
    724  1.137  christos 
    725  1.137  christos 	DPRINTF("Going away.");
    726  1.137  christos 
    727  1.137  christos 	exit(EXIT_SUCCESS);
    728    1.1       cgd }
    729    1.1       cgd 
    730  1.137  christos void
    731   1.87      tron setup(struct servtab *sep)
    732    1.1       cgd {
    733   1.97        pk 	int		on = 1;
    734   1.97        pk #ifdef INET6
    735   1.97        pk 	int		off = 0;
    736   1.97        pk #endif
    737   1.87      tron 	struct kevent	*ev;
    738    1.1       cgd 
    739    1.5        pk 	if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) {
    740  1.130    rillig 		DPRINTF("socket failed on " SERV_FMT ": %s",
    741  1.127  christos 		    SERV_PARAMS(sep), strerror(errno));
    742    1.1       cgd 		syslog(LOG_ERR, "%s/%s: socket: %m",
    743    1.1       cgd 		    sep->se_service, sep->se_proto);
    744    1.1       cgd 		return;
    745    1.1       cgd 	}
    746   1.46   mycroft 	/* Set all listening sockets to close-on-exec. */
    747   1.84   mycroft 	if (fcntl(sep->se_fd, F_SETFD, FD_CLOEXEC) < 0) {
    748  1.127  christos 		syslog(LOG_ERR, SERV_FMT ": fcntl(F_SETFD, FD_CLOEXEC): %m",
    749  1.127  christos 		    SERV_PARAMS(sep));
    750   1.84   mycroft 		close(sep->se_fd);
    751  1.110  christos 		sep->se_fd = -1;
    752   1.84   mycroft 		return;
    753   1.84   mycroft 	}
    754   1.42   thorpej 
    755    1.1       cgd #define	turnon(fd, opt) \
    756  1.110  christos setsockopt(fd, SOL_SOCKET, opt, &on, (socklen_t)sizeof(on))
    757    1.1       cgd 	if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) &&
    758    1.1       cgd 	    turnon(sep->se_fd, SO_DEBUG) < 0)
    759    1.1       cgd 		syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
    760    1.1       cgd 	if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
    761    1.1       cgd 		syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
    762    1.1       cgd #undef turnon
    763   1.42   thorpej 
    764   1.42   thorpej 	/* Set the socket buffer sizes, if specified. */
    765   1.42   thorpej 	if (sep->se_sndbuf != 0 && setsockopt(sep->se_fd, SOL_SOCKET,
    766  1.110  christos 	    SO_SNDBUF, &sep->se_sndbuf, (socklen_t)sizeof(sep->se_sndbuf)) < 0)
    767   1.42   thorpej 		syslog(LOG_ERR, "setsockopt (SO_SNDBUF %d): %m",
    768   1.42   thorpej 		    sep->se_sndbuf);
    769   1.42   thorpej 	if (sep->se_rcvbuf != 0 && setsockopt(sep->se_fd, SOL_SOCKET,
    770  1.110  christos 	    SO_RCVBUF, &sep->se_rcvbuf, (socklen_t)sizeof(sep->se_rcvbuf)) < 0)
    771   1.42   thorpej 		syslog(LOG_ERR, "setsockopt (SO_RCVBUF %d): %m",
    772   1.42   thorpej 		    sep->se_rcvbuf);
    773   1.90    itojun #ifdef INET6
    774   1.90    itojun 	if (sep->se_family == AF_INET6) {
    775   1.90    itojun 		int *v;
    776   1.90    itojun 		v = (sep->se_type == FAITH_TYPE) ? &on : &off;
    777   1.90    itojun 		if (setsockopt(sep->se_fd, IPPROTO_IPV6, IPV6_FAITH,
    778  1.110  christos 		    v, (socklen_t)sizeof(*v)) < 0)
    779   1.90    itojun 			syslog(LOG_ERR, "setsockopt (IPV6_FAITH): %m");
    780   1.90    itojun 	}
    781   1.90    itojun #endif
    782   1.49    itojun #ifdef IPSEC
    783  1.124     ozaki 	/* Avoid setting a policy if a policy specifier doesn't exist. */
    784  1.124     ozaki 	if (sep->se_policy != NULL) {
    785  1.124     ozaki 		int e = ipsecsetup(sep->se_family, sep->se_fd, sep->se_policy);
    786  1.124     ozaki 		if (e < 0) {
    787  1.127  christos 			syslog(LOG_ERR, SERV_FMT ": ipsec setup failed",
    788  1.127  christos 			    SERV_PARAMS(sep));
    789  1.124     ozaki 			(void)close(sep->se_fd);
    790  1.124     ozaki 			sep->se_fd = -1;
    791  1.124     ozaki 			return;
    792  1.124     ozaki 		}
    793   1.50    itojun 	}
    794   1.49    itojun #endif
    795   1.42   thorpej 
    796  1.127  christos 	if (bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size) < 0) {
    797  1.127  christos 		DPRINTF(SERV_FMT ": bind failed: %s",
    798  1.127  christos 			SERV_PARAMS(sep), strerror(errno));
    799  1.127  christos 		syslog(LOG_ERR, SERV_FMT ": bind: %m",
    800  1.127  christos 		    SERV_PARAMS(sep));
    801    1.1       cgd 		(void) close(sep->se_fd);
    802    1.1       cgd 		sep->se_fd = -1;
    803    1.1       cgd 		if (!timingout) {
    804  1.134    rillig 			timingout = true;
    805    1.1       cgd 			alarm(RETRYTIME);
    806    1.1       cgd 		}
    807    1.1       cgd 		return;
    808    1.1       cgd 	}
    809    1.1       cgd 	if (sep->se_socktype == SOCK_STREAM)
    810    1.1       cgd 		listen(sep->se_fd, 10);
    811    1.5        pk 
    812  1.139       ryo 	/* for internal dgram, setsockopt() is required for recvfromto() */
    813  1.139       ryo 	if (sep->se_socktype == SOCK_DGRAM && sep->se_bi != NULL) {
    814  1.139       ryo 		switch (sep->se_family) {
    815  1.139       ryo 		case AF_INET:
    816  1.139       ryo 			if (setsockopt(sep->se_fd, IPPROTO_IP,
    817  1.139       ryo 			    IP_RECVDSTADDR, &on, sizeof(on)) < 0)
    818  1.139       ryo 				syslog(LOG_ERR,
    819  1.139       ryo 				    "setsockopt (IP_RECVDSTADDR): %m");
    820  1.139       ryo 			break;
    821  1.139       ryo #ifdef INET6
    822  1.139       ryo 		case AF_INET6:
    823  1.139       ryo 			if (setsockopt(sep->se_fd, IPPROTO_IPV6,
    824  1.139       ryo 			    IPV6_RECVPKTINFO, &on, sizeof(on)) < 0)
    825  1.139       ryo 				syslog(LOG_ERR,
    826  1.139       ryo 				    "setsockopt (IPV6_RECVPKTINFO): %m");
    827  1.139       ryo 			break;
    828  1.139       ryo #endif
    829  1.139       ryo 		}
    830  1.139       ryo 	}
    831  1.139       ryo 
    832  1.107       tls 	/* Set the accept filter, if specified. To be done after listen.*/
    833  1.107       tls 	if (sep->se_accf.af_name[0] != 0 && setsockopt(sep->se_fd, SOL_SOCKET,
    834  1.110  christos 	    SO_ACCEPTFILTER, &sep->se_accf,
    835  1.110  christos 	    (socklen_t)sizeof(sep->se_accf)) < 0)
    836  1.107       tls 		syslog(LOG_ERR, "setsockopt(SO_ACCEPTFILTER %s): %m",
    837  1.107       tls 		    sep->se_accf.af_name);
    838  1.107       tls 
    839   1.87      tron 	ev = allocchange();
    840   1.87      tron 	EV_SET(ev, sep->se_fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0,
    841   1.85      tron 	    (intptr_t)sep);
    842    1.5        pk 	if (sep->se_fd > maxsock) {
    843    1.1       cgd 		maxsock = sep->se_fd;
    844  1.111     lukem 		if (maxsock > (int)(rlim_ofile_cur - FD_MARGIN))
    845    1.5        pk 			bump_nofile();
    846    1.5        pk 	}
    847  1.127  christos 	DPRINTF(SERV_FMT ": registered on fd %d", SERV_PARAMS(sep), sep->se_fd);
    848   1.23   mycroft }
    849   1.23   mycroft 
    850   1.23   mycroft /*
    851   1.23   mycroft  * Finish with a service and its socket.
    852   1.23   mycroft  */
    853  1.137  christos void
    854   1.87      tron close_sep(struct servtab *sep)
    855  1.130    rillig {
    856  1.127  christos 
    857   1.23   mycroft 	if (sep->se_fd >= 0) {
    858   1.23   mycroft 		(void) close(sep->se_fd);
    859   1.23   mycroft 		sep->se_fd = -1;
    860   1.23   mycroft 	}
    861   1.23   mycroft 	sep->se_count = 0;
    862  1.127  christos 	if (sep->se_ip_max != SERVTAB_UNSPEC_SIZE_T) {
    863  1.137  christos 		rl_clear_ip_list(sep);
    864  1.127  christos 	}
    865    1.5        pk }
    866    1.5        pk 
    867  1.137  christos void
    868   1.87      tron register_rpc(struct servtab *sep)
    869    1.5        pk {
    870    1.5        pk #ifdef RPC
    871   1.62      fvdl 	struct netbuf nbuf;
    872   1.49    itojun 	struct sockaddr_storage ss;
    873   1.62      fvdl 	struct netconfig *nconf;
    874  1.102    rillig 	socklen_t socklen;
    875  1.102    rillig 	int n;
    876    1.5        pk 
    877   1.62      fvdl 	if ((nconf = getnetconfigent(sep->se_proto+4)) == NULL) {
    878   1.62      fvdl 		syslog(LOG_ERR, "%s: getnetconfigent failed",
    879    1.5        pk 		    sep->se_proto);
    880    1.5        pk 		return;
    881    1.5        pk 	}
    882  1.102    rillig 	socklen = sizeof ss;
    883  1.110  christos 	if (getsockname(sep->se_fd, (struct sockaddr *)(void *)&ss, &socklen) < 0) {
    884  1.127  christos 		syslog(LOG_ERR, SERV_FMT ": getsockname: %m",
    885  1.127  christos 		    SERV_PARAMS(sep));
    886    1.5        pk 		return;
    887    1.5        pk 	}
    888    1.5        pk 
    889   1.62      fvdl 	nbuf.buf = &ss;
    890   1.62      fvdl 	nbuf.len = ss.ss_len;
    891   1.62      fvdl 	nbuf.maxlen = sizeof (struct sockaddr_storage);
    892    1.5        pk 	for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
    893  1.127  christos 		DPRINTF("rpcb_set: %u %d %s %s",
    894  1.127  christos 		    sep->se_rpcprog, n, nconf->nc_netid,
    895  1.127  christos 		    taddr2uaddr(nconf, &nbuf));
    896  1.110  christos 		(void)rpcb_unset((unsigned int)sep->se_rpcprog, (unsigned int)n, nconf);
    897  1.134    rillig 		if (rpcb_set((unsigned int)sep->se_rpcprog, (unsigned int)n, nconf, &nbuf) == 0)
    898   1.62      fvdl 			syslog(LOG_ERR, "rpcb_set: %u %d %s %s%s",
    899   1.62      fvdl 			    sep->se_rpcprog, n, nconf->nc_netid,
    900   1.62      fvdl 			    taddr2uaddr(nconf, &nbuf), clnt_spcreateerror(""));
    901    1.5        pk 	}
    902    1.5        pk #endif /* RPC */
    903    1.5        pk }
    904    1.5        pk 
    905  1.137  christos void
    906   1.87      tron unregister_rpc(struct servtab *sep)
    907    1.5        pk {
    908    1.5        pk #ifdef RPC
    909    1.5        pk 	int n;
    910   1.62      fvdl 	struct netconfig *nconf;
    911   1.62      fvdl 
    912   1.62      fvdl 	if ((nconf = getnetconfigent(sep->se_proto+4)) == NULL) {
    913   1.62      fvdl 		syslog(LOG_ERR, "%s: getnetconfigent failed",
    914   1.62      fvdl 		    sep->se_proto);
    915   1.62      fvdl 		return;
    916   1.62      fvdl 	}
    917    1.5        pk 
    918    1.5        pk 	for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
    919  1.127  christos 		DPRINTF("rpcb_unset(%u, %d, %s)",
    920  1.127  christos 		    sep->se_rpcprog, n, nconf->nc_netid);
    921  1.134    rillig 		if (rpcb_unset((unsigned int)sep->se_rpcprog, (unsigned int)n, nconf) == 0)
    922   1.62      fvdl 			syslog(LOG_ERR, "rpcb_unset(%u, %d, %s) failed\n",
    923   1.62      fvdl 			    sep->se_rpcprog, n, nconf->nc_netid);
    924    1.5        pk 	}
    925    1.5        pk #endif /* RPC */
    926    1.1       cgd }
    927    1.1       cgd 
    928   1.87      tron static void
    929   1.87      tron inetd_setproctitle(char *a, int s)
    930    1.1       cgd {
    931   1.77  christos 	socklen_t size;
    932   1.49    itojun 	struct sockaddr_storage ss;
    933  1.110  christos 	char hbuf[NI_MAXHOST];
    934  1.110  christos 	const char *hp;
    935  1.110  christos 	struct sockaddr *sa;
    936    1.1       cgd 
    937   1.49    itojun 	size = sizeof(ss);
    938  1.110  christos 	sa = (struct sockaddr *)(void *)&ss;
    939  1.110  christos 	if (getpeername(s, sa, &size) == 0) {
    940  1.110  christos 		if (getnameinfo(sa, size, hbuf, (socklen_t)sizeof(hbuf), NULL,
    941  1.110  christos 		    0, niflags) != 0)
    942   1.99  christos 			hp = "?";
    943  1.110  christos 		else
    944  1.110  christos 			hp = hbuf;
    945   1.99  christos 		setproctitle("-%s [%s]", a, hp);
    946   1.49    itojun 	} else
    947   1.77  christos 		setproctitle("-%s", a);
    948    1.5        pk }
    949    1.5        pk 
    950   1.87      tron static void
    951   1.87      tron bump_nofile(void)
    952    1.5        pk {
    953    1.5        pk #define FD_CHUNK	32
    954    1.5        pk 	struct rlimit rl;
    955    1.5        pk 
    956    1.6        pk 	if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
    957    1.5        pk 		syslog(LOG_ERR, "getrlimit: %m");
    958   1.36       mrg 		return;
    959    1.5        pk 	}
    960    1.5        pk 	rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK);
    961    1.5        pk 	if (rl.rlim_cur <= rlim_ofile_cur) {
    962    1.5        pk 		syslog(LOG_ERR,
    963   1.23   mycroft 		    "bump_nofile: cannot extend file limit, max = %d",
    964   1.36       mrg 		    (int)rl.rlim_cur);
    965   1.36       mrg 		return;
    966    1.5        pk 	}
    967    1.5        pk 
    968    1.6        pk 	if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
    969    1.5        pk 		syslog(LOG_ERR, "setrlimit: %m");
    970   1.36       mrg 		return;
    971    1.5        pk 	}
    972    1.5        pk 
    973    1.5        pk 	rlim_ofile_cur = rl.rlim_cur;
    974   1.36       mrg 	return;
    975    1.5        pk }
    976    1.5        pk 
    977    1.1       cgd /*
    978  1.139       ryo  * In order to get the destination address (`to') with recvfromto(),
    979  1.139       ryo  * IP_RECVDSTADDR or IP_RECVPKTINFO for AF_INET, or IPV6_RECVPKTINFO
    980  1.139       ryo  * for AF_INET6, must be enabled with setsockopt(2).
    981  1.139       ryo  *
    982  1.139       ryo  * .sin_port and .sin6_port in 'to' are always stored as zero.
    983  1.139       ryo  * If necessary, extract them using getsockname(2).
    984  1.139       ryo  */
    985  1.139       ryo static ssize_t
    986  1.139       ryo recvfromto(int s, void * restrict buf, size_t len, int flags,
    987  1.139       ryo     struct sockaddr * restrict from, socklen_t * restrict fromlen,
    988  1.139       ryo     struct sockaddr * restrict to, socklen_t * restrict tolen)
    989  1.139       ryo {
    990  1.139       ryo 	struct msghdr msg;
    991  1.139       ryo 	struct iovec vec;
    992  1.139       ryo 	struct cmsghdr *cmsg;
    993  1.139       ryo 	struct sockaddr_storage ss;
    994  1.139       ryo 	char cmsgbuf[1024];
    995  1.139       ryo 	ssize_t rc;
    996  1.139       ryo 
    997  1.139       ryo 	if (to == NULL)
    998  1.139       ryo 		return recvfrom(s, buf, len, flags, from, fromlen);
    999  1.139       ryo 
   1000  1.139       ryo 	if (tolen == NULL || fromlen == NULL) {
   1001  1.139       ryo 		errno = EFAULT;
   1002  1.139       ryo 		return -1;
   1003  1.139       ryo 	}
   1004  1.139       ryo 
   1005  1.139       ryo 	vec.iov_base = buf;
   1006  1.139       ryo 	vec.iov_len = len;
   1007  1.139       ryo 	msg.msg_name = from;
   1008  1.139       ryo 	msg.msg_namelen = *fromlen;
   1009  1.139       ryo 	msg.msg_iov = &vec;
   1010  1.139       ryo 	msg.msg_iovlen = 1;
   1011  1.139       ryo 	msg.msg_control = cmsgbuf;
   1012  1.139       ryo 	msg.msg_controllen = sizeof(cmsgbuf);
   1013  1.139       ryo 
   1014  1.139       ryo 	rc = recvmsg(s, &msg, flags);
   1015  1.139       ryo 	if (rc < 0)
   1016  1.139       ryo 		return rc;
   1017  1.139       ryo 	*fromlen = msg.msg_namelen;
   1018  1.139       ryo 
   1019  1.139       ryo 	memset(&ss, 0, sizeof(ss));
   1020  1.139       ryo 	for (cmsg = (struct cmsghdr *)CMSG_FIRSTHDR(&msg); cmsg != NULL;
   1021  1.139       ryo 	    cmsg = (struct cmsghdr *)CMSG_NXTHDR(&msg, cmsg)) {
   1022  1.139       ryo 		if (cmsg->cmsg_level == IPPROTO_IP &&
   1023  1.139       ryo 		    cmsg->cmsg_type == IP_RECVDSTADDR) {
   1024  1.139       ryo 			struct in_addr *dst = (struct in_addr *)CMSG_DATA(cmsg);
   1025  1.139       ryo 			struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
   1026  1.139       ryo 
   1027  1.139       ryo 			sin->sin_len = sizeof(*sin);
   1028  1.139       ryo 			sin->sin_family = AF_INET;
   1029  1.139       ryo 			sin->sin_addr = *dst;
   1030  1.139       ryo 			break;
   1031  1.139       ryo 		}
   1032  1.139       ryo 		if (cmsg->cmsg_level == IPPROTO_IP &&
   1033  1.139       ryo 		    cmsg->cmsg_type == IP_PKTINFO) {
   1034  1.139       ryo 			struct in_pktinfo *pi =
   1035  1.139       ryo 			    (struct in_pktinfo *)CMSG_DATA(cmsg);
   1036  1.139       ryo 			struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
   1037  1.139       ryo 
   1038  1.139       ryo 			sin->sin_len = sizeof(*sin);
   1039  1.139       ryo 			sin->sin_family = AF_INET;
   1040  1.139       ryo 			sin->sin_addr = pi->ipi_addr;
   1041  1.139       ryo 			break;
   1042  1.139       ryo 		}
   1043  1.139       ryo #ifdef INET6
   1044  1.139       ryo 		if (cmsg->cmsg_level == IPPROTO_IPV6 &&
   1045  1.139       ryo 		    cmsg->cmsg_type == IPV6_PKTINFO) {
   1046  1.139       ryo 			struct in6_pktinfo *pi6 =
   1047  1.139       ryo 			    (struct in6_pktinfo *)CMSG_DATA(cmsg);
   1048  1.139       ryo 			struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
   1049  1.139       ryo 
   1050  1.139       ryo 			sin6->sin6_len = sizeof(*sin6);
   1051  1.139       ryo 			sin6->sin6_family = AF_INET6;
   1052  1.139       ryo 			sin6->sin6_addr = pi6->ipi6_addr;
   1053  1.139       ryo 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
   1054  1.139       ryo 			    IN6_IS_ADDR_MC_LINKLOCAL(&sin6->sin6_addr))
   1055  1.139       ryo 				sin6->sin6_scope_id = pi6->ipi6_ifindex;
   1056  1.139       ryo 			else
   1057  1.139       ryo 				sin6->sin6_scope_id = 0;
   1058  1.139       ryo 			break;
   1059  1.139       ryo 		}
   1060  1.139       ryo #endif /* INET6 */
   1061  1.139       ryo 	}
   1062  1.139       ryo 
   1063  1.139       ryo 	socklen_t sslen = (*tolen < ss.ss_len) ? *tolen : ss.ss_len;
   1064  1.139       ryo 	if (sslen > 0)
   1065  1.139       ryo 		memcpy(to, &ss, sslen);
   1066  1.139       ryo 	*tolen = sslen;
   1067  1.139       ryo 
   1068  1.139       ryo 	return rc;
   1069  1.139       ryo }
   1070  1.139       ryo 
   1071  1.139       ryo /*
   1072  1.139       ryo  * When sending, the source port is selected as the one bind(2)'ed
   1073  1.139       ryo  * to the socket.
   1074  1.139       ryo  * .sin_port and .sin6_port in `from' are always ignored.
   1075  1.139       ryo  */
   1076  1.139       ryo static ssize_t
   1077  1.139       ryo sendfromto(int s, const void *buf, size_t len, int flags,
   1078  1.139       ryo     const struct sockaddr *from, socklen_t fromlen,
   1079  1.139       ryo     const struct sockaddr *to, socklen_t tolen)
   1080  1.139       ryo {
   1081  1.139       ryo 	struct msghdr msg;
   1082  1.139       ryo 	struct iovec vec;
   1083  1.139       ryo 	struct cmsghdr *cmsg;
   1084  1.139       ryo 	char cmsgbuf[256];
   1085  1.139       ryo 	__CTASSERT(sizeof(cmsgbuf) > CMSG_SPACE(sizeof(struct in_pktinfo)));
   1086  1.139       ryo #ifdef INET6
   1087  1.139       ryo 	__CTASSERT(sizeof(cmsgbuf) > CMSG_SPACE(sizeof(struct in6_pktinfo)));
   1088  1.139       ryo #endif
   1089  1.139       ryo 
   1090  1.139       ryo 	if (from == NULL || fromlen == 0)
   1091  1.139       ryo 		return sendto(s, buf, len, flags, to, tolen);
   1092  1.139       ryo 
   1093  1.139       ryo 	vec.iov_base = __UNCONST(buf);
   1094  1.139       ryo 	vec.iov_len = len;
   1095  1.139       ryo 	msg.msg_name = __UNCONST(to);
   1096  1.139       ryo 	msg.msg_namelen = tolen;
   1097  1.139       ryo 	msg.msg_iov = &vec;
   1098  1.139       ryo 	msg.msg_iovlen = 1;
   1099  1.139       ryo 	msg.msg_control = cmsgbuf;
   1100  1.139       ryo 	msg.msg_controllen = 0;
   1101  1.139       ryo 
   1102  1.139       ryo 	if (fromlen < 2) {	/* sa_len + sa_family */
   1103  1.139       ryo 		errno = EINVAL;
   1104  1.139       ryo 		return -1;
   1105  1.139       ryo 	}
   1106  1.139       ryo 
   1107  1.139       ryo 	cmsg = (struct cmsghdr *)cmsgbuf;
   1108  1.139       ryo 	if (from->sa_family == AF_INET) {
   1109  1.139       ryo 		const struct sockaddr_in *from4 =
   1110  1.139       ryo 		    (const struct sockaddr_in *)from;
   1111  1.139       ryo 		struct in_pktinfo *pi;
   1112  1.139       ryo 
   1113  1.139       ryo 		if (fromlen != sizeof(struct sockaddr_in) ||
   1114  1.139       ryo 		    from4->sin_family != AF_INET) {
   1115  1.139       ryo 			errno = EINVAL;
   1116  1.139       ryo 			return -1;
   1117  1.139       ryo 		}
   1118  1.139       ryo 
   1119  1.139       ryo 		msg.msg_controllen += CMSG_SPACE(sizeof(struct in_pktinfo));
   1120  1.139       ryo 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
   1121  1.139       ryo 		cmsg->cmsg_level = IPPROTO_IP;
   1122  1.139       ryo 		cmsg->cmsg_type = IP_PKTINFO;
   1123  1.139       ryo 
   1124  1.139       ryo 		pi = (struct in_pktinfo *)CMSG_DATA(cmsg);
   1125  1.139       ryo 		pi->ipi_addr = from4->sin_addr;
   1126  1.139       ryo 		pi->ipi_ifindex = 0;
   1127  1.139       ryo #ifdef INET6
   1128  1.139       ryo 	} else if (from->sa_family == AF_INET6) {
   1129  1.139       ryo 		const struct sockaddr_in6 *from6 =
   1130  1.139       ryo 		    (const struct sockaddr_in6 *)from;
   1131  1.139       ryo 		struct in6_pktinfo *pi6;
   1132  1.139       ryo 
   1133  1.139       ryo 		if (fromlen != sizeof(struct sockaddr_in6) ||
   1134  1.139       ryo 		    from6->sin6_family != AF_INET6) {
   1135  1.139       ryo 			errno = EINVAL;
   1136  1.139       ryo 			return -1;
   1137  1.139       ryo 		}
   1138  1.139       ryo 
   1139  1.139       ryo 		msg.msg_controllen += CMSG_SPACE(sizeof(struct in6_pktinfo));
   1140  1.139       ryo 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
   1141  1.139       ryo 		cmsg->cmsg_level = IPPROTO_IPV6;
   1142  1.139       ryo 		cmsg->cmsg_type = IPV6_PKTINFO;
   1143  1.139       ryo 
   1144  1.139       ryo 		pi6 = (struct in6_pktinfo *)CMSG_DATA(cmsg);
   1145  1.139       ryo 		pi6->ipi6_addr = from6->sin6_addr;
   1146  1.139       ryo 		if (IN6_IS_ADDR_LINKLOCAL(&from6->sin6_addr) ||
   1147  1.139       ryo 		    IN6_IS_ADDR_MC_LINKLOCAL(&from6->sin6_addr)) {
   1148  1.139       ryo 			pi6->ipi6_ifindex = from6->sin6_scope_id;
   1149  1.139       ryo 		} else {
   1150  1.139       ryo 			pi6->ipi6_ifindex = 0;
   1151  1.139       ryo 		}
   1152  1.139       ryo #endif /* INET6 */
   1153  1.139       ryo 	} else {
   1154  1.139       ryo 		return sendto(s, buf, len, flags, to, tolen);
   1155  1.139       ryo 	}
   1156  1.139       ryo 
   1157  1.139       ryo 	return sendmsg(s, &msg, flags);
   1158  1.139       ryo }
   1159  1.139       ryo 
   1160  1.139       ryo /*
   1161    1.1       cgd  * Internet services provided internally by inetd:
   1162    1.1       cgd  */
   1163    1.5        pk #define	BUFSIZE	4096
   1164    1.1       cgd 
   1165    1.1       cgd /* ARGSUSED */
   1166   1.87      tron static void
   1167   1.87      tron echo_stream(int s, struct servtab *sep)	/* Echo service -- echo data back */
   1168    1.1       cgd {
   1169    1.1       cgd 	char buffer[BUFSIZE];
   1170  1.110  christos 	ssize_t i;
   1171    1.1       cgd 
   1172    1.9       cgd 	inetd_setproctitle(sep->se_service, s);
   1173    1.1       cgd 	while ((i = read(s, buffer, sizeof(buffer))) > 0 &&
   1174  1.110  christos 	    write(s, buffer, (size_t)i) > 0)
   1175  1.137  christos 		continue;
   1176    1.1       cgd }
   1177    1.1       cgd 
   1178    1.1       cgd /* ARGSUSED */
   1179   1.87      tron static void
   1180   1.87      tron echo_dg(int s, struct servtab *sep)	/* Echo service -- echo data back */
   1181    1.1       cgd {
   1182    1.1       cgd 	char buffer[BUFSIZE];
   1183  1.110  christos 	ssize_t i;
   1184  1.139       ryo 	socklen_t rsize, lsize;
   1185  1.139       ryo 	struct sockaddr_storage remote, local;
   1186  1.139       ryo 	struct sockaddr *lsa, *rsa;
   1187  1.139       ryo 
   1188  1.139       ryo 	rsa = (struct sockaddr *)(void *)&remote;
   1189  1.139       ryo 	lsa = (struct sockaddr *)(void *)&local;
   1190  1.139       ryo 	rsize = sizeof(remote);
   1191  1.139       ryo 	lsize = sizeof(local);
   1192  1.139       ryo 	if ((i = recvfromto(s, buffer, sizeof(buffer), 0,
   1193  1.139       ryo 	    rsa, &rsize, lsa, &lsize)) < 0)
   1194    1.1       cgd 		return;
   1195  1.139       ryo 	if (port_good_dg(rsa))
   1196  1.139       ryo 		(void) sendfromto(s, buffer, (size_t)i, 0,
   1197  1.139       ryo 		    lsa, lsize, rsa, rsize);
   1198    1.1       cgd }
   1199    1.1       cgd 
   1200    1.1       cgd /* ARGSUSED */
   1201   1.87      tron static void
   1202   1.87      tron discard_stream(int s, struct servtab *sep) /* Discard service -- ignore data */
   1203    1.1       cgd {
   1204    1.1       cgd 	char buffer[BUFSIZE];
   1205    1.1       cgd 
   1206    1.9       cgd 	inetd_setproctitle(sep->se_service, s);
   1207    1.5        pk 	while ((errno = 0, read(s, buffer, sizeof(buffer)) > 0) ||
   1208    1.5        pk 			errno == EINTR)
   1209    1.5        pk 		;
   1210    1.1       cgd }
   1211    1.1       cgd 
   1212    1.1       cgd /* ARGSUSED */
   1213   1.87      tron static void
   1214   1.87      tron discard_dg(int s, struct servtab *sep)	/* Discard service -- ignore data */
   1215  1.130    rillig 
   1216    1.1       cgd {
   1217    1.1       cgd 	char buffer[BUFSIZE];
   1218    1.1       cgd 
   1219    1.1       cgd 	(void) read(s, buffer, sizeof(buffer));
   1220    1.1       cgd }
   1221    1.1       cgd 
   1222    1.1       cgd #define LINESIZ 72
   1223    1.1       cgd char ring[128];
   1224    1.1       cgd char *endring;
   1225    1.1       cgd 
   1226   1.87      tron static void
   1227   1.87      tron initring(void)
   1228    1.1       cgd {
   1229   1.23   mycroft 	int i;
   1230    1.1       cgd 
   1231    1.1       cgd 	endring = ring;
   1232    1.1       cgd 
   1233    1.1       cgd 	for (i = 0; i <= 128; ++i)
   1234    1.1       cgd 		if (isprint(i))
   1235  1.131    rillig 			*endring++ = (char)i;
   1236    1.1       cgd }
   1237    1.1       cgd 
   1238    1.1       cgd /* ARGSUSED */
   1239   1.87      tron static void
   1240  1.131    rillig chargen_stream(int s, struct servtab *sep)	/* Character generator */
   1241    1.1       cgd {
   1242  1.110  christos 	size_t len;
   1243   1.23   mycroft 	char *rs, text[LINESIZ+2];
   1244    1.1       cgd 
   1245    1.9       cgd 	inetd_setproctitle(sep->se_service, s);
   1246    1.1       cgd 
   1247  1.134    rillig 	if (endring == NULL) {
   1248    1.1       cgd 		initring();
   1249    1.1       cgd 		rs = ring;
   1250    1.1       cgd 	}
   1251    1.1       cgd 
   1252    1.1       cgd 	text[LINESIZ] = '\r';
   1253    1.1       cgd 	text[LINESIZ + 1] = '\n';
   1254    1.1       cgd 	for (rs = ring;;) {
   1255  1.131    rillig 		if ((len = (size_t)(endring - rs)) >= LINESIZ)
   1256   1.23   mycroft 			memmove(text, rs, LINESIZ);
   1257    1.1       cgd 		else {
   1258   1.23   mycroft 			memmove(text, rs, len);
   1259   1.23   mycroft 			memmove(text + len, ring, LINESIZ - len);
   1260    1.1       cgd 		}
   1261    1.1       cgd 		if (++rs == endring)
   1262    1.1       cgd 			rs = ring;
   1263    1.1       cgd 		if (write(s, text, sizeof(text)) != sizeof(text))
   1264    1.1       cgd 			break;
   1265    1.1       cgd 	}
   1266    1.1       cgd }
   1267    1.1       cgd 
   1268    1.1       cgd /* ARGSUSED */
   1269   1.87      tron static void
   1270   1.87      tron chargen_dg(int s, struct servtab *sep)		/* Character generator */
   1271    1.1       cgd {
   1272  1.139       ryo 	struct sockaddr_storage remote, local;
   1273  1.139       ryo 	struct sockaddr *rsa, *lsa;
   1274    1.1       cgd 	static char *rs;
   1275  1.110  christos 	size_t len;
   1276  1.139       ryo 	socklen_t rsize, lsize;
   1277    1.1       cgd 	char text[LINESIZ+2];
   1278    1.1       cgd 
   1279    1.1       cgd 	if (endring == 0) {
   1280    1.1       cgd 		initring();
   1281    1.1       cgd 		rs = ring;
   1282    1.1       cgd 	}
   1283    1.1       cgd 
   1284  1.139       ryo 	rsa = (struct sockaddr *)(void *)&remote;
   1285  1.139       ryo 	lsa = (struct sockaddr *)(void *)&local;
   1286  1.139       ryo 	rsize = sizeof(remote);
   1287  1.139       ryo 	lsize = sizeof(local);
   1288  1.139       ryo 	if (recvfromto(s, text, sizeof(text), 0,
   1289  1.139       ryo 	    rsa, &rsize, lsa, &lsize) < 0)
   1290    1.1       cgd 		return;
   1291    1.1       cgd 
   1292  1.139       ryo 	if (!port_good_dg(rsa))
   1293   1.47       hwr 		return;
   1294   1.47       hwr 
   1295  1.131    rillig 	if ((len = (size_t)(endring - rs)) >= LINESIZ)
   1296   1.23   mycroft 		memmove(text, rs, LINESIZ);
   1297    1.1       cgd 	else {
   1298   1.23   mycroft 		memmove(text, rs, len);
   1299   1.23   mycroft 		memmove(text + len, ring, LINESIZ - len);
   1300    1.1       cgd 	}
   1301    1.1       cgd 	if (++rs == endring)
   1302    1.1       cgd 		rs = ring;
   1303    1.1       cgd 	text[LINESIZ] = '\r';
   1304    1.1       cgd 	text[LINESIZ + 1] = '\n';
   1305  1.139       ryo 	(void) sendfromto(s, text, sizeof(text), 0, lsa, lsize, rsa, rsize);
   1306    1.1       cgd }
   1307    1.1       cgd 
   1308    1.1       cgd /*
   1309    1.1       cgd  * Return a machine readable date and time, in the form of the
   1310    1.1       cgd  * number of seconds since midnight, Jan 1, 1900.  Since gettimeofday
   1311    1.1       cgd  * returns the number of seconds since midnight, Jan 1, 1970,
   1312    1.1       cgd  * we must add 2208988800 seconds to this figure to make up for
   1313    1.1       cgd  * some seventy years Bell Labs was asleep.
   1314    1.1       cgd  */
   1315    1.1       cgd 
   1316   1.87      tron static uint32_t
   1317   1.87      tron machtime(void)
   1318    1.1       cgd {
   1319    1.1       cgd 	struct timeval tv;
   1320    1.1       cgd 
   1321   1.85      tron 	if (gettimeofday(&tv, NULL) < 0) {
   1322  1.127  christos 		DPRINTF("Unable to get time of day");
   1323   1.75       abs 		return (0);
   1324    1.1       cgd 	}
   1325   1.75       abs #define	OFFSET ((uint32_t)25567 * 24*60*60)
   1326   1.75       abs 	return (htonl((uint32_t)(tv.tv_sec + OFFSET)));
   1327   1.23   mycroft #undef OFFSET
   1328    1.1       cgd }
   1329    1.1       cgd 
   1330    1.1       cgd /* ARGSUSED */
   1331   1.87      tron static void
   1332   1.87      tron machtime_stream(int s, struct servtab *sep)
   1333    1.1       cgd {
   1334   1.75       abs 	uint32_t result;
   1335    1.1       cgd 
   1336    1.1       cgd 	result = machtime();
   1337  1.110  christos 	(void) write(s, &result, sizeof(result));
   1338    1.1       cgd }
   1339    1.1       cgd 
   1340    1.1       cgd /* ARGSUSED */
   1341   1.23   mycroft void
   1342   1.87      tron machtime_dg(int s, struct servtab *sep)
   1343    1.1       cgd {
   1344   1.75       abs 	uint32_t result;
   1345  1.139       ryo 	struct sockaddr_storage remote, local;
   1346  1.139       ryo 	struct sockaddr *rsa, *lsa;
   1347  1.139       ryo 	socklen_t rsize, lsize;
   1348  1.139       ryo 
   1349  1.139       ryo 	rsa = (struct sockaddr *)(void *)&remote;
   1350  1.139       ryo 	lsa = (struct sockaddr *)(void *)&local;
   1351  1.139       ryo 	rsize = sizeof(remote);
   1352  1.139       ryo 	lsize = sizeof(local);
   1353  1.139       ryo 	if (recvfromto(s, &result, sizeof(result), 0,
   1354  1.139       ryo 	    rsa, &rsize, lsa, &lsize) < 0)
   1355    1.1       cgd 		return;
   1356  1.139       ryo 	if (!port_good_dg(rsa))
   1357   1.47       hwr 		return;
   1358    1.1       cgd 	result = machtime();
   1359  1.139       ryo 	(void)sendfromto(s, &result, sizeof(result), 0, lsa, lsize, rsa, rsize);
   1360    1.1       cgd }
   1361    1.1       cgd 
   1362    1.1       cgd /* ARGSUSED */
   1363   1.87      tron static void
   1364   1.87      tron daytime_stream(int s,struct servtab *sep)
   1365   1.87      tron /* Return human-readable time of day */
   1366    1.1       cgd {
   1367    1.1       cgd 	char buffer[256];
   1368  1.110  christos 	time_t clk;
   1369   1.12       mrg 	int len;
   1370    1.1       cgd 
   1371  1.110  christos 	clk = time((time_t *) 0);
   1372    1.1       cgd 
   1373  1.110  christos 	len = snprintf(buffer, sizeof buffer, "%.24s\r\n", ctime(&clk));
   1374  1.131    rillig 	(void) write(s, buffer, (size_t)len);
   1375    1.1       cgd }
   1376    1.1       cgd 
   1377    1.1       cgd /* ARGSUSED */
   1378   1.23   mycroft void
   1379   1.87      tron daytime_dg(int s, struct servtab *sep)
   1380   1.87      tron /* Return human-readable time of day */
   1381    1.1       cgd {
   1382    1.1       cgd 	char buffer[256];
   1383  1.110  christos 	time_t clk;
   1384  1.139       ryo 	struct sockaddr_storage remote, local;
   1385  1.139       ryo 	struct sockaddr *rsa, *lsa;
   1386  1.139       ryo 	socklen_t rsize, lsize;
   1387   1.78    itojun 	int len;
   1388    1.1       cgd 
   1389  1.110  christos 	clk = time((time_t *) 0);
   1390    1.1       cgd 
   1391  1.139       ryo 	rsa = (struct sockaddr *)(void *)&remote;
   1392  1.139       ryo 	lsa = (struct sockaddr *)(void *)&local;
   1393  1.139       ryo 	rsize = sizeof(remote);
   1394  1.139       ryo 	lsize = sizeof(local);
   1395  1.139       ryo 	if (recvfromto(s, buffer, sizeof(buffer), 0,
   1396  1.139       ryo 	    rsa, &rsize, lsa, &lsize) < 0)
   1397    1.1       cgd 		return;
   1398  1.139       ryo 	if (!port_good_dg(rsa))
   1399   1.47       hwr 		return;
   1400  1.110  christos 	len = snprintf(buffer, sizeof buffer, "%.24s\r\n", ctime(&clk));
   1401  1.139       ryo 	(void) sendfromto(s, buffer, (size_t)len, 0, lsa, lsize, rsa, rsize);
   1402    1.1       cgd }
   1403    1.1       cgd 
   1404   1.87      tron static void
   1405   1.87      tron usage(void)
   1406   1.23   mycroft {
   1407   1.23   mycroft #ifdef LIBWRAP
   1408   1.73       cgd 	(void)fprintf(stderr, "usage: %s [-dl] [conf]\n", getprogname());
   1409   1.23   mycroft #else
   1410   1.73       cgd 	(void)fprintf(stderr, "usage: %s [-d] [conf]\n", getprogname());
   1411   1.23   mycroft #endif
   1412  1.127  christos 	exit(EXIT_FAILURE);
   1413   1.23   mycroft }
   1414   1.23   mycroft 
   1415   1.23   mycroft 
   1416   1.23   mycroft /*
   1417   1.23   mycroft  *  Based on TCPMUX.C by Mark K. Lottor November 1988
   1418   1.23   mycroft  *  sri-nic::ps:<mkl>tcpmux.c
   1419   1.23   mycroft  */
   1420   1.23   mycroft 
   1421   1.23   mycroft static int		/* # of characters upto \r,\n or \0 */
   1422  1.113       roy get_line(int fd,	char *buf, int len)
   1423   1.23   mycroft {
   1424  1.110  christos 	int count = 0;
   1425  1.110  christos 	ssize_t n;
   1426   1.23   mycroft 
   1427   1.23   mycroft 	do {
   1428  1.131    rillig 		n = read(fd, buf, (size_t)(len - count));
   1429   1.23   mycroft 		if (n == 0)
   1430   1.23   mycroft 			return (count);
   1431   1.23   mycroft 		if (n < 0)
   1432   1.23   mycroft 			return (-1);
   1433   1.23   mycroft 		while (--n >= 0) {
   1434   1.23   mycroft 			if (*buf == '\r' || *buf == '\n' || *buf == '\0')
   1435   1.23   mycroft 				return (count);
   1436   1.23   mycroft 			count++;
   1437   1.23   mycroft 			buf++;
   1438   1.23   mycroft 		}
   1439   1.23   mycroft 	} while (count < len);
   1440   1.23   mycroft 	return (count);
   1441   1.23   mycroft }
   1442   1.23   mycroft 
   1443   1.23   mycroft #define MAX_SERV_LEN	(256+2)		/* 2 bytes for \r\n */
   1444   1.23   mycroft 
   1445   1.23   mycroft #define strwrite(fd, buf)	(void) write(fd, buf, sizeof(buf)-1)
   1446   1.23   mycroft 
   1447   1.87      tron static void
   1448   1.87      tron tcpmux(int ctrl, struct servtab *sep)
   1449   1.23   mycroft {
   1450   1.23   mycroft 	char service[MAX_SERV_LEN+1];
   1451   1.23   mycroft 	int len;
   1452   1.23   mycroft 
   1453   1.23   mycroft 	/* Get requested service name */
   1454  1.113       roy 	if ((len = get_line(ctrl, service, MAX_SERV_LEN)) < 0) {
   1455   1.30   mycroft 		strwrite(ctrl, "-Error reading service name\r\n");
   1456   1.30   mycroft 		goto reject;
   1457   1.23   mycroft 	}
   1458   1.23   mycroft 	service[len] = '\0';
   1459   1.23   mycroft 
   1460  1.127  christos 	DPRINTF("tcpmux: %s: service requested", service);
   1461   1.23   mycroft 
   1462   1.23   mycroft 	/*
   1463   1.23   mycroft 	 * Help is a required command, and lists available services,
   1464   1.23   mycroft 	 * one per line.
   1465   1.23   mycroft 	 */
   1466  1.134    rillig 	if (strcasecmp(service, "help") == 0) {
   1467   1.31   mycroft 		strwrite(ctrl, "+Available services:\r\n");
   1468   1.31   mycroft 		strwrite(ctrl, "help\r\n");
   1469   1.85      tron 		for (sep = servtab; sep != NULL; sep = sep->se_next) {
   1470   1.23   mycroft 			if (!ISMUX(sep))
   1471   1.23   mycroft 				continue;
   1472   1.30   mycroft 			(void)write(ctrl, sep->se_service,
   1473   1.30   mycroft 			    strlen(sep->se_service));
   1474   1.30   mycroft 			strwrite(ctrl, "\r\n");
   1475   1.23   mycroft 		}
   1476   1.30   mycroft 		goto reject;
   1477   1.23   mycroft 	}
   1478   1.23   mycroft 
   1479   1.23   mycroft 	/* Try matching a service in inetd.conf with the request */
   1480   1.85      tron 	for (sep = servtab; sep != NULL; sep = sep->se_next) {
   1481   1.23   mycroft 		if (!ISMUX(sep))
   1482   1.23   mycroft 			continue;
   1483  1.134    rillig 		if (strcasecmp(service, sep->se_service) == 0) {
   1484   1.30   mycroft 			if (ISMUXPLUS(sep))
   1485   1.30   mycroft 				strwrite(ctrl, "+Go\r\n");
   1486  1.134    rillig 			run_service(ctrl, sep, true /* forked */);
   1487   1.30   mycroft 			return;
   1488   1.23   mycroft 		}
   1489   1.23   mycroft 	}
   1490   1.30   mycroft 	strwrite(ctrl, "-Service not available\r\n");
   1491   1.30   mycroft reject:
   1492  1.127  christos 	_exit(EXIT_FAILURE);
   1493   1.23   mycroft }
   1494   1.23   mycroft 
   1495   1.47       hwr /*
   1496   1.71    itojun  * check if the address/port where send data to is one of the obvious ports
   1497   1.47       hwr  * that are used for denial of service attacks like two echo ports
   1498   1.47       hwr  * just echoing data between them
   1499   1.47       hwr  */
   1500   1.87      tron static int
   1501   1.87      tron port_good_dg(struct sockaddr *sa)
   1502   1.47       hwr {
   1503   1.71    itojun 	struct in_addr in;
   1504  1.110  christos 	struct sockaddr_in *sin;
   1505   1.71    itojun #ifdef INET6
   1506   1.71    itojun 	struct in6_addr *in6;
   1507  1.110  christos 	struct sockaddr_in6 *sin6;
   1508   1.71    itojun #endif
   1509   1.47       hwr 	u_int16_t port;
   1510  1.110  christos 	int i;
   1511   1.57    itojun 	char hbuf[NI_MAXHOST];
   1512   1.47       hwr 
   1513   1.54    itojun 	switch (sa->sa_family) {
   1514   1.54    itojun 	case AF_INET:
   1515  1.110  christos 		sin = (struct sockaddr_in *)(void *)sa;
   1516  1.110  christos 		in.s_addr = ntohl(sin->sin_addr.s_addr);
   1517  1.110  christos 		port = ntohs(sin->sin_port);
   1518   1.97        pk #ifdef INET6
   1519   1.71    itojun 	v4chk:
   1520   1.97        pk #endif
   1521   1.71    itojun 		if (IN_MULTICAST(in.s_addr))
   1522   1.71    itojun 			goto bad;
   1523   1.71    itojun 		switch ((in.s_addr & 0xff000000) >> 24) {
   1524   1.71    itojun 		case 0: case 127: case 255:
   1525   1.71    itojun 			goto bad;
   1526   1.71    itojun 		}
   1527   1.82    itojun 		if (dg_broadcast(&in))
   1528   1.82    itojun 			goto bad;
   1529   1.54    itojun 		break;
   1530   1.58    itojun #ifdef INET6
   1531   1.54    itojun 	case AF_INET6:
   1532  1.110  christos 		sin6 = (struct sockaddr_in6 *)(void *)sa;
   1533  1.110  christos 		in6 = &sin6->sin6_addr;
   1534  1.110  christos 		port = ntohs(sin6->sin6_port);
   1535   1.71    itojun 		if (IN6_IS_ADDR_MULTICAST(in6) || IN6_IS_ADDR_UNSPECIFIED(in6))
   1536   1.71    itojun 			goto bad;
   1537   1.71    itojun 		if (IN6_IS_ADDR_V4MAPPED(in6) || IN6_IS_ADDR_V4COMPAT(in6)) {
   1538   1.71    itojun 			memcpy(&in, &in6->s6_addr[12], sizeof(in));
   1539   1.71    itojun 			in.s_addr = ntohl(in.s_addr);
   1540   1.71    itojun 			goto v4chk;
   1541   1.71    itojun 		}
   1542   1.54    itojun 		break;
   1543   1.58    itojun #endif
   1544   1.54    itojun 	default:
   1545   1.54    itojun 		/* XXX unsupported af, is it safe to assume it to be safe? */
   1546  1.134    rillig 		return true;
   1547   1.54    itojun 	}
   1548   1.47       hwr 
   1549   1.71    itojun 	for (i = 0; bad_ports[i] != 0; i++) {
   1550   1.71    itojun 		if (port == bad_ports[i])
   1551   1.71    itojun 			goto bad;
   1552   1.71    itojun 	}
   1553   1.47       hwr 
   1554  1.134    rillig 	return true;
   1555   1.71    itojun 
   1556   1.71    itojun bad:
   1557  1.110  christos 	if (getnameinfo(sa, sa->sa_len, hbuf, (socklen_t)sizeof(hbuf), NULL, 0,
   1558   1.80    itojun 	    niflags) != 0)
   1559   1.92    itojun 		strlcpy(hbuf, "?", sizeof(hbuf));
   1560   1.71    itojun 	syslog(LOG_WARNING,"Possible DoS attack from %s, Port %d",
   1561   1.71    itojun 		hbuf, port);
   1562  1.134    rillig 	return false;
   1563   1.82    itojun }
   1564   1.82    itojun 
   1565   1.82    itojun /* XXX need optimization */
   1566   1.87      tron static int
   1567   1.87      tron dg_broadcast(struct in_addr *in)
   1568   1.82    itojun {
   1569   1.82    itojun 	struct ifaddrs *ifa, *ifap;
   1570   1.82    itojun 	struct sockaddr_in *sin;
   1571   1.82    itojun 
   1572   1.82    itojun 	if (getifaddrs(&ifap) < 0)
   1573  1.134    rillig 		return false;
   1574  1.134    rillig 	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
   1575   1.82    itojun 		if (ifa->ifa_addr->sa_family != AF_INET ||
   1576   1.82    itojun 		    (ifa->ifa_flags & IFF_BROADCAST) == 0)
   1577   1.82    itojun 			continue;
   1578  1.110  christos 		sin = (struct sockaddr_in *)(void *)ifa->ifa_broadaddr;
   1579   1.82    itojun 		if (sin->sin_addr.s_addr == in->s_addr) {
   1580   1.82    itojun 			freeifaddrs(ifap);
   1581  1.134    rillig 			return true;
   1582   1.82    itojun 		}
   1583   1.82    itojun 	}
   1584   1.82    itojun 	freeifaddrs(ifap);
   1585  1.134    rillig 	return false;
   1586   1.85      tron }
   1587   1.85      tron 
   1588   1.85      tron static int
   1589   1.85      tron my_kevent(const struct kevent *changelist, size_t nchanges,
   1590   1.85      tron     struct kevent *eventlist, size_t nevents)
   1591   1.85      tron {
   1592   1.85      tron 	int	result;
   1593   1.85      tron 
   1594   1.85      tron 	while ((result = kevent(kq, changelist, nchanges, eventlist, nevents,
   1595   1.85      tron 	    NULL)) < 0)
   1596   1.85      tron 		if (errno != EINTR) {
   1597   1.85      tron 			syslog(LOG_ERR, "kevent: %m");
   1598   1.85      tron 			exit(EXIT_FAILURE);
   1599   1.85      tron 		}
   1600   1.85      tron 
   1601   1.85      tron 	return (result);
   1602   1.87      tron }
   1603   1.87      tron 
   1604   1.87      tron static struct kevent *
   1605   1.87      tron allocchange(void)
   1606   1.87      tron {
   1607  1.127  christos 	if (changes == __arraycount(changebuf)) {
   1608  1.127  christos 		(void) my_kevent(changebuf, __arraycount(changebuf), NULL, 0);
   1609   1.87      tron 		changes = 0;
   1610   1.87      tron 	}
   1611   1.87      tron 
   1612   1.87      tron 	return (&changebuf[changes++]);
   1613   1.47       hwr }
   1614  1.127  christos 
   1615  1.137  christos bool
   1616  1.137  christos try_biltin(struct servtab *sep)
   1617  1.127  christos {
   1618  1.138    rillig 	for (size_t i = 0; i < __arraycount(biltins); i++) {
   1619  1.137  christos 		if (biltins[i].bi_socktype == sep->se_socktype &&
   1620  1.137  christos 		    strcmp(biltins[i].bi_service, sep->se_service) == 0) {
   1621  1.137  christos 			sep->se_bi = &biltins[i];
   1622  1.137  christos 			sep->se_wait = biltins[i].bi_wait;
   1623  1.137  christos 			return true;
   1624  1.127  christos 		}
   1625  1.127  christos 	}
   1626  1.137  christos 	return false;
   1627  1.127  christos }
   1628