90720afca6ccd01b2e345d9172d78203f3b7aafe
[dragonfly.git] / sbin / mount_portal / conf.c
1 /*
2  * Copyright (c) 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * All rights reserved.
5  *
6  * This code is derived from software donated to Berkeley by
7  * Jan-Simon Pendry.
8  *
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.
24  *
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.
36  *
37  *      @(#)conf.c      8.2 (Berkeley) 3/27/94
38  *
39  * $FreeBSD: src/sbin/mount_portal/conf.c,v 1.8 1999/08/28 00:13:35 peter Exp $
40  */
41
42 #include <errno.h>
43 #include <limits.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <regex.h>
49 #include <sys/types.h>
50 #include <sys/param.h>
51 #include <sys/syslog.h>
52
53 #include "portald.h"
54
55 #define ALLOC(ty)       (xmalloc(sizeof(ty)))
56
57 typedef struct path path;
58 struct path {
59         qelem p_q;              /* 2-way linked list */
60         int p_lno;              /* Line number of this record */
61         char *p_args;           /* copy of arg string (malloc) */
62         char *p_key;            /* Pathname to match (also p_argv[0]) */
63         regex_t p_rx;           /* RE to match against pathname () */
64         int p_rxvalid;          /* non-zero if valid regular expression */
65         int p_argc;             /* number of elements in arg string */
66         char **p_argv;          /* argv[] pointers into arg string (malloc) */
67 };
68
69 static char *conf_file;         /* XXX for regerror */
70 static path *curp;              /* XXX for regerror */
71
72 /*
73  * Add an element to a 2-way list,
74  * just after (pred)
75  */
76 static void
77 ins_que(qelem *elem, qelem *pred)
78 {
79         qelem *p = pred->q_forw;
80         elem->q_back = pred;
81         elem->q_forw = p;
82         pred->q_forw = elem;
83         p->q_back = elem;
84 }
85
86 /*
87  * Remove an element from a 2-way list
88  */
89 static void
90 rem_que(qelem *elem)
91 {
92         qelem *p = elem->q_forw;
93         qelem *p2 = elem->q_back;
94         p2->q_forw = p;
95         p->q_back = p2;
96 }
97
98 /*
99  * Error checking malloc
100  */
101 static void *
102 xmalloc(unsigned siz)
103 {
104         void *p = malloc(siz);
105         if (p)
106                 return (p);
107         syslog(LOG_ALERT, "malloc: failed to get %d bytes", siz);
108         exit(1);
109 }
110
111 /*
112  * Insert the path in the list.
113  * If there is already an element with the same key then
114  * the *second* one is ignored (return 0).  If the key is
115  * not found then the path is added to the end of the list
116  * and 1 is returned.
117  */
118 static int
119 pinsert(path *p0, qelem *q0)
120 {
121         qelem *q;
122
123         if (p0->p_argc == 0)
124                 return (0);
125
126         for (q = q0->q_forw; q != q0; q = q->q_forw) {
127                 path *p = (path *) q;
128                 if (strcmp(p->p_key, p0->p_key) == 0)
129                         return (0);
130         }
131         ins_que(&p0->p_q, q0->q_back);
132         return (1);
133
134 }
135
136 static path *
137 palloc(char *cline, int lno)
138 {
139         int c;
140         char *s;
141         char *key;
142         path *p;
143         char **ap;
144
145         /*
146          * Implement comment chars
147          */
148         s = strchr(cline, '#');
149         if (s)
150                 *s = 0;
151
152         /*
153          * Do a pass through the string to count the number
154          * of arguments
155          */
156         c = 0;
157         key = strdup(cline);
158         for (s = key; s != NULL; ) {
159                 char *val;
160                 while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
161                         ;
162                 if (val)
163                         c++;
164         }
165         c++;
166         free(key);
167
168         if (c <= 1)
169                 return (0);
170
171         /*
172          * Now do another pass and generate a new path structure
173          */
174         p = ALLOC(path);
175         p->p_argc = 0;
176         p->p_argv = xmalloc(c * sizeof(char *));
177         p->p_args = strdup(cline);
178         ap = p->p_argv;
179         for (s = p->p_args; s != NULL; ) {
180                 char *val;
181                 while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
182                         ;
183                 if (val) {
184                         *ap++ = val;
185                         p->p_argc++;
186                 }
187         }
188         *ap = NULL;
189
190 #ifdef DEBUG
191         for (c = 0; c < p->p_argc; c++)
192                 printf("%sv[%d] = %s\n", c?"\t":"", c, p->p_argv[c]);
193 #endif
194
195         p->p_key = p->p_argv[0];
196         if (strpbrk(p->p_key, RE_CHARS)) {
197                 int val;
198
199                 curp = p;                       /* XXX */
200                 val = regcomp(&p->p_rx, p->p_key, REG_EXTENDED | REG_NOSUB);
201                 if (val) {
202                         char errbuf[_POSIX2_LINE_MAX];
203                         regerror(val, &p->p_rx, errbuf, sizeof errbuf);
204                         syslog(LOG_ERR, "%s:%d: regcomp %s: %s",
205                                conf_file, curp->p_lno, curp->p_key, errbuf);
206                         regfree(&p->p_rx);
207                         p->p_rxvalid = 0;
208                 } else {
209                         p->p_rxvalid = 1;
210                 }
211                 curp = NULL;                    /* XXX */
212         } else {
213                 p->p_rxvalid = 0;
214         }
215         p->p_lno = lno;
216
217         return (p);
218 }
219
220 /*
221  * Free a path structure
222  */
223 static void
224 pfree(path *p)
225 {
226         free(p->p_args);
227         if (p->p_rxvalid) {
228                 regfree(&p->p_rx);
229         }
230         free((char *) p->p_argv);
231         free((char *) p);
232 }
233
234 /*
235  * Discard all currently held path structures on q0.
236  * and add all the ones on xq.
237  */
238 static void
239 preplace(qelem *q0, qelem *xq)
240 {
241         /*
242          * While the list is not empty,
243          * take the first element off the list
244          * and free it.
245          */
246         while (q0->q_forw != q0) {
247                 qelem *q = q0->q_forw;
248                 rem_que(q);
249                 pfree((path *) q);
250         }
251         while (xq->q_forw != xq) {
252                 qelem *q = xq->q_forw;
253                 rem_que(q);
254                 ins_que(q, q0);
255         }
256 }
257
258 /*
259  * Read the lines from the configuration file and
260  * add them to the list of paths.
261  */
262 static void
263 readfp(qelem *q0, FILE *fp)
264 {
265         char cline[LINE_MAX];
266         int nread = 0;
267         qelem q;
268
269         /*
270          * Make a new empty list.
271          */
272         q.q_forw = q.q_back = &q;
273
274         /*
275          * Read the lines from the configuration file.
276          */
277         while (fgets(cline, sizeof(cline), fp)) {
278                 path *p = palloc(cline, nread+1);
279                 if (p && !pinsert(p, &q))
280                         pfree(p);
281                 nread++;
282         }
283
284         /*
285          * If some records were read, then throw
286          * away the old list and replace with the
287          * new one.
288          */
289         if (nread)
290                 preplace(q0, &q);
291 }
292
293 /*
294  * Read the configuration file (conf) and replace
295  * the existing path list with the new version.
296  * If the file is not readable, then no changes take place
297  */
298 void
299 conf_read(qelem *q, char *conf)
300 {
301         FILE *fp = fopen(conf, "r");
302         if (fp) {
303                 conf_file = conf;               /* XXX */
304                 readfp(q, fp);
305                 conf_file = NULL;               /* XXX */
306                 fclose(fp);
307         } else {
308                 syslog(LOG_ERR, "open config file \"%s\": %s", conf, strerror(errno));
309         }
310 }
311
312
313 char **
314 conf_match(qelem *q0, char *key)
315 {
316         qelem *q;
317
318         for (q = q0->q_forw; q != q0; q = q->q_forw) {
319                 path *p = (path *) q;
320                 if (p->p_rxvalid) {
321                         if (!regexec(&p->p_rx, key, 0, 0, 0)) {
322                                 return p->p_argv + 1;
323                         }
324                 } else {
325                         if (strncmp(p->p_key, key, strlen(p->p_key)) == 0)
326                                 return (p->p_argv+1);
327                 }
328         }
329
330         return (0);
331 }