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>
43 #include "configuration.h"
50 #include "inputline.h"
63 /** @brief Path to config file
65 * set_configfile() sets the default if it is null.
69 /** @brief Path to user's config file
71 * set_configfile() sets the default if it is null.
75 /** @brief Read user configuration
77 * If clear, the user-specific configuration is not read.
79 int config_per_user = 1;
82 /** @brief Table of audio APIs
84 * Only set in server processes.
86 const struct uaudio *const *config_uaudio_apis;
89 /** @brief Config file parser state */
91 /** @brief Filename */
94 /** @brief Line number */
97 /** @brief Configuration object under construction */
98 struct config *config;
101 /** @brief Current configuration */
102 struct config *config;
104 /** @brief One configuration item */
106 /** @brief Name as it appears in the config file */
109 /** @brief Offset in @ref config structure */
112 /** @brief Pointer to item type */
113 const struct conftype *type;
115 /** @brief Pointer to item-specific validation routine
116 * @param cs Configuration state
117 * @param nvec Length of (proposed) new value
118 * @param vec Elements of new value
119 * @return 0 on success, non-0 on error
121 * The validate function should report any error it detects.
123 int (*validate)(const struct config_state *cs,
124 int nvec, char **vec);
127 /** @brief Type of a configuration item */
129 /** @brief Pointer to function to set item
130 * @param cs Configuration state
131 * @param whoami Configuration item to set
132 * @param nvec Length of new value
133 * @param vec New value
134 * @return 0 on success, non-0 on error
136 int (*set)(const struct config_state *cs,
137 const struct conf *whoami,
138 int nvec, char **vec);
140 /** @brief Pointer to function to free item
141 * @param c Configuration structure to free an item of
142 * @param whoami Configuration item to free
144 void (*free)(struct config *c, const struct conf *whoami);
147 /** @brief Compute the address of an item */
148 #define ADDRESS(C, TYPE) ((TYPE *)((char *)(C) + whoami->offset))
149 /** @brief Return the value of an item */
150 #define VALUE(C, TYPE) (*ADDRESS(C, TYPE))
152 static int stringlist_compare(const struct stringlist *a,
153 const struct stringlist *b);
154 static int namepartlist_compare(const struct namepartlist *a,
155 const struct namepartlist *b);
157 static int set_signal(const struct config_state *cs,
158 const struct conf *whoami,
159 int nvec, char **vec) {
163 disorder_error(0, "%s:%d: '%s' requires one argument",
164 cs->path, cs->line, whoami->name);
167 if((n = find_signal(vec[0])) == -1) {
168 disorder_error(0, "%s:%d: unknown signal '%s'",
169 cs->path, cs->line, vec[0]);
172 VALUE(cs->config, int) = n;
176 static int set_collections(const struct config_state *cs,
177 const struct conf *whoami,
178 int nvec, char **vec) {
179 struct collectionlist *cl;
180 const char *root, *encoding, *module;
199 disorder_error(0, "%s:%d: '%s' requires at least one argument",
200 cs->path, cs->line, whoami->name);
203 disorder_error(0, "%s:%d: '%s' requires at most three arguments",
204 cs->path, cs->line, whoami->name);
207 /* Sanity check root */
209 disorder_error(0, "%s:%d: collection root must start with '/'",
213 if(root[1] && root[strlen(root)-1] == '/') {
214 disorder_error(0, "%s:%d: collection root must not end with '/'",
223 encoding = nl_langinfo(CODESET);
228 cl = ADDRESS(cs->config, struct collectionlist);
230 cl->s = xrealloc(cl->s, cl->n * sizeof (struct collection));
231 cl->s[cl->n - 1].module = xstrdup(module);
232 cl->s[cl->n - 1].encoding = xstrdup(encoding);
233 cl->s[cl->n - 1].root = xstrdup(root);
237 static int set_boolean(const struct config_state *cs,
238 const struct conf *whoami,
239 int nvec, char **vec) {
243 disorder_error(0, "%s:%d: '%s' takes only one argument",
244 cs->path, cs->line, whoami->name);
247 if(!strcmp(vec[0], "yes")) state = 1;
248 else if(!strcmp(vec[0], "no")) state = 0;
250 disorder_error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'",
251 cs->path, cs->line, whoami->name);
254 VALUE(cs->config, int) = state;
258 static int set_string(const struct config_state *cs,
259 const struct conf *whoami,
260 int nvec, char **vec) {
262 disorder_error(0, "%s:%d: '%s' takes only one argument",
263 cs->path, cs->line, whoami->name);
266 xfree(VALUE(cs->config, char *));
267 VALUE(cs->config, char *) = xstrdup(vec[0]);
271 static int set_integer(const struct config_state *cs,
272 const struct conf *whoami,
273 int nvec, char **vec) {
277 disorder_error(0, "%s:%d: '%s' takes only one argument",
278 cs->path, cs->line, whoami->name);
281 if(xstrtol(ADDRESS(cs->config, long), vec[0], &e, 0)) {
282 disorder_error(errno, "%s:%d: converting integer", cs->path, cs->line);
286 disorder_error(0, "%s:%d: invalid integer syntax", cs->path, cs->line);
292 static int set_stringlist_accum(const struct config_state *cs,
293 const struct conf *whoami,
294 int nvec, char **vec) {
296 struct stringlist *s;
297 struct stringlistlist *sll;
299 sll = ADDRESS(cs->config, struct stringlistlist);
305 sll->s = xrealloc(sll->s, (sll->n * sizeof (struct stringlist)));
306 s = &sll->s[sll->n - 1];
308 s->s = xmalloc((nvec + 1) * sizeof (char *));
309 for(n = 0; n < nvec; ++n)
310 s->s[n] = xstrdup(vec[n]);
314 static int set_string_accum(const struct config_state *cs,
315 const struct conf *whoami,
316 int nvec, char **vec) {
318 struct stringlist *sl;
320 sl = ADDRESS(cs->config, struct stringlist);
325 for(n = 0; n < nvec; ++n) {
327 sl->s = xrealloc(sl->s, (sl->n * sizeof (char *)));
328 sl->s[sl->n - 1] = xstrdup(vec[n]);
333 static int parse_sample_format(const struct config_state *cs,
334 struct stream_header *format,
335 int nvec, char **vec) {
340 disorder_error(0, "%s:%d: wrong number of arguments", cs->path, cs->line);
343 if(xstrtol(&t, p, &p, 0)) {
344 disorder_error(errno, "%s:%d: converting bits-per-sample",
348 if(t != 8 && t != 16) {
349 disorder_error(0, "%s:%d: bad bits-per-sample (%ld)",
350 cs->path, cs->line, t);
353 if(format) format->bits = (uint8_t)t;
355 case 'l': case 'L': t = ENDIAN_LITTLE; p++; break;
356 case 'b': case 'B': t = ENDIAN_BIG; p++; break;
357 default: t = ENDIAN_NATIVE; break;
359 if(format) format->endian = (uint8_t)t;
361 disorder_error(errno, "%s:%d: expected `/' after bits-per-sample",
366 if(xstrtol(&t, p, &p, 0)) {
367 disorder_error(errno, "%s:%d: converting sample-rate", cs->path, cs->line);
370 if(t < 1 || t > INT_MAX) {
371 disorder_error(0, "%s:%d: silly sample-rate (%ld)", cs->path, cs->line, t);
374 if(format) format->rate = t;
376 disorder_error(0, "%s:%d: expected `/' after sample-rate",
381 if(xstrtol(&t, p, &p, 0)) {
382 disorder_error(errno, "%s:%d: converting channels", cs->path, cs->line);
386 disorder_error(0, "%s:%d: silly number (%ld) of channels",
387 cs->path, cs->line, t);
390 if(format) format->channels = (uint8_t)t;
392 disorder_error(0, "%s:%d: junk after channels", cs->path, cs->line);
398 static int set_sample_format(const struct config_state *cs,
399 const struct conf *whoami,
400 int nvec, char **vec) {
401 return parse_sample_format(cs, ADDRESS(cs->config, struct stream_header),
405 static int set_namepart(const struct config_state *cs,
406 const struct conf *whoami,
407 int nvec, char **vec) {
408 struct namepartlist *npl = ADDRESS(cs->config, struct namepartlist);
411 char errstr[RXCERR_LEN];
416 disorder_error(0, "%s:%d: namepart needs at least 3 arguments",
421 disorder_error(0, "%s:%d: namepart needs at most 5 arguments",
425 reflags = nvec >= 5 ? regsub_flags(vec[4]) : 0;
426 if(!(re = regexp_compile(vec[1], regsub_compile_options(reflags),
427 errstr, sizeof(errstr), &erroffset)))
429 disorder_error(0, "%s:%d: compiling regexp /%s/: %s (offset %zu)",
430 cs->path, cs->line, vec[1], errstr, erroffset);
433 npl->s = xrealloc(npl->s, (npl->n + 1) * sizeof (struct namepart));
434 npl->s[npl->n].part = xstrdup(vec[0]);
435 npl->s[npl->n].re = re;
436 npl->s[npl->n].res = xstrdup(vec[1]);
437 npl->s[npl->n].replace = xstrdup(vec[2]);
438 npl->s[npl->n].context = xstrdup(vec[3]);
439 npl->s[npl->n].reflags = reflags;
441 /* XXX a bit of a bodge; relies on there being very few parts. */
442 for(n = 0; (n < cs->config->nparts
443 && strcmp(cs->config->parts[n], vec[0])); ++n)
445 if(n >= cs->config->nparts) {
446 cs->config->parts = xrealloc(cs->config->parts,
447 (cs->config->nparts + 1) * sizeof (char *));
448 cs->config->parts[cs->config->nparts++] = xstrdup(vec[0]);
453 static int set_transform(const struct config_state *cs,
454 const struct conf *whoami,
455 int nvec, char **vec) {
456 struct transformlist *tl = ADDRESS(cs->config, struct transformlist);
458 char errstr[RXCERR_LEN];
463 disorder_error(0, "%s:%d: transform needs at least 3 arguments",
468 disorder_error(0, "%s:%d: transform needs at most 5 arguments",
472 reflags = (nvec >= 5 ? regsub_flags(vec[4]) : 0);
473 if(!(re = regexp_compile(vec[1], regsub_compile_options(reflags),
474 errstr, sizeof(errstr), &erroffset)))
476 disorder_error(0, "%s:%d: compiling regexp /%s/: %s (offset %zu)",
477 cs->path, cs->line, vec[1], errstr, erroffset);
480 tl->t = xrealloc(tl->t, (tl->n + 1) * sizeof (struct namepart));
481 tl->t[tl->n].type = xstrdup(vec[0]);
482 tl->t[tl->n].context = xstrdup(vec[3] ? vec[3] : "*");
483 tl->t[tl->n].re = re;
484 tl->t[tl->n].replace = xstrdup(vec[2]);
485 tl->t[tl->n].flags = reflags;
490 static int set_rights(const struct config_state *cs,
491 const struct conf *whoami,
492 int nvec, char **vec) {
494 disorder_error(0, "%s:%d: '%s' requires one argument",
495 cs->path, cs->line, whoami->name);
498 if(parse_rights(vec[0], 0, 1)) {
499 disorder_error(0, "%s:%d: invalid rights string '%s'",
500 cs->path, cs->line, vec[0]);
503 return set_string(cs, whoami, nvec, vec);
506 static int set_netaddress(const struct config_state *cs,
507 const struct conf *whoami,
508 int nvec, char **vec) {
509 struct netaddress *na = ADDRESS(cs->config, struct netaddress);
511 if(netaddress_parse(na, nvec, vec)) {
512 disorder_error(0, "%s:%d: invalid network address", cs->path, cs->line);
520 static void free_none(struct config attribute((unused)) *c,
521 const struct conf attribute((unused)) *whoami) {
524 static void free_string(struct config *c,
525 const struct conf *whoami) {
526 xfree(VALUE(c, char *));
527 VALUE(c, char *) = 0;
530 static void free_stringlist(struct config *c,
531 const struct conf *whoami) {
533 struct stringlist *sl = ADDRESS(c, struct stringlist);
535 for(n = 0; n < sl->n; ++n)
540 static void free_stringlistlist(struct config *c,
541 const struct conf *whoami) {
543 struct stringlistlist *sll = ADDRESS(c, struct stringlistlist);
544 struct stringlist *sl;
546 for(n = 0; n < sll->n; ++n) {
548 for(m = 0; m < sl->n; ++m)
555 static void free_collectionlist(struct config *c,
556 const struct conf *whoami) {
557 struct collectionlist *cll = ADDRESS(c, struct collectionlist);
558 struct collection *cl;
561 for(n = 0; n < cll->n; ++n) {
570 static void free_namepartlist(struct config *c,
571 const struct conf *whoami) {
572 struct namepartlist *npl = ADDRESS(c, struct namepartlist);
576 for(n = 0; n < npl->n; ++n) {
587 static void free_transformlist(struct config *c,
588 const struct conf *whoami) {
589 struct transformlist *tl = ADDRESS(c, struct transformlist);
593 for(n = 0; n < tl->n; ++n) {
603 static void free_netaddress(struct config *c,
604 const struct conf *whoami) {
605 struct netaddress *na = ADDRESS(c, struct netaddress);
610 /* configuration types */
612 static const struct conftype
613 type_signal = { set_signal, free_none },
614 type_collections = { set_collections, free_collectionlist },
615 type_boolean = { set_boolean, free_none },
616 type_string = { set_string, free_string },
617 type_integer = { set_integer, free_none },
618 type_stringlist_accum = { set_stringlist_accum, free_stringlistlist },
619 type_string_accum = { set_string_accum, free_stringlist },
620 type_sample_format = { set_sample_format, free_none },
621 type_namepart = { set_namepart, free_namepartlist },
622 type_transform = { set_transform, free_transformlist },
623 type_netaddress = { set_netaddress, free_netaddress },
624 type_rights = { set_rights, free_string };
626 /* specific validation routine */
628 /** @brief Perform a test on a filename
629 * @param test Test function to call on mode bits
630 * @param what Type of file sought
632 * If @p test returns 0 then the file is not a @p what and an error
633 * is reported and -1 is returned.
635 #define VALIDATE_FILE(test, what) do { \
639 for(n = 0; n < nvec; ++n) { \
640 if(stat(vec[n], &sb) < 0) { \
641 disorder_error(errno, "%s:%d: %s", \
642 cs->path, cs->line, vec[n]); \
645 if(!test(sb.st_mode)) { \
646 disorder_error(0, "%s:%d: %s is not a %s", \
647 cs->path, cs->line, vec[n], what); \
653 /** @brief Validate an absolute path
654 * @param cs Configuration state
655 * @param nvec Length of (proposed) new value
656 * @param vec Elements of new value
657 * @return 0 on success, non-0 on error
659 static int validate_isabspath(const struct config_state *cs,
660 int nvec, char **vec) {
663 for(n = 0; n < nvec; ++n)
664 if(vec[n][0] != '/') {
665 disorder_error(0, "%s:%d: %s: not an absolute path",
666 cs->path, cs->line, vec[n]);
672 /** @brief Validate an existing directory
673 * @param cs Configuration state
674 * @param nvec Length of (proposed) new value
675 * @param vec Elements of new value
676 * @return 0 on success, non-0 on error
678 static int validate_isdir(const struct config_state *cs,
679 int nvec, char **vec) {
680 VALIDATE_FILE(S_ISDIR, "directory");
684 /** @brief Validate an existing regular file
685 * @param cs Configuration state
686 * @param nvec Length of (proposed) new value
687 * @param vec Elements of new value
688 * @return 0 on success, non-0 on error
690 static int validate_isreg(const struct config_state *cs,
691 int nvec, char **vec) {
692 VALIDATE_FILE(S_ISREG, "regular file");
696 /** @brief Validate a player pattern
697 * @param cs Configuration state
698 * @param nvec Length of (proposed) new value
699 * @param vec Elements of new value
700 * @return 0 on success, non-0 on error
702 static int validate_player(const struct config_state *cs,
704 char attribute((unused)) **vec) {
705 if(nvec && nvec < 2) {
706 disorder_error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
713 /** @brief Validate a track length pattern
714 * @param cs Configuration state
715 * @param nvec Length of (proposed) new value
716 * @param vec Elements of new value
717 * @return 0 on success, non-0 on error
719 static int validate_tracklength(const struct config_state *cs,
721 char attribute((unused)) **vec) {
722 if(nvec && nvec < 2) {
723 disorder_error(0, "%s:%d: should be at least 'tracklength PATTERN MODULE'",
730 /** @brief Common code for validating integer values
731 * @param cs Configuration state
732 * @param nvec Length of (proposed) new value
733 * @param vec Elements of new value
734 * @param n_out Where to put the value
736 static int common_validate_integer(const struct config_state *cs,
737 int nvec, char **vec, long *n_out) {
741 disorder_error(0, "%s:%d: missing argument", cs->path, cs->line);
745 disorder_error(0, "%s:%d: too many arguments", cs->path, cs->line);
748 if(xstrtol(n_out, vec[0], 0, 0)) {
749 disorder_error(0, "%s:%d: %s", cs->path, cs->line,
750 format_error(ec_errno, errno, errbuf, sizeof errbuf));
756 /** @brief Validate a non-negative (@c long) integer
757 * @param cs Configuration state
758 * @param nvec Length of (proposed) new value
759 * @param vec Elements of new value
760 * @return 0 on success, non-0 on error
762 static int validate_non_negative(const struct config_state *cs,
763 int nvec, char **vec) {
765 if(common_validate_integer(cs, nvec, vec, &n)) return -1;
767 disorder_error(0, "%s:%d: must not be negative", cs->path, cs->line);
773 /** @brief Validate a positive (@c long) integer
774 * @param cs Configuration state
775 * @param nvec Length of (proposed) new value
776 * @param vec Elements of new value
777 * @return 0 on success, non-0 on error
779 static int validate_positive(const struct config_state *cs,
780 int nvec, char **vec) {
782 if(common_validate_integer(cs, nvec, vec, &n)) return -1;
784 disorder_error(0, "%s:%d: must be positive", cs->path, cs->line);
791 /** @brief Validate a system username
792 * @param cs Configuration state
793 * @param nvec Length of (proposed) new value
794 * @param vec Elements of new value
795 * @return 0 on success, non-0 on error
797 static int validate_isauser(const struct config_state *cs,
798 int attribute((unused)) nvec,
800 if(!getpwnam(vec[0])) {
801 disorder_error(0, "%s:%d: no such user as '%s'", cs->path, cs->line, vec[0]);
808 /** @brief Validate a sample format string
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_sample_format(const struct config_state *cs,
815 int attribute((unused)) nvec,
817 return parse_sample_format(cs, 0, nvec, vec);
820 /** @brief Validate anything
821 * @param cs Configuration state
822 * @param nvec Length of (proposed) new value
823 * @param vec Elements of new value
826 static int validate_any(const struct config_state attribute((unused)) *cs,
827 int attribute((unused)) nvec,
828 char attribute((unused)) **vec) {
832 /** @brief Validate a URL
833 * @param cs Configuration state
834 * @param nvec Length of (proposed) new value
835 * @param vec Elements of new value
836 * @return 0 on success, non-0 on error
840 static int validate_url(const struct config_state attribute((unused)) *cs,
841 int attribute((unused)) nvec,
845 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
846 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
848 n = strspn(s, ("abcdefghijklmnopqrstuvwxyz"
849 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
852 disorder_error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
855 if(!strncmp(s, "http:", 5)
856 || !strncmp(s, "https:", 6)) {
858 /* we only do a rather cursory check */
859 if(strncmp(s, "//", 2)) {
860 disorder_error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
867 /** @brief Validate an alias pattern
868 * @param cs Configuration state
869 * @param nvec Length of (proposed) new value
870 * @param vec Elements of new value
871 * @return 0 on success, non-0 on error
873 static int validate_alias(const struct config_state *cs,
877 int in_brackets = 0, c;
880 disorder_error(0, "%s:%d: missing argument", cs->path, cs->line);
884 disorder_error(0, "%s:%d: too many arguments", cs->path, cs->line);
888 while((c = (unsigned char)*s++)) {
892 else if(!isalnum(c)) {
893 disorder_error(0, "%s:%d: invalid part name in alias expansion in '%s'",
894 cs->path, cs->line, vec[0]);
902 } else if(c == '\\') {
903 if(!(c = (unsigned char)*s++)) {
904 disorder_error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
905 cs->path, cs->line, vec[0]);
907 } else if(c != '\\' && c != '{') {
908 disorder_error(0, "%s:%d: invalid escape in alias expansion in '%s'",
909 cs->path, cs->line, vec[0]);
918 "%s:%d: unterminated part name in alias expansion in '%s'",
919 cs->path, cs->line, vec[0]);
925 /** @brief Validate a hash algorithm name
926 * @param cs Configuration state
927 * @param nvec Length of (proposed) new value
928 * @param vec Elements of new value
929 * @return 0 on success, non-0 on error
931 static int validate_algo(const struct config_state attribute((unused)) *cs,
935 disorder_error(0, "%s:%d: invalid algorithm specification", cs->path, cs->line);
938 if(!valid_authhash(vec[0])) {
939 disorder_error(0, "%s:%d: unsuported algorithm '%s'", cs->path, cs->line, vec[0]);
946 /** @brief Validate a playback backend name
947 * @param cs Configuration state
948 * @param nvec Length of (proposed) new value
949 * @param vec Elements of new value
950 * @return 0 on success, non-0 on error
952 static int validate_backend(const struct config_state attribute((unused)) *cs,
957 disorder_error(0, "%s:%d: invalid sound API specification", cs->path, cs->line);
960 if(!strcmp(vec[0], "network")) {
961 disorder_error(0, "'api network' is deprecated; use 'api rtp'");
964 if(config_uaudio_apis) {
965 for(n = 0; config_uaudio_apis[n]; ++n)
966 if(!strcmp(vec[0], config_uaudio_apis[n]->name))
968 disorder_error(0, "%s:%d: unrecognized sound API '%s'", cs->path, cs->line, vec[0]);
971 /* In non-server processes we have no idea what's valid */
976 /** @brief Validate a pause mode string
977 * @param cs Configuration state
978 * @param nvec Length of (proposed) new value
979 * @param vec Elements of new value
980 * @return 0 on success, non-0 on error
982 static int validate_pausemode(const struct config_state attribute((unused)) *cs,
985 if(nvec == 1 && (!strcmp(vec[0], "silence") || !strcmp(vec[0], "suspend")))
987 disorder_error(0, "%s:%d: invalid pause mode", cs->path, cs->line);
991 /** @brief Validate an MTU-discovery setting
992 * @param cs Configuration state
993 * @param nvec Length of (proposed) new value
994 * @param vec Elements of new value
995 * @return 0 on success, non-0 on error
997 static int validate_mtu_discovery(const struct config_state attribute((unused)) *cs,
1001 (!strcmp(vec[0], "default") ||
1002 !strcmp(vec[0], "yes") ||
1003 !strcmp(vec[0], "no")))
1005 disorder_error(0, "%s:%d: invalid MTU-discovery setting", cs->path, cs->line);
1009 /** @brief Validate a destination network address
1010 * @param cs Configuration state
1011 * @param nvec Length of (proposed) new value
1012 * @param vec Elements of new value
1013 * @return 0 on success, non-0 on error
1015 * By a destination address, it is meant that it must not be a wildcard
1018 static int validate_destaddr(const struct config_state attribute((unused)) *cs,
1021 struct netaddress na[1];
1023 if(netaddress_parse(na, nvec, vec)) {
1024 disorder_error(0, "%s:%d: invalid network address", cs->path, cs->line);
1028 disorder_error(0, "%s:%d: destination address required", cs->path, cs->line);
1035 /** @brief Validate an internet address
1036 * @param cs Configuration state
1037 * @param nvec Length of (proposed) new value
1038 * @param vec Elements of new value
1039 * @return 0 on success, non-0 on error
1041 * By a destination address, it is meant that it must be either IPv4 or IPv6.
1043 static int validate_inetaddr(const struct config_state *cs,
1044 int nvec, char **vec) {
1045 struct netaddress na[1];
1047 if(netaddress_parse(na, nvec, vec)) {
1048 disorder_error(0, "%s:%d: invalid network address", cs->path, cs->line);
1052 case AF_INET: case AF_INET6: case AF_UNSPEC: break;
1054 disorder_error(0, "%s:%d: must be an intenet address",
1055 cs->path, cs->line);
1061 /** @brief Item name and and offset */
1062 #define C(x) #x, offsetof(struct config, x)
1063 /** @brief Item name and and offset */
1064 #define C2(x,y) #x, offsetof(struct config, y)
1066 /** @brief All configuration items */
1067 static const struct conf conf[] = {
1068 { C(alias), &type_string, validate_alias },
1070 { C(api), &type_string, validate_backend },
1072 { C(authorization_algorithm), &type_string, validate_algo },
1073 { C(broadcast), &type_netaddress, validate_destaddr },
1074 { C(broadcast_from), &type_netaddress, validate_any },
1075 { C(channel), &type_string, validate_any },
1076 { C(checkpoint_kbyte), &type_integer, validate_non_negative },
1077 { C(checkpoint_min), &type_integer, validate_non_negative },
1078 { C(collection), &type_collections, validate_any },
1079 { C(connect), &type_netaddress, validate_destaddr },
1080 { C(cookie_key_lifetime), &type_integer, validate_positive },
1081 { C(cookie_login_lifetime), &type_integer, validate_positive },
1082 { C(dbversion), &type_integer, validate_positive },
1083 { C(default_rights), &type_rights, validate_any },
1084 { C(device), &type_string, validate_any },
1085 { C(history), &type_integer, validate_positive },
1087 { C(home), &type_string, validate_isabspath },
1089 { C(listen), &type_netaddress, validate_any },
1090 { C(mail_sender), &type_string, validate_any },
1091 { C(mixer), &type_string, validate_any },
1092 { C(mount_rescan), &type_boolean, validate_any },
1093 { C(multicast_loop), &type_boolean, validate_any },
1094 { C(multicast_ttl), &type_integer, validate_non_negative },
1095 { C(namepart), &type_namepart, validate_any },
1096 { C(new_bias), &type_integer, validate_positive },
1097 { C(new_bias_age), &type_integer, validate_positive },
1098 { C(new_max), &type_integer, validate_positive },
1099 { C2(nice, nice_rescan), &type_integer, validate_non_negative },
1100 { C(nice_rescan), &type_integer, validate_non_negative },
1101 { C(nice_server), &type_integer, validate_any },
1102 { C(nice_speaker), &type_integer, validate_any },
1103 { C(noticed_history), &type_integer, validate_positive },
1104 { C(password), &type_string, validate_any },
1105 { C(pause_mode), &type_string, validate_pausemode },
1106 { C(player), &type_stringlist_accum, validate_player },
1107 { C(playlist_lock_timeout), &type_integer, validate_positive },
1108 { C(playlist_max) , &type_integer, validate_positive },
1109 { C(plugins), &type_string_accum, validate_isdir },
1110 { C(queue_pad), &type_integer, validate_positive },
1111 { C(refresh), &type_integer, validate_positive },
1112 { C(refresh_min), &type_integer, validate_non_negative },
1113 { C(reminder_interval), &type_integer, validate_positive },
1114 { C(remote_userman), &type_boolean, validate_any },
1115 { C(replay_min), &type_integer, validate_non_negative },
1116 { C(rtp_always_request), &type_boolean, validate_any },
1117 { C(rtp_delay_threshold), &type_integer, validate_positive },
1118 { C(rtp_instance_name), &type_string, validate_any },
1119 { C(rtp_max_payload), &type_integer, validate_positive },
1120 { C(rtp_maxbuffer), &type_integer, validate_non_negative },
1121 { C(rtp_minbuffer), &type_integer, validate_non_negative },
1122 { C(rtp_mode), &type_string, validate_any },
1123 { C(rtp_mtu_discovery), &type_string, validate_mtu_discovery },
1124 { C(rtp_rcvbuf), &type_integer, validate_non_negative },
1125 { C(rtp_request_address), &type_netaddress, validate_inetaddr },
1126 { C(rtp_verbose), &type_boolean, validate_any },
1127 { C(sample_format), &type_sample_format, validate_sample_format },
1128 { C(scratch), &type_string_accum, validate_isreg },
1130 { C(sendmail), &type_string, validate_isabspath },
1132 { C(short_display), &type_integer, validate_positive },
1133 { C(signal), &type_signal, validate_any },
1134 { C(smtp_server), &type_string, validate_any },
1135 { C(sox_generation), &type_integer, validate_non_negative },
1137 { C2(speaker_backend, api), &type_string, validate_backend },
1139 { C(speaker_command), &type_string, validate_any },
1140 { C(stopword), &type_string_accum, validate_any },
1141 { C(templates), &type_string_accum, validate_isdir },
1142 { C(tracklength), &type_stringlist_accum, validate_tracklength },
1143 { C(transform), &type_transform, validate_any },
1144 { C(url), &type_string, validate_url },
1146 { C(user), &type_string, validate_isauser },
1148 { C(username), &type_string, validate_any },
1151 /** @brief Find a configuration item's definition by key */
1152 static const struct conf *find(const char *key) {
1155 if((n = TABLE_FIND(conf, name, key)) < 0)
1160 /** @brief Set a new configuration value
1161 * @param cs Configuration state
1162 * @param nvec Length of @p vec
1163 * @param vec Name and new value
1164 * @return 0 on success, non-0 on error.
1166 * @c vec[0] is the name, the rest is the value.
1168 static int config_set(const struct config_state *cs,
1169 int nvec, char **vec) {
1170 const struct conf *which;
1172 D(("config_set %s", vec[0]));
1173 if(!(which = find(vec[0]))) {
1174 disorder_error(0, "%s:%d: unknown configuration key '%s'",
1175 cs->path, cs->line, vec[0]);
1178 return (which->validate(cs, nvec - 1, vec + 1)
1179 || which->type->set(cs, which, nvec - 1, vec + 1));
1182 /** @brief Set a configuration item from parameters
1183 * @param cs Configuration state
1184 * @param which Item to set
1185 * @param ... Value as strings, terminated by (char *)NULL
1186 * @return 0 on success, non-0 on error
1188 static int config_set_args(const struct config_state *cs,
1189 const char *which, ...) {
1196 vector_append(v, (char *)which);
1197 va_start(ap, which);
1198 while((s = va_arg(ap, char *)))
1199 vector_append(v, s);
1201 vector_terminate(v);
1202 rc = config_set(cs, v->nvec, v->vec);
1207 /** @brief Error callback used by config_include()
1208 * @param msg Error message
1209 * @param u User data (@ref config_state)
1211 static void config_error(const char *msg, void *u) {
1212 const struct config_state *cs = u;
1214 disorder_error(0, "%s:%d: %s", cs->path, cs->line, msg);
1217 /** @brief Include a file by name
1218 * @param c Configuration to update
1219 * @param path Path to read
1220 * @return 0 on success, non-0 on error
1222 static int config_include(struct config *c, const char *path) {
1224 char *buffer, *inputbuffer, **vec;
1226 struct config_state cs;
1231 D(("%s: reading configuration", path));
1232 if(!(fp = fopen(path, "r"))) {
1233 disorder_error(errno, "error opening %s", path);
1236 while(!inputline(path, fp, &inputbuffer, '\n')) {
1238 if(!(buffer = mb2utf8(inputbuffer))) {
1239 disorder_error(errno, "%s:%d: cannot convert to UTF-8", cs.path, cs.line);
1245 if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES,
1246 config_error, &cs))) {
1252 /* 'include' is special-cased */
1253 if(!strcmp(vec[0], "include")) {
1255 disorder_error(0, "%s:%d: must be 'include PATH'", cs.path, cs.line);
1258 config_include(c, vec[1]);
1260 ret |= config_set(&cs, n, vec);
1262 for(n = 0; vec[n]; ++n) xfree(vec[n]);
1267 disorder_error(errno, "error reading %s", path);
1274 /** @brief Default stopword setting */
1275 static const char *const default_stopwords[] = {
1334 #define NDEFAULT_STOPWORDS (sizeof default_stopwords / sizeof *default_stopwords)
1336 /** @brief Default player patterns */
1337 static const char *const default_players[] = {
1343 #define NDEFAULT_PLAYERS (sizeof default_players / sizeof *default_players)
1345 /** @brief Make a new default configuration
1346 * @return New configuration
1348 static struct config *config_default(void) {
1349 struct config *c = xmalloc(sizeof *c);
1351 const char *logname;
1354 struct config_state cs;
1357 cs.path = "<internal>";
1360 /* Strings had better be xstrdup'd as they will get freed at some point. */
1363 c->home = xstrdup(pkgstatedir);
1368 DWORD bufsize = sizeof buffer;
1369 if(!GetUserNameA(buffer, &bufsize))
1370 disorder_fatal(0, "cannot determine our username");
1371 c->username = xstrdup(buffer);
1374 if(!(pw = getpwuid(getuid())))
1375 disorder_fatal(0, "cannot determine our username");
1376 logname = pw->pw_name;
1377 c->username = xstrdup(logname);
1382 c->signal = SIGKILL;
1384 c->signal = SIGTERM;
1386 c->alias = xstrdup("{/artist}{/album}{/title}{ext}");
1387 c->device = xstrdup("default");
1388 c->nice_rescan = 10;
1389 c->speaker_command = 0;
1390 c->sample_format.bits = 16;
1391 c->sample_format.rate = 44100;
1392 c->sample_format.channels = 2;
1393 c->sample_format.endian = ENDIAN_NATIVE;
1395 c->replay_min = 8 * 3600;
1397 c->multicast_ttl = 1;
1398 c->multicast_loop = 1;
1399 c->authorization_algorithm = xstrdup("sha1");
1400 c->noticed_history = 31;
1401 c->short_display = 32;
1405 c->cookie_login_lifetime = 86400;
1406 c->cookie_key_lifetime = 86400 * 7;
1408 if(sendmail_binary[0] && strcmp(sendmail_binary, "none"))
1409 c->sendmail = xstrdup(sendmail_binary);
1411 c->smtp_server = xstrdup("127.0.0.1");
1413 c->reminder_interval = 600; /* 10m */
1414 c->new_bias_age = 7 * 86400; /* 1 week */
1415 c->new_bias = 4500000; /* 50 times the base weight */
1416 c->sox_generation = DEFAULT_SOX_GENERATION;
1417 c->playlist_max = INT_MAX; /* effectively no limit */
1418 c->playlist_lock_timeout = 10; /* 10s */
1419 c->mount_rescan = 1;
1420 /* Default stopwords */
1421 if(config_set(&cs, (int)NDEFAULT_STOPWORDS, (char **)default_stopwords))
1423 /* Default player configuration */
1424 for(n = 0; n < NDEFAULT_PLAYERS; ++n) {
1425 if(config_set_args(&cs, "player",
1426 default_players[n], "execraw", "disorder-decode", (char *)0))
1428 if(config_set_args(&cs, "tracklength",
1429 default_players[n], "disorder-tracklength", (char *)0))
1432 c->broadcast.af = -1;
1433 c->broadcast_from.af = -1;
1436 c->rtp_mode = xstrdup("auto");
1437 c->rtp_max_payload = -1;
1438 c->rtp_mtu_discovery = xstrdup("default");
1443 /** @brief Construct a filename
1444 * @param c Configuration
1445 * @param name Base filename
1446 * @return Full filename
1448 * Usually use config_get_file() instead.
1450 char *config_get_file2(struct config *c, const char *name) {
1453 byte_xasprintf(&s, "%s/%s", c->home, name);
1458 /** @brief Set the default configuration file */
1459 static void set_configfile(void) {
1464 configfile = getenv("DISORDER_CONFIG");
1466 byte_xasprintf(&configfile, "%s/config", pkgconfdir);
1469 if(!userconfigfile && config_per_user) {
1470 if((t = getenv("DISORDER_USERCONFIG"))) userconfigfile = xstrdup(t);
1471 else if(!(userconfigfile = profile_filename("passwd")))
1472 disorder_fatal(0, "failed to find user profile directory");
1476 /** @brief Free a configuration object
1477 * @param c Configuration to free
1479 * @p c is indeterminate after this function is called.
1481 void config_free(struct config *c) {
1485 for(n = 0; n < (int)(sizeof conf / sizeof *conf); ++n)
1486 conf[n].type->free(c, &conf[n]);
1487 for(n = 0; n < c->nparts; ++n)
1494 /** @brief Set post-parse defaults
1495 * @param c Configuration to update
1496 * @param server True when running in the server
1498 * If @p server is set then certain parts of the configuration are more
1499 * strictly validated.
1501 static void config_postdefaults(struct config *c,
1503 struct config_state cs;
1504 const struct conf *whoami;
1507 static const char *namepart[][4] = {
1508 { "title", "/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
1509 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1510 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1511 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1512 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1514 #define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1516 static const char *transform[][5] = {
1517 { "track", "^.*/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
1518 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1519 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1520 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1521 { "dir", "[[:punct:]]", "", "sort", "g", }
1523 #define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1525 cs.path = "<internal>";
1528 if(!c->namepart.n) {
1529 whoami = find("namepart");
1530 for(n = 0; n < NNAMEPART; ++n)
1531 set_namepart(&cs, whoami, 4, (char **)namepart[n]);
1533 if(!c->transform.n) {
1534 whoami = find("transform");
1535 for(n = 0; n < NTRANSFORM; ++n)
1536 set_transform(&cs, whoami, 5, (char **)transform[n]);
1539 if(c->speaker_command)
1540 c->api = xstrdup("command");
1541 else if(c->broadcast.af != -1)
1542 c->api = xstrdup("rtp");
1544 else if(config_uaudio_apis)
1545 c->api = xstrdup(uaudio_default(config_uaudio_apis,
1546 UAUDIO_API_SERVER)->name);
1549 c->api = xstrdup("<none>");
1551 if(!strcmp(c->api, "network"))
1552 c->api = xstrdup("rtp");
1554 if(!strcmp(c->api, "command") && !c->speaker_command)
1555 disorder_fatal(0, "'api command' but speaker_command is not set");
1556 if((!strcmp(c->api, "rtp")) &&
1557 c->broadcast.af == -1 && strcmp(c->rtp_mode, "request"))
1558 disorder_fatal(0, "'api rtp' but broadcast is not set "
1559 "and mode is not not 'request'");
1561 /* Override sample format */
1562 if(!strcmp(c->api, "rtp")) {
1563 c->sample_format.rate = 44100;
1564 c->sample_format.channels = 2;
1565 c->sample_format.bits = 16;
1566 c->sample_format.endian = ENDIAN_NATIVE;
1568 if(!strcmp(c->api, "coreaudio")) {
1569 c->sample_format.rate = 44100;
1570 c->sample_format.channels = 2;
1571 c->sample_format.bits = 16;
1572 c->sample_format.endian = ENDIAN_NATIVE;
1574 if(!c->default_rights) {
1575 rights_type r = RIGHTS__MASK & ~(RIGHT_ADMIN|RIGHT_REGISTER
1577 |RIGHT_SCRATCH__MASK
1578 |RIGHT_REMOVE__MASK);
1579 r |= RIGHT_SCRATCH_ANY|RIGHT_MOVE_ANY|RIGHT_REMOVE_ANY;
1580 c->default_rights = rights_string(r);
1584 /** @brief (Re-)read the config file
1585 * @param server If set, do extra checking
1586 * @param oldconfig Old configuration for compatibility check
1587 * @return 0 on success, non-0 on error
1589 * If @p oldconfig is set, then certain compatibility checks are done between
1590 * the old and new configurations.
1592 int config_read(int server,
1593 const struct config *oldconfig) {
1596 struct passwd *pw = NULL;
1599 c = config_default();
1600 /* standalone client installs might not have a global config file */
1602 if(access(configfile, F_OK) == 0)
1603 if(config_include(c, configfile))
1605 /* if we can read the private config file, do */
1606 if((privconf = config_private())
1607 && access(privconf, R_OK) == 0
1608 && config_include(c, privconf))
1611 /* if there's a per-user system config file for this user, read it */
1612 if(config_per_user) {
1614 if(!(pw = getpwuid(getuid())))
1615 disorder_fatal(0, "cannot determine our username");
1616 if((privconf = config_usersysconf(pw))
1617 && access(privconf, F_OK) == 0
1618 && config_include(c, privconf))
1622 /* if we have a password file, read it */
1623 if(access(userconfigfile, F_OK) == 0
1624 && config_include(c, userconfigfile))
1627 /* install default namepart and transform settings */
1628 config_postdefaults(c, server);
1632 if(strcmp(c->home, oldconfig->home)) {
1633 disorder_error(0, "'home' cannot be changed without a restart");
1637 if(strcmp(c->alias, oldconfig->alias)) {
1638 disorder_error(0, "'alias' cannot be changed without a restart");
1641 if(strcmp(c->user, oldconfig->user)) {
1642 disorder_error(0, "'user' cannot be changed without a restart");
1645 if(c->nice_speaker != oldconfig->nice_speaker) {
1646 disorder_error(0, "'nice_speaker' cannot be changed without a restart");
1647 /* ...but we accept the new config anyway */
1649 if(c->nice_server != oldconfig->nice_server) {
1650 disorder_error(0, "'nice_server' cannot be changed without a restart");
1651 /* ...but we accept the new config anyway */
1653 if(namepartlist_compare(&c->namepart, &oldconfig->namepart)) {
1654 disorder_error(0, "'namepart' settings cannot be changed without a restart");
1657 if(stringlist_compare(&c->stopword, &oldconfig->stopword)) {
1658 disorder_error(0, "'stopword' settings cannot be changed without a restart");
1662 disorder_error(0, "not installing incompatible new configuration");
1666 /* everything is good so we shall use the new config */
1667 config_free(config);
1668 /* warn about obsolete directives */
1673 /** @brief Return the path to the private configuration file */
1674 char *config_private(void) {
1680 if((s = getenv("DISORDER_PRIVCONFIG"))) return xstrdup(s);
1682 byte_xasprintf(&s, "%s.private", configfile);
1688 /** @brief Return the path to user-specific system configuration */
1689 char *config_usersysconf(const struct passwd *pw) {
1693 if((s = getenv("DISORDER_USERCONFIG_SYS")))
1695 else if(!strchr(pw->pw_name, '/')) {
1696 byte_xasprintf(&s, "%s.%s", configfile, pw->pw_name);
1702 /** @brief Get a filename within the home directory
1703 * @param name Relative name
1706 char *config_get_file(const char *name) {
1707 return config_get_file2(config, name);
1711 /** @brief Order two stringlists
1712 * @param a First stringlist
1713 * @param b Second stringlist
1714 * @return <0, 0 or >0 if a<b, a=b or a>b
1716 static int stringlist_compare(const struct stringlist *a,
1717 const struct stringlist *b) {
1720 while(n < a->n && n < b->n) {
1721 if((c = strcmp(a->s[n], b->s[n])))
1727 else if(a->n > b->n)
1733 /** @brief Order two namepart definitions
1734 * @param a First namepart definition
1735 * @param b Second namepart definition
1736 * @return <0, 0 or >0 if a<b, a=b or a>b
1738 static int namepart_compare(const struct namepart *a,
1739 const struct namepart *b) {
1742 if((c = strcmp(a->part, b->part)))
1744 if((c = strcmp(a->res, b->res)))
1746 if((c = strcmp(a->replace, b->replace)))
1748 if((c = strcmp(a->context, b->context)))
1750 if(a->reflags > b->reflags)
1752 if(a->reflags < b->reflags)
1757 /** @brief Order two lists of namepart definitions
1758 * @param a First list of namepart definitions
1759 * @param b Second list of namepart definitions
1760 * @return <0, 0 or >0 if a<b, a=b or a>b
1762 static int namepartlist_compare(const struct namepartlist *a,
1763 const struct namepartlist *b) {
1766 while(n < a->n && n < b->n) {
1767 if((c = namepart_compare(&a->s[n], &b->s[n])))
1773 else if(a->n < b->n)
1779 /** @brief Verify configuration table.
1780 * @return The number of problems found
1782 int config_verify(void) {
1785 for(n = 1; n < sizeof conf / sizeof *conf; ++n)
1786 if(strcmp(conf[n-1].name, conf[n].name) >= 0) {
1787 fprintf(stderr, "%s >= %s\n", conf[n-1].name, conf[n].name);