]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/mount_portalfs/conf.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / mount_portalfs / 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  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)conf.c      8.2 (Berkeley) 3/27/94
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <errno.h>
40 #include <limits.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <regex.h>
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/syslog.h>
49
50 #include "portald.h"
51
52 #define ALLOC(ty)       (xmalloc(sizeof(ty)))
53
54 typedef struct path path;
55 struct path {
56         qelem p_q;              /* 2-way linked list */
57         int p_lno;              /* Line number of this record */
58         char *p_args;           /* copy of arg string (malloc) */
59         char *p_key;            /* Pathname to match (also p_argv[0]) */
60         regex_t p_rx;           /* RE to match against pathname () */
61         int p_rxvalid;          /* non-zero if valid regular expression */
62         int p_argc;             /* number of elements in arg string */
63         char **p_argv;          /* argv[] pointers into arg string (malloc) */
64 };
65
66 static char *conf_file;         /* XXX for regerror */
67 static path *curp;              /* XXX for regerror */
68
69 /*
70  * Add an element to a 2-way list,
71  * just after (pred)
72  */
73 static void ins_que(qelem *elem, qelem *pred)
74 {
75         qelem *p = pred->q_forw;
76         elem->q_back = pred;
77         elem->q_forw = p;
78         pred->q_forw = elem;
79         p->q_back = elem;
80 }
81
82 /*
83  * Remove an element from a 2-way list
84  */
85 static void rem_que(qelem *elem)
86 {
87         qelem *p = elem->q_forw;
88         qelem *p2 = elem->q_back;
89         p2->q_forw = p;
90         p->q_back = p2;
91 }
92
93 /*
94  * Error checking malloc
95  */
96 static void *xmalloc(unsigned siz)
97 {
98         void *p = malloc(siz);
99         if (p)
100                 return (p);
101         syslog(LOG_ALERT, "malloc: failed to get %d bytes", siz);
102         exit(1);
103 }
104
105 /*
106  * Insert the path in the list.
107  * If there is already an element with the same key then
108  * the *second* one is ignored (return 0).  If the key is
109  * not found then the path is added to the end of the list
110  * and 1 is returned.
111  */
112 static int pinsert(path *p0, qelem *q0)
113 {
114         qelem *q;
115
116         if (p0->p_argc == 0)
117                 return (0);
118
119         for (q = q0->q_forw; q != q0; q = q->q_forw) {
120                 path *p = (path *) q;
121                 if (strcmp(p->p_key, p0->p_key) == 0)
122                         return (0);
123         }
124         ins_que(&p0->p_q, q0->q_back);
125         return (1);
126
127 }
128
129 static path *palloc(char *cline, int lno)
130 {
131         int c;
132         char *s;
133         char *key;
134         path *p;
135         char **ap;
136
137         /*
138          * Implement comment chars
139          */
140         s = strchr(cline, '#');
141         if (s)
142                 *s = 0;
143
144         /*
145          * Do a pass through the string to count the number
146          * of arguments
147          */
148         c = 0;
149         key = strdup(cline);
150         for (s = key; s != NULL; ) {
151                 char *val;
152                 while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
153                         ;
154                 if (val)
155                         c++;
156         }
157         c++;
158         free(key);
159
160         if (c <= 1)
161                 return (0);
162
163         /*
164          * Now do another pass and generate a new path structure
165          */
166         p = ALLOC(path);
167         p->p_argc = 0;
168         p->p_argv = xmalloc(c * sizeof(char *));
169         p->p_args = strdup(cline);
170         ap = p->p_argv;
171         for (s = p->p_args; s != NULL; ) {
172                 char *val;
173                 while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
174                         ;
175                 if (val) {
176                         *ap++ = val;
177                         p->p_argc++;
178                 }
179         }
180         *ap = 0;
181
182 #ifdef DEBUG
183         for (c = 0; c < p->p_argc; c++)
184                 printf("%sv[%d] = %s\n", c?"\t":"", c, p->p_argv[c]);
185 #endif
186
187         p->p_key = p->p_argv[0];
188         if (strpbrk(p->p_key, RE_CHARS)) {
189                 int val;
190
191                 curp = p;                       /* XXX */
192                 val = regcomp(&p->p_rx, p->p_key, REG_EXTENDED | REG_NOSUB);
193                 if (val) {
194                         char errbuf[_POSIX2_LINE_MAX];
195                         regerror(val, &p->p_rx, errbuf, sizeof errbuf);
196                         syslog(LOG_ERR, "%s:%d: regcomp %s: %s",
197                                conf_file, curp->p_lno, curp->p_key, errbuf);
198                         regfree(&p->p_rx);
199                         p->p_rxvalid = 0;
200                 } else {
201                         p->p_rxvalid = 1;
202                 }
203                 curp = 0;                       /* XXX */
204         } else {
205                 p->p_rxvalid = 0;
206         }
207         p->p_lno = lno;
208
209         return (p);
210 }
211
212 /*
213  * Free a path structure
214  */
215 static void pfree(path *p)
216 {
217         free(p->p_args);
218         if (p->p_rxvalid) {
219                 regfree(&p->p_rx);
220         }
221         free((char *) p->p_argv);
222         free((char *) p);
223 }
224
225 /*
226  * Discard all currently held path structures on q0.
227  * and add all the ones on xq.
228  */
229 static void preplace(qelem *q0, qelem *xq)
230 {
231         /*
232          * While the list is not empty,
233          * take the first element off the list
234          * and free it.
235          */
236         while (q0->q_forw != q0) {
237                 qelem *q = q0->q_forw;
238                 rem_que(q);
239                 pfree((path *) q);
240         }
241         while (xq->q_forw != xq) {
242                 qelem *q = xq->q_forw;
243                 rem_que(q);
244                 ins_que(q, q0);
245         }
246 }
247
248 /*
249  * Read the lines from the configuration file and
250  * add them to the list of paths.
251  */
252 static void readfp(qelem *q0, FILE *fp)
253 {
254         char cline[LINE_MAX];
255         int nread = 0;
256         qelem q;
257
258         /*
259          * Make a new empty list.
260          */
261         q.q_forw = q.q_back = &q;
262
263         /*
264          * Read the lines from the configuration file.
265          */
266         while (fgets(cline, sizeof(cline), fp)) {
267                 path *p = palloc(cline, nread+1);
268                 if (p && !pinsert(p, &q))
269                         pfree(p);
270                 nread++;
271         }
272
273         /*
274          * If some records were read, then throw
275          * away the old list and replace with the
276          * new one.
277          */
278         if (nread)
279                 preplace(q0, &q);
280 }
281
282 /*
283  * Read the configuration file (conf) and replace
284  * the existing path list with the new version.
285  * If the file is not readable, then no changes take place
286  */
287 void conf_read(qelem *q, char *conf)
288 {
289         FILE *fp = fopen(conf, "r");
290         if (fp) {
291                 conf_file = conf;               /* XXX */
292                 readfp(q, fp);
293                 conf_file = 0;          /* XXX */
294                 (void) fclose(fp);
295         } else {
296                 syslog(LOG_ERR, "open config file \"%s\": %s", conf, strerror(errno));
297         }
298 }
299
300
301 char **conf_match(qelem *q0, char *key)
302 {
303         qelem *q;
304
305         for (q = q0->q_forw; q != q0; q = q->q_forw) {
306                 path *p = (path *) q;
307                 if (p->p_rxvalid) {
308                         if (!regexec(&p->p_rx, key, 0, 0, 0)) {
309                                 return p->p_argv + 1;
310                         }
311                 } else {
312                         if (strncmp(p->p_key, key, strlen(p->p_key)) == 0)
313                                 return (p->p_argv+1);
314                 }
315         }
316
317         return (0);
318 }