parse.c revision 1.5 1 1.5 christos /* $NetBSD: parse.c,v 1.5 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 #include <sys/cdefs.h>
63 1.1 christos #ifndef lint
64 1.1 christos #if 0
65 1.1 christos static char sccsid[] = "@(#)inetd.c 8.4 (Berkeley) 4/13/94";
66 1.1 christos #else
67 1.5 christos __RCSID("$NetBSD: parse.c,v 1.5 2022/08/10 08:37:53 christos Exp $");
68 1.1 christos #endif
69 1.1 christos #endif /* not lint */
70 1.1 christos
71 1.1 christos /*
72 1.1 christos * This file contains code and state for loading and managing servtabs.
73 1.1 christos * The "positional" syntax parsing is performed in this file. See parse_v2.c
74 1.1 christos * for "key-values" syntax parsing.
75 1.1 christos */
76 1.1 christos
77 1.1 christos #include <sys/param.h>
78 1.1 christos #include <sys/stat.h>
79 1.1 christos #include <sys/socket.h>
80 1.1 christos #include <sys/queue.h>
81 1.1 christos
82 1.1 christos #include <ctype.h>
83 1.1 christos #include <err.h>
84 1.1 christos #include <errno.h>
85 1.1 christos #include <fcntl.h>
86 1.1 christos #include <glob.h>
87 1.1 christos #include <libgen.h>
88 1.1 christos #include <stdio.h>
89 1.1 christos #include <stdlib.h>
90 1.1 christos #include <string.h>
91 1.1 christos #include <syslog.h>
92 1.1 christos #include <unistd.h>
93 1.1 christos
94 1.1 christos #include "inetd.h"
95 1.1 christos
96 1.1 christos static void config(void);
97 1.1 christos static void endconfig(void);
98 1.1 christos static struct servtab *enter(struct servtab *);
99 1.1 christos static struct servtab *getconfigent(char **);
100 1.1 christos #ifdef DEBUG_ENABLE
101 1.1 christos static void print_service(const char *, struct servtab *);
102 1.1 christos #endif
103 1.1 christos static struct servtab init_servtab(void);
104 1.1 christos static void include_configs(char *);
105 1.1 christos static int glob_error(const char *, int);
106 1.1 christos static void read_glob_configs(char *);
107 1.1 christos static void prepare_next_config(const char*);
108 1.1 christos static bool is_same_service(const struct servtab *, const struct servtab *);
109 1.1 christos static char *gen_file_pattern(const char *, const char *);
110 1.1 christos static bool check_no_reinclude(const char *);
111 1.1 christos static void include_matched_path(char *);
112 1.1 christos static void purge_unchecked(void);
113 1.1 christos static void freeconfig(struct servtab *);
114 1.1 christos static char *skip(char **);
115 1.1 christos
116 1.1 christos size_t line_number;
117 1.1 christos FILE *fconfig;
118 1.1 christos /* Temporary storage for new servtab */
119 1.1 christos static struct servtab serv;
120 1.1 christos /* Current line from current config file */
121 1.1 christos static char line[LINE_MAX];
122 1.1 christos char *defhost;
123 1.1 christos #ifdef IPSEC
124 1.1 christos char *policy;
125 1.1 christos #endif
126 1.1 christos
127 1.1 christos /*
128 1.1 christos * Recursively merge loaded service definitions with any defined
129 1.1 christos * in the current or included config files.
130 1.1 christos */
131 1.1 christos static void
132 1.1 christos config(void)
133 1.1 christos {
134 1.1 christos struct servtab *sep, *cp;
135 1.1 christos /*
136 1.1 christos * Current position in line, used with key-values notation,
137 1.1 christos * saves cp across getconfigent calls.
138 1.1 christos */
139 1.1 christos char *current_pos;
140 1.1 christos size_t n;
141 1.1 christos
142 1.1 christos /* open config file from beginning */
143 1.1 christos fconfig = fopen(CONFIG, "r");
144 1.1 christos if (fconfig == NULL) {
145 1.1 christos DPRINTF("Could not open file \"%s\": %s",
146 1.1 christos CONFIG, strerror(errno));
147 1.1 christos syslog(LOG_ERR, "%s: %m", CONFIG);
148 1.1 christos return;
149 1.1 christos }
150 1.1 christos
151 1.1 christos /* First call to nextline will advance line_number to 1 */
152 1.1 christos line_number = 0;
153 1.1 christos
154 1.1 christos /* Start parsing at the beginning of the first line */
155 1.1 christos current_pos = nextline(fconfig);
156 1.1 christos
157 1.1 christos while ((cp = getconfigent(¤t_pos)) != NULL) {
158 1.1 christos /* Find an already existing service definition */
159 1.1 christos for (sep = servtab; sep != NULL; sep = sep->se_next)
160 1.1 christos if (is_same_service(sep, cp))
161 1.1 christos break;
162 1.1 christos if (sep != NULL) {
163 1.1 christos int i;
164 1.1 christos
165 1.1 christos #define SWAP(type, a, b) {type c = a; a = b; b = c;}
166 1.1 christos
167 1.1 christos /*
168 1.1 christos * sep->se_wait may be holding the pid of a daemon
169 1.1 christos * that we're waiting for. If so, don't overwrite
170 1.1 christos * it unless the config file explicitly says don't
171 1.1 christos * wait.
172 1.1 christos */
173 1.1 christos if (cp->se_bi == 0 &&
174 1.1 christos (sep->se_wait == 1 || cp->se_wait == 0))
175 1.1 christos sep->se_wait = cp->se_wait;
176 1.1 christos SWAP(char *, sep->se_user, cp->se_user);
177 1.1 christos SWAP(char *, sep->se_group, cp->se_group);
178 1.1 christos SWAP(char *, sep->se_server, cp->se_server);
179 1.1 christos for (i = 0; i < MAXARGV; i++)
180 1.1 christos SWAP(char *, sep->se_argv[i], cp->se_argv[i]);
181 1.1 christos #ifdef IPSEC
182 1.1 christos SWAP(char *, sep->se_policy, cp->se_policy);
183 1.1 christos #endif
184 1.1 christos SWAP(service_type, cp->se_type, sep->se_type);
185 1.1 christos SWAP(size_t, cp->se_service_max, sep->se_service_max);
186 1.1 christos SWAP(size_t, cp->se_ip_max, sep->se_ip_max);
187 1.1 christos #undef SWAP
188 1.1 christos if (isrpcservice(sep))
189 1.1 christos unregister_rpc(sep);
190 1.1 christos sep->se_rpcversl = cp->se_rpcversl;
191 1.1 christos sep->se_rpcversh = cp->se_rpcversh;
192 1.1 christos freeconfig(cp);
193 1.1 christos #ifdef DEBUG_ENABLE
194 1.1 christos if (debug)
195 1.1 christos print_service("REDO", sep);
196 1.1 christos #endif
197 1.1 christos } else {
198 1.1 christos sep = enter(cp);
199 1.1 christos #ifdef DEBUG_ENABLE
200 1.1 christos if (debug)
201 1.1 christos print_service("ADD ", sep);
202 1.1 christos #endif
203 1.1 christos }
204 1.1 christos sep->se_checked = 1;
205 1.1 christos
206 1.1 christos /*
207 1.1 christos * Remainder of config(void) checks validity of servtab options
208 1.1 christos * and sets up the service by setting up sockets
209 1.1 christos * (in setup(servtab)).
210 1.1 christos */
211 1.1 christos switch (sep->se_family) {
212 1.1 christos case AF_LOCAL:
213 1.1 christos if (sep->se_fd != -1)
214 1.1 christos break;
215 1.1 christos n = strlen(sep->se_service);
216 1.1 christos if (n >= sizeof(sep->se_ctrladdr_un.sun_path)) {
217 1.1 christos syslog(LOG_ERR, "%s/%s: address too long",
218 1.1 christos sep->se_service, sep->se_proto);
219 1.1 christos sep->se_checked = 0;
220 1.1 christos continue;
221 1.1 christos }
222 1.1 christos (void)unlink(sep->se_service);
223 1.1 christos strlcpy(sep->se_ctrladdr_un.sun_path,
224 1.1 christos sep->se_service, n + 1);
225 1.1 christos sep->se_ctrladdr_un.sun_family = AF_LOCAL;
226 1.1 christos sep->se_ctrladdr_size = (socklen_t)(n +
227 1.1 christos sizeof(sep->se_ctrladdr_un) -
228 1.1 christos sizeof(sep->se_ctrladdr_un.sun_path));
229 1.1 christos if (!ISMUX(sep))
230 1.1 christos setup(sep);
231 1.1 christos break;
232 1.1 christos case AF_INET:
233 1.1 christos #ifdef INET6
234 1.1 christos case AF_INET6:
235 1.1 christos #endif
236 1.1 christos {
237 1.1 christos struct addrinfo hints, *res;
238 1.1 christos char *host;
239 1.1 christos const char *port;
240 1.1 christos int error;
241 1.1 christos int s;
242 1.1 christos
243 1.1 christos /* check if the family is supported */
244 1.1 christos s = socket(sep->se_family, SOCK_DGRAM, 0);
245 1.1 christos if (s < 0) {
246 1.1 christos syslog(LOG_WARNING,
247 1.1 christos "%s/%s: %s: the address family is not "
248 1.1 christos "supported by the kernel",
249 1.1 christos sep->se_service, sep->se_proto,
250 1.1 christos sep->se_hostaddr);
251 1.1 christos sep->se_checked = false;
252 1.1 christos continue;
253 1.1 christos }
254 1.1 christos close(s);
255 1.1 christos
256 1.1 christos memset(&hints, 0, sizeof(hints));
257 1.1 christos hints.ai_family = sep->se_family;
258 1.1 christos hints.ai_socktype = sep->se_socktype;
259 1.1 christos hints.ai_flags = AI_PASSIVE;
260 1.1 christos if (strcmp(sep->se_hostaddr, "*") == 0)
261 1.1 christos host = NULL;
262 1.1 christos else
263 1.1 christos host = sep->se_hostaddr;
264 1.1 christos if (isrpcservice(sep) || ISMUX(sep))
265 1.1 christos port = "0";
266 1.1 christos else
267 1.1 christos port = sep->se_service;
268 1.1 christos error = getaddrinfo(host, port, &hints, &res);
269 1.1 christos if (error != 0) {
270 1.1 christos if (error == EAI_SERVICE) {
271 1.1 christos /* gai_strerror not friendly enough */
272 1.1 christos syslog(LOG_WARNING, SERV_FMT ": "
273 1.1 christos "unknown service",
274 1.1 christos SERV_PARAMS(sep));
275 1.1 christos } else {
276 1.1 christos syslog(LOG_ERR, SERV_FMT ": %s: %s",
277 1.1 christos SERV_PARAMS(sep),
278 1.1 christos sep->se_hostaddr,
279 1.1 christos gai_strerror(error));
280 1.1 christos }
281 1.1 christos sep->se_checked = false;
282 1.1 christos continue;
283 1.1 christos }
284 1.1 christos if (res->ai_next != NULL) {
285 1.1 christos syslog(LOG_ERR, SERV_FMT
286 1.1 christos ": %s: resolved to multiple addr",
287 1.1 christos SERV_PARAMS(sep),
288 1.1 christos sep->se_hostaddr);
289 1.1 christos sep->se_checked = false;
290 1.1 christos freeaddrinfo(res);
291 1.1 christos continue;
292 1.1 christos }
293 1.1 christos memcpy(&sep->se_ctrladdr, res->ai_addr,
294 1.1 christos res->ai_addrlen);
295 1.1 christos if (ISMUX(sep)) {
296 1.1 christos sep->se_fd = -1;
297 1.1 christos freeaddrinfo(res);
298 1.1 christos continue;
299 1.1 christos }
300 1.1 christos sep->se_ctrladdr_size = res->ai_addrlen;
301 1.1 christos freeaddrinfo(res);
302 1.1 christos #ifdef RPC
303 1.1 christos if (isrpcservice(sep)) {
304 1.1 christos struct rpcent *rp;
305 1.1 christos
306 1.1 christos sep->se_rpcprog = atoi(sep->se_service);
307 1.1 christos if (sep->se_rpcprog == 0) {
308 1.1 christos rp = getrpcbyname(sep->se_service);
309 1.1 christos if (rp == 0) {
310 1.1 christos syslog(LOG_ERR,
311 1.1 christos SERV_FMT
312 1.1 christos ": unknown service",
313 1.1 christos SERV_PARAMS(sep));
314 1.1 christos sep->se_checked = false;
315 1.1 christos continue;
316 1.1 christos }
317 1.1 christos sep->se_rpcprog = rp->r_number;
318 1.1 christos }
319 1.1 christos if (sep->se_fd == -1 && !ISMUX(sep))
320 1.1 christos setup(sep);
321 1.1 christos if (sep->se_fd != -1)
322 1.1 christos register_rpc(sep);
323 1.1 christos } else
324 1.4 dholland #endif /* RPC */
325 1.1 christos {
326 1.1 christos if (sep->se_fd >= 0)
327 1.1 christos close_sep(sep);
328 1.1 christos if (sep->se_fd == -1 && !ISMUX(sep))
329 1.1 christos setup(sep);
330 1.1 christos }
331 1.1 christos }
332 1.1 christos }
333 1.1 christos }
334 1.1 christos endconfig();
335 1.1 christos }
336 1.1 christos
337 1.1 christos static struct servtab *
338 1.1 christos enter(struct servtab *cp)
339 1.1 christos {
340 1.1 christos struct servtab *sep;
341 1.1 christos
342 1.1 christos sep = malloc(sizeof (*sep));
343 1.1 christos if (sep == NULL) {
344 1.1 christos syslog(LOG_ERR, "Out of memory.");
345 1.1 christos exit(EXIT_FAILURE);
346 1.1 christos }
347 1.1 christos *sep = *cp;
348 1.1 christos sep->se_fd = -1;
349 1.1 christos sep->se_rpcprog = -1;
350 1.1 christos sep->se_next = servtab;
351 1.1 christos servtab = sep;
352 1.1 christos return (sep);
353 1.1 christos }
354 1.1 christos
355 1.1 christos static void
356 1.1 christos endconfig(void)
357 1.1 christos {
358 1.1 christos if (fconfig != NULL) {
359 1.1 christos (void) fclose(fconfig);
360 1.1 christos fconfig = NULL;
361 1.1 christos }
362 1.1 christos if (defhost != NULL) {
363 1.1 christos free(defhost);
364 1.1 christos defhost = NULL;
365 1.1 christos }
366 1.1 christos
367 1.1 christos #ifdef IPSEC
368 1.1 christos if (policy != NULL) {
369 1.1 christos free(policy);
370 1.1 christos policy = NULL;
371 1.1 christos }
372 1.1 christos #endif
373 1.1 christos
374 1.1 christos }
375 1.1 christos
376 1.1 christos #define LOG_EARLY_ENDCONF() \
377 1.1 christos ERR("Exiting %s early. Some services will be unavailable", CONFIG)
378 1.1 christos
379 1.1 christos #define LOG_TOO_FEW_ARGS() \
380 1.1 christos ERR("Expected more arguments")
381 1.1 christos
382 1.1 christos /* Parse the next service and apply any directives, and returns it as servtab */
383 1.1 christos static struct servtab *
384 1.1 christos getconfigent(char **current_pos)
385 1.1 christos {
386 1.1 christos struct servtab *sep = &serv;
387 1.1 christos int argc, val;
388 1.1 christos char *cp, *cp0, *arg, *buf0, *buf1, *sz0, *sz1;
389 1.1 christos static char TCPMUX_TOKEN[] = "tcpmux/";
390 1.1 christos #define MUX_LEN (sizeof(TCPMUX_TOKEN)-1)
391 1.1 christos char *hostdelim;
392 1.1 christos
393 1.1 christos /*
394 1.1 christos * Pre-condition: current_pos points into line,
395 1.1 christos * line contains config line. Continue where the last getconfigent
396 1.1 christos * left off. Allows for multiple service definitions per line.
397 1.1 christos */
398 1.1 christos cp = *current_pos;
399 1.1 christos
400 1.1 christos if (/*CONSTCOND*/false) {
401 1.1 christos /*
402 1.3 andvar * Go to the next line, but only after attempting to read the
403 1.1 christos * current one! Keep reading until we find a valid definition
404 1.1 christos * or EOF.
405 1.1 christos */
406 1.1 christos more:
407 1.1 christos cp = nextline(fconfig);
408 1.1 christos }
409 1.1 christos
410 1.1 christos if (cp == NULL) {
411 1.1 christos /* EOF or I/O error, let config() know to exit the file */
412 1.1 christos return NULL;
413 1.1 christos }
414 1.1 christos
415 1.1 christos /* Comments and IPsec policies */
416 1.1 christos if (cp[0] == '#') {
417 1.1 christos #ifdef IPSEC
418 1.1 christos /* lines starting with #@ is not a comment, but the policy */
419 1.1 christos if (cp[1] == '@') {
420 1.1 christos char *p;
421 1.1 christos for (p = cp + 2; isspace((unsigned char)*p); p++)
422 1.1 christos ;
423 1.1 christos if (*p == '\0') {
424 1.1 christos if (policy)
425 1.1 christos free(policy);
426 1.1 christos policy = NULL;
427 1.1 christos } else {
428 1.1 christos if (ipsecsetup_test(p) < 0) {
429 1.1 christos ERR("Invalid IPsec policy \"%s\"", p);
430 1.1 christos LOG_EARLY_ENDCONF();
431 1.1 christos /*
432 1.1 christos * Stop reading the current config to
433 1.1 christos * prevent services from being run
434 1.1 christos * without IPsec.
435 1.1 christos */
436 1.1 christos return NULL;
437 1.1 christos } else {
438 1.1 christos if (policy)
439 1.1 christos free(policy);
440 1.1 christos policy = newstr(p);
441 1.1 christos }
442 1.1 christos }
443 1.1 christos }
444 1.1 christos #endif
445 1.1 christos
446 1.1 christos goto more;
447 1.1 christos }
448 1.1 christos
449 1.1 christos /* Parse next token: listen-addr/hostname, service-spec, .include */
450 1.1 christos arg = skip(&cp);
451 1.1 christos
452 1.1 christos if (cp == NULL) {
453 1.1 christos goto more;
454 1.1 christos }
455 1.1 christos
456 1.2 rillig if (arg[0] == '.') {
457 1.1 christos if (strcmp(&arg[1], "include") == 0) {
458 1.1 christos /* include directive */
459 1.1 christos arg = skip(&cp);
460 1.2 rillig if (arg == NULL) {
461 1.1 christos LOG_TOO_FEW_ARGS();
462 1.1 christos return NULL;
463 1.1 christos }
464 1.1 christos include_configs(arg);
465 1.1 christos goto more;
466 1.1 christos } else {
467 1.1 christos ERR("Unknown directive '%s'", &arg[1]);
468 1.1 christos goto more;
469 1.1 christos }
470 1.1 christos }
471 1.1 christos
472 1.1 christos /* After this point, we might need to store data in a servtab */
473 1.1 christos *sep = init_servtab();
474 1.1 christos
475 1.1 christos /* Check for a host name. */
476 1.1 christos hostdelim = strrchr(arg, ':');
477 1.1 christos if (hostdelim != NULL) {
478 1.1 christos *hostdelim = '\0';
479 1.1 christos if (arg[0] == '[' && hostdelim > arg && hostdelim[-1] == ']') {
480 1.1 christos hostdelim[-1] = '\0';
481 1.1 christos sep->se_hostaddr = newstr(arg + 1);
482 1.1 christos } else
483 1.1 christos sep->se_hostaddr = newstr(arg);
484 1.1 christos arg = hostdelim + 1;
485 1.1 christos /*
486 1.1 christos * If the line is of the form `host:', then just change the
487 1.1 christos * default host for the following lines.
488 1.1 christos */
489 1.1 christos if (*arg == '\0') {
490 1.1 christos arg = skip(&cp);
491 1.1 christos if (cp == NULL) {
492 1.1 christos free(defhost);
493 1.1 christos defhost = sep->se_hostaddr;
494 1.1 christos goto more;
495 1.1 christos }
496 1.1 christos }
497 1.1 christos } else {
498 1.1 christos /* No host address found, set it to NULL to indicate absence */
499 1.1 christos sep->se_hostaddr = NULL;
500 1.1 christos }
501 1.1 christos if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) {
502 1.1 christos char *c = arg + MUX_LEN;
503 1.1 christos if (*c == '+') {
504 1.1 christos sep->se_type = MUXPLUS_TYPE;
505 1.1 christos c++;
506 1.1 christos } else
507 1.1 christos sep->se_type = MUX_TYPE;
508 1.1 christos sep->se_service = newstr(c);
509 1.1 christos } else {
510 1.1 christos sep->se_service = newstr(arg);
511 1.1 christos sep->se_type = NORM_TYPE;
512 1.1 christos }
513 1.1 christos
514 1.1 christos DPRINTCONF("Found service definition '%s'", sep->se_service);
515 1.1 christos
516 1.1 christos /* on/off/socktype */
517 1.1 christos arg = skip(&cp);
518 1.1 christos if (arg == NULL) {
519 1.1 christos LOG_TOO_FEW_ARGS();
520 1.1 christos freeconfig(sep);
521 1.1 christos goto more;
522 1.1 christos }
523 1.1 christos
524 1.1 christos /* Check for new v2 syntax */
525 1.1 christos if (strcmp(arg, "on") == 0 || strncmp(arg, "on#", 3) == 0) {
526 1.1 christos
527 1.1 christos if (arg[2] == '#') {
528 1.1 christos cp = nextline(fconfig);
529 1.1 christos }
530 1.1 christos
531 1.1 christos switch(parse_syntax_v2(sep, &cp)) {
532 1.1 christos case V2_SUCCESS:
533 1.1 christos *current_pos = cp;
534 1.1 christos return sep;
535 1.1 christos case V2_SKIP:
536 1.1 christos /*
537 1.1 christos * Skip invalid definitions, freeconfig is called in
538 1.1 christos * parse_v2.c
539 1.1 christos */
540 1.1 christos *current_pos = cp;
541 1.1 christos freeconfig(sep);
542 1.1 christos goto more;
543 1.1 christos case V2_ERROR:
544 1.1 christos /*
545 1.1 christos * Unrecoverable error, stop reading. freeconfig
546 1.1 christos * is called in parse_v2.c
547 1.1 christos */
548 1.1 christos LOG_EARLY_ENDCONF();
549 1.1 christos freeconfig(sep);
550 1.1 christos return NULL;
551 1.1 christos }
552 1.1 christos } else if (strcmp(arg, "off") == 0 || strncmp(arg, "off#", 4) == 0) {
553 1.1 christos
554 1.1 christos if (arg[3] == '#') {
555 1.1 christos cp = nextline(fconfig);
556 1.1 christos }
557 1.1 christos
558 1.1 christos /* Parse syntax the same as with 'on', but ignore the result */
559 1.1 christos switch(parse_syntax_v2(sep, &cp)) {
560 1.1 christos case V2_SUCCESS:
561 1.1 christos case V2_SKIP:
562 1.1 christos *current_pos = cp;
563 1.1 christos freeconfig(sep);
564 1.1 christos goto more;
565 1.1 christos case V2_ERROR:
566 1.1 christos /* Unrecoverable error, stop reading */
567 1.1 christos LOG_EARLY_ENDCONF();
568 1.1 christos freeconfig(sep);
569 1.1 christos return NULL;
570 1.1 christos }
571 1.1 christos } else {
572 1.1 christos /* continue parsing v1 */
573 1.1 christos parse_socktype(arg, sep);
574 1.1 christos if (sep->se_socktype == SOCK_STREAM) {
575 1.1 christos parse_accept_filter(arg, sep);
576 1.1 christos }
577 1.1 christos if (sep->se_hostaddr == NULL) {
578 1.1 christos /* Set host to current default */
579 1.1 christos sep->se_hostaddr = newstr(defhost);
580 1.1 christos }
581 1.1 christos }
582 1.1 christos
583 1.1 christos /* protocol */
584 1.1 christos arg = skip(&cp);
585 1.1 christos if (arg == NULL) {
586 1.1 christos LOG_TOO_FEW_ARGS();
587 1.1 christos freeconfig(sep);
588 1.1 christos goto more;
589 1.1 christos }
590 1.1 christos if (sep->se_type == NORM_TYPE &&
591 1.1 christos strncmp(arg, "faith/", strlen("faith/")) == 0) {
592 1.1 christos arg += strlen("faith/");
593 1.1 christos sep->se_type = FAITH_TYPE;
594 1.1 christos }
595 1.1 christos sep->se_proto = newstr(arg);
596 1.1 christos
597 1.1 christos #define MALFORMED(arg) \
598 1.1 christos do { \
599 1.1 christos ERR("%s: malformed buffer size option `%s'", \
600 1.1 christos sep->se_service, (arg)); \
601 1.1 christos freeconfig(sep); \
602 1.1 christos goto more; \
603 1.1 christos } while (false)
604 1.1 christos
605 1.1 christos #define GETVAL(arg) \
606 1.1 christos do { \
607 1.1 christos if (!isdigit((unsigned char)*(arg))) \
608 1.1 christos MALFORMED(arg); \
609 1.1 christos val = (int)strtol((arg), &cp0, 10); \
610 1.1 christos if (cp0 != NULL) { \
611 1.1 christos if (cp0[1] != '\0') \
612 1.1 christos MALFORMED((arg)); \
613 1.1 christos if (cp0[0] == 'k') \
614 1.1 christos val *= 1024; \
615 1.1 christos if (cp0[0] == 'm') \
616 1.1 christos val *= 1024 * 1024; \
617 1.1 christos } \
618 1.1 christos if (val < 1) { \
619 1.1 christos ERR("%s: invalid buffer size `%s'", \
620 1.1 christos sep->se_service, (arg)); \
621 1.1 christos freeconfig(sep); \
622 1.1 christos goto more; \
623 1.1 christos } \
624 1.1 christos } while (false)
625 1.1 christos
626 1.1 christos #define ASSIGN(arg) \
627 1.1 christos do { \
628 1.1 christos if (strcmp((arg), "sndbuf") == 0) \
629 1.1 christos sep->se_sndbuf = val; \
630 1.1 christos else if (strcmp((arg), "rcvbuf") == 0) \
631 1.1 christos sep->se_rcvbuf = val; \
632 1.1 christos else \
633 1.1 christos MALFORMED((arg)); \
634 1.1 christos } while (false)
635 1.1 christos
636 1.1 christos /*
637 1.1 christos * Extract the send and receive buffer sizes before parsing
638 1.1 christos * the protocol.
639 1.1 christos */
640 1.1 christos sep->se_sndbuf = sep->se_rcvbuf = 0;
641 1.1 christos buf0 = buf1 = sz0 = sz1 = NULL;
642 1.1 christos if ((buf0 = strchr(sep->se_proto, ',')) != NULL) {
643 1.1 christos /* Not meaningful for Tcpmux services. */
644 1.1 christos if (ISMUX(sep)) {
645 1.1 christos ERR("%s: can't specify buffer sizes for "
646 1.1 christos "tcpmux services", sep->se_service);
647 1.1 christos goto more;
648 1.1 christos }
649 1.1 christos
650 1.1 christos /* Skip the , */
651 1.1 christos *buf0++ = '\0';
652 1.1 christos
653 1.1 christos /* Check to see if another socket buffer size was specified. */
654 1.1 christos if ((buf1 = strchr(buf0, ',')) != NULL) {
655 1.1 christos /* Skip the , */
656 1.1 christos *buf1++ = '\0';
657 1.1 christos
658 1.1 christos /* Make sure a 3rd one wasn't specified. */
659 1.1 christos if (strchr(buf1, ',') != NULL) {
660 1.1 christos ERR("%s: too many buffer sizes",
661 1.1 christos sep->se_service);
662 1.1 christos goto more;
663 1.1 christos }
664 1.1 christos
665 1.1 christos /* Locate the size. */
666 1.1 christos if ((sz1 = strchr(buf1, '=')) == NULL)
667 1.1 christos MALFORMED(buf1);
668 1.1 christos
669 1.1 christos /* Skip the = */
670 1.1 christos *sz1++ = '\0';
671 1.1 christos }
672 1.1 christos
673 1.1 christos /* Locate the size. */
674 1.1 christos if ((sz0 = strchr(buf0, '=')) == NULL)
675 1.1 christos MALFORMED(buf0);
676 1.1 christos
677 1.1 christos /* Skip the = */
678 1.1 christos *sz0++ = '\0';
679 1.1 christos
680 1.1 christos GETVAL(sz0);
681 1.1 christos ASSIGN(buf0);
682 1.1 christos
683 1.1 christos if (buf1 != NULL) {
684 1.1 christos GETVAL(sz1);
685 1.1 christos ASSIGN(buf1);
686 1.1 christos }
687 1.1 christos }
688 1.1 christos
689 1.1 christos #undef ASSIGN
690 1.1 christos #undef GETVAL
691 1.1 christos #undef MALFORMED
692 1.1 christos
693 1.1 christos if (parse_protocol(sep)) {
694 1.1 christos freeconfig(sep);
695 1.1 christos goto more;
696 1.1 christos }
697 1.1 christos
698 1.1 christos /* wait/nowait:max */
699 1.1 christos arg = skip(&cp);
700 1.1 christos if (arg == NULL) {
701 1.1 christos LOG_TOO_FEW_ARGS();
702 1.1 christos freeconfig(sep);
703 1.1 christos goto more;
704 1.1 christos }
705 1.1 christos
706 1.1 christos /* Rate limiting parsing */ {
707 1.1 christos char *cp1;
708 1.1 christos if ((cp1 = strchr(arg, ':')) == NULL)
709 1.1 christos cp1 = strchr(arg, '.');
710 1.1 christos if (cp1 != NULL) {
711 1.1 christos int rstatus;
712 1.1 christos *cp1++ = '\0';
713 1.1 christos sep->se_service_max = (size_t)strtou(cp1, NULL, 10, 0,
714 1.1 christos SERVTAB_COUNT_MAX, &rstatus);
715 1.1 christos
716 1.1 christos if (rstatus != 0) {
717 1.1 christos if (rstatus != ERANGE) {
718 1.1 christos /* For compatibility w/ atoi parsing */
719 1.1 christos sep->se_service_max = 0;
720 1.1 christos }
721 1.1 christos
722 1.1 christos WRN("Improper \"max\" value '%s', "
723 1.1 christos "using '%zu' instead: %s",
724 1.1 christos cp1,
725 1.1 christos sep->se_service_max,
726 1.1 christos strerror(rstatus));
727 1.1 christos }
728 1.1 christos
729 1.1 christos } else
730 1.1 christos sep->se_service_max = TOOMANY;
731 1.1 christos }
732 1.1 christos if (parse_wait(sep, strcmp(arg, "wait") == 0)) {
733 1.1 christos freeconfig(sep);
734 1.1 christos goto more;
735 1.1 christos }
736 1.1 christos
737 1.1 christos /* Parse user:group token */
738 1.1 christos arg = skip(&cp);
739 1.2 rillig if (arg == NULL) {
740 1.1 christos LOG_TOO_FEW_ARGS();
741 1.1 christos freeconfig(sep);
742 1.1 christos goto more;
743 1.1 christos }
744 1.1 christos char* separator = strchr(arg, ':');
745 1.1 christos if (separator == NULL) {
746 1.1 christos /* Backwards compatibility, allow dot instead of colon */
747 1.1 christos separator = strchr(arg, '.');
748 1.1 christos }
749 1.1 christos
750 1.1 christos if (separator == NULL) {
751 1.1 christos /* Only user was specified */
752 1.1 christos sep->se_group = NULL;
753 1.1 christos } else {
754 1.1 christos *separator = '\0';
755 1.1 christos sep->se_group = newstr(separator + 1);
756 1.1 christos }
757 1.1 christos
758 1.1 christos sep->se_user = newstr(arg);
759 1.1 christos
760 1.1 christos /* Parser server-program (path to binary or "internal") */
761 1.1 christos arg = skip(&cp);
762 1.1 christos if (arg == NULL) {
763 1.1 christos LOG_TOO_FEW_ARGS();
764 1.1 christos freeconfig(sep);
765 1.1 christos goto more;
766 1.1 christos }
767 1.1 christos if (parse_server(sep, arg)) {
768 1.1 christos freeconfig(sep);
769 1.1 christos goto more;
770 1.1 christos }
771 1.1 christos
772 1.1 christos argc = 0;
773 1.1 christos for (arg = skip(&cp); cp != NULL; arg = skip(&cp)) {
774 1.1 christos if (argc < MAXARGV)
775 1.1 christos sep->se_argv[argc++] = newstr(arg);
776 1.1 christos }
777 1.1 christos while (argc <= MAXARGV)
778 1.1 christos sep->se_argv[argc++] = NULL;
779 1.1 christos #ifdef IPSEC
780 1.1 christos sep->se_policy = policy != NULL ? newstr(policy) : NULL;
781 1.1 christos #endif
782 1.1 christos /* getconfigent read a positional service def, move to next line */
783 1.1 christos *current_pos = nextline(fconfig);
784 1.1 christos return (sep);
785 1.1 christos }
786 1.1 christos
787 1.1 christos void
788 1.1 christos freeconfig(struct servtab *cp)
789 1.1 christos {
790 1.1 christos int i;
791 1.1 christos
792 1.1 christos free(cp->se_hostaddr);
793 1.1 christos free(cp->se_service);
794 1.1 christos free(cp->se_proto);
795 1.1 christos free(cp->se_user);
796 1.1 christos free(cp->se_group);
797 1.1 christos free(cp->se_server);
798 1.1 christos for (i = 0; i < MAXARGV; i++)
799 1.1 christos free(cp->se_argv[i]);
800 1.1 christos #ifdef IPSEC
801 1.1 christos free(cp->se_policy);
802 1.1 christos #endif
803 1.1 christos }
804 1.1 christos
805 1.1 christos /*
806 1.1 christos * Get next token *in the current service definition* from config file.
807 1.1 christos * Allows multi-line parse if single space or single tab-indented.
808 1.1 christos * Things in quotes are considered single token.
809 1.1 christos * Advances cp to next token.
810 1.1 christos */
811 1.1 christos static char *
812 1.1 christos skip(char **cpp)
813 1.1 christos {
814 1.1 christos char *cp = *cpp;
815 1.1 christos char *start;
816 1.1 christos char quote;
817 1.1 christos
818 1.1 christos if (*cpp == NULL)
819 1.1 christos return (NULL);
820 1.1 christos
821 1.1 christos again:
822 1.1 christos while (*cp == ' ' || *cp == '\t')
823 1.1 christos cp++;
824 1.1 christos if (*cp == '\0') {
825 1.1 christos int c;
826 1.1 christos
827 1.1 christos c = getc(fconfig);
828 1.1 christos (void) ungetc(c, fconfig);
829 1.1 christos if (c == ' ' || c == '\t')
830 1.1 christos if ((cp = nextline(fconfig)) != NULL)
831 1.1 christos goto again;
832 1.1 christos *cpp = NULL;
833 1.1 christos return (NULL);
834 1.1 christos }
835 1.1 christos start = cp;
836 1.1 christos /* Parse shell-style quotes */
837 1.1 christos quote = '\0';
838 1.1 christos while (*cp != '\0' && (quote != '\0' || (*cp != ' ' && *cp != '\t'))) {
839 1.1 christos if (*cp == '\'' || *cp == '"') {
840 1.1 christos if (quote != '\0' && *cp != quote)
841 1.1 christos cp++;
842 1.1 christos else {
843 1.1 christos if (quote != '\0')
844 1.1 christos quote = '\0';
845 1.1 christos else
846 1.1 christos quote = *cp;
847 1.1 christos memmove(cp, cp+1, strlen(cp));
848 1.1 christos }
849 1.1 christos } else
850 1.1 christos cp++;
851 1.1 christos }
852 1.1 christos if (*cp != '\0')
853 1.1 christos *cp++ = '\0';
854 1.1 christos *cpp = cp;
855 1.1 christos return (start);
856 1.1 christos }
857 1.1 christos
858 1.1 christos char *
859 1.1 christos nextline(FILE *fd)
860 1.1 christos {
861 1.1 christos char *cp;
862 1.1 christos
863 1.1 christos if (fgets(line, (int)sizeof(line), fd) == NULL) {
864 1.1 christos if (ferror(fd) != 0) {
865 1.1 christos ERR("Error when reading next line: %s",
866 1.1 christos strerror(errno));
867 1.1 christos }
868 1.1 christos return NULL;
869 1.1 christos }
870 1.1 christos cp = strchr(line, '\n');
871 1.1 christos if (cp != NULL)
872 1.1 christos *cp = '\0';
873 1.1 christos line_number++;
874 1.1 christos return line;
875 1.1 christos }
876 1.1 christos
877 1.1 christos char *
878 1.1 christos newstr(const char *cp)
879 1.1 christos {
880 1.1 christos char *dp;
881 1.1 christos if ((dp = strdup((cp != NULL) ? cp : "")) != NULL)
882 1.1 christos return (dp);
883 1.1 christos syslog(LOG_ERR, "strdup: %m");
884 1.1 christos exit(EXIT_FAILURE);
885 1.1 christos /*NOTREACHED*/
886 1.1 christos }
887 1.1 christos
888 1.1 christos #ifdef DEBUG_ENABLE
889 1.1 christos /*
890 1.1 christos * print_service:
891 1.1 christos * Dump relevant information to stderr
892 1.1 christos */
893 1.1 christos static void
894 1.1 christos print_service(const char *action, struct servtab *sep)
895 1.1 christos {
896 1.1 christos
897 1.1 christos if (isrpcservice(sep))
898 1.1 christos fprintf(stderr,
899 1.1 christos "%s: %s rpcprog=%d, rpcvers = %d/%d, proto=%s, "
900 1.1 christos "wait.max=%d.%zu, "
901 1.1 christos "user:group=%s:%s builtin=%lx server=%s"
902 1.1 christos #ifdef IPSEC
903 1.1 christos " policy=\"%s\""
904 1.1 christos #endif
905 1.1 christos "\n",
906 1.1 christos action, sep->se_service,
907 1.1 christos sep->se_rpcprog, sep->se_rpcversh, sep->se_rpcversl,
908 1.1 christos sep->se_proto, sep->se_wait, sep->se_service_max,
909 1.1 christos sep->se_user, sep->se_group,
910 1.1 christos (long)sep->se_bi, sep->se_server
911 1.1 christos #ifdef IPSEC
912 1.1 christos , (sep->se_policy != NULL ? sep->se_policy : "")
913 1.1 christos #endif
914 1.1 christos );
915 1.1 christos else
916 1.1 christos fprintf(stderr,
917 1.1 christos "%s: %s:%s proto=%s%s, wait.max=%d.%zu, user:group=%s:%s "
918 1.1 christos "builtin=%lx "
919 1.1 christos "server=%s"
920 1.1 christos #ifdef IPSEC
921 1.1 christos " policy=%s"
922 1.1 christos #endif
923 1.1 christos "\n",
924 1.1 christos action, sep->se_hostaddr, sep->se_service,
925 1.1 christos sep->se_type == FAITH_TYPE ? "faith/" : "",
926 1.1 christos sep->se_proto,
927 1.1 christos sep->se_wait, sep->se_service_max, sep->se_user,
928 1.1 christos sep->se_group, (long)sep->se_bi, sep->se_server
929 1.1 christos #ifdef IPSEC
930 1.1 christos , (sep->se_policy != NULL ? sep->se_policy : "")
931 1.1 christos #endif
932 1.1 christos );
933 1.1 christos }
934 1.1 christos #endif
935 1.1 christos
936 1.1 christos void
937 1.1 christos config_root(void)
938 1.1 christos {
939 1.1 christos struct servtab *sep;
940 1.1 christos /* Uncheck services */
941 1.1 christos for (sep = servtab; sep != NULL; sep = sep->se_next) {
942 1.1 christos sep->se_checked = false;
943 1.1 christos }
944 1.1 christos defhost = newstr("*");
945 1.1 christos #ifdef IPSEC
946 1.1 christos policy = NULL;
947 1.1 christos #endif
948 1.1 christos fconfig = NULL;
949 1.1 christos config();
950 1.1 christos purge_unchecked();
951 1.1 christos }
952 1.1 christos
953 1.1 christos static void
954 1.1 christos purge_unchecked(void)
955 1.1 christos {
956 1.1 christos struct servtab *sep, **sepp = &servtab;
957 1.1 christos int servtab_count = 0;
958 1.1 christos while ((sep = *sepp) != NULL) {
959 1.1 christos if (sep->se_checked) {
960 1.1 christos sepp = &sep->se_next;
961 1.1 christos servtab_count++;
962 1.1 christos continue;
963 1.1 christos }
964 1.1 christos *sepp = sep->se_next;
965 1.1 christos if (sep->se_fd >= 0)
966 1.1 christos close_sep(sep);
967 1.1 christos if (isrpcservice(sep))
968 1.1 christos unregister_rpc(sep);
969 1.1 christos if (sep->se_family == AF_LOCAL)
970 1.1 christos (void)unlink(sep->se_service);
971 1.1 christos #ifdef DEBUG_ENABLE
972 1.1 christos if (debug)
973 1.1 christos print_service("FREE", sep);
974 1.1 christos #endif
975 1.1 christos freeconfig(sep);
976 1.1 christos free(sep);
977 1.1 christos }
978 1.1 christos DPRINTF("%d service(s) loaded.", servtab_count);
979 1.1 christos }
980 1.1 christos
981 1.1 christos static bool
982 1.1 christos is_same_service(const struct servtab *sep, const struct servtab *cp)
983 1.1 christos {
984 1.1 christos return
985 1.1 christos strcmp(sep->se_service, cp->se_service) == 0 &&
986 1.1 christos strcmp(sep->se_hostaddr, cp->se_hostaddr) == 0 &&
987 1.1 christos strcmp(sep->se_proto, cp->se_proto) == 0 &&
988 1.1 christos sep->se_family == cp->se_family &&
989 1.1 christos ISMUX(sep) == ISMUX(cp);
990 1.1 christos }
991 1.1 christos
992 1.1 christos int
993 1.1 christos parse_protocol(struct servtab *sep)
994 1.1 christos {
995 1.1 christos int val;
996 1.1 christos
997 1.1 christos if (strcmp(sep->se_proto, "unix") == 0) {
998 1.1 christos sep->se_family = AF_LOCAL;
999 1.1 christos } else {
1000 1.1 christos val = (int)strlen(sep->se_proto);
1001 1.1 christos if (val == 0) {
1002 1.1 christos ERR("%s: invalid protocol specified",
1003 1.1 christos sep->se_service);
1004 1.1 christos return -1;
1005 1.1 christos }
1006 1.1 christos val = sep->se_proto[val - 1];
1007 1.1 christos switch (val) {
1008 1.1 christos case '4': /*tcp4 or udp4*/
1009 1.1 christos sep->se_family = AF_INET;
1010 1.1 christos break;
1011 1.1 christos #ifdef INET6
1012 1.1 christos case '6': /*tcp6 or udp6*/
1013 1.1 christos sep->se_family = AF_INET6;
1014 1.1 christos break;
1015 1.1 christos #endif
1016 1.1 christos default:
1017 1.1 christos /*
1018 1.1 christos * Use 'default' IP version which is IPv4, may
1019 1.1 christos * eventually be changed to AF_INET6
1020 1.1 christos */
1021 1.1 christos sep->se_family = AF_INET;
1022 1.1 christos break;
1023 1.1 christos }
1024 1.1 christos if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
1025 1.1 christos #ifdef RPC
1026 1.1 christos char *cp1, *ccp;
1027 1.1 christos cp1 = strchr(sep->se_service, '/');
1028 1.1 christos if (cp1 == 0) {
1029 1.1 christos ERR("%s: no rpc version",
1030 1.1 christos sep->se_service);
1031 1.1 christos return -1;
1032 1.1 christos }
1033 1.1 christos *cp1++ = '\0';
1034 1.1 christos sep->se_rpcversl = sep->se_rpcversh =
1035 1.1 christos (int)strtol(cp1, &ccp, 0);
1036 1.1 christos if (ccp == cp1) {
1037 1.1 christos badafterall:
1038 1.1 christos ERR("%s/%s: bad rpc version",
1039 1.1 christos sep->se_service, cp1);
1040 1.1 christos return -1;
1041 1.1 christos }
1042 1.1 christos if (*ccp == '-') {
1043 1.1 christos cp1 = ccp + 1;
1044 1.1 christos sep->se_rpcversh = (int)strtol(cp1, &ccp, 0);
1045 1.1 christos if (ccp == cp1)
1046 1.1 christos goto badafterall;
1047 1.1 christos }
1048 1.1 christos #else
1049 1.1 christos ERR("%s: rpc services not supported",
1050 1.1 christos sep->se_service);
1051 1.1 christos return -1;
1052 1.1 christos #endif /* RPC */
1053 1.1 christos }
1054 1.1 christos }
1055 1.1 christos return 0;
1056 1.1 christos }
1057 1.1 christos
1058 1.1 christos int
1059 1.1 christos parse_wait(struct servtab *sep, int wait)
1060 1.1 christos {
1061 1.1 christos if (!ISMUX(sep)) {
1062 1.1 christos sep->se_wait = wait;
1063 1.1 christos return 0;
1064 1.1 christos }
1065 1.1 christos /*
1066 1.1 christos * Silently enforce "nowait" for TCPMUX services since
1067 1.1 christos * they don't have an assigned port to listen on.
1068 1.1 christos */
1069 1.1 christos sep->se_wait = 0;
1070 1.1 christos
1071 1.1 christos if (strncmp(sep->se_proto, "tcp", 3)) {
1072 1.1 christos ERR("bad protocol for tcpmux service %s",
1073 1.1 christos sep->se_service);
1074 1.1 christos return -1;
1075 1.1 christos }
1076 1.1 christos if (sep->se_socktype != SOCK_STREAM) {
1077 1.1 christos ERR("bad socket type for tcpmux service %s",
1078 1.1 christos sep->se_service);
1079 1.1 christos return -1;
1080 1.1 christos }
1081 1.1 christos return 0;
1082 1.1 christos }
1083 1.1 christos
1084 1.1 christos int
1085 1.1 christos parse_server(struct servtab *sep, const char *arg)
1086 1.1 christos {
1087 1.1 christos sep->se_server = newstr(arg);
1088 1.1 christos if (strcmp(sep->se_server, "internal") != 0) {
1089 1.1 christos sep->se_bi = NULL;
1090 1.1 christos return 0;
1091 1.1 christos }
1092 1.1 christos
1093 1.1 christos if (!try_biltin(sep)) {
1094 1.1 christos ERR("Internal service %s unknown", sep->se_service);
1095 1.1 christos return -1;
1096 1.1 christos }
1097 1.1 christos return 0;
1098 1.1 christos }
1099 1.1 christos
1100 1.1 christos /* TODO test to make sure accept filter still works */
1101 1.1 christos void
1102 1.1 christos parse_accept_filter(char *arg, struct servtab *sep)
1103 1.1 christos {
1104 1.1 christos char *accf, *accf_arg;
1105 1.1 christos /* one and only one accept filter */
1106 1.1 christos accf = strchr(arg, ':');
1107 1.1 christos if (accf == NULL)
1108 1.1 christos return;
1109 1.1 christos if (accf != strrchr(arg, ':') || *(accf + 1) == '\0') {
1110 1.1 christos /* more than one || nothing beyond */
1111 1.1 christos sep->se_socktype = -1;
1112 1.1 christos return;
1113 1.1 christos }
1114 1.1 christos
1115 1.1 christos accf++; /* skip delimiter */
1116 1.1 christos strlcpy(sep->se_accf.af_name, accf, sizeof(sep->se_accf.af_name));
1117 1.1 christos accf_arg = strchr(accf, ',');
1118 1.1 christos if (accf_arg == NULL) /* zero or one arg, no more */
1119 1.1 christos return;
1120 1.1 christos
1121 1.1 christos if (strrchr(accf, ',') != accf_arg) {
1122 1.1 christos sep->se_socktype = -1;
1123 1.1 christos } else {
1124 1.1 christos accf_arg++;
1125 1.1 christos strlcpy(sep->se_accf.af_arg, accf_arg,
1126 1.1 christos sizeof(sep->se_accf.af_arg));
1127 1.1 christos }
1128 1.1 christos }
1129 1.1 christos
1130 1.1 christos void
1131 1.1 christos parse_socktype(char* arg, struct servtab* sep)
1132 1.1 christos {
1133 1.1 christos /* stream socket may have an accept filter, only check first chars */
1134 1.1 christos if (strncmp(arg, "stream", sizeof("stream") - 1) == 0)
1135 1.1 christos sep->se_socktype = SOCK_STREAM;
1136 1.1 christos else if (strcmp(arg, "dgram") == 0)
1137 1.1 christos sep->se_socktype = SOCK_DGRAM;
1138 1.1 christos else if (strcmp(arg, "rdm") == 0)
1139 1.1 christos sep->se_socktype = SOCK_RDM;
1140 1.1 christos else if (strcmp(arg, "seqpacket") == 0)
1141 1.1 christos sep->se_socktype = SOCK_SEQPACKET;
1142 1.1 christos else if (strcmp(arg, "raw") == 0)
1143 1.1 christos sep->se_socktype = SOCK_RAW;
1144 1.1 christos else
1145 1.1 christos sep->se_socktype = -1;
1146 1.1 christos }
1147 1.1 christos
1148 1.1 christos static struct servtab
1149 1.1 christos init_servtab(void)
1150 1.1 christos {
1151 1.1 christos /* This does not set every field to default. See enter() as well */
1152 1.1 christos return (struct servtab) {
1153 1.1 christos /*
1154 1.1 christos * Set se_max to non-zero so uninitialized value is not
1155 1.1 christos * a valid value. Useful in v2 syntax parsing.
1156 1.1 christos */
1157 1.1 christos .se_service_max = SERVTAB_UNSPEC_SIZE_T,
1158 1.1 christos .se_ip_max = SERVTAB_UNSPEC_SIZE_T,
1159 1.1 christos .se_wait = SERVTAB_UNSPEC_VAL,
1160 1.1 christos .se_socktype = SERVTAB_UNSPEC_VAL,
1161 1.1 christos .se_rl_ip_list = SLIST_HEAD_INITIALIZER(se_ip_list_head)
1162 1.1 christos /* All other fields initialized to 0 or null */
1163 1.1 christos };
1164 1.1 christos }
1165 1.1 christos
1166 1.1 christos /* Include directives bookkeeping structure */
1167 1.1 christos struct file_list {
1168 1.1 christos /* Absolute path used for checking for circular references */
1169 1.1 christos char *abs;
1170 1.1 christos /* Pointer to the absolute path of the parent config file,
1171 1.1 christos * on the stack */
1172 1.1 christos struct file_list *next;
1173 1.1 christos } *file_list_head;
1174 1.1 christos
1175 1.1 christos static void
1176 1.1 christos include_configs(char *pattern)
1177 1.1 christos {
1178 1.1 christos /* Allocate global per-config state on the thread stack */
1179 1.1 christos const char* save_CONFIG;
1180 1.1 christos FILE *save_fconfig;
1181 1.1 christos size_t save_line_number;
1182 1.1 christos char *save_defhost;
1183 1.1 christos struct file_list new_file;
1184 1.1 christos #ifdef IPSEC
1185 1.1 christos char *save_policy;
1186 1.1 christos #endif
1187 1.1 christos
1188 1.1 christos /* Store current globals on the stack */
1189 1.1 christos save_CONFIG = CONFIG;
1190 1.1 christos save_fconfig = fconfig;
1191 1.1 christos save_line_number = line_number;
1192 1.1 christos save_defhost = defhost;
1193 1.1 christos new_file.abs = realpath(CONFIG, NULL);
1194 1.1 christos new_file.next = file_list_head;
1195 1.1 christos #ifdef IPSEC
1196 1.1 christos save_policy = policy;
1197 1.1 christos #endif
1198 1.1 christos /* Put new_file at the top of the config stack */
1199 1.1 christos file_list_head = &new_file;
1200 1.1 christos read_glob_configs(pattern);
1201 1.1 christos free(new_file.abs);
1202 1.1 christos /* Pop new_file off the stack */
1203 1.1 christos file_list_head = new_file.next;
1204 1.1 christos
1205 1.1 christos /* Restore global per-config state */
1206 1.1 christos CONFIG = save_CONFIG;
1207 1.1 christos fconfig = save_fconfig;
1208 1.1 christos line_number = save_line_number;
1209 1.1 christos defhost = save_defhost;
1210 1.1 christos #ifdef IPSEC
1211 1.1 christos policy = save_policy;
1212 1.1 christos #endif
1213 1.1 christos }
1214 1.1 christos
1215 1.1 christos static void
1216 1.1 christos prepare_next_config(const char *file_name)
1217 1.1 christos {
1218 1.1 christos /* Setup new state that is normally only done in main */
1219 1.1 christos CONFIG = file_name;
1220 1.1 christos
1221 1.1 christos /* Inherit default host and IPsec policy */
1222 1.1 christos defhost = newstr(defhost);
1223 1.1 christos
1224 1.1 christos #ifdef IPSEC
1225 1.1 christos policy = (policy == NULL) ? NULL : newstr(policy);
1226 1.1 christos #endif
1227 1.1 christos }
1228 1.1 christos
1229 1.1 christos static void
1230 1.1 christos read_glob_configs(char *pattern)
1231 1.1 christos {
1232 1.1 christos glob_t results;
1233 1.1 christos char *full_pattern;
1234 1.1 christos int glob_result;
1235 1.1 christos full_pattern = gen_file_pattern(CONFIG, pattern);
1236 1.1 christos
1237 1.1 christos DPRINTCONF("Found include directive '%s'", full_pattern);
1238 1.1 christos
1239 1.1 christos glob_result = glob(full_pattern, GLOB_NOSORT, glob_error, &results);
1240 1.1 christos switch(glob_result) {
1241 1.1 christos case 0:
1242 1.1 christos /* No glob errors */
1243 1.1 christos break;
1244 1.1 christos case GLOB_ABORTED:
1245 1.1 christos ERR("Error while searching for include files");
1246 1.1 christos break;
1247 1.1 christos case GLOB_NOMATCH:
1248 1.1 christos /* It's fine if no files were matched. */
1249 1.1 christos DPRINTCONF("No files matched pattern '%s'", full_pattern);
1250 1.1 christos break;
1251 1.1 christos case GLOB_NOSPACE:
1252 1.1 christos ERR("Error when searching for include files: %s",
1253 1.1 christos strerror(errno));
1254 1.1 christos break;
1255 1.1 christos default:
1256 1.1 christos ERR("Unknown glob(3) error %d", errno);
1257 1.1 christos break;
1258 1.1 christos }
1259 1.1 christos free(full_pattern);
1260 1.1 christos
1261 1.1 christos for (size_t i = 0; i < results.gl_pathc; i++) {
1262 1.1 christos include_matched_path(results.gl_pathv[i]);
1263 1.1 christos }
1264 1.1 christos
1265 1.1 christos globfree(&results);
1266 1.1 christos }
1267 1.1 christos
1268 1.1 christos static void
1269 1.1 christos include_matched_path(char *glob_path)
1270 1.1 christos {
1271 1.1 christos struct stat sb;
1272 1.1 christos char *tmp;
1273 1.1 christos
1274 1.1 christos if (lstat(glob_path, &sb) != 0) {
1275 1.1 christos ERR("Error calling stat on path '%s': %s", glob_path,
1276 1.1 christos strerror(errno));
1277 1.1 christos return;
1278 1.1 christos }
1279 1.1 christos
1280 1.1 christos if (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode)) {
1281 1.1 christos DPRINTCONF("'%s' is not a file.", glob_path);
1282 1.1 christos ERR("The matched path '%s' is not a regular file", glob_path);
1283 1.1 christos return;
1284 1.1 christos }
1285 1.1 christos
1286 1.1 christos DPRINTCONF("Include '%s'", glob_path);
1287 1.1 christos
1288 1.1 christos if (S_ISLNK(sb.st_mode)) {
1289 1.1 christos tmp = glob_path;
1290 1.1 christos glob_path = realpath(tmp, NULL);
1291 1.1 christos }
1292 1.1 christos
1293 1.1 christos /* Ensure the file is not being reincluded .*/
1294 1.1 christos if (check_no_reinclude(glob_path)) {
1295 1.1 christos prepare_next_config(glob_path);
1296 1.1 christos config();
1297 1.1 christos } else {
1298 1.1 christos DPRINTCONF("File '%s' already included in current include "
1299 1.1 christos "chain", glob_path);
1300 1.1 christos WRN("Including file '%s' would cause a circular "
1301 1.1 christos "dependency", glob_path);
1302 1.1 christos }
1303 1.1 christos
1304 1.1 christos if (S_ISLNK(sb.st_mode)) {
1305 1.1 christos free(glob_path);
1306 1.1 christos glob_path = tmp;
1307 1.1 christos }
1308 1.1 christos }
1309 1.1 christos
1310 1.1 christos static bool
1311 1.1 christos check_no_reinclude(const char *glob_path)
1312 1.1 christos {
1313 1.1 christos struct file_list *cur = file_list_head;
1314 1.1 christos char *abs_path = realpath(glob_path, NULL);
1315 1.1 christos
1316 1.1 christos if (abs_path == NULL) {
1317 1.1 christos ERR("Error checking real path for '%s': %s",
1318 1.1 christos glob_path, strerror(errno));
1319 1.1 christos return false;
1320 1.1 christos }
1321 1.1 christos
1322 1.1 christos DPRINTCONF("Absolute path '%s'", abs_path);
1323 1.1 christos
1324 1.1 christos for (cur = file_list_head; cur != NULL; cur = cur->next) {
1325 1.1 christos if (strcmp(cur->abs, abs_path) == 0) {
1326 1.1 christos /* file included more than once */
1327 1.1 christos /* TODO relative or abs path for logging error? */
1328 1.1 christos free(abs_path);
1329 1.1 christos return false;
1330 1.1 christos }
1331 1.1 christos }
1332 1.1 christos free(abs_path);
1333 1.1 christos return true;
1334 1.1 christos }
1335 1.1 christos
1336 1.1 christos /* Resolve the pattern relative to the config file the pattern is from */
1337 1.1 christos static char *
1338 1.1 christos gen_file_pattern(const char *cur_config, const char *pattern)
1339 1.1 christos {
1340 1.1 christos if (pattern[0] == '/') {
1341 1.1 christos /* Absolute paths don't need any normalization */
1342 1.1 christos return newstr(pattern);
1343 1.1 christos }
1344 1.1 christos
1345 1.1 christos /* pattern is relative */
1346 1.1 christos /* Find the end of the file's directory */
1347 1.1 christos size_t i, last = 0;
1348 1.1 christos for (i = 0; cur_config[i] != '\0'; i++) {
1349 1.1 christos if (cur_config[i] == '/') {
1350 1.1 christos last = i;
1351 1.1 christos }
1352 1.1 christos }
1353 1.1 christos
1354 1.1 christos if (last == 0) {
1355 1.1 christos /* cur_config is just a filename, pattern already correct */
1356 1.1 christos return newstr(pattern);
1357 1.1 christos }
1358 1.1 christos
1359 1.1 christos /* Relativize pattern to cur_config file's directory */
1360 1.1 christos char *full_pattern = malloc(last + 1 + strlen(pattern) + 1);
1361 1.1 christos if (full_pattern == NULL) {
1362 1.1 christos syslog(LOG_ERR, "Out of memory.");
1363 1.1 christos exit(EXIT_FAILURE);
1364 1.1 christos }
1365 1.1 christos memcpy(full_pattern, cur_config, last);
1366 1.1 christos full_pattern[last] = '/';
1367 1.1 christos strcpy(&full_pattern[last + 1], pattern);
1368 1.1 christos return full_pattern;
1369 1.1 christos }
1370 1.1 christos
1371 1.1 christos static int
1372 1.1 christos glob_error(const char *path, int error)
1373 1.1 christos {
1374 1.1 christos WRN("Error while resolving path '%s': %s", path, strerror(error));
1375 1.1 christos return 0;
1376 1.1 christos }
1377