[BACK]Return to conf.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sbin / mount_portal

File: [cvs.NetBSD.org] / src / sbin / mount_portal / conf.c (download)

Revision 1.12, Mon Jul 2 16:33:05 2007 UTC (13 years ago) by pooka
Branch: MAIN
CVS Tags: yamt-pf42-baseX, yamt-pf42-base4, yamt-pf42-base3, yamt-pf42-base2, yamt-pf42-base, yamt-pf42, yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, wrstuden-revivesa-base-1, wrstuden-revivesa-base, wrstuden-revivesa, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-3-RELEASE, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-5-RELEASE, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, mjf-devfs2-base, mjf-devfs2, mjf-devfs-base, mjf-devfs, matt-premerge-20091211, matt-nb8-mediatek-base, matt-nb8-mediatek, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-mips64-premerge-20101231, matt-mips64-base2, matt-mips64-base, matt-mips64, matt-armv6-prevmlocking, matt-armv6-nbase, matt-armv6-base, matt-armv6, localcount-20160914, keiichi-mipv6-base, keiichi-mipv6, jym-xensuspend-nbase, jym-xensuspend-base, jym-xensuspend, hpcarm-cleanup-nbase, hpcarm-cleanup-base, hpcarm-cleanup, cube-autoconf-base, cube-autoconf, cherry-xenmp-base, cherry-xenmp, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver
Branch point for: phil-wifi
Changes since 1.11: +11 -5 lines

* narrow-angle const spray
* make config read return an error if it can't read the config file

/*	$NetBSD: conf.c,v 1.12 2007/07/02 16:33:05 pooka Exp $	*/

/*
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software donated to Berkeley by
 * Jan-Simon Pendry.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	from: Id: conf.c,v 1.2 1992/05/27 07:09:27 jsp Exp
 *	@(#)conf.c	8.2 (Berkeley) 3/27/94
 */

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: conf.c,v 1.12 2007/07/02 16:33:05 pooka Exp $");
#endif /* not lint */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/syslog.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <regex.h>

#include "portald.h"

#define	ALLOC(ty)	(xmalloc(sizeof(ty)))

typedef struct path path;
struct path {
	qelem p_q;		/* 2-way linked list */
	int p_lno;		/* Line number of this record */
	char *p_args;		/* copy of arg string (malloc) */
	char *p_key;		/* Pathname to match (also p_argv[0]) */
	regex_t p_re;		/* RE to match against pathname (malloc) */
	int p_use_re;		/* true if entry is RE */
	int p_argc;		/* number of elements in arg string */
	char **p_argv;		/* argv[] pointers into arg string (malloc) */
};

static	void	ins_que(qelem *, qelem *);
static	path   *palloc(char *, int, const char *);
static	void	pfree(path *);
static	int	pinsert(path *, qelem *);
static	void	preplace(qelem *, qelem *);
static	void	readfp(qelem *, FILE *, const char *);
static	void	rem_que(qelem *);
static	void   *xmalloc(size_t);

/*
 * Add an element to a 2-way list,
 * just after (pred)
 */
static void
ins_que(qelem *elem, qelem *pred)
{
	qelem *p = pred->q_forw;
	elem->q_back = pred;
	elem->q_forw = p;
	pred->q_forw = elem;
	p->q_back = elem;
}

/*
 * Remove an element from a 2-way list
 */
static void
rem_que(qelem *elem)
{
	qelem *p = elem->q_forw;
	qelem *p2 = elem->q_back;
	p2->q_forw = p;
	p->q_back = p2;
}

/*
 * Error checking malloc
 */
static void *
xmalloc(size_t siz)
{
	void *p = malloc(siz);
	if (p)
		return (p);
	syslog(LOG_ERR, "malloc: failed to get %lu bytes", (u_long)siz);
	exit(1);
}

/*
 * Insert the path in the list.
 * If there is already an element with the same key then
 * the *second* one is ignored (return 0).  If the key is
 * not found then the path is added to the end of the list
 * and 1 is returned.
 */
static int
pinsert(path *p0, qelem *q0)
{
	qelem *q;

	if (p0->p_argc == 0)
		return (0);

	for (q = q0->q_forw; q != q0; q = q->q_forw) {
		path *p = (path *) q;
		if (strcmp(p->p_key, p0->p_key) == 0)
			return (0);
	}
	ins_que(&p0->p_q, q0->q_back);
	return (1);
	
}

static path *
palloc(char *cline, int lno, const char *conf_file)
{
	int c, errcode;
	char *s;
	char *key;
	path *p;
	char **ap;

	/*
	 * Implement comment chars
	 */
	s = strchr(cline, '#');
	if (s)
		*s = 0;

	/*
	 * Do a pass through the string to count the number
	 * of arguments
	 */
	c = 0;
	key = strdup(cline);
	for (s = key; s != NULL; ) {
		char *val;
		while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
			;
		if (val)
			c++;
	}
	c++;
	free(key);

	if (c <= 1)
		return (0);

	/*
	 * Now do another pass and generate a new path structure
	 */
	p = ALLOC(path);
	p->p_argc = 0;
	p->p_argv = xmalloc(c * sizeof(char *));
	p->p_args = strdup(cline);
	ap = p->p_argv;
	for (s = p->p_args; s != NULL; ) {
		char *val;
		while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
			;
		if (val) {
			*ap++ = val;
			p->p_argc++;
		}
	}
	*ap = 0;

#ifdef DEBUG
	for (c = 0; c < p->p_argc; c++)
		printf("%sv[%d] = %s\n", c?"\t":"", c, p->p_argv[c]);
#endif

	p->p_key = p->p_argv[0];
	p->p_use_re = 0;
	if (strpbrk(p->p_key, RE_CHARS)) {
		errcode = regcomp(&p->p_re, p->p_key, REG_EXTENDED|REG_NOSUB);
		if (errcode == 0)
			p->p_use_re = 1;
		else {
			char buf[200];
			regerror(errcode, &p->p_re, buf, sizeof(buf));
			
			syslog(LOG_WARNING, "%s, line %d: regcomp \"%s\": %s",
	  		  conf_file, p->p_lno, p->p_key, buf);
		}
	}
	p->p_lno = lno;

	return (p);
}

/*
 * Free a path structure
 */
static void
pfree(path *p)
{
	free(p->p_args);
	free((char *) p->p_argv);
	if (p->p_use_re)
		regfree(&p->p_re);
	free((char *) p);
}

/*
 * Discard all currently held path structures on q0.
 * and add all the ones on xq.
 */
static void
preplace(qelem *q0, qelem *xq)
{
	/*
	 * While the list is not empty,
	 * take the first element off the list
	 * and free it.
	 */
	while (q0->q_forw != q0) {
		qelem *q = q0->q_forw;
		rem_que(q);
		pfree((path *) q);
	}
	while (xq->q_forw != xq) {
		qelem *q = xq->q_forw;
		rem_que(q);
		ins_que(q, q0);
	}
}

/*
 * Read the lines from the configuration file and
 * add them to the list of paths.
 */
static void
readfp(qelem *q0, FILE *fp, const char *conf_file)
{
	char cline[LINE_MAX];
	int nread = 0;
	qelem q;

	/*
	 * Make a new empty list.
	 */
	q.q_forw = q.q_back = &q;

	/*
	 * Read the lines from the configuration file.
	 */
	while (fgets(cline, sizeof(cline), fp)) {
		path *p = palloc(cline, nread+1, conf_file);
		if (p && !pinsert(p, &q))
			pfree(p);
		nread++;
	}

	/*
	 * If some records were read, then throw
	 * away the old list and replace with the
	 * new one.
	 */
	if (nread)
		preplace(q0, &q);
}

/*
 * Read the configuration file (conf) and replace
 * the existing path list with the new version.
 * If the file is not readable, then no changes take place
 */
int
conf_read(qelem *q, const char *conf)
{
	FILE *fp = fopen(conf, "r");
	int sverrno;
	if (fp) {
		readfp(q, fp, conf);
		(void) fclose(fp);
		return 0;
	} else {
		sverrno = errno;
		syslog(LOG_WARNING, "open config file \"%s\": %m", conf);
		errno = sverrno;
		return -1;
	}
}


char **
conf_match(qelem *q0, char *key)
{
	qelem *q;

	for (q = q0->q_forw; q != q0; q = q->q_forw) {
		path *p = (path *) q;
		if (p->p_use_re) {
			if (regexec(&p->p_re, key, 0, NULL, 0) == 0)
				return (p->p_argv+1);
		} else {
			if (strncmp(p->p_key, key, strlen(p->p_key)) == 0)
				return (p->p_argv+1);
		}
	}

	return (0);
}