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