2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2009 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 "configuration.h"
43 #include "inputline.h"
53 /** @brief Path to config file
55 * set_configfile() sets the deafult if it is null.
59 /** @brief Read user configuration
61 * If clear, the user-specific configuration is not read.
63 int config_per_user = 1;
65 /** @brief Table of audio APIs
67 * Only set in server processes.
69 const struct uaudio *const *config_uaudio_apis;
71 /** @brief Config file parser state */
73 /** @brief Filename */
75 /** @brief Line number */
77 /** @brief Configuration object under construction */
78 struct config *config;
81 /** @brief Current configuration */
82 struct config *config;
84 /** @brief One configuration item */
86 /** @brief Name as it appears in the config file */
88 /** @brief Offset in @ref config structure */
90 /** @brief Pointer to item type */
91 const struct conftype *type;
92 /** @brief Pointer to item-specific validation routine */
93 int (*validate)(const struct config_state *cs,
94 int nvec, char **vec);
97 /** @brief Type of a configuration item */
99 /** @brief Pointer to function to set item */
100 int (*set)(const struct config_state *cs,
101 const struct conf *whoami,
102 int nvec, char **vec);
103 /** @brief Pointer to function to free item */
104 void (*free)(struct config *c, const struct conf *whoami);
107 /** @brief Compute the address of an item */
108 #define ADDRESS(C, TYPE) ((TYPE *)((char *)(C) + whoami->offset))
109 /** @brief Return the value of an item */
110 #define VALUE(C, TYPE) (*ADDRESS(C, TYPE))
112 static int stringlist_compare(const struct stringlist *a,
113 const struct stringlist *b);
114 static int namepartlist_compare(const struct namepartlist *a,
115 const struct namepartlist *b);
117 static int set_signal(const struct config_state *cs,
118 const struct conf *whoami,
119 int nvec, char **vec) {
123 error(0, "%s:%d: '%s' requires one argument",
124 cs->path, cs->line, whoami->name);
127 if((n = find_signal(vec[0])) == -1) {
128 error(0, "%s:%d: unknown signal '%s'",
129 cs->path, cs->line, vec[0]);
132 VALUE(cs->config, int) = n;
136 static int set_collections(const struct config_state *cs,
137 const struct conf *whoami,
138 int nvec, char **vec) {
139 struct collectionlist *cl;
140 const char *root, *encoding, *module;
159 error(0, "%s:%d: '%s' requires at least one argument",
160 cs->path, cs->line, whoami->name);
163 error(0, "%s:%d: '%s' requires at most three arguments",
164 cs->path, cs->line, whoami->name);
167 /* Sanity check root */
169 error(0, "%s:%d: collection root must start with '/'",
173 if(root[1] && root[strlen(root)-1] == '/') {
174 error(0, "%s:%d: collection root must not end with '/'",
182 encoding = nl_langinfo(CODESET);
183 cl = ADDRESS(cs->config, struct collectionlist);
185 cl->s = xrealloc(cl->s, cl->n * sizeof (struct collection));
186 cl->s[cl->n - 1].module = xstrdup(module);
187 cl->s[cl->n - 1].encoding = xstrdup(encoding);
188 cl->s[cl->n - 1].root = xstrdup(root);
192 static int set_boolean(const struct config_state *cs,
193 const struct conf *whoami,
194 int nvec, char **vec) {
198 error(0, "%s:%d: '%s' takes only one argument",
199 cs->path, cs->line, whoami->name);
202 if(!strcmp(vec[0], "yes")) state = 1;
203 else if(!strcmp(vec[0], "no")) state = 0;
205 error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'",
206 cs->path, cs->line, whoami->name);
209 VALUE(cs->config, int) = state;
213 static int set_string(const struct config_state *cs,
214 const struct conf *whoami,
215 int nvec, char **vec) {
217 error(0, "%s:%d: '%s' takes only one argument",
218 cs->path, cs->line, whoami->name);
221 VALUE(cs->config, char *) = xstrdup(vec[0]);
225 static int set_stringlist(const struct config_state *cs,
226 const struct conf *whoami,
227 int nvec, char **vec) {
229 struct stringlist *sl;
231 sl = ADDRESS(cs->config, struct stringlist);
233 for(n = 0; n < nvec; ++n) {
235 sl->s = xrealloc(sl->s, (sl->n * sizeof (char *)));
236 sl->s[sl->n - 1] = xstrdup(vec[n]);
241 static int set_integer(const struct config_state *cs,
242 const struct conf *whoami,
243 int nvec, char **vec) {
247 error(0, "%s:%d: '%s' takes only one argument",
248 cs->path, cs->line, whoami->name);
251 if(xstrtol(ADDRESS(cs->config, long), vec[0], &e, 0)) {
252 error(errno, "%s:%d: converting integer", cs->path, cs->line);
256 error(0, "%s:%d: invalid integer syntax", cs->path, cs->line);
262 static int set_stringlist_accum(const struct config_state *cs,
263 const struct conf *whoami,
264 int nvec, char **vec) {
266 struct stringlist *s;
267 struct stringlistlist *sll;
269 sll = ADDRESS(cs->config, struct stringlistlist);
275 sll->s = xrealloc(sll->s, (sll->n * sizeof (struct stringlist)));
276 s = &sll->s[sll->n - 1];
278 s->s = xmalloc((nvec + 1) * sizeof (char *));
279 for(n = 0; n < nvec; ++n)
280 s->s[n] = xstrdup(vec[n]);
284 static int set_string_accum(const struct config_state *cs,
285 const struct conf *whoami,
286 int nvec, char **vec) {
288 struct stringlist *sl;
290 sl = ADDRESS(cs->config, struct stringlist);
295 for(n = 0; n < nvec; ++n) {
297 sl->s = xrealloc(sl->s, (sl->n * sizeof (char *)));
298 sl->s[sl->n - 1] = xstrdup(vec[n]);
303 static int set_restrict(const struct config_state *cs,
304 const struct conf *whoami,
305 int nvec, char **vec) {
309 static const struct restriction {
313 { "remove", RESTRICT_REMOVE },
314 { "scratch", RESTRICT_SCRATCH },
315 { "move", RESTRICT_MOVE },
318 for(n = 0; n < nvec; ++n) {
319 if((i = TABLE_FIND(restrictions, name, vec[n])) < 0) {
320 error(0, "%s:%d: invalid restriction '%s'",
321 cs->path, cs->line, vec[n]);
324 r |= restrictions[i].bit;
326 VALUE(cs->config, unsigned) = r;
330 static int parse_sample_format(const struct config_state *cs,
331 struct stream_header *format,
332 int nvec, char **vec) {
337 error(0, "%s:%d: wrong number of arguments", cs->path, cs->line);
340 if(xstrtol(&t, p, &p, 0)) {
341 error(errno, "%s:%d: converting bits-per-sample", cs->path, cs->line);
344 if(t != 8 && t != 16) {
345 error(0, "%s:%d: bad bite-per-sample (%ld)", cs->path, cs->line, t);
348 if(format) format->bits = t;
350 case 'l': case 'L': t = ENDIAN_LITTLE; p++; break;
351 case 'b': case 'B': t = ENDIAN_BIG; p++; break;
352 default: t = ENDIAN_NATIVE; break;
354 if(format) format->endian = t;
356 error(errno, "%s:%d: expected `/' after bits-per-sample",
361 if(xstrtol(&t, p, &p, 0)) {
362 error(errno, "%s:%d: converting sample-rate", cs->path, cs->line);
365 if(t < 1 || t > INT_MAX) {
366 error(0, "%s:%d: silly sample-rate (%ld)", cs->path, cs->line, t);
369 if(format) format->rate = t;
371 error(0, "%s:%d: expected `/' after sample-rate",
376 if(xstrtol(&t, p, &p, 0)) {
377 error(errno, "%s:%d: converting channels", cs->path, cs->line);
381 error(0, "%s:%d: silly number (%ld) of channels", cs->path, cs->line, t);
384 if(format) format->channels = t;
386 error(0, "%s:%d: junk after channels", cs->path, cs->line);
392 static int set_sample_format(const struct config_state *cs,
393 const struct conf *whoami,
394 int nvec, char **vec) {
395 return parse_sample_format(cs, ADDRESS(cs->config, struct stream_header),
399 static int set_namepart(const struct config_state *cs,
400 const struct conf *whoami,
401 int nvec, char **vec) {
402 struct namepartlist *npl = ADDRESS(cs->config, struct namepartlist);
409 error(0, "%s:%d: namepart needs at least 3 arguments", cs->path, cs->line);
413 error(0, "%s:%d: namepart needs at most 5 arguments", cs->path, cs->line);
416 reflags = nvec >= 5 ? regsub_flags(vec[4]) : 0;
417 if(!(re = pcre_compile(vec[1],
419 |regsub_compile_options(reflags),
420 &errstr, &erroffset, 0))) {
421 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
422 cs->path, cs->line, vec[1], errstr, erroffset);
425 npl->s = xrealloc(npl->s, (npl->n + 1) * sizeof (struct namepart));
426 npl->s[npl->n].part = xstrdup(vec[0]);
427 npl->s[npl->n].re = re;
428 npl->s[npl->n].res = xstrdup(vec[1]);
429 npl->s[npl->n].replace = xstrdup(vec[2]);
430 npl->s[npl->n].context = xstrdup(vec[3]);
431 npl->s[npl->n].reflags = reflags;
433 /* XXX a bit of a bodge; relies on there being very few parts. */
434 for(n = 0; (n < cs->config->nparts
435 && strcmp(cs->config->parts[n], vec[0])); ++n)
437 if(n >= cs->config->nparts) {
438 cs->config->parts = xrealloc(cs->config->parts,
439 (cs->config->nparts + 1) * sizeof (char *));
440 cs->config->parts[cs->config->nparts++] = xstrdup(vec[0]);
445 static int set_transform(const struct config_state *cs,
446 const struct conf *whoami,
447 int nvec, char **vec) {
448 struct transformlist *tl = ADDRESS(cs->config, struct transformlist);
455 error(0, "%s:%d: transform needs at least 3 arguments", cs->path, cs->line);
459 error(0, "%s:%d: transform needs at most 5 arguments", cs->path, cs->line);
462 reflags = (nvec >= 5 ? regsub_flags(vec[4]) : 0);
463 if(!(re = pcre_compile(vec[1],
465 |regsub_compile_options(reflags),
466 &errstr, &erroffset, 0))) {
467 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
468 cs->path, cs->line, vec[1], errstr, erroffset);
471 tl->t = xrealloc(tl->t, (tl->n + 1) * sizeof (struct namepart));
472 tl->t[tl->n].type = xstrdup(vec[0]);
473 tl->t[tl->n].context = xstrdup(vec[3] ? vec[3] : "*");
474 tl->t[tl->n].re = re;
475 tl->t[tl->n].replace = xstrdup(vec[2]);
476 tl->t[tl->n].flags = reflags;
481 static int set_rights(const struct config_state *cs,
482 const struct conf *whoami,
483 int nvec, char **vec) {
485 error(0, "%s:%d: '%s' requires one argument",
486 cs->path, cs->line, whoami->name);
489 if(parse_rights(vec[0], 0, 1)) {
490 error(0, "%s:%d: invalid rights string '%s'",
491 cs->path, cs->line, vec[0]);
494 *ADDRESS(cs->config, char *) = vec[0];
498 static int set_netaddress(const struct config_state *cs,
499 const struct conf *whoami,
500 int nvec, char **vec) {
501 struct netaddress *na = ADDRESS(cs->config, struct netaddress);
503 if(netaddress_parse(na, nvec, vec)) {
504 error(0, "%s:%d: invalid network address", cs->path, cs->line);
512 static void free_none(struct config attribute((unused)) *c,
513 const struct conf attribute((unused)) *whoami) {
516 static void free_string(struct config *c,
517 const struct conf *whoami) {
518 xfree(VALUE(c, char *));
519 VALUE(c, char *) = 0;
522 static void free_stringlist(struct config *c,
523 const struct conf *whoami) {
525 struct stringlist *sl = ADDRESS(c, struct stringlist);
527 for(n = 0; n < sl->n; ++n)
532 static void free_stringlistlist(struct config *c,
533 const struct conf *whoami) {
535 struct stringlistlist *sll = ADDRESS(c, struct stringlistlist);
536 struct stringlist *sl;
538 for(n = 0; n < sll->n; ++n) {
540 for(m = 0; m < sl->n; ++m)
547 static void free_collectionlist(struct config *c,
548 const struct conf *whoami) {
549 struct collectionlist *cll = ADDRESS(c, struct collectionlist);
550 struct collection *cl;
553 for(n = 0; n < cll->n; ++n) {
562 static void free_namepartlist(struct config *c,
563 const struct conf *whoami) {
564 struct namepartlist *npl = ADDRESS(c, struct namepartlist);
568 for(n = 0; n < npl->n; ++n) {
571 pcre_free(np->re); /* ...whatever pcre_free is set to. */
579 static void free_transformlist(struct config *c,
580 const struct conf *whoami) {
581 struct transformlist *tl = ADDRESS(c, struct transformlist);
585 for(n = 0; n < tl->n; ++n) {
588 pcre_free(t->re); /* ...whatever pcre_free is set to. */
595 static void free_netaddress(struct config *c,
596 const struct conf *whoami) {
597 struct netaddress *na = ADDRESS(c, struct netaddress);
602 /* configuration types */
604 static const struct conftype
605 type_signal = { set_signal, free_none },
606 type_collections = { set_collections, free_collectionlist },
607 type_boolean = { set_boolean, free_none },
608 type_string = { set_string, free_string },
609 type_stringlist = { set_stringlist, free_stringlist },
610 type_integer = { set_integer, free_none },
611 type_stringlist_accum = { set_stringlist_accum, free_stringlistlist },
612 type_string_accum = { set_string_accum, free_stringlist },
613 type_sample_format = { set_sample_format, free_none },
614 type_restrict = { set_restrict, free_none },
615 type_namepart = { set_namepart, free_namepartlist },
616 type_transform = { set_transform, free_transformlist },
617 type_netaddress = { set_netaddress, free_netaddress },
618 type_rights = { set_rights, free_none };
620 /* specific validation routine */
622 #define VALIDATE_FILE(test, what) do { \
626 for(n = 0; n < nvec; ++n) { \
627 if(stat(vec[n], &sb) < 0) { \
628 error(errno, "%s:%d: %s", cs->path, cs->line, vec[n]); \
631 if(!test(sb.st_mode)) { \
632 error(0, "%s:%d: %s is not a %s", \
633 cs->path, cs->line, vec[n], what); \
639 static int validate_isabspath(const struct config_state *cs,
640 int nvec, char **vec) {
643 for(n = 0; n < nvec; ++n)
644 if(vec[n][0] != '/') {
645 error(errno, "%s:%d: %s: not an absolute path",
646 cs->path, cs->line, vec[n]);
652 static int validate_isdir(const struct config_state *cs,
653 int nvec, char **vec) {
654 VALIDATE_FILE(S_ISDIR, "directory");
658 static int validate_isreg(const struct config_state *cs,
659 int nvec, char **vec) {
660 VALIDATE_FILE(S_ISREG, "regular file");
664 static int validate_player(const struct config_state *cs,
666 char attribute((unused)) **vec) {
668 error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
675 static int validate_tracklength(const struct config_state *cs,
677 char attribute((unused)) **vec) {
679 error(0, "%s:%d: should be at least 'tracklength PATTERN MODULE'",
686 static int validate_allow(const struct config_state *cs,
688 char attribute((unused)) **vec) {
690 error(0, "%s:%d: must be 'allow NAME PASS'", cs->path, cs->line);
696 static int validate_non_negative(const struct config_state *cs,
697 int nvec, char **vec) {
701 error(0, "%s:%d: missing argument", cs->path, cs->line);
705 error(0, "%s:%d: too many arguments", cs->path, cs->line);
708 if(xstrtol(&n, vec[0], 0, 0)) {
709 error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno));
713 error(0, "%s:%d: must not be negative", cs->path, cs->line);
719 static int validate_positive(const struct config_state *cs,
720 int nvec, char **vec) {
724 error(0, "%s:%d: missing argument", cs->path, cs->line);
728 error(0, "%s:%d: too many arguments", cs->path, cs->line);
731 if(xstrtol(&n, vec[0], 0, 0)) {
732 error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno));
736 error(0, "%s:%d: must be positive", cs->path, cs->line);
742 static int validate_isauser(const struct config_state *cs,
743 int attribute((unused)) nvec,
747 if(!(pw = getpwnam(vec[0]))) {
748 error(0, "%s:%d: no such user as '%s'", cs->path, cs->line, vec[0]);
754 static int validate_sample_format(const struct config_state *cs,
755 int attribute((unused)) nvec,
757 return parse_sample_format(cs, 0, nvec, vec);
760 static int validate_any(const struct config_state attribute((unused)) *cs,
761 int attribute((unused)) nvec,
762 char attribute((unused)) **vec) {
766 static int validate_url(const struct config_state attribute((unused)) *cs,
767 int attribute((unused)) nvec,
771 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
772 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
774 n = strspn(s, ("abcdefghijklmnopqrstuvwxyz"
775 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
778 error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
781 if(!strncmp(s, "http:", 5)
782 || !strncmp(s, "https:", 6)) {
784 /* we only do a rather cursory check */
785 if(strncmp(s, "//", 2)) {
786 error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]);
793 static int validate_alias(const struct config_state *cs,
797 int in_brackets = 0, c;
800 error(0, "%s:%d: missing argument", cs->path, cs->line);
804 error(0, "%s:%d: too many arguments", cs->path, cs->line);
808 while((c = (unsigned char)*s++)) {
812 else if(!isalnum(c)) {
813 error(0, "%s:%d: invalid part name in alias expansion in '%s'",
814 cs->path, cs->line, vec[0]);
822 } else if(c == '\\') {
823 if(!(c = (unsigned char)*s++)) {
824 error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
825 cs->path, cs->line, vec[0]);
827 } else if(c != '\\' && c != '{') {
828 error(0, "%s:%d: invalid escape in alias expansion in '%s'",
829 cs->path, cs->line, vec[0]);
837 error(0, "%s:%d: unterminated part name in alias expansion in '%s'",
838 cs->path, cs->line, vec[0]);
844 static int validate_algo(const struct config_state attribute((unused)) *cs,
848 error(0, "%s:%d: invalid algorithm specification", cs->path, cs->line);
851 if(!valid_authhash(vec[0])) {
852 error(0, "%s:%d: unsuported algorithm '%s'", cs->path, cs->line, vec[0]);
858 static int validate_backend(const struct config_state attribute((unused)) *cs,
863 error(0, "%s:%d: invalid sound API specification", cs->path, cs->line);
866 if(!strcmp(vec[0], "network")) {
867 error(0, "'api network' is deprecated; use 'api rtp'");
870 if(config_uaudio_apis) {
871 for(n = 0; config_uaudio_apis[n]; ++n)
872 if(!strcmp(vec[0], config_uaudio_apis[n]->name))
874 error(0, "%s:%d: unrecognized sound API '%s'", cs->path, cs->line, vec[0]);
877 /* In non-server processes we have no idea what's valid */
881 static int validate_pausemode(const struct config_state attribute((unused)) *cs,
884 if(nvec == 1 && (!strcmp(vec[0], "silence") || !strcmp(vec[0], "suspend")))
886 error(0, "%s:%d: invalid pause mode", cs->path, cs->line);
890 static int validate_destaddr(const struct config_state attribute((unused)) *cs,
893 struct netaddress na[1];
895 if(netaddress_parse(na, nvec, vec)) {
896 error(0, "%s:%d: invalid network address", cs->path, cs->line);
900 error(0, "%s:%d: destination address required", cs->path, cs->line);
906 /** @brief Item name and and offset */
907 #define C(x) #x, offsetof(struct config, x)
908 /** @brief Item name and and offset */
909 #define C2(x,y) #x, offsetof(struct config, y)
911 /** @brief All configuration items */
912 static const struct conf conf[] = {
913 { C(alias), &type_string, validate_alias },
914 { C(allow), &type_stringlist_accum, validate_allow },
915 { C(api), &type_string, validate_backend },
916 { C(authorization_algorithm), &type_string, validate_algo },
917 { C(broadcast), &type_netaddress, validate_destaddr },
918 { C(broadcast_from), &type_netaddress, validate_any },
919 { C(channel), &type_string, validate_any },
920 { C(checkpoint_kbyte), &type_integer, validate_non_negative },
921 { C(checkpoint_min), &type_integer, validate_non_negative },
922 { C(collection), &type_collections, validate_any },
923 { C(connect), &type_netaddress, validate_destaddr },
924 { C(cookie_login_lifetime), &type_integer, validate_positive },
925 { C(cookie_key_lifetime), &type_integer, validate_positive },
926 { C(dbversion), &type_integer, validate_positive },
927 { C(default_rights), &type_rights, validate_any },
928 { C(device), &type_string, validate_any },
929 { C(gap), &type_integer, validate_non_negative },
930 { C(history), &type_integer, validate_positive },
931 { C(home), &type_string, validate_isabspath },
932 { C(listen), &type_netaddress, validate_any },
933 { C(lock), &type_boolean, validate_any },
934 { C(mail_sender), &type_string, validate_any },
935 { C(mixer), &type_string, validate_any },
936 { C(multicast_loop), &type_boolean, validate_any },
937 { C(multicast_ttl), &type_integer, validate_non_negative },
938 { C(namepart), &type_namepart, validate_any },
939 { C(new_bias), &type_integer, validate_positive },
940 { C(new_bias_age), &type_integer, validate_positive },
941 { C(new_max), &type_integer, validate_positive },
942 { C2(nice, nice_rescan), &type_integer, validate_non_negative },
943 { C(nice_rescan), &type_integer, validate_non_negative },
944 { C(nice_server), &type_integer, validate_any },
945 { C(nice_speaker), &type_integer, validate_any },
946 { C(noticed_history), &type_integer, validate_positive },
947 { C(password), &type_string, validate_any },
948 { C(pause_mode), &type_string, validate_pausemode },
949 { C(player), &type_stringlist_accum, validate_player },
950 { C(playlist_lock_timeout), &type_integer, validate_positive },
951 { C(playlist_max) , &type_integer, validate_positive },
952 { C(plugins), &type_string_accum, validate_isdir },
953 { C(prefsync), &type_integer, validate_positive },
954 { C(queue_pad), &type_integer, validate_positive },
955 { C(replay_min), &type_integer, validate_non_negative },
956 { C(refresh), &type_integer, validate_positive },
957 { C(reminder_interval), &type_integer, validate_positive },
958 { C(remote_userman), &type_boolean, validate_any },
959 { C2(restrict, restrictions), &type_restrict, validate_any },
960 { C(rtp_delay_threshold), &type_integer, validate_positive },
961 { C(sample_format), &type_sample_format, validate_sample_format },
962 { C(scratch), &type_string_accum, validate_isreg },
963 { C(sendmail), &type_string, validate_isabspath },
964 { C(short_display), &type_integer, validate_positive },
965 { C(signal), &type_signal, validate_any },
966 { C(smtp_server), &type_string, validate_any },
967 { C(sox_generation), &type_integer, validate_non_negative },
968 { C2(speaker_backend, api), &type_string, validate_backend },
969 { C(speaker_command), &type_string, validate_any },
970 { C(stopword), &type_string_accum, validate_any },
971 { C(templates), &type_string_accum, validate_isdir },
972 { C(tracklength), &type_stringlist_accum, validate_tracklength },
973 { C(transform), &type_transform, validate_any },
974 { C(trust), &type_string_accum, validate_any },
975 { C(url), &type_string, validate_url },
976 { C(user), &type_string, validate_isauser },
977 { C(username), &type_string, validate_any },
980 /** @brief Find a configuration item's definition by key */
981 static const struct conf *find(const char *key) {
984 if((n = TABLE_FIND(conf, name, key)) < 0)
989 /** @brief Set a new configuration value */
990 static int config_set(const struct config_state *cs,
991 int nvec, char **vec) {
992 const struct conf *which;
994 D(("config_set %s", vec[0]));
995 if(!(which = find(vec[0]))) {
996 error(0, "%s:%d: unknown configuration key '%s'",
997 cs->path, cs->line, vec[0]);
1000 return (which->validate(cs, nvec - 1, vec + 1)
1001 || which->type->set(cs, which, nvec - 1, vec + 1));
1004 static int config_set_args(const struct config_state *cs,
1005 const char *which, ...) {
1011 vector_append(v, (char *)which);
1012 va_start(ap, which);
1013 while((s = va_arg(ap, char *)))
1014 vector_append(v, s);
1016 vector_terminate(v);
1017 return config_set(cs, v->nvec, v->vec);
1020 /** @brief Error callback used by config_include() */
1021 static void config_error(const char *msg, void *u) {
1022 const struct config_state *cs = u;
1024 error(0, "%s:%d: %s", cs->path, cs->line, msg);
1027 /** @brief Include a file by name */
1028 static int config_include(struct config *c, const char *path) {
1030 char *buffer, *inputbuffer, **vec;
1032 struct config_state cs;
1037 D(("%s: reading configuration", path));
1038 if(!(fp = fopen(path, "r"))) {
1039 error(errno, "error opening %s", path);
1042 while(!inputline(path, fp, &inputbuffer, '\n')) {
1044 if(!(buffer = mb2utf8(inputbuffer))) {
1045 error(errno, "%s:%d: cannot convert to UTF-8", cs.path, cs.line);
1051 if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES,
1052 config_error, &cs))) {
1058 if(!strcmp(vec[0], "include")) {
1060 error(0, "%s:%d: must be 'include PATH'", cs.path, cs.line);
1063 config_include(c, vec[1]);
1065 ret |= config_set(&cs, n, vec);
1067 for(n = 0; vec[n]; ++n) xfree(vec[n]);
1072 error(errno, "error reading %s", path);
1079 static const char *const default_stopwords[] = {
1138 #define NDEFAULT_STOPWORDS (sizeof default_stopwords / sizeof *default_stopwords)
1140 static const char *const default_players[] = {
1146 #define NDEFAULT_PLAYERS (sizeof default_players / sizeof *default_players)
1148 /** @brief Make a new default configuration */
1149 static struct config *config_default(void) {
1150 struct config *c = xmalloc(sizeof *c);
1151 const char *logname;
1153 struct config_state cs;
1156 cs.path = "<internal>";
1159 /* Strings had better be xstrdup'd as they will get freed at some point. */
1162 c->home = xstrdup(pkgstatedir);
1163 if(!(pw = getpwuid(getuid())))
1164 fatal(0, "cannot determine our username");
1165 logname = pw->pw_name;
1166 c->username = xstrdup(logname);
1169 c->signal = SIGKILL;
1170 c->alias = xstrdup("{/artist}{/album}{/title}{ext}");
1172 c->device = xstrdup("default");
1173 c->nice_rescan = 10;
1174 c->speaker_command = 0;
1175 c->sample_format.bits = 16;
1176 c->sample_format.rate = 44100;
1177 c->sample_format.channels = 2;
1178 c->sample_format.endian = ENDIAN_NATIVE;
1180 c->replay_min = 8 * 3600;
1182 c->multicast_ttl = 1;
1183 c->multicast_loop = 1;
1184 c->authorization_algorithm = xstrdup("sha1");
1185 c->noticed_history = 31;
1186 c->short_display = 32;
1190 c->cookie_login_lifetime = 86400;
1191 c->cookie_key_lifetime = 86400 * 7;
1192 if(sendmail_binary[0] && strcmp(sendmail_binary, "none"))
1193 c->sendmail = xstrdup(sendmail_binary);
1194 c->smtp_server = xstrdup("127.0.0.1");
1196 c->reminder_interval = 600; /* 10m */
1197 c->new_bias_age = 7 * 86400; /* 1 week */
1198 c->new_bias = 4500000; /* 50 times the base weight */
1199 c->sox_generation = DEFAULT_SOX_GENERATION;
1200 c->playlist_max = INT_MAX; /* effectively no limit */
1201 c->playlist_lock_timeout = 10; /* 10s */
1202 /* Default stopwords */
1203 if(config_set(&cs, (int)NDEFAULT_STOPWORDS, (char **)default_stopwords))
1205 /* Default player configuration */
1206 for(n = 0; n < NDEFAULT_PLAYERS; ++n) {
1207 if(config_set_args(&cs, "player",
1208 default_players[n], "execraw", "disorder-decode", (char *)0))
1210 if(config_set_args(&cs, "tracklength",
1211 default_players[n], "disorder-tracklength", (char *)0))
1214 c->broadcast.af = -1;
1215 c->broadcast_from.af = -1;
1221 char *config_get_file2(struct config *c, const char *name) {
1224 byte_xasprintf(&s, "%s/%s", c->home, name);
1228 /** @brief Set the default configuration file */
1229 static void set_configfile(void) {
1231 byte_xasprintf(&configfile, "%s/config", pkgconfdir);
1234 /** @brief Free a configuration object */
1235 static void config_free(struct config *c) {
1239 for(n = 0; n < (int)(sizeof conf / sizeof *conf); ++n)
1240 conf[n].type->free(c, &conf[n]);
1241 for(n = 0; n < c->nparts; ++n)
1248 /** @brief Set post-parse defaults */
1249 static void config_postdefaults(struct config *c,
1251 struct config_state cs;
1252 const struct conf *whoami;
1255 static const char *namepart[][4] = {
1256 { "title", "/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
1257 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1258 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1259 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1260 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1262 #define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1264 static const char *transform[][5] = {
1265 { "track", "^.*/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
1266 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1267 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1268 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1269 { "dir", "[[:punct:]]", "", "sort", "g", }
1271 #define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1273 cs.path = "<internal>";
1276 if(!c->namepart.n) {
1277 whoami = find("namepart");
1278 for(n = 0; n < NNAMEPART; ++n)
1279 set_namepart(&cs, whoami, 4, (char **)namepart[n]);
1281 if(!c->transform.n) {
1282 whoami = find("transform");
1283 for(n = 0; n < NTRANSFORM; ++n)
1284 set_transform(&cs, whoami, 5, (char **)transform[n]);
1287 if(c->speaker_command)
1288 c->api = xstrdup("command");
1289 else if(c->broadcast.af != -1)
1290 c->api = xstrdup("rtp");
1291 else if(config_uaudio_apis)
1292 c->api = xstrdup(config_uaudio_apis[0]->name);
1294 c->api = xstrdup("<none>");
1296 if(!strcmp(c->api, "network"))
1297 c->api = xstrdup("rtp");
1299 if(!strcmp(c->api, "command") && !c->speaker_command)
1300 fatal(0, "'api command' but speaker_command is not set");
1301 if((!strcmp(c->api, "rtp")) && c->broadcast.af == -1)
1302 fatal(0, "'api rtp' but broadcast is not set");
1304 /* Override sample format */
1305 if(!strcmp(c->api, "rtp")) {
1306 c->sample_format.rate = 44100;
1307 c->sample_format.channels = 2;
1308 c->sample_format.bits = 16;
1309 c->sample_format.endian = ENDIAN_NATIVE;
1311 if(!strcmp(c->api, "coreaudio")) {
1312 c->sample_format.rate = 44100;
1313 c->sample_format.channels = 2;
1314 c->sample_format.bits = 16;
1315 c->sample_format.endian = ENDIAN_NATIVE;
1317 if(!c->default_rights) {
1318 rights_type r = RIGHTS__MASK & ~(RIGHT_ADMIN|RIGHT_REGISTER
1320 |RIGHT_SCRATCH__MASK
1321 |RIGHT_REMOVE__MASK);
1322 /* The idea is to approximate the meaning of the old 'restrict' directive
1323 * in the default rights if they are not overridden. */
1324 if(c->restrictions & RESTRICT_SCRATCH)
1325 r |= RIGHT_SCRATCH_MINE|RIGHT_SCRATCH_RANDOM;
1327 r |= RIGHT_SCRATCH_ANY;
1328 if(!(c->restrictions & RESTRICT_MOVE))
1329 r |= RIGHT_MOVE_ANY;
1330 if(c->restrictions & RESTRICT_REMOVE)
1331 r |= RIGHT_REMOVE_MINE;
1333 r |= RIGHT_REMOVE_ANY;
1334 c->default_rights = rights_string(r);
1338 /** @brief (Re-)read the config file
1339 * @param server If set, do extra checking
1340 * @param oldconfig Old configuration for compatibility check
1341 * @return 0 on success, non-0 on error
1343 * If @p oldconfig is set, then certain compatibility checks are done between
1344 * the old and new configurations.
1346 int config_read(int server,
1347 const struct config *oldconfig) {
1353 c = config_default();
1354 /* standalone Disobedience installs might not have a global config file */
1355 if(access(configfile, F_OK) == 0)
1356 if(config_include(c, configfile))
1358 /* if we can read the private config file, do */
1359 if((privconf = config_private())
1360 && access(privconf, R_OK) == 0
1361 && config_include(c, privconf))
1364 /* if there's a per-user system config file for this user, read it */
1365 if(config_per_user) {
1366 if(!(pw = getpwuid(getuid())))
1367 fatal(0, "cannot determine our username");
1368 if((privconf = config_usersysconf(pw))
1369 && access(privconf, F_OK) == 0
1370 && config_include(c, privconf))
1373 /* if we have a password file, read it */
1374 if((privconf = config_userconf(0, pw))
1375 && access(privconf, F_OK) == 0
1376 && config_include(c, privconf))
1380 /* install default namepart and transform settings */
1381 config_postdefaults(c, server);
1384 if(strcmp(c->home, oldconfig->home)) {
1385 error(0, "'home' cannot be changed without a restart");
1388 if(strcmp(c->alias, oldconfig->alias)) {
1389 error(0, "'alias' cannot be changed without a restart");
1392 if(strcmp(c->user, oldconfig->user)) {
1393 error(0, "'user' cannot be changed without a restart");
1396 if(c->nice_speaker != oldconfig->nice_speaker) {
1397 error(0, "'nice_speaker' cannot be changed without a restart");
1398 /* ...but we accept the new config anyway */
1400 if(c->nice_server != oldconfig->nice_server) {
1401 error(0, "'nice_server' cannot be changed without a restart");
1402 /* ...but we accept the new config anyway */
1404 if(namepartlist_compare(&c->namepart, &oldconfig->namepart)) {
1405 error(0, "'namepart' settings cannot be changed without a restart");
1408 if(stringlist_compare(&c->stopword, &oldconfig->stopword)) {
1409 error(0, "'stopword' settings cannot be changed without a restart");
1413 error(0, "not installing incompatible new configuration");
1417 /* everything is good so we shall use the new config */
1418 config_free(config);
1419 /* warn about obsolete directives */
1421 error(0, "'restrict' will be removed in a future version");
1423 error(0, "'allow' will be removed in a future version");
1425 error(0, "'trust' will be removed in a future version");
1427 error(0, "'lock' will be removed in a future version");
1429 error(0, "'gap' will be removed in a future version");
1431 error(0, "'prefsync' will be removed in a future version");
1436 /** @brief Return the path to the private configuration file */
1437 char *config_private(void) {
1441 byte_xasprintf(&s, "%s.private", configfile);
1445 /** @brief Return the path to user's personal configuration file */
1446 char *config_userconf(const char *home, const struct passwd *pw) {
1449 if(!home && !pw && !(pw = getpwuid(getuid())))
1450 fatal(0, "cannot determine our username");
1451 byte_xasprintf(&s, "%s/.disorder/passwd", home ? home : pw->pw_dir);
1455 /** @brief Return the path to user-specific system configuration */
1456 char *config_usersysconf(const struct passwd *pw) {
1460 if(!strchr(pw->pw_name, '/')) {
1461 byte_xasprintf(&s, "%s.%s", configfile, pw->pw_name);
1467 char *config_get_file(const char *name) {
1468 return config_get_file2(config, name);
1471 static int stringlist_compare(const struct stringlist *a,
1472 const struct stringlist *b) {
1475 while(n < a->n && n < b->n) {
1476 if((c = strcmp(a->s[n], b->s[n])))
1482 else if(a->n > b->n)
1488 static int namepart_compare(const struct namepart *a,
1489 const struct namepart *b) {
1492 if((c = strcmp(a->part, b->part)))
1494 if((c = strcmp(a->res, b->res)))
1496 if((c = strcmp(a->replace, b->replace)))
1498 if((c = strcmp(a->context, b->context)))
1500 if(a->reflags > b->reflags)
1502 if(a->reflags < b->reflags)
1507 static int namepartlist_compare(const struct namepartlist *a,
1508 const struct namepartlist *b) {
1511 while(n < a->n && n < b->n) {
1512 if((c = namepart_compare(&a->s[n], &b->s[n])))
1518 else if(a->n < b->n)