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