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