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 set_signal(const struct config_state
*cs
,
113 const struct conf
*whoami
,
114 int nvec
, char **vec
) {
118 error(0, "%s:%d: '%s' requires one argument",
119 cs
->path
, cs
->line
, whoami
->name
);
122 if((n
= find_signal(vec
[0])) == -1) {
123 error(0, "%s:%d: unknown signal '%s'",
124 cs
->path
, cs
->line
, vec
[0]);
127 VALUE(cs
->config
, int) = n
;
131 static int set_collections(const struct config_state
*cs
,
132 const struct conf
*whoami
,
133 int nvec
, char **vec
) {
134 struct collectionlist
*cl
;
135 const char *root
, *encoding
, *module
;
154 error(0, "%s:%d: '%s' requires at least one argument",
155 cs
->path
, cs
->line
, whoami
->name
);
158 error(0, "%s:%d: '%s' requires at most three arguments",
159 cs
->path
, cs
->line
, whoami
->name
);
162 /* Sanity check root */
164 error(0, "%s:%d: collection root must start with '/'",
168 if(root
[1] && root
[strlen(root
)-1] == '/') {
169 error(0, "%s:%d: collection root must not end with '/'",
177 encoding
= nl_langinfo(CODESET
);
178 cl
= ADDRESS(cs
->config
, struct collectionlist
);
180 cl
->s
= xrealloc(cl
->s
, cl
->n
* sizeof (struct collection
));
181 cl
->s
[cl
->n
- 1].module
= xstrdup(module
);
182 cl
->s
[cl
->n
- 1].encoding
= xstrdup(encoding
);
183 cl
->s
[cl
->n
- 1].root
= xstrdup(root
);
187 static int set_boolean(const struct config_state
*cs
,
188 const struct conf
*whoami
,
189 int nvec
, char **vec
) {
193 error(0, "%s:%d: '%s' takes only one argument",
194 cs
->path
, cs
->line
, whoami
->name
);
197 if(!strcmp(vec
[0], "yes")) state
= 1;
198 else if(!strcmp(vec
[0], "no")) state
= 0;
200 error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'",
201 cs
->path
, cs
->line
, whoami
->name
);
204 VALUE(cs
->config
, int) = state
;
208 static int set_string(const struct config_state
*cs
,
209 const struct conf
*whoami
,
210 int nvec
, char **vec
) {
212 error(0, "%s:%d: '%s' takes only one argument",
213 cs
->path
, cs
->line
, whoami
->name
);
216 VALUE(cs
->config
, char *) = xstrdup(vec
[0]);
220 static int set_stringlist(const struct config_state
*cs
,
221 const struct conf
*whoami
,
222 int nvec
, char **vec
) {
224 struct stringlist
*sl
;
226 sl
= ADDRESS(cs
->config
, struct stringlist
);
228 for(n
= 0; n
< nvec
; ++n
) {
230 sl
->s
= xrealloc(sl
->s
, (sl
->n
* sizeof (char *)));
231 sl
->s
[sl
->n
- 1] = xstrdup(vec
[n
]);
236 static int set_integer(const struct config_state
*cs
,
237 const struct conf
*whoami
,
238 int nvec
, char **vec
) {
242 error(0, "%s:%d: '%s' takes only one argument",
243 cs
->path
, cs
->line
, whoami
->name
);
246 if(xstrtol(ADDRESS(cs
->config
, long), vec
[0], &e
, 0)) {
247 error(errno
, "%s:%d: converting integer", cs
->path
, cs
->line
);
251 error(0, "%s:%d: invalid integer syntax", cs
->path
, cs
->line
);
257 static int set_stringlist_accum(const struct config_state
*cs
,
258 const struct conf
*whoami
,
259 int nvec
, char **vec
) {
261 struct stringlist
*s
;
262 struct stringlistlist
*sll
;
264 sll
= ADDRESS(cs
->config
, struct stringlistlist
);
270 sll
->s
= xrealloc(sll
->s
, (sll
->n
* sizeof (struct stringlist
)));
271 s
= &sll
->s
[sll
->n
- 1];
273 s
->s
= xmalloc((nvec
+ 1) * sizeof (char *));
274 for(n
= 0; n
< nvec
; ++n
)
275 s
->s
[n
] = xstrdup(vec
[n
]);
279 static int set_string_accum(const struct config_state
*cs
,
280 const struct conf
*whoami
,
281 int nvec
, char **vec
) {
283 struct stringlist
*sl
;
285 sl
= ADDRESS(cs
->config
, struct stringlist
);
290 for(n
= 0; n
< nvec
; ++n
) {
292 sl
->s
= xrealloc(sl
->s
, (sl
->n
* sizeof (char *)));
293 sl
->s
[sl
->n
- 1] = xstrdup(vec
[n
]);
298 static int set_restrict(const struct config_state
*cs
,
299 const struct conf
*whoami
,
300 int nvec
, char **vec
) {
304 static const struct restriction
{
308 { "remove", RESTRICT_REMOVE
},
309 { "scratch", RESTRICT_SCRATCH
},
310 { "move", RESTRICT_MOVE
},
313 for(n
= 0; n
< nvec
; ++n
) {
314 if((i
= TABLE_FIND(restrictions
, name
, vec
[n
])) < 0) {
315 error(0, "%s:%d: invalid restriction '%s'",
316 cs
->path
, cs
->line
, vec
[n
]);
319 r
|= restrictions
[i
].bit
;
321 VALUE(cs
->config
, unsigned) = r
;
325 static int parse_sample_format(const struct config_state
*cs
,
326 struct stream_header
*format
,
327 int nvec
, char **vec
) {
332 error(0, "%s:%d: wrong number of arguments", cs
->path
, cs
->line
);
335 if(xstrtol(&t
, p
, &p
, 0)) {
336 error(errno
, "%s:%d: converting bits-per-sample", cs
->path
, cs
->line
);
339 if(t
!= 8 && t
!= 16) {
340 error(0, "%s:%d: bad bite-per-sample (%ld)", cs
->path
, cs
->line
, t
);
343 if(format
) format
->bits
= t
;
345 case 'l': case 'L': t
= ENDIAN_LITTLE
; p
++; break;
346 case 'b': case 'B': t
= ENDIAN_BIG
; p
++; break;
347 default: t
= ENDIAN_NATIVE
; break;
349 if(format
) format
->endian
= t
;
351 error(errno
, "%s:%d: expected `/' after bits-per-sample",
356 if(xstrtol(&t
, p
, &p
, 0)) {
357 error(errno
, "%s:%d: converting sample-rate", cs
->path
, cs
->line
);
360 if(t
< 1 || t
> INT_MAX
) {
361 error(0, "%s:%d: silly sample-rate (%ld)", cs
->path
, cs
->line
, t
);
364 if(format
) format
->rate
= t
;
366 error(0, "%s:%d: expected `/' after sample-rate",
371 if(xstrtol(&t
, p
, &p
, 0)) {
372 error(errno
, "%s:%d: converting channels", cs
->path
, cs
->line
);
376 error(0, "%s:%d: silly number (%ld) of channels", cs
->path
, cs
->line
, t
);
379 if(format
) format
->channels
= t
;
381 error(0, "%s:%d: junk after channels", cs
->path
, cs
->line
);
387 static int set_sample_format(const struct config_state
*cs
,
388 const struct conf
*whoami
,
389 int nvec
, char **vec
) {
390 return parse_sample_format(cs
, ADDRESS(cs
->config
, struct stream_header
),
394 static int set_namepart(const struct config_state
*cs
,
395 const struct conf
*whoami
,
396 int nvec
, char **vec
) {
397 struct namepartlist
*npl
= ADDRESS(cs
->config
, struct namepartlist
);
404 error(0, "%s:%d: namepart needs at least 3 arguments", cs
->path
, cs
->line
);
408 error(0, "%s:%d: namepart needs at most 5 arguments", cs
->path
, cs
->line
);
411 reflags
= nvec
>= 5 ?
regsub_flags(vec
[4]) : 0;
412 if(!(re
= pcre_compile(vec
[1],
414 |regsub_compile_options(reflags
),
415 &errstr
, &erroffset
, 0))) {
416 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
417 cs
->path
, cs
->line
, vec
[1], errstr
, erroffset
);
420 npl
->s
= xrealloc(npl
->s
, (npl
->n
+ 1) * sizeof (struct namepart
));
421 npl
->s
[npl
->n
].part
= xstrdup(vec
[0]);
422 npl
->s
[npl
->n
].re
= re
;
423 npl
->s
[npl
->n
].replace
= xstrdup(vec
[2]);
424 npl
->s
[npl
->n
].context
= xstrdup(vec
[3]);
425 npl
->s
[npl
->n
].reflags
= reflags
;
427 /* XXX a bit of a bodge; relies on there being very few parts. */
428 for(n
= 0; (n
< cs
->config
->nparts
429 && strcmp(cs
->config
->parts
[n
], vec
[0])); ++n
)
431 if(n
>= cs
->config
->nparts
) {
432 cs
->config
->parts
= xrealloc(cs
->config
->parts
,
433 (cs
->config
->nparts
+ 1) * sizeof (char *));
434 cs
->config
->parts
[cs
->config
->nparts
++] = xstrdup(vec
[0]);
439 static int set_transform(const struct config_state
*cs
,
440 const struct conf
*whoami
,
441 int nvec
, char **vec
) {
442 struct transformlist
*tl
= ADDRESS(cs
->config
, struct transformlist
);
449 error(0, "%s:%d: transform needs at least 3 arguments", cs
->path
, cs
->line
);
453 error(0, "%s:%d: transform needs at most 5 arguments", cs
->path
, cs
->line
);
456 reflags
= (nvec
>= 5 ?
regsub_flags(vec
[4]) : 0);
457 if(!(re
= pcre_compile(vec
[1],
459 |regsub_compile_options(reflags
),
460 &errstr
, &erroffset
, 0))) {
461 error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)",
462 cs
->path
, cs
->line
, vec
[1], errstr
, erroffset
);
465 tl
->t
= xrealloc(tl
->t
, (tl
->n
+ 1) * sizeof (struct namepart
));
466 tl
->t
[tl
->n
].type
= xstrdup(vec
[0]);
467 tl
->t
[tl
->n
].context
= xstrdup(vec
[3] ? vec
[3] : "*");
468 tl
->t
[tl
->n
].re
= re
;
469 tl
->t
[tl
->n
].replace
= xstrdup(vec
[2]);
470 tl
->t
[tl
->n
].flags
= reflags
;
475 static int set_rights(const struct config_state
*cs
,
476 const struct conf
*whoami
,
477 int nvec
, char **vec
) {
479 error(0, "%s:%d: '%s' requires one argument",
480 cs
->path
, cs
->line
, whoami
->name
);
483 if(parse_rights(vec
[0], 0, 1)) {
484 error(0, "%s:%d: invalid rights string '%s'",
485 cs
->path
, cs
->line
, vec
[0]);
488 *ADDRESS(cs
->config
, char *) = vec
[0];
492 static int set_netaddress(const struct config_state
*cs
,
493 const struct conf
*whoami
,
494 int nvec
, char **vec
) {
495 struct netaddress
*na
= ADDRESS(cs
->config
, struct netaddress
);
497 if(netaddress_parse(na
, nvec
, vec
)) {
498 error(0, "%s:%d: invalid network address", cs
->path
, cs
->line
);
506 static void free_none(struct config
attribute((unused
)) *c
,
507 const struct conf
attribute((unused
)) *whoami
) {
510 static void free_string(struct config
*c
,
511 const struct conf
*whoami
) {
512 xfree(VALUE(c
, char *));
515 static void free_stringlist(struct config
*c
,
516 const struct conf
*whoami
) {
518 struct stringlist
*sl
= ADDRESS(c
, struct stringlist
);
520 for(n
= 0; n
< sl
->n
; ++n
)
525 static void free_stringlistlist(struct config
*c
,
526 const struct conf
*whoami
) {
528 struct stringlistlist
*sll
= ADDRESS(c
, struct stringlistlist
);
529 struct stringlist
*sl
;
531 for(n
= 0; n
< sll
->n
; ++n
) {
533 for(m
= 0; m
< sl
->n
; ++m
)
540 static void free_collectionlist(struct config
*c
,
541 const struct conf
*whoami
) {
542 struct collectionlist
*cll
= ADDRESS(c
, struct collectionlist
);
543 struct collection
*cl
;
546 for(n
= 0; n
< cll
->n
; ++n
) {
555 static void free_namepartlist(struct config
*c
,
556 const struct conf
*whoami
) {
557 struct namepartlist
*npl
= ADDRESS(c
, struct namepartlist
);
561 for(n
= 0; n
< npl
->n
; ++n
) {
564 pcre_free(np
->re
); /* ...whatever pcre_free is set to. */
571 static void free_transformlist(struct config
*c
,
572 const struct conf
*whoami
) {
573 struct transformlist
*tl
= ADDRESS(c
, struct transformlist
);
577 for(n
= 0; n
< tl
->n
; ++n
) {
580 pcre_free(t
->re
); /* ...whatever pcre_free is set to. */
587 static void free_netaddress(struct config
*c
,
588 const struct conf
*whoami
) {
589 struct netaddress
*na
= ADDRESS(c
, struct netaddress
);
594 /* configuration types */
596 static const struct conftype
597 type_signal
= { set_signal
, free_none
},
598 type_collections
= { set_collections
, free_collectionlist
},
599 type_boolean
= { set_boolean
, free_none
},
600 type_string
= { set_string
, free_string
},
601 type_stringlist
= { set_stringlist
, free_stringlist
},
602 type_integer
= { set_integer
, free_none
},
603 type_stringlist_accum
= { set_stringlist_accum
, free_stringlistlist
},
604 type_string_accum
= { set_string_accum
, free_stringlist
},
605 type_sample_format
= { set_sample_format
, free_none
},
606 type_restrict
= { set_restrict
, free_none
},
607 type_namepart
= { set_namepart
, free_namepartlist
},
608 type_transform
= { set_transform
, free_transformlist
},
609 type_netaddress
= { set_netaddress
, free_netaddress
},
610 type_rights
= { set_rights
, free_none
};
612 /* specific validation routine */
614 #define VALIDATE_FILE(test, what) do { \
618 for(n = 0; n < nvec; ++n) { \
619 if(stat(vec[n], &sb) < 0) { \
620 error(errno, "%s:%d: %s", cs->path, cs->line, vec[n]); \
623 if(!test(sb.st_mode)) { \
624 error(0, "%s:%d: %s is not a %s", \
625 cs->path, cs->line, vec[n], what); \
631 static int validate_isabspath(const struct config_state
*cs
,
632 int nvec
, char **vec
) {
635 for(n
= 0; n
< nvec
; ++n
)
636 if(vec
[n
][0] != '/') {
637 error(errno
, "%s:%d: %s: not an absolute path",
638 cs
->path
, cs
->line
, vec
[n
]);
644 static int validate_isdir(const struct config_state
*cs
,
645 int nvec
, char **vec
) {
646 VALIDATE_FILE(S_ISDIR
, "directory");
650 static int validate_isreg(const struct config_state
*cs
,
651 int nvec
, char **vec
) {
652 VALIDATE_FILE(S_ISREG
, "regular file");
656 static int validate_player(const struct config_state
*cs
,
658 char attribute((unused
)) **vec
) {
660 error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
667 static int validate_tracklength(const struct config_state
*cs
,
669 char attribute((unused
)) **vec
) {
671 error(0, "%s:%d: should be at least 'tracklength PATTERN MODULE'",
678 static int validate_allow(const struct config_state
*cs
,
680 char attribute((unused
)) **vec
) {
682 error(0, "%s:%d: must be 'allow NAME PASS'", cs
->path
, cs
->line
);
688 static int validate_non_negative(const struct config_state
*cs
,
689 int nvec
, char **vec
) {
693 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
697 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
700 if(xstrtol(&n
, vec
[0], 0, 0)) {
701 error(0, "%s:%d: %s", cs
->path
, cs
->line
, strerror(errno
));
705 error(0, "%s:%d: must not be negative", cs
->path
, cs
->line
);
711 static int validate_positive(const struct config_state
*cs
,
712 int nvec
, char **vec
) {
716 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
720 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
723 if(xstrtol(&n
, vec
[0], 0, 0)) {
724 error(0, "%s:%d: %s", cs
->path
, cs
->line
, strerror(errno
));
728 error(0, "%s:%d: must be positive", cs
->path
, cs
->line
);
734 static int validate_isauser(const struct config_state
*cs
,
735 int attribute((unused
)) nvec
,
739 if(!(pw
= getpwnam(vec
[0]))) {
740 error(0, "%s:%d: no such user as '%s'", cs
->path
, cs
->line
, vec
[0]);
746 static int validate_sample_format(const struct config_state
*cs
,
747 int attribute((unused
)) nvec
,
749 return parse_sample_format(cs
, 0, nvec
, vec
);
752 static int validate_any(const struct config_state
attribute((unused
)) *cs
,
753 int attribute((unused
)) nvec
,
754 char attribute((unused
)) **vec
) {
758 static int validate_url(const struct config_state
attribute((unused
)) *cs
,
759 int attribute((unused
)) nvec
,
763 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
764 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
766 n
= strspn(s
, ("abcdefghijklmnopqrstuvwxyz"
767 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
770 error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
773 if(!strncmp(s
, "http:", 5)
774 || !strncmp(s
, "https:", 6)) {
776 /* we only do a rather cursory check */
777 if(strncmp(s
, "//", 2)) {
778 error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
785 static int validate_alias(const struct config_state
*cs
,
789 int in_brackets
= 0, c
;
792 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
796 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
800 while((c
= (unsigned char)*s
++)) {
804 else if(!isalnum(c
)) {
805 error(0, "%s:%d: invalid part name in alias expansion in '%s'",
806 cs
->path
, cs
->line
, vec
[0]);
814 } else if(c
== '\\') {
815 if(!(c
= (unsigned char)*s
++)) {
816 error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
817 cs
->path
, cs
->line
, vec
[0]);
819 } else if(c
!= '\\' && c
!= '{') {
820 error(0, "%s:%d: invalid escape in alias expansion in '%s'",
821 cs
->path
, cs
->line
, vec
[0]);
829 error(0, "%s:%d: unterminated part name in alias expansion in '%s'",
830 cs
->path
, cs
->line
, vec
[0]);
836 static int validate_addrport(const struct config_state
attribute((unused
)) *cs
,
838 char attribute((unused
)) **vec
) {
841 error(0, "%s:%d: missing address",
845 error(0, "%s:%d: missing port name/number",
851 error(0, "%s:%d: expected ADDRESS PORT",
857 static int validate_port(const struct config_state
attribute((unused
)) *cs
,
859 char attribute((unused
)) **vec
) {
862 error(0, "%s:%d: missing address",
869 error(0, "%s:%d: expected [ADDRESS] PORT",
875 static int validate_algo(const struct config_state
attribute((unused
)) *cs
,
879 error(0, "%s:%d: invalid algorithm specification", cs
->path
, cs
->line
);
882 if(!valid_authhash(vec
[0])) {
883 error(0, "%s:%d: unsuported algorithm '%s'", cs
->path
, cs
->line
, vec
[0]);
889 static int validate_backend(const struct config_state
attribute((unused
)) *cs
,
894 error(0, "%s:%d: invalid sound API specification", cs
->path
, cs
->line
);
897 if(!strcmp(vec
[0], "network")) {
898 error(0, "'api network' is deprecated; use 'api rtp'");
901 if(config_uaudio_apis
) {
902 for(n
= 0; config_uaudio_apis
[n
]; ++n
)
903 if(!strcmp(vec
[0], config_uaudio_apis
[n
]->name
))
905 error(0, "%s:%d: unrecognized sound API '%s'", cs
->path
, cs
->line
, vec
[0]);
908 /* In non-server processes we have no idea what's valid */
912 static int validate_pausemode(const struct config_state
attribute((unused
)) *cs
,
915 if(nvec
== 1 && (!strcmp(vec
[0], "silence") || !strcmp(vec
[0], "suspend")))
917 error(0, "%s:%d: invalid pause mode", cs
->path
, cs
->line
);
921 static int validate_destaddr(const struct config_state
attribute((unused
)) *cs
,
924 struct netaddress na
[1];
926 if(netaddress_parse(na
, nvec
, vec
)) {
927 error(0, "%s:%d: invalid network address", cs
->path
, cs
->line
);
931 error(0, "%s:%d: destination address required", cs
->path
, cs
->line
);
937 /** @brief Item name and and offset */
938 #define C(x) #x, offsetof(struct config, x)
939 /** @brief Item name and and offset */
940 #define C2(x,y) #x, offsetof(struct config, y)
942 /** @brief All configuration items */
943 static const struct conf conf
[] = {
944 { C(alias
), &type_string
, validate_alias
},
945 { C(allow
), &type_stringlist_accum
, validate_allow
},
946 { C(api
), &type_string
, validate_backend
},
947 { C(authorization_algorithm
), &type_string
, validate_algo
},
948 { C(broadcast
), &type_netaddress
, validate_destaddr
},
949 { C(broadcast_from
), &type_netaddress
, validate_any
},
950 { C(channel
), &type_string
, validate_any
},
951 { C(checkpoint_kbyte
), &type_integer
, validate_non_negative
},
952 { C(checkpoint_min
), &type_integer
, validate_non_negative
},
953 { C(collection
), &type_collections
, validate_any
},
954 { C(connect
), &type_stringlist
, validate_addrport
},
955 { C(cookie_login_lifetime
), &type_integer
, validate_positive
},
956 { C(cookie_key_lifetime
), &type_integer
, validate_positive
},
957 { C(dbversion
), &type_integer
, validate_positive
},
958 { C(default_rights
), &type_rights
, validate_any
},
959 { C(device
), &type_string
, validate_any
},
960 { C(gap
), &type_integer
, validate_non_negative
},
961 { C(history
), &type_integer
, validate_positive
},
962 { C(home
), &type_string
, validate_isabspath
},
963 { C(listen
), &type_stringlist
, validate_port
},
964 { C(lock
), &type_boolean
, validate_any
},
965 { C(mail_sender
), &type_string
, validate_any
},
966 { C(mixer
), &type_string
, validate_any
},
967 { C(multicast_loop
), &type_boolean
, validate_any
},
968 { C(multicast_ttl
), &type_integer
, validate_non_negative
},
969 { C(namepart
), &type_namepart
, validate_any
},
970 { C(new_bias
), &type_integer
, validate_positive
},
971 { C(new_bias_age
), &type_integer
, validate_positive
},
972 { C(new_max
), &type_integer
, validate_positive
},
973 { C2(nice
, nice_rescan
), &type_integer
, validate_non_negative
},
974 { C(nice_rescan
), &type_integer
, validate_non_negative
},
975 { C(nice_server
), &type_integer
, validate_any
},
976 { C(nice_speaker
), &type_integer
, validate_any
},
977 { C(noticed_history
), &type_integer
, validate_positive
},
978 { C(password
), &type_string
, validate_any
},
979 { C(pause_mode
), &type_string
, validate_pausemode
},
980 { C(player
), &type_stringlist_accum
, validate_player
},
981 { C(plugins
), &type_string_accum
, validate_isdir
},
982 { C(prefsync
), &type_integer
, validate_positive
},
983 { C(queue_pad
), &type_integer
, validate_positive
},
984 { C(replay_min
), &type_integer
, validate_non_negative
},
985 { C(refresh
), &type_integer
, validate_positive
},
986 { C(reminder_interval
), &type_integer
, validate_positive
},
987 { C(remote_userman
), &type_boolean
, validate_any
},
988 { C2(restrict
, restrictions
), &type_restrict
, validate_any
},
989 { C(rtp_delay_threshold
), &type_integer
, validate_positive
},
990 { C(sample_format
), &type_sample_format
, validate_sample_format
},
991 { C(scratch
), &type_string_accum
, validate_isreg
},
992 { C(sendmail
), &type_string
, validate_isabspath
},
993 { C(short_display
), &type_integer
, validate_positive
},
994 { C(signal
), &type_signal
, validate_any
},
995 { C(smtp_server
), &type_string
, validate_any
},
996 { C(sox_generation
), &type_integer
, validate_non_negative
},
997 { C2(speaker_backend
, api
), &type_string
, validate_backend
},
998 { C(speaker_command
), &type_string
, validate_any
},
999 { C(stopword
), &type_string_accum
, validate_any
},
1000 { C(templates
), &type_string_accum
, validate_isdir
},
1001 { C(tracklength
), &type_stringlist_accum
, validate_tracklength
},
1002 { C(transform
), &type_transform
, validate_any
},
1003 { C(trust
), &type_string_accum
, validate_any
},
1004 { C(url
), &type_string
, validate_url
},
1005 { C(user
), &type_string
, validate_isauser
},
1006 { C(username
), &type_string
, validate_any
},
1009 /** @brief Find a configuration item's definition by key */
1010 static const struct conf
*find(const char *key
) {
1013 if((n
= TABLE_FIND(conf
, name
, key
)) < 0)
1018 /** @brief Set a new configuration value */
1019 static int config_set(const struct config_state
*cs
,
1020 int nvec
, char **vec
) {
1021 const struct conf
*which
;
1023 D(("config_set %s", vec
[0]));
1024 if(!(which
= find(vec
[0]))) {
1025 error(0, "%s:%d: unknown configuration key '%s'",
1026 cs
->path
, cs
->line
, vec
[0]);
1029 return (which
->validate(cs
, nvec
- 1, vec
+ 1)
1030 || which
->type
->set(cs
, which
, nvec
- 1, vec
+ 1));
1033 static int config_set_args(const struct config_state
*cs
,
1034 const char *which
, ...) {
1040 vector_append(v
, (char *)which
);
1041 va_start(ap
, which
);
1042 while((s
= va_arg(ap
, char *)))
1043 vector_append(v
, s
);
1045 vector_terminate(v
);
1046 return config_set(cs
, v
->nvec
, v
->vec
);
1049 /** @brief Error callback used by config_include() */
1050 static void config_error(const char *msg
, void *u
) {
1051 const struct config_state
*cs
= u
;
1053 error(0, "%s:%d: %s", cs
->path
, cs
->line
, msg
);
1056 /** @brief Include a file by name */
1057 static int config_include(struct config
*c
, const char *path
) {
1059 char *buffer
, *inputbuffer
, **vec
;
1061 struct config_state cs
;
1066 D(("%s: reading configuration", path
));
1067 if(!(fp
= fopen(path
, "r"))) {
1068 error(errno
, "error opening %s", path
);
1071 while(!inputline(path
, fp
, &inputbuffer
, '\n')) {
1073 if(!(buffer
= mb2utf8(inputbuffer
))) {
1074 error(errno
, "%s:%d: cannot convert to UTF-8", cs
.path
, cs
.line
);
1080 if(!(vec
= split(buffer
, &n
, SPLIT_COMMENTS
|SPLIT_QUOTES
,
1081 config_error
, &cs
))) {
1087 if(!strcmp(vec
[0], "include")) {
1089 error(0, "%s:%d: must be 'include PATH'", cs
.path
, cs
.line
);
1092 config_include(c
, vec
[1]);
1094 ret
|= config_set(&cs
, n
, vec
);
1096 for(n
= 0; vec
[n
]; ++n
) xfree(vec
[n
]);
1101 error(errno
, "error reading %s", path
);
1108 static const char *const default_stopwords
[] = {
1167 #define NDEFAULT_STOPWORDS (sizeof default_stopwords / sizeof *default_stopwords)
1169 static const char *const default_players
[] = {
1175 #define NDEFAULT_PLAYERS (sizeof default_players / sizeof *default_players)
1177 /** @brief Make a new default configuration */
1178 static struct config
*config_default(void) {
1179 struct config
*c
= xmalloc(sizeof *c
);
1180 const char *logname
;
1182 struct config_state cs
;
1185 cs
.path
= "<internal>";
1188 /* Strings had better be xstrdup'd as they will get freed at some point. */
1191 c
->home
= xstrdup(pkgstatedir
);
1192 if(!(pw
= getpwuid(getuid())))
1193 fatal(0, "cannot determine our username");
1194 logname
= pw
->pw_name
;
1195 c
->username
= xstrdup(logname
);
1198 c
->signal
= SIGKILL
;
1199 c
->alias
= xstrdup("{/artist}{/album}{/title}{ext}");
1201 c
->device
= xstrdup("default");
1202 c
->nice_rescan
= 10;
1203 c
->speaker_command
= 0;
1204 c
->sample_format
.bits
= 16;
1205 c
->sample_format
.rate
= 44100;
1206 c
->sample_format
.channels
= 2;
1207 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1209 c
->replay_min
= 8 * 3600;
1211 c
->multicast_ttl
= 1;
1212 c
->multicast_loop
= 1;
1213 c
->authorization_algorithm
= xstrdup("sha1");
1214 c
->noticed_history
= 31;
1215 c
->short_display
= 32;
1219 c
->cookie_login_lifetime
= 86400;
1220 c
->cookie_key_lifetime
= 86400 * 7;
1221 if(sendmail_binary
[0] && strcmp(sendmail_binary
, "none"))
1222 c
->sendmail
= xstrdup(sendmail_binary
);
1223 c
->smtp_server
= xstrdup("127.0.0.1");
1225 c
->reminder_interval
= 600; /* 10m */
1226 c
->new_bias_age
= 7 * 86400; /* 1 week */
1227 c
->new_bias
= 4500000; /* 50 times the base weight */
1228 c
->sox_generation
= DEFAULT_SOX_GENERATION
;
1229 /* Default stopwords */
1230 if(config_set(&cs
, (int)NDEFAULT_STOPWORDS
, (char **)default_stopwords
))
1232 /* Default player configuration */
1233 for(n
= 0; n
< NDEFAULT_PLAYERS
; ++n
) {
1234 if(config_set_args(&cs
, "player",
1235 default_players
[n
], "execraw", "disorder-decode", (char *)0))
1237 if(config_set_args(&cs
, "tracklength",
1238 default_players
[n
], "disorder-tracklength", (char *)0))
1241 c
->broadcast
.af
= -1;
1242 c
->broadcast_from
.af
= -1;
1246 char *config_get_file2(struct config
*c
, const char *name
) {
1249 byte_xasprintf(&s
, "%s/%s", c
->home
, name
);
1253 /** @brief Set the default configuration file */
1254 static void set_configfile(void) {
1256 byte_xasprintf(&configfile
, "%s/config", pkgconfdir
);
1259 /** @brief Free a configuration object */
1260 static void config_free(struct config
*c
) {
1264 for(n
= 0; n
< (int)(sizeof conf
/ sizeof *conf
); ++n
)
1265 conf
[n
].type
->free(c
, &conf
[n
]);
1266 for(n
= 0; n
< c
->nparts
; ++n
)
1273 /** @brief Set post-parse defaults */
1274 static void config_postdefaults(struct config
*c
,
1276 struct config_state cs
;
1277 const struct conf
*whoami
;
1280 static const char *namepart
[][4] = {
1281 { "title", "/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
1282 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1283 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1284 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1285 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1287 #define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1289 static const char *transform
[][5] = {
1290 { "track", "^.*/([0-9]+ *[-:]? *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
1291 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1292 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1293 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1294 { "dir", "[[:punct:]]", "", "sort", "g", }
1296 #define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1298 cs
.path
= "<internal>";
1301 if(!c
->namepart
.n
) {
1302 whoami
= find("namepart");
1303 for(n
= 0; n
< NNAMEPART
; ++n
)
1304 set_namepart(&cs
, whoami
, 4, (char **)namepart
[n
]);
1306 if(!c
->transform
.n
) {
1307 whoami
= find("transform");
1308 for(n
= 0; n
< NTRANSFORM
; ++n
)
1309 set_transform(&cs
, whoami
, 5, (char **)transform
[n
]);
1312 if(c
->speaker_command
)
1313 c
->api
= xstrdup("command");
1314 else if(c
->broadcast
.af
!= -1)
1315 c
->api
= xstrdup("rtp");
1316 else if(config_uaudio_apis
)
1317 c
->api
= xstrdup(config_uaudio_apis
[0]->name
);
1319 c
->api
= xstrdup("<none>");
1321 if(!strcmp(c
->api
, "network"))
1322 c
->api
= xstrdup("rtp");
1324 if(!strcmp(c
->api
, "command") && !c
->speaker_command
)
1325 fatal(0, "'api command' but speaker_command is not set");
1326 if((!strcmp(c
->api
, "rtp")) && c
->broadcast
.af
== -1)
1327 fatal(0, "'api rtp' but broadcast is not set");
1329 /* Override sample format */
1330 if(!strcmp(c
->api
, "rtp")) {
1331 c
->sample_format
.rate
= 44100;
1332 c
->sample_format
.channels
= 2;
1333 c
->sample_format
.bits
= 16;
1334 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1336 if(!strcmp(c
->api
, "coreaudio")) {
1337 c
->sample_format
.rate
= 44100;
1338 c
->sample_format
.channels
= 2;
1339 c
->sample_format
.bits
= 16;
1340 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1342 if(!c
->default_rights
) {
1343 rights_type r
= RIGHTS__MASK
& ~(RIGHT_ADMIN
|RIGHT_REGISTER
1345 |RIGHT_SCRATCH__MASK
1346 |RIGHT_REMOVE__MASK
);
1347 /* The idea is to approximate the meaning of the old 'restrict' directive
1348 * in the default rights if they are not overridden. */
1349 if(c
->restrictions
& RESTRICT_SCRATCH
)
1350 r
|= RIGHT_SCRATCH_MINE
|RIGHT_SCRATCH_RANDOM
;
1352 r
|= RIGHT_SCRATCH_ANY
;
1353 if(!(c
->restrictions
& RESTRICT_MOVE
))
1354 r
|= RIGHT_MOVE_ANY
;
1355 if(c
->restrictions
& RESTRICT_REMOVE
)
1356 r
|= RIGHT_REMOVE_MINE
;
1358 r
|= RIGHT_REMOVE_ANY
;
1359 c
->default_rights
= rights_string(r
);
1363 /** @brief (Re-)read the config file
1364 * @param server If set, do extra checking
1366 int config_read(int server
) {
1372 c
= config_default();
1373 /* standalone Disobedience installs might not have a global config file */
1374 if(access(configfile
, F_OK
) == 0)
1375 if(config_include(c
, configfile
))
1377 /* if we can read the private config file, do */
1378 if((privconf
= config_private())
1379 && access(privconf
, R_OK
) == 0
1380 && config_include(c
, privconf
))
1383 /* if there's a per-user system config file for this user, read it */
1384 if(config_per_user
) {
1385 if(!(pw
= getpwuid(getuid())))
1386 fatal(0, "cannot determine our username");
1387 if((privconf
= config_usersysconf(pw
))
1388 && access(privconf
, F_OK
) == 0
1389 && config_include(c
, privconf
))
1392 /* if we have a password file, read it */
1393 if((privconf
= config_userconf(0, pw
))
1394 && access(privconf
, F_OK
) == 0
1395 && config_include(c
, privconf
))
1399 /* install default namepart and transform settings */
1400 config_postdefaults(c
, server
);
1401 /* everything is good so we shall use the new config */
1402 config_free(config
);
1403 /* warn about obsolete directives */
1405 error(0, "'restrict' will be removed in a future version");
1407 error(0, "'allow' will be removed in a future version");
1409 error(0, "'trust' will be removed in a future version");
1414 /** @brief Return the path to the private configuration file */
1415 char *config_private(void) {
1419 byte_xasprintf(&s
, "%s.private", configfile
);
1423 /** @brief Return the path to user's personal configuration file */
1424 char *config_userconf(const char *home
, const struct passwd
*pw
) {
1427 if(!home
&& !pw
&& !(pw
= getpwuid(getuid())))
1428 fatal(0, "cannot determine our username");
1429 byte_xasprintf(&s
, "%s/.disorder/passwd", home ? home
: pw
->pw_dir
);
1433 /** @brief Return the path to user-specific system configuration */
1434 char *config_usersysconf(const struct passwd
*pw
) {
1438 if(!strchr(pw
->pw_name
, '/')) {
1439 byte_xasprintf(&s
, "%s.%s", configfile
, pw
->pw_name
);
1445 char *config_get_file(const char *name
) {
1446 return config_get_file2(config
, name
);