2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2011, 2013 Richard Kettlewell
4 * Portions copyright (C) 2007 Mark Wooding
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.
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.
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/>.
19 /** @file lib/configuration.c
20 * @brief Configuration file support
26 #include <sys/types.h>
37 # include <langinfo.h>
45 #include "configuration.h"
51 #include "inputline.h"
63 /** @brief Path to config file
65 * set_configfile() sets the default if it is null.
69 /** @brief Read user configuration
71 * If clear, the user-specific configuration is not read.
73 int config_per_user = 1;
75 /** @brief Table of audio APIs
77 * Only set in server processes.
79 const struct uaudio *const *config_uaudio_apis;
81 /** @brief Config file parser state */
83 /** @brief Filename */
86 /** @brief Line number */
89 /** @brief Configuration object under construction */
90 struct config *config;
93 /** @brief Current configuration */
94 struct config *config;
96 /** @brief One configuration item */
98 /** @brief Name as it appears in the config file */
101 /** @brief Offset in @ref config structure */
104 /** @brief Pointer to item type */
105 const struct conftype *type;
107 /** @brief Pointer to item-specific validation routine
108 * @param cs Configuration state
109 * @param nvec Length of (proposed) new value
110 * @param vec Elements of new value
111 * @return 0 on success, non-0 on error
113 * The validate function should report any error it detects.
115 int (*validate)(const struct config_state *cs,
116 int nvec, char **vec);
119 /** @brief Type of a configuration item */
121 /** @brief Pointer to function to set item
122 * @param cs Configuration state
123 * @param whoami Configuration item to set
124 * @param nvec Length of new value
125 * @param vec New value
126 * @return 0 on success, non-0 on error
128 int (*set)(const struct config_state *cs,
129 const struct conf *whoami,
130 int nvec, char **vec);
132 /** @brief Pointer to function to free item
133 * @param c Configuration structure to free an item of
134 * @param whoami Configuration item to free
136 void (*free)(struct config *c, const struct conf *whoami);
139 /** @brief Compute the address of an item */
140 #define ADDRESS(C, TYPE) ((TYPE *)((char *)(C) + whoami->offset))
141 /** @brief Return the value of an item */
142 #define VALUE(C, TYPE) (*ADDRESS(C, TYPE))
144 static int stringlist_compare(const struct stringlist *a,
145 const struct stringlist *b);
146 static int namepartlist_compare(const struct namepartlist *a,
147 const struct namepartlist *b);
149 static int set_signal(const struct config_state *cs,
150 const struct conf *whoami,
151 int nvec, char **vec) {
155 disorder_error(0, "%s:%d: '%s' requires one argument",
156 cs->path, cs->line, whoami->name);
159 if((n = find_signal(vec[0])) == -1) {
160 disorder_error(0, "%s:%d: unknown signal '%s'",
161 cs->path, cs->line, vec[0]);
164 VALUE(cs->config, int) = n;
168 static int set_collections(const struct config_state *cs,
169 const struct conf *whoami,
170 int nvec, char **vec) {
171 struct collectionlist *cl;
172 const char *root, *encoding, *module;
191 disorder_error(0, "%s:%d: '%s' requires at least one argument",
192 cs->path, cs->line, whoami->name);
195 disorder_error(0, "%s:%d: '%s' requires at most three arguments",
196 cs->path, cs->line, whoami->name);
199 /* Sanity check root */
201 disorder_error(0, "%s:%d: collection root must start with '/'",
205 if(root[1] && root[strlen(root)-1] == '/') {
206 disorder_error(0, "%s:%d: collection root must not end with '/'",
215 encoding = nl_langinfo(CODESET);
220 cl = ADDRESS(cs->config, struct collectionlist);
222 cl->s = xrealloc(cl->s, cl->n * sizeof (struct collection));
223 cl->s[cl->n - 1].module = xstrdup(module);
224 cl->s[cl->n - 1].encoding = xstrdup(encoding);
225 cl->s[cl->n - 1].root = xstrdup(root);
229 static int set_boolean(const struct config_state *cs,
230 const struct conf *whoami,
231 int nvec, char **vec) {
235 disorder_error(0, "%s:%d: '%s' takes only one argument",
236 cs->path, cs->line, whoami->name);
239 if(!strcmp(vec[0], "yes")) state = 1;
240 else if(!strcmp(vec[0], "no")) state = 0;
242 disorder_error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'",
243 cs->path, cs->line, whoami->name);
246 VALUE(cs->config, int) = state;
250 static int set_string(const struct config_state *cs,
251 const struct conf *whoami,
252 int nvec, char **vec) {
254 disorder_error(0, "%s:%d: '%s' takes only one argument",
255 cs->path, cs->line, whoami->name);
258 xfree(VALUE(cs->config, char *));
259 VALUE(cs->config, char *) = xstrdup(vec[0]);
263 static int set_stringlist(const struct config_state *cs,
264 const struct conf *whoami,
265 int nvec, char **vec) {
267 struct stringlist *sl;
269 sl = ADDRESS(cs->config, struct stringlist);
271 for(n = 0; n < nvec; ++n) {
273 sl->s = xrealloc(sl->s, (sl->n * sizeof (char *)));
274 sl->s[sl->n - 1] = xstrdup(vec[n]);
279 static int set_integer(const struct config_state *cs,
280 const struct conf *whoami,
281 int nvec, char **vec) {
285 disorder_error(0, "%s:%d: '%s' takes only one argument",
286 cs->path, cs->line, whoami->name);
289 if(xstrtol(ADDRESS(cs->config, long), vec[0], &e, 0)) {
290 disorder_error(errno, "%s:%d: converting integer", cs->path, cs->line);
294 disorder_error(0, "%s:%d: invalid integer syntax", cs->path, cs->line);
300 static int set_stringlist_accum(const struct config_state *cs,
301 const struct conf *whoami,
302 int nvec, char **vec) {
304 struct stringlist *s;
305 struct stringlistlist *sll;
307 sll = ADDRESS(cs->config, struct stringlistlist);
313 sll->s = xrealloc(sll->s, (sll->n * sizeof (struct stringlist)));
314 s = &sll->s[sll->n - 1];
316 s->s = xmalloc((nvec + 1) * sizeof (char *));
317 for(n = 0; n < nvec; ++n)
318 s->s[n] = xstrdup(vec[n]);
322 static int set_string_accum(const struct config_state *cs,
323 const struct conf *whoami,
324 int nvec, char **vec) {
326 struct stringlist *sl;
328 sl = ADDRESS(cs->config, struct stringlist);
333 for(n = 0; n < nvec; ++n) {
335 sl->s = xrealloc(sl->s, (sl->n * sizeof (char *)));
336 sl->s[sl->n - 1] = xstrdup(vec[n]);
341 static int parse_sample_format(const struct config_state *cs,
342 struct stream_header *format,
343 int nvec, char **vec) {
348 disorder_error(0, "%s:%d: wrong number of arguments", cs->path, cs->line);
351 if(xstrtol(&t, p, &p, 0)) {
352 disorder_error(errno, "%s:%d: converting bits-per-sample",
356 if(t != 8 && t != 16) {
357 disorder_error(0, "%s:%d: bad bits-per-sample (%ld)",
358 cs->path, cs->line, t);
361 if(format) format->bits = t;
363 case 'l': case 'L': t = ENDIAN_LITTLE; p++; break;
364 case 'b': case 'B': t = ENDIAN_BIG; p++; break;
365 default: t = ENDIAN_NATIVE; break;
367 if(format) format->endian = t;
369 disorder_error(errno, "%s:%d: expected `/' after bits-per-sample",
374 if(xstrtol(&t, p, &p, 0)) {
375 disorder_error(errno, "%s:%d: converting sample-rate", cs->path, cs->line);
378 if(t < 1 || t > INT_MAX) {
379 disorder_error(0, "%s:%d: silly sample-rate (%ld)", cs->path, cs->line, t);
382 if(format) format->rate = t;
384 disorder_error(0, "%s:%d: expected `/' after sample-rate",
389 if(xstrtol(&t, p, &p, 0)) {
390 disorder_error(errno, "%s:%d: converting channels", cs->path, cs->line);
394 disorder_error(0, "%s:%d: silly number (%ld) of channels",
395 cs->path, cs->line, t);
398 if(format) format->channels = t;
400 disorder_error(0, "%s:%d: junk after channels", cs->path, cs->line);
406 static int set_sample_format(const struct config_state *cs,
407 const struct conf *whoami,
408 int nvec, char **vec) {
409 return parse_sample_format(cs, ADDRESS(cs->config, struct stream_header),
414 static int set_namepart(const struct config_state *cs,
415 const struct conf *whoami,
416 int nvec, char **vec) {
417 struct namepartlist *npl = ADDRESS(cs->config, struct namepartlist);
424 disorder_error(0, "%s:%d: namepart needs at least 3 arguments",
429 disorder_error(0, "%s:%d: namepart needs at most 5 arguments",
433 reflags = nvec >= 5 ? regsub_flags(vec[4]) : 0;
434 if(!(re = pcre_compile(vec[1],
436 |regsub_compile_options(reflags),
437 &errstr, &erroffset, 0))) {
438 disorder_error(0, "%s:%d: compiling regexp /%s/: %s (offset %d)",
439 cs->path, cs->line, vec[1], errstr, erroffset);
442 npl->s = xrealloc(npl->s, (npl->n + 1) * sizeof (struct namepart));
443 npl->s[npl->n].part = xstrdup(vec[0]);
444 npl->s[npl->n].re = re;
445 npl->s[npl->n].res = xstrdup(vec[1]);
446 npl->s[npl->n].replace = xstrdup(vec[2]);
447 npl->s[npl->n].context = xstrdup(vec[3]);
448 npl->s[npl->n].reflags = reflags;
450 /* XXX a bit of a bodge; relies on there being very few parts. */
451 for(n = 0; (n < cs->config->nparts
452 && strcmp(cs->config->parts[n], vec[0])); ++n)
454 if(n >= cs->config->nparts) {
455 cs->config->parts = xrealloc(cs->config->parts,
456 (cs->config->nparts + 1) * sizeof (char *));
457 cs->config->parts[cs->config->nparts++] = xstrdup(vec[0]);
462 static int set_transform(const struct config_state *cs,
463 const struct conf *whoami,
464 int nvec, char **vec) {
465 struct transformlist *tl = ADDRESS(cs->config, struct transformlist);
472 disorder_error(0, "%s:%d: transform needs at least 3 arguments",
477 disorder_error(0, "%s:%d: transform needs at most 5 arguments",
481 reflags = (nvec >= 5 ? regsub_flags(vec[4]) : 0);
482 if(!(re = pcre_compile(vec[1],
484 |regsub_compile_options(reflags),
485 &errstr, &erroffset, 0))) {
486 disorder_error(0, "%s:%d: compiling regexp /%s/: %s (offset %d)",
487 cs->path, cs->line, vec[1], errstr, erroffset);
490 tl->t = xrealloc(tl->t, (tl->n + 1) * sizeof (struct namepart));
491 tl->t[tl->n].type = xstrdup(vec[0]);
492 tl->t[tl->n].context = xstrdup(vec[3] ? vec[3] : "*");
493 tl->t[tl->n].re = re;
494 tl->t[tl->n].replace = xstrdup(vec[2]);
495 tl->t[tl->n].flags = reflags;
501 static int set_rights(const struct config_state *cs,
502 const struct conf *whoami,
503 int nvec, char **vec) {
505 disorder_error(0, "%s:%d: '%s' requires one argument",
506 cs->path, cs->line, whoami->name);
509 if(parse_rights(vec[0], 0, 1)) {
510 disorder_error(0, "%s:%d: invalid rights string '%s'",
511 cs->path, cs->line, vec[0]);
514 return set_string(cs, whoami, nvec, vec);
517 static int set_netaddress(const struct config_state *cs,
518 const struct conf *whoami,
519 int nvec, char **vec) {
520 struct netaddress *na = ADDRESS(cs->config, struct netaddress);
522 if(netaddress_parse(na, nvec, vec)) {
523 disorder_error(0, "%s:%d: invalid network address", cs->path, cs->line);
531 static void free_none(struct config attribute((unused)) *c,
532 const struct conf attribute((unused)) *whoami) {
535 static void free_string(struct config *c,
536 const struct conf *whoami) {
537 xfree(VALUE(c, char *));
538 VALUE(c, char *) = 0;
541 static void free_stringlist(struct config *c,
542 const struct conf *whoami) {
544 struct stringlist *sl = ADDRESS(c, struct stringlist);
546 for(n = 0; n < sl->n; ++n)
551 static void free_stringlistlist(struct config *c,
552 const struct conf *whoami) {
554 struct stringlistlist *sll = ADDRESS(c, struct stringlistlist);
555 struct stringlist *sl;
557 for(n = 0; n < sll->n; ++n) {
559 for(m = 0; m < sl->n; ++m)
566 static void free_collectionlist(struct config *c,
567 const struct conf *whoami) {
568 struct collectionlist *cll = ADDRESS(c, struct collectionlist);
569 struct collection *cl;
572 for(n = 0; n < cll->n; ++n) {
582 static void free_namepartlist(struct config *c,
583 const struct conf *whoami) {
584 struct namepartlist *npl = ADDRESS(c, struct namepartlist);
588 for(n = 0; n < npl->n; ++n) {
591 pcre_free(np->re); /* ...whatever pcre_free is set to. */
599 static void free_transformlist(struct config *c,
600 const struct conf *whoami) {
601 struct transformlist *tl = ADDRESS(c, struct transformlist);
605 for(n = 0; n < tl->n; ++n) {
608 pcre_free(t->re); /* ...whatever pcre_free is set to. */
616 static void free_netaddress(struct config *c,
617 const struct conf *whoami) {
618 struct netaddress *na = ADDRESS(c, struct netaddress);
623 /* configuration types */
625 static const struct conftype
626 type_signal = { set_signal, free_none },
627 type_collections = { set_collections, free_collectionlist },
628 type_boolean = { set_boolean, free_none },
629 type_string = { set_string, free_string },
630 type_stringlist = { set_stringlist, free_stringlist },
631 type_integer = { set_integer, free_none },
632 type_stringlist_accum = { set_stringlist_accum, free_stringlistlist },
633 type_string_accum = { set_string_accum, free_stringlist },
634 type_sample_format = { set_sample_format, free_none },
636 type_namepart = { set_namepart, free_namepartlist },
637 type_transform = { set_transform, free_transformlist },
639 type_netaddress = { set_netaddress, free_netaddress },
640 type_rights = { set_rights, free_string };
642 /* specific validation routine */
644 /** @brief Perform a test on a filename
645 * @param test Test function to call on mode bits
646 * @param what Type of file sought
648 * If @p test returns 0 then the file is not a @p what and an error
649 * is reported and -1 is returned.
651 #define VALIDATE_FILE(test, what) do { \
655 for(n = 0; n < nvec; ++n) { \
656 if(stat(vec[n], &sb) < 0) { \
657 disorder_error(errno, "%s:%d: %s", \
658 cs->path, cs->line, vec[n]); \
661 if(!test(sb.st_mode)) { \
662 disorder_error(0, "%s:%d: %s is not a %s", \
663 cs->path, cs->line, vec[n], what); \
669 /** @brief Validate an absolute path
670 * @param cs Configuration state
671 * @param nvec Length of (proposed) new value
672 * @param vec Elements of new value
673 * @return 0 on success, non-0 on error
675 static int validate_isabspath(const struct config_state *cs,
676 int nvec, char **vec) {
679 for(n = 0; n < nvec; ++n)
680 if(vec[n][0] != '/') {
681 disorder_error(0, "%s:%d: %s: not an absolute path",
682 cs->path, cs->line, vec[n]);
688 /** @brief Validate an existing directory
689 * @param cs Configuration state
690 * @param nvec Length of (proposed) new value
691 * @param vec Elements of new value
692 * @return 0 on success, non-0 on error
694 static int validate_isdir(const struct config_state *cs,
695 int nvec, char **vec) {
696 VALIDATE_FILE(S_ISDIR, "directory");
700 /** @brief Validate an existing regular file
701 * @param cs Configuration state
702 * @param nvec Length of (proposed) new value
703 * @param vec Elements of new value
704 * @return 0 on success, non-0 on error
706 static int validate_isreg(const struct config_state *cs,
707 int nvec, char **vec) {
708 VALIDATE_FILE(S_ISREG, "regular file");
712 /** @brief Validate a player pattern
713 * @param cs Configuration state
714 * @param nvec Length of (proposed) new value
715 * @param vec Elements of new value
716 * @return 0 on success, non-0 on error
718 static int validate_player(const struct config_state *cs,
720 char attribute((unused)) **vec) {
721 if(nvec && nvec < 2) {
722 disorder_error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
729 /** @brief Validate a track length pattern
730 * @param cs Configuration state
731 * @param nvec Length of (proposed) new value
732 * @param vec Elements of new value
733 * @return 0 on success, non-0 on error
735 static int validate_tracklength(const struct config_state *cs,
737 char attribute((unused)) **vec) {
738 if(nvec && nvec < 2) {
739 disorder_error(0, "%s:%d: should be at least 'tracklength PATTERN MODULE'",
746 /** @brief Validate a non-negative (@c long) integer
747 * @param cs Configuration state
748 * @param nvec Length of (proposed) new value
749 * @param vec Elements of new value
750 * @return 0 on success, non-0 on error
752 static int validate_non_negative(const struct config_state *cs,
753 int nvec, char **vec) {
758 disorder_error(0, "%s:%d: missing argument", cs->path, cs->line);
762 disorder_error(0, "%s:%d: too many arguments", cs->path, cs->line);
765 if(xstrtol(&n, vec[0], 0, 0)) {
766 disorder_error(0, "%s:%d: %s", cs->path, cs->line,
767 format_error(ec_errno, errno, errbuf, sizeof errbuf));
771 disorder_error(0, "%s:%d: must not be negative", cs->path, cs->line);
777 /** @brief Validate a positive (@c long) integer
778 * @param cs Configuration state
779 * @param nvec Length of (proposed) new value
780 * @param vec Elements of new value
781 * @return 0 on success, non-0 on error
783 static int validate_positive(const struct config_state *cs,
784 int nvec, char **vec) {
789 disorder_error(0, "%s:%d: missing argument", cs->path, cs->line);
793 disorder_error(0, "%s:%d: too many arguments", cs->path, cs->line);
796 if(xstrtol(&n, vec[0], 0, 0)) {
797 disorder_error(0, "%s:%d: %s", cs->path, cs->line,
798 format_error(ec_errno, errno, errbuf, sizeof errbuf));
802 disorder_error(0, "%s:%d: must be positive", cs->path, cs->line);
808 /** @brief Validate a system username
809 * @param cs Configuration state
810 * @param nvec Length of (proposed) new value
811 * @param vec Elements of new value
812 * @return 0 on success, non-0 on error
814 static int validate_isauser(const struct config_state *cs,
815 int attribute((unused)) nvec,
817 if(!getpwnam(vec[0])) {
818 disorder_error(0, "%s:%d: no such user as '%s'", cs->path, cs->line, vec[0]);
824 /** @brief Validate a sample format string
825 * @param cs Configuration state
826 * @param nvec Length of (proposed) new value
827 * @param vec Elements of new value
828 * @return 0 on success, non-0 on error
830 static int validate_sample_format(const struct config_state *cs,
831 int attribute((unused)) nvec,
833 return parse_sample_format(cs, 0, nvec, vec);
836 /** @brief Validate anything
837 * @param cs Configuration state
838 * @param nvec Length of (proposed) new value
839 * @param vec Elements of new value
842 static int validate_any(const struct config_state attribute((unused)) *cs,
843 int attribute((unused)) nvec,
844 char attribute((unused)) **vec) {
848 /** @brief Validate a URL
849 * @param cs Configuration state
850 * @param nvec Length of (proposed) new value
851 * @param vec Elements of new value
852 * @return 0 on success, non-0 on error
856 static int validate_url(const struct config_state attribute((unused)) *cs,
857 int attribute((unused)) nvec,
861 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
862 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
864 n = strspn(s, ("abcdefghijklmnopqrstuvwxyz"
865 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
868 disorder_error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
871 if(!strncmp(s, "http:", 5)
872 || !strncmp(s, "https:", 6)) {
874 /* we only do a rather cursory check */
875 if(strncmp(s, "//", 2)) {
876 disorder_error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
883 /** @brief Validate an alias pattern
884 * @param cs Configuration state
885 * @param nvec Length of (proposed) new value
886 * @param vec Elements of new value
887 * @return 0 on success, non-0 on error
889 static int validate_alias(const struct config_state *cs,
893 int in_brackets = 0, c;
896 disorder_error(0, "%s:%d: missing argument", cs->path, cs->line);
900 disorder_error(0, "%s:%d: too many arguments", cs->path, cs->line);
904 while((c = (unsigned char)*s++)) {
908 else if(!isalnum(c)) {
909 disorder_error(0, "%s:%d: invalid part name in alias expansion in '%s'",
910 cs->path, cs->line, vec[0]);
918 } else if(c == '\\') {
919 if(!(c = (unsigned char)*s++)) {
920 disorder_error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
921 cs->path, cs->line, vec[0]);
923 } else if(c != '\\' && c != '{') {
924 disorder_error(0, "%s:%d: invalid escape in alias expansion in '%s'",
925 cs->path, cs->line, vec[0]);
934 "%s:%d: unterminated part name in alias expansion in '%s'",
935 cs->path, cs->line, vec[0]);
941 /** @brief Validate a hash algorithm name
942 * @param cs Configuration state
943 * @param nvec Length of (proposed) new value
944 * @param vec Elements of new value
945 * @return 0 on success, non-0 on error
947 static int validate_algo(const struct config_state attribute((unused)) *cs,
951 disorder_error(0, "%s:%d: invalid algorithm specification", cs->path, cs->line);
954 if(!valid_authhash(vec[0])) {
955 disorder_error(0, "%s:%d: unsuported algorithm '%s'", cs->path, cs->line, vec[0]);
961 /** @brief Validate a playback backend name
962 * @param cs Configuration state
963 * @param nvec Length of (proposed) new value
964 * @param vec Elements of new value
965 * @return 0 on success, non-0 on error
967 static int validate_backend(const struct config_state attribute((unused)) *cs,
972 disorder_error(0, "%s:%d: invalid sound API specification", cs->path, cs->line);
975 if(!strcmp(vec[0], "network")) {
976 disorder_error(0, "'api network' is deprecated; use 'api rtp'");
979 if(config_uaudio_apis) {
980 for(n = 0; config_uaudio_apis[n]; ++n)
981 if(!strcmp(vec[0], config_uaudio_apis[n]->name))
983 disorder_error(0, "%s:%d: unrecognized sound API '%s'", cs->path, cs->line, vec[0]);
986 /* In non-server processes we have no idea what's valid */
990 /** @brief Validate a pause mode string
991 * @param cs Configuration state
992 * @param nvec Length of (proposed) new value
993 * @param vec Elements of new value
994 * @return 0 on success, non-0 on error
996 static int validate_pausemode(const struct config_state attribute((unused)) *cs,
999 if(nvec == 1 && (!strcmp(vec[0], "silence") || !strcmp(vec[0], "suspend")))
1001 disorder_error(0, "%s:%d: invalid pause mode", cs->path, cs->line);
1005 /** @brief Validate a destination network address
1006 * @param cs Configuration state
1007 * @param nvec Length of (proposed) new value
1008 * @param vec Elements of new value
1009 * @return 0 on success, non-0 on error
1011 * By a destination address, it is meant that it must not be a wildcard
1014 static int validate_destaddr(const struct config_state attribute((unused)) *cs,
1017 struct netaddress na[1];
1019 if(netaddress_parse(na, nvec, vec)) {
1020 disorder_error(0, "%s:%d: invalid network address", cs->path, cs->line);
1024 disorder_error(0, "%s:%d: destination address required", cs->path, cs->line);
1031 /** @brief Item name and and offset */
1032 #define C(x) #x, offsetof(struct config, x)
1033 /** @brief Item name and and offset */
1034 #define C2(x,y) #x, offsetof(struct config, y)
1036 /** @brief All configuration items */
1037 static const struct conf conf[] = {
1038 { C(alias), &type_string, validate_alias },
1039 { C(api), &type_string, validate_backend },
1040 { C(authorization_algorithm), &type_string, validate_algo },
1041 { C(broadcast), &type_netaddress, validate_destaddr },
1042 { C(broadcast_from), &type_netaddress, validate_any },
1043 { C(channel), &type_string, validate_any },
1044 { C(checkpoint_kbyte), &type_integer, validate_non_negative },
1045 { C(checkpoint_min), &type_integer, validate_non_negative },
1046 { C(collection), &type_collections, validate_any },
1047 { C(connect), &type_netaddress, validate_destaddr },
1048 { C(cookie_key_lifetime), &type_integer, validate_positive },
1049 { C(cookie_login_lifetime), &type_integer, validate_positive },
1050 { C(dbversion), &type_integer, validate_positive },
1051 { C(default_rights), &type_rights, validate_any },
1052 { C(device), &type_string, validate_any },
1053 { C(history), &type_integer, validate_positive },
1054 { C(home), &type_string, validate_isabspath },
1055 { C(listen), &type_netaddress, validate_any },
1056 { C(mail_sender), &type_string, validate_any },
1057 { C(mixer), &type_string, validate_any },
1058 { C(mount_rescan), &type_boolean, validate_any },
1059 { C(multicast_loop), &type_boolean, validate_any },
1060 { C(multicast_ttl), &type_integer, validate_non_negative },
1062 { C(namepart), &type_namepart, validate_any },
1064 { C(new_bias), &type_integer, validate_positive },
1065 { C(new_bias_age), &type_integer, validate_positive },
1066 { C(new_max), &type_integer, validate_positive },
1067 { C2(nice, nice_rescan), &type_integer, validate_non_negative },
1068 { C(nice_rescan), &type_integer, validate_non_negative },
1069 { C(nice_server), &type_integer, validate_any },
1070 { C(nice_speaker), &type_integer, validate_any },
1071 { C(noticed_history), &type_integer, validate_positive },
1072 { C(password), &type_string, validate_any },
1073 { C(pause_mode), &type_string, validate_pausemode },
1074 { C(player), &type_stringlist_accum, validate_player },
1075 { C(playlist_lock_timeout), &type_integer, validate_positive },
1076 { C(playlist_max) , &type_integer, validate_positive },
1077 { C(plugins), &type_string_accum, validate_isdir },
1078 { C(queue_pad), &type_integer, validate_positive },
1079 { C(refresh), &type_integer, validate_positive },
1080 { C(refresh_min), &type_integer, validate_non_negative },
1081 { C(reminder_interval), &type_integer, validate_positive },
1082 { C(remote_userman), &type_boolean, validate_any },
1083 { C(replay_min), &type_integer, validate_non_negative },
1084 { C(rtp_delay_threshold), &type_integer, validate_positive },
1085 { C(rtp_mode), &type_string, validate_any },
1086 { C(rtp_verbose), &type_boolean, validate_any },
1087 { C(sample_format), &type_sample_format, validate_sample_format },
1088 { C(scratch), &type_string_accum, validate_isreg },
1089 { C(sendmail), &type_string, validate_isabspath },
1090 { C(short_display), &type_integer, validate_positive },
1091 { C(signal), &type_signal, validate_any },
1092 { C(smtp_server), &type_string, validate_any },
1093 { C(sox_generation), &type_integer, validate_non_negative },
1094 { C2(speaker_backend, api), &type_string, validate_backend },
1095 { C(speaker_command), &type_string, validate_any },
1096 { C(stopword), &type_string_accum, validate_any },
1097 { C(templates), &type_string_accum, validate_isdir },
1098 { C(tracklength), &type_stringlist_accum, validate_tracklength },
1100 { C(transform), &type_transform, validate_any },
1102 { C(url), &type_string, validate_url },
1103 { C(user), &type_string, validate_isauser },
1104 { C(username), &type_string, validate_any },
1107 /** @brief Find a configuration item's definition by key */
1108 static const struct conf *find(const char *key) {
1111 if((n = TABLE_FIND(conf, name, key)) < 0)
1116 /** @brief Set a new configuration value
1117 * @param cs Configuration state
1118 * @param nvec Length of @p vec
1119 * @param vec Name and new value
1120 * @return 0 on success, non-0 on error.
1122 * @c vec[0] is the name, the rest is the value.
1124 static int config_set(const struct config_state *cs,
1125 int nvec, char **vec) {
1126 const struct conf *which;
1128 D(("config_set %s", vec[0]));
1129 if(!(which = find(vec[0]))) {
1130 disorder_error(0, "%s:%d: unknown configuration key '%s'",
1131 cs->path, cs->line, vec[0]);
1134 return (which->validate(cs, nvec - 1, vec + 1)
1135 || which->type->set(cs, which, nvec - 1, vec + 1));
1138 /** @brief Set a configuration item from parameters
1139 * @param cs Configuration state
1140 * @param which Item to set
1141 * @param ... Value as strings, terminated by (char *)NULL
1142 * @return 0 on success, non-0 on error
1144 static int config_set_args(const struct config_state *cs,
1145 const char *which, ...) {
1152 vector_append(v, (char *)which);
1153 va_start(ap, which);
1154 while((s = va_arg(ap, char *)))
1155 vector_append(v, s);
1157 vector_terminate(v);
1158 rc = config_set(cs, v->nvec, v->vec);
1163 /** @brief Error callback used by config_include()
1164 * @param msg Error message
1165 * @param u User data (@ref config_state)
1167 static void config_error(const char *msg, void *u) {
1168 const struct config_state *cs = u;
1170 disorder_error(0, "%s:%d: %s", cs->path, cs->line, msg);
1173 /** @brief Include a file by name
1174 * @param c Configuration to update
1175 * @param path Path to read
1176 * @return 0 on success, non-0 on error
1178 static int config_include(struct config *c, const char *path) {
1180 char *buffer, *inputbuffer, **vec;
1182 struct config_state cs;
1187 D(("%s: reading configuration", path));
1188 if(!(fp = fopen(path, "r"))) {
1189 disorder_error(errno, "error opening %s", path);
1192 while(!inputline(path, fp, &inputbuffer, '\n')) {
1194 if(!(buffer = mb2utf8(inputbuffer))) {
1195 disorder_error(errno, "%s:%d: cannot convert to UTF-8", cs.path, cs.line);
1201 if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES,
1202 config_error, &cs))) {
1208 /* 'include' is special-cased */
1209 if(!strcmp(vec[0], "include")) {
1211 disorder_error(0, "%s:%d: must be 'include PATH'", cs.path, cs.line);
1214 config_include(c, vec[1]);
1216 ret |= config_set(&cs, n, vec);
1218 for(n = 0; vec[n]; ++n) xfree(vec[n]);
1223 disorder_error(errno, "error reading %s", path);
1230 /** @brief Default stopword setting */
1231 static const char *const default_stopwords[] = {
1290 #define NDEFAULT_STOPWORDS (sizeof default_stopwords / sizeof *default_stopwords)
1292 /** @brief Default player patterns */
1293 static const char *const default_players[] = {
1299 #define NDEFAULT_PLAYERS (sizeof default_players / sizeof *default_players)
1301 /** @brief Make a new default configuration
1302 * @return New configuration
1304 static struct config *config_default(void) {
1305 struct config *c = xmalloc(sizeof *c);
1306 const char *logname;
1308 struct config_state cs;
1311 cs.path = "<internal>";
1314 /* Strings had better be xstrdup'd as they will get freed at some point. */
1316 c->home = xstrdup(pkgstatedir);
1317 if(!(pw = getpwuid(getuid())))
1318 disorder_fatal(0, "cannot determine our username");
1319 logname = pw->pw_name;
1320 c->username = xstrdup(logname);
1323 c->signal = SIGKILL;
1324 c->alias = xstrdup("{/artist}{/album}{/title}{ext}");
1325 c->device = xstrdup("default");
1326 c->nice_rescan = 10;
1327 c->speaker_command = 0;
1328 c->sample_format.bits = 16;
1329 c->sample_format.rate = 44100;
1330 c->sample_format.channels = 2;
1331 c->sample_format.endian = ENDIAN_NATIVE;
1333 c->replay_min = 8 * 3600;
1335 c->multicast_ttl = 1;
1336 c->multicast_loop = 1;
1337 c->authorization_algorithm = xstrdup("sha1");
1338 c->noticed_history = 31;
1339 c->short_display = 32;
1343 c->cookie_login_lifetime = 86400;
1344 c->cookie_key_lifetime = 86400 * 7;
1345 if(sendmail_binary[0] && strcmp(sendmail_binary, "none"))
1346 c->sendmail = xstrdup(sendmail_binary);
1347 c->smtp_server = xstrdup("127.0.0.1");
1349 c->reminder_interval = 600; /* 10m */
1350 c->new_bias_age = 7 * 86400; /* 1 week */
1351 c->new_bias = 4500000; /* 50 times the base weight */
1352 c->sox_generation = DEFAULT_SOX_GENERATION;
1353 c->playlist_max = INT_MAX; /* effectively no limit */
1354 c->playlist_lock_timeout = 10; /* 10s */
1355 c->mount_rescan = 1;
1356 /* Default stopwords */
1357 if(config_set(&cs, (int)NDEFAULT_STOPWORDS, (char **)default_stopwords))
1359 /* Default player configuration */
1360 for(n = 0; n < NDEFAULT_PLAYERS; ++n) {
1361 if(config_set_args(&cs, "player",
1362 default_players[n], "execraw", "disorder-decode", (char *)0))
1364 if(config_set_args(&cs, "tracklength",
1365 default_players[n], "disorder-tracklength", (char *)0))
1368 c->broadcast.af = -1;
1369 c->broadcast_from.af = -1;
1372 c->rtp_mode = xstrdup("auto");
1376 /** @brief Construct a filename
1377 * @param c Configuration
1378 * @param name Base filename
1379 * @return Full filename
1381 * Usually use config_get_file() instead.
1383 char *config_get_file2(struct config *c, const char *name) {
1386 byte_xasprintf(&s, "%s/%s", c->home, name);
1390 /** @brief Set the default configuration file */
1391 static void set_configfile(void) {
1393 byte_xasprintf(&configfile, "%s/config", pkgconfdir);
1396 /** @brief Free a configuration object
1397 * @param c Configuration to free
1399 * @p c is indeterminate after this function is called.
1401 void config_free(struct config *c) {
1405 for(n = 0; n < (int)(sizeof conf / sizeof *conf); ++n)
1406 conf[n].type->free(c, &conf[n]);
1407 for(n = 0; n < c->nparts; ++n)
1414 /** @brief Set post-parse defaults
1415 * @param c Configuration to update
1416 * @param server True when running in the server
1418 * If @p server is set then certain parts of the configuration are more
1419 * strictly validated.
1421 static void config_postdefaults(struct config *c,
1423 struct config_state cs;
1425 const struct conf *whoami;
1430 static const char *namepart[][4] = {
1431 { "title", "/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
1432 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1433 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1434 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1435 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1437 #define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1439 static const char *transform[][5] = {
1440 { "track", "^.*/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
1441 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1442 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1443 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1444 { "dir", "[[:punct:]]", "", "sort", "g", }
1446 #define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1449 cs.path = "<internal>";
1453 if(!c->namepart.n) {
1454 whoami = find("namepart");
1455 for(n = 0; n < NNAMEPART; ++n)
1456 set_namepart(&cs, whoami, 4, (char **)namepart[n]);
1458 if(!c->transform.n) {
1459 whoami = find("transform");
1460 for(n = 0; n < NTRANSFORM; ++n)
1461 set_transform(&cs, whoami, 5, (char **)transform[n]);
1465 if(c->speaker_command)
1466 c->api = xstrdup("command");
1467 else if(c->broadcast.af != -1)
1468 c->api = xstrdup("rtp");
1469 else if(config_uaudio_apis)
1470 c->api = xstrdup(uaudio_default(config_uaudio_apis,
1471 UAUDIO_API_SERVER)->name);
1473 c->api = xstrdup("<none>");
1475 if(!strcmp(c->api, "network"))
1476 c->api = xstrdup("rtp");
1478 if(!strcmp(c->api, "command") && !c->speaker_command)
1479 disorder_fatal(0, "'api command' but speaker_command is not set");
1480 if((!strcmp(c->api, "rtp")) && c->broadcast.af == -1)
1481 disorder_fatal(0, "'api rtp' but broadcast is not set");
1483 /* Override sample format */
1484 if(!strcmp(c->api, "rtp")) {
1485 c->sample_format.rate = 44100;
1486 c->sample_format.channels = 2;
1487 c->sample_format.bits = 16;
1488 c->sample_format.endian = ENDIAN_NATIVE;
1490 if(!strcmp(c->api, "coreaudio")) {
1491 c->sample_format.rate = 44100;
1492 c->sample_format.channels = 2;
1493 c->sample_format.bits = 16;
1494 c->sample_format.endian = ENDIAN_NATIVE;
1496 if(!c->default_rights) {
1497 rights_type r = RIGHTS__MASK & ~(RIGHT_ADMIN|RIGHT_REGISTER
1499 |RIGHT_SCRATCH__MASK
1500 |RIGHT_REMOVE__MASK);
1501 r |= RIGHT_SCRATCH_ANY|RIGHT_MOVE_ANY|RIGHT_REMOVE_ANY;
1502 c->default_rights = rights_string(r);
1506 /** @brief (Re-)read the config file
1507 * @param server If set, do extra checking
1508 * @param oldconfig Old configuration for compatibility check
1509 * @return 0 on success, non-0 on error
1511 * If @p oldconfig is set, then certain compatibility checks are done between
1512 * the old and new configurations.
1514 int config_read(int server,
1515 const struct config *oldconfig) {
1521 c = config_default();
1522 /* standalone Disobedience installs might not have a global config file */
1523 if(access(configfile, F_OK) == 0)
1524 if(config_include(c, configfile))
1526 /* if we can read the private config file, do */
1527 if((privconf = config_private())
1528 && access(privconf, R_OK) == 0
1529 && config_include(c, privconf))
1532 /* if there's a per-user system config file for this user, read it */
1533 if(config_per_user) {
1534 if(!(pw = getpwuid(getuid())))
1535 disorder_fatal(0, "cannot determine our username");
1536 if((privconf = config_usersysconf(pw))
1537 && access(privconf, F_OK) == 0
1538 && config_include(c, privconf))
1541 /* if we have a password file, read it */
1542 if((privconf = config_userconf(0, pw))
1543 && access(privconf, F_OK) == 0
1544 && config_include(c, privconf))
1548 /* install default namepart and transform settings */
1549 config_postdefaults(c, server);
1552 if(strcmp(c->home, oldconfig->home)) {
1553 disorder_error(0, "'home' cannot be changed without a restart");
1556 if(strcmp(c->alias, oldconfig->alias)) {
1557 disorder_error(0, "'alias' cannot be changed without a restart");
1560 if(strcmp(c->user, oldconfig->user)) {
1561 disorder_error(0, "'user' cannot be changed without a restart");
1564 if(c->nice_speaker != oldconfig->nice_speaker) {
1565 disorder_error(0, "'nice_speaker' cannot be changed without a restart");
1566 /* ...but we accept the new config anyway */
1568 if(c->nice_server != oldconfig->nice_server) {
1569 disorder_error(0, "'nice_server' cannot be changed without a restart");
1570 /* ...but we accept the new config anyway */
1573 if(namepartlist_compare(&c->namepart, &oldconfig->namepart)) {
1574 disorder_error(0, "'namepart' settings cannot be changed without a restart");
1578 if(stringlist_compare(&c->stopword, &oldconfig->stopword)) {
1579 disorder_error(0, "'stopword' settings cannot be changed without a restart");
1583 disorder_error(0, "not installing incompatible new configuration");
1587 /* everything is good so we shall use the new config */
1588 config_free(config);
1589 /* warn about obsolete directives */
1594 /** @brief Return the path to the private configuration file */
1595 char *config_private(void) {
1599 byte_xasprintf(&s, "%s.private", configfile);
1603 /** @brief Return the path to user's personal configuration file */
1604 char *config_userconf(const char *home, const struct passwd *pw) {
1607 if(!home && !pw && !(pw = getpwuid(getuid())))
1608 disorder_fatal(0, "cannot determine our username");
1609 byte_xasprintf(&s, "%s/.disorder/passwd", home ? home : pw->pw_dir);
1613 /** @brief Return the path to user-specific system configuration */
1614 char *config_usersysconf(const struct passwd *pw) {
1618 if(!strchr(pw->pw_name, '/')) {
1619 byte_xasprintf(&s, "%s.%s", configfile, pw->pw_name);
1625 /** @brief Get a filename within the home directory
1626 * @param name Relative name
1629 char *config_get_file(const char *name) {
1630 return config_get_file2(config, name);
1633 /** @brief Order two stringlists
1634 * @param a First stringlist
1635 * @param b Second stringlist
1636 * @return <0, 0 or >0 if a<b, a=b or a>b
1638 static int stringlist_compare(const struct stringlist *a,
1639 const struct stringlist *b) {
1642 while(n < a->n && n < b->n) {
1643 if((c = strcmp(a->s[n], b->s[n])))
1649 else if(a->n > b->n)
1656 /** @brief Order two namepart definitions
1657 * @param a First namepart definition
1658 * @param b Second namepart definition
1659 * @return <0, 0 or >0 if a<b, a=b or a>b
1661 static int namepart_compare(const struct namepart *a,
1662 const struct namepart *b) {
1665 if((c = strcmp(a->part, b->part)))
1667 if((c = strcmp(a->res, b->res)))
1669 if((c = strcmp(a->replace, b->replace)))
1671 if((c = strcmp(a->context, b->context)))
1673 if(a->reflags > b->reflags)
1675 if(a->reflags < b->reflags)
1680 /** @brief Order two lists of namepart definitions
1681 * @param a First list of namepart definitions
1682 * @param b Second list of namepart definitions
1683 * @return <0, 0 or >0 if a<b, a=b or a>b
1685 static int namepartlist_compare(const struct namepartlist *a,
1686 const struct namepartlist *b) {
1689 while(n < a->n && n < b->n) {
1690 if((c = namepart_compare(&a->s[n], &b->s[n])))
1696 else if(a->n < b->n)
1703 /** @brief Verify configuration table.
1704 * @return The number of problems found
1706 int config_verify(void) {
1709 for(n = 1; n < sizeof conf / sizeof *conf; ++n)
1710 if(strcmp(conf[n-1].name, conf[n].name) >= 0) {
1711 fprintf(stderr, "%s >= %s\n", conf[n-1].name, conf[n].name);