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