- addlog(...) is identical to log(-1, ...). Get rid of it to avoid
[dragonfly.git] / sbin / mount_portal / conf.c
blob46d10a278f650b6d8339e12baeb878fcf809b8f1
1 /*
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 * All rights reserved.
6 * This code is derived from software donated to Berkeley by
7 * Jan-Simon Pendry.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
37 * @(#)conf.c 8.2 (Berkeley) 3/27/94
39 * $FreeBSD: src/sbin/mount_portal/conf.c,v 1.8 1999/08/28 00:13:35 peter Exp $
40 * $DragonFly: src/sbin/mount_portal/conf.c,v 1.5 2005/11/06 12:36:40 swildner Exp $
43 #include <errno.h>
44 #include <limits.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <regex.h>
50 #include <sys/types.h>
51 #include <sys/param.h>
52 #include <sys/syslog.h>
54 #include "portald.h"
56 #define ALLOC(ty) (xmalloc(sizeof(ty)))
58 typedef struct path path;
59 struct path {
60 qelem p_q; /* 2-way linked list */
61 int p_lno; /* Line number of this record */
62 char *p_args; /* copy of arg string (malloc) */
63 char *p_key; /* Pathname to match (also p_argv[0]) */
64 regex_t p_rx; /* RE to match against pathname () */
65 int p_rxvalid; /* non-zero if valid regular expression */
66 int p_argc; /* number of elements in arg string */
67 char **p_argv; /* argv[] pointers into arg string (malloc) */
70 static char *conf_file; /* XXX for regerror */
71 static path *curp; /* XXX for regerror */
74 * Add an element to a 2-way list,
75 * just after (pred)
77 static void
78 ins_que(qelem *elem, qelem *pred)
80 qelem *p = pred->q_forw;
81 elem->q_back = pred;
82 elem->q_forw = p;
83 pred->q_forw = elem;
84 p->q_back = elem;
88 * Remove an element from a 2-way list
90 static void
91 rem_que(qelem *elem)
93 qelem *p = elem->q_forw;
94 qelem *p2 = elem->q_back;
95 p2->q_forw = p;
96 p->q_back = p2;
100 * Error checking malloc
102 static void *
103 xmalloc(unsigned siz)
105 void *p = malloc(siz);
106 if (p)
107 return (p);
108 syslog(LOG_ALERT, "malloc: failed to get %d bytes", siz);
109 exit(1);
113 * Insert the path in the list.
114 * If there is already an element with the same key then
115 * the *second* one is ignored (return 0). If the key is
116 * not found then the path is added to the end of the list
117 * and 1 is returned.
119 static int
120 pinsert(path *p0, qelem *q0)
122 qelem *q;
124 if (p0->p_argc == 0)
125 return (0);
127 for (q = q0->q_forw; q != q0; q = q->q_forw) {
128 path *p = (path *) q;
129 if (strcmp(p->p_key, p0->p_key) == 0)
130 return (0);
132 ins_que(&p0->p_q, q0->q_back);
133 return (1);
137 static path *
138 palloc(char *cline, int lno)
140 int c;
141 char *s;
142 char *key;
143 path *p;
144 char **ap;
147 * Implement comment chars
149 s = strchr(cline, '#');
150 if (s)
151 *s = 0;
154 * Do a pass through the string to count the number
155 * of arguments
157 c = 0;
158 key = strdup(cline);
159 for (s = key; s != NULL; ) {
160 char *val;
161 while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
163 if (val)
164 c++;
166 c++;
167 free(key);
169 if (c <= 1)
170 return (0);
173 * Now do another pass and generate a new path structure
175 p = ALLOC(path);
176 p->p_argc = 0;
177 p->p_argv = xmalloc(c * sizeof(char *));
178 p->p_args = strdup(cline);
179 ap = p->p_argv;
180 for (s = p->p_args; s != NULL; ) {
181 char *val;
182 while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
184 if (val) {
185 *ap++ = val;
186 p->p_argc++;
189 *ap = 0;
191 #ifdef DEBUG
192 for (c = 0; c < p->p_argc; c++)
193 printf("%sv[%d] = %s\n", c?"\t":"", c, p->p_argv[c]);
194 #endif
196 p->p_key = p->p_argv[0];
197 if (strpbrk(p->p_key, RE_CHARS)) {
198 int val;
200 curp = p; /* XXX */
201 val = regcomp(&p->p_rx, p->p_key, REG_EXTENDED | REG_NOSUB);
202 if (val) {
203 char errbuf[_POSIX2_LINE_MAX];
204 regerror(val, &p->p_rx, errbuf, sizeof errbuf);
205 syslog(LOG_ERR, "%s:%d: regcomp %s: %s",
206 conf_file, curp->p_lno, curp->p_key, errbuf);
207 regfree(&p->p_rx);
208 p->p_rxvalid = 0;
209 } else {
210 p->p_rxvalid = 1;
212 curp = 0; /* XXX */
213 } else {
214 p->p_rxvalid = 0;
216 p->p_lno = lno;
218 return (p);
222 * Free a path structure
224 static void
225 pfree(path *p)
227 free(p->p_args);
228 if (p->p_rxvalid) {
229 regfree(&p->p_rx);
231 free((char *) p->p_argv);
232 free((char *) p);
236 * Discard all currently held path structures on q0.
237 * and add all the ones on xq.
239 static void
240 preplace(qelem *q0, qelem *xq)
243 * While the list is not empty,
244 * take the first element off the list
245 * and free it.
247 while (q0->q_forw != q0) {
248 qelem *q = q0->q_forw;
249 rem_que(q);
250 pfree((path *) q);
252 while (xq->q_forw != xq) {
253 qelem *q = xq->q_forw;
254 rem_que(q);
255 ins_que(q, q0);
260 * Read the lines from the configuration file and
261 * add them to the list of paths.
263 static void
264 readfp(qelem *q0, FILE *fp)
266 char cline[LINE_MAX];
267 int nread = 0;
268 qelem q;
271 * Make a new empty list.
273 q.q_forw = q.q_back = &q;
276 * Read the lines from the configuration file.
278 while (fgets(cline, sizeof(cline), fp)) {
279 path *p = palloc(cline, nread+1);
280 if (p && !pinsert(p, &q))
281 pfree(p);
282 nread++;
286 * If some records were read, then throw
287 * away the old list and replace with the
288 * new one.
290 if (nread)
291 preplace(q0, &q);
295 * Read the configuration file (conf) and replace
296 * the existing path list with the new version.
297 * If the file is not readable, then no changes take place
299 void
300 conf_read(qelem *q, char *conf)
302 FILE *fp = fopen(conf, "r");
303 if (fp) {
304 conf_file = conf; /* XXX */
305 readfp(q, fp);
306 conf_file = 0; /* XXX */
307 fclose(fp);
308 } else {
309 syslog(LOG_ERR, "open config file \"%s\": %s", conf, strerror(errno));
314 char **
315 conf_match(qelem *q0, char *key)
317 qelem *q;
319 for (q = q0->q_forw; q != q0; q = q->q_forw) {
320 path *p = (path *) q;
321 if (p->p_rxvalid) {
322 if (!regexec(&p->p_rx, key, 0, 0, 0)) {
323 return p->p_argv + 1;
325 } else {
326 if (strncmp(p->p_key, key, strlen(p->p_key)) == 0)
327 return (p->p_argv+1);
331 return (0);