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