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