conf.c revision 1.6 1 /* $NetBSD: conf.c,v 1.6 1997/09/16 08:37:11 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software donated to Berkeley by
8 * Jan-Simon Pendry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * from: Id: conf.c,v 1.2 1992/05/27 07:09:27 jsp Exp
39 * @(#)conf.c 8.2 (Berkeley) 3/27/94
40 */
41
42 #include <sys/cdefs.h>
43 #ifndef lint
44 __RCSID("$NetBSD: conf.c,v 1.6 1997/09/16 08:37:11 mrg Exp $");
45 #endif /* not lint */
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <string.h>
51 #include <errno.h>
52 #include <limits.h>
53 #include <regexp.h>
54 #include <sys/types.h>
55 #include <sys/param.h>
56 #include <sys/syslog.h>
57
58 #include "portald.h"
59
60 #define ALLOC(ty) (xmalloc(sizeof(ty)))
61
62 typedef struct path path;
63 struct path {
64 qelem p_q; /* 2-way linked list */
65 int p_lno; /* Line number of this record */
66 char *p_args; /* copy of arg string (malloc) */
67 char *p_key; /* Pathname to match (also p_argv[0]) */
68 regexp *p_re; /* RE to match against pathname (malloc) */
69 int p_argc; /* number of elements in arg string */
70 char **p_argv; /* argv[] pointers into arg string (malloc) */
71 };
72
73 static void ins_que __P((qelem *, qelem *));
74 static path *palloc __P((char *, int));
75 static void pfree __P((path *));
76 static int pinsert __P((path *, qelem *));
77 static void preplace __P((qelem *, qelem *));
78 static void readfp __P((qelem *, FILE *));
79 void regerror __P((const char *));
80 static void rem_que __P((qelem *));
81 static void *xmalloc __P((size_t));
82
83 static char *conf_file; /* XXX for regerror */
84 static path *curp; /* XXX for regerror */
85
86 /*
87 * Add an element to a 2-way list,
88 * just after (pred)
89 */
90 static void
91 ins_que(elem, pred)
92 qelem *elem, *pred;
93 {
94 qelem *p = pred->q_forw;
95 elem->q_back = pred;
96 elem->q_forw = p;
97 pred->q_forw = elem;
98 p->q_back = elem;
99 }
100
101 /*
102 * Remove an element from a 2-way list
103 */
104 static void
105 rem_que(elem)
106 qelem *elem;
107 {
108 qelem *p = elem->q_forw;
109 qelem *p2 = elem->q_back;
110 p2->q_forw = p;
111 p->q_back = p2;
112 }
113
114 /*
115 * Error checking malloc
116 */
117 static void *
118 xmalloc(siz)
119 size_t siz;
120 {
121 void *p = malloc(siz);
122 if (p)
123 return (p);
124 syslog(LOG_ALERT, "malloc: failed to get %lu bytes", (u_long)siz);
125 exit(1);
126 }
127
128 /*
129 * Insert the path in the list.
130 * If there is already an element with the same key then
131 * the *second* one is ignored (return 0). If the key is
132 * not found then the path is added to the end of the list
133 * and 1 is returned.
134 */
135 static int
136 pinsert(p0, q0)
137 path *p0;
138 qelem *q0;
139 {
140 qelem *q;
141
142 if (p0->p_argc == 0)
143 return (0);
144
145 for (q = q0->q_forw; q != q0; q = q->q_forw) {
146 path *p = (path *) q;
147 if (strcmp(p->p_key, p0->p_key) == 0)
148 return (0);
149 }
150 ins_que(&p0->p_q, q0->q_back);
151 return (1);
152
153 }
154
155 void
156 regerror(s)
157 const char *s;
158 {
159 syslog(LOG_ERR, "%s:%d: regcomp %s: %s",
160 conf_file, curp->p_lno, curp->p_key, s);
161 }
162
163 static path *
164 palloc(cline, lno)
165 char *cline;
166 int lno;
167 {
168 int c;
169 char *s;
170 char *key;
171 path *p;
172 char **ap;
173
174 /*
175 * Implement comment chars
176 */
177 s = strchr(cline, '#');
178 if (s)
179 *s = 0;
180
181 /*
182 * Do a pass through the string to count the number
183 * of arguments
184 */
185 c = 0;
186 key = strdup(cline);
187 for (s = key; s != NULL; ) {
188 char *val;
189 while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
190 ;
191 if (val)
192 c++;
193 }
194 c++;
195 free(key);
196
197 if (c <= 1)
198 return (0);
199
200 /*
201 * Now do another pass and generate a new path structure
202 */
203 p = ALLOC(path);
204 p->p_argc = 0;
205 p->p_argv = xmalloc(c * sizeof(char *));
206 p->p_args = strdup(cline);
207 ap = p->p_argv;
208 for (s = p->p_args; s != NULL; ) {
209 char *val;
210 while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
211 ;
212 if (val) {
213 *ap++ = val;
214 p->p_argc++;
215 }
216 }
217 *ap = 0;
218
219 #ifdef DEBUG
220 for (c = 0; c < p->p_argc; c++)
221 printf("%sv[%d] = %s\n", c?"\t":"", c, p->p_argv[c]);
222 #endif
223
224 p->p_key = p->p_argv[0];
225 if (strpbrk(p->p_key, RE_CHARS)) {
226 curp = p; /* XXX */
227 p->p_re = regcomp(p->p_key);
228 curp = 0; /* XXX */
229 } else {
230 p->p_re = 0;
231 }
232 p->p_lno = lno;
233
234 return (p);
235 }
236
237 /*
238 * Free a path structure
239 */
240 static void
241 pfree(p)
242 path *p;
243 {
244 free(p->p_args);
245 if (p->p_re)
246 free((char *) p->p_re);
247 free((char *) p->p_argv);
248 free((char *) p);
249 }
250
251 /*
252 * Discard all currently held path structures on q0.
253 * and add all the ones on xq.
254 */
255 static void
256 preplace(q0, xq)
257 qelem *q0;
258 qelem *xq;
259 {
260 /*
261 * While the list is not empty,
262 * take the first element off the list
263 * and free it.
264 */
265 while (q0->q_forw != q0) {
266 qelem *q = q0->q_forw;
267 rem_que(q);
268 pfree((path *) q);
269 }
270 while (xq->q_forw != xq) {
271 qelem *q = xq->q_forw;
272 rem_que(q);
273 ins_que(q, q0);
274 }
275 }
276
277 /*
278 * Read the lines from the configuration file and
279 * add them to the list of paths.
280 */
281 static void
282 readfp(q0, fp)
283 qelem *q0;
284 FILE *fp;
285 {
286 char cline[LINE_MAX];
287 int nread = 0;
288 qelem q;
289
290 /*
291 * Make a new empty list.
292 */
293 q.q_forw = q.q_back = &q;
294
295 /*
296 * Read the lines from the configuration file.
297 */
298 while (fgets(cline, sizeof(cline), fp)) {
299 path *p = palloc(cline, nread+1);
300 if (p && !pinsert(p, &q))
301 pfree(p);
302 nread++;
303 }
304
305 /*
306 * If some records were read, then throw
307 * away the old list and replace with the
308 * new one.
309 */
310 if (nread)
311 preplace(q0, &q);
312 }
313
314 /*
315 * Read the configuration file (conf) and replace
316 * the existing path list with the new version.
317 * If the file is not readable, then no changes take place
318 */
319 void
320 conf_read(q, conf)
321 qelem *q;
322 char *conf;
323 {
324 FILE *fp = fopen(conf, "r");
325 if (fp) {
326 conf_file = conf; /* XXX */
327 readfp(q, fp);
328 conf_file = 0; /* XXX */
329 (void) fclose(fp);
330 } else {
331 syslog(LOG_ERR, "open config file \"%s\": %s", conf,
332 strerror(errno));
333 }
334 }
335
336
337 char **
338 conf_match(q0, key)
339 qelem *q0;
340 char *key;
341 {
342 qelem *q;
343
344 for (q = q0->q_forw; q != q0; q = q->q_forw) {
345 path *p = (path *) q;
346 if (p->p_re) {
347 if (regexec(p->p_re, key))
348 return (p->p_argv+1);
349 } else {
350 if (strncmp(p->p_key, key, strlen(p->p_key)) == 0)
351 return (p->p_argv+1);
352 }
353 }
354
355 return (0);
356 }
357