2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 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 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 /** @file lib/configuration.c
22 * @brief Configuration file support
32 #include <sys/types.h>
43 #include "configuration.h"
49 #include "inputline.h"
59 /** @brief Path to config file
61 * set_configfile() sets the deafult if it is null.
65 /** @brief Read user configuration
67 * If clear, the user-specific configuration is not read.
69 int config_per_user
= 1;
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
, struct restriction
, 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_backend(const struct config_state
*cs
,
476 const struct conf
*whoami
,
477 int nvec
, char **vec
) {
478 int *const valuep
= ADDRESS(cs
->config
, int);
481 error(0, "%s:%d: '%s' requires one argument",
482 cs
->path
, cs
->line
, whoami
->name
);
485 if(!strcmp(vec
[0], "alsa")) {
486 #if HAVE_ALSA_ASOUNDLIB_H
487 *valuep
= BACKEND_ALSA
;
489 error(0, "%s:%d: ALSA is not available on this platform",
493 } else if(!strcmp(vec
[0], "command"))
494 *valuep
= BACKEND_COMMAND
;
495 else if(!strcmp(vec
[0], "network"))
496 *valuep
= BACKEND_NETWORK
;
497 else if(!strcmp(vec
[0], "coreaudio")) {
498 #if HAVE_COREAUDIO_AUDIOHARDWARE_H
499 *valuep
= BACKEND_COREAUDIO
;
501 error(0, "%s:%d: Core Audio is not available on this platform",
505 } else if(!strcmp(vec
[0], "oss")) {
506 #if HAVE_SYS_SOUNDCARD_H
507 *valuep
= BACKEND_OSS
;
509 error(0, "%s:%d: OSS is not available on this platform",
514 error(0, "%s:%d: invalid '%s' value '%s'",
515 cs
->path
, cs
->line
, whoami
->name
, vec
[0]);
521 static int set_rights(const struct config_state
*cs
,
522 const struct conf
*whoami
,
523 int nvec
, char **vec
) {
525 error(0, "%s:%d: '%s' requires one argument",
526 cs
->path
, cs
->line
, whoami
->name
);
529 if(parse_rights(vec
[0], 0, 1)) {
530 error(0, "%s:%d: invalid rights string '%s'",
531 cs
->path
, cs
->line
, vec
[0]);
534 *ADDRESS(cs
->config
, char *) = vec
[0];
540 static void free_none(struct config
attribute((unused
)) *c
,
541 const struct conf
attribute((unused
)) *whoami
) {
544 static void free_string(struct config
*c
,
545 const struct conf
*whoami
) {
546 xfree(VALUE(c
, char *));
549 static void free_stringlist(struct config
*c
,
550 const struct conf
*whoami
) {
552 struct stringlist
*sl
= ADDRESS(c
, struct stringlist
);
554 for(n
= 0; n
< sl
->n
; ++n
)
559 static void free_stringlistlist(struct config
*c
,
560 const struct conf
*whoami
) {
562 struct stringlistlist
*sll
= ADDRESS(c
, struct stringlistlist
);
563 struct stringlist
*sl
;
565 for(n
= 0; n
< sll
->n
; ++n
) {
567 for(m
= 0; m
< sl
->n
; ++m
)
574 static void free_collectionlist(struct config
*c
,
575 const struct conf
*whoami
) {
576 struct collectionlist
*cll
= ADDRESS(c
, struct collectionlist
);
577 struct collection
*cl
;
580 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. */
605 static void free_transformlist(struct config
*c
,
606 const struct conf
*whoami
) {
607 struct transformlist
*tl
= ADDRESS(c
, struct transformlist
);
611 for(n
= 0; n
< tl
->n
; ++n
) {
614 pcre_free(t
->re
); /* ...whatever pcre_free is set to. */
621 /* configuration types */
623 static const struct conftype
624 type_signal
= { set_signal
, free_none
},
625 type_collections
= { set_collections
, free_collectionlist
},
626 type_boolean
= { set_boolean
, free_none
},
627 type_string
= { set_string
, free_string
},
628 type_stringlist
= { set_stringlist
, free_stringlist
},
629 type_integer
= { set_integer
, free_none
},
630 type_stringlist_accum
= { set_stringlist_accum
, free_stringlistlist
},
631 type_string_accum
= { set_string_accum
, free_stringlist
},
632 type_sample_format
= { set_sample_format
, free_none
},
633 type_restrict
= { set_restrict
, free_none
},
634 type_namepart
= { set_namepart
, free_namepartlist
},
635 type_transform
= { set_transform
, free_transformlist
},
636 type_rights
= { set_rights
, free_none
},
637 type_backend
= { set_backend
, free_none
};
639 /* specific validation routine */
641 #define VALIDATE_FILE(test, what) do { \
645 for(n = 0; n < nvec; ++n) { \
646 if(stat(vec[n], &sb) < 0) { \
647 error(errno, "%s:%d: %s", cs->path, cs->line, vec[n]); \
650 if(!test(sb.st_mode)) { \
651 error(0, "%s:%d: %s is not a %s", \
652 cs->path, cs->line, vec[n], what); \
658 static int validate_isabspath(const struct config_state
*cs
,
659 int nvec
, char **vec
) {
662 for(n
= 0; n
< nvec
; ++n
)
663 if(vec
[n
][0] != '/') {
664 error(errno
, "%s:%d: %s: not an absolute path",
665 cs
->path
, cs
->line
, vec
[n
]);
671 static int validate_isdir(const struct config_state
*cs
,
672 int nvec
, char **vec
) {
673 VALIDATE_FILE(S_ISDIR
, "directory");
677 static int validate_isreg(const struct config_state
*cs
,
678 int nvec
, char **vec
) {
679 VALIDATE_FILE(S_ISREG
, "regular file");
683 static int validate_player(const struct config_state
*cs
,
685 char attribute((unused
)) **vec
) {
687 error(0, "%s:%d: should be at least 'player PATTERN MODULE'",
694 static int validate_tracklength(const struct config_state
*cs
,
696 char attribute((unused
)) **vec
) {
698 error(0, "%s:%d: should be at least 'tracklength PATTERN MODULE'",
705 static int validate_allow(const struct config_state
*cs
,
707 char attribute((unused
)) **vec
) {
709 error(0, "%s:%d: must be 'allow NAME PASS'", cs
->path
, cs
->line
);
715 static int validate_non_negative(const struct config_state
*cs
,
716 int nvec
, char **vec
) {
720 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
724 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
727 if(xstrtol(&n
, vec
[0], 0, 0)) {
728 error(0, "%s:%d: %s", cs
->path
, cs
->line
, strerror(errno
));
732 error(0, "%s:%d: must not be negative", cs
->path
, cs
->line
);
738 static int validate_positive(const struct config_state
*cs
,
739 int nvec
, char **vec
) {
743 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
747 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
750 if(xstrtol(&n
, vec
[0], 0, 0)) {
751 error(0, "%s:%d: %s", cs
->path
, cs
->line
, strerror(errno
));
755 error(0, "%s:%d: must be positive", cs
->path
, cs
->line
);
761 static int validate_isauser(const struct config_state
*cs
,
762 int attribute((unused
)) nvec
,
766 if(!(pw
= getpwnam(vec
[0]))) {
767 error(0, "%s:%d: no such user as '%s'", cs
->path
, cs
->line
, vec
[0]);
773 static int validate_sample_format(const struct config_state
*cs
,
774 int attribute((unused
)) nvec
,
776 return parse_sample_format(cs
, 0, nvec
, vec
);
779 static int validate_any(const struct config_state
attribute((unused
)) *cs
,
780 int attribute((unused
)) nvec
,
781 char attribute((unused
)) **vec
) {
785 static int validate_url(const struct config_state
attribute((unused
)) *cs
,
786 int attribute((unused
)) nvec
,
790 /* absoluteURI = scheme ":" ( hier_part | opaque_part )
791 scheme = alpha *( alpha | digit | "+" | "-" | "." ) */
793 n
= strspn(s
, ("abcdefghijklmnopqrstuvwxyz"
794 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
797 error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
800 if(!strncmp(s
, "http:", 5)
801 || !strncmp(s
, "https:", 6)) {
803 /* we only do a rather cursory check */
804 if(strncmp(s
, "//", 2)) {
805 error(0, "%s:%d: invalid url '%s'", cs
->path
, cs
->line
, vec
[0]);
812 static int validate_alias(const struct config_state
*cs
,
816 int in_brackets
= 0, c
;
819 error(0, "%s:%d: missing argument", cs
->path
, cs
->line
);
823 error(0, "%s:%d: too many arguments", cs
->path
, cs
->line
);
827 while((c
= (unsigned char)*s
++)) {
831 else if(!isalnum(c
)) {
832 error(0, "%s:%d: invalid part name in alias expansion in '%s'",
833 cs
->path
, cs
->line
, vec
[0]);
841 } else if(c
== '\\') {
842 if(!(c
= (unsigned char)*s
++)) {
843 error(0, "%s:%d: unterminated escape in alias expansion in '%s'",
844 cs
->path
, cs
->line
, vec
[0]);
846 } else if(c
!= '\\' && c
!= '{') {
847 error(0, "%s:%d: invalid escape in alias expansion in '%s'",
848 cs
->path
, cs
->line
, vec
[0]);
856 error(0, "%s:%d: unterminated part name in alias expansion in '%s'",
857 cs
->path
, cs
->line
, vec
[0]);
863 static int validate_addrport(const struct config_state
attribute((unused
)) *cs
,
865 char attribute((unused
)) **vec
) {
868 error(0, "%s:%d: missing address",
872 error(0, "%s:%d: missing port name/number",
878 error(0, "%s:%d: expected ADDRESS PORT",
884 static int validate_port(const struct config_state
attribute((unused
)) *cs
,
886 char attribute((unused
)) **vec
) {
889 error(0, "%s:%d: missing address",
896 error(0, "%s:%d: expected [ADDRESS] PORT",
902 static int validate_algo(const struct config_state
attribute((unused
)) *cs
,
906 error(0, "%s:%d: invalid algorithm specification", cs
->path
, cs
->line
);
909 if(!valid_authhash(vec
[0])) {
910 error(0, "%s:%d: unsuported algorithm '%s'", cs
->path
, cs
->line
, vec
[0]);
916 /** @brief Item name and and offset */
917 #define C(x) #x, offsetof(struct config, x)
918 /** @brief Item name and and offset */
919 #define C2(x,y) #x, offsetof(struct config, y)
921 /** @brief All configuration items */
922 static const struct conf conf
[] = {
923 { C(alias
), &type_string
, validate_alias
},
924 { C(allow
), &type_stringlist_accum
, validate_allow
},
925 { C(api
), &type_backend
, validate_any
},
926 { C(authorization_algorithm
), &type_string
, validate_algo
},
927 { C(broadcast
), &type_stringlist
, validate_addrport
},
928 { C(broadcast_from
), &type_stringlist
, validate_addrport
},
929 { C(channel
), &type_string
, validate_any
},
930 { C(checkpoint_kbyte
), &type_integer
, validate_non_negative
},
931 { C(checkpoint_min
), &type_integer
, validate_non_negative
},
932 { C(collection
), &type_collections
, validate_any
},
933 { C(connect
), &type_stringlist
, validate_addrport
},
934 { C(cookie_login_lifetime
), &type_integer
, validate_positive
},
935 { C(cookie_key_lifetime
), &type_integer
, validate_positive
},
936 { C(dbversion
), &type_integer
, validate_positive
},
937 { C(default_rights
), &type_rights
, validate_any
},
938 { C(device
), &type_string
, validate_any
},
939 { C(gap
), &type_integer
, validate_non_negative
},
940 { C(history
), &type_integer
, validate_positive
},
941 { C(home
), &type_string
, validate_isabspath
},
942 { C(listen
), &type_stringlist
, validate_port
},
943 { C(lock
), &type_boolean
, validate_any
},
944 { C(mail_sender
), &type_string
, validate_any
},
945 { C(mixer
), &type_string
, validate_any
},
946 { C(multicast_loop
), &type_boolean
, validate_any
},
947 { C(multicast_ttl
), &type_integer
, validate_non_negative
},
948 { C(namepart
), &type_namepart
, validate_any
},
949 { C(new_max
), &type_integer
, validate_positive
},
950 { C2(nice
, nice_rescan
), &type_integer
, validate_non_negative
},
951 { C(nice_rescan
), &type_integer
, validate_non_negative
},
952 { C(nice_server
), &type_integer
, validate_any
},
953 { C(nice_speaker
), &type_integer
, validate_any
},
954 { C(noticed_history
), &type_integer
, validate_positive
},
955 { C(password
), &type_string
, validate_any
},
956 { C(player
), &type_stringlist_accum
, validate_player
},
957 { C(plugins
), &type_string_accum
, validate_isdir
},
958 { C(prefsync
), &type_integer
, validate_positive
},
959 { C(queue_pad
), &type_integer
, validate_positive
},
960 { C(replay_min
), &type_integer
, validate_non_negative
},
961 { C(refresh
), &type_integer
, validate_positive
},
962 { C(reminder_interval
), &type_integer
, validate_positive
},
963 { C(remote_userman
), &type_boolean
, validate_any
},
964 { C2(restrict
, restrictions
), &type_restrict
, validate_any
},
965 { C(sample_format
), &type_sample_format
, validate_sample_format
},
966 { C(scratch
), &type_string_accum
, validate_isreg
},
967 { C(short_display
), &type_integer
, validate_positive
},
968 { C(signal
), &type_signal
, validate_any
},
969 { C(smtp_server
), &type_string
, validate_any
},
970 { C(sox_generation
), &type_integer
, validate_non_negative
},
971 { C2(speaker_backend
, api
), &type_backend
, validate_any
},
972 { C(speaker_command
), &type_string
, validate_any
},
973 { C(stopword
), &type_string_accum
, validate_any
},
974 { C(templates
), &type_string_accum
, validate_isdir
},
975 { C(tracklength
), &type_stringlist_accum
, validate_tracklength
},
976 { C(transform
), &type_transform
, validate_any
},
977 { C(trust
), &type_string_accum
, validate_any
},
978 { C(url
), &type_string
, validate_url
},
979 { C(user
), &type_string
, validate_isauser
},
980 { C(username
), &type_string
, validate_any
},
983 /** @brief Find a configuration item's definition by key */
984 static const struct conf
*find(const char *key
) {
987 if((n
= TABLE_FIND(conf
, struct conf
, name
, key
)) < 0)
992 /** @brief Set a new configuration value */
993 static int config_set(const struct config_state
*cs
,
994 int nvec
, char **vec
) {
995 const struct conf
*which
;
997 D(("config_set %s", vec
[0]));
998 if(!(which
= find(vec
[0]))) {
999 error(0, "%s:%d: unknown configuration key '%s'",
1000 cs
->path
, cs
->line
, vec
[0]);
1003 return (which
->validate(cs
, nvec
- 1, vec
+ 1)
1004 || which
->type
->set(cs
, which
, nvec
- 1, vec
+ 1));
1007 static int config_set_args(const struct config_state
*cs
,
1008 const char *which
, ...) {
1014 vector_append(v
, (char *)which
);
1015 va_start(ap
, which
);
1016 while((s
= va_arg(ap
, char *)))
1017 vector_append(v
, s
);
1019 vector_terminate(v
);
1020 return config_set(cs
, v
->nvec
, v
->vec
);
1023 /** @brief Error callback used by config_include() */
1024 static void config_error(const char *msg
, void *u
) {
1025 const struct config_state
*cs
= u
;
1027 error(0, "%s:%d: %s", cs
->path
, cs
->line
, msg
);
1030 /** @brief Include a file by name */
1031 static int config_include(struct config
*c
, const char *path
) {
1033 char *buffer
, *inputbuffer
, **vec
;
1035 struct config_state cs
;
1040 D(("%s: reading configuration", path
));
1041 if(!(fp
= fopen(path
, "r"))) {
1042 error(errno
, "error opening %s", path
);
1045 while(!inputline(path
, fp
, &inputbuffer
, '\n')) {
1047 if(!(buffer
= mb2utf8(inputbuffer
))) {
1048 error(errno
, "%s:%d: cannot convert to UTF-8", cs
.path
, cs
.line
);
1054 if(!(vec
= split(buffer
, &n
, SPLIT_COMMENTS
|SPLIT_QUOTES
,
1055 config_error
, &cs
))) {
1061 if(!strcmp(vec
[0], "include")) {
1063 error(0, "%s:%d: must be 'include PATH'", cs
.path
, cs
.line
);
1066 config_include(c
, vec
[1]);
1068 ret
|= config_set(&cs
, n
, vec
);
1070 for(n
= 0; vec
[n
]; ++n
) xfree(vec
[n
]);
1075 error(errno
, "error reading %s", path
);
1082 static const char *const default_stopwords
[] = {
1141 #define NDEFAULT_STOPWORDS (sizeof default_stopwords / sizeof *default_stopwords)
1143 static const char *const default_players
[] = {
1149 #define NDEFAULT_PLAYERS (sizeof default_players / sizeof *default_players)
1151 /** @brief Make a new default configuration */
1152 static struct config
*config_default(void) {
1153 struct config
*c
= xmalloc(sizeof *c
);
1154 const char *logname
;
1156 struct config_state cs
;
1159 cs
.path
= "<internal>";
1162 /* Strings had better be xstrdup'd as they will get freed at some point. */
1165 c
->home
= xstrdup(pkgstatedir
);
1166 if(!(pw
= getpwuid(getuid())))
1167 fatal(0, "cannot determine our username");
1168 logname
= pw
->pw_name
;
1169 c
->username
= xstrdup(logname
);
1172 c
->signal
= SIGKILL
;
1173 c
->alias
= xstrdup("{/artist}{/album}{/title}{ext}");
1175 c
->device
= xstrdup("default");
1176 c
->nice_rescan
= 10;
1177 c
->speaker_command
= 0;
1178 c
->sample_format
.bits
= 16;
1179 c
->sample_format
.rate
= 44100;
1180 c
->sample_format
.channels
= 2;
1181 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1183 c
->replay_min
= 8 * 3600;
1185 c
->multicast_ttl
= 1;
1186 c
->multicast_loop
= 1;
1187 c
->authorization_algorithm
= xstrdup("sha1");
1188 c
->noticed_history
= 31;
1189 c
->short_display
= 32;
1193 c
->cookie_login_lifetime
= 86400;
1194 c
->cookie_key_lifetime
= 86400 * 7;
1195 c
->smtp_server
= xstrdup("127.0.0.1");
1197 c
->reminder_interval
= 600; /* 10m */
1198 /* Default stopwords */
1199 if(config_set(&cs
, (int)NDEFAULT_STOPWORDS
, (char **)default_stopwords
))
1201 /* Default player configuration */
1202 for(n
= 0; n
< NDEFAULT_PLAYERS
; ++n
) {
1203 if(config_set_args(&cs
, "player",
1204 default_players
[n
], "execraw", "disorder-decode", (char *)0))
1206 if(config_set_args(&cs
, "tracklength",
1207 default_players
[n
], "disorder-tracklength", (char *)0))
1213 static char *get_file(struct config
*c
, const char *name
) {
1216 byte_xasprintf(&s
, "%s/%s", c
->home
, name
);
1220 /** @brief Set the default configuration file */
1221 static void set_configfile(void) {
1223 byte_xasprintf(&configfile
, "%s/config", pkgconfdir
);
1226 /** @brief Free a configuration object */
1227 static void config_free(struct config
*c
) {
1231 for(n
= 0; n
< (int)(sizeof conf
/ sizeof *conf
); ++n
)
1232 conf
[n
].type
->free(c
, &conf
[n
]);
1233 for(n
= 0; n
< c
->nparts
; ++n
)
1240 /** @brief Set post-parse defaults */
1241 static void config_postdefaults(struct config
*c
,
1243 struct config_state cs
;
1244 const struct conf
*whoami
;
1247 static const char *namepart
[][4] = {
1248 { "title", "/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" },
1249 { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" },
1250 { "album", "/([^/]+)/[^/]+$", "$1", "*" },
1251 { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" },
1252 { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" },
1254 #define NNAMEPART (int)(sizeof namepart / sizeof *namepart)
1256 static const char *transform
[][5] = {
1257 { "track", "^.*/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" },
1258 { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" },
1259 { "dir", "^.*/([^/]+)$", "$1", "*", "" },
1260 { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", },
1261 { "dir", "[[:punct:]]", "", "sort", "g", }
1263 #define NTRANSFORM (int)(sizeof transform / sizeof *transform)
1265 cs
.path
= "<internal>";
1268 if(!c
->namepart
.n
) {
1269 whoami
= find("namepart");
1270 for(n
= 0; n
< NNAMEPART
; ++n
)
1271 set_namepart(&cs
, whoami
, 4, (char **)namepart
[n
]);
1273 if(!c
->transform
.n
) {
1274 whoami
= find("transform");
1275 for(n
= 0; n
< NTRANSFORM
; ++n
)
1276 set_transform(&cs
, whoami
, 5, (char **)transform
[n
]);
1279 if(c
->speaker_command
)
1280 c
->api
= BACKEND_COMMAND
;
1281 else if(c
->broadcast
.n
)
1282 c
->api
= BACKEND_NETWORK
;
1284 c
->api
= DEFAULT_BACKEND
;
1287 if(c
->api
== BACKEND_COMMAND
&& !c
->speaker_command
)
1288 fatal(0, "'api command' but speaker_command is not set");
1289 if(c
->api
== BACKEND_NETWORK
&& !c
->broadcast
.n
)
1290 fatal(0, "'api network' but broadcast is not set");
1292 /* Override sample format */
1294 case BACKEND_NETWORK
:
1295 c
->sample_format
.rate
= 44100;
1296 c
->sample_format
.channels
= 2;
1297 c
->sample_format
.bits
= 16;
1298 c
->sample_format
.endian
= ENDIAN_BIG
;
1300 case BACKEND_COREAUDIO
:
1301 c
->sample_format
.rate
= 44100;
1302 c
->sample_format
.channels
= 2;
1303 c
->sample_format
.bits
= 16;
1304 c
->sample_format
.endian
= ENDIAN_NATIVE
;
1307 if(!c
->default_rights
) {
1308 rights_type r
= RIGHTS__MASK
& ~(RIGHT_ADMIN
|RIGHT_REGISTER
1310 |RIGHT_SCRATCH__MASK
1311 |RIGHT_REMOVE__MASK
);
1312 /* The idea is to approximate the meaning of the old 'restrict' directive
1313 * in the default rights if they are not overridden. */
1314 if(c
->restrictions
& RESTRICT_SCRATCH
)
1315 r
|= RIGHT_SCRATCH_MINE
|RIGHT_SCRATCH_RANDOM
;
1317 r
|= RIGHT_SCRATCH_ANY
;
1318 if(!(c
->restrictions
& RESTRICT_MOVE
))
1319 r
|= RIGHT_MOVE_ANY
;
1320 if(c
->restrictions
& RESTRICT_REMOVE
)
1321 r
|= RIGHT_REMOVE_MINE
;
1323 r
|= RIGHT_REMOVE_ANY
;
1324 c
->default_rights
= rights_string(r
);
1328 /** @brief (Re-)read the config file
1329 * @param server If set, do extra checking
1331 int config_read(int server
) {
1337 c
= config_default();
1338 /* standalone Disobedience installs might not have a global config file */
1339 if(access(configfile
, F_OK
) == 0)
1340 if(config_include(c
, configfile
))
1342 /* if we can read the private config file, do */
1343 if((privconf
= config_private())
1344 && access(privconf
, R_OK
) == 0
1345 && config_include(c
, privconf
))
1348 /* if there's a per-user system config file for this user, read it */
1349 if(config_per_user
) {
1350 if(!(pw
= getpwuid(getuid())))
1351 fatal(0, "cannot determine our username");
1352 if((privconf
= config_usersysconf(pw
))
1353 && access(privconf
, F_OK
) == 0
1354 && config_include(c
, privconf
))
1357 /* if we have a password file, read it */
1358 if((privconf
= config_userconf(0, pw
))
1359 && access(privconf
, F_OK
) == 0
1360 && config_include(c
, privconf
))
1364 /* install default namepart and transform settings */
1365 config_postdefaults(c
, server
);
1366 /* everything is good so we shall use the new config */
1367 config_free(config
);
1368 /* warn about obsolete directives */
1370 error(0, "'restrict' will be removed in a future version");
1372 error(0, "'allow' will be removed in a future version");
1374 error(0, "'trust' will be removed in a future version");
1379 /** @brief Return the path to the private configuration file */
1380 char *config_private(void) {
1384 byte_xasprintf(&s
, "%s.private", configfile
);
1388 /** @brief Return the path to user's personal configuration file */
1389 char *config_userconf(const char *home
, const struct passwd
*pw
) {
1392 if(!home
&& !pw
&& !(pw
= getpwuid(getuid())))
1393 fatal(0, "cannot determine our username");
1394 byte_xasprintf(&s
, "%s/.disorder/passwd", home ? home
: pw
->pw_dir
);
1398 /** @brief Return the path to user-specific system configuration */
1399 char *config_usersysconf(const struct passwd
*pw
) {
1403 if(!strchr(pw
->pw_name
, '/')) {
1404 byte_xasprintf(&s
, "%s.%s", configfile
, pw
->pw_name
);
1410 char *config_get_file(const char *name
) {
1411 return get_file(config
, name
);