gconfig.c 12.4 KB
Newer Older
1
/*
2
 * Copyright (C) 2007, 2008 Stig Venaas <venaas@uninett.no>
3 4 5 6 7 8 9 10 11 12
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 */

#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
venaas's avatar
venaas committed
13
#include <limits.h>
venaas's avatar
venaas committed
14
#include <glob.h>
15
#include <sys/types.h>
venaas's avatar
venaas committed
16
#include <ctype.h>
17
#include <libgen.h>
venaas's avatar
venaas committed
18
#include <errno.h>
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include "debug.h"
#include "util.h"
#include "gconfig.h"

/* returns NULL on error, where to continue parsing if token and ok. E.g. "" will return token with empty string */
char *strtokenquote(char *s, char **token, char *del, char *quote, char *comment) {
    char *t = s, *q, *r;

    if (!t || !token || !del)
	return NULL;
    while (*t && strchr(del, *t))
	t++;
    if (!*t || (comment && strchr(comment, *t))) {
	*token = NULL;
	return t + 1; /* needs to be non-NULL, but value doesn't matter */
    }
    if (quote && (q = strchr(quote, *t))) {
	t++;
	r = t;
	while (*t && *t != *q)
	    t++;
	if (!*t || (t[1] && !strchr(del, t[1])))
	    return NULL;
	*t = '\0';
	*token = r;
	return t + 1;
    }
    *token = t;
    t++;
    while (*t && !strchr(del, *t))
	t++;
    *t = '\0';
    return t + 1;
}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
int pushgconfdata(struct gconffile **cf, const char *data) {
    int i;
    struct gconffile *newcf;

    if (!*cf) {
	newcf = malloc(sizeof(struct gconffile) * 2);
	if (!newcf)
	    return 0;
	memset(newcf, 0, sizeof(struct gconffile) * 2);
    } else {
	for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
	newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
	if (!newcf)
	    return 0;
	memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
	memset(newcf, 0, sizeof(struct gconffile));
    }
    newcf[0].data = data;
    *cf = newcf;
    return 1;
}

76
FILE *pushgconffile(struct gconffile **cf, FILE *file, const char *description) {
77 78
    int i;
    struct gconffile *newcf;
79
    char *desc;
80

81 82
    if (!file) {
        debug(DBG_INFO, "could not read config from %s", description);
83 84
	return NULL;
    }
85
    debug(DBG_DBG, "reading config from %s", description);
86

87 88
    desc = stringcopy(description, 0);
    if (!desc)
89
	goto errmalloc;
90

91 92 93
    if (!*cf) {
	newcf = malloc(sizeof(struct gconffile) * 2);
	if (!newcf)
94
	    goto errmalloc;
95
	memset(newcf, 0, sizeof(struct gconffile) * 2);
96
    } else {
97
	for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
98 99
	newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
	if (!newcf)
100
	    goto errmalloc;
101
	memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
102
	memset(newcf, 0, sizeof(struct gconffile));
103
    }
104 105
    newcf[0].file = file;
    newcf[0].path = desc;
106
    *cf = newcf;
107
    return file;
108 109

errmalloc:
110 111
    free(desc);
    fclose(file);
112 113
    debug(DBG_ERR, "malloc failed");
    return NULL;
114 115
}

116 117 118 119 120 121 122 123
FILE *pushgconfpath(struct gconffile **cf, const char *path) {
    FILE *f;

    f = fopen(path, "r");
    return pushgconffile(cf, f, path);
}

FILE *pushgconfpaths(struct gconffile **cf, const char *cfgpath) {
venaas's avatar
venaas committed
124
    int i;
125
    FILE *f = NULL;
venaas's avatar
venaas committed
126
    glob_t globbuf;
127
    char *path, *curfile = NULL, *dir;
128

129 130 131 132 133
    /* if cfgpath is relative, make it relative to current config */
    if (*cfgpath == '/')
	path = (char *)cfgpath;
    else {
	/* dirname may modify its argument */
134 135 136 137 138
	curfile = stringcopy((*cf)->path, 0);
	if (!curfile) {
	    debug(DBG_ERR, "malloc failed");
	    goto exit;
	}
139 140
	dir = dirname(curfile);
	path = malloc(strlen(dir) + strlen(cfgpath) + 2);
141 142 143 144
	if (!path) {
	    debug(DBG_ERR, "malloc failed");
	    goto exit;
	}
145 146 147 148
	strcpy(path, dir);
	path[strlen(dir)] = '/';
	strcpy(path + strlen(dir) + 1, cfgpath);
    }
venaas's avatar
venaas committed
149 150
    memset(&globbuf, 0, sizeof(glob_t));
    if (glob(path, 0, NULL, &globbuf)) {
151
	debug(DBG_WARN, "could not glob %s", path);
152
	goto exit;
153
    }
154 155

    for (i = globbuf.gl_pathc - 1; i >= 0; i--) {
156
	f = pushgconfpath(cf, globbuf.gl_pathv[i]);
157 158
	if (!f)
	    break;
159
    }
160 161
    globfree(&globbuf);

162
exit:
163 164 165
    if (curfile) {
	free(curfile);
	free(path);
venaas's avatar
venaas committed
166 167 168 169
    }
    return f;
}

170
int popgconf(struct gconffile **cf) {
171 172 173
    int i;

    if (!*cf)
174 175
	return 0;
    for (i = 0; (*cf)[i].data || (*cf)[i].path; i++);
176
    if (i && (*cf)[0].file) {
177
	fclose((*cf)[0].file);
178 179 180 181
	if ((*cf)[0].path) {
	    debug(DBG_DBG, "closing config file %s", (*cf)[0].path);
	    free((*cf)[0].path);
	}
182
    }
183 184 185
    if (i < 2) {
	free(*cf);
	*cf = NULL;
186
	return 0;
187 188
    }
    memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
189 190 191
    return 1;
}

192 193 194 195 196 197 198 199 200 201
void freegconfmstr(char **mstr) {
    int i;

    if (mstr) {
	for (i = 0; mstr[i]; i++)
	    free(mstr[i]);
	free(mstr);
    }
}

202 203 204 205 206
void freegconf(struct gconffile **cf) {
    int i;

    if (!*cf)
	return;
207

208 209 210 211 212 213 214 215 216 217 218
    for (i = 0; (*cf)[i].data || (*cf)[i].path; i++) {
	if ((*cf)[i].file) {
	    fclose((*cf)[i].file);
	    if ((*cf)[i].path) {
		debug(DBG_DBG, "closing config file %s", (*cf)[i].path);
		free((*cf)[i].path);
	    }
	}
    }
    free(*cf);
    *cf = NULL;
219 220
}

venaas's avatar
venaas committed
221 222 223
struct gconffile *openconfigfile(const char *file) {
    struct gconffile *cf = NULL;

224
    if (!pushgconfpath(&cf, file)) {
225 226 227
	debug(DBG_ERR, "could not read config file %s\n%s", file, strerror(errno));
	return NULL;
    }
venaas's avatar
venaas committed
228 229 230 231
    debug(DBG_DBG, "reading config file %s", file);
    return cf;
}

232 233 234 235 236 237 238 239 240 241
/* Parses config with following syntax:
 * One of these:
 * option-name value
 * option-name = value
 * Or:
 * option-name value {
 *     option-name [=] value
 *     ...
 * }
 */
242

243 244
int getlinefromcf(struct gconffile *cf, char *line, const size_t size) {
    size_t i, pos;
245

246 247
    if (!cf)
	return 0;
248

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    if (cf->file)
	return fgets(line, size, cf->file) ? 1 : 0;
    else if (cf->data) {
	pos = cf->datapos;
	if (!cf->data[pos])
	    return 0;
	for (i = pos; cf->data[i] && cf->data[i] != '\n'; i++);
	if (cf->data[i] == '\n')
	    i++;
	if (i - pos > size - 1)
	    i = size - 1 + pos;
	memcpy(line, cf->data + pos, i - pos);
	line[i - pos] = '\0';
	cf->datapos = i;
	return 1;
    }
    return 0;
}

268
int getconfigline(struct gconffile **cf, char *block, char **opt, char **val, int *conftype) {
venaas's avatar
venaas committed
269 270 271
    char line[1024];
    char *tokens[3], *s;
    int tcount;
272

venaas's avatar
venaas committed
273 274 275
    *opt = NULL;
    *val = NULL;
    *conftype = 0;
276

277
    if (!cf || !*cf || (!(*cf)->file && !(*cf)->data))
278
	return 1;
venaas's avatar
venaas committed
279

280
    for (;;) {
281
	if (!getlinefromcf(*cf, line, 1024)) {
282
	    if (popgconf(cf))
283
		continue;
284
	    return 1;
285
	}
286 287 288
	s = line;
	for (tcount = 0; tcount < 3; tcount++) {
	    s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
289 290 291 292
	    if (!s) {
		debug(DBG_ERR, "Syntax error in line starting with: %s", line);
		return 0;
	    }
293 294 295 296 297 298 299 300
	    if (!tokens[tcount])
		break;
	}
	if (!tcount || **tokens == '#')
	    continue;

	if (**tokens == '}') {
	    if (block)
301 302 303
		return 1;
	    debug(DBG_ERR, "configuration error, found } with no matching {");
	    return 0;
304
	}
venaas's avatar
venaas committed
305 306
	break;
    }
307

venaas's avatar
venaas committed
308 309
    switch (tcount) {
    case 2:
310 311 312 313 314 315
	*opt = stringcopy(tokens[0], 0);
	if (!*opt)
	    goto errmalloc;
	*val = stringcopy(tokens[1], 0);
	if (!*val)
	    goto errmalloc;
venaas's avatar
venaas committed
316 317 318 319
	*conftype = CONF_STR;
	break;
    case 3:
	if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
320 321 322 323 324 325
	    *opt = stringcopy(tokens[0], 0);
	    if (!*opt)
		goto errmalloc;
	    *val = stringcopy(tokens[2], 0);
	    if (!*val)
		goto errmalloc;
venaas's avatar
venaas committed
326 327 328 329
	    *conftype = CONF_STR;
	    break;
	}
	if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
330 331 332 333 334 335
	    *opt = stringcopy(tokens[0], 0);
	    if (!*opt)
		goto errmalloc;
	    *val = stringcopy(tokens[1], 0);
	    if (!*val)
		goto errmalloc;
venaas's avatar
venaas committed
336
	    *conftype = CONF_CBK;
337 338
	    break;
	}
venaas's avatar
venaas committed
339 340 341
	/* fall through */
    default:
	if (block)
342 343 344 345
	    debug(DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
	else
	    debug(DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
	return 0;
venaas's avatar
venaas committed
346 347
    }

348 349
    if (**val)
	return 1;
350

351 352 353
    debug(DBG_ERR, "configuration error, option %s needs a non-empty value", *opt);
    goto errexit;

354
errmalloc:
355
    debug(DBG_ERR, "malloc failed");
356
errexit:
357 358 359 360 361
    free(*opt);
    *opt = NULL;
    free(*val);
    *val = NULL;
    return 0;
venaas's avatar
venaas committed
362
}
363

venaas's avatar
venaas committed
364 365 366 367 368 369 370 371 372 373 374 375 376
uint8_t hexdigit2int(char d) {
    if (d >= '0' && d <= '9')
	return d - '0';
    if (d >= 'a' && d <= 'f')
	return 10 + d - 'a';
    if (d >= 'A' && d <= 'F')
	return 10 + d - 'A';
    return 0;
}

void unhex(char *s) {
    char *t;
    for (t = s; *t; s++) {
venaas's avatar
venaas committed
377
	if (*t == '%' && isxdigit((int)t[1]) && isxdigit((int)t[2])) {
venaas's avatar
venaas committed
378 379 380 381 382 383 384 385
	    *s = 16 * hexdigit2int(t[1]) + hexdigit2int(t[2]);
	    t += 3;
	} else
	    *s = *t++;
    }
    *s = '\0';
}

386 387
typedef int (*t_fptr)(struct gconffile **, void *, char *, char *, char *);

388 389 390
/* returns 1 if ok, 0 on error */
/* caller must free returned values also on error */
int getgenericconfig(struct gconffile **cf, char *block, ...) {
venaas's avatar
venaas committed
391
    va_list ap;
venaas's avatar
venaas committed
392
    char *opt = NULL, *val, *word, *optval, **str = NULL, ***mstr = NULL, **newmstr, *endptr;
393
    uint8_t *bln = NULL;
venaas's avatar
venaas committed
394
    long int *lint = NULL;
venaas's avatar
venaas committed
395
    int type = 0, conftype = 0, n;
396
    t_fptr cbk = NULL;
397
    void *cbkarg = NULL;
venaas's avatar
venaas committed
398 399

    for (;;) {
400
	free(opt);
401 402
	if (!getconfigline(cf, block, &opt, &val, &conftype))
	    return 0;
venaas's avatar
venaas committed
403
	if (!opt)
404
	    return 1;
405 406

	if (conftype == CONF_STR && !strcasecmp(opt, "include")) {
407
	    if (!pushgconfpaths(cf, val)) {
408 409 410
		debug(DBG_ERR, "failed to include config file %s", val);
		goto errexit;
	    }
411
	    free(val);
412 413
	    continue;
	}
414

415 416 417 418 419 420 421
	va_start(ap, block);
	while ((word = va_arg(ap, char *))) {
	    type = va_arg(ap, int);
	    switch (type) {
	    case CONF_STR:
		str = va_arg(ap, char **);
		if (!str)
422
		    goto errparam;
423 424 425 426
		break;
	    case CONF_MSTR:
		mstr = va_arg(ap, char ***);
		if (!mstr)
427
		    goto errparam;
428
		break;
429 430 431
	    case CONF_BLN:
		bln = va_arg(ap, uint8_t *);
		if (!bln)
432
		    goto errparam;
433
		break;
venaas's avatar
venaas committed
434 435 436 437 438
	    case CONF_LINT:
		lint = va_arg(ap, long int *);
		if (!lint)
		    goto errparam;
		break;
439
	    case CONF_CBK:
440
		cbk = va_arg(ap, t_fptr);
441 442 443
		if (!cbk)
		    goto errparam;
		cbkarg = va_arg(ap, void *);
444 445
		break;
	    default:
446
		goto errparam;
447 448 449 450 451
	    }
	    if (!strcasecmp(opt, word))
		break;
	}
	va_end(ap);
452

453 454
	if (!word) {
	    if (block)
455 456 457
		debug(DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
	    debug(DBG_ERR, "configuration error, unknown option %s", opt);
	    goto errexit;
458 459
	}

venaas's avatar
venaas committed
460
	if (((type == CONF_STR || type == CONF_MSTR || type == CONF_BLN || type == CONF_LINT) && conftype != CONF_STR) ||
461 462
	    (type == CONF_CBK && conftype != CONF_CBK)) {
	    if (block)
463 464 465
		debug(DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
	    debug(DBG_ERR, "configuration error, wrong syntax for option %s", opt);
	    goto errexit;
466 467 468 469
	}

	switch (type) {
	case CONF_STR:
470 471 472 473
	    if (*str) {
		debug(DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
		goto errexit;
	    }
venaas's avatar
venaas committed
474
	    unhex(val);
475
	    *str = val;
476 477 478 479 480 481
	    break;
	case CONF_MSTR:
	    if (*mstr)
		for (n = 0; (*mstr)[n]; n++);
	    else
		n = 0;
482 483 484 485 486
	    newmstr = realloc(*mstr, sizeof(char *) * (n + 2));
	    if (!newmstr) {
		debug(DBG_ERR, "malloc failed");
		goto errexit;
	    }
venaas's avatar
venaas committed
487
	    unhex(val);
488 489 490
	    newmstr[n] = val;
	    newmstr[n + 1] = NULL;
	    *mstr = newmstr;
491
	    break;
492 493 494 495 496
	case CONF_BLN:
	    if (!strcasecmp(val, "on"))
		*bln = 1;
	    else if (!strcasecmp(val, "off"))
		*bln = 0;
497 498 499 500 501 502 503
	    else {
		if (block)
		    debug(DBG_ERR, "configuration error in block %s, value for option %s must be on or off, not %s", block, opt, val);
		else
		    debug(DBG_ERR, "configuration error, value for option %s must be on or off, not %s", opt, val);
		goto errexit;
	    }
504
	    break;
venaas's avatar
venaas committed
505 506 507 508 509 510 511 512 513 514 515
	case CONF_LINT:
	    endptr = NULL;
	    *lint = strtol(val, &endptr, 0);
	    if (*lint == LONG_MIN || *lint == LONG_MAX || !endptr || endptr == val || *endptr != '\0') {
		if (block)
		    debug(DBG_ERR, "configuration error in block %s, value for option %s must be an integer, not %s", block, opt, val);
		else
		    debug(DBG_ERR, "configuration error, value for option %s must be an integer, not %s", opt, val);
		goto errexit;
	    }
	    break;
516 517
	case CONF_CBK:
	    optval = malloc(strlen(opt) + strlen(val) + 2);
518 519 520 521
	    if (!optval) {
		debug(DBG_ERR, "malloc failed");
		goto errexit;
	    }
522
	    sprintf(optval, "%s %s", opt, val);
523 524 525 526
	    if (!cbk(cf, cbkarg, optval, opt, val)) {
		free(optval);
		goto errexit;
	    }
527
	    free(val);
528
	    free(optval);
529
	    continue;
530
	default:
531
	    goto errparam;
532
	}
533 534
	if (block)
	    debug(DBG_DBG, "getgenericconfig: block %s: %s = %s", block, opt, val);
535
	else
536
	    debug(DBG_DBG, "getgenericconfig: %s = %s", opt, val);
venaas's avatar
venaas committed
537
	if (type == CONF_BLN || type == CONF_LINT)
538
	    free(val);
539
    }
540

541
errparam:
542
    debug(DBG_ERR, "getgenericconfig: internal parameter error");
543
errexit:
544 545 546
    free(opt);
    free(val);
    return 0;
547
}
548 549 550 551

/* Local Variables: */
/* c-file-style: "stroustrup" */
/* End: */