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