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>
48 #include "configuration.h"
54 #include "inputline.h"
68 /** @brief Path to config file
70 * set_configfile() sets the default if it is null.
74 /** @brief Read user configuration
76 * If clear, the user-specific configuration is not read.
78 int config_per_user
= 1;
81 /** @brief Table of audio APIs
83 * Only set in server processes.
85 const struct uaudio
*const *config_uaudio_apis
;
88 /** @brief Config file parser state */
90 /** @brief Filename */
93 /** @brief Line number */
96 /** @brief Configuration object under construction */
97 struct config
*config
;
100 /** @brief Current configuration */
101 struct config
*config
;
103 /** @brief One configuration item */
105 /** @brief Name as it appears in the config file */
108 /** @brief Offset in @ref config structure */
111 /** @brief Pointer to item type */
112 const struct conftype
*type
;
114 /** @brief Pointer to item-specific validation routine
115 * @param cs Configuration state
116 * @param nvec Length of (proposed) new value
117 * @param vec Elements of new value
118 * @return 0 on success, non-0 on error
120 * The validate function should report any error it detects.
122 int (*validate
)(const struct config_state
*cs
,
123 int nvec
, char **vec
);
126 /** @brief Type of a configuration item */
128 /** @brief Pointer to function to set item
129 * @param cs Configuration state
130 * @param whoami Configuration item to set
131 * @param nvec Length of new value
132 * @param vec New value
133 * @return 0 on success, non-0 on error
135 int (*set
)(const struct config_state
*cs
,
136 const struct conf
*whoami
,
137 int nvec
, char **vec
);
139 /** @brief Pointer to function to free item
140 * @param c Configuration structure to free an item of
141 * @param whoami Configuration item to free
143 void (*free
)(struct config
*c
, const struct conf
*whoami
);
146 /** @brief Compute the address of an item */
147 #define ADDRESS(C, TYPE) ((TYPE *)((char *)(C) + whoami->offset))
148 /** @brief Return the value of an item */
149 #define VALUE(C, TYPE) (*ADDRESS(C, TYPE))
151 static int stringlist_compare(const struct stringlist
*a
,
152 const struct stringlist
*b
);
153 static int namepartlist_compare(const struct namepartlist
*a
,
154 const struct namepartlist
*b
);
156 static int set_signal(const struct config_state
*cs
,
157 const struct conf
*whoami
,
158 int nvec
, char **vec
) {
162 disorder_error(0, "%s:%d: '%s' requires one argument",
163 cs
->path
, cs
->line
, whoami
->name
);
166 if((n
= find_signal(vec
[0])) == -1) {
167 disorder_error(0, "%s:%d: unknown signal '%s'",
168 cs
->path
, cs
->line
, vec
[0]);
171 VALUE(cs
->config
, int) = n
;
175 static int set_collections(const struct config_state
*cs
,
176 const struct conf
*whoami
,
177 int nvec
, char **vec
) {
178 struct collectionlist
*cl
;
179 const char *root
, *encoding
, *module
;
198 disorder_error(0, "%s:%d: '%s' requires at least one argument",
199 cs
->path
, cs
->line
, whoami
->name
);
202 disorder_error(0, "%s:%d: '%s' requires at most three arguments",
203 cs
->path
, cs
->line
, whoami
->name
);
206 /* Sanity check root */
208 disorder_error(0, "%s:%d: collection root must start with '/'",
212 if(root
[1] && root
[strlen(root
)-1] == '/') {
213 disorder_error(0, "%s:%d: collection root must not end with '/'",
222 encoding
= nl_langinfo(CODESET
);
227 cl
= ADDRESS(cs
->config
, struct collectionlist
);
229 cl
->s
= xrealloc(cl
->s
, cl
->n
* sizeof (struct collection
));
230 cl
->s
[cl
->n
- 1].module
= xstrdup(module
);
231 cl
->s
[cl
->n
- 1].encoding
= xstrdup(encoding
);
232 cl
->s
[cl
->n
- 1].root
= xstrdup(root
);
236 static int set_boolean(const struct config_state
*cs
,
237 const struct conf
*whoami
,
238 int nvec
, char **vec
) {
242 disorder_error(0, "%s:%d: '%s' takes only one argument",
243 cs
->path
, cs
->line
, whoami
->name
);
246 if(!strcmp(vec
[0], "yes")) state
= 1;
247 else if(!strcmp(vec
[0], "no")) state
= 0;
249 disorder_error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'",
250 cs
->path
, cs
->line
, whoami
->name
);
253 VALUE(cs
->config
, int) = state
;
257 static int set_string(const struct config_state
*cs
,
258 const struct conf
*whoami
,
259 int nvec
, char **vec
) {
261 disorder_error(0, "%s:%d: '%s' takes only one argument",
262 cs
->path
, cs
->line
, whoami
->name
);
265 xfree(VALUE(cs
->config
, char *));
266 VALUE(cs
->config
, char *) = xstrdup(vec
[0]);
270 static int set_stringlist(const struct config_state
*cs
,
271 const struct conf
*whoami
,
272 int nvec
, char **vec
) {
274 struct stringlist
*sl
;
276 sl
= ADDRESS(cs
->config
, struct stringlist
);
278 for(n
= 0; n
< nvec
; ++n
) {
280 sl
->s
= xrealloc(sl
->s
, (sl
->n
* sizeof (char *)));
281 sl
->s
[sl
->n
- 1] = xstrdup(vec
[n
]);
286 static int set_integer(const struct config_state
*cs
,
287 const struct conf
*whoami
,
288 int nvec
, char **vec
) {
292 disorder_error(0, "%s:%d: '%s' takes only one argument",
293 cs
->path
, cs
->line
, whoami
->name
);
296 if(xstrtol(ADDRESS(cs
->config
, long), vec
[0], &e
, 0)) {
297 disorder_error(errno
, "%s:%d: converting integer", cs
->path
, cs
->line
);
301 disorder_error(0, "%s:%d: invalid integer syntax", cs
->path
, cs
->line
);
307 static int set_stringlist_accum(const struct config_state
*cs
,
308 const struct conf
*whoami
,
309 int nvec
, char **vec
) {
311 struct stringlist
*s
;
312 struct stringlistlist
*sll
;
314 sll
= ADDRESS(cs
->config
, struct stringlistlist
);
320 sll
->s
= xrealloc(sll
->s
, (sll
->n
* sizeof (struct stringlist
)));
321 s
= &sll
->s
[sll
->n
- 1];
323 s
->s
= xmalloc((nvec
+ 1) * sizeof (char *));
324 for(n
= 0; n
< nvec
; ++n
)
325 s
->s
[n
] = xstrdup(vec
[n
]);
329 static int set_string_accum(const struct config_state
*cs
,
330 const struct conf
*whoami
,
331 int nvec
, char **vec
) {
333 struct stringlist
*sl
;
335 sl
= ADDRESS(cs
->config
, struct stringlist
);
340 for(n
= 0; n
< nvec
; ++n
) {
342 sl
->s
= xrealloc(sl
->s
, (sl
->n
* sizeof (char *)));
343 sl
->s
[sl
->n
- 1] = xstrdup(vec
[n
]);
348 static int parse_sample_format(const struct config_state
*cs
,
349 struct stream_header
*format
,
350 int nvec
, char **vec
) {
355 disorder_error(0, "%s:%d: wrong number of arguments", cs
->path
, cs
->line
);
358 if(xstrtol(&t
, p
, &p
, 0)) {
359 disorder_error(errno
, "%s:%d: converting bits-per-sample",
363 if(t
!= 8 && t
!= 16) {
364 disorder_error(0, "%s:%d: bad bits-per-sample (%ld)",
365 cs
->path
, cs
->line
, t
);
368 if(format
) format
->bits
= (uint8_t)t
;
370 case 'l': case 'L': t
= ENDIAN_LITTLE
; p
++; break;
371 case 'b': case 'B': t
= ENDIAN_BIG
; p
++; break;
372 default: t
= ENDIAN_NATIVE
; break;
374 if(format
) format
->endian
= (uint8_t)t
;
376 disorder_error(errno
, "%s:%d: expected `/' after bits-per-sample",
381 if(xstrtol(&t
, p
, &p
, 0)) {
382 disorder_error(errno
, "%s:%d: converting sample-rate", cs
->path
, cs
->line
);
385 if(t
< 1 || t
> INT_MAX
) {
386 disorder_error(0, "%s:%d: silly sample-rate (%ld)", cs
->path
, cs
->line
, t
);
389 if(format
) format
->rate
= t
;
391 disorder_error(0, "%s:%d: expected `/' after sample-rate",
396 if(xstrtol(&t
, p
, &p
, 0)) {
397 disorder_error(errno
, "%s:%d: converting channels", cs
->path
, cs
->line
);
401 disorder_error(0, "%s:%d: silly number (%ld) of channels",
402 cs
->path
, cs
->line
, t
);
405 if(format
) format
->channels
= (uint8_t)t
;
407 disorder_error(0, "%s:%d: junk after channels", cs
->path
, cs
->line
);
413 static int set_sample_format(const struct config_state
*cs
,
414 const struct conf
*whoami
,
415 int nvec
, char **vec
) {
416 return parse_sample_format(cs
, ADDRESS(cs
->config
, struct stream_header
),
421 static int set_namepart(const struct config_state
*cs
,
422 const struct conf
*whoami
,
423 int nvec
, char **vec
) {
424 struct namepartlist
*npl
= ADDRESS(cs
->config
, struct namepartlist
);
431 disorder_error(0, "%s:%d: namepart needs at least 3 arguments",
436 disorder_error(0, "%s:%d: namepart needs at most 5 arguments",
440 reflags
= nvec
>= 5 ?
regsub_flags(vec
[4]) : 0;
441 if(!(re
= pcre_compile(vec
[1],
443 |regsub_compile_options(reflags
),
444 &errstr
, &erroffset
, 0))) {
445 disorder_error(0, "%s:%d: compiling regexp /%s/: %s (offset %d)",
446 cs
->path
, cs
->line
, vec
[1], errstr
, erroffset
);
449 npl
->s
= xrealloc(npl
->s
, (npl
->n
+ 1) * sizeof (struct namepart
));
450 npl
->s
[npl
->n
].part
= xstrdup(vec
[0]);
451 npl
->s
[npl
->n
].re
= re
;
452 npl
->s
[npl
->n
].res
= xstrdup(vec
[1]);
453 npl
->s
[npl
->n
].replace
= xstrdup(vec
[2]);
454 npl
->s
[npl
->n
].context
= xstrdup(vec
[3]);
455 npl
->s
[npl
->n
].reflags
= reflags
;
457 /* XXX a bit of a bodge; relies on there being very few parts. */
458 for(n
= 0; (n
< cs
->config
->nparts
459 && strcmp(cs
->config
->parts
[n
], vec
[0])); ++n
)
461 if(n
>= cs
->config
->nparts
) {
462 cs
->config
->parts
= xrealloc(cs
->config
->parts
,
463 (cs
->config
->nparts
+ 1) * sizeof (char *));
464 cs
->config
->parts
[cs
->config
->nparts
++] = xstrdup(vec
[0]);
469 static int set_transform(const struct config_state
*cs
,
470 const struct conf
*whoami
,
471 int nvec
, char **vec
) {
472 struct transformlist
*tl
= ADDRESS(cs
->config
, struct transformlist
);
479 disorder_error(0, "%s:%d: transform needs at least 3 arguments",
484 disorder_error(0, "%s:%d: transform needs at most 5 arguments",
488 reflags
= (nvec
>= 5 ?
regsub_flags(vec
[4]) : 0);
489 if(!(re
= pcre_compile(vec
[1],
491 |regsub_compile_options(reflags
),
492 &errstr
, &erroffset
, 0))) {
493 disorder_error(0, "%s:%d: compiling regexp /%s/: %s (offset %d)",
494 cs
->path
, cs
->line
, vec
[1], errstr
, erroffset
);
497 tl
->t
= xrealloc(tl
->t
, (tl
->n
+ 1) * sizeof (struct namepart
));
498 tl
->t
[tl
->n
].type
= xstrdup(vec
[0]);
499 tl
->t
[tl
->n
].context
= xstrdup(vec
[3] ? vec
[3] : "*");
500 tl
->t
[tl
->n
].re
= re
;
501 tl
->t
[tl
->n
].replace
= xstrdup(vec
[2]);
502 tl
->t
[tl
->n
].flags
= reflags
;
508 static int set_rights(const struct config_state
*cs
,
509 const struct conf
*whoami
,
510 int nvec
, char **vec
) {
512 disorder_error(0, "%s:%d: '%s' requires one argument",
513 cs
->path
, cs
->line
, whoami
->name
);
516 if(parse_rights(vec
[0], 0, 1)) {
517 disorder_error(0, "%s:%d: invalid rights string '%s'",
518 cs
->path
, cs
->line
, vec
[0]);
521 return set_string(cs
, whoami
, nvec
, vec
);
524 static int set_netaddress(const struct config_state
*cs
,
525 const struct conf
*whoami
,
526 int nvec
, char **vec
) {
527 struct netaddress
*na
= ADDRESS(cs
->config
, struct netaddress
);
529 if(netaddress_parse(na
, nvec
, vec
)) {
530 disorder_error(0, "%s:%d: invalid network address", cs
->path
, cs
->line
);
538 static void free_none(struct config
attribute((unused
)) *c
,
539 const struct conf
attribute((unused
)) *whoami
) {
542 static void free_string(struct config
*c
,
543 const struct conf
*whoami
) {
544 xfree(VALUE(c
, char *));
545 VALUE(c
, char *) = 0;
548 static void free_stringlist(struct config
*c
,
549 const struct conf
*whoami
) {
551 struct stringlist
*sl
= ADDRESS(c
, struct stringlist
);
553 for(n
= 0; n
< sl
->n
; ++n
)
558 static void free_stringlistlist(struct config
*c
,
559 const struct conf
*whoami
) {
561 struct stringlistlist
*sll
= ADDRESS(c
, struct stringlistlist
);
562 struct stringlist
*sl
;
564 for(n
= 0; n
< sll
->n
; ++n
) {
566 for(m
= 0; m
< sl
->n
; ++m
)
573 static void free_collectionlist(struct config
*c
,
574 const struct conf
*whoami
) {
575 struct collectionlist
*cll
= ADDRESS(c
, struct collectionlist
);
576 struct collection
*cl
;
579 for(n
= 0; n
< cll
->n
; ++n
) {
589 static void free_namepartlist(struct config
*c
,
590 const struct conf
*whoami
) {
591 struct namepartlist
*npl
= ADDRESS(c
, struct namepartlist
);
595 for(n
= 0; n
< npl
->n
; ++n
) {
598 pcre_free(np
->re
); /* ...whatever pcre_free is set to. */
606 static void free_transformlist(struct config
*c
,
607 const struct conf
*whoami
) {
608 struct transformlist
*tl
= ADDRESS(c
, struct transformlist
);
612 for(n
= 0; n
< tl
->n
; ++n
) {
615 pcre_free(t
->re
); /* ...whatever pcre_free is set to. */
623 static void free_netaddress(struct config
*c
,
624 const struct conf
*whoami
) {
625 struct netaddress
*na
= ADDRESS(c
, struct netaddress
);
630 /* configuration types */
632 static const struct conftype
633 type_signal
= { set_signal
, free_none
},
634 type_collections
= { set_collections
, free_collectionlist
},
635 type_boolean
= { set_boolean
, free_none
},
636 type_string
= { set_string
, free_string
},
637 type_stringlist
= { set_stringlist
, free_stringlist
},
638 type_integer
= { set_integer
, free_none
},
639 type_stringlist_accum
= { set_stringlist_accum
, free_stringlistlist
},
640 type_string_accum
= { set_string_accum
, free_stringlist
},
641 type_sample_format
= { set_sample_format
, free_none
},
643 type_namepart
= { set_namepart
, free_namepartlist
},
644 type_transform
= { set_transform
, free_transformlist
},
646 type_netaddress
= { set_netaddress
, free_netaddress
},
647 type_rights
= { set_rights
, free_string
};
649 /* specific validation routine */
651 /** @brief Perform a test on a filename
652 * @param test Test function to call on mode bits
653 * @param what Type of file sought
655 * If @p test returns 0 then the file is not a @p what and an error
656 * is reported and -1 is returned.
658 #define VALIDATE_FILE(test, what) do { \
662 for(n = 0; n < nvec; ++n) { \
663 if(stat(vec[n], &sb) < 0) { \
664 disorder_error(errno, "%s:%d: %s", \
665 cs->path, cs->line, vec[n]); \
668 if(!test(sb.st_mode)) { \
669 disorder_error(0, "%s:%d: %s is not a %s", \
670 cs->path, cs->line, vec[n], what); \
676 /** @brief Validate an absolute path
677 * @param cs Configuration state
678 * @param nvec Length of (proposed) new value
679 * @param vec Elements of new value
680 * @return 0 on success, non-0 on error
682 static int validate_isabspath(const struct config_state
*cs
,
683 int nvec
, char **vec
) {
686 for(n
= 0; n
< nvec
; ++n
)
687 if(vec
[n
][0] != '/') {
688 disorder_error(0, "%s:%d: %s: not an absolute path",
689 cs
->path
, cs
->line
, vec
[n
]);
695 /** @brief Validate an existing directory
696 * @param cs Configuration state
697 * @param nvec Length of (proposed) new value
698 * @param vec Elements of new value
699 * @return 0 on success, non-0 on error
701 static int validate_isdir(const struct config_state
*cs
,
702 int nvec
, char **vec
) {
703 VALIDATE_FILE(S_ISDIR
, "directory");
707 /** @brief Validate an existing regular file
708 * @param cs Configuration state
709 * @param nvec Length of (proposed) new value
710 * @param vec Elements of new value
711 * @return 0 on success, non-0 on error
713 static int validate_isreg(const struct config_state
*cs
,
714 int nvec
, char **vec
) {
715 VALIDATE_FILE(S_ISREG
, "regular file");
719 /** @brief Validate a player pattern
720 * @param cs Configuration state
721 * @param nvec Length of (proposed) new value
722 * @param vec Elements of new value
723 * @return 0 on success, non-0 on error
725 static int validate_player(const struct config_state
*cs
,
727 char attribute((unused
)) **vec
) {
728 if(nvec
&& nvec
< 2) {
729 disorder_error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
736 /** @brief Validate a track length pattern
737 * @param cs Configuration state
738 * @param nvec Length of (proposed) new value
739 * @param vec Elements of new value
740 * @return 0 on success, non-0 on error
742 static int validate_tracklength(const struct config_state
*cs
,
744 char attribute((unused
)) **vec
) {
745 if(nvec
&& nvec
< 2) {
746 disorder_error(0, "%s:%d: should be at least 'tracklength PATTERN MODULE'",
753 /** @brief Validate a non-negative (@c long) integer
754 * @param cs Configuration state
755 * @param nvec Length of (proposed) new value
756 * @param vec Elements of new value
757 * @return 0 on success, non-0 on error
759 static int validate_non_negative(const struct config_state
*cs
,
760 int nvec
, char **vec
) {
765 disorder_error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
769 disorder_error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
772 if(xstrtol(&n
, vec
[0], 0, 0)) {
773 disorder_error(0, "%s:%d: %s", cs
->path
, cs
->line
,
774 format_error(ec_errno
, errno
, errbuf
, sizeof errbuf
));
778 disorder_error(0, "%s:%d: must not be negative", cs
->path
, cs
->line
);
784 /** @brief Validate a positive (@c long) integer
785 * @param cs Configuration state
786 * @param nvec Length of (proposed) new value
787 * @param vec Elements of new value
788 * @return 0 on success, non-0 on error
790 static int validate_positive(const struct config_state
*cs
,
791 int nvec
, char **vec
) {
796 disorder_error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
800 disorder_error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
803 if(xstrtol(&n
, vec
[0], 0, 0)) {
804 disorder_error(0, "%s:%d: %s", cs
->path
, cs
->line
,
805 format_error(ec_errno
, errno
, errbuf
, sizeof errbuf
));
809 disorder_error(0, "%s:%d: must be positive", cs
->path
, cs
->line
);
816 /** @brief Validate a system username
817 * @param cs Configuration state
818 * @param nvec Length of (proposed) new value
819 * @param vec Elements of new value
820 * @return 0 on success, non-0 on error
822 static int validate_isauser(const struct config_state
*cs
,
823 int attribute((unused
)) nvec
,
825 if(!getpwnam(vec
[0])) {
826 disorder_error(0, "%s:%d: no such user as '%s'", cs
->path
, cs
->line
, vec
[0]);
833 /** @brief Validate a sample format string
834 * @param cs Configuration state
835 * @param nvec Length of (proposed) new value
836 * @param vec Elements of new value
837 * @return 0 on success, non-0 on error
839 static int validate_sample_format(const struct config_state
*cs
,
840 int attribute((unused
)) nvec
,
842 return parse_sample_format(cs
, 0, nvec
, vec
);
845 /** @brief Validate anything
846 * @param cs Configuration state
847 * @param nvec Length of (proposed) new value
848 * @param vec Elements of new value
851 static int validate_any(const struct config_state
attribute((unused
)) *cs
,
852 int attribute((unused
)) nvec
,
853 char attribute((unused
)) **vec
) {
857 /** @brief Validate a URL
858 * @param cs Configuration state
859 * @param nvec Length of (proposed) new value
860 * @param vec Elements of new value
861 * @return 0 on success, non-0 on error
865 static int validate_url(const struct config_state
attribute((unused
)) *cs
,
866 int attribute((unused
)) nvec
,
870 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
871 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
873 n
= strspn(s
, ("abcdefghijklmnopqrstuvwxyz"
874 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
877 disorder_error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
880 if(!strncmp(s
, "http:", 5)
881 || !strncmp(s
, "https:", 6)) {
883 /* we only do a rather cursory check */
884 if(strncmp(s
, "//", 2)) {
885 disorder_error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
892 /** @brief Validate an alias pattern
893 * @param cs Configuration state
894 * @param nvec Length of (proposed) new value
895 * @param vec Elements of new value
896 * @return 0 on success, non-0 on error
898 static int validate_alias(const struct config_state
*cs
,
902 int in_brackets
= 0, c
;
905 disorder_error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
909 disorder_error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
913 while((c
= (unsigned char)*s
++)) {
917 else if(!isalnum(c
)) {
918 disorder_error(0, "%s:%d: invalid part name in alias expansion in '%s'",
919 cs
->path
, cs
->line
, vec
[0]);
927 } else if(c
== '\\') {
928 if(!(c
= (unsigned char)*s
++)) {
929 disorder_error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
930 cs
->path
, cs
->line
, vec
[0]);
932 } else if(c
!= '\\' && c
!= '{') {
933 disorder_error(0, "%s:%d: invalid escape in alias expansion in '%s'",
934 cs
->path
, cs
->line
, vec
[0]);
943 "%s:%d: unterminated part name in alias expansion in '%s'",
944 cs
->path
, cs
->line
, vec
[0]);
950 /** @brief Validate a hash algorithm name
951 * @param cs Configuration state
952 * @param nvec Length of (proposed) new value
953 * @param vec Elements of new value
954 * @return 0 on success, non-0 on error
956 static int validate_algo(const struct config_state
attribute((unused
)) *cs
,
960 disorder_error(0, "%s:%d: invalid algorithm specification", cs
->path
, cs
->line
);
963 if(!valid_authhash(vec
[0])) {
964 disorder_error(0, "%s:%d: unsuported algorithm '%s'", cs
->path
, cs
->line
, vec
[0]);
971 /** @brief Validate a playback backend name
972 * @param cs Configuration state
973 * @param nvec Length of (proposed) new value
974 * @param vec Elements of new value
975 * @return 0 on success, non-0 on error
977 static int validate_backend(const struct config_state
attribute((unused
)) *cs
,
982 disorder_error(0, "%s:%d: invalid sound API specification", cs
->path
, cs
->line
);
985 if(!strcmp(vec
[0], "network")) {
986 disorder_error(0, "'api network' is deprecated; use 'api rtp'");
989 if(config_uaudio_apis
) {
990 for(n
= 0; config_uaudio_apis
[n
]; ++n
)
991 if(!strcmp(vec
[0], config_uaudio_apis
[n
]->name
))
993 disorder_error(0, "%s:%d: unrecognized sound API '%s'", cs
->path
, cs
->line
, vec
[0]);
996 /* In non-server processes we have no idea what's valid */
1001 /** @brief Validate a pause mode string
1002 * @param cs Configuration state
1003 * @param nvec Length of (proposed) new value
1004 * @param vec Elements of new value
1005 * @return 0 on success, non-0 on error
1007 static int validate_pausemode(const struct config_state
attribute((unused
)) *cs
,
1010 if(nvec
== 1 && (!strcmp(vec
[0], "silence") || !strcmp(vec
[0], "suspend")))
1012 disorder_error(0, "%s:%d: invalid pause mode", cs
->path
, cs
->line
);
1016 /** @brief Validate a destination network address
1017 * @param cs Configuration state
1018 * @param nvec Length of (proposed) new value
1019 * @param vec Elements of new value
1020 * @return 0 on success, non-0 on error
1022 * By a destination address, it is meant that it must not be a wildcard
1025 static int validate_destaddr(const struct config_state
attribute((unused
)) *cs
,
1028 struct netaddress na
[1];
1030 if(netaddress_parse(na
, nvec
, vec
)) {
1031 disorder_error(0, "%s:%d: invalid network address", cs
->path
, cs
->line
);
1035 disorder_error(0, "%s:%d: destination address required", cs
->path
, cs
->line
);
1042 /** @brief Item name and and offset */
1043 #define C(x) #x, offsetof(struct config, x)
1044 /** @brief Item name and and offset */
1045 #define C2(x,y) #x, offsetof(struct config, y)
1047 /** @brief All configuration items */
1048 static const struct conf conf
[] = {
1049 { C(alias
), &type_string
, validate_alias
},
1051 { C(api
), &type_string
, validate_backend
},
1053 { C(authorization_algorithm
), &type_string
, validate_algo
},
1054 { C(broadcast
), &type_netaddress
, validate_destaddr
},
1055 { C(broadcast_from
), &type_netaddress
, validate_any
},
1056 { C(channel
), &type_string
, validate_any
},
1057 { C(checkpoint_kbyte
), &type_integer
, validate_non_negative
},
1058 { C(checkpoint_min
), &type_integer
, validate_non_negative
},
1059 { C(collection
), &type_collections
, validate_any
},
1060 { C(connect
), &type_netaddress
, validate_destaddr
},
1061 { C(cookie_key_lifetime
), &type_integer
, validate_positive
},
1062 { C(cookie_login_lifetime
), &type_integer
, validate_positive
},
1063 { C(dbversion
), &type_integer
, validate_positive
},
1064 { C(default_rights
), &type_rights
, validate_any
},
1065 { C(device
), &type_string
, validate_any
},
1066 { C(history
), &type_integer
, validate_positive
},
1068 { C(home
), &type_string
, validate_isabspath
},
1070 { C(listen
), &type_netaddress
, validate_any
},
1071 { C(mail_sender
), &type_string
, validate_any
},
1072 { C(mixer
), &type_string
, validate_any
},
1073 { C(mount_rescan
), &type_boolean
, validate_any
},
1074 { C(multicast_loop
), &type_boolean
, validate_any
},
1075 { C(multicast_ttl
), &type_integer
, validate_non_negative
},
1077 { C(namepart
), &type_namepart
, validate_any
},
1079 { C(new_bias
), &type_integer
, validate_positive
},
1080 { C(new_bias_age
), &type_integer
, validate_positive
},
1081 { C(new_max
), &type_integer
, validate_positive
},
1082 { C2(nice
, nice_rescan
), &type_integer
, validate_non_negative
},
1083 { C(nice_rescan
), &type_integer
, validate_non_negative
},
1084 { C(nice_server
), &type_integer
, validate_any
},
1085 { C(nice_speaker
), &type_integer
, validate_any
},
1086 { C(noticed_history
), &type_integer
, validate_positive
},
1087 { C(password
), &type_string
, validate_any
},
1088 { C(pause_mode
), &type_string
, validate_pausemode
},
1089 { C(player
), &type_stringlist_accum
, validate_player
},
1090 { C(playlist_lock_timeout
), &type_integer
, validate_positive
},
1091 { C(playlist_max
) , &type_integer
, validate_positive
},
1092 { C(plugins
), &type_string_accum
, validate_isdir
},
1093 { C(queue_pad
), &type_integer
, validate_positive
},
1094 { C(refresh
), &type_integer
, validate_positive
},
1095 { C(refresh_min
), &type_integer
, validate_non_negative
},
1096 { C(reminder_interval
), &type_integer
, validate_positive
},
1097 { C(remote_userman
), &type_boolean
, validate_any
},
1098 { C(replay_min
), &type_integer
, validate_non_negative
},
1099 { C(rtp_delay_threshold
), &type_integer
, validate_positive
},
1100 { C(rtp_mode
), &type_string
, validate_any
},
1101 { C(rtp_verbose
), &type_boolean
, validate_any
},
1102 { C(sample_format
), &type_sample_format
, validate_sample_format
},
1103 { C(scratch
), &type_string_accum
, validate_isreg
},
1105 { C(sendmail
), &type_string
, validate_isabspath
},
1107 { C(short_display
), &type_integer
, validate_positive
},
1108 { C(signal
), &type_signal
, validate_any
},
1109 { C(smtp_server
), &type_string
, validate_any
},
1110 { C(sox_generation
), &type_integer
, validate_non_negative
},
1112 { C2(speaker_backend
, api
), &type_string
, validate_backend
},
1114 { C(speaker_command
), &type_string
, validate_any
},
1115 { C(stopword
), &type_string_accum
, validate_any
},
1116 { C(templates
), &type_string_accum
, validate_isdir
},
1117 { C(tracklength
), &type_stringlist_accum
, validate_tracklength
},
1119 { C(transform
), &type_transform
, validate_any
},
1121 { C(url
), &type_string
, validate_url
},
1123 { C(user
), &type_string
, validate_isauser
},
1125 { C(username
), &type_string
, validate_any
},
1128 /** @brief Find a configuration item's definition by key */
1129 static const struct conf
*find(const char *key
) {
1132 if((n
= TABLE_FIND(conf
, name
, key
)) < 0)
1137 /** @brief Set a new configuration value
1138 * @param cs Configuration state
1139 * @param nvec Length of @p vec
1140 * @param vec Name and new value
1141 * @return 0 on success, non-0 on error.
1143 * @c vec[0] is the name, the rest is the value.
1145 static int config_set(const struct config_state
*cs
,
1146 int nvec
, char **vec
) {
1147 const struct conf
*which
;
1149 D(("config_set %s", vec
[0]));
1150 if(!(which
= find(vec
[0]))) {
1151 disorder_error(0, "%s:%d: unknown configuration key '%s'",
1152 cs
->path
, cs
->line
, vec
[0]);
1155 return (which
->validate(cs
, nvec
- 1, vec
+ 1)
1156 || which
->type
->set(cs
, which
, nvec
- 1, vec
+ 1));
1159 /** @brief Set a configuration item from parameters
1160 * @param cs Configuration state
1161 * @param which Item to set
1162 * @param ... Value as strings, terminated by (char *)NULL
1163 * @return 0 on success, non-0 on error
1165 static int config_set_args(const struct config_state
*cs
,
1166 const char *which
, ...) {
1173 vector_append(v
, (char *)which
);
1174 va_start(ap
, which
);
1175 while((s
= va_arg(ap
, char *)))
1176 vector_append(v
, s
);
1178 vector_terminate(v
);
1179 rc
= config_set(cs
, v
->nvec
, v
->vec
);
1184 /** @brief Error callback used by config_include()
1185 * @param msg Error message
1186 * @param u User data (@ref config_state)
1188 static void config_error(const char *msg
, void *u
) {
1189 const struct config_state
*cs
= u
;
1191 disorder_error(0, "%s:%d: %s", cs
->path
, cs
->line
, msg
);
1194 /** @brief Include a file by name
1195 * @param c Configuration to update
1196 * @param path Path to read
1197 * @return 0 on success, non-0 on error
1199 static int config_include(struct config
*c
, const char *path
) {
1201 char *buffer
, *inputbuffer
, **vec
;
1203 struct config_state cs
;
1208 D(("%s: reading configuration", path
));
1209 if(!(fp
= fopen(path
, "r"))) {
1210 disorder_error(errno
, "error opening %s", path
);
1213 while(!inputline(path
, fp
, &inputbuffer
, '\n')) {
1215 if(!(buffer
= mb2utf8(inputbuffer
))) {
1216 disorder_error(errno
, "%s:%d: cannot convert to UTF-8", cs
.path
, cs
.line
);
1222 if(!(vec
= split(buffer
, &n
, SPLIT_COMMENTS
|SPLIT_QUOTES
,
1223 config_error
, &cs
))) {
1229 /* 'include' is special-cased */
1230 if(!strcmp(vec
[0], "include")) {
1232 disorder_error(0, "%s:%d: must be 'include PATH'", cs
.path
, cs
.line
);
1235 config_include(c
, vec
[1]);
1237 ret
|= config_set(&cs
, n
, vec
);
1239 for(n
= 0; vec
[n
]; ++n
) xfree(vec
[n
]);
1244 disorder_error(errno
, "error reading %s", path
);
1251 /** @brief Default stopword setting */
1252 static const char *const default_stopwords
[] = {
1311 #define NDEFAULT_STOPWORDS (sizeof default_stopwords / sizeof *default_stopwords)
1313 /** @brief Default player patterns */
1314 static const char *const default_players
[] = {
1320 #define NDEFAULT_PLAYERS (sizeof default_players / sizeof *default_players)
1322 /** @brief Make a new default configuration
1323 * @return New configuration
1325 static struct config
*config_default(void) {
1326 struct config
*c
= xmalloc(sizeof *c
);
1328 const char *logname
;
1331 struct config_state cs
;
1334 cs
.path
= "<internal>";
1337 /* Strings had better be xstrdup'd as they will get freed at some point. */
1340 c
->home
= xstrdup(pkgstatedir
);
1345 DWORD bufsize
= sizeof buffer
;
1346 if(!GetUserNameA(buffer
, &bufsize
))
1347 disorder_fatal(0, "cannot determine our username");
1348 c
->username
= xstrdup(buffer
);
1351 if(!(pw
= getpwuid(getuid())))
1352 disorder_fatal(0, "cannot determine our username");
1353 logname
= pw
->pw_name
;
1354 c
->username
= xstrdup(logname
);
1359 c
->signal
= SIGKILL
;
1361 c
->signal
= SIGTERM
;
1363 c
->alias
= xstrdup("{/artist}{/album}{/title}{ext}");
1364 c
->device
= xstrdup("default");
1365 c
->nice_rescan
= 10;
1366 c
->speaker_command
= 0;
1367 c
->sample_format
.bits
= 16;
1368 c
->sample_format
.rate
= 44100;
1369 c
->sample_format
.channels
= 2;
1370 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1372 c
->replay_min
= 8 * 3600;
1374 c
->multicast_ttl
= 1;
1375 c
->multicast_loop
= 1;
1376 c
->authorization_algorithm
= xstrdup("sha1");
1377 c
->noticed_history
= 31;
1378 c
->short_display
= 32;
1382 c
->cookie_login_lifetime
= 86400;
1383 c
->cookie_key_lifetime
= 86400 * 7;
1385 if(sendmail_binary
[0] && strcmp(sendmail_binary
, "none"))
1386 c
->sendmail
= xstrdup(sendmail_binary
);
1388 c
->smtp_server
= xstrdup("127.0.0.1");
1390 c
->reminder_interval
= 600; /* 10m */
1391 c
->new_bias_age
= 7 * 86400; /* 1 week */
1392 c
->new_bias
= 4500000; /* 50 times the base weight */
1393 c
->sox_generation
= DEFAULT_SOX_GENERATION
;
1394 c
->playlist_max
= INT_MAX
; /* effectively no limit */
1395 c
->playlist_lock_timeout
= 10; /* 10s */
1396 c
->mount_rescan
= 1;
1397 /* Default stopwords */
1398 if(config_set(&cs
, (int)NDEFAULT_STOPWORDS
, (char **)default_stopwords
))
1400 /* Default player configuration */
1401 for(n
= 0; n
< NDEFAULT_PLAYERS
; ++n
) {
1402 if(config_set_args(&cs
, "player",
1403 default_players
[n
], "execraw", "disorder-decode", (char *)0))
1405 if(config_set_args(&cs
, "tracklength",
1406 default_players
[n
], "disorder-tracklength", (char *)0))
1409 c
->broadcast
.af
= -1;
1410 c
->broadcast_from
.af
= -1;
1413 c
->rtp_mode
= xstrdup("auto");
1418 /** @brief Construct a filename
1419 * @param c Configuration
1420 * @param name Base filename
1421 * @return Full filename
1423 * Usually use config_get_file() instead.
1425 char *config_get_file2(struct config
*c
, const char *name
) {
1428 byte_xasprintf(&s
, "%s/%s", c
->home
, name
);
1433 /** @brief Set the default configuration file */
1434 static void set_configfile(void) {
1437 byte_xasprintf(&configfile
, "%s/config", pkgconfdir
);
1441 /** @brief Free a configuration object
1442 * @param c Configuration to free
1444 * @p c is indeterminate after this function is called.
1446 void config_free(struct config
*c
) {
1450 for(n
= 0; n
< (int)(sizeof conf
/ sizeof *conf
); ++n
)
1451 conf
[n
].type
->free(c
, &conf
[n
]);
1452 for(n
= 0; n
< c
->nparts
; ++n
)
1459 /** @brief Set post-parse defaults
1460 * @param c Configuration to update
1461 * @param server True when running in the server
1463 * If @p server is set then certain parts of the configuration are more
1464 * strictly validated.
1466 static void config_postdefaults(struct config
*c
,
1468 struct config_state cs
;
1470 const struct conf
*whoami
;
1475 static const char *namepart
[][4] = {
1476 { "title", "/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
1477 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1478 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1479 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1480 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1482 #define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1484 static const char *transform
[][5] = {
1485 { "track", "^.*/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
1486 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1487 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1488 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1489 { "dir", "[[:punct:]]", "", "sort", "g", }
1491 #define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1494 cs
.path
= "<internal>";
1498 if(!c
->namepart
.n
) {
1499 whoami
= find("namepart");
1500 for(n
= 0; n
< NNAMEPART
; ++n
)
1501 set_namepart(&cs
, whoami
, 4, (char **)namepart
[n
]);
1503 if(!c
->transform
.n
) {
1504 whoami
= find("transform");
1505 for(n
= 0; n
< NTRANSFORM
; ++n
)
1506 set_transform(&cs
, whoami
, 5, (char **)transform
[n
]);
1510 if(c
->speaker_command
)
1511 c
->api
= xstrdup("command");
1512 else if(c
->broadcast
.af
!= -1)
1513 c
->api
= xstrdup("rtp");
1515 else if(config_uaudio_apis
)
1516 c
->api
= xstrdup(uaudio_default(config_uaudio_apis
,
1517 UAUDIO_API_SERVER
)->name
);
1520 c
->api
= xstrdup("<none>");
1522 if(!strcmp(c
->api
, "network"))
1523 c
->api
= xstrdup("rtp");
1525 if(!strcmp(c
->api
, "command") && !c
->speaker_command
)
1526 disorder_fatal(0, "'api command' but speaker_command is not set");
1527 if((!strcmp(c
->api
, "rtp")) && c
->broadcast
.af
== -1)
1528 disorder_fatal(0, "'api rtp' but broadcast is not set");
1530 /* Override sample format */
1531 if(!strcmp(c
->api
, "rtp")) {
1532 c
->sample_format
.rate
= 44100;
1533 c
->sample_format
.channels
= 2;
1534 c
->sample_format
.bits
= 16;
1535 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1537 if(!strcmp(c
->api
, "coreaudio")) {
1538 c
->sample_format
.rate
= 44100;
1539 c
->sample_format
.channels
= 2;
1540 c
->sample_format
.bits
= 16;
1541 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1543 if(!c
->default_rights
) {
1544 rights_type r
= RIGHTS__MASK
& ~(RIGHT_ADMIN
|RIGHT_REGISTER
1546 |RIGHT_SCRATCH__MASK
1547 |RIGHT_REMOVE__MASK
);
1548 r
|= RIGHT_SCRATCH_ANY
|RIGHT_MOVE_ANY
|RIGHT_REMOVE_ANY
;
1549 c
->default_rights
= rights_string(r
);
1553 /** @brief (Re-)read the config file
1554 * @param server If set, do extra checking
1555 * @param oldconfig Old configuration for compatibility check
1556 * @return 0 on success, non-0 on error
1558 * If @p oldconfig is set, then certain compatibility checks are done between
1559 * the old and new configurations.
1561 int config_read(int server
,
1562 const struct config
*oldconfig
) {
1565 struct passwd
*pw
= NULL
;
1568 c
= config_default();
1569 /* standalone client installs might not have a global config file */
1571 if(access(configfile
, F_OK
) == 0)
1572 if(config_include(c
, configfile
))
1574 /* if we can read the private config file, do */
1575 if((privconf
= config_private())
1576 && access(privconf
, R_OK
) == 0
1577 && config_include(c
, privconf
))
1580 /* if there's a per-user system config file for this user, read it */
1581 if(config_per_user
) {
1583 if(!(pw
= getpwuid(getuid())))
1584 disorder_fatal(0, "cannot determine our username");
1585 if((privconf
= config_usersysconf(pw
))
1586 && access(privconf
, F_OK
) == 0
1587 && config_include(c
, privconf
))
1591 /* if we have a password file, read it */
1592 if((privconf
= config_userconf(0, pw
))
1593 && access(privconf
, F_OK
) == 0
1594 && config_include(c
, privconf
))
1598 /* install default namepart and transform settings */
1599 config_postdefaults(c
, server
);
1603 if(strcmp(c
->home
, oldconfig
->home
)) {
1604 disorder_error(0, "'home' cannot be changed without a restart");
1608 if(strcmp(c
->alias
, oldconfig
->alias
)) {
1609 disorder_error(0, "'alias' cannot be changed without a restart");
1612 if(strcmp(c
->user
, oldconfig
->user
)) {
1613 disorder_error(0, "'user' cannot be changed without a restart");
1616 if(c
->nice_speaker
!= oldconfig
->nice_speaker
) {
1617 disorder_error(0, "'nice_speaker' cannot be changed without a restart");
1618 /* ...but we accept the new config anyway */
1620 if(c
->nice_server
!= oldconfig
->nice_server
) {
1621 disorder_error(0, "'nice_server' cannot be changed without a restart");
1622 /* ...but we accept the new config anyway */
1625 if(namepartlist_compare(&c
->namepart
, &oldconfig
->namepart
)) {
1626 disorder_error(0, "'namepart' settings cannot be changed without a restart");
1630 if(stringlist_compare(&c
->stopword
, &oldconfig
->stopword
)) {
1631 disorder_error(0, "'stopword' settings cannot be changed without a restart");
1635 disorder_error(0, "not installing incompatible new configuration");
1639 /* everything is good so we shall use the new config */
1640 config_free(config
);
1641 /* warn about obsolete directives */
1646 /** @brief Return the path to the private configuration file */
1647 char *config_private(void) {
1654 byte_xasprintf(&s
, "%s.private", configfile
);
1659 /** @brief Return the path to user's personal configuration file */
1660 char *config_userconf(const char *home
, const struct passwd
*pw
) {
1665 if(SHGetKnownFolderPath(&FOLDERID_RoamingAppData
, 0, NULL
, &wpath
) != S_OK
)
1666 disorder_fatal(0, "error calling SHGetKnownFolderPath");
1667 appdata
= win_wtomb(wpath
);
1668 CoTaskMemFree(wpath
);
1669 byte_xasprintf(&s
, "%s\\DisOrder\\passwd", appdata
);
1671 if(!home
&& !pw
&& !(pw
= getpwuid(getuid())))
1672 disorder_fatal(0, "cannot determine our username");
1673 byte_xasprintf(&s
, "%s/.disorder/passwd", home ? home
: pw
->pw_dir
);
1679 /** @brief Return the path to user-specific system configuration */
1680 char *config_usersysconf(const struct passwd
*pw
) {
1684 if(!strchr(pw
->pw_name
, '/')) {
1685 byte_xasprintf(&s
, "%s.%s", configfile
, pw
->pw_name
);
1691 /** @brief Get a filename within the home directory
1692 * @param name Relative name
1695 char *config_get_file(const char *name
) {
1696 return config_get_file2(config
, name
);
1700 /** @brief Order two stringlists
1701 * @param a First stringlist
1702 * @param b Second stringlist
1703 * @return <0, 0 or >0 if a<b, a=b or a>b
1705 static int stringlist_compare(const struct stringlist
*a
,
1706 const struct stringlist
*b
) {
1709 while(n
< a
->n
&& n
< b
->n
) {
1710 if((c
= strcmp(a
->s
[n
], b
->s
[n
])))
1716 else if(a
->n
> b
->n
)
1723 /** @brief Order two namepart definitions
1724 * @param a First namepart definition
1725 * @param b Second namepart definition
1726 * @return <0, 0 or >0 if a<b, a=b or a>b
1728 static int namepart_compare(const struct namepart
*a
,
1729 const struct namepart
*b
) {
1732 if((c
= strcmp(a
->part
, b
->part
)))
1734 if((c
= strcmp(a
->res
, b
->res
)))
1736 if((c
= strcmp(a
->replace
, b
->replace
)))
1738 if((c
= strcmp(a
->context
, b
->context
)))
1740 if(a
->reflags
> b
->reflags
)
1742 if(a
->reflags
< b
->reflags
)
1747 /** @brief Order two lists of namepart definitions
1748 * @param a First list of namepart definitions
1749 * @param b Second list of namepart definitions
1750 * @return <0, 0 or >0 if a<b, a=b or a>b
1752 static int namepartlist_compare(const struct namepartlist
*a
,
1753 const struct namepartlist
*b
) {
1756 while(n
< a
->n
&& n
< b
->n
) {
1757 if((c
= namepart_compare(&a
->s
[n
], &b
->s
[n
])))
1763 else if(a
->n
< b
->n
)
1770 /** @brief Verify configuration table.
1771 * @return The number of problems found
1773 int config_verify(void) {
1776 for(n
= 1; n
< sizeof conf
/ sizeof *conf
; ++n
)
1777 if(strcmp(conf
[n
-1].name
, conf
[n
].name
) >= 0) {
1778 fprintf(stderr
, "%s >= %s\n", conf
[n
-1].name
, conf
[n
].name
);