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