2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2010 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 default 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 */
76 /** @brief Line number */
79 /** @brief Configuration object under construction */
80 struct config
*config
;
83 /** @brief Current configuration */
84 struct config
*config
;
86 /** @brief One configuration item */
88 /** @brief Name as it appears in the config file */
91 /** @brief Offset in @ref config structure */
94 /** @brief Pointer to item type */
95 const struct conftype
*type
;
97 /** @brief Pointer to item-specific validation routine
98 * @param cs Configuration state
99 * @param nvec Length of (proposed) new value
100 * @param vec Elements of new value
101 * @return 0 on success, non-0 on error
103 * The validate function should report any error it detects.
105 int (*validate
)(const struct config_state
*cs
,
106 int nvec
, char **vec
);
109 /** @brief Type of a configuration item */
111 /** @brief Pointer to function to set item
112 * @param cs Configuration state
113 * @param whoami Configuration item to set
114 * @param nvec Length of new value
115 * @param vec New value
116 * @return 0 on success, non-0 on error
118 int (*set
)(const struct config_state
*cs
,
119 const struct conf
*whoami
,
120 int nvec
, char **vec
);
122 /** @brief Pointer to function to free item
123 * @param c Configuration structure to free an item of
124 * @param whoami Configuration item to free
126 void (*free
)(struct config
*c
, const struct conf
*whoami
);
129 /** @brief Compute the address of an item */
130 #define ADDRESS(C, TYPE) ((TYPE *)((char *)(C) + whoami->offset))
131 /** @brief Return the value of an item */
132 #define VALUE(C, TYPE) (*ADDRESS(C, TYPE))
134 static int stringlist_compare(const struct stringlist
*a
,
135 const struct stringlist
*b
);
136 static int namepartlist_compare(const struct namepartlist
*a
,
137 const struct namepartlist
*b
);
139 static int set_signal(const struct config_state
*cs
,
140 const struct conf
*whoami
,
141 int nvec
, char **vec
) {
145 disorder_error(0, "%s:%d: '%s' requires one argument",
146 cs
->path
, cs
->line
, whoami
->name
);
149 if((n
= find_signal(vec
[0])) == -1) {
150 disorder_error(0, "%s:%d: unknown signal '%s'",
151 cs
->path
, cs
->line
, vec
[0]);
154 VALUE(cs
->config
, int) = n
;
158 static int set_collections(const struct config_state
*cs
,
159 const struct conf
*whoami
,
160 int nvec
, char **vec
) {
161 struct collectionlist
*cl
;
162 const char *root
, *encoding
, *module
;
181 disorder_error(0, "%s:%d: '%s' requires at least one argument",
182 cs
->path
, cs
->line
, whoami
->name
);
185 disorder_error(0, "%s:%d: '%s' requires at most three arguments",
186 cs
->path
, cs
->line
, whoami
->name
);
189 /* Sanity check root */
191 disorder_error(0, "%s:%d: collection root must start with '/'",
195 if(root
[1] && root
[strlen(root
)-1] == '/') {
196 disorder_error(0, "%s:%d: collection root must not end with '/'",
204 encoding
= nl_langinfo(CODESET
);
205 cl
= ADDRESS(cs
->config
, struct collectionlist
);
207 cl
->s
= xrealloc(cl
->s
, cl
->n
* sizeof (struct collection
));
208 cl
->s
[cl
->n
- 1].module
= xstrdup(module
);
209 cl
->s
[cl
->n
- 1].encoding
= xstrdup(encoding
);
210 cl
->s
[cl
->n
- 1].root
= xstrdup(root
);
214 static int set_boolean(const struct config_state
*cs
,
215 const struct conf
*whoami
,
216 int nvec
, char **vec
) {
220 disorder_error(0, "%s:%d: '%s' takes only one argument",
221 cs
->path
, cs
->line
, whoami
->name
);
224 if(!strcmp(vec
[0], "yes")) state
= 1;
225 else if(!strcmp(vec
[0], "no")) state
= 0;
227 disorder_error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'",
228 cs
->path
, cs
->line
, whoami
->name
);
231 VALUE(cs
->config
, int) = state
;
235 static int set_string(const struct config_state
*cs
,
236 const struct conf
*whoami
,
237 int nvec
, char **vec
) {
239 disorder_error(0, "%s:%d: '%s' takes only one argument",
240 cs
->path
, cs
->line
, whoami
->name
);
243 xfree(VALUE(cs
->config
, char *));
244 VALUE(cs
->config
, char *) = xstrdup(vec
[0]);
248 static int set_stringlist(const struct config_state
*cs
,
249 const struct conf
*whoami
,
250 int nvec
, char **vec
) {
252 struct stringlist
*sl
;
254 sl
= ADDRESS(cs
->config
, struct stringlist
);
256 for(n
= 0; n
< nvec
; ++n
) {
258 sl
->s
= xrealloc(sl
->s
, (sl
->n
* sizeof (char *)));
259 sl
->s
[sl
->n
- 1] = xstrdup(vec
[n
]);
264 static int set_integer(const struct config_state
*cs
,
265 const struct conf
*whoami
,
266 int nvec
, char **vec
) {
270 disorder_error(0, "%s:%d: '%s' takes only one argument",
271 cs
->path
, cs
->line
, whoami
->name
);
274 if(xstrtol(ADDRESS(cs
->config
, long), vec
[0], &e
, 0)) {
275 disorder_error(errno
, "%s:%d: converting integer", cs
->path
, cs
->line
);
279 disorder_error(0, "%s:%d: invalid integer syntax", cs
->path
, cs
->line
);
285 static int set_stringlist_accum(const struct config_state
*cs
,
286 const struct conf
*whoami
,
287 int nvec
, char **vec
) {
289 struct stringlist
*s
;
290 struct stringlistlist
*sll
;
292 sll
= ADDRESS(cs
->config
, struct stringlistlist
);
298 sll
->s
= xrealloc(sll
->s
, (sll
->n
* sizeof (struct stringlist
)));
299 s
= &sll
->s
[sll
->n
- 1];
301 s
->s
= xmalloc((nvec
+ 1) * sizeof (char *));
302 for(n
= 0; n
< nvec
; ++n
)
303 s
->s
[n
] = xstrdup(vec
[n
]);
307 static int set_string_accum(const struct config_state
*cs
,
308 const struct conf
*whoami
,
309 int nvec
, char **vec
) {
311 struct stringlist
*sl
;
313 sl
= ADDRESS(cs
->config
, struct stringlist
);
318 for(n
= 0; n
< nvec
; ++n
) {
320 sl
->s
= xrealloc(sl
->s
, (sl
->n
* sizeof (char *)));
321 sl
->s
[sl
->n
- 1] = xstrdup(vec
[n
]);
326 static int parse_sample_format(const struct config_state
*cs
,
327 struct stream_header
*format
,
328 int nvec
, char **vec
) {
333 disorder_error(0, "%s:%d: wrong number of arguments", cs
->path
, cs
->line
);
336 if(xstrtol(&t
, p
, &p
, 0)) {
337 disorder_error(errno
, "%s:%d: converting bits-per-sample",
341 if(t
!= 8 && t
!= 16) {
342 disorder_error(0, "%s:%d: bad bits-per-sample (%ld)",
343 cs
->path
, cs
->line
, t
);
346 if(format
) format
->bits
= t
;
348 case 'l': case 'L': t
= ENDIAN_LITTLE
; p
++; break;
349 case 'b': case 'B': t
= ENDIAN_BIG
; p
++; break;
350 default: t
= ENDIAN_NATIVE
; break;
352 if(format
) format
->endian
= t
;
354 disorder_error(errno
, "%s:%d: expected `/' after bits-per-sample",
359 if(xstrtol(&t
, p
, &p
, 0)) {
360 disorder_error(errno
, "%s:%d: converting sample-rate", cs
->path
, cs
->line
);
363 if(t
< 1 || t
> INT_MAX
) {
364 disorder_error(0, "%s:%d: silly sample-rate (%ld)", cs
->path
, cs
->line
, t
);
367 if(format
) format
->rate
= t
;
369 disorder_error(0, "%s:%d: expected `/' after sample-rate",
374 if(xstrtol(&t
, p
, &p
, 0)) {
375 disorder_error(errno
, "%s:%d: converting channels", cs
->path
, cs
->line
);
379 disorder_error(0, "%s:%d: silly number (%ld) of channels",
380 cs
->path
, cs
->line
, t
);
383 if(format
) format
->channels
= t
;
385 disorder_error(0, "%s:%d: junk after channels", cs
->path
, cs
->line
);
391 static int set_sample_format(const struct config_state
*cs
,
392 const struct conf
*whoami
,
393 int nvec
, char **vec
) {
394 return parse_sample_format(cs
, ADDRESS(cs
->config
, struct stream_header
),
398 static int set_namepart(const struct config_state
*cs
,
399 const struct conf
*whoami
,
400 int nvec
, char **vec
) {
401 struct namepartlist
*npl
= ADDRESS(cs
->config
, struct namepartlist
);
408 disorder_error(0, "%s:%d: namepart needs at least 3 arguments",
413 disorder_error(0, "%s:%d: namepart needs at most 5 arguments",
417 reflags
= nvec
>= 5 ?
regsub_flags(vec
[4]) : 0;
418 if(!(re
= pcre_compile(vec
[1],
420 |regsub_compile_options(reflags
),
421 &errstr
, &erroffset
, 0))) {
422 disorder_error(0, "%s:%d: compiling regexp /%s/: %s (offset %d)",
423 cs
->path
, cs
->line
, vec
[1], errstr
, erroffset
);
426 npl
->s
= xrealloc(npl
->s
, (npl
->n
+ 1) * sizeof (struct namepart
));
427 npl
->s
[npl
->n
].part
= xstrdup(vec
[0]);
428 npl
->s
[npl
->n
].re
= re
;
429 npl
->s
[npl
->n
].res
= xstrdup(vec
[1]);
430 npl
->s
[npl
->n
].replace
= xstrdup(vec
[2]);
431 npl
->s
[npl
->n
].context
= xstrdup(vec
[3]);
432 npl
->s
[npl
->n
].reflags
= reflags
;
434 /* XXX a bit of a bodge; relies on there being very few parts. */
435 for(n
= 0; (n
< cs
->config
->nparts
436 && strcmp(cs
->config
->parts
[n
], vec
[0])); ++n
)
438 if(n
>= cs
->config
->nparts
) {
439 cs
->config
->parts
= xrealloc(cs
->config
->parts
,
440 (cs
->config
->nparts
+ 1) * sizeof (char *));
441 cs
->config
->parts
[cs
->config
->nparts
++] = xstrdup(vec
[0]);
446 static int set_transform(const struct config_state
*cs
,
447 const struct conf
*whoami
,
448 int nvec
, char **vec
) {
449 struct transformlist
*tl
= ADDRESS(cs
->config
, struct transformlist
);
456 disorder_error(0, "%s:%d: transform needs at least 3 arguments",
461 disorder_error(0, "%s:%d: transform needs at most 5 arguments",
465 reflags
= (nvec
>= 5 ?
regsub_flags(vec
[4]) : 0);
466 if(!(re
= pcre_compile(vec
[1],
468 |regsub_compile_options(reflags
),
469 &errstr
, &erroffset
, 0))) {
470 disorder_error(0, "%s:%d: compiling regexp /%s/: %s (offset %d)",
471 cs
->path
, cs
->line
, vec
[1], errstr
, erroffset
);
474 tl
->t
= xrealloc(tl
->t
, (tl
->n
+ 1) * sizeof (struct namepart
));
475 tl
->t
[tl
->n
].type
= xstrdup(vec
[0]);
476 tl
->t
[tl
->n
].context
= xstrdup(vec
[3] ? vec
[3] : "*");
477 tl
->t
[tl
->n
].re
= re
;
478 tl
->t
[tl
->n
].replace
= xstrdup(vec
[2]);
479 tl
->t
[tl
->n
].flags
= reflags
;
484 static int set_rights(const struct config_state
*cs
,
485 const struct conf
*whoami
,
486 int nvec
, char **vec
) {
488 disorder_error(0, "%s:%d: '%s' requires one argument",
489 cs
->path
, cs
->line
, whoami
->name
);
492 if(parse_rights(vec
[0], 0, 1)) {
493 disorder_error(0, "%s:%d: invalid rights string '%s'",
494 cs
->path
, cs
->line
, vec
[0]);
497 return set_string(cs
, whoami
, nvec
, vec
);
500 static int set_netaddress(const struct config_state
*cs
,
501 const struct conf
*whoami
,
502 int nvec
, char **vec
) {
503 struct netaddress
*na
= ADDRESS(cs
->config
, struct netaddress
);
505 if(netaddress_parse(na
, nvec
, vec
)) {
506 disorder_error(0, "%s:%d: invalid network address", cs
->path
, cs
->line
);
514 static void free_none(struct config
attribute((unused
)) *c
,
515 const struct conf
attribute((unused
)) *whoami
) {
518 static void free_string(struct config
*c
,
519 const struct conf
*whoami
) {
520 xfree(VALUE(c
, char *));
521 VALUE(c
, char *) = 0;
524 static void free_stringlist(struct config
*c
,
525 const struct conf
*whoami
) {
527 struct stringlist
*sl
= ADDRESS(c
, struct stringlist
);
529 for(n
= 0; n
< sl
->n
; ++n
)
534 static void free_stringlistlist(struct config
*c
,
535 const struct conf
*whoami
) {
537 struct stringlistlist
*sll
= ADDRESS(c
, struct stringlistlist
);
538 struct stringlist
*sl
;
540 for(n
= 0; n
< sll
->n
; ++n
) {
542 for(m
= 0; m
< sl
->n
; ++m
)
549 static void free_collectionlist(struct config
*c
,
550 const struct conf
*whoami
) {
551 struct collectionlist
*cll
= ADDRESS(c
, struct collectionlist
);
552 struct collection
*cl
;
555 for(n
= 0; n
< cll
->n
; ++n
) {
564 static void free_namepartlist(struct config
*c
,
565 const struct conf
*whoami
) {
566 struct namepartlist
*npl
= ADDRESS(c
, struct namepartlist
);
570 for(n
= 0; n
< npl
->n
; ++n
) {
573 pcre_free(np
->re
); /* ...whatever pcre_free is set to. */
581 static void free_transformlist(struct config
*c
,
582 const struct conf
*whoami
) {
583 struct transformlist
*tl
= ADDRESS(c
, struct transformlist
);
587 for(n
= 0; n
< tl
->n
; ++n
) {
590 pcre_free(t
->re
); /* ...whatever pcre_free is set to. */
597 static void free_netaddress(struct config
*c
,
598 const struct conf
*whoami
) {
599 struct netaddress
*na
= ADDRESS(c
, struct netaddress
);
604 /* configuration types */
606 static const struct conftype
607 type_signal
= { set_signal
, free_none
},
608 type_collections
= { set_collections
, free_collectionlist
},
609 type_boolean
= { set_boolean
, free_none
},
610 type_string
= { set_string
, free_string
},
611 type_stringlist
= { set_stringlist
, free_stringlist
},
612 type_integer
= { set_integer
, free_none
},
613 type_stringlist_accum
= { set_stringlist_accum
, free_stringlistlist
},
614 type_string_accum
= { set_string_accum
, free_stringlist
},
615 type_sample_format
= { set_sample_format
, free_none
},
616 type_namepart
= { set_namepart
, free_namepartlist
},
617 type_transform
= { set_transform
, free_transformlist
},
618 type_netaddress
= { set_netaddress
, free_netaddress
},
619 type_rights
= { set_rights
, free_string
};
621 /* specific validation routine */
623 /** @brief Perform a test on a filename
624 * @param test Test function to call on mode bits
625 * @param what Type of file sought
627 * If @p test returns 0 then the file is not a @p what and an error
628 * is reported and -1 is returned.
630 #define VALIDATE_FILE(test, what) do { \
634 for(n = 0; n < nvec; ++n) { \
635 if(stat(vec[n], &sb) < 0) { \
636 disorder_error(errno, "%s:%d: %s", \
637 cs->path, cs->line, vec[n]); \
640 if(!test(sb.st_mode)) { \
641 disorder_error(0, "%s:%d: %s is not a %s", \
642 cs->path, cs->line, vec[n], what); \
648 /** @brief Validate an absolute path
649 * @param cs Configuration state
650 * @param nvec Length of (proposed) new value
651 * @param vec Elements of new value
652 * @return 0 on success, non-0 on error
654 static int validate_isabspath(const struct config_state
*cs
,
655 int nvec
, char **vec
) {
658 for(n
= 0; n
< nvec
; ++n
)
659 if(vec
[n
][0] != '/') {
660 disorder_error(errno
, "%s:%d: %s: not an absolute path",
661 cs
->path
, cs
->line
, vec
[n
]);
667 /** @brief Validate an existing directory
668 * @param cs Configuration state
669 * @param nvec Length of (proposed) new value
670 * @param vec Elements of new value
671 * @return 0 on success, non-0 on error
673 static int validate_isdir(const struct config_state
*cs
,
674 int nvec
, char **vec
) {
675 VALIDATE_FILE(S_ISDIR
, "directory");
679 /** @brief Validate an existing regular file
680 * @param cs Configuration state
681 * @param nvec Length of (proposed) new value
682 * @param vec Elements of new value
683 * @return 0 on success, non-0 on error
685 static int validate_isreg(const struct config_state
*cs
,
686 int nvec
, char **vec
) {
687 VALIDATE_FILE(S_ISREG
, "regular file");
691 /** @brief Validate a player pattern
692 * @param cs Configuration state
693 * @param nvec Length of (proposed) new value
694 * @param vec Elements of new value
695 * @return 0 on success, non-0 on error
697 static int validate_player(const struct config_state
*cs
,
699 char attribute((unused
)) **vec
) {
700 if(nvec
&& nvec
< 2) {
701 disorder_error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
708 /** @brief Validate a track length pattern
709 * @param cs Configuration state
710 * @param nvec Length of (proposed) new value
711 * @param vec Elements of new value
712 * @return 0 on success, non-0 on error
714 static int validate_tracklength(const struct config_state
*cs
,
716 char attribute((unused
)) **vec
) {
717 if(nvec
&& nvec
< 2) {
718 disorder_error(0, "%s:%d: should be at least 'tracklength PATTERN MODULE'",
725 /** @brief Validate a non-negative (@c long) integer
726 * @param cs Configuration state
727 * @param nvec Length of (proposed) new value
728 * @param vec Elements of new value
729 * @return 0 on success, non-0 on error
731 static int validate_non_negative(const struct config_state
*cs
,
732 int nvec
, char **vec
) {
736 disorder_error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
740 disorder_error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
743 if(xstrtol(&n
, vec
[0], 0, 0)) {
744 disorder_error(0, "%s:%d: %s", cs
->path
, cs
->line
, strerror(errno
));
748 disorder_error(0, "%s:%d: must not be negative", cs
->path
, cs
->line
);
754 /** @brief Validate a positive (@c long) integer
755 * @param cs Configuration state
756 * @param nvec Length of (proposed) new value
757 * @param vec Elements of new value
758 * @return 0 on success, non-0 on error
760 static int validate_positive(const struct config_state
*cs
,
761 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
, strerror(errno
));
777 disorder_error(0, "%s:%d: must be positive", cs
->path
, cs
->line
);
783 /** @brief Validate a system username
784 * @param cs Configuration state
785 * @param nvec Length of (proposed) new value
786 * @param vec Elements of new value
787 * @return 0 on success, non-0 on error
789 static int validate_isauser(const struct config_state
*cs
,
790 int attribute((unused
)) nvec
,
792 if(!getpwnam(vec
[0])) {
793 disorder_error(0, "%s:%d: no such user as '%s'", cs
->path
, cs
->line
, vec
[0]);
799 /** @brief Validate a sample format string
800 * @param cs Configuration state
801 * @param nvec Length of (proposed) new value
802 * @param vec Elements of new value
803 * @return 0 on success, non-0 on error
805 static int validate_sample_format(const struct config_state
*cs
,
806 int attribute((unused
)) nvec
,
808 return parse_sample_format(cs
, 0, nvec
, vec
);
811 /** @brief Validate anything
812 * @param cs Configuration state
813 * @param nvec Length of (proposed) new value
814 * @param vec Elements of new value
817 static int validate_any(const struct config_state
attribute((unused
)) *cs
,
818 int attribute((unused
)) nvec
,
819 char attribute((unused
)) **vec
) {
823 /** @brief Validate a URL
824 * @param cs Configuration state
825 * @param nvec Length of (proposed) new value
826 * @param vec Elements of new value
827 * @return 0 on success, non-0 on error
831 static int validate_url(const struct config_state
attribute((unused
)) *cs
,
832 int attribute((unused
)) nvec
,
836 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
837 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
839 n
= strspn(s
, ("abcdefghijklmnopqrstuvwxyz"
840 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
843 disorder_error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
846 if(!strncmp(s
, "http:", 5)
847 || !strncmp(s
, "https:", 6)) {
849 /* we only do a rather cursory check */
850 if(strncmp(s
, "//", 2)) {
851 disorder_error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
858 /** @brief Validate an alias pattern
859 * @param cs Configuration state
860 * @param nvec Length of (proposed) new value
861 * @param vec Elements of new value
862 * @return 0 on success, non-0 on error
864 static int validate_alias(const struct config_state
*cs
,
868 int in_brackets
= 0, c
;
871 disorder_error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
875 disorder_error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
879 while((c
= (unsigned char)*s
++)) {
883 else if(!isalnum(c
)) {
884 disorder_error(0, "%s:%d: invalid part name in alias expansion in '%s'",
885 cs
->path
, cs
->line
, vec
[0]);
893 } else if(c
== '\\') {
894 if(!(c
= (unsigned char)*s
++)) {
895 disorder_error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
896 cs
->path
, cs
->line
, vec
[0]);
898 } else if(c
!= '\\' && c
!= '{') {
899 disorder_error(0, "%s:%d: invalid escape in alias expansion in '%s'",
900 cs
->path
, cs
->line
, vec
[0]);
909 "%s:%d: unterminated part name in alias expansion in '%s'",
910 cs
->path
, cs
->line
, vec
[0]);
916 /** @brief Validate a hash algorithm name
917 * @param cs Configuration state
918 * @param nvec Length of (proposed) new value
919 * @param vec Elements of new value
920 * @return 0 on success, non-0 on error
922 static int validate_algo(const struct config_state
attribute((unused
)) *cs
,
926 disorder_error(0, "%s:%d: invalid algorithm specification", cs
->path
, cs
->line
);
929 if(!valid_authhash(vec
[0])) {
930 disorder_error(0, "%s:%d: unsuported algorithm '%s'", cs
->path
, cs
->line
, vec
[0]);
936 /** @brief Validate a playback backend name
937 * @param cs Configuration state
938 * @param nvec Length of (proposed) new value
939 * @param vec Elements of new value
940 * @return 0 on success, non-0 on error
942 static int validate_backend(const struct config_state
attribute((unused
)) *cs
,
947 disorder_error(0, "%s:%d: invalid sound API specification", cs
->path
, cs
->line
);
950 if(!strcmp(vec
[0], "network")) {
951 disorder_error(0, "'api network' is deprecated; use 'api rtp'");
954 if(config_uaudio_apis
) {
955 for(n
= 0; config_uaudio_apis
[n
]; ++n
)
956 if(!strcmp(vec
[0], config_uaudio_apis
[n
]->name
))
958 disorder_error(0, "%s:%d: unrecognized sound API '%s'", cs
->path
, cs
->line
, vec
[0]);
961 /* In non-server processes we have no idea what's valid */
965 /** @brief Validate a pause mode string
966 * @param cs Configuration state
967 * @param nvec Length of (proposed) new value
968 * @param vec Elements of new value
969 * @return 0 on success, non-0 on error
971 static int validate_pausemode(const struct config_state
attribute((unused
)) *cs
,
974 if(nvec
== 1 && (!strcmp(vec
[0], "silence") || !strcmp(vec
[0], "suspend")))
976 disorder_error(0, "%s:%d: invalid pause mode", cs
->path
, cs
->line
);
980 /** @brief Validate a destination network address
981 * @param cs Configuration state
982 * @param nvec Length of (proposed) new value
983 * @param vec Elements of new value
984 * @return 0 on success, non-0 on error
986 * By a destination address, it is meant that it must not be a wildcard
989 static int validate_destaddr(const struct config_state
attribute((unused
)) *cs
,
992 struct netaddress na
[1];
994 if(netaddress_parse(na
, nvec
, vec
)) {
995 disorder_error(0, "%s:%d: invalid network address", cs
->path
, cs
->line
);
999 disorder_error(0, "%s:%d: destination address required", cs
->path
, cs
->line
);
1006 /** @brief Item name and and offset */
1007 #define C(x) #x, offsetof(struct config, x)
1008 /** @brief Item name and and offset */
1009 #define C2(x,y) #x, offsetof(struct config, y)
1011 /** @brief All configuration items */
1012 static const struct conf conf
[] = {
1013 { C(alias
), &type_string
, validate_alias
},
1014 { C(api
), &type_string
, validate_backend
},
1015 { C(authorization_algorithm
), &type_string
, validate_algo
},
1016 { C(broadcast
), &type_netaddress
, validate_destaddr
},
1017 { C(broadcast_from
), &type_netaddress
, validate_any
},
1018 { C(channel
), &type_string
, validate_any
},
1019 { C(checkpoint_kbyte
), &type_integer
, validate_non_negative
},
1020 { C(checkpoint_min
), &type_integer
, validate_non_negative
},
1021 { C(collection
), &type_collections
, validate_any
},
1022 { C(connect
), &type_netaddress
, validate_destaddr
},
1023 { C(cookie_key_lifetime
), &type_integer
, validate_positive
},
1024 { C(cookie_login_lifetime
), &type_integer
, validate_positive
},
1025 { C(dbversion
), &type_integer
, validate_positive
},
1026 { C(default_rights
), &type_rights
, validate_any
},
1027 { C(device
), &type_string
, validate_any
},
1028 { C(history
), &type_integer
, validate_positive
},
1029 { C(home
), &type_string
, validate_isabspath
},
1030 { C(listen
), &type_netaddress
, validate_any
},
1031 { C(mail_sender
), &type_string
, validate_any
},
1032 { C(mixer
), &type_string
, validate_any
},
1033 { C(mount_rescan
), &type_boolean
, validate_any
},
1034 { C(multicast_loop
), &type_boolean
, validate_any
},
1035 { C(multicast_ttl
), &type_integer
, validate_non_negative
},
1036 { C(namepart
), &type_namepart
, validate_any
},
1037 { C(new_bias
), &type_integer
, validate_positive
},
1038 { C(new_bias_age
), &type_integer
, validate_positive
},
1039 { C(new_max
), &type_integer
, validate_positive
},
1040 { C2(nice
, nice_rescan
), &type_integer
, validate_non_negative
},
1041 { C(nice_rescan
), &type_integer
, validate_non_negative
},
1042 { C(nice_server
), &type_integer
, validate_any
},
1043 { C(nice_speaker
), &type_integer
, validate_any
},
1044 { C(noticed_history
), &type_integer
, validate_positive
},
1045 { C(password
), &type_string
, validate_any
},
1046 { C(pause_mode
), &type_string
, validate_pausemode
},
1047 { C(player
), &type_stringlist_accum
, validate_player
},
1048 { C(playlist_lock_timeout
), &type_integer
, validate_positive
},
1049 { C(playlist_max
) , &type_integer
, validate_positive
},
1050 { C(plugins
), &type_string_accum
, validate_isdir
},
1051 { C(queue_pad
), &type_integer
, validate_positive
},
1052 { C(refresh
), &type_integer
, validate_positive
},
1053 { C(refresh_min
), &type_integer
, validate_non_negative
},
1054 { C(reminder_interval
), &type_integer
, validate_positive
},
1055 { C(remote_userman
), &type_boolean
, validate_any
},
1056 { C(replay_min
), &type_integer
, validate_non_negative
},
1057 { C(rtp_delay_threshold
), &type_integer
, validate_positive
},
1058 { C(rtp_verbose
), &type_boolean
, validate_any
},
1059 { C(sample_format
), &type_sample_format
, validate_sample_format
},
1060 { C(scratch
), &type_string_accum
, validate_isreg
},
1061 { C(sendmail
), &type_string
, validate_isabspath
},
1062 { C(short_display
), &type_integer
, validate_positive
},
1063 { C(signal
), &type_signal
, validate_any
},
1064 { C(smtp_server
), &type_string
, validate_any
},
1065 { C(sox_generation
), &type_integer
, validate_non_negative
},
1066 { C2(speaker_backend
, api
), &type_string
, validate_backend
},
1067 { C(speaker_command
), &type_string
, validate_any
},
1068 { C(stopword
), &type_string_accum
, validate_any
},
1069 { C(templates
), &type_string_accum
, validate_isdir
},
1070 { C(tracklength
), &type_stringlist_accum
, validate_tracklength
},
1071 { C(transform
), &type_transform
, validate_any
},
1072 { C(url
), &type_string
, validate_url
},
1073 { C(user
), &type_string
, validate_isauser
},
1074 { C(username
), &type_string
, validate_any
},
1077 /** @brief Find a configuration item's definition by key */
1078 static const struct conf
*find(const char *key
) {
1081 if((n
= TABLE_FIND(conf
, name
, key
)) < 0)
1086 /** @brief Set a new configuration value
1087 * @param cs Configuration state
1088 * @param nvec Length of @p vec
1089 * @param vec Name and new value
1090 * @return 0 on success, non-0 on error.
1092 * @c vec[0] is the name, the rest is the value.
1094 static int config_set(const struct config_state
*cs
,
1095 int nvec
, char **vec
) {
1096 const struct conf
*which
;
1098 D(("config_set %s", vec
[0]));
1099 if(!(which
= find(vec
[0]))) {
1100 disorder_error(0, "%s:%d: unknown configuration key '%s'",
1101 cs
->path
, cs
->line
, vec
[0]);
1104 return (which
->validate(cs
, nvec
- 1, vec
+ 1)
1105 || which
->type
->set(cs
, which
, nvec
- 1, vec
+ 1));
1108 /** @brief Set a configuration item from parameters
1109 * @param cs Configuration state
1110 * @param which Item to set
1111 * @param ... Value as strings, terminated by (char *)NULL
1112 * @return 0 on success, non-0 on error
1114 static int config_set_args(const struct config_state
*cs
,
1115 const char *which
, ...) {
1121 vector_append(v
, (char *)which
);
1122 va_start(ap
, which
);
1123 while((s
= va_arg(ap
, char *)))
1124 vector_append(v
, s
);
1126 vector_terminate(v
);
1127 int rc
= config_set(cs
, v
->nvec
, v
->vec
);
1132 /** @brief Error callback used by config_include()
1133 * @param msg Error message
1134 * @param u User data (@ref config_state)
1136 static void config_error(const char *msg
, void *u
) {
1137 const struct config_state
*cs
= u
;
1139 disorder_error(0, "%s:%d: %s", cs
->path
, cs
->line
, msg
);
1142 /** @brief Include a file by name
1143 * @param c Configuration to update
1144 * @param path Path to read
1145 * @return 0 on success, non-0 on error
1147 static int config_include(struct config
*c
, const char *path
) {
1149 char *buffer
, *inputbuffer
, **vec
;
1151 struct config_state cs
;
1156 D(("%s: reading configuration", path
));
1157 if(!(fp
= fopen(path
, "r"))) {
1158 disorder_error(errno
, "error opening %s", path
);
1161 while(!inputline(path
, fp
, &inputbuffer
, '\n')) {
1163 if(!(buffer
= mb2utf8(inputbuffer
))) {
1164 disorder_error(errno
, "%s:%d: cannot convert to UTF-8", cs
.path
, cs
.line
);
1170 if(!(vec
= split(buffer
, &n
, SPLIT_COMMENTS
|SPLIT_QUOTES
,
1171 config_error
, &cs
))) {
1177 /* 'include' is special-cased */
1178 if(!strcmp(vec
[0], "include")) {
1180 disorder_error(0, "%s:%d: must be 'include PATH'", cs
.path
, cs
.line
);
1183 config_include(c
, vec
[1]);
1185 ret
|= config_set(&cs
, n
, vec
);
1187 for(n
= 0; vec
[n
]; ++n
) xfree(vec
[n
]);
1192 disorder_error(errno
, "error reading %s", path
);
1199 /** @brief Default stopword setting */
1200 static const char *const default_stopwords
[] = {
1259 #define NDEFAULT_STOPWORDS (sizeof default_stopwords / sizeof *default_stopwords)
1261 /** @brief Default player patterns */
1262 static const char *const default_players
[] = {
1268 #define NDEFAULT_PLAYERS (sizeof default_players / sizeof *default_players)
1270 /** @brief Make a new default configuration
1271 * @return New configuration
1273 static struct config
*config_default(void) {
1274 struct config
*c
= xmalloc(sizeof *c
);
1275 const char *logname
;
1277 struct config_state cs
;
1280 cs
.path
= "<internal>";
1283 /* Strings had better be xstrdup'd as they will get freed at some point. */
1285 c
->home
= xstrdup(pkgstatedir
);
1286 if(!(pw
= getpwuid(getuid())))
1287 disorder_fatal(0, "cannot determine our username");
1288 logname
= pw
->pw_name
;
1289 c
->username
= xstrdup(logname
);
1292 c
->signal
= SIGKILL
;
1293 c
->alias
= xstrdup("{/artist}{/album}{/title}{ext}");
1294 c
->device
= xstrdup("default");
1295 c
->nice_rescan
= 10;
1296 c
->speaker_command
= 0;
1297 c
->sample_format
.bits
= 16;
1298 c
->sample_format
.rate
= 44100;
1299 c
->sample_format
.channels
= 2;
1300 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1302 c
->replay_min
= 8 * 3600;
1304 c
->multicast_ttl
= 1;
1305 c
->multicast_loop
= 1;
1306 c
->authorization_algorithm
= xstrdup("sha1");
1307 c
->noticed_history
= 31;
1308 c
->short_display
= 32;
1312 c
->cookie_login_lifetime
= 86400;
1313 c
->cookie_key_lifetime
= 86400 * 7;
1314 if(sendmail_binary
[0] && strcmp(sendmail_binary
, "none"))
1315 c
->sendmail
= xstrdup(sendmail_binary
);
1316 c
->smtp_server
= xstrdup("127.0.0.1");
1318 c
->reminder_interval
= 600; /* 10m */
1319 c
->new_bias_age
= 7 * 86400; /* 1 week */
1320 c
->new_bias
= 4500000; /* 50 times the base weight */
1321 c
->sox_generation
= DEFAULT_SOX_GENERATION
;
1322 c
->playlist_max
= INT_MAX
; /* effectively no limit */
1323 c
->playlist_lock_timeout
= 10; /* 10s */
1324 c
->mount_rescan
= 1;
1325 /* Default stopwords */
1326 if(config_set(&cs
, (int)NDEFAULT_STOPWORDS
, (char **)default_stopwords
))
1328 /* Default player configuration */
1329 for(n
= 0; n
< NDEFAULT_PLAYERS
; ++n
) {
1330 if(config_set_args(&cs
, "player",
1331 default_players
[n
], "execraw", "disorder-decode", (char *)0))
1333 if(config_set_args(&cs
, "tracklength",
1334 default_players
[n
], "disorder-tracklength", (char *)0))
1337 c
->broadcast
.af
= -1;
1338 c
->broadcast_from
.af
= -1;
1344 /** @brief Construct a filename
1345 * @param c Configuration
1346 * @param name Base filename
1347 * @return Full filename
1349 * Usually use config_get_file() instead.
1351 char *config_get_file2(struct config
*c
, const char *name
) {
1354 byte_xasprintf(&s
, "%s/%s", c
->home
, name
);
1358 /** @brief Set the default configuration file */
1359 static void set_configfile(void) {
1361 byte_xasprintf(&configfile
, "%s/config", pkgconfdir
);
1364 /** @brief Free a configuration object
1365 * @param c Configuration to free
1367 * @p c is indeterminate after this function is called.
1369 void config_free(struct config
*c
) {
1373 for(n
= 0; n
< (int)(sizeof conf
/ sizeof *conf
); ++n
)
1374 conf
[n
].type
->free(c
, &conf
[n
]);
1375 for(n
= 0; n
< c
->nparts
; ++n
)
1382 /** @brief Set post-parse defaults
1383 * @param c Configuration to update
1384 * @param server True when running in the server
1386 * If @p server is set then certain parts of the configuration are more
1387 * strictly validated.
1389 static void config_postdefaults(struct config
*c
,
1391 struct config_state cs
;
1392 const struct conf
*whoami
;
1395 static const char *namepart
[][4] = {
1396 { "title", "/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
1397 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1398 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1399 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1400 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1402 #define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1404 static const char *transform
[][5] = {
1405 { "track", "^.*/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
1406 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1407 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1408 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1409 { "dir", "[[:punct:]]", "", "sort", "g", }
1411 #define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1413 cs
.path
= "<internal>";
1416 if(!c
->namepart
.n
) {
1417 whoami
= find("namepart");
1418 for(n
= 0; n
< NNAMEPART
; ++n
)
1419 set_namepart(&cs
, whoami
, 4, (char **)namepart
[n
]);
1421 if(!c
->transform
.n
) {
1422 whoami
= find("transform");
1423 for(n
= 0; n
< NTRANSFORM
; ++n
)
1424 set_transform(&cs
, whoami
, 5, (char **)transform
[n
]);
1427 if(c
->speaker_command
)
1428 c
->api
= xstrdup("command");
1429 else if(c
->broadcast
.af
!= -1)
1430 c
->api
= xstrdup("rtp");
1431 else if(config_uaudio_apis
)
1432 c
->api
= xstrdup(config_uaudio_apis
[0]->name
);
1434 c
->api
= xstrdup("<none>");
1436 if(!strcmp(c
->api
, "network"))
1437 c
->api
= xstrdup("rtp");
1439 if(!strcmp(c
->api
, "command") && !c
->speaker_command
)
1440 disorder_fatal(0, "'api command' but speaker_command is not set");
1441 if((!strcmp(c
->api
, "rtp")) && c
->broadcast
.af
== -1)
1442 disorder_fatal(0, "'api rtp' but broadcast is not set");
1444 /* Override sample format */
1445 if(!strcmp(c
->api
, "rtp")) {
1446 c
->sample_format
.rate
= 44100;
1447 c
->sample_format
.channels
= 2;
1448 c
->sample_format
.bits
= 16;
1449 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1451 if(!strcmp(c
->api
, "coreaudio")) {
1452 c
->sample_format
.rate
= 44100;
1453 c
->sample_format
.channels
= 2;
1454 c
->sample_format
.bits
= 16;
1455 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1457 if(!c
->default_rights
) {
1458 rights_type r
= RIGHTS__MASK
& ~(RIGHT_ADMIN
|RIGHT_REGISTER
1460 |RIGHT_SCRATCH__MASK
1461 |RIGHT_REMOVE__MASK
);
1462 r
|= RIGHT_SCRATCH_ANY
|RIGHT_MOVE_ANY
|RIGHT_REMOVE_ANY
;
1463 c
->default_rights
= rights_string(r
);
1467 /** @brief (Re-)read the config file
1468 * @param server If set, do extra checking
1469 * @param oldconfig Old configuration for compatibility check
1470 * @return 0 on success, non-0 on error
1472 * If @p oldconfig is set, then certain compatibility checks are done between
1473 * the old and new configurations.
1475 int config_read(int server
,
1476 const struct config
*oldconfig
) {
1482 c
= config_default();
1483 /* standalone Disobedience installs might not have a global config file */
1484 if(access(configfile
, F_OK
) == 0)
1485 if(config_include(c
, configfile
))
1487 /* if we can read the private config file, do */
1488 if((privconf
= config_private())
1489 && access(privconf
, R_OK
) == 0
1490 && config_include(c
, privconf
))
1493 /* if there's a per-user system config file for this user, read it */
1494 if(config_per_user
) {
1495 if(!(pw
= getpwuid(getuid())))
1496 disorder_fatal(0, "cannot determine our username");
1497 if((privconf
= config_usersysconf(pw
))
1498 && access(privconf
, F_OK
) == 0
1499 && config_include(c
, privconf
))
1502 /* if we have a password file, read it */
1503 if((privconf
= config_userconf(0, pw
))
1504 && access(privconf
, F_OK
) == 0
1505 && config_include(c
, privconf
))
1509 /* install default namepart and transform settings */
1510 config_postdefaults(c
, server
);
1513 if(strcmp(c
->home
, oldconfig
->home
)) {
1514 disorder_error(0, "'home' cannot be changed without a restart");
1517 if(strcmp(c
->alias
, oldconfig
->alias
)) {
1518 disorder_error(0, "'alias' cannot be changed without a restart");
1521 if(strcmp(c
->user
, oldconfig
->user
)) {
1522 disorder_error(0, "'user' cannot be changed without a restart");
1525 if(c
->nice_speaker
!= oldconfig
->nice_speaker
) {
1526 disorder_error(0, "'nice_speaker' cannot be changed without a restart");
1527 /* ...but we accept the new config anyway */
1529 if(c
->nice_server
!= oldconfig
->nice_server
) {
1530 disorder_error(0, "'nice_server' cannot be changed without a restart");
1531 /* ...but we accept the new config anyway */
1533 if(namepartlist_compare(&c
->namepart
, &oldconfig
->namepart
)) {
1534 disorder_error(0, "'namepart' settings cannot be changed without a restart");
1537 if(stringlist_compare(&c
->stopword
, &oldconfig
->stopword
)) {
1538 disorder_error(0, "'stopword' settings cannot be changed without a restart");
1542 disorder_error(0, "not installing incompatible new configuration");
1546 /* everything is good so we shall use the new config */
1547 config_free(config
);
1548 /* warn about obsolete directives */
1553 /** @brief Return the path to the private configuration file */
1554 char *config_private(void) {
1558 byte_xasprintf(&s
, "%s.private", configfile
);
1562 /** @brief Return the path to user's personal configuration file */
1563 char *config_userconf(const char *home
, const struct passwd
*pw
) {
1566 if(!home
&& !pw
&& !(pw
= getpwuid(getuid())))
1567 disorder_fatal(0, "cannot determine our username");
1568 byte_xasprintf(&s
, "%s/.disorder/passwd", home ? home
: pw
->pw_dir
);
1572 /** @brief Return the path to user-specific system configuration */
1573 char *config_usersysconf(const struct passwd
*pw
) {
1577 if(!strchr(pw
->pw_name
, '/')) {
1578 byte_xasprintf(&s
, "%s.%s", configfile
, pw
->pw_name
);
1584 /** @brief Get a filename within the home directory
1585 * @param name Relative name
1588 char *config_get_file(const char *name
) {
1589 return config_get_file2(config
, name
);
1592 /** @brief Order two stringlists
1593 * @param a First stringlist
1594 * @param b Second stringlist
1595 * @return <0, 0 or >0 if a<b, a=b or a>b
1597 static int stringlist_compare(const struct stringlist
*a
,
1598 const struct stringlist
*b
) {
1601 while(n
< a
->n
&& n
< b
->n
) {
1602 if((c
= strcmp(a
->s
[n
], b
->s
[n
])))
1608 else if(a
->n
> b
->n
)
1614 /** @brief Order two namepart definitions
1615 * @param a First namepart definition
1616 * @param b Second namepart definition
1617 * @return <0, 0 or >0 if a<b, a=b or a>b
1619 static int namepart_compare(const struct namepart
*a
,
1620 const struct namepart
*b
) {
1623 if((c
= strcmp(a
->part
, b
->part
)))
1625 if((c
= strcmp(a
->res
, b
->res
)))
1627 if((c
= strcmp(a
->replace
, b
->replace
)))
1629 if((c
= strcmp(a
->context
, b
->context
)))
1631 if(a
->reflags
> b
->reflags
)
1633 if(a
->reflags
< b
->reflags
)
1638 /** @brief Order two lists of namepart definitions
1639 * @param a First list of namepart definitions
1640 * @param b Second list of namepart definitions
1641 * @return <0, 0 or >0 if a<b, a=b or a>b
1643 static int namepartlist_compare(const struct namepartlist
*a
,
1644 const struct namepartlist
*b
) {
1647 while(n
< a
->n
&& n
< b
->n
) {
1648 if((c
= namepart_compare(&a
->s
[n
], &b
->s
[n
])))
1654 else if(a
->n
< b
->n
)
1660 /** @brief Verify configuration table.
1661 * @return The number of problems found
1663 int config_verify(void) {
1665 for(size_t n
= 1; n
< sizeof conf
/ sizeof *conf
; ++n
)
1666 if(strcmp(conf
[n
-1].name
, conf
[n
].name
) >= 0) {
1667 fprintf(stderr
, "%s >= %s\n", conf
[n
-1].name
, conf
[n
].name
);