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