xref: /netbsd-src/sbin/mount_portal/conf.c (revision 4ab11b0d22f69a963490643d22723c83aa0d860f)
1*4ab11b0dSkre /*	$NetBSD: conf.c,v 1.14 2019/05/23 04:34:25 kre Exp $	*/
20114e805Scgd 
3b8b85029Scgd /*
4b8b85029Scgd  * Copyright (c) 1992, 1993
5b8b85029Scgd  *	The Regents of the University of California.  All rights reserved.
6b8b85029Scgd  *
7b8b85029Scgd  * This code is derived from software donated to Berkeley by
8b8b85029Scgd  * Jan-Simon Pendry.
9b8b85029Scgd  *
10b8b85029Scgd  * Redistribution and use in source and binary forms, with or without
11b8b85029Scgd  * modification, are permitted provided that the following conditions
12b8b85029Scgd  * are met:
13b8b85029Scgd  * 1. Redistributions of source code must retain the above copyright
14b8b85029Scgd  *    notice, this list of conditions and the following disclaimer.
15b8b85029Scgd  * 2. Redistributions in binary form must reproduce the above copyright
16b8b85029Scgd  *    notice, this list of conditions and the following disclaimer in the
17b8b85029Scgd  *    documentation and/or other materials provided with the distribution.
18276d62f6Sagc  * 3. Neither the name of the University nor the names of its contributors
19b8b85029Scgd  *    may be used to endorse or promote products derived from this software
20b8b85029Scgd  *    without specific prior written permission.
21b8b85029Scgd  *
22b8b85029Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23b8b85029Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24b8b85029Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25b8b85029Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26b8b85029Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27b8b85029Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28b8b85029Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29b8b85029Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30b8b85029Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31b8b85029Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32b8b85029Scgd  * SUCH DAMAGE.
33b8b85029Scgd  *
345922d844Smycroft  *	from: Id: conf.c,v 1.2 1992/05/27 07:09:27 jsp Exp
350114e805Scgd  *	@(#)conf.c	8.2 (Berkeley) 3/27/94
36b8b85029Scgd  */
37b8b85029Scgd 
384eb9f40aSlukem #include <sys/cdefs.h>
394eb9f40aSlukem #ifndef lint
40*4ab11b0dSkre __RCSID("$NetBSD: conf.c,v 1.14 2019/05/23 04:34:25 kre Exp $");
414eb9f40aSlukem #endif /* not lint */
424eb9f40aSlukem 
4373367a6dSjdolecek #include <sys/types.h>
4473367a6dSjdolecek #include <sys/param.h>
4573367a6dSjdolecek #include <sys/syslog.h>
46b8b85029Scgd #include <stdio.h>
47b8b85029Scgd #include <stdlib.h>
48b8b85029Scgd #include <unistd.h>
49b8b85029Scgd #include <string.h>
50b8b85029Scgd #include <errno.h>
51b8b85029Scgd #include <limits.h>
5273367a6dSjdolecek #include <regex.h>
53b8b85029Scgd 
54b8b85029Scgd #include "portald.h"
55b8b85029Scgd 
56b8b85029Scgd #define	ALLOC(ty)	(xmalloc(sizeof(ty)))
57b8b85029Scgd 
58b8b85029Scgd typedef struct path path;
59b8b85029Scgd struct path {
60b8b85029Scgd 	qelem p_q;		/* 2-way linked list */
61b8b85029Scgd 	int p_lno;		/* Line number of this record */
62b8b85029Scgd 	char *p_args;		/* copy of arg string (malloc) */
63b8b85029Scgd 	char *p_key;		/* Pathname to match (also p_argv[0]) */
6473367a6dSjdolecek 	regex_t p_re;		/* RE to match against pathname (malloc) */
6573367a6dSjdolecek 	int p_use_re;		/* true if entry is RE */
66b8b85029Scgd 	int p_argc;		/* number of elements in arg string */
67b8b85029Scgd 	char **p_argv;		/* argv[] pointers into arg string (malloc) */
68b8b85029Scgd };
69b8b85029Scgd 
70a8d83732Sxtraeme static	void	ins_que(qelem *, qelem *);
71a8d83732Sxtraeme static	path   *palloc(char *, int, const char *);
72a8d83732Sxtraeme static	void	pfree(path *);
73a8d83732Sxtraeme static	int	pinsert(path *, qelem *);
74a8d83732Sxtraeme static	void	preplace(qelem *, qelem *);
75a8d83732Sxtraeme static	void	readfp(qelem *, FILE *, const char *);
76a8d83732Sxtraeme static	void	rem_que(qelem *);
77a8d83732Sxtraeme static	void   *xmalloc(size_t);
784eb9f40aSlukem 
79b8b85029Scgd /*
80b8b85029Scgd  * Add an element to a 2-way list,
81b8b85029Scgd  * just after (pred)
82b8b85029Scgd  */
834eb9f40aSlukem static void
ins_que(qelem * elem,qelem * pred)84a8d83732Sxtraeme ins_que(qelem *elem, qelem *pred)
85b8b85029Scgd {
86b8b85029Scgd 	qelem *p = pred->q_forw;
87b8b85029Scgd 	elem->q_back = pred;
88b8b85029Scgd 	elem->q_forw = p;
89b8b85029Scgd 	pred->q_forw = elem;
90b8b85029Scgd 	p->q_back = elem;
91b8b85029Scgd }
92b8b85029Scgd 
93b8b85029Scgd /*
94b8b85029Scgd  * Remove an element from a 2-way list
95b8b85029Scgd  */
964eb9f40aSlukem static void
rem_que(qelem * elem)97a8d83732Sxtraeme rem_que(qelem *elem)
98b8b85029Scgd {
99b8b85029Scgd 	qelem *p = elem->q_forw;
100b8b85029Scgd 	qelem *p2 = elem->q_back;
101b8b85029Scgd 	p2->q_forw = p;
102b8b85029Scgd 	p->q_back = p2;
103b8b85029Scgd }
104b8b85029Scgd 
105b8b85029Scgd /*
106b8b85029Scgd  * Error checking malloc
107b8b85029Scgd  */
1084eb9f40aSlukem static void *
xmalloc(size_t siz)109a8d83732Sxtraeme xmalloc(size_t siz)
110b8b85029Scgd {
111b8b85029Scgd 	void *p = malloc(siz);
112de0c4852Skre 
113b8b85029Scgd 	if (p)
114de0c4852Skre 		return p;
115de0c4852Skre 
116415d1abdSlukem 	syslog(LOG_ERR, "malloc: failed to get %lu bytes", (u_long)siz);
117b8b85029Scgd 	exit(1);
118b8b85029Scgd }
119b8b85029Scgd 
120b8b85029Scgd /*
121b8b85029Scgd  * Insert the path in the list.
122b8b85029Scgd  * If there is already an element with the same key then
123b8b85029Scgd  * the *second* one is ignored (return 0).  If the key is
124b8b85029Scgd  * not found then the path is added to the end of the list
125b8b85029Scgd  * and 1 is returned.
126b8b85029Scgd  */
1274eb9f40aSlukem static int
pinsert(path * p0,qelem * q0)128a8d83732Sxtraeme pinsert(path *p0, qelem *q0)
129b8b85029Scgd {
130b8b85029Scgd 	qelem *q;
131b8b85029Scgd 
132b8b85029Scgd 	if (p0->p_argc == 0)
133de0c4852Skre 		return 0;
134b8b85029Scgd 
135b8b85029Scgd 	for (q = q0->q_forw; q != q0; q = q->q_forw) {
136b8b85029Scgd 		path *p = (path *)q;
137de0c4852Skre 
138b8b85029Scgd 		if (strcmp(p->p_key, p0->p_key) == 0)
139de0c4852Skre 			return 0;
140b8b85029Scgd 	}
141b8b85029Scgd 	ins_que(&p0->p_q, q0->q_back);
142de0c4852Skre 	return 1;
143b8b85029Scgd 
144b8b85029Scgd }
145b8b85029Scgd 
1464eb9f40aSlukem static path *
palloc(char * cline,int lno,const char * conf_file)147a8d83732Sxtraeme palloc(char *cline, int lno, const char *conf_file)
148b8b85029Scgd {
14973367a6dSjdolecek 	int c, errcode;
150b8b85029Scgd 	char *s;
151b8b85029Scgd 	char *key;
152b8b85029Scgd 	path *p;
153b8b85029Scgd 	char **ap;
154b8b85029Scgd 
155b8b85029Scgd 	/*
156b8b85029Scgd 	 * Do a pass through the string to count the number
157*4ab11b0dSkre 	 * of arguments.   Stop if we encounter a comment.
158b8b85029Scgd 	 */
159b8b85029Scgd 	c = 0;
160b8b85029Scgd 	key = strdup(cline);
161b8b85029Scgd 	for (s = key; s != NULL; ) {
162b8b85029Scgd 		char *val;
163de0c4852Skre 
164*4ab11b0dSkre 		if (*s == '#') {	/* '#" at beginning of word */
165*4ab11b0dSkre 			cline[s-key] = '\0';	/* delete comment -> EOL */
166*4ab11b0dSkre 			break;
167*4ab11b0dSkre 		}
168*4ab11b0dSkre 
169b8b85029Scgd 		while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
170b8b85029Scgd 			;
171b8b85029Scgd 		if (val)
172b8b85029Scgd 			c++;
173b8b85029Scgd 	}
174b8b85029Scgd 	c++;
175b8b85029Scgd 	free(key);
176b8b85029Scgd 
177b8b85029Scgd 	if (c <= 1)
178de0c4852Skre 		return 0;
179b8b85029Scgd 
180b8b85029Scgd 	/*
181b8b85029Scgd 	 * Now do another pass and generate a new path structure
182b8b85029Scgd 	 */
183b8b85029Scgd 	p = ALLOC(path);
184b8b85029Scgd 	p->p_argc = 0;
185b8b85029Scgd 	p->p_argv = xmalloc(c * sizeof(char *));
186b8b85029Scgd 	p->p_args = strdup(cline);
187b8b85029Scgd 	ap = p->p_argv;
188b8b85029Scgd 	for (s = p->p_args; s != NULL; ) {
189b8b85029Scgd 		char *val;
190de0c4852Skre 
191b8b85029Scgd 		while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
192b8b85029Scgd 			;
193b8b85029Scgd 		if (val) {
194b8b85029Scgd 			*ap++ = val;
195b8b85029Scgd 			p->p_argc++;
196b8b85029Scgd 		}
197b8b85029Scgd 	}
198b8b85029Scgd 	*ap = 0;
199b8b85029Scgd 
200b8b85029Scgd #ifdef DEBUG
201b8b85029Scgd 	for (c = 0; c < p->p_argc; c++)
202b8b85029Scgd 		printf("%sv[%d] = %s\n", c?"\t":"", c, p->p_argv[c]);
203b8b85029Scgd #endif
204b8b85029Scgd 
205b8b85029Scgd 	p->p_key = p->p_argv[0];
20673367a6dSjdolecek 	p->p_use_re = 0;
207b8b85029Scgd 	if (strpbrk(p->p_key, RE_CHARS)) {
20873367a6dSjdolecek 		errcode = regcomp(&p->p_re, p->p_key, REG_EXTENDED|REG_NOSUB);
20973367a6dSjdolecek 		if (errcode == 0)
21073367a6dSjdolecek 			p->p_use_re = 1;
21173367a6dSjdolecek 		else {
21273367a6dSjdolecek 			char buf[200];
21373367a6dSjdolecek 			regerror(errcode, &p->p_re, buf, sizeof(buf));
21473367a6dSjdolecek 
215415d1abdSlukem 			syslog(LOG_WARNING, "%s, line %d: regcomp \"%s\": %s",
21673367a6dSjdolecek 	  		  conf_file, p->p_lno, p->p_key, buf);
21773367a6dSjdolecek 		}
21873367a6dSjdolecek 	}
219b8b85029Scgd 	p->p_lno = lno;
220b8b85029Scgd 
221de0c4852Skre 	return p;
222b8b85029Scgd }
223b8b85029Scgd 
224b8b85029Scgd /*
225b8b85029Scgd  * Free a path structure
226b8b85029Scgd  */
2274eb9f40aSlukem static void
pfree(path * p)228a8d83732Sxtraeme pfree(path *p)
229b8b85029Scgd {
230b8b85029Scgd 	free(p->p_args);
231b8b85029Scgd 	free((char *)p->p_argv);
23273367a6dSjdolecek 	if (p->p_use_re)
23373367a6dSjdolecek 		regfree(&p->p_re);
234b8b85029Scgd 	free((char *)p);
235b8b85029Scgd }
236b8b85029Scgd 
237b8b85029Scgd /*
238b8b85029Scgd  * Discard all currently held path structures on q0.
239b8b85029Scgd  * and add all the ones on xq.
240b8b85029Scgd  */
2414eb9f40aSlukem static void
preplace(qelem * q0,qelem * xq)242a8d83732Sxtraeme preplace(qelem *q0, qelem *xq)
243b8b85029Scgd {
244b8b85029Scgd 	/*
245b8b85029Scgd 	 * While the list is not empty,
246b8b85029Scgd 	 * take the first element off the list
247b8b85029Scgd 	 * and free it.
248b8b85029Scgd 	 */
249b8b85029Scgd 	while (q0->q_forw != q0) {
2505922d844Smycroft 		qelem *q = q0->q_forw;
251b8b85029Scgd 		rem_que(q);
252b8b85029Scgd 		pfree((path *)q);
253b8b85029Scgd 	}
254b8b85029Scgd 	while (xq->q_forw != xq) {
255b8b85029Scgd 		qelem *q = xq->q_forw;
256b8b85029Scgd 		rem_que(q);
257b8b85029Scgd 		ins_que(q, q0);
258b8b85029Scgd 	}
259b8b85029Scgd }
260b8b85029Scgd 
261b8b85029Scgd /*
262b8b85029Scgd  * Read the lines from the configuration file and
263b8b85029Scgd  * add them to the list of paths.
264b8b85029Scgd  */
2654eb9f40aSlukem static void
readfp(qelem * q0,FILE * fp,const char * conf_file)266a8d83732Sxtraeme readfp(qelem *q0, FILE *fp, const char *conf_file)
267b8b85029Scgd {
268b8b85029Scgd 	char cline[LINE_MAX];
269b8b85029Scgd 	int nread = 0;
270b8b85029Scgd 	qelem q;
271b8b85029Scgd 
272b8b85029Scgd 	/*
273b8b85029Scgd 	 * Make a new empty list.
274b8b85029Scgd 	 */
275b8b85029Scgd 	q.q_forw = q.q_back = &q;
276b8b85029Scgd 
277b8b85029Scgd 	/*
278b8b85029Scgd 	 * Read the lines from the configuration file.
279b8b85029Scgd 	 */
280b8b85029Scgd 	while (fgets(cline, sizeof(cline), fp)) {
28173367a6dSjdolecek 		path *p = palloc(cline, nread+1, conf_file);
282de0c4852Skre 
283b8b85029Scgd 		if (p && !pinsert(p, &q))
284b8b85029Scgd 			pfree(p);
285b8b85029Scgd 		nread++;
286b8b85029Scgd 	}
287b8b85029Scgd 
288b8b85029Scgd 	/*
289b8b85029Scgd 	 * If some records were read, then throw
290b8b85029Scgd 	 * away the old list and replace with the
291b8b85029Scgd 	 * new one.
292b8b85029Scgd 	 */
293b8b85029Scgd 	if (nread)
294b8b85029Scgd 		preplace(q0, &q);
295b8b85029Scgd }
296b8b85029Scgd 
297b8b85029Scgd /*
298b8b85029Scgd  * Read the configuration file (conf) and replace
299b8b85029Scgd  * the existing path list with the new version.
300b8b85029Scgd  * If the file is not readable, then no changes take place
301b8b85029Scgd  */
3026ff8a427Spooka int
conf_read(qelem * q,const char * conf)3036ff8a427Spooka conf_read(qelem *q, const char *conf)
304b8b85029Scgd {
305b8b85029Scgd 	FILE *fp = fopen(conf, "r");
306de0c4852Skre 
307b8b85029Scgd 	if (fp) {
30873367a6dSjdolecek 		readfp(q, fp, conf);
309b8b85029Scgd 		(void)fclose(fp);
3106ff8a427Spooka 		return 0;
3116ff8a427Spooka 	} else {
312de0c4852Skre 		int sverrno = errno;
313de0c4852Skre 
314415d1abdSlukem 		syslog(LOG_WARNING, "open config file \"%s\": %m", conf);
3156ff8a427Spooka 		errno = sverrno;
3166ff8a427Spooka 		return -1;
3176ff8a427Spooka 	}
318b8b85029Scgd }
319b8b85029Scgd 
320b8b85029Scgd 
3214eb9f40aSlukem char **
conf_match(qelem * q0,char * key)322a8d83732Sxtraeme conf_match(qelem *q0, char *key)
323b8b85029Scgd {
324b8b85029Scgd 	qelem *q;
325b8b85029Scgd 
326b8b85029Scgd 	for (q = q0->q_forw; q != q0; q = q->q_forw) {
327b8b85029Scgd 		path *p = (path *)q;
328de0c4852Skre 
32973367a6dSjdolecek 		if (p->p_use_re) {
33073367a6dSjdolecek 			if (regexec(&p->p_re, key, 0, NULL, 0) == 0)
331de0c4852Skre 				return p->p_argv + 1;
332b8b85029Scgd 		} else {
333b8b85029Scgd 			if (strncmp(p->p_key, key, strlen(p->p_key)) == 0)
334de0c4852Skre 				return p->p_argv + 1;
335b8b85029Scgd 		}
336b8b85029Scgd 	}
337b8b85029Scgd 
338de0c4852Skre 	return 0;
339b8b85029Scgd }
340