| 1 |
/* |
| 2 |
* Copyright (c) 2005-2011 Atheme Project (http://www.atheme.org) |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 9 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 10 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 11 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 12 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 13 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 14 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 15 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 16 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 17 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 18 |
* POSSIBILITY OF SUCH DAMAGE. |
| 19 |
*/ |
| 20 |
|
| 21 |
/* |
| 22 |
* Description of config files parsed by this: |
| 23 |
* |
| 24 |
* configfile = *WS *configentry |
| 25 |
* configentry = value [1*WS value] [1*WS "{" *(configentry 1*WS) "}" ] *WS ";" |
| 26 |
* value = 1*achar / DQUOTE *qchar DQUOTE |
| 27 |
* achar = <any CHAR except WS or DQUOTE> |
| 28 |
* qchar = <any CHAR except DQUOTE or \> / "\\" / "\" DQUOTE |
| 29 |
* comment = "/" "*" <anything except * /> "*" "/" / |
| 30 |
* "#" *CHAR %0x0A / |
| 31 |
* "//" *CHAR %0x0A |
| 32 |
* WS = %x09 / %x0A / %x0D / SPACE / "=" / comment |
| 33 |
* |
| 34 |
* A value of "include" for toplevel configentries causes a file to be |
| 35 |
* included. The included file is logically appended to the current file, |
| 36 |
* no matter where the include directive is. Include files must have balanced |
| 37 |
* braces. |
| 38 |
*/ |
| 39 |
|
| 40 |
/* |
| 41 |
* Original idea from the csircd config parser written by Fred Jacobs |
| 42 |
* and Chris Behrens. |
| 43 |
*/ |
| 44 |
|
| 45 |
#include "mowgli.h" |
| 46 |
|
| 47 |
#define MAX_INCLUDE_NESTING 16 |
| 48 |
|
| 49 |
static mowgli_config_file_t *mowgli_config_file_parse(const char *filename, char *confdata); |
| 50 |
static void mowgli_config_file_entry_free(mowgli_config_file_entry_t *ceptr); |
| 51 |
static mowgli_config_file_t *mowgli_config_file_load_internal(mowgli_config_file_t *parent, const char *filename); |
| 52 |
|
| 53 |
#define CF_ERRORED(cf) ((cf)->curline <= 0) |
| 54 |
|
| 55 |
static void MOWGLI_FATTR_PRINTF(2, 3) |
| 56 |
mowgli_config_file_error(mowgli_config_file_t *cf, const char *format, ...) |
| 57 |
{ |
| 58 |
va_list ap; |
| 59 |
char buffer[1024]; |
| 60 |
char *ptr; |
| 61 |
|
| 62 |
va_start(ap, format); |
| 63 |
vsnprintf(buffer, sizeof buffer, format, ap); |
| 64 |
va_end(ap); |
| 65 |
|
| 66 |
if ((ptr = strchr(buffer, '\n')) != NULL) |
| 67 |
*ptr = '\0'; |
| 68 |
|
| 69 |
if (cf != NULL) |
| 70 |
{ |
| 71 |
if (cf->curline < 0) |
| 72 |
cf->curline = -cf->curline; |
| 73 |
|
| 74 |
mowgli_log("%s:%d: %s", cf->filename, cf->curline, buffer); |
| 75 |
|
| 76 |
/* mark config parse as failed */ |
| 77 |
cf->curline = -cf->curline; |
| 78 |
} |
| 79 |
else |
| 80 |
{ |
| 81 |
mowgli_log("mowgli_config_file_parse(): %s", buffer); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
static void |
| 86 |
skip_ws(char **pos, mowgli_config_file_t *cf) |
| 87 |
{ |
| 88 |
int startline; |
| 89 |
|
| 90 |
for (;;) |
| 91 |
{ |
| 92 |
switch (**pos) |
| 93 |
{ |
| 94 |
case ' ': |
| 95 |
case '\t': |
| 96 |
case '\r': |
| 97 |
case '=': /* XXX */ |
| 98 |
break; |
| 99 |
case '\n': |
| 100 |
cf->curline++; |
| 101 |
break; |
| 102 |
case '/': |
| 103 |
|
| 104 |
if ((*pos)[1] == '*') |
| 105 |
{ |
| 106 |
startline = cf->curline; |
| 107 |
(*pos)++; |
| 108 |
(*pos)++; |
| 109 |
|
| 110 |
while (**pos != '\0' && (**pos != '*' || (*pos)[1] != '/')) |
| 111 |
{ |
| 112 |
if (**pos == '\n') |
| 113 |
cf->curline++; |
| 114 |
|
| 115 |
(*pos)++; |
| 116 |
} |
| 117 |
|
| 118 |
if (**pos == '\0') |
| 119 |
mowgli_config_file_error(cf, "File ends inside comment starting at line %d", startline); |
| 120 |
else |
| 121 |
(*pos)++; /* skip '*' */ |
| 122 |
} |
| 123 |
else if ((*pos)[1] == '/') |
| 124 |
{ |
| 125 |
while (**pos != '\0' && **pos != '\n' && **pos != '\r') |
| 126 |
{ |
| 127 |
(*pos)++; |
| 128 |
} |
| 129 |
|
| 130 |
continue; |
| 131 |
} |
| 132 |
else |
| 133 |
{ |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
break; |
| 138 |
case '#': |
| 139 |
|
| 140 |
while (**pos != '\0' && **pos != '\n' && **pos != '\r') |
| 141 |
{ |
| 142 |
(*pos)++; |
| 143 |
} |
| 144 |
|
| 145 |
continue; |
| 146 |
default: |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
if (**pos == '\0') |
| 151 |
return; |
| 152 |
|
| 153 |
(*pos)++; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
static char * |
| 158 |
get_value(char **pos, mowgli_config_file_t *cf, char *skipped) |
| 159 |
{ |
| 160 |
char *p = *pos; |
| 161 |
char *q; |
| 162 |
char *start; |
| 163 |
|
| 164 |
*skipped = '\0'; |
| 165 |
|
| 166 |
if (*p == '"') |
| 167 |
{ |
| 168 |
p++; |
| 169 |
start = p; |
| 170 |
q = p; |
| 171 |
|
| 172 |
while (*p != '\0' && *p != '\r' && *p != '\n' && *p != '"') |
| 173 |
{ |
| 174 |
if ((*p == '\\') && ((p[1] == '"') || (p[1] == '\\'))) |
| 175 |
p++; |
| 176 |
|
| 177 |
*q++ = *p++; |
| 178 |
} |
| 179 |
|
| 180 |
if (*p == '\0') |
| 181 |
{ |
| 182 |
mowgli_config_file_error(cf, "File ends inside quoted string"); |
| 183 |
return NULL; |
| 184 |
} |
| 185 |
|
| 186 |
if ((*p == '\r') || (*p == '\n')) |
| 187 |
{ |
| 188 |
mowgli_config_file_error(cf, "Newline inside quoted string"); |
| 189 |
return NULL; |
| 190 |
} |
| 191 |
|
| 192 |
if (*p != '"') |
| 193 |
{ |
| 194 |
mowgli_config_file_error(cf, "Weird character terminating quoted string (BUG)"); |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
|
| 198 |
p++; |
| 199 |
*q = '\0'; |
| 200 |
*pos = p; |
| 201 |
skip_ws(pos, cf); |
| 202 |
return start; |
| 203 |
} |
| 204 |
else |
| 205 |
{ |
| 206 |
start = p; |
| 207 |
|
| 208 |
while (*p != '\0' && *p != '\t' && *p != '\r' && *p != '\n' && |
| 209 |
*p != ' ' && *p != '/' && *p != '#' && |
| 210 |
*p != ';' && *p != '{' && *p != '}') |
| 211 |
{ |
| 212 |
p++; |
| 213 |
} |
| 214 |
|
| 215 |
if (p == start) |
| 216 |
return NULL; |
| 217 |
|
| 218 |
*pos = p; |
| 219 |
skip_ws(pos, cf); |
| 220 |
|
| 221 |
if (p == *pos) |
| 222 |
*skipped = *p; |
| 223 |
|
| 224 |
*p = '\0'; |
| 225 |
|
| 226 |
if (p == *pos) |
| 227 |
(*pos)++; |
| 228 |
|
| 229 |
return start; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
static mowgli_config_file_t * |
| 234 |
mowgli_config_file_parse(const char *filename, char *confdata) |
| 235 |
{ |
| 236 |
mowgli_config_file_t *cf, *subcf, *lastcf; |
| 237 |
mowgli_config_file_entry_t **pprevce, *ce, *upce; |
| 238 |
char *p, *val; |
| 239 |
char c; |
| 240 |
|
| 241 |
cf = mowgli_alloc(sizeof *cf); |
| 242 |
cf->filename = mowgli_strdup(filename); |
| 243 |
cf->curline = 1; |
| 244 |
cf->mem = confdata; |
| 245 |
lastcf = cf; |
| 246 |
pprevce = &cf->entries; |
| 247 |
upce = NULL; |
| 248 |
p = confdata; |
| 249 |
|
| 250 |
while (*p != '\0') |
| 251 |
{ |
| 252 |
skip_ws(&p, cf); |
| 253 |
|
| 254 |
if ((*p == '\0') || CF_ERRORED(cf)) |
| 255 |
break; |
| 256 |
|
| 257 |
if (*p == '}') |
| 258 |
{ |
| 259 |
if (upce == NULL) |
| 260 |
{ |
| 261 |
mowgli_config_file_error(cf, "Extraneous closing brace"); |
| 262 |
break; |
| 263 |
} |
| 264 |
|
| 265 |
ce = upce; |
| 266 |
ce->sectlinenum = cf->curline; |
| 267 |
pprevce = &ce->next; |
| 268 |
upce = ce->prevlevel; |
| 269 |
p++; |
| 270 |
skip_ws(&p, cf); |
| 271 |
|
| 272 |
if (CF_ERRORED(cf)) |
| 273 |
break; |
| 274 |
|
| 275 |
if (*p != ';') |
| 276 |
{ |
| 277 |
mowgli_config_file_error(cf, "Missing semicolon after closing brace for section ending at line %d", ce->sectlinenum); |
| 278 |
break; |
| 279 |
} |
| 280 |
|
| 281 |
ce = NULL; |
| 282 |
p++; |
| 283 |
continue; |
| 284 |
} |
| 285 |
|
| 286 |
val = get_value(&p, cf, &c); |
| 287 |
|
| 288 |
if (CF_ERRORED(cf)) |
| 289 |
break; |
| 290 |
|
| 291 |
if (val == NULL) |
| 292 |
{ |
| 293 |
mowgli_config_file_error(cf, "Unexpected character trying to read variable name"); |
| 294 |
break; |
| 295 |
} |
| 296 |
|
| 297 |
ce = mowgli_alloc(sizeof *ce); |
| 298 |
ce->fileptr = cf; |
| 299 |
ce->varlinenum = cf->curline; |
| 300 |
ce->varname = val; |
| 301 |
ce->prevlevel = upce; |
| 302 |
*pprevce = ce; |
| 303 |
pprevce = &ce->next; |
| 304 |
|
| 305 |
if ((c == '\0') && ((*p == '{') || (*p == ';'))) |
| 306 |
c = *p++; |
| 307 |
|
| 308 |
if (c == '{') |
| 309 |
{ |
| 310 |
pprevce = &ce->entries; |
| 311 |
upce = ce; |
| 312 |
ce = NULL; |
| 313 |
} |
| 314 |
else if (c == ';') |
| 315 |
{ |
| 316 |
ce = NULL; |
| 317 |
} |
| 318 |
else if (c != '\0') |
| 319 |
{ |
| 320 |
mowgli_config_file_error(cf, "Unexpected characters after unquoted string %s", ce->varname); |
| 321 |
break; |
| 322 |
} |
| 323 |
else |
| 324 |
{ |
| 325 |
val = get_value(&p, cf, &c); |
| 326 |
|
| 327 |
if (CF_ERRORED(cf)) |
| 328 |
break; |
| 329 |
|
| 330 |
if (val == NULL) |
| 331 |
{ |
| 332 |
mowgli_config_file_error(cf, "Unexpected character trying to read value for %s", ce->varname); |
| 333 |
break; |
| 334 |
} |
| 335 |
|
| 336 |
ce->vardata = val; |
| 337 |
|
| 338 |
if ((c == '\0') && ((*p == '{') || (*p == ';'))) |
| 339 |
c = *p++; |
| 340 |
|
| 341 |
if (c == '{') |
| 342 |
{ |
| 343 |
pprevce = &ce->entries; |
| 344 |
upce = ce; |
| 345 |
ce = NULL; |
| 346 |
} |
| 347 |
else if (c == ';') |
| 348 |
{ |
| 349 |
if ((upce == NULL) && !strcasecmp(ce->varname, "include")) |
| 350 |
{ |
| 351 |
subcf = mowgli_config_file_load_internal(cf, ce->vardata); |
| 352 |
|
| 353 |
if (subcf == NULL) |
| 354 |
{ |
| 355 |
mowgli_config_file_error(cf, "Error in file included from here"); |
| 356 |
break; |
| 357 |
} |
| 358 |
|
| 359 |
lastcf->next = subcf; |
| 360 |
|
| 361 |
while (lastcf->next != NULL) |
| 362 |
{ |
| 363 |
lastcf = lastcf->next; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
ce = NULL; |
| 368 |
} |
| 369 |
else |
| 370 |
{ |
| 371 |
mowgli_config_file_error(cf, "Unexpected characters after value %s %s", ce->varname, ce->vardata); |
| 372 |
break; |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
if (!CF_ERRORED(cf) && (upce != NULL)) |
| 378 |
{ |
| 379 |
mowgli_config_file_error(cf, "One or more sections not closed"); |
| 380 |
ce = upce; |
| 381 |
|
| 382 |
while (ce->prevlevel != NULL) |
| 383 |
{ |
| 384 |
ce = ce->prevlevel; |
| 385 |
} |
| 386 |
|
| 387 |
if (ce->vardata != NULL) |
| 388 |
mowgli_config_file_error(cf, "First unclosed section is %s %s at line %d", |
| 389 |
ce->varname, ce->vardata, ce->varlinenum); |
| 390 |
else |
| 391 |
mowgli_config_file_error(cf, "First unclosed section is %s at line %d", |
| 392 |
ce->varname, ce->varlinenum); |
| 393 |
} |
| 394 |
|
| 395 |
if (CF_ERRORED(cf)) |
| 396 |
{ |
| 397 |
mowgli_config_file_free(cf); |
| 398 |
cf = NULL; |
| 399 |
} |
| 400 |
|
| 401 |
return cf; |
| 402 |
} |
| 403 |
|
| 404 |
static void |
| 405 |
mowgli_config_file_entry_free(mowgli_config_file_entry_t *ceptr) |
| 406 |
{ |
| 407 |
mowgli_config_file_entry_t *nptr; |
| 408 |
|
| 409 |
for (; ceptr; ceptr = nptr) |
| 410 |
{ |
| 411 |
nptr = ceptr->next; |
| 412 |
|
| 413 |
if (ceptr->entries) |
| 414 |
mowgli_config_file_entry_free(ceptr->entries); |
| 415 |
|
| 416 |
/* ce_varname and ce_vardata are inside cf_mem */ |
| 417 |
mowgli_free(ceptr); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
void |
| 422 |
mowgli_config_file_free(mowgli_config_file_t *cfptr) |
| 423 |
{ |
| 424 |
mowgli_config_file_t *nptr; |
| 425 |
|
| 426 |
for (; cfptr; cfptr = nptr) |
| 427 |
{ |
| 428 |
nptr = cfptr->next; |
| 429 |
|
| 430 |
if (cfptr->entries) |
| 431 |
mowgli_config_file_entry_free(cfptr->entries); |
| 432 |
|
| 433 |
mowgli_free(cfptr->filename); |
| 434 |
mowgli_free(cfptr->mem); |
| 435 |
mowgli_free(cfptr); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
static mowgli_config_file_t * |
| 440 |
mowgli_config_file_load_internal(mowgli_config_file_t *parent, const char *filename) |
| 441 |
{ |
| 442 |
struct stat sb; |
| 443 |
|
| 444 |
FILE *fp; |
| 445 |
size_t ret; |
| 446 |
char *buf = NULL; |
| 447 |
mowgli_config_file_t *cfptr; |
| 448 |
static int nestcnt; |
| 449 |
|
| 450 |
if (nestcnt > MAX_INCLUDE_NESTING) |
| 451 |
{ |
| 452 |
mowgli_config_file_error(parent, "Includes nested too deep \"%s\"\n", filename); |
| 453 |
return NULL; |
| 454 |
} |
| 455 |
|
| 456 |
fp = fopen(filename, "rb"); |
| 457 |
|
| 458 |
if (!fp) |
| 459 |
{ |
| 460 |
mowgli_config_file_error(parent, "Couldn't open \"%s\": %s\n", filename, strerror(errno)); |
| 461 |
return NULL; |
| 462 |
} |
| 463 |
|
| 464 |
if (stat(filename, &sb) == -1) |
| 465 |
{ |
| 466 |
mowgli_config_file_error(parent, "Couldn't fstat \"%s\": %s\n", filename, strerror(errno)); |
| 467 |
fclose(fp); |
| 468 |
return NULL; |
| 469 |
} |
| 470 |
|
| 471 |
if (!S_ISREG(sb.st_mode)) |
| 472 |
{ |
| 473 |
mowgli_config_file_error(parent, "Not a regular file: \"%s\"\n", filename); |
| 474 |
fclose(fp); |
| 475 |
return NULL; |
| 476 |
} |
| 477 |
|
| 478 |
if (sb.st_size > SSIZE_MAX - 1) |
| 479 |
{ |
| 480 |
mowgli_config_file_error(parent, "File too large: \"%s\"\n", filename); |
| 481 |
fclose(fp); |
| 482 |
return NULL; |
| 483 |
} |
| 484 |
|
| 485 |
buf = mowgli_alloc(sb.st_size + 1); |
| 486 |
|
| 487 |
if (sb.st_size) |
| 488 |
{ |
| 489 |
errno = 0; |
| 490 |
ret = fread(buf, 1, sb.st_size, fp); |
| 491 |
|
| 492 |
if (ret != (size_t) sb.st_size) |
| 493 |
{ |
| 494 |
mowgli_config_file_error(parent, "Error reading \"%s\": %s\n", filename, strerror(errno ? errno : EFAULT)); |
| 495 |
mowgli_free(buf); |
| 496 |
fclose(fp); |
| 497 |
return NULL; |
| 498 |
} |
| 499 |
} |
| 500 |
else |
| 501 |
{ |
| 502 |
ret = 0; |
| 503 |
} |
| 504 |
|
| 505 |
buf[ret] = '\0'; |
| 506 |
fclose(fp); |
| 507 |
nestcnt++; |
| 508 |
cfptr = mowgli_config_file_parse(filename, buf); |
| 509 |
nestcnt--; |
| 510 |
|
| 511 |
/* buf is owned by cfptr or freed now */ |
| 512 |
return cfptr; |
| 513 |
} |
| 514 |
|
| 515 |
mowgli_config_file_t * |
| 516 |
mowgli_config_file_load(const char *filename) |
| 517 |
{ |
| 518 |
return mowgli_config_file_load_internal(NULL, filename); |
| 519 |
} |
| 520 |
|
| 521 |
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
| 522 |
* vim:ts=8 |
| 523 |
* vim:sw=8 |
| 524 |
* vim:noexpandtab |
| 525 |
*/ |