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