Home | History | Annotate | Line # | Download | only in mount_portal
conf.c revision 1.7
      1 /*	$NetBSD: conf.c,v 1.7 1997/09/21 02:35:41 enami 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.7 1997/09/21 02:35:41 enami 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 	p->p_lno = lno;
    232 
    233 	return (p);
    234 }
    235 
    236 /*
    237  * Free a path structure
    238  */
    239 static void
    240 pfree(p)
    241 	path *p;
    242 {
    243 	free(p->p_args);
    244 	if (p->p_re)
    245 		free((char *) p->p_re);
    246 	free((char *) p->p_argv);
    247 	free((char *) p);
    248 }
    249 
    250 /*
    251  * Discard all currently held path structures on q0.
    252  * and add all the ones on xq.
    253  */
    254 static void
    255 preplace(q0, xq)
    256 	qelem *q0;
    257 	qelem *xq;
    258 {
    259 	/*
    260 	 * While the list is not empty,
    261 	 * take the first element off the list
    262 	 * and free it.
    263 	 */
    264 	while (q0->q_forw != q0) {
    265 		qelem *q = q0->q_forw;
    266 		rem_que(q);
    267 		pfree((path *) q);
    268 	}
    269 	while (xq->q_forw != xq) {
    270 		qelem *q = xq->q_forw;
    271 		rem_que(q);
    272 		ins_que(q, q0);
    273 	}
    274 }
    275 
    276 /*
    277  * Read the lines from the configuration file and
    278  * add them to the list of paths.
    279  */
    280 static void
    281 readfp(q0, fp)
    282 	qelem *q0;
    283 	FILE *fp;
    284 {
    285 	char cline[LINE_MAX];
    286 	int nread = 0;
    287 	qelem q;
    288 
    289 	/*
    290 	 * Make a new empty list.
    291 	 */
    292 	q.q_forw = q.q_back = &q;
    293 
    294 	/*
    295 	 * Read the lines from the configuration file.
    296 	 */
    297 	while (fgets(cline, sizeof(cline), fp)) {
    298 		path *p = palloc(cline, nread+1);
    299 		if (p && !pinsert(p, &q))
    300 			pfree(p);
    301 		nread++;
    302 	}
    303 
    304 	/*
    305 	 * If some records were read, then throw
    306 	 * away the old list and replace with the
    307 	 * new one.
    308 	 */
    309 	if (nread)
    310 		preplace(q0, &q);
    311 }
    312 
    313 /*
    314  * Read the configuration file (conf) and replace
    315  * the existing path list with the new version.
    316  * If the file is not readable, then no changes take place
    317  */
    318 void
    319 conf_read(q, conf)
    320 	qelem *q;
    321 	char *conf;
    322 {
    323 	FILE *fp = fopen(conf, "r");
    324 	if (fp) {
    325 		conf_file = conf;		/* XXX */
    326 		readfp(q, fp);
    327 		conf_file = 0;		/* XXX */
    328 		(void) fclose(fp);
    329 	} else
    330 		syslog(LOG_ERR, "open config file \"%s\": %m", conf);
    331 }
    332 
    333 
    334 char **
    335 conf_match(q0, key)
    336 	qelem *q0;
    337 	char *key;
    338 {
    339 	qelem *q;
    340 
    341 	for (q = q0->q_forw; q != q0; q = q->q_forw) {
    342 		path *p = (path *) q;
    343 		if (p->p_re) {
    344 			if (regexec(p->p_re, key))
    345 				return (p->p_argv+1);
    346 		} else {
    347 			if (strncmp(p->p_key, key, strlen(p->p_key)) == 0)
    348 				return (p->p_argv+1);
    349 		}
    350 	}
    351 
    352 	return (0);
    353 }
    354