chiark / gitweb /
typo
[disorder] / lib / configuration.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
e83d0967 3 * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
460b9539 4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
0e4472a0 20/** @file lib/configuration.c
21 * @brief Configuration file support
22 */
460b9539 23
24#include <config.h>
25#include "types.h"
26
27#include <stdio.h>
28#include <string.h>
29#include <stdlib.h>
30#include <errno.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <unistd.h>
34#include <ctype.h>
35#include <stddef.h>
36#include <pwd.h>
37#include <langinfo.h>
38#include <pcre.h>
39#include <signal.h>
40
41#include "configuration.h"
42#include "mem.h"
43#include "log.h"
44#include "split.h"
45#include "syscalls.h"
46#include "table.h"
47#include "inputline.h"
48#include "charset.h"
49#include "defs.h"
50#include "mixer.h"
51#include "printf.h"
52#include "regsub.h"
53#include "signame.h"
637fdea3 54#include "authhash.h"
460b9539 55
3f3bb97b
RK
56/** @brief Path to config file
57 *
58 * set_configfile() sets the deafult if it is null.
59 */
460b9539 60char *configfile;
61
3f3bb97b 62/** @brief Config file parser state */
460b9539 63struct config_state {
3f3bb97b 64 /** @brief Filename */
460b9539 65 const char *path;
3f3bb97b 66 /** @brief Line number */
460b9539 67 int line;
3f3bb97b 68 /** @brief Configuration object under construction */
460b9539 69 struct config *config;
70};
71
3f3bb97b 72/** @brief Current configuration */
460b9539 73struct config *config;
74
3f3bb97b 75/** @brief One configuration item */
460b9539 76struct conf {
3f3bb97b 77 /** @brief Name as it appears in the config file */
460b9539 78 const char *name;
3f3bb97b 79 /** @brief Offset in @ref config structure */
460b9539 80 size_t offset;
3f3bb97b 81 /** @brief Pointer to item type */
460b9539 82 const struct conftype *type;
3f3bb97b 83 /** @brief Pointer to item-specific validation routine */
460b9539 84 int (*validate)(const struct config_state *cs,
85 int nvec, char **vec);
86};
87
3f3bb97b 88/** @brief Type of a configuration item */
460b9539 89struct conftype {
3f3bb97b 90 /** @brief Pointer to function to set item */
460b9539 91 int (*set)(const struct config_state *cs,
92 const struct conf *whoami,
93 int nvec, char **vec);
3f3bb97b 94 /** @brief Pointer to function to free item */
460b9539 95 void (*free)(struct config *c, const struct conf *whoami);
96};
97
3f3bb97b 98/** @brief Compute the address of an item */
460b9539 99#define ADDRESS(C, TYPE) ((TYPE *)((char *)(C) + whoami->offset))
3f3bb97b 100/** @brief Return the value of an item */
460b9539 101#define VALUE(C, TYPE) (*ADDRESS(C, TYPE))
102
103static int set_signal(const struct config_state *cs,
104 const struct conf *whoami,
105 int nvec, char **vec) {
106 int n;
107
108 if(nvec != 1) {
109 error(0, "%s:%d: '%s' requires one argument",
110 cs->path, cs->line, whoami->name);
111 return -1;
112 }
113 if((n = find_signal(vec[0])) == -1) {
114 error(0, "%s:%d: unknown signal '%s'",
115 cs->path, cs->line, vec[0]);
116 return -1;
117 }
118 VALUE(cs->config, int) = n;
119 return 0;
120}
121
122static int set_collections(const struct config_state *cs,
123 const struct conf *whoami,
124 int nvec, char **vec) {
125 struct collectionlist *cl;
126
127 if(nvec != 3) {
128 error(0, "%s:%d: '%s' requires three arguments",
129 cs->path, cs->line, whoami->name);
130 return -1;
131 }
132 if(vec[2][0] != '/') {
133 error(0, "%s:%d: collection root must start with '/'",
134 cs->path, cs->line);
135 return -1;
136 }
137 if(vec[2][1] && vec[2][strlen(vec[2])-1] == '/') {
138 error(0, "%s:%d: collection root must not end with '/'",
139 cs->path, cs->line);
140 return -1;
141 }
142 cl = ADDRESS(cs->config, struct collectionlist);
143 ++cl->n;
144 cl->s = xrealloc(cl->s, cl->n * sizeof (struct collection));
145 cl->s[cl->n - 1].module = xstrdup(vec[0]);
146 cl->s[cl->n - 1].encoding = xstrdup(vec[1]);
147 cl->s[cl->n - 1].root = xstrdup(vec[2]);
148 return 0;
149}
150
151static int set_boolean(const struct config_state *cs,
152 const struct conf *whoami,
153 int nvec, char **vec) {
154 int state;
155
156 if(nvec != 1) {
157 error(0, "%s:%d: '%s' takes only one argument",
158 cs->path, cs->line, whoami->name);
159 return -1;
160 }
161 if(!strcmp(vec[0], "yes")) state = 1;
162 else if(!strcmp(vec[0], "no")) state = 0;
163 else {
164 error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'",
165 cs->path, cs->line, whoami->name);
166 return -1;
167 }
168 VALUE(cs->config, int) = state;
169 return 0;
170}
171
172static int set_string(const struct config_state *cs,
173 const struct conf *whoami,
174 int nvec, char **vec) {
175 if(nvec != 1) {
176 error(0, "%s:%d: '%s' takes only one argument",
177 cs->path, cs->line, whoami->name);
178 return -1;
179 }
180 VALUE(cs->config, char *) = xstrdup(vec[0]);
181 return 0;
182}
183
184static int set_stringlist(const struct config_state *cs,
185 const struct conf *whoami,
186 int nvec, char **vec) {
187 int n;
188 struct stringlist *sl;
189
190 sl = ADDRESS(cs->config, struct stringlist);
191 sl->n = 0;
192 for(n = 0; n < nvec; ++n) {
193 sl->n++;
194 sl->s = xrealloc(sl->s, (sl->n * sizeof (char *)));
195 sl->s[sl->n - 1] = xstrdup(vec[n]);
196 }
197 return 0;
198}
199
200static int set_integer(const struct config_state *cs,
201 const struct conf *whoami,
202 int nvec, char **vec) {
203 char *e;
204
205 if(nvec != 1) {
206 error(0, "%s:%d: '%s' takes only one argument",
207 cs->path, cs->line, whoami->name);
208 return -1;
209 }
210 if(xstrtol(ADDRESS(cs->config, long), vec[0], &e, 0)) {
211 error(errno, "%s:%d: converting integer", cs->path, cs->line);
212 return -1;
213 }
214 if(*e) {
215 error(0, "%s:%d: invalid integer syntax", cs->path, cs->line);
216 return -1;
217 }
218 return 0;
219}
220
221static int set_stringlist_accum(const struct config_state *cs,
222 const struct conf *whoami,
223 int nvec, char **vec) {
224 int n;
225 struct stringlist *s;
226 struct stringlistlist *sll;
227
228 sll = ADDRESS(cs->config, struct stringlistlist);
229 sll->n++;
230 sll->s = xrealloc(sll->s, (sll->n * sizeof (struct stringlist)));
231 s = &sll->s[sll->n - 1];
232 s->n = nvec;
233 s->s = xmalloc((nvec + 1) * sizeof (char *));
234 for(n = 0; n < nvec; ++n)
235 s->s[n] = xstrdup(vec[n]);
236 return 0;
237}
238
239static int set_string_accum(const struct config_state *cs,
240 const struct conf *whoami,
241 int nvec, char **vec) {
242 int n;
243 struct stringlist *sl;
244
245 sl = ADDRESS(cs->config, struct stringlist);
246 for(n = 0; n < nvec; ++n) {
247 sl->n++;
248 sl->s = xrealloc(sl->s, (sl->n * sizeof (char *)));
249 sl->s[sl->n - 1] = xstrdup(vec[n]);
250 }
251 return 0;
252}
253
254static int set_restrict(const struct config_state *cs,
255 const struct conf *whoami,
256 int nvec, char **vec) {
257 unsigned r = 0;
258 int n, i;
259
260 static const struct restriction {
261 const char *name;
262 unsigned bit;
263 } restrictions[] = {
264 { "remove", RESTRICT_REMOVE },
265 { "scratch", RESTRICT_SCRATCH },
266 { "move", RESTRICT_MOVE },
267 };
268
269 for(n = 0; n < nvec; ++n) {
270 if((i = TABLE_FIND(restrictions, struct restriction, name, vec[n])) < 0) {
271 error(0, "%s:%d: invalid restriction '%s'",
272 cs->path, cs->line, vec[n]);
273 return -1;
274 }
275 r |= restrictions[i].bit;
276 }
277 VALUE(cs->config, unsigned) = r;
278 return 0;
279}
280
9d5da576 281static int parse_sample_format(const struct config_state *cs,
6d2d327c 282 struct stream_header *format,
9d5da576 283 int nvec, char **vec) {
284 char *p = vec[0];
285 long t;
286
6d2d327c 287 if(nvec != 1) {
9d5da576 288 error(0, "%s:%d: wrong number of arguments", cs->path, cs->line);
289 return -1;
290 }
6d2d327c 291 if(xstrtol(&t, p, &p, 0)) {
9d5da576 292 error(errno, "%s:%d: converting bits-per-sample", cs->path, cs->line);
293 return -1;
294 }
6d2d327c 295 if(t != 8 && t != 16) {
9d5da576 296 error(0, "%s:%d: bad bite-per-sample (%ld)", cs->path, cs->line, t);
297 return -1;
298 }
6d2d327c 299 if(format) format->bits = t;
9d5da576 300 switch (*p) {
6d2d327c
RK
301 case 'l': case 'L': t = ENDIAN_LITTLE; p++; break;
302 case 'b': case 'B': t = ENDIAN_BIG; p++; break;
303 default: t = ENDIAN_NATIVE; break;
9d5da576 304 }
6d2d327c
RK
305 if(format) format->endian = t;
306 if(*p != '/') {
9d5da576 307 error(errno, "%s:%d: expected `/' after bits-per-sample",
308 cs->path, cs->line);
309 return -1;
310 }
311 p++;
6d2d327c 312 if(xstrtol(&t, p, &p, 0)) {
9d5da576 313 error(errno, "%s:%d: converting sample-rate", cs->path, cs->line);
314 return -1;
315 }
6d2d327c 316 if(t < 1 || t > INT_MAX) {
9d5da576 317 error(0, "%s:%d: silly sample-rate (%ld)", cs->path, cs->line, t);
318 return -1;
319 }
6d2d327c
RK
320 if(format) format->rate = t;
321 if(*p != '/') {
9d5da576 322 error(0, "%s:%d: expected `/' after sample-rate",
323 cs->path, cs->line);
324 return -1;
325 }
326 p++;
6d2d327c 327 if(xstrtol(&t, p, &p, 0)) {
9d5da576 328 error(errno, "%s:%d: converting channels", cs->path, cs->line);
329 return -1;
330 }
6d2d327c 331 if(t < 1 || t > 8) {
9d5da576 332 error(0, "%s:%d: silly number (%ld) of channels", cs->path, cs->line, t);
333 return -1;
334 }
6d2d327c
RK
335 if(format) format->channels = t;
336 if(*p) {
9d5da576 337 error(0, "%s:%d: junk after channels", cs->path, cs->line);
338 return -1;
339 }
340 return 0;
341}
342
343static int set_sample_format(const struct config_state *cs,
344 const struct conf *whoami,
345 int nvec, char **vec) {
6d2d327c 346 return parse_sample_format(cs, ADDRESS(cs->config, struct stream_header),
9d5da576 347 nvec, vec);
348}
349
460b9539 350static int set_namepart(const struct config_state *cs,
351 const struct conf *whoami,
352 int nvec, char **vec) {
353 struct namepartlist *npl = ADDRESS(cs->config, struct namepartlist);
354 unsigned reflags;
355 const char *errstr;
356 int erroffset, n;
357 pcre *re;
358
359 if(nvec < 3) {
360 error(0, "%s:%d: namepart needs at least 3 arguments", cs->path, cs->line);
361 return -1;
362 }
363 if(nvec > 5) {
364 error(0, "%s:%d: namepart needs at most 5 arguments", cs->path, cs->line);
365 return -1;
366 }
367 reflags = nvec >= 5 ? regsub_flags(vec[4]) : 0;
368 if(!(re = pcre_compile(vec[1],
369 PCRE_UTF8
370 |regsub_compile_options(reflags),
371 &errstr, &erroffset, 0))) {
372 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
373 cs->path, cs->line, vec[1], errstr, erroffset);
374 return -1;
375 }
376 npl->s = xrealloc(npl->s, (npl->n + 1) * sizeof (struct namepart));
377 npl->s[npl->n].part = xstrdup(vec[0]);
378 npl->s[npl->n].re = re;
379 npl->s[npl->n].replace = xstrdup(vec[2]);
380 npl->s[npl->n].context = xstrdup(vec[3]);
381 npl->s[npl->n].reflags = reflags;
382 ++npl->n;
383 /* XXX a bit of a bodge; relies on there being very few parts. */
384 for(n = 0; (n < cs->config->nparts
385 && strcmp(cs->config->parts[n], vec[0])); ++n)
386 ;
387 if(n >= cs->config->nparts) {
388 cs->config->parts = xrealloc(cs->config->parts,
389 (cs->config->nparts + 1) * sizeof (char *));
390 cs->config->parts[cs->config->nparts++] = xstrdup(vec[0]);
391 }
392 return 0;
393}
394
395static int set_transform(const struct config_state *cs,
396 const struct conf *whoami,
397 int nvec, char **vec) {
398 struct transformlist *tl = ADDRESS(cs->config, struct transformlist);
399 pcre *re;
400 unsigned reflags;
401 const char *errstr;
402 int erroffset;
403
404 if(nvec < 3) {
405 error(0, "%s:%d: transform needs at least 3 arguments", cs->path, cs->line);
406 return -1;
407 }
408 if(nvec > 5) {
409 error(0, "%s:%d: transform needs at most 5 arguments", cs->path, cs->line);
410 return -1;
411 }
412 reflags = (nvec >= 5 ? regsub_flags(vec[4]) : 0);
413 if(!(re = pcre_compile(vec[1],
414 PCRE_UTF8
415 |regsub_compile_options(reflags),
416 &errstr, &erroffset, 0))) {
417 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
418 cs->path, cs->line, vec[1], errstr, erroffset);
419 return -1;
420 }
421 tl->t = xrealloc(tl->t, (tl->n + 1) * sizeof (struct namepart));
422 tl->t[tl->n].type = xstrdup(vec[0]);
423 tl->t[tl->n].context = xstrdup(vec[3] ? vec[3] : "*");
424 tl->t[tl->n].re = re;
425 tl->t[tl->n].replace = xstrdup(vec[2]);
426 tl->t[tl->n].flags = reflags;
427 ++tl->n;
428 return 0;
429}
430
e83d0967
RK
431static int set_backend(const struct config_state *cs,
432 const struct conf *whoami,
433 int nvec, char **vec) {
434 int *const valuep = ADDRESS(cs->config, int);
435
436 if(nvec != 1) {
437 error(0, "%s:%d: '%s' requires one argument",
438 cs->path, cs->line, whoami->name);
439 return -1;
440 }
441 if(!strcmp(vec[0], "alsa")) {
442#if API_ALSA
443 *valuep = BACKEND_ALSA;
444#else
445 error(0, "%s:%d: ALSA is not available on this platform",
446 cs->path, cs->line);
447 return -1;
448#endif
449 } else if(!strcmp(vec[0], "command"))
450 *valuep = BACKEND_COMMAND;
451 else if(!strcmp(vec[0], "network"))
452 *valuep = BACKEND_NETWORK;
453 else {
454 error(0, "%s:%d: invalid '%s' value '%s'",
455 cs->path, cs->line, whoami->name, vec[0]);
456 return -1;
457 }
458 return 0;
459}
460
460b9539 461/* free functions */
462
463static void free_none(struct config attribute((unused)) *c,
464 const struct conf attribute((unused)) *whoami) {
465}
466
467static void free_string(struct config *c,
468 const struct conf *whoami) {
469 xfree(VALUE(c, char *));
470}
471
472static void free_stringlist(struct config *c,
473 const struct conf *whoami) {
474 int n;
475 struct stringlist *sl = ADDRESS(c, struct stringlist);
476
477 for(n = 0; n < sl->n; ++n)
478 xfree(sl->s[n]);
479 xfree(sl->s);
480}
481
482static void free_stringlistlist(struct config *c,
483 const struct conf *whoami) {
484 int n, m;
485 struct stringlistlist *sll = ADDRESS(c, struct stringlistlist);
486 struct stringlist *sl;
487
488 for(n = 0; n < sll->n; ++n) {
489 sl = &sll->s[n];
490 for(m = 0; m < sl->n; ++m)
491 xfree(sl->s[m]);
492 xfree(sl->s);
493 }
494 xfree(sll->s);
495}
496
497static void free_collectionlist(struct config *c,
498 const struct conf *whoami) {
499 struct collectionlist *cll = ADDRESS(c, struct collectionlist);
500 struct collection *cl;
501 int n;
502
503 for(n = 0; n < cll->n; ++n) {
504 cl = &cll->s[n];
505 xfree(cl->module);
506 xfree(cl->encoding);
507 xfree(cl->root);
508 }
509 xfree(cll->s);
510}
511
512static void free_namepartlist(struct config *c,
513 const struct conf *whoami) {
514 struct namepartlist *npl = ADDRESS(c, struct namepartlist);
515 struct namepart *np;
516 int n;
517
518 for(n = 0; n < npl->n; ++n) {
519 np = &npl->s[n];
520 xfree(np->part);
521 pcre_free(np->re); /* ...whatever pcre_free is set to. */
522 xfree(np->replace);
523 xfree(np->context);
524 }
525 xfree(npl->s);
526}
527
528static void free_transformlist(struct config *c,
529 const struct conf *whoami) {
530 struct transformlist *tl = ADDRESS(c, struct transformlist);
531 struct transform *t;
532 int n;
533
534 for(n = 0; n < tl->n; ++n) {
535 t = &tl->t[n];
536 xfree(t->type);
537 pcre_free(t->re); /* ...whatever pcre_free is set to. */
538 xfree(t->replace);
539 xfree(t->context);
540 }
541 xfree(tl->t);
542}
543
544/* configuration types */
545
546static const struct conftype
547 type_signal = { set_signal, free_none },
548 type_collections = { set_collections, free_collectionlist },
549 type_boolean = { set_boolean, free_none },
550 type_string = { set_string, free_string },
551 type_stringlist = { set_stringlist, free_stringlist },
552 type_integer = { set_integer, free_none },
553 type_stringlist_accum = { set_stringlist_accum, free_stringlistlist },
554 type_string_accum = { set_string_accum, free_stringlist },
9d5da576 555 type_sample_format = { set_sample_format, free_none },
460b9539 556 type_restrict = { set_restrict, free_none },
557 type_namepart = { set_namepart, free_namepartlist },
e83d0967
RK
558 type_transform = { set_transform, free_transformlist },
559 type_backend = { set_backend, free_none };
460b9539 560
561/* specific validation routine */
562
563#define VALIDATE_FILE(test, what) do { \
564 struct stat sb; \
565 int n; \
566 \
567 for(n = 0; n < nvec; ++n) { \
568 if(stat(vec[n], &sb) < 0) { \
569 error(errno, "%s:%d: %s", cs->path, cs->line, vec[n]); \
570 return -1; \
571 } \
572 if(!test(sb.st_mode)) { \
573 error(0, "%s:%d: %s is not a %s", \
574 cs->path, cs->line, vec[n], what); \
575 return -1; \
576 } \
577 } \
578} while(0)
579
580static int validate_isdir(const struct config_state *cs,
581 int nvec, char **vec) {
582 VALIDATE_FILE(S_ISDIR, "directory");
583 return 0;
584}
585
586static int validate_isreg(const struct config_state *cs,
587 int nvec, char **vec) {
588 VALIDATE_FILE(S_ISREG, "regular file");
589 return 0;
590}
591
592static int validate_ischr(const struct config_state *cs,
593 int nvec, char **vec) {
594 VALIDATE_FILE(S_ISCHR, "character device");
595 return 0;
596}
597
598static int validate_player(const struct config_state *cs,
599 int nvec,
600 char attribute((unused)) **vec) {
601 if(nvec < 2) {
602 error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
603 cs->path, cs->line);
604 return -1;
605 }
606 return 0;
607}
608
609static int validate_allow(const struct config_state *cs,
610 int nvec,
611 char attribute((unused)) **vec) {
612 if(nvec != 2) {
613 error(0, "%s:%d: must be 'allow NAME PASS'", cs->path, cs->line);
614 return -1;
615 }
616 return 0;
617}
618
619static int validate_non_negative(const struct config_state *cs,
620 int nvec, char **vec) {
621 long n;
622
623 if(nvec < 1) {
624 error(0, "%s:%d: missing argument", cs->path, cs->line);
625 return -1;
626 }
627 if(nvec > 1) {
628 error(0, "%s:%d: too many arguments", cs->path, cs->line);
629 return -1;
630 }
631 if(xstrtol(&n, vec[0], 0, 0)) {
632 error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno));
633 return -1;
634 }
635 if(n < 0) {
636 error(0, "%s:%d: must not be negative", cs->path, cs->line);
637 return -1;
638 }
639 return 0;
640}
641
642static int validate_positive(const struct config_state *cs,
643 int nvec, char **vec) {
644 long n;
645
646 if(nvec < 1) {
647 error(0, "%s:%d: missing argument", cs->path, cs->line);
648 return -1;
649 }
650 if(nvec > 1) {
651 error(0, "%s:%d: too many arguments", cs->path, cs->line);
652 return -1;
653 }
654 if(xstrtol(&n, vec[0], 0, 0)) {
655 error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno));
656 return -1;
657 }
658 if(n <= 0) {
659 error(0, "%s:%d: must be positive", cs->path, cs->line);
660 return -1;
661 }
662 return 0;
663}
664
665static int validate_isauser(const struct config_state *cs,
666 int attribute((unused)) nvec,
667 char **vec) {
668 struct passwd *pw;
669
670 if(!(pw = getpwnam(vec[0]))) {
671 error(0, "%s:%d: no such user as '%s'", cs->path, cs->line, vec[0]);
672 return -1;
673 }
674 return 0;
675}
676
9d5da576 677static int validate_sample_format(const struct config_state *cs,
678 int attribute((unused)) nvec,
679 char **vec) {
680 return parse_sample_format(cs, 0, nvec, vec);
681}
682
460b9539 683static int validate_channel(const struct config_state *cs,
684 int attribute((unused)) nvec,
685 char **vec) {
686 if(mixer_channel(vec[0]) == -1) {
687 error(0, "%s:%d: invalid channel '%s'", cs->path, cs->line, vec[0]);
688 return -1;
689 }
690 return 0;
691}
692
693static int validate_any(const struct config_state attribute((unused)) *cs,
694 int attribute((unused)) nvec,
695 char attribute((unused)) **vec) {
696 return 0;
697}
698
699static int validate_url(const struct config_state attribute((unused)) *cs,
700 int attribute((unused)) nvec,
701 char **vec) {
702 const char *s;
703 int n;
704 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
705 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
706 s = vec[0];
707 n = strspn(s, ("abcdefghijklmnopqrstuvwxyz"
708 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
709 "0123456789"));
710 if(s[n] != ':') {
711 error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
712 return -1;
713 }
714 if(!strncmp(s, "http:", 5)
715 || !strncmp(s, "https:", 6)) {
716 s += n + 1;
717 /* we only do a rather cursory check */
718 if(strncmp(s, "//", 2)) {
719 error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
720 return -1;
721 }
722 }
723 return 0;
724}
725
726static int validate_alias(const struct config_state *cs,
727 int nvec,
728 char **vec) {
729 const char *s;
730 int in_brackets = 0, c;
731
732 if(nvec < 1) {
733 error(0, "%s:%d: missing argument", cs->path, cs->line);
734 return -1;
735 }
736 if(nvec > 1) {
737 error(0, "%s:%d: too many arguments", cs->path, cs->line);
738 return -1;
739 }
740 s = vec[0];
741 while((c = (unsigned char)*s++)) {
742 if(in_brackets) {
743 if(c == '}')
744 in_brackets = 0;
745 else if(!isalnum(c)) {
746 error(0, "%s:%d: invalid part name in alias expansion in '%s'",
747 cs->path, cs->line, vec[0]);
748 return -1;
749 }
750 } else {
751 if(c == '{') {
752 in_brackets = 1;
753 if(*s == '/')
754 ++s;
755 } else if(c == '\\') {
756 if(!(c = (unsigned char)*s++)) {
757 error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
758 cs->path, cs->line, vec[0]);
759 return -1;
760 } else if(c != '\\' && c != '{') {
761 error(0, "%s:%d: invalid escape in alias expansion in '%s'",
762 cs->path, cs->line, vec[0]);
763 return -1;
764 }
765 }
766 }
767 ++s;
768 }
769 if(in_brackets) {
770 error(0, "%s:%d: unterminated part name in alias expansion in '%s'",
771 cs->path, cs->line, vec[0]);
772 return -1;
773 }
774 return 0;
775}
776
e83d0967
RK
777static int validate_addrport(const struct config_state attribute((unused)) *cs,
778 int nvec,
779 char attribute((unused)) **vec) {
780 switch(nvec) {
781 case 0:
782 error(0, "%s:%d: missing address",
783 cs->path, cs->line);
784 return -1;
785 case 1:
786 error(0, "%s:%d: missing port name/number",
787 cs->path, cs->line);
788 return -1;
789 case 2:
790 return 0;
791 default:
792 error(0, "%s:%d: expected ADDRESS PORT",
793 cs->path, cs->line);
794 return -1;
795 }
796}
797
ccf0aafa 798static int validate_port(const struct config_state attribute((unused)) *cs,
e83d0967
RK
799 int nvec,
800 char attribute((unused)) **vec) {
801 switch(nvec) {
802 case 0:
803 error(0, "%s:%d: missing address",
804 cs->path, cs->line);
805 return -1;
806 case 1:
807 case 2:
808 return 0;
809 default:
ccf0aafa 810 error(0, "%s:%d: expected [ADDRESS] PORT",
e83d0967
RK
811 cs->path, cs->line);
812 return -1;
813 }
814}
815
637fdea3
RK
816static int validate_algo(const struct config_state attribute((unused)) *cs,
817 int nvec,
818 char **vec) {
819 if(nvec != 1) {
820 error(0, "%s:%d: invalid algorithm specification", cs->path, cs->line);
821 return -1;
822 }
823 if(!valid_authhash(vec[0])) {
824 error(0, "%s:%d: unsuported algorithm '%s'", cs->path, cs->line, vec[0]);
825 return -1;
826 }
827 return 0;
828}
829
3f3bb97b 830/** @brief Item name and and offset */
460b9539 831#define C(x) #x, offsetof(struct config, x)
3f3bb97b 832/** @brief Item name and and offset */
460b9539 833#define C2(x,y) #x, offsetof(struct config, y)
834
3f3bb97b 835/** @brief All configuration items */
460b9539 836static const struct conf conf[] = {
837 { C(alias), &type_string, validate_alias },
838 { C(allow), &type_stringlist_accum, validate_allow },
637fdea3 839 { C(authorization_algorithm), &type_string, validate_algo },
e83d0967 840 { C(broadcast), &type_stringlist, validate_addrport },
ccf0aafa 841 { C(broadcast_from), &type_stringlist, validate_addrport },
460b9539 842 { C(channel), &type_string, validate_channel },
843 { C(checkpoint_kbyte), &type_integer, validate_non_negative },
844 { C(checkpoint_min), &type_integer, validate_non_negative },
845 { C(collection), &type_collections, validate_any },
e83d0967 846 { C(connect), &type_stringlist, validate_addrport },
460b9539 847 { C(device), &type_string, validate_any },
848 { C(gap), &type_integer, validate_non_negative },
849 { C(history), &type_integer, validate_positive },
850 { C(home), &type_string, validate_isdir },
ccf0aafa 851 { C(listen), &type_stringlist, validate_port },
460b9539 852 { C(lock), &type_boolean, validate_any },
853 { C(mixer), &type_string, validate_ischr },
23205f9c 854 { C(multicast_ttl), &type_integer, validate_non_negative },
460b9539 855 { C(namepart), &type_namepart, validate_any },
856 { C2(nice, nice_rescan), &type_integer, validate_non_negative },
857 { C(nice_rescan), &type_integer, validate_non_negative },
858 { C(nice_server), &type_integer, validate_any },
859 { C(nice_speaker), &type_integer, validate_any },
860 { C(password), &type_string, validate_any },
861 { C(player), &type_stringlist_accum, validate_player },
862 { C(plugins), &type_string_accum, validate_isdir },
863 { C(prefsync), &type_integer, validate_positive },
459d4402 864 { C(queue_pad), &type_integer, validate_positive },
460b9539 865 { C(refresh), &type_integer, validate_positive },
866 { C2(restrict, restrictions), &type_restrict, validate_any },
9d5da576 867 { C(sample_format), &type_sample_format, validate_sample_format },
460b9539 868 { C(scratch), &type_string_accum, validate_isreg },
869 { C(signal), &type_signal, validate_any },
5330d674 870 { C(sox_generation), &type_integer, validate_non_negative },
e83d0967 871 { C(speaker_backend), &type_backend, validate_any },
9d5da576 872 { C(speaker_command), &type_string, validate_any },
460b9539 873 { C(stopword), &type_string_accum, validate_any },
874 { C(templates), &type_string_accum, validate_isdir },
875 { C(transform), &type_transform, validate_any },
876 { C(trust), &type_string_accum, validate_any },
877 { C(url), &type_string, validate_url },
878 { C(user), &type_string, validate_isauser },
879 { C(username), &type_string, validate_any },
880};
881
3f3bb97b 882/** @brief Find a configuration item's definition by key */
460b9539 883static const struct conf *find(const char *key) {
884 int n;
885
886 if((n = TABLE_FIND(conf, struct conf, name, key)) < 0)
887 return 0;
888 return &conf[n];
889}
890
3f3bb97b 891/** @brief Set a new configuration value */
460b9539 892static int config_set(const struct config_state *cs,
893 int nvec, char **vec) {
894 const struct conf *which;
895
896 D(("config_set %s", vec[0]));
897 if(!(which = find(vec[0]))) {
898 error(0, "%s:%d: unknown configuration key '%s'",
899 cs->path, cs->line, vec[0]);
900 return -1;
901 }
902 return (which->validate(cs, nvec - 1, vec + 1)
903 || which->type->set(cs, which, nvec - 1, vec + 1));
904}
905
0e4472a0 906/** @brief Error callback used by config_include() */
460b9539 907static void config_error(const char *msg, void *u) {
908 const struct config_state *cs = u;
909
910 error(0, "%s:%d: %s", cs->path, cs->line, msg);
911}
912
3f3bb97b 913/** @brief Include a file by name */
460b9539 914static int config_include(struct config *c, const char *path) {
915 FILE *fp;
916 char *buffer, *inputbuffer, **vec;
917 int n, ret = 0;
918 struct config_state cs;
919
920 cs.path = path;
921 cs.line = 0;
922 cs.config = c;
923 D(("%s: reading configuration", path));
924 if(!(fp = fopen(path, "r"))) {
925 error(errno, "error opening %s", path);
926 return -1;
927 }
928 while(!inputline(path, fp, &inputbuffer, '\n')) {
929 ++cs.line;
930 if(!(buffer = mb2utf8(inputbuffer))) {
931 error(errno, "%s:%d: cannot convert to UTF-8", cs.path, cs.line);
932 ret = -1;
933 xfree(inputbuffer);
934 continue;
935 }
936 xfree(inputbuffer);
937 if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES,
938 config_error, &cs))) {
939 ret = -1;
940 xfree(buffer);
941 continue;
942 }
943 if(n) {
944 if(!strcmp(vec[0], "include")) {
945 if(n != 2) {
946 error(0, "%s:%d: must be 'include PATH'", cs.path, cs.line);
947 ret = -1;
948 } else
949 config_include(c, vec[1]);
950 } else
951 ret |= config_set(&cs, n, vec);
952 }
953 for(n = 0; vec[n]; ++n) xfree(vec[n]);
954 xfree(vec);
955 xfree(buffer);
956 }
957 if(ferror(fp)) {
958 error(errno, "error reading %s", path);
959 ret = -1;
960 }
961 fclose(fp);
962 return ret;
963}
964
3f3bb97b 965/** @brief Make a new default configuration */
460b9539 966static struct config *config_default(void) {
967 struct config *c = xmalloc(sizeof *c);
968 const char *logname;
969 struct passwd *pw;
970
971 /* Strings had better be xstrdup'd as they will get freed at some point. */
972 c->gap = 2;
973 c->history = 60;
974 c->home = xstrdup(pkgstatedir);
975 if(!(pw = getpwuid(getuid())))
976 fatal(0, "cannot determine our username");
977 logname = pw->pw_name;
978 c->username = xstrdup(logname);
979 c->refresh = 15;
980 c->prefsync = 3600;
981 c->signal = SIGKILL;
982 c->alias = xstrdup("{/artist}{/album}{/title}{ext}");
983 c->lock = 1;
984 c->device = xstrdup("default");
985 c->nice_rescan = 10;
9d5da576 986 c->speaker_command = 0;
987 c->sample_format.bits = 16;
988 c->sample_format.rate = 44100;
989 c->sample_format.channels = 2;
6d2d327c 990 c->sample_format.endian = ENDIAN_NATIVE;
459d4402 991 c->queue_pad = 10;
e83d0967 992 c->speaker_backend = -1;
23205f9c 993 c->multicast_ttl = 1;
637fdea3 994 c->authorization_algorithm = xstrdup("sha1");
460b9539 995 return c;
996}
997
998static char *get_file(struct config *c, const char *name) {
999 char *s;
1000
1001 byte_xasprintf(&s, "%s/%s", c->home, name);
1002 return s;
1003}
1004
3f3bb97b 1005/** @brief Set the default configuration file */
460b9539 1006static void set_configfile(void) {
1007 if(!configfile)
1008 byte_xasprintf(&configfile, "%s/config", pkgconfdir);
1009}
1010
3f3bb97b 1011/** @brief Free a configuration object */
460b9539 1012static void config_free(struct config *c) {
1013 int n;
1014
1015 if(c) {
1016 for(n = 0; n < (int)(sizeof conf / sizeof *conf); ++n)
1017 conf[n].type->free(c, &conf[n]);
1018 for(n = 0; n < c->nparts; ++n)
1019 xfree(c->parts[n]);
1020 xfree(c->parts);
1021 xfree(c);
1022 }
1023}
1024
3f3bb97b 1025/** @brief Set post-parse defaults */
460b9539 1026static void config_postdefaults(struct config *c) {
1027 struct config_state cs;
1028 const struct conf *whoami;
1029 int n;
1030
1031 static const char *namepart[][4] = {
88608992 1032 { "title", "/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
460b9539 1033 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1034 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1035 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1036 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1037 };
1038#define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1039
1040 static const char *transform[][5] = {
88608992 1041 { "track", "^.*/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
460b9539 1042 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1043 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1044 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1045 { "dir", "[[:punct:]]", "", "sort", "g", }
1046 };
1047#define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1048
1049 cs.path = "<internal>";
1050 cs.line = 0;
1051 cs.config = c;
1052 if(!c->namepart.n) {
1053 whoami = find("namepart");
1054 for(n = 0; n < NNAMEPART; ++n)
1055 set_namepart(&cs, whoami, 4, (char **)namepart[n]);
1056 }
1057 if(!c->transform.n) {
1058 whoami = find("transform");
1059 for(n = 0; n < NTRANSFORM; ++n)
1060 set_transform(&cs, whoami, 5, (char **)transform[n]);
1061 }
e83d0967
RK
1062 if(c->speaker_backend == -1) {
1063 if(c->speaker_command)
1064 c->speaker_backend = BACKEND_COMMAND;
1065 else if(c->broadcast.n)
1066 c->speaker_backend = BACKEND_NETWORK;
1067 else {
1068#if API_ALSA
1069 c->speaker_backend = BACKEND_ALSA;
1070#else
1071 c->speaker_backend = BACKEND_COMMAND;
1072#endif
1073 }
1074 }
1075 if(c->speaker_backend == BACKEND_COMMAND && !c->speaker_command)
1076 fatal(0, "speaker_backend is command but speaker_command is not set");
1077 if(c->speaker_backend == BACKEND_NETWORK && !c->broadcast.n)
1078 fatal(0, "speaker_backend is network but broadcast is not set");
6d2d327c
RK
1079 if(c->speaker_backend) {
1080 /* Override sample format */
1081 c->sample_format.rate = 44100;
1082 c->sample_format.channels = 2;
1083 c->sample_format.bits = 16;
1084 c->sample_format.endian = ENDIAN_BIG;
1085 }
460b9539 1086}
1087
3f3bb97b 1088/** @brief (Re-)read the config file */
460b9539 1089int config_read() {
1090 struct config *c;
1091 char *privconf;
1092 struct passwd *pw;
1093
1094 set_configfile();
1095 c = config_default();
9ade2319 1096 /* standalone Disobedience installs might not have a global config file */
1097 if(access(configfile, F_OK) == 0)
1098 if(config_include(c, configfile))
1099 return -1;
460b9539 1100 /* if we can read the private config file, do */
1101 if((privconf = config_private())
1102 && access(privconf, R_OK) == 0
1103 && config_include(c, privconf))
1104 return -1;
1105 xfree(privconf);
1106 /* if there's a per-user system config file for this user, read it */
1107 if(!(pw = getpwuid(getuid())))
1108 fatal(0, "cannot determine our username");
1109 if((privconf = config_usersysconf(pw))
1110 && access(privconf, F_OK) == 0
1111 && config_include(c, privconf))
1112 return -1;
1113 xfree(privconf);
1114 /* if we have a password file, read it */
1115 if((privconf = config_userconf(getenv("HOME"), pw))
1116 && access(privconf, F_OK) == 0
1117 && config_include(c, privconf))
1118 return -1;
1119 xfree(privconf);
1120 /* install default namepart and transform settings */
1121 config_postdefaults(c);
1122 /* everything is good so we shall use the new config */
1123 config_free(config);
1124 config = c;
1125 return 0;
1126}
1127
3f3bb97b 1128/** @brief Return the path to the private configuration file */
460b9539 1129char *config_private(void) {
1130 char *s;
1131
1132 set_configfile();
1133 byte_xasprintf(&s, "%s.private", configfile);
1134 return s;
1135}
1136
3f3bb97b 1137/** @brief Return the path to user's personal configuration file */
460b9539 1138char *config_userconf(const char *home, const struct passwd *pw) {
1139 char *s;
1140
1141 byte_xasprintf(&s, "%s/.disorder/passwd", home ? home : pw->pw_dir);
1142 return s;
1143}
1144
3f3bb97b
RK
1145/** @brief Return the path to user-specific system configuration */
1146char *config_usersysconf(const struct passwd *pw) {
460b9539 1147 char *s;
1148
1149 set_configfile();
1150 if(!strchr(pw->pw_name, '/')) {
1151 byte_xasprintf(&s, "%s.%s", configfile, pw->pw_name);
1152 return s;
1153 } else
1154 return 0;
1155}
1156
1157char *config_get_file(const char *name) {
1158 return get_file(config, name);
1159}
1160
1161/*
1162Local Variables:
1163c-basic-offset:2
1164comment-column:40
1165fill-column:79
1166End:
1167*/