chiark / gitweb /
lib/configuration.c, lib/uaudio-rtp.c: Allow tweaking MTU-discovery.
[disorder] / lib / configuration.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
b0116b5c 3 * Copyright (C) 2004-2011, 2013 Richard Kettlewell
313acc77 4 * Portions copyright (C) 2007 Mark Wooding
460b9539 5 *
e7eb3a27 6 * This program is free software: you can redistribute it and/or modify
460b9539 7 * it under the terms of the GNU General Public License as published by
e7eb3a27 8 * the Free Software Foundation, either version 3 of the License, or
460b9539 9 * (at your option) any later version.
e7eb3a27
RK
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
460b9539 16 * You should have received a copy of the GNU General Public License
e7eb3a27 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 18 */
0e4472a0 19/** @file lib/configuration.c
20 * @brief Configuration file support
21 */
460b9539 22
05b75f8d 23#include "common.h"
460b9539 24
460b9539 25#include <errno.h>
26#include <sys/types.h>
27#include <sys/stat.h>
cca89d7c
RK
28#if HAVE_UNISTD_H
29# include <unistd.h>
30#endif
460b9539 31#include <ctype.h>
32#include <stddef.h>
cca89d7c
RK
33#if HAVE_PWD_H
34# include <pwd.h>
35#endif
36#if HAVE_LANGINFO_H
37# include <langinfo.h>
38#endif
a2e9d147 39
9e42afcd
RK
40#if HAVE_SHLOBJ_H
41# include <Shlobj.h>
42#endif
460b9539 43#include <signal.h>
44
04e1fa7c 45#include "rights.h"
460b9539 46#include "configuration.h"
47#include "mem.h"
48#include "log.h"
49#include "split.h"
50#include "syscalls.h"
51#include "table.h"
52#include "inputline.h"
53#include "charset.h"
54#include "defs.h"
460b9539 55#include "printf.h"
a2e9d147 56#include "regexp.h"
c7e016d3 57#include "regsub.h"
460b9539 58#include "signame.h"
637fdea3 59#include "authhash.h"
e6a35d1c 60#include "vector.h"
9e42afcd 61#if !_WIN32
b50cfb8a 62#include "uaudio.h"
9e42afcd 63#endif
460b9539 64
3f3bb97b
RK
65/** @brief Path to config file
66 *
9e6c445d 67 * set_configfile() sets the default if it is null.
3f3bb97b 68 */
460b9539 69char *configfile;
70
63ad732f
RK
71/** @brief Read user configuration
72 *
73 * If clear, the user-specific configuration is not read.
74 */
75int config_per_user = 1;
76
9e42afcd 77#if !_WIN32
b50cfb8a
RK
78/** @brief Table of audio APIs
79 *
80 * Only set in server processes.
81 */
82const struct uaudio *const *config_uaudio_apis;
9e42afcd 83#endif
b50cfb8a 84
3f3bb97b 85/** @brief Config file parser state */
460b9539 86struct config_state {
3f3bb97b 87 /** @brief Filename */
460b9539 88 const char *path;
34d37b3e 89
3f3bb97b 90 /** @brief Line number */
460b9539 91 int line;
34d37b3e 92
3f3bb97b 93 /** @brief Configuration object under construction */
460b9539 94 struct config *config;
95};
96
3f3bb97b 97/** @brief Current configuration */
460b9539 98struct config *config;
99
3f3bb97b 100/** @brief One configuration item */
460b9539 101struct conf {
3f3bb97b 102 /** @brief Name as it appears in the config file */
460b9539 103 const char *name;
34d37b3e 104
3f3bb97b 105 /** @brief Offset in @ref config structure */
460b9539 106 size_t offset;
34d37b3e 107
3f3bb97b 108 /** @brief Pointer to item type */
460b9539 109 const struct conftype *type;
34d37b3e
RK
110
111 /** @brief Pointer to item-specific validation routine
112 * @param cs Configuration state
113 * @param nvec Length of (proposed) new value
114 * @param vec Elements of new value
115 * @return 0 on success, non-0 on error
116 *
117 * The validate function should report any error it detects.
118 */
460b9539 119 int (*validate)(const struct config_state *cs,
120 int nvec, char **vec);
121};
122
3f3bb97b 123/** @brief Type of a configuration item */
460b9539 124struct conftype {
34d37b3e
RK
125 /** @brief Pointer to function to set item
126 * @param cs Configuration state
127 * @param whoami Configuration item to set
128 * @param nvec Length of new value
129 * @param vec New value
130 * @return 0 on success, non-0 on error
131 */
460b9539 132 int (*set)(const struct config_state *cs,
133 const struct conf *whoami,
134 int nvec, char **vec);
34d37b3e
RK
135
136 /** @brief Pointer to function to free item
137 * @param c Configuration structure to free an item of
138 * @param whoami Configuration item to free
139 */
460b9539 140 void (*free)(struct config *c, const struct conf *whoami);
141};
142
3f3bb97b 143/** @brief Compute the address of an item */
460b9539 144#define ADDRESS(C, TYPE) ((TYPE *)((char *)(C) + whoami->offset))
3f3bb97b 145/** @brief Return the value of an item */
460b9539 146#define VALUE(C, TYPE) (*ADDRESS(C, TYPE))
147
9417e1a3
RK
148static int stringlist_compare(const struct stringlist *a,
149 const struct stringlist *b);
150static int namepartlist_compare(const struct namepartlist *a,
151 const struct namepartlist *b);
152
460b9539 153static int set_signal(const struct config_state *cs,
154 const struct conf *whoami,
155 int nvec, char **vec) {
156 int n;
157
158 if(nvec != 1) {
2e9ba080 159 disorder_error(0, "%s:%d: '%s' requires one argument",
460b9539 160 cs->path, cs->line, whoami->name);
161 return -1;
162 }
163 if((n = find_signal(vec[0])) == -1) {
2e9ba080 164 disorder_error(0, "%s:%d: unknown signal '%s'",
460b9539 165 cs->path, cs->line, vec[0]);
166 return -1;
167 }
168 VALUE(cs->config, int) = n;
169 return 0;
170}
171
172static int set_collections(const struct config_state *cs,
173 const struct conf *whoami,
174 int nvec, char **vec) {
175 struct collectionlist *cl;
01cef138 176 const char *root, *encoding, *module;
460b9539 177
01cef138
RK
178 switch(nvec) {
179 case 1:
180 module = 0;
181 encoding = 0;
182 root = vec[0];
183 break;
184 case 2:
185 module = vec[0];
186 encoding = 0;
187 root = vec[1];
188 break;
189 case 3:
190 module = vec[0];
191 encoding = vec[1];
192 root = vec[2];
193 break;
194 case 0:
2e9ba080
RK
195 disorder_error(0, "%s:%d: '%s' requires at least one argument",
196 cs->path, cs->line, whoami->name);
01cef138
RK
197 return -1;
198 default:
2e9ba080
RK
199 disorder_error(0, "%s:%d: '%s' requires at most three arguments",
200 cs->path, cs->line, whoami->name);
460b9539 201 return -1;
202 }
01cef138
RK
203 /* Sanity check root */
204 if(root[0] != '/') {
2e9ba080
RK
205 disorder_error(0, "%s:%d: collection root must start with '/'",
206 cs->path, cs->line);
460b9539 207 return -1;
208 }
01cef138 209 if(root[1] && root[strlen(root)-1] == '/') {
2e9ba080
RK
210 disorder_error(0, "%s:%d: collection root must not end with '/'",
211 cs->path, cs->line);
460b9539 212 return -1;
213 }
01cef138
RK
214 /* Defaults */
215 if(!module)
216 module = "fs";
cca89d7c 217#if HAVE_LANGINFO_H
01cef138
RK
218 if(!encoding)
219 encoding = nl_langinfo(CODESET);
cca89d7c
RK
220#else
221 if(!encoding)
222 encoding = "ascii";
223#endif
460b9539 224 cl = ADDRESS(cs->config, struct collectionlist);
225 ++cl->n;
226 cl->s = xrealloc(cl->s, cl->n * sizeof (struct collection));
01cef138
RK
227 cl->s[cl->n - 1].module = xstrdup(module);
228 cl->s[cl->n - 1].encoding = xstrdup(encoding);
229 cl->s[cl->n - 1].root = xstrdup(root);
460b9539 230 return 0;
231}
232
233static int set_boolean(const struct config_state *cs,
234 const struct conf *whoami,
235 int nvec, char **vec) {
236 int state;
237
238 if(nvec != 1) {
2e9ba080
RK
239 disorder_error(0, "%s:%d: '%s' takes only one argument",
240 cs->path, cs->line, whoami->name);
460b9539 241 return -1;
242 }
243 if(!strcmp(vec[0], "yes")) state = 1;
244 else if(!strcmp(vec[0], "no")) state = 0;
245 else {
2e9ba080
RK
246 disorder_error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'",
247 cs->path, cs->line, whoami->name);
460b9539 248 return -1;
249 }
250 VALUE(cs->config, int) = state;
251 return 0;
252}
253
254static int set_string(const struct config_state *cs,
255 const struct conf *whoami,
256 int nvec, char **vec) {
257 if(nvec != 1) {
2e9ba080
RK
258 disorder_error(0, "%s:%d: '%s' takes only one argument",
259 cs->path, cs->line, whoami->name);
460b9539 260 return -1;
261 }
47854c5f 262 xfree(VALUE(cs->config, char *));
460b9539 263 VALUE(cs->config, char *) = xstrdup(vec[0]);
264 return 0;
265}
266
460b9539 267static int set_integer(const struct config_state *cs,
268 const struct conf *whoami,
269 int nvec, char **vec) {
270 char *e;
271
272 if(nvec != 1) {
2e9ba080
RK
273 disorder_error(0, "%s:%d: '%s' takes only one argument",
274 cs->path, cs->line, whoami->name);
460b9539 275 return -1;
276 }
277 if(xstrtol(ADDRESS(cs->config, long), vec[0], &e, 0)) {
2e9ba080 278 disorder_error(errno, "%s:%d: converting integer", cs->path, cs->line);
460b9539 279 return -1;
280 }
281 if(*e) {
2e9ba080 282 disorder_error(0, "%s:%d: invalid integer syntax", cs->path, cs->line);
460b9539 283 return -1;
284 }
285 return 0;
286}
287
288static int set_stringlist_accum(const struct config_state *cs,
289 const struct conf *whoami,
290 int nvec, char **vec) {
291 int n;
292 struct stringlist *s;
293 struct stringlistlist *sll;
294
295 sll = ADDRESS(cs->config, struct stringlistlist);
40c30921
RK
296 if(nvec == 0) {
297 sll->n = 0;
298 return 0;
299 }
460b9539 300 sll->n++;
301 sll->s = xrealloc(sll->s, (sll->n * sizeof (struct stringlist)));
302 s = &sll->s[sll->n - 1];
303 s->n = nvec;
304 s->s = xmalloc((nvec + 1) * sizeof (char *));
305 for(n = 0; n < nvec; ++n)
306 s->s[n] = xstrdup(vec[n]);
307 return 0;
308}
309
310static int set_string_accum(const struct config_state *cs,
311 const struct conf *whoami,
312 int nvec, char **vec) {
313 int n;
314 struct stringlist *sl;
315
316 sl = ADDRESS(cs->config, struct stringlist);
40c30921
RK
317 if(nvec == 0) {
318 sl->n = 0;
319 return 0;
320 }
460b9539 321 for(n = 0; n < nvec; ++n) {
322 sl->n++;
323 sl->s = xrealloc(sl->s, (sl->n * sizeof (char *)));
324 sl->s[sl->n - 1] = xstrdup(vec[n]);
325 }
326 return 0;
327}
328
9d5da576 329static int parse_sample_format(const struct config_state *cs,
6d2d327c 330 struct stream_header *format,
9d5da576 331 int nvec, char **vec) {
332 char *p = vec[0];
333 long t;
334
6d2d327c 335 if(nvec != 1) {
2e9ba080 336 disorder_error(0, "%s:%d: wrong number of arguments", cs->path, cs->line);
9d5da576 337 return -1;
338 }
6d2d327c 339 if(xstrtol(&t, p, &p, 0)) {
2e9ba080
RK
340 disorder_error(errno, "%s:%d: converting bits-per-sample",
341 cs->path, cs->line);
9d5da576 342 return -1;
343 }
6d2d327c 344 if(t != 8 && t != 16) {
ef0a7964 345 disorder_error(0, "%s:%d: bad bits-per-sample (%ld)",
2e9ba080 346 cs->path, cs->line, t);
9d5da576 347 return -1;
348 }
9e42afcd 349 if(format) format->bits = (uint8_t)t;
9d5da576 350 switch (*p) {
6d2d327c
RK
351 case 'l': case 'L': t = ENDIAN_LITTLE; p++; break;
352 case 'b': case 'B': t = ENDIAN_BIG; p++; break;
353 default: t = ENDIAN_NATIVE; break;
9d5da576 354 }
9e42afcd 355 if(format) format->endian = (uint8_t)t;
6d2d327c 356 if(*p != '/') {
2e9ba080 357 disorder_error(errno, "%s:%d: expected `/' after bits-per-sample",
9d5da576 358 cs->path, cs->line);
359 return -1;
360 }
361 p++;
6d2d327c 362 if(xstrtol(&t, p, &p, 0)) {
2e9ba080 363 disorder_error(errno, "%s:%d: converting sample-rate", cs->path, cs->line);
9d5da576 364 return -1;
365 }
6d2d327c 366 if(t < 1 || t > INT_MAX) {
2e9ba080 367 disorder_error(0, "%s:%d: silly sample-rate (%ld)", cs->path, cs->line, t);
9d5da576 368 return -1;
369 }
6d2d327c
RK
370 if(format) format->rate = t;
371 if(*p != '/') {
2e9ba080
RK
372 disorder_error(0, "%s:%d: expected `/' after sample-rate",
373 cs->path, cs->line);
9d5da576 374 return -1;
375 }
376 p++;
6d2d327c 377 if(xstrtol(&t, p, &p, 0)) {
2e9ba080 378 disorder_error(errno, "%s:%d: converting channels", cs->path, cs->line);
9d5da576 379 return -1;
380 }
6d2d327c 381 if(t < 1 || t > 8) {
2e9ba080
RK
382 disorder_error(0, "%s:%d: silly number (%ld) of channels",
383 cs->path, cs->line, t);
9d5da576 384 return -1;
385 }
9e42afcd 386 if(format) format->channels = (uint8_t)t;
6d2d327c 387 if(*p) {
2e9ba080 388 disorder_error(0, "%s:%d: junk after channels", cs->path, cs->line);
9d5da576 389 return -1;
390 }
391 return 0;
392}
393
394static int set_sample_format(const struct config_state *cs,
395 const struct conf *whoami,
396 int nvec, char **vec) {
6d2d327c 397 return parse_sample_format(cs, ADDRESS(cs->config, struct stream_header),
9d5da576 398 nvec, vec);
399}
400
460b9539 401static int set_namepart(const struct config_state *cs,
402 const struct conf *whoami,
403 int nvec, char **vec) {
404 struct namepartlist *npl = ADDRESS(cs->config, struct namepartlist);
405 unsigned reflags;
a2e9d147
MW
406 regexp *re;
407 char errstr[RXCERR_LEN];
408 size_t erroffset;
409 int n;
460b9539 410
411 if(nvec < 3) {
2e9ba080
RK
412 disorder_error(0, "%s:%d: namepart needs at least 3 arguments",
413 cs->path, cs->line);
460b9539 414 return -1;
415 }
416 if(nvec > 5) {
2e9ba080
RK
417 disorder_error(0, "%s:%d: namepart needs at most 5 arguments",
418 cs->path, cs->line);
460b9539 419 return -1;
420 }
421 reflags = nvec >= 5 ? regsub_flags(vec[4]) : 0;
a2e9d147
MW
422 if(!(re = regexp_compile(vec[1], regsub_compile_options(reflags),
423 errstr, sizeof(errstr), &erroffset)))
424 {
425 disorder_error(0, "%s:%d: compiling regexp /%s/: %s (offset %zu)",
2e9ba080 426 cs->path, cs->line, vec[1], errstr, erroffset);
460b9539 427 return -1;
428 }
429 npl->s = xrealloc(npl->s, (npl->n + 1) * sizeof (struct namepart));
430 npl->s[npl->n].part = xstrdup(vec[0]);
431 npl->s[npl->n].re = re;
9417e1a3 432 npl->s[npl->n].res = xstrdup(vec[1]);
460b9539 433 npl->s[npl->n].replace = xstrdup(vec[2]);
434 npl->s[npl->n].context = xstrdup(vec[3]);
435 npl->s[npl->n].reflags = reflags;
436 ++npl->n;
437 /* XXX a bit of a bodge; relies on there being very few parts. */
438 for(n = 0; (n < cs->config->nparts
439 && strcmp(cs->config->parts[n], vec[0])); ++n)
440 ;
441 if(n >= cs->config->nparts) {
442 cs->config->parts = xrealloc(cs->config->parts,
443 (cs->config->nparts + 1) * sizeof (char *));
444 cs->config->parts[cs->config->nparts++] = xstrdup(vec[0]);
445 }
446 return 0;
447}
448
449static int set_transform(const struct config_state *cs,
450 const struct conf *whoami,
451 int nvec, char **vec) {
452 struct transformlist *tl = ADDRESS(cs->config, struct transformlist);
a2e9d147
MW
453 regexp *re;
454 char errstr[RXCERR_LEN];
460b9539 455 unsigned reflags;
a2e9d147 456 size_t erroffset;
460b9539 457
458 if(nvec < 3) {
2e9ba080
RK
459 disorder_error(0, "%s:%d: transform needs at least 3 arguments",
460 cs->path, cs->line);
460b9539 461 return -1;
462 }
463 if(nvec > 5) {
2e9ba080
RK
464 disorder_error(0, "%s:%d: transform needs at most 5 arguments",
465 cs->path, cs->line);
460b9539 466 return -1;
467 }
468 reflags = (nvec >= 5 ? regsub_flags(vec[4]) : 0);
a2e9d147
MW
469 if(!(re = regexp_compile(vec[1], regsub_compile_options(reflags),
470 errstr, sizeof(errstr), &erroffset)))
471 {
472 disorder_error(0, "%s:%d: compiling regexp /%s/: %s (offset %zu)",
2e9ba080 473 cs->path, cs->line, vec[1], errstr, erroffset);
460b9539 474 return -1;
475 }
476 tl->t = xrealloc(tl->t, (tl->n + 1) * sizeof (struct namepart));
477 tl->t[tl->n].type = xstrdup(vec[0]);
478 tl->t[tl->n].context = xstrdup(vec[3] ? vec[3] : "*");
479 tl->t[tl->n].re = re;
480 tl->t[tl->n].replace = xstrdup(vec[2]);
481 tl->t[tl->n].flags = reflags;
482 ++tl->n;
483 return 0;
484}
485
04e1fa7c
RK
486static int set_rights(const struct config_state *cs,
487 const struct conf *whoami,
488 int nvec, char **vec) {
04e1fa7c 489 if(nvec != 1) {
2e9ba080
RK
490 disorder_error(0, "%s:%d: '%s' requires one argument",
491 cs->path, cs->line, whoami->name);
04e1fa7c
RK
492 return -1;
493 }
0f55e905 494 if(parse_rights(vec[0], 0, 1)) {
2e9ba080
RK
495 disorder_error(0, "%s:%d: invalid rights string '%s'",
496 cs->path, cs->line, vec[0]);
04e1fa7c
RK
497 return -1;
498 }
47854c5f 499 return set_string(cs, whoami, nvec, vec);
04e1fa7c
RK
500}
501
76e72f65
RK
502static int set_netaddress(const struct config_state *cs,
503 const struct conf *whoami,
504 int nvec, char **vec) {
505 struct netaddress *na = ADDRESS(cs->config, struct netaddress);
506
507 if(netaddress_parse(na, nvec, vec)) {
2e9ba080 508 disorder_error(0, "%s:%d: invalid network address", cs->path, cs->line);
76e72f65
RK
509 return -1;
510 }
511 return 0;
512}
513
460b9539 514/* free functions */
515
516static void free_none(struct config attribute((unused)) *c,
517 const struct conf attribute((unused)) *whoami) {
518}
519
520static void free_string(struct config *c,
521 const struct conf *whoami) {
522 xfree(VALUE(c, char *));
f183d30b 523 VALUE(c, char *) = 0;
460b9539 524}
525
526static void free_stringlist(struct config *c,
527 const struct conf *whoami) {
528 int n;
529 struct stringlist *sl = ADDRESS(c, struct stringlist);
530
531 for(n = 0; n < sl->n; ++n)
532 xfree(sl->s[n]);
533 xfree(sl->s);
534}
535
536static void free_stringlistlist(struct config *c,
537 const struct conf *whoami) {
538 int n, m;
539 struct stringlistlist *sll = ADDRESS(c, struct stringlistlist);
540 struct stringlist *sl;
541
542 for(n = 0; n < sll->n; ++n) {
543 sl = &sll->s[n];
544 for(m = 0; m < sl->n; ++m)
545 xfree(sl->s[m]);
546 xfree(sl->s);
547 }
548 xfree(sll->s);
549}
550
551static void free_collectionlist(struct config *c,
552 const struct conf *whoami) {
553 struct collectionlist *cll = ADDRESS(c, struct collectionlist);
554 struct collection *cl;
555 int n;
556
557 for(n = 0; n < cll->n; ++n) {
558 cl = &cll->s[n];
559 xfree(cl->module);
560 xfree(cl->encoding);
561 xfree(cl->root);
562 }
563 xfree(cll->s);
564}
565
566static void free_namepartlist(struct config *c,
567 const struct conf *whoami) {
568 struct namepartlist *npl = ADDRESS(c, struct namepartlist);
569 struct namepart *np;
570 int n;
571
572 for(n = 0; n < npl->n; ++n) {
573 np = &npl->s[n];
574 xfree(np->part);
a2e9d147 575 regexp_free(np->re);
9417e1a3 576 xfree(np->res);
460b9539 577 xfree(np->replace);
578 xfree(np->context);
579 }
580 xfree(npl->s);
581}
582
583static void free_transformlist(struct config *c,
584 const struct conf *whoami) {
585 struct transformlist *tl = ADDRESS(c, struct transformlist);
586 struct transform *t;
587 int n;
588
589 for(n = 0; n < tl->n; ++n) {
590 t = &tl->t[n];
591 xfree(t->type);
a2e9d147 592 regexp_free(t->re);
460b9539 593 xfree(t->replace);
594 xfree(t->context);
595 }
596 xfree(tl->t);
597}
598
76e72f65
RK
599static void free_netaddress(struct config *c,
600 const struct conf *whoami) {
601 struct netaddress *na = ADDRESS(c, struct netaddress);
602
603 xfree(na->address);
604}
605
460b9539 606/* configuration types */
607
608static const struct conftype
609 type_signal = { set_signal, free_none },
610 type_collections = { set_collections, free_collectionlist },
611 type_boolean = { set_boolean, free_none },
612 type_string = { set_string, free_string },
460b9539 613 type_integer = { set_integer, free_none },
614 type_stringlist_accum = { set_stringlist_accum, free_stringlistlist },
615 type_string_accum = { set_string_accum, free_stringlist },
9d5da576 616 type_sample_format = { set_sample_format, free_none },
460b9539 617 type_namepart = { set_namepart, free_namepartlist },
e83d0967 618 type_transform = { set_transform, free_transformlist },
76e72f65 619 type_netaddress = { set_netaddress, free_netaddress },
47854c5f 620 type_rights = { set_rights, free_string };
460b9539 621
622/* specific validation routine */
623
34d37b3e
RK
624/** @brief Perform a test on a filename
625 * @param test Test function to call on mode bits
626 * @param what Type of file sought
627 *
628 * If @p test returns 0 then the file is not a @p what and an error
629 * is reported and -1 is returned.
630 */
2e9ba080
RK
631#define VALIDATE_FILE(test, what) do { \
632 struct stat sb; \
633 int n; \
634 \
635 for(n = 0; n < nvec; ++n) { \
636 if(stat(vec[n], &sb) < 0) { \
637 disorder_error(errno, "%s:%d: %s", \
638 cs->path, cs->line, vec[n]); \
639 return -1; \
640 } \
641 if(!test(sb.st_mode)) { \
642 disorder_error(0, "%s:%d: %s is not a %s", \
643 cs->path, cs->line, vec[n], what); \
644 return -1; \
645 } \
646 } \
460b9539 647} while(0)
648
34d37b3e
RK
649/** @brief Validate an absolute path
650 * @param cs Configuration state
651 * @param nvec Length of (proposed) new value
652 * @param vec Elements of new value
653 * @return 0 on success, non-0 on error
654 */
659d87e8
RK
655static int validate_isabspath(const struct config_state *cs,
656 int nvec, char **vec) {
657 int n;
658
659 for(n = 0; n < nvec; ++n)
660 if(vec[n][0] != '/') {
70adc86b 661 disorder_error(0, "%s:%d: %s: not an absolute path",
2e9ba080 662 cs->path, cs->line, vec[n]);
659d87e8
RK
663 return -1;
664 }
665 return 0;
666}
667
34d37b3e
RK
668/** @brief Validate an existing directory
669 * @param cs Configuration state
670 * @param nvec Length of (proposed) new value
671 * @param vec Elements of new value
672 * @return 0 on success, non-0 on error
673 */
460b9539 674static int validate_isdir(const struct config_state *cs,
675 int nvec, char **vec) {
676 VALIDATE_FILE(S_ISDIR, "directory");
677 return 0;
678}
679
34d37b3e
RK
680/** @brief Validate an existing regular file
681 * @param cs Configuration state
682 * @param nvec Length of (proposed) new value
683 * @param vec Elements of new value
684 * @return 0 on success, non-0 on error
685 */
460b9539 686static int validate_isreg(const struct config_state *cs,
687 int nvec, char **vec) {
688 VALIDATE_FILE(S_ISREG, "regular file");
689 return 0;
690}
691
34d37b3e
RK
692/** @brief Validate a player pattern
693 * @param cs Configuration state
694 * @param nvec Length of (proposed) new value
695 * @param vec Elements of new value
696 * @return 0 on success, non-0 on error
697 */
460b9539 698static int validate_player(const struct config_state *cs,
699 int nvec,
700 char attribute((unused)) **vec) {
ec554a3d 701 if(nvec && nvec < 2) {
2e9ba080
RK
702 disorder_error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
703 cs->path, cs->line);
460b9539 704 return -1;
705 }
706 return 0;
707}
708
34d37b3e
RK
709/** @brief Validate a track length pattern
710 * @param cs Configuration state
711 * @param nvec Length of (proposed) new value
712 * @param vec Elements of new value
713 * @return 0 on success, non-0 on error
714 */
62dc3748
RK
715static int validate_tracklength(const struct config_state *cs,
716 int nvec,
717 char attribute((unused)) **vec) {
ec554a3d 718 if(nvec && nvec < 2) {
2e9ba080
RK
719 disorder_error(0, "%s:%d: should be at least 'tracklength PATTERN MODULE'",
720 cs->path, cs->line);
62dc3748
RK
721 return -1;
722 }
723 return 0;
724}
725
187b1faf 726/** @brief Common code for validating integer values
34d37b3e
RK
727 * @param cs Configuration state
728 * @param nvec Length of (proposed) new value
729 * @param vec Elements of new value
187b1faf 730 * @param n_out Where to put the value
34d37b3e 731 */
187b1faf
MW
732static int common_validate_integer(const struct config_state *cs,
733 int nvec, char **vec, long *n_out) {
b94e827f 734 char errbuf[1024];
460b9539 735
736 if(nvec < 1) {
2e9ba080 737 disorder_error(0, "%s:%d: missing argument", cs->path, cs->line);
460b9539 738 return -1;
739 }
740 if(nvec > 1) {
2e9ba080 741 disorder_error(0, "%s:%d: too many arguments", cs->path, cs->line);
460b9539 742 return -1;
743 }
187b1faf 744 if(xstrtol(n_out, vec[0], 0, 0)) {
b94e827f
RK
745 disorder_error(0, "%s:%d: %s", cs->path, cs->line,
746 format_error(ec_errno, errno, errbuf, sizeof errbuf));
460b9539 747 return -1;
748 }
187b1faf
MW
749 return 0;
750}
751
752/** @brief Validate a non-negative (@c long) integer
753 * @param cs Configuration state
754 * @param nvec Length of (proposed) new value
755 * @param vec Elements of new value
756 * @return 0 on success, non-0 on error
757 */
758static int validate_non_negative(const struct config_state *cs,
759 int nvec, char **vec) {
760 long n;
761 if(common_validate_integer(cs, nvec, vec, &n)) return -1;
460b9539 762 if(n < 0) {
2e9ba080 763 disorder_error(0, "%s:%d: must not be negative", cs->path, cs->line);
460b9539 764 return -1;
765 }
766 return 0;
767}
768
34d37b3e
RK
769/** @brief Validate a positive (@c long) integer
770 * @param cs Configuration state
771 * @param nvec Length of (proposed) new value
772 * @param vec Elements of new value
773 * @return 0 on success, non-0 on error
774 */
460b9539 775static int validate_positive(const struct config_state *cs,
776 int nvec, char **vec) {
777 long n;
187b1faf 778 if(common_validate_integer(cs, nvec, vec, &n)) return -1;
460b9539 779 if(n <= 0) {
2e9ba080 780 disorder_error(0, "%s:%d: must be positive", cs->path, cs->line);
460b9539 781 return -1;
782 }
783 return 0;
784}
785
9e42afcd 786#if !_WIN32
34d37b3e
RK
787/** @brief Validate a system username
788 * @param cs Configuration state
789 * @param nvec Length of (proposed) new value
790 * @param vec Elements of new value
791 * @return 0 on success, non-0 on error
792 */
460b9539 793static int validate_isauser(const struct config_state *cs,
794 int attribute((unused)) nvec,
795 char **vec) {
b3756e27 796 if(!getpwnam(vec[0])) {
2e9ba080 797 disorder_error(0, "%s:%d: no such user as '%s'", cs->path, cs->line, vec[0]);
460b9539 798 return -1;
799 }
800 return 0;
801}
9e42afcd 802#endif
460b9539 803
34d37b3e
RK
804/** @brief Validate a sample format string
805 * @param cs Configuration state
806 * @param nvec Length of (proposed) new value
807 * @param vec Elements of new value
808 * @return 0 on success, non-0 on error
809 */
9d5da576 810static int validate_sample_format(const struct config_state *cs,
811 int attribute((unused)) nvec,
812 char **vec) {
813 return parse_sample_format(cs, 0, nvec, vec);
814}
815
34d37b3e
RK
816/** @brief Validate anything
817 * @param cs Configuration state
818 * @param nvec Length of (proposed) new value
819 * @param vec Elements of new value
820 * @return 0
821 */
460b9539 822static int validate_any(const struct config_state attribute((unused)) *cs,
823 int attribute((unused)) nvec,
824 char attribute((unused)) **vec) {
825 return 0;
826}
827
34d37b3e
RK
828/** @brief Validate a URL
829 * @param cs Configuration state
830 * @param nvec Length of (proposed) new value
831 * @param vec Elements of new value
832 * @return 0 on success, non-0 on error
833 *
834 * Rather cursory.
835 */
460b9539 836static int validate_url(const struct config_state attribute((unused)) *cs,
837 int attribute((unused)) nvec,
838 char **vec) {
839 const char *s;
840 int n;
841 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
842 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
843 s = vec[0];
844 n = strspn(s, ("abcdefghijklmnopqrstuvwxyz"
845 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
846 "0123456789"));
847 if(s[n] != ':') {
2e9ba080 848 disorder_error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
460b9539 849 return -1;
850 }
851 if(!strncmp(s, "http:", 5)
852 || !strncmp(s, "https:", 6)) {
853 s += n + 1;
854 /* we only do a rather cursory check */
855 if(strncmp(s, "//", 2)) {
2e9ba080 856 disorder_error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
460b9539 857 return -1;
858 }
859 }
860 return 0;
861}
862
34d37b3e
RK
863/** @brief Validate an alias pattern
864 * @param cs Configuration state
865 * @param nvec Length of (proposed) new value
866 * @param vec Elements of new value
867 * @return 0 on success, non-0 on error
868 */
460b9539 869static int validate_alias(const struct config_state *cs,
870 int nvec,
871 char **vec) {
872 const char *s;
873 int in_brackets = 0, c;
874
875 if(nvec < 1) {
2e9ba080 876 disorder_error(0, "%s:%d: missing argument", cs->path, cs->line);
460b9539 877 return -1;
878 }
879 if(nvec > 1) {
2e9ba080 880 disorder_error(0, "%s:%d: too many arguments", cs->path, cs->line);
460b9539 881 return -1;
882 }
883 s = vec[0];
884 while((c = (unsigned char)*s++)) {
885 if(in_brackets) {
886 if(c == '}')
887 in_brackets = 0;
888 else if(!isalnum(c)) {
2e9ba080
RK
889 disorder_error(0, "%s:%d: invalid part name in alias expansion in '%s'",
890 cs->path, cs->line, vec[0]);
460b9539 891 return -1;
892 }
893 } else {
894 if(c == '{') {
895 in_brackets = 1;
896 if(*s == '/')
897 ++s;
898 } else if(c == '\\') {
899 if(!(c = (unsigned char)*s++)) {
2e9ba080
RK
900 disorder_error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
901 cs->path, cs->line, vec[0]);
460b9539 902 return -1;
903 } else if(c != '\\' && c != '{') {
2e9ba080
RK
904 disorder_error(0, "%s:%d: invalid escape in alias expansion in '%s'",
905 cs->path, cs->line, vec[0]);
460b9539 906 return -1;
907 }
908 }
909 }
910 ++s;
911 }
912 if(in_brackets) {
2e9ba080
RK
913 disorder_error(0,
914 "%s:%d: unterminated part name in alias expansion in '%s'",
915 cs->path, cs->line, vec[0]);
460b9539 916 return -1;
917 }
918 return 0;
919}
920
34d37b3e
RK
921/** @brief Validate a hash algorithm name
922 * @param cs Configuration state
923 * @param nvec Length of (proposed) new value
924 * @param vec Elements of new value
925 * @return 0 on success, non-0 on error
926 */
637fdea3
RK
927static int validate_algo(const struct config_state attribute((unused)) *cs,
928 int nvec,
929 char **vec) {
930 if(nvec != 1) {
2e9ba080 931 disorder_error(0, "%s:%d: invalid algorithm specification", cs->path, cs->line);
637fdea3
RK
932 return -1;
933 }
934 if(!valid_authhash(vec[0])) {
2e9ba080 935 disorder_error(0, "%s:%d: unsuported algorithm '%s'", cs->path, cs->line, vec[0]);
637fdea3
RK
936 return -1;
937 }
938 return 0;
939}
940
9e42afcd 941#if !_WIN32
34d37b3e
RK
942/** @brief Validate a playback backend name
943 * @param cs Configuration state
944 * @param nvec Length of (proposed) new value
945 * @param vec Elements of new value
946 * @return 0 on success, non-0 on error
947 */
b50cfb8a
RK
948static int validate_backend(const struct config_state attribute((unused)) *cs,
949 int nvec,
950 char **vec) {
951 int n;
952 if(nvec != 1) {
2e9ba080 953 disorder_error(0, "%s:%d: invalid sound API specification", cs->path, cs->line);
b50cfb8a
RK
954 return -1;
955 }
956 if(!strcmp(vec[0], "network")) {
2e9ba080 957 disorder_error(0, "'api network' is deprecated; use 'api rtp'");
b50cfb8a
RK
958 return 0;
959 }
960 if(config_uaudio_apis) {
961 for(n = 0; config_uaudio_apis[n]; ++n)
962 if(!strcmp(vec[0], config_uaudio_apis[n]->name))
963 return 0;
2e9ba080 964 disorder_error(0, "%s:%d: unrecognized sound API '%s'", cs->path, cs->line, vec[0]);
b50cfb8a
RK
965 return -1;
966 }
967 /* In non-server processes we have no idea what's valid */
968 return 0;
969}
9e42afcd 970#endif
b50cfb8a 971
34d37b3e
RK
972/** @brief Validate a pause mode string
973 * @param cs Configuration state
974 * @param nvec Length of (proposed) new value
975 * @param vec Elements of new value
976 * @return 0 on success, non-0 on error
977 */
f75ab9d3
RK
978static int validate_pausemode(const struct config_state attribute((unused)) *cs,
979 int nvec,
980 char **vec) {
981 if(nvec == 1 && (!strcmp(vec[0], "silence") || !strcmp(vec[0], "suspend")))
982 return 0;
2e9ba080 983 disorder_error(0, "%s:%d: invalid pause mode", cs->path, cs->line);
f75ab9d3
RK
984 return -1;
985}
986
10511fad
MW
987/** @brief Validate an MTU-discovery setting
988 * @param cs Configuration state
989 * @param nvec Length of (proposed) new value
990 * @param vec Elements of new value
991 * @return 0 on success, non-0 on error
992 */
993static int validate_mtu_discovery(const struct config_state attribute((unused)) *cs,
994 int nvec,
995 char **vec) {
996 if (nvec == 1 &&
997 (!strcmp(vec[0], "default") ||
998 !strcmp(vec[0], "yes") ||
999 !strcmp(vec[0], "no")))
1000 return 0;
1001 disorder_error(0, "%s:%d: invalid MTU-discovery setting", cs->path, cs->line);
1002 return -1;
1003}
1004
34d37b3e
RK
1005/** @brief Validate a destination network address
1006 * @param cs Configuration state
1007 * @param nvec Length of (proposed) new value
1008 * @param vec Elements of new value
1009 * @return 0 on success, non-0 on error
1010 *
1011 * By a destination address, it is meant that it must not be a wildcard
1012 * address.
1013 */
76e72f65
RK
1014static int validate_destaddr(const struct config_state attribute((unused)) *cs,
1015 int nvec,
1016 char **vec) {
1017 struct netaddress na[1];
1018
1019 if(netaddress_parse(na, nvec, vec)) {
2e9ba080 1020 disorder_error(0, "%s:%d: invalid network address", cs->path, cs->line);
76e72f65
RK
1021 return -1;
1022 }
1023 if(!na->address) {
2e9ba080 1024 disorder_error(0, "%s:%d: destination address required", cs->path, cs->line);
76e72f65
RK
1025 return -1;
1026 }
47854c5f 1027 xfree(na->address);
76e72f65
RK
1028 return 0;
1029}
1030
14b5913c
MW
1031/** @brief Validate an internet address
1032 * @param cs Configuration state
1033 * @param nvec Length of (proposed) new value
1034 * @param vec Elements of new value
1035 * @return 0 on success, non-0 on error
1036 *
1037 * By a destination address, it is meant that it must be either IPv4 or IPv6.
1038 */
1039static int validate_inetaddr(const struct config_state *cs,
1040 int nvec, char **vec) {
1041 struct netaddress na[1];
1042
1043 if(netaddress_parse(na, nvec, vec)) {
1044 disorder_error(0, "%s:%d: invalid network address", cs->path, cs->line);
1045 return -1;
1046 }
1047 switch(na->af) {
1048 case AF_INET: case AF_INET6: case AF_UNSPEC: break;
1049 default:
1050 disorder_error(0, "%s:%d: must be an intenet address",
1051 cs->path, cs->line);
1052 return -1;
1053 }
1054 return 0;
1055}
1056
3f3bb97b 1057/** @brief Item name and and offset */
460b9539 1058#define C(x) #x, offsetof(struct config, x)
3f3bb97b 1059/** @brief Item name and and offset */
460b9539 1060#define C2(x,y) #x, offsetof(struct config, y)
1061
3f3bb97b 1062/** @brief All configuration items */
460b9539 1063static const struct conf conf[] = {
1064 { C(alias), &type_string, validate_alias },
9e42afcd 1065#if !_WIN32
b50cfb8a 1066 { C(api), &type_string, validate_backend },
9e42afcd 1067#endif
637fdea3 1068 { C(authorization_algorithm), &type_string, validate_algo },
76e72f65
RK
1069 { C(broadcast), &type_netaddress, validate_destaddr },
1070 { C(broadcast_from), &type_netaddress, validate_any },
bd8895a8 1071 { C(channel), &type_string, validate_any },
460b9539 1072 { C(checkpoint_kbyte), &type_integer, validate_non_negative },
1073 { C(checkpoint_min), &type_integer, validate_non_negative },
1074 { C(collection), &type_collections, validate_any },
e41a9999 1075 { C(connect), &type_netaddress, validate_destaddr },
b12be54a 1076 { C(cookie_key_lifetime), &type_integer, validate_positive },
2a1c84fb 1077 { C(cookie_login_lifetime), &type_integer, validate_positive },
8818b7fc 1078 { C(dbversion), &type_integer, validate_positive },
04e1fa7c 1079 { C(default_rights), &type_rights, validate_any },
460b9539 1080 { C(device), &type_string, validate_any },
460b9539 1081 { C(history), &type_integer, validate_positive },
9e42afcd 1082#if !_WIN32
659d87e8 1083 { C(home), &type_string, validate_isabspath },
9e42afcd 1084#endif
80dc2c5f 1085 { C(listen), &type_netaddress, validate_any },
bb6ae3fb 1086 { C(mail_sender), &type_string, validate_any },
bd8895a8 1087 { C(mixer), &type_string, validate_any },
8488cf7d 1088 { C(mount_rescan), &type_boolean, validate_any },
61941295 1089 { C(multicast_loop), &type_boolean, validate_any },
23205f9c 1090 { C(multicast_ttl), &type_integer, validate_non_negative },
460b9539 1091 { C(namepart), &type_namepart, validate_any },
05dcfac6
RK
1092 { C(new_bias), &type_integer, validate_positive },
1093 { C(new_bias_age), &type_integer, validate_positive },
d742bb47 1094 { C(new_max), &type_integer, validate_positive },
460b9539 1095 { C2(nice, nice_rescan), &type_integer, validate_non_negative },
1096 { C(nice_rescan), &type_integer, validate_non_negative },
1097 { C(nice_server), &type_integer, validate_any },
1098 { C(nice_speaker), &type_integer, validate_any },
2a10b70b 1099 { C(noticed_history), &type_integer, validate_positive },
460b9539 1100 { C(password), &type_string, validate_any },
f75ab9d3 1101 { C(pause_mode), &type_string, validate_pausemode },
460b9539 1102 { C(player), &type_stringlist_accum, validate_player },
ddbf05c8 1103 { C(playlist_lock_timeout), &type_integer, validate_positive },
2563dc1f 1104 { C(playlist_max) , &type_integer, validate_positive },
460b9539 1105 { C(plugins), &type_string_accum, validate_isdir },
459d4402 1106 { C(queue_pad), &type_integer, validate_positive },
460b9539 1107 { C(refresh), &type_integer, validate_positive },
533272be 1108 { C(refresh_min), &type_integer, validate_non_negative },
6207d2f3 1109 { C(reminder_interval), &type_integer, validate_positive },
810b8083 1110 { C(remote_userman), &type_boolean, validate_any },
2a1c84fb 1111 { C(replay_min), &type_integer, validate_non_negative },
14b5913c 1112 { C(rtp_always_request), &type_boolean, validate_any },
ba70caca 1113 { C(rtp_delay_threshold), &type_integer, validate_positive },
2a2b84aa 1114 { C(rtp_max_payload), &type_integer, validate_positive },
14b5913c
MW
1115 { C(rtp_maxbuffer), &type_integer, validate_non_negative },
1116 { C(rtp_minbuffer), &type_integer, validate_non_negative },
b0116b5c 1117 { C(rtp_mode), &type_string, validate_any },
10511fad 1118 { C(rtp_mtu_discovery), &type_string, validate_mtu_discovery },
14b5913c
MW
1119 { C(rtp_rcvbuf), &type_integer, validate_non_negative },
1120 { C(rtp_request_address), &type_netaddress, validate_inetaddr },
87864f77 1121 { C(rtp_verbose), &type_boolean, validate_any },
9d5da576 1122 { C(sample_format), &type_sample_format, validate_sample_format },
460b9539 1123 { C(scratch), &type_string_accum, validate_isreg },
9e42afcd 1124#if !_WIN32
2eee4b0c 1125 { C(sendmail), &type_string, validate_isabspath },
9e42afcd 1126#endif
61507e3c 1127 { C(short_display), &type_integer, validate_positive },
460b9539 1128 { C(signal), &type_signal, validate_any },
bb6ae3fb 1129 { C(smtp_server), &type_string, validate_any },
5330d674 1130 { C(sox_generation), &type_integer, validate_non_negative },
9e42afcd 1131#if !_WIN32
b50cfb8a 1132 { C2(speaker_backend, api), &type_string, validate_backend },
9e42afcd 1133#endif
9d5da576 1134 { C(speaker_command), &type_string, validate_any },
460b9539 1135 { C(stopword), &type_string_accum, validate_any },
1136 { C(templates), &type_string_accum, validate_isdir },
62dc3748 1137 { C(tracklength), &type_stringlist_accum, validate_tracklength },
460b9539 1138 { C(transform), &type_transform, validate_any },
460b9539 1139 { C(url), &type_string, validate_url },
9e42afcd 1140#if !_WIN32
460b9539 1141 { C(user), &type_string, validate_isauser },
9e42afcd 1142#endif
460b9539 1143 { C(username), &type_string, validate_any },
1144};
1145
3f3bb97b 1146/** @brief Find a configuration item's definition by key */
460b9539 1147static const struct conf *find(const char *key) {
1148 int n;
1149
ba937f01 1150 if((n = TABLE_FIND(conf, name, key)) < 0)
460b9539 1151 return 0;
1152 return &conf[n];
1153}
1154
34d37b3e
RK
1155/** @brief Set a new configuration value
1156 * @param cs Configuration state
1157 * @param nvec Length of @p vec
1158 * @param vec Name and new value
1159 * @return 0 on success, non-0 on error.
1160 *
1161 * @c vec[0] is the name, the rest is the value.
1162 */
460b9539 1163static int config_set(const struct config_state *cs,
1164 int nvec, char **vec) {
1165 const struct conf *which;
1166
1167 D(("config_set %s", vec[0]));
1168 if(!(which = find(vec[0]))) {
2e9ba080 1169 disorder_error(0, "%s:%d: unknown configuration key '%s'",
460b9539 1170 cs->path, cs->line, vec[0]);
1171 return -1;
1172 }
1173 return (which->validate(cs, nvec - 1, vec + 1)
1174 || which->type->set(cs, which, nvec - 1, vec + 1));
1175}
1176
34d37b3e
RK
1177/** @brief Set a configuration item from parameters
1178 * @param cs Configuration state
1179 * @param which Item to set
1180 * @param ... Value as strings, terminated by (char *)NULL
1181 * @return 0 on success, non-0 on error
1182 */
e6a35d1c
RK
1183static int config_set_args(const struct config_state *cs,
1184 const char *which, ...) {
1185 va_list ap;
1186 struct vector v[1];
1187 char *s;
05b3f1f6 1188 int rc;
e6a35d1c
RK
1189
1190 vector_init(v);
1191 vector_append(v, (char *)which);
1192 va_start(ap, which);
1193 while((s = va_arg(ap, char *)))
1194 vector_append(v, s);
1195 va_end(ap);
1196 vector_terminate(v);
05b3f1f6 1197 rc = config_set(cs, v->nvec, v->vec);
47854c5f
RK
1198 xfree(v->vec);
1199 return rc;
e6a35d1c
RK
1200}
1201
34d37b3e
RK
1202/** @brief Error callback used by config_include()
1203 * @param msg Error message
1204 * @param u User data (@ref config_state)
1205 */
460b9539 1206static void config_error(const char *msg, void *u) {
1207 const struct config_state *cs = u;
1208
2e9ba080 1209 disorder_error(0, "%s:%d: %s", cs->path, cs->line, msg);
460b9539 1210}
1211
34d37b3e
RK
1212/** @brief Include a file by name
1213 * @param c Configuration to update
1214 * @param path Path to read
1215 * @return 0 on success, non-0 on error
1216 */
460b9539 1217static int config_include(struct config *c, const char *path) {
1218 FILE *fp;
1219 char *buffer, *inputbuffer, **vec;
1220 int n, ret = 0;
1221 struct config_state cs;
1222
1223 cs.path = path;
1224 cs.line = 0;
1225 cs.config = c;
1226 D(("%s: reading configuration", path));
1227 if(!(fp = fopen(path, "r"))) {
2e9ba080 1228 disorder_error(errno, "error opening %s", path);
460b9539 1229 return -1;
1230 }
1231 while(!inputline(path, fp, &inputbuffer, '\n')) {
1232 ++cs.line;
1233 if(!(buffer = mb2utf8(inputbuffer))) {
2e9ba080 1234 disorder_error(errno, "%s:%d: cannot convert to UTF-8", cs.path, cs.line);
460b9539 1235 ret = -1;
1236 xfree(inputbuffer);
1237 continue;
1238 }
1239 xfree(inputbuffer);
1240 if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES,
1241 config_error, &cs))) {
1242 ret = -1;
1243 xfree(buffer);
1244 continue;
1245 }
1246 if(n) {
34d37b3e 1247 /* 'include' is special-cased */
460b9539 1248 if(!strcmp(vec[0], "include")) {
1249 if(n != 2) {
2e9ba080 1250 disorder_error(0, "%s:%d: must be 'include PATH'", cs.path, cs.line);
460b9539 1251 ret = -1;
1252 } else
1253 config_include(c, vec[1]);
1254 } else
1255 ret |= config_set(&cs, n, vec);
1256 }
1257 for(n = 0; vec[n]; ++n) xfree(vec[n]);
1258 xfree(vec);
1259 xfree(buffer);
1260 }
1261 if(ferror(fp)) {
2e9ba080 1262 disorder_error(errno, "error reading %s", path);
460b9539 1263 ret = -1;
1264 }
1265 fclose(fp);
1266 return ret;
1267}
1268
34d37b3e 1269/** @brief Default stopword setting */
86be0c30 1270static const char *const default_stopwords[] = {
1271 "stopword",
1272
1273 "01",
1274 "02",
1275 "03",
1276 "04",
1277 "05",
1278 "06",
1279 "07",
1280 "08",
1281 "09",
1282 "1",
1283 "10",
1284 "11",
1285 "12",
1286 "13",
1287 "14",
1288 "15",
1289 "16",
1290 "17",
1291 "18",
1292 "19",
1293 "2",
1294 "20",
1295 "21",
1296 "22",
1297 "23",
1298 "24",
1299 "25",
1300 "26",
1301 "27",
1302 "28",
1303 "29",
1304 "3",
1305 "30",
1306 "4",
1307 "5",
1308 "6",
1309 "7",
1310 "8",
1311 "9",
1312 "a",
1313 "am",
1314 "an",
1315 "and",
1316 "as",
1317 "for",
1318 "i",
1319 "im",
1320 "in",
1321 "is",
1322 "of",
1323 "on",
1324 "the",
1325 "to",
1326 "too",
1327 "we",
1328};
1329#define NDEFAULT_STOPWORDS (sizeof default_stopwords / sizeof *default_stopwords)
1330
34d37b3e 1331/** @brief Default player patterns */
e6a35d1c
RK
1332static const char *const default_players[] = {
1333 "*.ogg",
1334 "*.flac",
1335 "*.mp3",
1336 "*.wav",
1337};
1338#define NDEFAULT_PLAYERS (sizeof default_players / sizeof *default_players)
1339
34d37b3e
RK
1340/** @brief Make a new default configuration
1341 * @return New configuration
1342 */
460b9539 1343static struct config *config_default(void) {
1344 struct config *c = xmalloc(sizeof *c);
9e42afcd 1345#if !_WIN32
460b9539 1346 const char *logname;
1347 struct passwd *pw;
9e42afcd 1348#endif
86be0c30 1349 struct config_state cs;
e6a35d1c 1350 size_t n;
460b9539 1351
86be0c30 1352 cs.path = "<internal>";
1353 cs.line = 0;
1354 cs.config = c;
460b9539 1355 /* Strings had better be xstrdup'd as they will get freed at some point. */
460b9539 1356 c->history = 60;
9e42afcd 1357#if !_WIN32
460b9539 1358 c->home = xstrdup(pkgstatedir);
9e42afcd
RK
1359#endif
1360#if _WIN32
1361 {
1362 char buffer[128];
1363 DWORD bufsize = sizeof buffer;
1364 if(!GetUserNameA(buffer, &bufsize))
1365 disorder_fatal(0, "cannot determine our username");
1366 c->username = xstrdup(buffer);
1367 }
1368#else
460b9539 1369 if(!(pw = getpwuid(getuid())))
2e9ba080 1370 disorder_fatal(0, "cannot determine our username");
460b9539 1371 logname = pw->pw_name;
1372 c->username = xstrdup(logname);
9e42afcd 1373#endif
460b9539 1374 c->refresh = 15;
533272be 1375 c->refresh_min = 1;
9e42afcd 1376#ifdef SIGKILL
460b9539 1377 c->signal = SIGKILL;
9e42afcd
RK
1378#else
1379 c->signal = SIGTERM;
1380#endif
460b9539 1381 c->alias = xstrdup("{/artist}{/album}{/title}{ext}");
460b9539 1382 c->device = xstrdup("default");
1383 c->nice_rescan = 10;
9d5da576 1384 c->speaker_command = 0;
1385 c->sample_format.bits = 16;
1386 c->sample_format.rate = 44100;
1387 c->sample_format.channels = 2;
6d2d327c 1388 c->sample_format.endian = ENDIAN_NATIVE;
459d4402 1389 c->queue_pad = 10;
cebe3127 1390 c->replay_min = 8 * 3600;
b50cfb8a 1391 c->api = NULL;
23205f9c 1392 c->multicast_ttl = 1;
61941295 1393 c->multicast_loop = 1;
637fdea3 1394 c->authorization_algorithm = xstrdup("sha1");
2a10b70b 1395 c->noticed_history = 31;
61507e3c 1396 c->short_display = 32;
bd8895a8 1397 c->mixer = 0;
1398 c->channel = 0;
8818b7fc 1399 c->dbversion = 2;
b12be54a
RK
1400 c->cookie_login_lifetime = 86400;
1401 c->cookie_key_lifetime = 86400 * 7;
9e42afcd 1402#if !_WIN32
2eee4b0c
RK
1403 if(sendmail_binary[0] && strcmp(sendmail_binary, "none"))
1404 c->sendmail = xstrdup(sendmail_binary);
9e42afcd 1405#endif
bb6ae3fb 1406 c->smtp_server = xstrdup("127.0.0.1");
d742bb47 1407 c->new_max = 100;
6207d2f3 1408 c->reminder_interval = 600; /* 10m */
05dcfac6 1409 c->new_bias_age = 7 * 86400; /* 1 week */
6151ae7e 1410 c->new_bias = 4500000; /* 50 times the base weight */
419893d7 1411 c->sox_generation = DEFAULT_SOX_GENERATION;
2563dc1f 1412 c->playlist_max = INT_MAX; /* effectively no limit */
ddbf05c8 1413 c->playlist_lock_timeout = 10; /* 10s */
8488cf7d 1414 c->mount_rescan = 1;
e6a35d1c 1415 /* Default stopwords */
86be0c30 1416 if(config_set(&cs, (int)NDEFAULT_STOPWORDS, (char **)default_stopwords))
1417 exit(1);
e6a35d1c
RK
1418 /* Default player configuration */
1419 for(n = 0; n < NDEFAULT_PLAYERS; ++n) {
1420 if(config_set_args(&cs, "player",
1421 default_players[n], "execraw", "disorder-decode", (char *)0))
1422 exit(1);
1423 if(config_set_args(&cs, "tracklength",
1424 default_players[n], "disorder-tracklength", (char *)0))
1425 exit(1);
1426 }
76e72f65
RK
1427 c->broadcast.af = -1;
1428 c->broadcast_from.af = -1;
80dc2c5f 1429 c->listen.af = -1;
e41a9999 1430 c->connect.af = -1;
b0116b5c 1431 c->rtp_mode = xstrdup("auto");
2a2b84aa 1432 c->rtp_max_payload = -1;
10511fad 1433 c->rtp_mtu_discovery = xstrdup("default");
460b9539 1434 return c;
1435}
1436
9e42afcd 1437#if !_WIN32
34d37b3e
RK
1438/** @brief Construct a filename
1439 * @param c Configuration
1440 * @param name Base filename
1441 * @return Full filename
1442 *
1443 * Usually use config_get_file() instead.
1444 */
319d7107 1445char *config_get_file2(struct config *c, const char *name) {
460b9539 1446 char *s;
1447
1448 byte_xasprintf(&s, "%s/%s", c->home, name);
1449 return s;
1450}
9e42afcd 1451#endif
460b9539 1452
3f3bb97b 1453/** @brief Set the default configuration file */
460b9539 1454static void set_configfile(void) {
9e42afcd 1455#if !_WIN32
460b9539 1456 if(!configfile)
1457 byte_xasprintf(&configfile, "%s/config", pkgconfdir);
9e42afcd 1458#endif
460b9539 1459}
1460
34d37b3e
RK
1461/** @brief Free a configuration object
1462 * @param c Configuration to free
1463 *
1464 * @p c is indeterminate after this function is called.
1465 */
47854c5f 1466void config_free(struct config *c) {
460b9539 1467 int n;
1468
1469 if(c) {
1470 for(n = 0; n < (int)(sizeof conf / sizeof *conf); ++n)
1471 conf[n].type->free(c, &conf[n]);
1472 for(n = 0; n < c->nparts; ++n)
1473 xfree(c->parts[n]);
1474 xfree(c->parts);
1475 xfree(c);
1476 }
1477}
1478
34d37b3e
RK
1479/** @brief Set post-parse defaults
1480 * @param c Configuration to update
1481 * @param server True when running in the server
1482 *
1483 * If @p server is set then certain parts of the configuration are more
1484 * strictly validated.
1485 */
c00fce3a
RK
1486static void config_postdefaults(struct config *c,
1487 int server) {
460b9539 1488 struct config_state cs;
1489 const struct conf *whoami;
1490 int n;
1491
1492 static const char *namepart[][4] = {
bcf50f5c 1493 { "title", "/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
460b9539 1494 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1495 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1496 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1497 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1498 };
1499#define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1500
1501 static const char *transform[][5] = {
bcf50f5c 1502 { "track", "^.*/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
460b9539 1503 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1504 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1505 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1506 { "dir", "[[:punct:]]", "", "sort", "g", }
1507 };
1508#define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1509
1510 cs.path = "<internal>";
1511 cs.line = 0;
1512 cs.config = c;
1513 if(!c->namepart.n) {
1514 whoami = find("namepart");
1515 for(n = 0; n < NNAMEPART; ++n)
1516 set_namepart(&cs, whoami, 4, (char **)namepart[n]);
1517 }
1518 if(!c->transform.n) {
1519 whoami = find("transform");
1520 for(n = 0; n < NTRANSFORM; ++n)
1521 set_transform(&cs, whoami, 5, (char **)transform[n]);
1522 }
b50cfb8a 1523 if(!c->api) {
e83d0967 1524 if(c->speaker_command)
b50cfb8a 1525 c->api = xstrdup("command");
76e72f65 1526 else if(c->broadcast.af != -1)
b50cfb8a 1527 c->api = xstrdup("rtp");
9e42afcd 1528#if !_WIN32
b50cfb8a 1529 else if(config_uaudio_apis)
06385470
RK
1530 c->api = xstrdup(uaudio_default(config_uaudio_apis,
1531 UAUDIO_API_SERVER)->name);
9e42afcd 1532#endif
8aae240b 1533 else
b50cfb8a 1534 c->api = xstrdup("<none>");
e83d0967 1535 }
b50cfb8a
RK
1536 if(!strcmp(c->api, "network"))
1537 c->api = xstrdup("rtp");
c00fce3a 1538 if(server) {
b50cfb8a 1539 if(!strcmp(c->api, "command") && !c->speaker_command)
2e9ba080 1540 disorder_fatal(0, "'api command' but speaker_command is not set");
3fe2333a
MW
1541 if((!strcmp(c->api, "rtp")) &&
1542 c->broadcast.af == -1 && strcmp(c->rtp_mode, "request"))
1543 disorder_fatal(0, "'api rtp' but broadcast is not set "
1544 "and mode is not not 'request'");
c00fce3a 1545 }
e99d42b1 1546 /* Override sample format */
b50cfb8a 1547 if(!strcmp(c->api, "rtp")) {
6d2d327c
RK
1548 c->sample_format.rate = 44100;
1549 c->sample_format.channels = 2;
1550 c->sample_format.bits = 16;
b50cfb8a
RK
1551 c->sample_format.endian = ENDIAN_NATIVE;
1552 }
1553 if(!strcmp(c->api, "coreaudio")) {
937be4c0
RK
1554 c->sample_format.rate = 44100;
1555 c->sample_format.channels = 2;
1556 c->sample_format.bits = 16;
1557 c->sample_format.endian = ENDIAN_NATIVE;
04e1fa7c
RK
1558 }
1559 if(!c->default_rights) {
1560 rights_type r = RIGHTS__MASK & ~(RIGHT_ADMIN|RIGHT_REGISTER
1561 |RIGHT_MOVE__MASK
1562 |RIGHT_SCRATCH__MASK
1563 |RIGHT_REMOVE__MASK);
657fdb79 1564 r |= RIGHT_SCRATCH_ANY|RIGHT_MOVE_ANY|RIGHT_REMOVE_ANY;
0f55e905 1565 c->default_rights = rights_string(r);
04e1fa7c 1566 }
460b9539 1567}
1568
c00fce3a
RK
1569/** @brief (Re-)read the config file
1570 * @param server If set, do extra checking
02ba7921
RK
1571 * @param oldconfig Old configuration for compatibility check
1572 * @return 0 on success, non-0 on error
1573 *
1574 * If @p oldconfig is set, then certain compatibility checks are done between
1575 * the old and new configurations.
c00fce3a 1576 */
02ba7921
RK
1577int config_read(int server,
1578 const struct config *oldconfig) {
460b9539 1579 struct config *c;
1580 char *privconf;
9e42afcd 1581 struct passwd *pw = NULL;
460b9539 1582
1583 set_configfile();
1584 c = config_default();
9e42afcd
RK
1585 /* standalone client installs might not have a global config file */
1586 if(configfile)
1587 if(access(configfile, F_OK) == 0)
1588 if(config_include(c, configfile))
1589 return -1;
460b9539 1590 /* if we can read the private config file, do */
1591 if((privconf = config_private())
1592 && access(privconf, R_OK) == 0
1593 && config_include(c, privconf))
1594 return -1;
1595 xfree(privconf);
1596 /* if there's a per-user system config file for this user, read it */
63ad732f 1597 if(config_per_user) {
9e42afcd 1598#if !_WIN32
63ad732f 1599 if(!(pw = getpwuid(getuid())))
2e9ba080 1600 disorder_fatal(0, "cannot determine our username");
63ad732f
RK
1601 if((privconf = config_usersysconf(pw))
1602 && access(privconf, F_OK) == 0
1603 && config_include(c, privconf))
460b9539 1604 return -1;
63ad732f 1605 xfree(privconf);
9e42afcd 1606#endif
63ad732f 1607 /* if we have a password file, read it */
5b14453f 1608 if((privconf = config_userconf(0, pw))
63ad732f
RK
1609 && access(privconf, F_OK) == 0
1610 && config_include(c, privconf))
1611 return -1;
1612 xfree(privconf);
1613 }
460b9539 1614 /* install default namepart and transform settings */
c00fce3a 1615 config_postdefaults(c, server);
02ba7921
RK
1616 if(oldconfig) {
1617 int failed = 0;
9e42afcd 1618#if !_WIN32
02ba7921 1619 if(strcmp(c->home, oldconfig->home)) {
2e9ba080 1620 disorder_error(0, "'home' cannot be changed without a restart");
02ba7921
RK
1621 failed = 1;
1622 }
9e42afcd 1623#endif
02ba7921 1624 if(strcmp(c->alias, oldconfig->alias)) {
2e9ba080 1625 disorder_error(0, "'alias' cannot be changed without a restart");
02ba7921
RK
1626 failed = 1;
1627 }
1628 if(strcmp(c->user, oldconfig->user)) {
2e9ba080 1629 disorder_error(0, "'user' cannot be changed without a restart");
02ba7921
RK
1630 failed = 1;
1631 }
1632 if(c->nice_speaker != oldconfig->nice_speaker) {
2e9ba080 1633 disorder_error(0, "'nice_speaker' cannot be changed without a restart");
02ba7921
RK
1634 /* ...but we accept the new config anyway */
1635 }
553d8115 1636 if(c->nice_server != oldconfig->nice_server) {
2e9ba080 1637 disorder_error(0, "'nice_server' cannot be changed without a restart");
553d8115
RK
1638 /* ...but we accept the new config anyway */
1639 }
9417e1a3 1640 if(namepartlist_compare(&c->namepart, &oldconfig->namepart)) {
2e9ba080 1641 disorder_error(0, "'namepart' settings cannot be changed without a restart");
9417e1a3
RK
1642 failed = 1;
1643 }
1644 if(stringlist_compare(&c->stopword, &oldconfig->stopword)) {
2e9ba080 1645 disorder_error(0, "'stopword' settings cannot be changed without a restart");
9417e1a3
RK
1646 failed = 1;
1647 }
02ba7921 1648 if(failed) {
2e9ba080 1649 disorder_error(0, "not installing incompatible new configuration");
02ba7921
RK
1650 return -1;
1651 }
1652 }
460b9539 1653 /* everything is good so we shall use the new config */
1654 config_free(config);
04e1fa7c 1655 /* warn about obsolete directives */
460b9539 1656 config = c;
1657 return 0;
1658}
1659
3f3bb97b 1660/** @brief Return the path to the private configuration file */
460b9539 1661char *config_private(void) {
9e42afcd
RK
1662#if _WIN32
1663 return NULL;
1664#else
460b9539 1665 char *s;
1666
1667 set_configfile();
1668 byte_xasprintf(&s, "%s.private", configfile);
1669 return s;
9e42afcd 1670#endif
460b9539 1671}
1672
3f3bb97b 1673/** @brief Return the path to user's personal configuration file */
460b9539 1674char *config_userconf(const char *home, const struct passwd *pw) {
1675 char *s;
9e42afcd
RK
1676#if _WIN32
1677 wchar_t *wpath = 0;
1678 char *appdata;
1679 if(SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, NULL, &wpath) != S_OK)
1680 disorder_fatal(0, "error calling SHGetKnownFolderPath");
1681 appdata = win_wtomb(wpath);
1682 CoTaskMemFree(wpath);
1683 byte_xasprintf(&s, "%s\\DisOrder\\passwd", appdata);
1684#else
73f1b9f3 1685 if(!home && !pw && !(pw = getpwuid(getuid())))
2e9ba080 1686 disorder_fatal(0, "cannot determine our username");
460b9539 1687 byte_xasprintf(&s, "%s/.disorder/passwd", home ? home : pw->pw_dir);
9e42afcd 1688#endif
460b9539 1689 return s;
1690}
1691
9e42afcd 1692#if !_WIN32
3f3bb97b
RK
1693/** @brief Return the path to user-specific system configuration */
1694char *config_usersysconf(const struct passwd *pw) {
460b9539 1695 char *s;
1696
1697 set_configfile();
1698 if(!strchr(pw->pw_name, '/')) {
1699 byte_xasprintf(&s, "%s.%s", configfile, pw->pw_name);
1700 return s;
1701 } else
1702 return 0;
1703}
1704
34d37b3e
RK
1705/** @brief Get a filename within the home directory
1706 * @param name Relative name
1707 * @return Full path
1708 */
460b9539 1709char *config_get_file(const char *name) {
319d7107 1710 return config_get_file2(config, name);
460b9539 1711}
9e42afcd 1712#endif
460b9539 1713
34d37b3e
RK
1714/** @brief Order two stringlists
1715 * @param a First stringlist
1716 * @param b Second stringlist
1717 * @return <0, 0 or >0 if a<b, a=b or a>b
1718 */
9417e1a3
RK
1719static int stringlist_compare(const struct stringlist *a,
1720 const struct stringlist *b) {
e4132fd0 1721 int n = 0, c;
9417e1a3
RK
1722
1723 while(n < a->n && n < b->n) {
1724 if((c = strcmp(a->s[n], b->s[n])))
1725 return c;
1726 ++n;
1727 }
1728 if(a->n < b->n)
1729 return -1;
1730 else if(a->n > b->n)
1731 return 1;
1732 else
1733 return 0;
1734}
1735
34d37b3e
RK
1736/** @brief Order two namepart definitions
1737 * @param a First namepart definition
1738 * @param b Second namepart definition
1739 * @return <0, 0 or >0 if a<b, a=b or a>b
1740 */
9417e1a3
RK
1741static int namepart_compare(const struct namepart *a,
1742 const struct namepart *b) {
1743 int c;
1744
1745 if((c = strcmp(a->part, b->part)))
1746 return c;
1747 if((c = strcmp(a->res, b->res)))
1748 return c;
1749 if((c = strcmp(a->replace, b->replace)))
1750 return c;
1751 if((c = strcmp(a->context, b->context)))
1752 return c;
1753 if(a->reflags > b->reflags)
1754 return 1;
1755 if(a->reflags < b->reflags)
1756 return -1;
1757 return 0;
1758}
1759
34d37b3e
RK
1760/** @brief Order two lists of namepart definitions
1761 * @param a First list of namepart definitions
1762 * @param b Second list of namepart definitions
1763 * @return <0, 0 or >0 if a<b, a=b or a>b
1764 */
9417e1a3
RK
1765static int namepartlist_compare(const struct namepartlist *a,
1766 const struct namepartlist *b) {
e4132fd0 1767 int n = 0, c;
9417e1a3
RK
1768
1769 while(n < a->n && n < b->n) {
1770 if((c = namepart_compare(&a->s[n], &b->s[n])))
1771 return c;
1772 ++n;
1773 }
1774 if(a->n > b->n)
1775 return 1;
1776 else if(a->n < b->n)
1777 return -1;
1778 else
1779 return 0;
1780}
1781
2a1c84fb
RK
1782/** @brief Verify configuration table.
1783 * @return The number of problems found
1784*/
1785int config_verify(void) {
1786 int fails = 0;
05b3f1f6
RK
1787 size_t n;
1788 for(n = 1; n < sizeof conf / sizeof *conf; ++n)
2a1c84fb
RK
1789 if(strcmp(conf[n-1].name, conf[n].name) >= 0) {
1790 fprintf(stderr, "%s >= %s\n", conf[n-1].name, conf[n].name);
1791 ++fails;
1792 }
1793 return fails;
1794}
1795
460b9539 1796/*
1797Local Variables:
1798c-basic-offset:2
1799comment-column:40
1800fill-column:79
1801End:
1802*/