#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: conf.c,v 1.14 2019/05/23 04:34:25 kre Exp $");
#endif
#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;
int p_lno;
char *p_args;
char *p_key;
regex_t p_re;
int p_use_re;
int p_argc;
char **p_argv;
};
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);
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;
}
static void
rem_que(qelem *elem)
{
qelem *p = elem->q_forw;
qelem *p2 = elem->q_back;
p2->q_forw = p;
p->q_back = p2;
}
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);
}
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;
c = 0;
key = strdup(cline);
for (s = key; s != NULL; ) {
char *val;
if (*s == '#') {
cline[s-key] = '\0';
break;
}
while ((val = strsep(&s, " \t\n")) != NULL && *val == '\0')
;
if (val)
c++;
}
c++;
free(key);
if (c <= 1)
return 0;
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;
}
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);
}
static void
preplace(qelem *q0, qelem *xq)
{
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);
}
}
static void
readfp(qelem *q0, FILE *fp, const char *conf_file)
{
char cline[LINE_MAX];
int nread = 0;
qelem q;
q.q_forw = q.q_back = &q;
while (fgets(cline, sizeof(cline), fp)) {
path *p = palloc(cline, nread+1, conf_file);
if (p && !pinsert(p, &q))
pfree(p);
nread++;
}
if (nread)
preplace(q0, &q);
}
int
conf_read(qelem *q, const char *conf)
{
FILE *fp = fopen(conf, "r");
if (fp) {
readfp(q, fp, conf);
(void)fclose(fp);
return 0;
} else {
int 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;
}