inetd.h revision 1.5 1 1.5 rillig /* $NetBSD: inetd.h,v 1.5 2021/10/12 22:51:28 rillig 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.4 christos
77 1.1 christos #include "pathnames.h"
78 1.1 christos
79 1.1 christos #ifdef IPSEC
80 1.1 christos #include <netipsec/ipsec.h>
81 1.1 christos #ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */
82 1.1 christos #undef IPSEC
83 1.1 christos #endif
84 1.1 christos #include "ipsec.h"
85 1.1 christos #endif
86 1.1 christos
87 1.1 christos typedef enum service_type {
88 1.2 rillig NORM_TYPE = 0,
89 1.2 rillig MUX_TYPE = 1,
90 1.2 rillig MUXPLUS_TYPE = 2,
91 1.1 christos FAITH_TYPE = 3
92 1.1 christos } service_type;
93 1.1 christos
94 1.1 christos #define ISMUXPLUS(sep) ((sep)->se_type == MUXPLUS_TYPE)
95 1.1 christos #define ISMUX(sep) (((sep)->se_type == MUX_TYPE) || ISMUXPLUS(sep))
96 1.1 christos
97 1.1 christos #define TOOMANY 40 /* don't start more than TOOMANY */
98 1.1 christos
99 1.1 christos #define CONF_ERROR_FMT "%s line %zu: "
100 1.1 christos
101 1.1 christos /* Log warning/error with 0 or variadic args with line number and file name */
102 1.1 christos
103 1.1 christos #define ILV(prio, msg, ...) syslog(prio, CONF_ERROR_FMT msg ".", \
104 1.1 christos CONFIG, line_number __VA_OPT__(,) __VA_ARGS__)
105 1.1 christos
106 1.1 christos #define WRN(msg, ...) ILV(LOG_WARNING, msg __VA_OPT__(,) __VA_ARGS__)
107 1.1 christos #define ERR(msg, ...) ILV(LOG_ERR, msg __VA_OPT__(,) __VA_ARGS__)
108 1.1 christos
109 1.1 christos /* Debug logging */
110 1.1 christos #ifdef DEBUG_ENABLE
111 1.1 christos #define DPRINTF(fmt, ...) do {\
112 1.1 christos if (debug) {\
113 1.1 christos fprintf(stderr, fmt "\n" __VA_OPT__(,) __VA_ARGS__);\
114 1.1 christos }\
115 1.3 rillig } while (false)
116 1.1 christos #else
117 1.1 christos #define DPRINTF(fmt, ...) __nothing
118 1.1 christos #endif
119 1.1 christos
120 1.1 christos #define DPRINTCONF(fmt, ...) DPRINTF(CONF_ERROR_FMT fmt,\
121 1.1 christos CONFIG, line_number __VA_OPT__(,) __VA_ARGS__)
122 1.1 christos
123 1.1 christos #define STRINGIFY(x) #x
124 1.1 christos #define TOSTRING(x) STRINGIFY(x)
125 1.1 christos
126 1.4 christos /* "Unspecified" indicator value for servtabs (mainly used by v2 syntax) */
127 1.4 christos #define SERVTAB_UNSPEC_VAL -1
128 1.4 christos
129 1.4 christos #define SERVTAB_UNSPEC_SIZE_T SIZE_MAX
130 1.4 christos
131 1.4 christos #define SERVTAB_COUNT_MAX (SIZE_MAX - (size_t)1)
132 1.4 christos
133 1.4 christos /* Standard logging and debug print format for a servtab */
134 1.4 christos #define SERV_FMT "%s/%s"
135 1.4 christos #define SERV_PARAMS(sep) sep->se_service,sep->se_proto
136 1.4 christos
137 1.4 christos /* rate limiting macros */
138 1.4 christos #define CNT_INTVL ((time_t)60) /* servers in CNT_INTVL sec. */
139 1.4 christos #define RETRYTIME (60*10) /* retry after bind or server fail */
140 1.4 christos
141 1.1 christos struct servtab {
142 1.1 christos char *se_hostaddr; /* host address to listen on */
143 1.1 christos char *se_service; /* name of service */
144 1.1 christos int se_socktype; /* type of socket to use */
145 1.1 christos sa_family_t se_family; /* address family */
146 1.1 christos char *se_proto; /* protocol used */
147 1.1 christos int se_sndbuf; /* sndbuf size */
148 1.1 christos int se_rcvbuf; /* rcvbuf size */
149 1.1 christos int se_rpcprog; /* rpc program number */
150 1.1 christos int se_rpcversl; /* rpc program lowest version */
151 1.1 christos int se_rpcversh; /* rpc program highest version */
152 1.1 christos #define isrpcservice(sep) ((sep)->se_rpcversl != 0)
153 1.1 christos pid_t se_wait; /* single threaded server */
154 1.1 christos short se_checked; /* looked at during merge */
155 1.1 christos char *se_user; /* user name to run as */
156 1.1 christos char *se_group; /* group name to run as */
157 1.1 christos struct biltin *se_bi; /* if built-in, description */
158 1.1 christos char *se_server; /* server program */
159 1.1 christos #define MAXARGV 64
160 1.1 christos char *se_argv[MAXARGV+1]; /* program arguments */
161 1.1 christos #ifdef IPSEC
162 1.1 christos char *se_policy; /* IPsec poilcy string */
163 1.1 christos #endif
164 1.1 christos struct accept_filter_arg se_accf; /* accept filter for stream service */
165 1.1 christos int se_fd; /* open descriptor */
166 1.1 christos service_type se_type; /* type */
167 1.1 christos union {
168 1.4 christos /* ensure correctness of C struct initializer */
169 1.4 christos struct sockaddr_storage se_ctrladdr_storage;
170 1.1 christos struct sockaddr se_ctrladdr;
171 1.1 christos struct sockaddr_in se_ctrladdr_in;
172 1.1 christos struct sockaddr_in6 se_ctrladdr_in6; /* in6 is used by bind()/getaddrinfo */
173 1.1 christos struct sockaddr_un se_ctrladdr_un;
174 1.1 christos }; /* bound address */
175 1.1 christos socklen_t se_ctrladdr_size;
176 1.4 christos size_t se_service_max; /* max # of instances of this service per minute */
177 1.1 christos size_t se_count; /* number of instances of this service started since se_time */
178 1.1 christos size_t se_ip_max; /* max # of instances of this service per ip per minute */
179 1.4 christos SLIST_HEAD(iplist, rl_ip_node) se_rl_ip_list; /* per-address (IP) rate limting */
180 1.1 christos time_t se_time; /* start of se_count and ip_max counts, in seconds from arbitrary point */
181 1.4 christos
182 1.4 christos /* TODO convert to using SLIST */
183 1.1 christos struct servtab *se_next;
184 1.1 christos };
185 1.1 christos
186 1.4 christos struct rl_ip_node {
187 1.4 christos /* Linked list entries */
188 1.4 christos SLIST_ENTRY(rl_ip_node) entries;
189 1.4 christos /*
190 1.4 christos * Number of service spawns from *_addr since se_time (includes
191 1.4 christos * attempted starts if greater than se_ip_max).
192 1.4 christos */
193 1.4 christos size_t count;
194 1.4 christos union {
195 1.4 christos struct in_addr ipv4_addr;
196 1.4 christos #ifdef INET6
197 1.4 christos /* align for efficient comparison in rl_try_get, could use 8 instead */
198 1.4 christos struct in6_addr ipv6_addr __attribute__((aligned(16)));
199 1.4 christos #endif
200 1.5 rillig /*
201 1.5 rillig * other_addr is used for other address types besides the
202 1.4 christos * special cases (IPv4/IPv6), using getnameinfo.
203 1.4 christos */
204 1.4 christos struct {
205 1.4 christos /* A field is required before the special array member */
206 1.4 christos char _placeholder;
207 1.4 christos /* malloc'd storage varies with length of string */
208 1.4 christos char other_addr[];
209 1.4 christos };
210 1.4 christos };
211 1.5 rillig /*
212 1.4 christos * Do not declare further members after union, offsetof is used to
213 1.4 christos * determine malloc size.
214 1.4 christos */
215 1.4 christos };
216 1.4 christos
217 1.5 rillig /*
218 1.4 christos * From inetd.c
219 1.4 christos */
220 1.4 christos
221 1.4 christos void setup(struct servtab *);
222 1.4 christos void close_sep(struct servtab *);
223 1.4 christos void register_rpc(struct servtab *);
224 1.4 christos void unregister_rpc(struct servtab *);
225 1.4 christos bool try_biltin(struct servtab *);
226 1.4 christos
227 1.4 christos /* Global debug mode boolean, enabled with -d */
228 1.4 christos extern int debug;
229 1.4 christos
230 1.4 christos /* rate limit or other error timed out flag */
231 1.4 christos extern int timingout;
232 1.4 christos
233 1.4 christos /* servtab linked list */
234 1.4 christos extern struct servtab *servtab;
235 1.4 christos
236 1.4 christos /*
237 1.4 christos * From parse.c
238 1.4 christos */
239 1.4 christos
240 1.4 christos void config_root(void);
241 1.1 christos int parse_protocol(struct servtab *);
242 1.1 christos int parse_wait(struct servtab *, int);
243 1.1 christos int parse_server(struct servtab *, const char *);
244 1.1 christos void parse_socktype(char *, struct servtab *);
245 1.1 christos void parse_accept_filter(char *, struct servtab *);
246 1.1 christos char *nextline(FILE *);
247 1.1 christos char *newstr(const char *);
248 1.1 christos
249 1.4 christos /* Current line number in current config file */
250 1.4 christos extern size_t line_number;
251 1.1 christos
252 1.1 christos /* Current config file path */
253 1.4 christos extern const char *CONFIG;
254 1.1 christos
255 1.1 christos /* Open config file */
256 1.1 christos extern FILE *fconfig;
257 1.1 christos
258 1.1 christos /* Default listening hostname/IP for current config file */
259 1.4 christos extern char *defhost;
260 1.1 christos
261 1.1 christos /* Default IPsec policy for current config file */
262 1.4 christos extern char *policy;
263 1.4 christos
264 1.4 christos /*
265 1.5 rillig * From ratelimit.c
266 1.4 christos */
267 1.1 christos
268 1.4 christos int rl_process(struct servtab *, int);
269 1.4 christos void rl_clear_ip_list(struct servtab *);
270 1.4 christos
271 1.4 christos /*
272 1.4 christos * From parse_v2.c
273 1.4 christos */
274 1.1 christos
275 1.1 christos typedef enum parse_v2_result {V2_SUCCESS, V2_SKIP, V2_ERROR} parse_v2_result;
276 1.1 christos
277 1.2 rillig /*
278 1.1 christos * Parse a key-values service definition, starting at the token after
279 1.1 christos * on/off (i.e. parse a series of key-values pairs terminated by a semicolon).
280 1.1 christos * Fills the provided servtab structure. Does not call freeconfig on error.
281 1.1 christos */
282 1.1 christos parse_v2_result parse_syntax_v2(struct servtab *, char **);
283 1.1 christos
284 1.1 christos #endif
285