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>
46 #include "configuration.h"
52 #include "inputline.h"
65 /** @brief Path to config file
67 * set_configfile() sets the default if it is null.
71 /** @brief Read user configuration
73 * If clear, the user-specific configuration is not read.
75 int config_per_user = 1;
78 /** @brief Table of audio APIs
80 * Only set in server processes.
82 const struct uaudio *const *config_uaudio_apis;
85 /** @brief Config file parser state */
87 /** @brief Filename */
90 /** @brief Line number */
93 /** @brief Configuration object under construction */
94 struct config *config;
97 /** @brief Current configuration */
98 struct config *config;
100 /** @brief One configuration item */
102 /** @brief Name as it appears in the config file */
105 /** @brief Offset in @ref config structure */
108 /** @brief Pointer to item type */
109 const struct conftype *type;
111 /** @brief Pointer to item-specific validation routine
112 * @param cs Configuration state
113 * @param nvec Length of (proposed) new value
114 * @param vec Elements of new value
115 * @return 0 on success, non-0 on error
117 * The validate function should report any error it detects.
119 int (*validate)(const struct config_state *cs,
120 int nvec, char **vec);
123 /** @brief Type of a configuration item */
125 /** @brief Pointer to function to set item
126 * @param cs Configuration state
127 * @param whoami Configuration item to set
128 * @param nvec Length of new value
129 * @param vec New value
130 * @return 0 on success, non-0 on error
132 int (*set)(const struct config_state *cs,
133 const struct conf *whoami,
134 int nvec, char **vec);
136 /** @brief Pointer to function to free item
137 * @param c Configuration structure to free an item of
138 * @param whoami Configuration item to free
140 void (*free)(struct config *c, const struct conf *whoami);
143 /** @brief Compute the address of an item */
144 #define ADDRESS(C, TYPE) ((TYPE *)((char *)(C) + whoami->offset))
145 /** @brief Return the value of an item */
146 #define VALUE(C, TYPE) (*ADDRESS(C, TYPE))
148 static int stringlist_compare(const struct stringlist *a,
149 const struct stringlist *b);
150 static int namepartlist_compare(const struct namepartlist *a,
151 const struct namepartlist *b);
153 static int set_signal(const struct config_state *cs,
154 const struct conf *whoami,
155 int nvec, char **vec) {
159 disorder_error(0, "%s:%d: '%s' requires one argument",
160 cs->path, cs->line, whoami->name);
163 if((n = find_signal(vec[0])) == -1) {
164 disorder_error(0, "%s:%d: unknown signal '%s'",
165 cs->path, cs->line, vec[0]);
168 VALUE(cs->config, int) = n;
172 static int set_collections(const struct config_state *cs,
173 const struct conf *whoami,
174 int nvec, char **vec) {
175 struct collectionlist *cl;
176 const char *root, *encoding, *module;
195 disorder_error(0, "%s:%d: '%s' requires at least one argument",
196 cs->path, cs->line, whoami->name);
199 disorder_error(0, "%s:%d: '%s' requires at most three arguments",
200 cs->path, cs->line, whoami->name);
203 /* Sanity check root */
205 disorder_error(0, "%s:%d: collection root must start with '/'",
209 if(root[1] && root[strlen(root)-1] == '/') {
210 disorder_error(0, "%s:%d: collection root must not end with '/'",
219 encoding = nl_langinfo(CODESET);
224 cl = ADDRESS(cs->config, struct collectionlist);
226 cl->s = xrealloc(cl->s, cl->n * sizeof (struct collection));
227 cl->s[cl->n - 1].module = xstrdup(module);
228 cl->s[cl->n - 1].encoding = xstrdup(encoding);
229 cl->s[cl->n - 1].root = xstrdup(root);
233 static int set_boolean(const struct config_state *cs,
234 const struct conf *whoami,
235 int nvec, char **vec) {
239 disorder_error(0, "%s:%d: '%s' takes only one argument",
240 cs->path, cs->line, whoami->name);
243 if(!strcmp(vec[0], "yes")) state = 1;
244 else if(!strcmp(vec[0], "no")) state = 0;
246 disorder_error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'",
247 cs->path, cs->line, whoami->name);
250 VALUE(cs->config, int) = state;
254 static int set_string(const struct config_state *cs,
255 const struct conf *whoami,
256 int nvec, char **vec) {
258 disorder_error(0, "%s:%d: '%s' takes only one argument",
259 cs->path, cs->line, whoami->name);
262 xfree(VALUE(cs->config, char *));
263 VALUE(cs->config, char *) = xstrdup(vec[0]);
267 static int set_integer(const struct config_state *cs,
268 const struct conf *whoami,
269 int nvec, char **vec) {
273 disorder_error(0, "%s:%d: '%s' takes only one argument",
274 cs->path, cs->line, whoami->name);
277 if(xstrtol(ADDRESS(cs->config, long), vec[0], &e, 0)) {
278 disorder_error(errno, "%s:%d: converting integer", cs->path, cs->line);
282 disorder_error(0, "%s:%d: invalid integer syntax", cs->path, cs->line);
288 static int set_stringlist_accum(const struct config_state *cs,
289 const struct conf *whoami,
290 int nvec, char **vec) {
292 struct stringlist *s;
293 struct stringlistlist *sll;
295 sll = ADDRESS(cs->config, struct stringlistlist);
301 sll->s = xrealloc(sll->s, (sll->n * sizeof (struct stringlist)));
302 s = &sll->s[sll->n - 1];
304 s->s = xmalloc((nvec + 1) * sizeof (char *));
305 for(n = 0; n < nvec; ++n)
306 s->s[n] = xstrdup(vec[n]);
310 static int set_string_accum(const struct config_state *cs,
311 const struct conf *whoami,
312 int nvec, char **vec) {
314 struct stringlist *sl;
316 sl = ADDRESS(cs->config, struct stringlist);
321 for(n = 0; n < nvec; ++n) {
323 sl->s = xrealloc(sl->s, (sl->n * sizeof (char *)));
324 sl->s[sl->n - 1] = xstrdup(vec[n]);
329 static int parse_sample_format(const struct config_state *cs,
330 struct stream_header *format,
331 int nvec, char **vec) {
336 disorder_error(0, "%s:%d: wrong number of arguments", cs->path, cs->line);
339 if(xstrtol(&t, p, &p, 0)) {
340 disorder_error(errno, "%s:%d: converting bits-per-sample",
344 if(t != 8 && t != 16) {
345 disorder_error(0, "%s:%d: bad bits-per-sample (%ld)",
346 cs->path, cs->line, t);
349 if(format) format->bits = (uint8_t)t;
351 case 'l': case 'L': t = ENDIAN_LITTLE; p++; break;
352 case 'b': case 'B': t = ENDIAN_BIG; p++; break;
353 default: t = ENDIAN_NATIVE; break;
355 if(format) format->endian = (uint8_t)t;
357 disorder_error(errno, "%s:%d: expected `/' after bits-per-sample",
362 if(xstrtol(&t, p, &p, 0)) {
363 disorder_error(errno, "%s:%d: converting sample-rate", cs->path, cs->line);
366 if(t < 1 || t > INT_MAX) {
367 disorder_error(0, "%s:%d: silly sample-rate (%ld)", cs->path, cs->line, t);
370 if(format) format->rate = t;
372 disorder_error(0, "%s:%d: expected `/' after sample-rate",
377 if(xstrtol(&t, p, &p, 0)) {
378 disorder_error(errno, "%s:%d: converting channels", cs->path, cs->line);
382 disorder_error(0, "%s:%d: silly number (%ld) of channels",
383 cs->path, cs->line, t);
386 if(format) format->channels = (uint8_t)t;
388 disorder_error(0, "%s:%d: junk after channels", cs->path, cs->line);
394 static int set_sample_format(const struct config_state *cs,
395 const struct conf *whoami,
396 int nvec, char **vec) {
397 return parse_sample_format(cs, ADDRESS(cs->config, struct stream_header),
401 static int set_namepart(const struct config_state *cs,
402 const struct conf *whoami,
403 int nvec, char **vec) {
404 struct namepartlist *npl = ADDRESS(cs->config, struct namepartlist);
407 char errstr[RXCERR_LEN];
412 disorder_error(0, "%s:%d: namepart needs at least 3 arguments",
417 disorder_error(0, "%s:%d: namepart needs at most 5 arguments",
421 reflags = nvec >= 5 ? regsub_flags(vec[4]) : 0;
422 if(!(re = regexp_compile(vec[1], regsub_compile_options(reflags),
423 errstr, sizeof(errstr), &erroffset)))
425 disorder_error(0, "%s:%d: compiling regexp /%s/: %s (offset %zu)",
426 cs->path, cs->line, vec[1], errstr, erroffset);
429 npl->s = xrealloc(npl->s, (npl->n + 1) * sizeof (struct namepart));
430 npl->s[npl->n].part = xstrdup(vec[0]);
431 npl->s[npl->n].re = re;
432 npl->s[npl->n].res = xstrdup(vec[1]);
433 npl->s[npl->n].replace = xstrdup(vec[2]);
434 npl->s[npl->n].context = xstrdup(vec[3]);
435 npl->s[npl->n].reflags = reflags;
437 /* XXX a bit of a bodge; relies on there being very few parts. */
438 for(n = 0; (n < cs->config->nparts
439 && strcmp(cs->config->parts[n], vec[0])); ++n)
441 if(n >= cs->config->nparts) {
442 cs->config->parts = xrealloc(cs->config->parts,
443 (cs->config->nparts + 1) * sizeof (char *));
444 cs->config->parts[cs->config->nparts++] = xstrdup(vec[0]);
449 static int set_transform(const struct config_state *cs,
450 const struct conf *whoami,
451 int nvec, char **vec) {
452 struct transformlist *tl = ADDRESS(cs->config, struct transformlist);
454 char errstr[RXCERR_LEN];
459 disorder_error(0, "%s:%d: transform needs at least 3 arguments",
464 disorder_error(0, "%s:%d: transform needs at most 5 arguments",
468 reflags = (nvec >= 5 ? regsub_flags(vec[4]) : 0);
469 if(!(re = regexp_compile(vec[1], regsub_compile_options(reflags),
470 errstr, sizeof(errstr), &erroffset)))
472 disorder_error(0, "%s:%d: compiling regexp /%s/: %s (offset %zu)",
473 cs->path, cs->line, vec[1], errstr, erroffset);
476 tl->t = xrealloc(tl->t, (tl->n + 1) * sizeof (struct namepart));
477 tl->t[tl->n].type = xstrdup(vec[0]);
478 tl->t[tl->n].context = xstrdup(vec[3] ? vec[3] : "*");
479 tl->t[tl->n].re = re;
480 tl->t[tl->n].replace = xstrdup(vec[2]);
481 tl->t[tl->n].flags = reflags;
486 static int set_rights(const struct config_state *cs,
487 const struct conf *whoami,
488 int nvec, char **vec) {
490 disorder_error(0, "%s:%d: '%s' requires one argument",
491 cs->path, cs->line, whoami->name);
494 if(parse_rights(vec[0], 0, 1)) {
495 disorder_error(0, "%s:%d: invalid rights string '%s'",
496 cs->path, cs->line, vec[0]);
499 return set_string(cs, whoami, nvec, vec);
502 static int set_netaddress(const struct config_state *cs,
503 const struct conf *whoami,
504 int nvec, char **vec) {
505 struct netaddress *na = ADDRESS(cs->config, struct netaddress);
507 if(netaddress_parse(na, nvec, vec)) {
508 disorder_error(0, "%s:%d: invalid network address", cs->path, cs->line);
516 static void free_none(struct config attribute((unused)) *c,
517 const struct conf attribute((unused)) *whoami) {
520 static void free_string(struct config *c,
521 const struct conf *whoami) {
522 xfree(VALUE(c, char *));
523 VALUE(c, char *) = 0;
526 static void free_stringlist(struct config *c,
527 const struct conf *whoami) {
529 struct stringlist *sl = ADDRESS(c, struct stringlist);
531 for(n = 0; n < sl->n; ++n)
536 static void free_stringlistlist(struct config *c,
537 const struct conf *whoami) {
539 struct stringlistlist *sll = ADDRESS(c, struct stringlistlist);
540 struct stringlist *sl;
542 for(n = 0; n < sll->n; ++n) {
544 for(m = 0; m < sl->n; ++m)
551 static void free_collectionlist(struct config *c,
552 const struct conf *whoami) {
553 struct collectionlist *cll = ADDRESS(c, struct collectionlist);
554 struct collection *cl;
557 for(n = 0; n < cll->n; ++n) {
566 static void free_namepartlist(struct config *c,
567 const struct conf *whoami) {
568 struct namepartlist *npl = ADDRESS(c, struct namepartlist);
572 for(n = 0; n < npl->n; ++n) {
583 static void free_transformlist(struct config *c,
584 const struct conf *whoami) {
585 struct transformlist *tl = ADDRESS(c, struct transformlist);
589 for(n = 0; n < tl->n; ++n) {
599 static void free_netaddress(struct config *c,
600 const struct conf *whoami) {
601 struct netaddress *na = ADDRESS(c, struct netaddress);
606 /* configuration types */
608 static const struct conftype
609 type_signal = { set_signal, free_none },
610 type_collections = { set_collections, free_collectionlist },
611 type_boolean = { set_boolean, free_none },
612 type_string = { set_string, free_string },
613 type_integer = { set_integer, free_none },
614 type_stringlist_accum = { set_stringlist_accum, free_stringlistlist },
615 type_string_accum = { set_string_accum, free_stringlist },
616 type_sample_format = { set_sample_format, free_none },
617 type_namepart = { set_namepart, free_namepartlist },
618 type_transform = { set_transform, free_transformlist },
619 type_netaddress = { set_netaddress, free_netaddress },
620 type_rights = { set_rights, free_string };
622 /* specific validation routine */
624 /** @brief Perform a test on a filename
625 * @param test Test function to call on mode bits
626 * @param what Type of file sought
628 * If @p test returns 0 then the file is not a @p what and an error
629 * is reported and -1 is returned.
631 #define VALIDATE_FILE(test, what) do { \
635 for(n = 0; n < nvec; ++n) { \
636 if(stat(vec[n], &sb) < 0) { \
637 disorder_error(errno, "%s:%d: %s", \
638 cs->path, cs->line, vec[n]); \
641 if(!test(sb.st_mode)) { \
642 disorder_error(0, "%s:%d: %s is not a %s", \
643 cs->path, cs->line, vec[n], what); \
649 /** @brief Validate an absolute path
650 * @param cs Configuration state
651 * @param nvec Length of (proposed) new value
652 * @param vec Elements of new value
653 * @return 0 on success, non-0 on error
655 static int validate_isabspath(const struct config_state *cs,
656 int nvec, char **vec) {
659 for(n = 0; n < nvec; ++n)
660 if(vec[n][0] != '/') {
661 disorder_error(0, "%s:%d: %s: not an absolute path",
662 cs->path, cs->line, vec[n]);
668 /** @brief Validate an existing directory
669 * @param cs Configuration state
670 * @param nvec Length of (proposed) new value
671 * @param vec Elements of new value
672 * @return 0 on success, non-0 on error
674 static int validate_isdir(const struct config_state *cs,
675 int nvec, char **vec) {
676 VALIDATE_FILE(S_ISDIR, "directory");
680 /** @brief Validate an existing regular file
681 * @param cs Configuration state
682 * @param nvec Length of (proposed) new value
683 * @param vec Elements of new value
684 * @return 0 on success, non-0 on error
686 static int validate_isreg(const struct config_state *cs,
687 int nvec, char **vec) {
688 VALIDATE_FILE(S_ISREG, "regular file");
692 /** @brief Validate a player pattern
693 * @param cs Configuration state
694 * @param nvec Length of (proposed) new value
695 * @param vec Elements of new value
696 * @return 0 on success, non-0 on error
698 static int validate_player(const struct config_state *cs,
700 char attribute((unused)) **vec) {
701 if(nvec && nvec < 2) {
702 disorder_error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
709 /** @brief Validate a track length pattern
710 * @param cs Configuration state
711 * @param nvec Length of (proposed) new value
712 * @param vec Elements of new value
713 * @return 0 on success, non-0 on error
715 static int validate_tracklength(const struct config_state *cs,
717 char attribute((unused)) **vec) {
718 if(nvec && nvec < 2) {
719 disorder_error(0, "%s:%d: should be at least 'tracklength PATTERN MODULE'",
726 /** @brief Common code for validating integer values
727 * @param cs Configuration state
728 * @param nvec Length of (proposed) new value
729 * @param vec Elements of new value
730 * @param n_out Where to put the value
732 static int common_validate_integer(const struct config_state *cs,
733 int nvec, char **vec, long *n_out) {
737 disorder_error(0, "%s:%d: missing argument", cs->path, cs->line);
741 disorder_error(0, "%s:%d: too many arguments", cs->path, cs->line);
744 if(xstrtol(n_out, vec[0], 0, 0)) {
745 disorder_error(0, "%s:%d: %s", cs->path, cs->line,
746 format_error(ec_errno, errno, errbuf, sizeof errbuf));
752 /** @brief Validate a non-negative (@c long) integer
753 * @param cs Configuration state
754 * @param nvec Length of (proposed) new value
755 * @param vec Elements of new value
756 * @return 0 on success, non-0 on error
758 static int validate_non_negative(const struct config_state *cs,
759 int nvec, char **vec) {
761 if(common_validate_integer(cs, nvec, vec, &n)) return -1;
763 disorder_error(0, "%s:%d: must not be negative", cs->path, cs->line);
769 /** @brief Validate a positive (@c long) integer
770 * @param cs Configuration state
771 * @param nvec Length of (proposed) new value
772 * @param vec Elements of new value
773 * @return 0 on success, non-0 on error
775 static int validate_positive(const struct config_state *cs,
776 int nvec, char **vec) {
778 if(common_validate_integer(cs, nvec, vec, &n)) return -1;
780 disorder_error(0, "%s:%d: must be positive", cs->path, cs->line);
787 /** @brief Validate a system username
788 * @param cs Configuration state
789 * @param nvec Length of (proposed) new value
790 * @param vec Elements of new value
791 * @return 0 on success, non-0 on error
793 static int validate_isauser(const struct config_state *cs,
794 int attribute((unused)) nvec,
796 if(!getpwnam(vec[0])) {
797 disorder_error(0, "%s:%d: no such user as '%s'", cs->path, cs->line, vec[0]);
804 /** @brief Validate a sample format string
805 * @param cs Configuration state
806 * @param nvec Length of (proposed) new value
807 * @param vec Elements of new value
808 * @return 0 on success, non-0 on error
810 static int validate_sample_format(const struct config_state *cs,
811 int attribute((unused)) nvec,
813 return parse_sample_format(cs, 0, nvec, vec);
816 /** @brief Validate anything
817 * @param cs Configuration state
818 * @param nvec Length of (proposed) new value
819 * @param vec Elements of new value
822 static int validate_any(const struct config_state attribute((unused)) *cs,
823 int attribute((unused)) nvec,
824 char attribute((unused)) **vec) {
828 /** @brief Validate a URL
829 * @param cs Configuration state
830 * @param nvec Length of (proposed) new value
831 * @param vec Elements of new value
832 * @return 0 on success, non-0 on error
836 static int validate_url(const struct config_state attribute((unused)) *cs,
837 int attribute((unused)) nvec,
841 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
842 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
844 n = strspn(s, ("abcdefghijklmnopqrstuvwxyz"
845 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
848 disorder_error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
851 if(!strncmp(s, "http:", 5)
852 || !strncmp(s, "https:", 6)) {
854 /* we only do a rather cursory check */
855 if(strncmp(s, "//", 2)) {
856 disorder_error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
863 /** @brief Validate an alias pattern
864 * @param cs Configuration state
865 * @param nvec Length of (proposed) new value
866 * @param vec Elements of new value
867 * @return 0 on success, non-0 on error
869 static int validate_alias(const struct config_state *cs,
873 int in_brackets = 0, c;
876 disorder_error(0, "%s:%d: missing argument", cs->path, cs->line);
880 disorder_error(0, "%s:%d: too many arguments", cs->path, cs->line);
884 while((c = (unsigned char)*s++)) {
888 else if(!isalnum(c)) {
889 disorder_error(0, "%s:%d: invalid part name in alias expansion in '%s'",
890 cs->path, cs->line, vec[0]);
898 } else if(c == '\\') {
899 if(!(c = (unsigned char)*s++)) {
900 disorder_error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
901 cs->path, cs->line, vec[0]);
903 } else if(c != '\\' && c != '{') {
904 disorder_error(0, "%s:%d: invalid escape in alias expansion in '%s'",
905 cs->path, cs->line, vec[0]);
914 "%s:%d: unterminated part name in alias expansion in '%s'",
915 cs->path, cs->line, vec[0]);
921 /** @brief Validate a hash algorithm name
922 * @param cs Configuration state
923 * @param nvec Length of (proposed) new value
924 * @param vec Elements of new value
925 * @return 0 on success, non-0 on error
927 static int validate_algo(const struct config_state attribute((unused)) *cs,
931 disorder_error(0, "%s:%d: invalid algorithm specification", cs->path, cs->line);
934 if(!valid_authhash(vec[0])) {
935 disorder_error(0, "%s:%d: unsuported algorithm '%s'", cs->path, cs->line, vec[0]);
942 /** @brief Validate a playback backend name
943 * @param cs Configuration state
944 * @param nvec Length of (proposed) new value
945 * @param vec Elements of new value
946 * @return 0 on success, non-0 on error
948 static int validate_backend(const struct config_state attribute((unused)) *cs,
953 disorder_error(0, "%s:%d: invalid sound API specification", cs->path, cs->line);
956 if(!strcmp(vec[0], "network")) {
957 disorder_error(0, "'api network' is deprecated; use 'api rtp'");
960 if(config_uaudio_apis) {
961 for(n = 0; config_uaudio_apis[n]; ++n)
962 if(!strcmp(vec[0], config_uaudio_apis[n]->name))
964 disorder_error(0, "%s:%d: unrecognized sound API '%s'", cs->path, cs->line, vec[0]);
967 /* In non-server processes we have no idea what's valid */
972 /** @brief Validate a pause mode string
973 * @param cs Configuration state
974 * @param nvec Length of (proposed) new value
975 * @param vec Elements of new value
976 * @return 0 on success, non-0 on error
978 static int validate_pausemode(const struct config_state attribute((unused)) *cs,
981 if(nvec == 1 && (!strcmp(vec[0], "silence") || !strcmp(vec[0], "suspend")))
983 disorder_error(0, "%s:%d: invalid pause mode", cs->path, cs->line);
987 /** @brief Validate an MTU-discovery setting
988 * @param cs Configuration state
989 * @param nvec Length of (proposed) new value
990 * @param vec Elements of new value
991 * @return 0 on success, non-0 on error
993 static int validate_mtu_discovery(const struct config_state attribute((unused)) *cs,
997 (!strcmp(vec[0], "default") ||
998 !strcmp(vec[0], "yes") ||
999 !strcmp(vec[0], "no")))
1001 disorder_error(0, "%s:%d: invalid MTU-discovery setting", 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 Validate an internet address
1032 * @param cs Configuration state
1033 * @param nvec Length of (proposed) new value
1034 * @param vec Elements of new value
1035 * @return 0 on success, non-0 on error
1037 * By a destination address, it is meant that it must be either IPv4 or IPv6.
1039 static int validate_inetaddr(const struct config_state *cs,
1040 int nvec, char **vec) {
1041 struct netaddress na[1];
1043 if(netaddress_parse(na, nvec, vec)) {
1044 disorder_error(0, "%s:%d: invalid network address", cs->path, cs->line);
1048 case AF_INET: case AF_INET6: case AF_UNSPEC: break;
1050 disorder_error(0, "%s:%d: must be an intenet address",
1051 cs->path, cs->line);
1057 /** @brief Item name and and offset */
1058 #define C(x) #x, offsetof(struct config, x)
1059 /** @brief Item name and and offset */
1060 #define C2(x,y) #x, offsetof(struct config, y)
1062 /** @brief All configuration items */
1063 static const struct conf conf[] = {
1064 { C(alias), &type_string, validate_alias },
1066 { C(api), &type_string, validate_backend },
1068 { C(authorization_algorithm), &type_string, validate_algo },
1069 { C(broadcast), &type_netaddress, validate_destaddr },
1070 { C(broadcast_from), &type_netaddress, validate_any },
1071 { C(channel), &type_string, validate_any },
1072 { C(checkpoint_kbyte), &type_integer, validate_non_negative },
1073 { C(checkpoint_min), &type_integer, validate_non_negative },
1074 { C(collection), &type_collections, validate_any },
1075 { C(connect), &type_netaddress, validate_destaddr },
1076 { C(cookie_key_lifetime), &type_integer, validate_positive },
1077 { C(cookie_login_lifetime), &type_integer, validate_positive },
1078 { C(dbversion), &type_integer, validate_positive },
1079 { C(default_rights), &type_rights, validate_any },
1080 { C(device), &type_string, validate_any },
1081 { C(history), &type_integer, validate_positive },
1083 { C(home), &type_string, validate_isabspath },
1085 { C(listen), &type_netaddress, validate_any },
1086 { C(mail_sender), &type_string, validate_any },
1087 { C(mixer), &type_string, validate_any },
1088 { C(mount_rescan), &type_boolean, validate_any },
1089 { C(multicast_loop), &type_boolean, validate_any },
1090 { C(multicast_ttl), &type_integer, validate_non_negative },
1091 { C(namepart), &type_namepart, validate_any },
1092 { C(new_bias), &type_integer, validate_positive },
1093 { C(new_bias_age), &type_integer, validate_positive },
1094 { C(new_max), &type_integer, validate_positive },
1095 { C2(nice, nice_rescan), &type_integer, validate_non_negative },
1096 { C(nice_rescan), &type_integer, validate_non_negative },
1097 { C(nice_server), &type_integer, validate_any },
1098 { C(nice_speaker), &type_integer, validate_any },
1099 { C(noticed_history), &type_integer, validate_positive },
1100 { C(password), &type_string, validate_any },
1101 { C(pause_mode), &type_string, validate_pausemode },
1102 { C(player), &type_stringlist_accum, validate_player },
1103 { C(playlist_lock_timeout), &type_integer, validate_positive },
1104 { C(playlist_max) , &type_integer, validate_positive },
1105 { C(plugins), &type_string_accum, validate_isdir },
1106 { C(queue_pad), &type_integer, validate_positive },
1107 { C(refresh), &type_integer, validate_positive },
1108 { C(refresh_min), &type_integer, validate_non_negative },
1109 { C(reminder_interval), &type_integer, validate_positive },
1110 { C(remote_userman), &type_boolean, validate_any },
1111 { C(replay_min), &type_integer, validate_non_negative },
1112 { C(rtp_always_request), &type_boolean, validate_any },
1113 { C(rtp_delay_threshold), &type_integer, validate_positive },
1114 { C(rtp_instance_name), &type_string, validate_any },
1115 { C(rtp_max_payload), &type_integer, validate_positive },
1116 { C(rtp_maxbuffer), &type_integer, validate_non_negative },
1117 { C(rtp_minbuffer), &type_integer, validate_non_negative },
1118 { C(rtp_mode), &type_string, validate_any },
1119 { C(rtp_mtu_discovery), &type_string, validate_mtu_discovery },
1120 { C(rtp_rcvbuf), &type_integer, validate_non_negative },
1121 { C(rtp_request_address), &type_netaddress, validate_inetaddr },
1122 { C(rtp_verbose), &type_boolean, validate_any },
1123 { C(sample_format), &type_sample_format, validate_sample_format },
1124 { C(scratch), &type_string_accum, validate_isreg },
1126 { C(sendmail), &type_string, validate_isabspath },
1128 { C(short_display), &type_integer, validate_positive },
1129 { C(signal), &type_signal, validate_any },
1130 { C(smtp_server), &type_string, validate_any },
1131 { C(sox_generation), &type_integer, validate_non_negative },
1133 { C2(speaker_backend, api), &type_string, validate_backend },
1135 { C(speaker_command), &type_string, validate_any },
1136 { C(stopword), &type_string_accum, validate_any },
1137 { C(templates), &type_string_accum, validate_isdir },
1138 { C(tracklength), &type_stringlist_accum, validate_tracklength },
1139 { C(transform), &type_transform, validate_any },
1140 { C(url), &type_string, validate_url },
1142 { C(user), &type_string, validate_isauser },
1144 { C(username), &type_string, validate_any },
1147 /** @brief Find a configuration item's definition by key */
1148 static const struct conf *find(const char *key) {
1151 if((n = TABLE_FIND(conf, name, key)) < 0)
1156 /** @brief Set a new configuration value
1157 * @param cs Configuration state
1158 * @param nvec Length of @p vec
1159 * @param vec Name and new value
1160 * @return 0 on success, non-0 on error.
1162 * @c vec[0] is the name, the rest is the value.
1164 static int config_set(const struct config_state *cs,
1165 int nvec, char **vec) {
1166 const struct conf *which;
1168 D(("config_set %s", vec[0]));
1169 if(!(which = find(vec[0]))) {
1170 disorder_error(0, "%s:%d: unknown configuration key '%s'",
1171 cs->path, cs->line, vec[0]);
1174 return (which->validate(cs, nvec - 1, vec + 1)
1175 || which->type->set(cs, which, nvec - 1, vec + 1));
1178 /** @brief Set a configuration item from parameters
1179 * @param cs Configuration state
1180 * @param which Item to set
1181 * @param ... Value as strings, terminated by (char *)NULL
1182 * @return 0 on success, non-0 on error
1184 static int config_set_args(const struct config_state *cs,
1185 const char *which, ...) {
1192 vector_append(v, (char *)which);
1193 va_start(ap, which);
1194 while((s = va_arg(ap, char *)))
1195 vector_append(v, s);
1197 vector_terminate(v);
1198 rc = config_set(cs, v->nvec, v->vec);
1203 /** @brief Error callback used by config_include()
1204 * @param msg Error message
1205 * @param u User data (@ref config_state)
1207 static void config_error(const char *msg, void *u) {
1208 const struct config_state *cs = u;
1210 disorder_error(0, "%s:%d: %s", cs->path, cs->line, msg);
1213 /** @brief Include a file by name
1214 * @param c Configuration to update
1215 * @param path Path to read
1216 * @return 0 on success, non-0 on error
1218 static int config_include(struct config *c, const char *path) {
1220 char *buffer, *inputbuffer, **vec;
1222 struct config_state cs;
1227 D(("%s: reading configuration", path));
1228 if(!(fp = fopen(path, "r"))) {
1229 disorder_error(errno, "error opening %s", path);
1232 while(!inputline(path, fp, &inputbuffer, '\n')) {
1234 if(!(buffer = mb2utf8(inputbuffer))) {
1235 disorder_error(errno, "%s:%d: cannot convert to UTF-8", cs.path, cs.line);
1241 if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES,
1242 config_error, &cs))) {
1248 /* 'include' is special-cased */
1249 if(!strcmp(vec[0], "include")) {
1251 disorder_error(0, "%s:%d: must be 'include PATH'", cs.path, cs.line);
1254 config_include(c, vec[1]);
1256 ret |= config_set(&cs, n, vec);
1258 for(n = 0; vec[n]; ++n) xfree(vec[n]);
1263 disorder_error(errno, "error reading %s", path);
1270 /** @brief Default stopword setting */
1271 static const char *const default_stopwords[] = {
1330 #define NDEFAULT_STOPWORDS (sizeof default_stopwords / sizeof *default_stopwords)
1332 /** @brief Default player patterns */
1333 static const char *const default_players[] = {
1339 #define NDEFAULT_PLAYERS (sizeof default_players / sizeof *default_players)
1341 /** @brief Make a new default configuration
1342 * @return New configuration
1344 static struct config *config_default(void) {
1345 struct config *c = xmalloc(sizeof *c);
1347 const char *logname;
1350 struct config_state cs;
1353 cs.path = "<internal>";
1356 /* Strings had better be xstrdup'd as they will get freed at some point. */
1359 c->home = xstrdup(pkgstatedir);
1364 DWORD bufsize = sizeof buffer;
1365 if(!GetUserNameA(buffer, &bufsize))
1366 disorder_fatal(0, "cannot determine our username");
1367 c->username = xstrdup(buffer);
1370 if(!(pw = getpwuid(getuid())))
1371 disorder_fatal(0, "cannot determine our username");
1372 logname = pw->pw_name;
1373 c->username = xstrdup(logname);
1378 c->signal = SIGKILL;
1380 c->signal = SIGTERM;
1382 c->alias = xstrdup("{/artist}{/album}{/title}{ext}");
1383 c->device = xstrdup("default");
1384 c->nice_rescan = 10;
1385 c->speaker_command = 0;
1386 c->sample_format.bits = 16;
1387 c->sample_format.rate = 44100;
1388 c->sample_format.channels = 2;
1389 c->sample_format.endian = ENDIAN_NATIVE;
1391 c->replay_min = 8 * 3600;
1393 c->multicast_ttl = 1;
1394 c->multicast_loop = 1;
1395 c->authorization_algorithm = xstrdup("sha1");
1396 c->noticed_history = 31;
1397 c->short_display = 32;
1401 c->cookie_login_lifetime = 86400;
1402 c->cookie_key_lifetime = 86400 * 7;
1404 if(sendmail_binary[0] && strcmp(sendmail_binary, "none"))
1405 c->sendmail = xstrdup(sendmail_binary);
1407 c->smtp_server = xstrdup("127.0.0.1");
1409 c->reminder_interval = 600; /* 10m */
1410 c->new_bias_age = 7 * 86400; /* 1 week */
1411 c->new_bias = 4500000; /* 50 times the base weight */
1412 c->sox_generation = DEFAULT_SOX_GENERATION;
1413 c->playlist_max = INT_MAX; /* effectively no limit */
1414 c->playlist_lock_timeout = 10; /* 10s */
1415 c->mount_rescan = 1;
1416 /* Default stopwords */
1417 if(config_set(&cs, (int)NDEFAULT_STOPWORDS, (char **)default_stopwords))
1419 /* Default player configuration */
1420 for(n = 0; n < NDEFAULT_PLAYERS; ++n) {
1421 if(config_set_args(&cs, "player",
1422 default_players[n], "execraw", "disorder-decode", (char *)0))
1424 if(config_set_args(&cs, "tracklength",
1425 default_players[n], "disorder-tracklength", (char *)0))
1428 c->broadcast.af = -1;
1429 c->broadcast_from.af = -1;
1432 c->rtp_mode = xstrdup("auto");
1433 c->rtp_max_payload = -1;
1434 c->rtp_mtu_discovery = xstrdup("default");
1439 /** @brief Construct a filename
1440 * @param c Configuration
1441 * @param name Base filename
1442 * @return Full filename
1444 * Usually use config_get_file() instead.
1446 char *config_get_file2(struct config *c, const char *name) {
1449 byte_xasprintf(&s, "%s/%s", c->home, name);
1454 /** @brief Set the default configuration file */
1455 static void set_configfile(void) {
1458 byte_xasprintf(&configfile, "%s/config", pkgconfdir);
1462 /** @brief Free a configuration object
1463 * @param c Configuration to free
1465 * @p c is indeterminate after this function is called.
1467 void config_free(struct config *c) {
1471 for(n = 0; n < (int)(sizeof conf / sizeof *conf); ++n)
1472 conf[n].type->free(c, &conf[n]);
1473 for(n = 0; n < c->nparts; ++n)
1480 /** @brief Set post-parse defaults
1481 * @param c Configuration to update
1482 * @param server True when running in the server
1484 * If @p server is set then certain parts of the configuration are more
1485 * strictly validated.
1487 static void config_postdefaults(struct config *c,
1489 struct config_state cs;
1490 const struct conf *whoami;
1493 static const char *namepart[][4] = {
1494 { "title", "/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
1495 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1496 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1497 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1498 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1500 #define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1502 static const char *transform[][5] = {
1503 { "track", "^.*/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
1504 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1505 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1506 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1507 { "dir", "[[:punct:]]", "", "sort", "g", }
1509 #define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1511 cs.path = "<internal>";
1514 if(!c->namepart.n) {
1515 whoami = find("namepart");
1516 for(n = 0; n < NNAMEPART; ++n)
1517 set_namepart(&cs, whoami, 4, (char **)namepart[n]);
1519 if(!c->transform.n) {
1520 whoami = find("transform");
1521 for(n = 0; n < NTRANSFORM; ++n)
1522 set_transform(&cs, whoami, 5, (char **)transform[n]);
1525 if(c->speaker_command)
1526 c->api = xstrdup("command");
1527 else if(c->broadcast.af != -1)
1528 c->api = xstrdup("rtp");
1530 else if(config_uaudio_apis)
1531 c->api = xstrdup(uaudio_default(config_uaudio_apis,
1532 UAUDIO_API_SERVER)->name);
1535 c->api = xstrdup("<none>");
1537 if(!strcmp(c->api, "network"))
1538 c->api = xstrdup("rtp");
1540 if(!strcmp(c->api, "command") && !c->speaker_command)
1541 disorder_fatal(0, "'api command' but speaker_command is not set");
1542 if((!strcmp(c->api, "rtp")) &&
1543 c->broadcast.af == -1 && strcmp(c->rtp_mode, "request"))
1544 disorder_fatal(0, "'api rtp' but broadcast is not set "
1545 "and mode is not not 'request'");
1547 /* Override sample format */
1548 if(!strcmp(c->api, "rtp")) {
1549 c->sample_format.rate = 44100;
1550 c->sample_format.channels = 2;
1551 c->sample_format.bits = 16;
1552 c->sample_format.endian = ENDIAN_NATIVE;
1554 if(!strcmp(c->api, "coreaudio")) {
1555 c->sample_format.rate = 44100;
1556 c->sample_format.channels = 2;
1557 c->sample_format.bits = 16;
1558 c->sample_format.endian = ENDIAN_NATIVE;
1560 if(!c->default_rights) {
1561 rights_type r = RIGHTS__MASK & ~(RIGHT_ADMIN|RIGHT_REGISTER
1563 |RIGHT_SCRATCH__MASK
1564 |RIGHT_REMOVE__MASK);
1565 r |= RIGHT_SCRATCH_ANY|RIGHT_MOVE_ANY|RIGHT_REMOVE_ANY;
1566 c->default_rights = rights_string(r);
1570 /** @brief (Re-)read the config file
1571 * @param server If set, do extra checking
1572 * @param oldconfig Old configuration for compatibility check
1573 * @return 0 on success, non-0 on error
1575 * If @p oldconfig is set, then certain compatibility checks are done between
1576 * the old and new configurations.
1578 int config_read(int server,
1579 const struct config *oldconfig) {
1582 struct passwd *pw = NULL;
1585 c = config_default();
1586 /* standalone client installs might not have a global config file */
1588 if(access(configfile, F_OK) == 0)
1589 if(config_include(c, configfile))
1591 /* if we can read the private config file, do */
1592 if((privconf = config_private())
1593 && access(privconf, R_OK) == 0
1594 && config_include(c, privconf))
1597 /* if there's a per-user system config file for this user, read it */
1598 if(config_per_user) {
1600 if(!(pw = getpwuid(getuid())))
1601 disorder_fatal(0, "cannot determine our username");
1602 if((privconf = config_usersysconf(pw))
1603 && access(privconf, F_OK) == 0
1604 && config_include(c, privconf))
1608 /* if we have a password file, read it */
1609 if((privconf = config_userconf())
1610 && access(privconf, F_OK) == 0
1611 && config_include(c, privconf))
1615 /* install default namepart and transform settings */
1616 config_postdefaults(c, server);
1620 if(strcmp(c->home, oldconfig->home)) {
1621 disorder_error(0, "'home' cannot be changed without a restart");
1625 if(strcmp(c->alias, oldconfig->alias)) {
1626 disorder_error(0, "'alias' cannot be changed without a restart");
1629 if(strcmp(c->user, oldconfig->user)) {
1630 disorder_error(0, "'user' cannot be changed without a restart");
1633 if(c->nice_speaker != oldconfig->nice_speaker) {
1634 disorder_error(0, "'nice_speaker' cannot be changed without a restart");
1635 /* ...but we accept the new config anyway */
1637 if(c->nice_server != oldconfig->nice_server) {
1638 disorder_error(0, "'nice_server' cannot be changed without a restart");
1639 /* ...but we accept the new config anyway */
1641 if(namepartlist_compare(&c->namepart, &oldconfig->namepart)) {
1642 disorder_error(0, "'namepart' settings cannot be changed without a restart");
1645 if(stringlist_compare(&c->stopword, &oldconfig->stopword)) {
1646 disorder_error(0, "'stopword' settings cannot be changed without a restart");
1650 disorder_error(0, "not installing incompatible new configuration");
1654 /* everything is good so we shall use the new config */
1655 config_free(config);
1656 /* warn about obsolete directives */
1661 /** @brief Return the path to the private configuration file */
1662 char *config_private(void) {
1669 byte_xasprintf(&s, "%s.private", configfile);
1674 /** @brief Return the path to user's personal configuration file */
1675 char *config_userconf(void) {
1680 if(SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, NULL, &wpath) != S_OK)
1681 disorder_fatal(0, "error calling SHGetKnownFolderPath");
1682 appdata = win_wtomb(wpath);
1683 CoTaskMemFree(wpath);
1684 byte_xasprintf(&s, "%s\\DisOrder\\passwd", appdata);
1687 if(!(pw = getpwuid(getuid())))
1688 disorder_fatal(0, "cannot determine our username");
1689 byte_xasprintf(&s, "%s/.disorder/passwd", pw->pw_dir);
1695 /** @brief Return the path to user-specific system configuration */
1696 char *config_usersysconf(const struct passwd *pw) {
1700 if(!strchr(pw->pw_name, '/')) {
1701 byte_xasprintf(&s, "%s.%s", configfile, pw->pw_name);
1707 /** @brief Get a filename within the home directory
1708 * @param name Relative name
1711 char *config_get_file(const char *name) {
1712 return config_get_file2(config, name);
1716 /** @brief Order two stringlists
1717 * @param a First stringlist
1718 * @param b Second stringlist
1719 * @return <0, 0 or >0 if a<b, a=b or a>b
1721 static int stringlist_compare(const struct stringlist *a,
1722 const struct stringlist *b) {
1725 while(n < a->n && n < b->n) {
1726 if((c = strcmp(a->s[n], b->s[n])))
1732 else if(a->n > b->n)
1738 /** @brief Order two namepart definitions
1739 * @param a First namepart definition
1740 * @param b Second namepart definition
1741 * @return <0, 0 or >0 if a<b, a=b or a>b
1743 static int namepart_compare(const struct namepart *a,
1744 const struct namepart *b) {
1747 if((c = strcmp(a->part, b->part)))
1749 if((c = strcmp(a->res, b->res)))
1751 if((c = strcmp(a->replace, b->replace)))
1753 if((c = strcmp(a->context, b->context)))
1755 if(a->reflags > b->reflags)
1757 if(a->reflags < b->reflags)
1762 /** @brief Order two lists of namepart definitions
1763 * @param a First list of namepart definitions
1764 * @param b Second list of namepart definitions
1765 * @return <0, 0 or >0 if a<b, a=b or a>b
1767 static int namepartlist_compare(const struct namepartlist *a,
1768 const struct namepartlist *b) {
1771 while(n < a->n && n < b->n) {
1772 if((c = namepart_compare(&a->s[n], &b->s[n])))
1778 else if(a->n < b->n)
1784 /** @brief Verify configuration table.
1785 * @return The number of problems found
1787 int config_verify(void) {
1790 for(n = 1; n < sizeof conf / sizeof *conf; ++n)
1791 if(strcmp(conf[n-1].name, conf[n].name) >= 0) {
1792 fprintf(stderr, "%s >= %s\n", conf[n-1].name, conf[n].name);