Home | History | Annotate | Line # | Download | only in inetd
inetd.h revision 1.4
      1 /*	$NetBSD: inetd.h,v 1.4 2021/10/12 19:08:04 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center and by Matthias Scheler.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1983, 1991, 1993, 1994
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. Neither the name of the University nor the names of its contributors
     46  *    may be used to endorse or promote products derived from this software
     47  *    without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  * SUCH DAMAGE.
     60  */
     61 
     62 #ifndef _INETD_H
     63 #define _INETD_H
     64 
     65 #include <netinet/in.h>
     66 #include <sys/socket.h>
     67 #include <sys/time.h>
     68 #include <sys/un.h>
     69 #include <sys/queue.h>
     70 
     71 #include <arpa/inet.h>
     72 
     73 #include <netdb.h>
     74 #include <stdbool.h>
     75 
     76 
     77 #include "pathnames.h"
     78 
     79 #ifdef IPSEC
     80 #include <netipsec/ipsec.h>
     81 #ifndef IPSEC_POLICY_IPSEC	/* no ipsec support on old ipsec */
     82 #undef IPSEC
     83 #endif
     84 #include "ipsec.h"
     85 #endif
     86 
     87 typedef enum service_type {
     88 	NORM_TYPE = 0,
     89 	MUX_TYPE = 1,
     90 	MUXPLUS_TYPE = 2,
     91 	FAITH_TYPE = 3
     92 } service_type;
     93 
     94 #define ISMUXPLUS(sep)	((sep)->se_type == MUXPLUS_TYPE)
     95 #define ISMUX(sep)	(((sep)->se_type == MUX_TYPE) || ISMUXPLUS(sep))
     96 
     97 #define	TOOMANY		40		/* don't start more than TOOMANY */
     98 
     99 #define CONF_ERROR_FMT "%s line %zu: "
    100 
    101 /* Log warning/error with 0 or variadic args with line number and file name */
    102 
    103 #define ILV(prio, msg, ...) syslog(prio, CONF_ERROR_FMT msg ".", \
    104     CONFIG, line_number __VA_OPT__(,) __VA_ARGS__)
    105 
    106 #define WRN(msg, ...) ILV(LOG_WARNING, msg __VA_OPT__(,) __VA_ARGS__)
    107 #define ERR(msg, ...) ILV(LOG_ERR, msg __VA_OPT__(,) __VA_ARGS__)
    108 
    109 /* Debug logging */
    110 #ifdef DEBUG_ENABLE
    111 #define DPRINTF(fmt, ...) do {\
    112 	if (debug) {\
    113 		fprintf(stderr, fmt "\n" __VA_OPT__(,) __VA_ARGS__);\
    114 	}\
    115 } while (false)
    116 #else
    117 #define DPRINTF(fmt, ...) __nothing
    118 #endif
    119 
    120 #define DPRINTCONF(fmt, ...) DPRINTF(CONF_ERROR_FMT fmt,\
    121 	CONFIG, line_number __VA_OPT__(,) __VA_ARGS__)
    122 
    123 #define STRINGIFY(x) #x
    124 #define TOSTRING(x) STRINGIFY(x)
    125 
    126 /* "Unspecified" indicator value for servtabs (mainly used by v2 syntax) */
    127 #define SERVTAB_UNSPEC_VAL -1
    128 
    129 #define SERVTAB_UNSPEC_SIZE_T SIZE_MAX
    130 
    131 #define SERVTAB_COUNT_MAX (SIZE_MAX - (size_t)1)
    132 
    133 /* Standard logging and debug print format for a servtab */
    134 #define SERV_FMT "%s/%s"
    135 #define SERV_PARAMS(sep) sep->se_service,sep->se_proto
    136 
    137 /* rate limiting macros */
    138 #define	CNT_INTVL	((time_t)60)	/* servers in CNT_INTVL sec. */
    139 #define	RETRYTIME	(60*10)		/* retry after bind or server fail */
    140 
    141 struct	servtab {
    142 	char	*se_hostaddr;		/* host address to listen on */
    143 	char	*se_service;		/* name of service */
    144 	int	se_socktype;		/* type of socket to use */
    145 	sa_family_t	se_family;	/* address family */
    146 	char	*se_proto;		/* protocol used */
    147 	int	se_sndbuf;		/* sndbuf size */
    148 	int	se_rcvbuf;		/* rcvbuf size */
    149 	int	se_rpcprog;		/* rpc program number */
    150 	int	se_rpcversl;		/* rpc program lowest version */
    151 	int	se_rpcversh;		/* rpc program highest version */
    152 #define isrpcservice(sep)	((sep)->se_rpcversl != 0)
    153 	pid_t	se_wait;		/* single threaded server */
    154 	short	se_checked;		/* looked at during merge */
    155 	char	*se_user;		/* user name to run as */
    156 	char	*se_group;		/* group name to run as */
    157 	struct	biltin *se_bi;		/* if built-in, description */
    158 	char	*se_server;		/* server program */
    159 #define	MAXARGV 64
    160 	char	*se_argv[MAXARGV+1];	/* program arguments */
    161 #ifdef IPSEC
    162 	char	*se_policy;		/* IPsec poilcy string */
    163 #endif
    164 	struct accept_filter_arg se_accf; /* accept filter for stream service */
    165 	int	se_fd;			/* open descriptor */
    166 	service_type	se_type;	/* type */
    167 	union {
    168 		/* ensure correctness of C struct initializer */
    169 		struct sockaddr_storage	se_ctrladdr_storage;
    170 		struct sockaddr	se_ctrladdr;
    171 		struct sockaddr_in	se_ctrladdr_in;
    172 		struct sockaddr_in6	se_ctrladdr_in6; /* in6 is used by bind()/getaddrinfo */
    173 		struct sockaddr_un	se_ctrladdr_un;
    174 	};				/* bound address */
    175 	socklen_t	se_ctrladdr_size;
    176 	size_t	se_service_max;		/* max # of instances of this service per minute */
    177 	size_t	se_count;		/* number of instances of this service started since se_time */
    178 	size_t	se_ip_max;  		/* max # of instances of this service per ip per minute */
    179 	SLIST_HEAD(iplist, rl_ip_node) se_rl_ip_list; /* per-address (IP) rate limting */
    180 	time_t se_time;	/* start of se_count and ip_max counts, in seconds from arbitrary point */
    181 
    182 	/* TODO convert to using SLIST */
    183 	struct	servtab *se_next;
    184 };
    185 
    186 struct rl_ip_node {
    187 	/* Linked list entries */
    188 	SLIST_ENTRY(rl_ip_node) entries;
    189 	/*
    190 	 * Number of service spawns from *_addr since se_time (includes
    191 	 * attempted starts if greater than se_ip_max).
    192 	 */
    193 	size_t count;
    194 	union {
    195 		struct in_addr	ipv4_addr;
    196 #ifdef INET6
    197 		/* align for efficient comparison in rl_try_get, could use 8 instead */
    198 		struct in6_addr	ipv6_addr __attribute__((aligned(16)));
    199 #endif
    200 		/*
    201 		 * other_addr is used for other address types besides the
    202 		 * special cases (IPv4/IPv6), using getnameinfo.
    203 		 */
    204 		struct {
    205 			/* A field is required before the special array member */
    206 			char _placeholder;
    207 			/* malloc'd storage varies with length of string */
    208 			char other_addr[];
    209 		};
    210 	};
    211 	/*
    212 	 * Do not declare further members after union, offsetof is used to
    213 	 * determine malloc size.
    214 	 */
    215 };
    216 
    217 /*
    218  * From inetd.c
    219  */
    220 
    221 void	setup(struct servtab *);
    222 void	close_sep(struct servtab *);
    223 void	register_rpc(struct servtab *);
    224 void	unregister_rpc(struct servtab *);
    225 bool	try_biltin(struct servtab *);
    226 
    227 /* Global debug mode boolean, enabled with -d */
    228 extern int debug;
    229 
    230 /* rate limit or other error timed out flag */
    231 extern int	timingout;
    232 
    233 /* servtab linked list */
    234 extern struct servtab *servtab;
    235 
    236 /*
    237  * From parse.c
    238  */
    239 
    240 void	config_root(void);
    241 int 	parse_protocol(struct servtab *);
    242 int 	parse_wait(struct servtab *, int);
    243 int 	parse_server(struct servtab *, const char *);
    244 void 	parse_socktype(char *, struct servtab *);
    245 void 	parse_accept_filter(char *, struct servtab *);
    246 char 	*nextline(FILE *);
    247 char 	*newstr(const char *);
    248 
    249 /* Current line number in current config file */
    250 extern size_t	line_number;
    251 
    252 /* Current config file path */
    253 extern const char	*CONFIG;
    254 
    255 /* Open config file */
    256 extern FILE	*fconfig;
    257 
    258 /* Default listening hostname/IP for current config file */
    259 extern char	*defhost;
    260 
    261 /* Default IPsec policy for current config file */
    262 extern char	*policy;
    263 
    264 /*
    265  * From ratelimit.c
    266  */
    267 
    268 int	rl_process(struct servtab *, int);
    269 void	rl_clear_ip_list(struct servtab *);
    270 
    271 /*
    272  * From parse_v2.c
    273  */
    274 
    275 typedef enum parse_v2_result {V2_SUCCESS, V2_SKIP, V2_ERROR} parse_v2_result;
    276 
    277 /*
    278  * Parse a key-values service definition, starting at the token after
    279  * on/off (i.e. parse a series of key-values pairs terminated by a semicolon).
    280  * Fills the provided servtab structure. Does not call freeconfig on error.
    281  */
    282 parse_v2_result	parse_syntax_v2(struct servtab *, char **);
    283 
    284 #endif
    285