Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
e83d0967 | 3 | * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell |
460b9539 | 4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
0e4472a0 | 20 | /** @file lib/configuration.c |
21 | * @brief Configuration file support | |
22 | */ | |
460b9539 | 23 | |
24 | #include <config.h> | |
25 | #include "types.h" | |
26 | ||
27 | #include <stdio.h> | |
28 | #include <string.h> | |
29 | #include <stdlib.h> | |
30 | #include <errno.h> | |
31 | #include <sys/types.h> | |
32 | #include <sys/stat.h> | |
33 | #include <unistd.h> | |
34 | #include <ctype.h> | |
35 | #include <stddef.h> | |
36 | #include <pwd.h> | |
37 | #include <langinfo.h> | |
38 | #include <pcre.h> | |
39 | #include <signal.h> | |
40 | ||
41 | #include "configuration.h" | |
42 | #include "mem.h" | |
43 | #include "log.h" | |
44 | #include "split.h" | |
45 | #include "syscalls.h" | |
46 | #include "table.h" | |
47 | #include "inputline.h" | |
48 | #include "charset.h" | |
49 | #include "defs.h" | |
50 | #include "mixer.h" | |
51 | #include "printf.h" | |
52 | #include "regsub.h" | |
53 | #include "signame.h" | |
637fdea3 | 54 | #include "authhash.h" |
460b9539 | 55 | |
3f3bb97b RK |
56 | /** @brief Path to config file |
57 | * | |
58 | * set_configfile() sets the deafult if it is null. | |
59 | */ | |
460b9539 | 60 | char *configfile; |
61 | ||
3f3bb97b | 62 | /** @brief Config file parser state */ |
460b9539 | 63 | struct config_state { |
3f3bb97b | 64 | /** @brief Filename */ |
460b9539 | 65 | const char *path; |
3f3bb97b | 66 | /** @brief Line number */ |
460b9539 | 67 | int line; |
3f3bb97b | 68 | /** @brief Configuration object under construction */ |
460b9539 | 69 | struct config *config; |
70 | }; | |
71 | ||
3f3bb97b | 72 | /** @brief Current configuration */ |
460b9539 | 73 | struct config *config; |
74 | ||
3f3bb97b | 75 | /** @brief One configuration item */ |
460b9539 | 76 | struct conf { |
3f3bb97b | 77 | /** @brief Name as it appears in the config file */ |
460b9539 | 78 | const char *name; |
3f3bb97b | 79 | /** @brief Offset in @ref config structure */ |
460b9539 | 80 | size_t offset; |
3f3bb97b | 81 | /** @brief Pointer to item type */ |
460b9539 | 82 | const struct conftype *type; |
3f3bb97b | 83 | /** @brief Pointer to item-specific validation routine */ |
460b9539 | 84 | int (*validate)(const struct config_state *cs, |
85 | int nvec, char **vec); | |
86 | }; | |
87 | ||
3f3bb97b | 88 | /** @brief Type of a configuration item */ |
460b9539 | 89 | struct conftype { |
3f3bb97b | 90 | /** @brief Pointer to function to set item */ |
460b9539 | 91 | int (*set)(const struct config_state *cs, |
92 | const struct conf *whoami, | |
93 | int nvec, char **vec); | |
3f3bb97b | 94 | /** @brief Pointer to function to free item */ |
460b9539 | 95 | void (*free)(struct config *c, const struct conf *whoami); |
96 | }; | |
97 | ||
3f3bb97b | 98 | /** @brief Compute the address of an item */ |
460b9539 | 99 | #define ADDRESS(C, TYPE) ((TYPE *)((char *)(C) + whoami->offset)) |
3f3bb97b | 100 | /** @brief Return the value of an item */ |
460b9539 | 101 | #define VALUE(C, TYPE) (*ADDRESS(C, TYPE)) |
102 | ||
103 | static int set_signal(const struct config_state *cs, | |
104 | const struct conf *whoami, | |
105 | int nvec, char **vec) { | |
106 | int n; | |
107 | ||
108 | if(nvec != 1) { | |
109 | error(0, "%s:%d: '%s' requires one argument", | |
110 | cs->path, cs->line, whoami->name); | |
111 | return -1; | |
112 | } | |
113 | if((n = find_signal(vec[0])) == -1) { | |
114 | error(0, "%s:%d: unknown signal '%s'", | |
115 | cs->path, cs->line, vec[0]); | |
116 | return -1; | |
117 | } | |
118 | VALUE(cs->config, int) = n; | |
119 | return 0; | |
120 | } | |
121 | ||
122 | static int set_collections(const struct config_state *cs, | |
123 | const struct conf *whoami, | |
124 | int nvec, char **vec) { | |
125 | struct collectionlist *cl; | |
126 | ||
127 | if(nvec != 3) { | |
128 | error(0, "%s:%d: '%s' requires three arguments", | |
129 | cs->path, cs->line, whoami->name); | |
130 | return -1; | |
131 | } | |
132 | if(vec[2][0] != '/') { | |
133 | error(0, "%s:%d: collection root must start with '/'", | |
134 | cs->path, cs->line); | |
135 | return -1; | |
136 | } | |
137 | if(vec[2][1] && vec[2][strlen(vec[2])-1] == '/') { | |
138 | error(0, "%s:%d: collection root must not end with '/'", | |
139 | cs->path, cs->line); | |
140 | return -1; | |
141 | } | |
142 | cl = ADDRESS(cs->config, struct collectionlist); | |
143 | ++cl->n; | |
144 | cl->s = xrealloc(cl->s, cl->n * sizeof (struct collection)); | |
145 | cl->s[cl->n - 1].module = xstrdup(vec[0]); | |
146 | cl->s[cl->n - 1].encoding = xstrdup(vec[1]); | |
147 | cl->s[cl->n - 1].root = xstrdup(vec[2]); | |
148 | return 0; | |
149 | } | |
150 | ||
151 | static int set_boolean(const struct config_state *cs, | |
152 | const struct conf *whoami, | |
153 | int nvec, char **vec) { | |
154 | int state; | |
155 | ||
156 | if(nvec != 1) { | |
157 | error(0, "%s:%d: '%s' takes only one argument", | |
158 | cs->path, cs->line, whoami->name); | |
159 | return -1; | |
160 | } | |
161 | if(!strcmp(vec[0], "yes")) state = 1; | |
162 | else if(!strcmp(vec[0], "no")) state = 0; | |
163 | else { | |
164 | error(0, "%s:%d: argument to '%s' must be 'yes' or 'no'", | |
165 | cs->path, cs->line, whoami->name); | |
166 | return -1; | |
167 | } | |
168 | VALUE(cs->config, int) = state; | |
169 | return 0; | |
170 | } | |
171 | ||
172 | static int set_string(const struct config_state *cs, | |
173 | const struct conf *whoami, | |
174 | int nvec, char **vec) { | |
175 | if(nvec != 1) { | |
176 | error(0, "%s:%d: '%s' takes only one argument", | |
177 | cs->path, cs->line, whoami->name); | |
178 | return -1; | |
179 | } | |
180 | VALUE(cs->config, char *) = xstrdup(vec[0]); | |
181 | return 0; | |
182 | } | |
183 | ||
184 | static int set_stringlist(const struct config_state *cs, | |
185 | const struct conf *whoami, | |
186 | int nvec, char **vec) { | |
187 | int n; | |
188 | struct stringlist *sl; | |
189 | ||
190 | sl = ADDRESS(cs->config, struct stringlist); | |
191 | sl->n = 0; | |
192 | for(n = 0; n < nvec; ++n) { | |
193 | sl->n++; | |
194 | sl->s = xrealloc(sl->s, (sl->n * sizeof (char *))); | |
195 | sl->s[sl->n - 1] = xstrdup(vec[n]); | |
196 | } | |
197 | return 0; | |
198 | } | |
199 | ||
200 | static int set_integer(const struct config_state *cs, | |
201 | const struct conf *whoami, | |
202 | int nvec, char **vec) { | |
203 | char *e; | |
204 | ||
205 | if(nvec != 1) { | |
206 | error(0, "%s:%d: '%s' takes only one argument", | |
207 | cs->path, cs->line, whoami->name); | |
208 | return -1; | |
209 | } | |
210 | if(xstrtol(ADDRESS(cs->config, long), vec[0], &e, 0)) { | |
211 | error(errno, "%s:%d: converting integer", cs->path, cs->line); | |
212 | return -1; | |
213 | } | |
214 | if(*e) { | |
215 | error(0, "%s:%d: invalid integer syntax", cs->path, cs->line); | |
216 | return -1; | |
217 | } | |
218 | return 0; | |
219 | } | |
220 | ||
221 | static int set_stringlist_accum(const struct config_state *cs, | |
222 | const struct conf *whoami, | |
223 | int nvec, char **vec) { | |
224 | int n; | |
225 | struct stringlist *s; | |
226 | struct stringlistlist *sll; | |
227 | ||
228 | sll = ADDRESS(cs->config, struct stringlistlist); | |
229 | sll->n++; | |
230 | sll->s = xrealloc(sll->s, (sll->n * sizeof (struct stringlist))); | |
231 | s = &sll->s[sll->n - 1]; | |
232 | s->n = nvec; | |
233 | s->s = xmalloc((nvec + 1) * sizeof (char *)); | |
234 | for(n = 0; n < nvec; ++n) | |
235 | s->s[n] = xstrdup(vec[n]); | |
236 | return 0; | |
237 | } | |
238 | ||
239 | static int set_string_accum(const struct config_state *cs, | |
240 | const struct conf *whoami, | |
241 | int nvec, char **vec) { | |
242 | int n; | |
243 | struct stringlist *sl; | |
244 | ||
245 | sl = ADDRESS(cs->config, struct stringlist); | |
246 | for(n = 0; n < nvec; ++n) { | |
247 | sl->n++; | |
248 | sl->s = xrealloc(sl->s, (sl->n * sizeof (char *))); | |
249 | sl->s[sl->n - 1] = xstrdup(vec[n]); | |
250 | } | |
251 | return 0; | |
252 | } | |
253 | ||
254 | static int set_restrict(const struct config_state *cs, | |
255 | const struct conf *whoami, | |
256 | int nvec, char **vec) { | |
257 | unsigned r = 0; | |
258 | int n, i; | |
259 | ||
260 | static const struct restriction { | |
261 | const char *name; | |
262 | unsigned bit; | |
263 | } restrictions[] = { | |
264 | { "remove", RESTRICT_REMOVE }, | |
265 | { "scratch", RESTRICT_SCRATCH }, | |
266 | { "move", RESTRICT_MOVE }, | |
267 | }; | |
268 | ||
269 | for(n = 0; n < nvec; ++n) { | |
270 | if((i = TABLE_FIND(restrictions, struct restriction, name, vec[n])) < 0) { | |
271 | error(0, "%s:%d: invalid restriction '%s'", | |
272 | cs->path, cs->line, vec[n]); | |
273 | return -1; | |
274 | } | |
275 | r |= restrictions[i].bit; | |
276 | } | |
277 | VALUE(cs->config, unsigned) = r; | |
278 | return 0; | |
279 | } | |
280 | ||
9d5da576 | 281 | static int parse_sample_format(const struct config_state *cs, |
6d2d327c | 282 | struct stream_header *format, |
9d5da576 | 283 | int nvec, char **vec) { |
284 | char *p = vec[0]; | |
285 | long t; | |
286 | ||
6d2d327c | 287 | if(nvec != 1) { |
9d5da576 | 288 | error(0, "%s:%d: wrong number of arguments", cs->path, cs->line); |
289 | return -1; | |
290 | } | |
6d2d327c | 291 | if(xstrtol(&t, p, &p, 0)) { |
9d5da576 | 292 | error(errno, "%s:%d: converting bits-per-sample", cs->path, cs->line); |
293 | return -1; | |
294 | } | |
6d2d327c | 295 | if(t != 8 && t != 16) { |
9d5da576 | 296 | error(0, "%s:%d: bad bite-per-sample (%ld)", cs->path, cs->line, t); |
297 | return -1; | |
298 | } | |
6d2d327c | 299 | if(format) format->bits = t; |
9d5da576 | 300 | switch (*p) { |
6d2d327c RK |
301 | case 'l': case 'L': t = ENDIAN_LITTLE; p++; break; |
302 | case 'b': case 'B': t = ENDIAN_BIG; p++; break; | |
303 | default: t = ENDIAN_NATIVE; break; | |
9d5da576 | 304 | } |
6d2d327c RK |
305 | if(format) format->endian = t; |
306 | if(*p != '/') { | |
9d5da576 | 307 | error(errno, "%s:%d: expected `/' after bits-per-sample", |
308 | cs->path, cs->line); | |
309 | return -1; | |
310 | } | |
311 | p++; | |
6d2d327c | 312 | if(xstrtol(&t, p, &p, 0)) { |
9d5da576 | 313 | error(errno, "%s:%d: converting sample-rate", cs->path, cs->line); |
314 | return -1; | |
315 | } | |
6d2d327c | 316 | if(t < 1 || t > INT_MAX) { |
9d5da576 | 317 | error(0, "%s:%d: silly sample-rate (%ld)", cs->path, cs->line, t); |
318 | return -1; | |
319 | } | |
6d2d327c RK |
320 | if(format) format->rate = t; |
321 | if(*p != '/') { | |
9d5da576 | 322 | error(0, "%s:%d: expected `/' after sample-rate", |
323 | cs->path, cs->line); | |
324 | return -1; | |
325 | } | |
326 | p++; | |
6d2d327c | 327 | if(xstrtol(&t, p, &p, 0)) { |
9d5da576 | 328 | error(errno, "%s:%d: converting channels", cs->path, cs->line); |
329 | return -1; | |
330 | } | |
6d2d327c | 331 | if(t < 1 || t > 8) { |
9d5da576 | 332 | error(0, "%s:%d: silly number (%ld) of channels", cs->path, cs->line, t); |
333 | return -1; | |
334 | } | |
6d2d327c RK |
335 | if(format) format->channels = t; |
336 | if(*p) { | |
9d5da576 | 337 | error(0, "%s:%d: junk after channels", cs->path, cs->line); |
338 | return -1; | |
339 | } | |
340 | return 0; | |
341 | } | |
342 | ||
343 | static int set_sample_format(const struct config_state *cs, | |
344 | const struct conf *whoami, | |
345 | int nvec, char **vec) { | |
6d2d327c | 346 | return parse_sample_format(cs, ADDRESS(cs->config, struct stream_header), |
9d5da576 | 347 | nvec, vec); |
348 | } | |
349 | ||
460b9539 | 350 | static int set_namepart(const struct config_state *cs, |
351 | const struct conf *whoami, | |
352 | int nvec, char **vec) { | |
353 | struct namepartlist *npl = ADDRESS(cs->config, struct namepartlist); | |
354 | unsigned reflags; | |
355 | const char *errstr; | |
356 | int erroffset, n; | |
357 | pcre *re; | |
358 | ||
359 | if(nvec < 3) { | |
360 | error(0, "%s:%d: namepart needs at least 3 arguments", cs->path, cs->line); | |
361 | return -1; | |
362 | } | |
363 | if(nvec > 5) { | |
364 | error(0, "%s:%d: namepart needs at most 5 arguments", cs->path, cs->line); | |
365 | return -1; | |
366 | } | |
367 | reflags = nvec >= 5 ? regsub_flags(vec[4]) : 0; | |
368 | if(!(re = pcre_compile(vec[1], | |
369 | PCRE_UTF8 | |
370 | |regsub_compile_options(reflags), | |
371 | &errstr, &erroffset, 0))) { | |
372 | error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)", | |
373 | cs->path, cs->line, vec[1], errstr, erroffset); | |
374 | return -1; | |
375 | } | |
376 | npl->s = xrealloc(npl->s, (npl->n + 1) * sizeof (struct namepart)); | |
377 | npl->s[npl->n].part = xstrdup(vec[0]); | |
378 | npl->s[npl->n].re = re; | |
379 | npl->s[npl->n].replace = xstrdup(vec[2]); | |
380 | npl->s[npl->n].context = xstrdup(vec[3]); | |
381 | npl->s[npl->n].reflags = reflags; | |
382 | ++npl->n; | |
383 | /* XXX a bit of a bodge; relies on there being very few parts. */ | |
384 | for(n = 0; (n < cs->config->nparts | |
385 | && strcmp(cs->config->parts[n], vec[0])); ++n) | |
386 | ; | |
387 | if(n >= cs->config->nparts) { | |
388 | cs->config->parts = xrealloc(cs->config->parts, | |
389 | (cs->config->nparts + 1) * sizeof (char *)); | |
390 | cs->config->parts[cs->config->nparts++] = xstrdup(vec[0]); | |
391 | } | |
392 | return 0; | |
393 | } | |
394 | ||
395 | static int set_transform(const struct config_state *cs, | |
396 | const struct conf *whoami, | |
397 | int nvec, char **vec) { | |
398 | struct transformlist *tl = ADDRESS(cs->config, struct transformlist); | |
399 | pcre *re; | |
400 | unsigned reflags; | |
401 | const char *errstr; | |
402 | int erroffset; | |
403 | ||
404 | if(nvec < 3) { | |
405 | error(0, "%s:%d: transform needs at least 3 arguments", cs->path, cs->line); | |
406 | return -1; | |
407 | } | |
408 | if(nvec > 5) { | |
409 | error(0, "%s:%d: transform needs at most 5 arguments", cs->path, cs->line); | |
410 | return -1; | |
411 | } | |
412 | reflags = (nvec >= 5 ? regsub_flags(vec[4]) : 0); | |
413 | if(!(re = pcre_compile(vec[1], | |
414 | PCRE_UTF8 | |
415 | |regsub_compile_options(reflags), | |
416 | &errstr, &erroffset, 0))) { | |
417 | error(0, "%s:%d: error compiling regexp /%s/: %s (offset %d)", | |
418 | cs->path, cs->line, vec[1], errstr, erroffset); | |
419 | return -1; | |
420 | } | |
421 | tl->t = xrealloc(tl->t, (tl->n + 1) * sizeof (struct namepart)); | |
422 | tl->t[tl->n].type = xstrdup(vec[0]); | |
423 | tl->t[tl->n].context = xstrdup(vec[3] ? vec[3] : "*"); | |
424 | tl->t[tl->n].re = re; | |
425 | tl->t[tl->n].replace = xstrdup(vec[2]); | |
426 | tl->t[tl->n].flags = reflags; | |
427 | ++tl->n; | |
428 | return 0; | |
429 | } | |
430 | ||
e83d0967 RK |
431 | static int set_backend(const struct config_state *cs, |
432 | const struct conf *whoami, | |
433 | int nvec, char **vec) { | |
434 | int *const valuep = ADDRESS(cs->config, int); | |
435 | ||
436 | if(nvec != 1) { | |
437 | error(0, "%s:%d: '%s' requires one argument", | |
438 | cs->path, cs->line, whoami->name); | |
439 | return -1; | |
440 | } | |
441 | if(!strcmp(vec[0], "alsa")) { | |
442 | #if API_ALSA | |
443 | *valuep = BACKEND_ALSA; | |
444 | #else | |
445 | error(0, "%s:%d: ALSA is not available on this platform", | |
446 | cs->path, cs->line); | |
447 | return -1; | |
448 | #endif | |
449 | } else if(!strcmp(vec[0], "command")) | |
450 | *valuep = BACKEND_COMMAND; | |
451 | else if(!strcmp(vec[0], "network")) | |
452 | *valuep = BACKEND_NETWORK; | |
453 | else { | |
454 | error(0, "%s:%d: invalid '%s' value '%s'", | |
455 | cs->path, cs->line, whoami->name, vec[0]); | |
456 | return -1; | |
457 | } | |
458 | return 0; | |
459 | } | |
460 | ||
460b9539 | 461 | /* free functions */ |
462 | ||
463 | static void free_none(struct config attribute((unused)) *c, | |
464 | const struct conf attribute((unused)) *whoami) { | |
465 | } | |
466 | ||
467 | static void free_string(struct config *c, | |
468 | const struct conf *whoami) { | |
469 | xfree(VALUE(c, char *)); | |
470 | } | |
471 | ||
472 | static void free_stringlist(struct config *c, | |
473 | const struct conf *whoami) { | |
474 | int n; | |
475 | struct stringlist *sl = ADDRESS(c, struct stringlist); | |
476 | ||
477 | for(n = 0; n < sl->n; ++n) | |
478 | xfree(sl->s[n]); | |
479 | xfree(sl->s); | |
480 | } | |
481 | ||
482 | static void free_stringlistlist(struct config *c, | |
483 | const struct conf *whoami) { | |
484 | int n, m; | |
485 | struct stringlistlist *sll = ADDRESS(c, struct stringlistlist); | |
486 | struct stringlist *sl; | |
487 | ||
488 | for(n = 0; n < sll->n; ++n) { | |
489 | sl = &sll->s[n]; | |
490 | for(m = 0; m < sl->n; ++m) | |
491 | xfree(sl->s[m]); | |
492 | xfree(sl->s); | |
493 | } | |
494 | xfree(sll->s); | |
495 | } | |
496 | ||
497 | static void free_collectionlist(struct config *c, | |
498 | const struct conf *whoami) { | |
499 | struct collectionlist *cll = ADDRESS(c, struct collectionlist); | |
500 | struct collection *cl; | |
501 | int n; | |
502 | ||
503 | for(n = 0; n < cll->n; ++n) { | |
504 | cl = &cll->s[n]; | |
505 | xfree(cl->module); | |
506 | xfree(cl->encoding); | |
507 | xfree(cl->root); | |
508 | } | |
509 | xfree(cll->s); | |
510 | } | |
511 | ||
512 | static void free_namepartlist(struct config *c, | |
513 | const struct conf *whoami) { | |
514 | struct namepartlist *npl = ADDRESS(c, struct namepartlist); | |
515 | struct namepart *np; | |
516 | int n; | |
517 | ||
518 | for(n = 0; n < npl->n; ++n) { | |
519 | np = &npl->s[n]; | |
520 | xfree(np->part); | |
521 | pcre_free(np->re); /* ...whatever pcre_free is set to. */ | |
522 | xfree(np->replace); | |
523 | xfree(np->context); | |
524 | } | |
525 | xfree(npl->s); | |
526 | } | |
527 | ||
528 | static void free_transformlist(struct config *c, | |
529 | const struct conf *whoami) { | |
530 | struct transformlist *tl = ADDRESS(c, struct transformlist); | |
531 | struct transform *t; | |
532 | int n; | |
533 | ||
534 | for(n = 0; n < tl->n; ++n) { | |
535 | t = &tl->t[n]; | |
536 | xfree(t->type); | |
537 | pcre_free(t->re); /* ...whatever pcre_free is set to. */ | |
538 | xfree(t->replace); | |
539 | xfree(t->context); | |
540 | } | |
541 | xfree(tl->t); | |
542 | } | |
543 | ||
544 | /* configuration types */ | |
545 | ||
546 | static const struct conftype | |
547 | type_signal = { set_signal, free_none }, | |
548 | type_collections = { set_collections, free_collectionlist }, | |
549 | type_boolean = { set_boolean, free_none }, | |
550 | type_string = { set_string, free_string }, | |
551 | type_stringlist = { set_stringlist, free_stringlist }, | |
552 | type_integer = { set_integer, free_none }, | |
553 | type_stringlist_accum = { set_stringlist_accum, free_stringlistlist }, | |
554 | type_string_accum = { set_string_accum, free_stringlist }, | |
9d5da576 | 555 | type_sample_format = { set_sample_format, free_none }, |
460b9539 | 556 | type_restrict = { set_restrict, free_none }, |
557 | type_namepart = { set_namepart, free_namepartlist }, | |
e83d0967 RK |
558 | type_transform = { set_transform, free_transformlist }, |
559 | type_backend = { set_backend, free_none }; | |
460b9539 | 560 | |
561 | /* specific validation routine */ | |
562 | ||
563 | #define VALIDATE_FILE(test, what) do { \ | |
564 | struct stat sb; \ | |
565 | int n; \ | |
566 | \ | |
567 | for(n = 0; n < nvec; ++n) { \ | |
568 | if(stat(vec[n], &sb) < 0) { \ | |
569 | error(errno, "%s:%d: %s", cs->path, cs->line, vec[n]); \ | |
570 | return -1; \ | |
571 | } \ | |
572 | if(!test(sb.st_mode)) { \ | |
573 | error(0, "%s:%d: %s is not a %s", \ | |
574 | cs->path, cs->line, vec[n], what); \ | |
575 | return -1; \ | |
576 | } \ | |
577 | } \ | |
578 | } while(0) | |
579 | ||
580 | static int validate_isdir(const struct config_state *cs, | |
581 | int nvec, char **vec) { | |
582 | VALIDATE_FILE(S_ISDIR, "directory"); | |
583 | return 0; | |
584 | } | |
585 | ||
586 | static int validate_isreg(const struct config_state *cs, | |
587 | int nvec, char **vec) { | |
588 | VALIDATE_FILE(S_ISREG, "regular file"); | |
589 | return 0; | |
590 | } | |
591 | ||
592 | static int validate_ischr(const struct config_state *cs, | |
593 | int nvec, char **vec) { | |
594 | VALIDATE_FILE(S_ISCHR, "character device"); | |
595 | return 0; | |
596 | } | |
597 | ||
598 | static int validate_player(const struct config_state *cs, | |
599 | int nvec, | |
600 | char attribute((unused)) **vec) { | |
601 | if(nvec < 2) { | |
602 | error(0, "%s:%d: should be at least 'player PATTERN MODULE'", | |
603 | cs->path, cs->line); | |
604 | return -1; | |
605 | } | |
606 | return 0; | |
607 | } | |
608 | ||
609 | static int validate_allow(const struct config_state *cs, | |
610 | int nvec, | |
611 | char attribute((unused)) **vec) { | |
612 | if(nvec != 2) { | |
613 | error(0, "%s:%d: must be 'allow NAME PASS'", cs->path, cs->line); | |
614 | return -1; | |
615 | } | |
616 | return 0; | |
617 | } | |
618 | ||
619 | static int validate_non_negative(const struct config_state *cs, | |
620 | int nvec, char **vec) { | |
621 | long n; | |
622 | ||
623 | if(nvec < 1) { | |
624 | error(0, "%s:%d: missing argument", cs->path, cs->line); | |
625 | return -1; | |
626 | } | |
627 | if(nvec > 1) { | |
628 | error(0, "%s:%d: too many arguments", cs->path, cs->line); | |
629 | return -1; | |
630 | } | |
631 | if(xstrtol(&n, vec[0], 0, 0)) { | |
632 | error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno)); | |
633 | return -1; | |
634 | } | |
635 | if(n < 0) { | |
636 | error(0, "%s:%d: must not be negative", cs->path, cs->line); | |
637 | return -1; | |
638 | } | |
639 | return 0; | |
640 | } | |
641 | ||
642 | static int validate_positive(const struct config_state *cs, | |
643 | int nvec, char **vec) { | |
644 | long n; | |
645 | ||
646 | if(nvec < 1) { | |
647 | error(0, "%s:%d: missing argument", cs->path, cs->line); | |
648 | return -1; | |
649 | } | |
650 | if(nvec > 1) { | |
651 | error(0, "%s:%d: too many arguments", cs->path, cs->line); | |
652 | return -1; | |
653 | } | |
654 | if(xstrtol(&n, vec[0], 0, 0)) { | |
655 | error(0, "%s:%d: %s", cs->path, cs->line, strerror(errno)); | |
656 | return -1; | |
657 | } | |
658 | if(n <= 0) { | |
659 | error(0, "%s:%d: must be positive", cs->path, cs->line); | |
660 | return -1; | |
661 | } | |
662 | return 0; | |
663 | } | |
664 | ||
665 | static int validate_isauser(const struct config_state *cs, | |
666 | int attribute((unused)) nvec, | |
667 | char **vec) { | |
668 | struct passwd *pw; | |
669 | ||
670 | if(!(pw = getpwnam(vec[0]))) { | |
671 | error(0, "%s:%d: no such user as '%s'", cs->path, cs->line, vec[0]); | |
672 | return -1; | |
673 | } | |
674 | return 0; | |
675 | } | |
676 | ||
9d5da576 | 677 | static int validate_sample_format(const struct config_state *cs, |
678 | int attribute((unused)) nvec, | |
679 | char **vec) { | |
680 | return parse_sample_format(cs, 0, nvec, vec); | |
681 | } | |
682 | ||
460b9539 | 683 | static int validate_channel(const struct config_state *cs, |
684 | int attribute((unused)) nvec, | |
685 | char **vec) { | |
686 | if(mixer_channel(vec[0]) == -1) { | |
687 | error(0, "%s:%d: invalid channel '%s'", cs->path, cs->line, vec[0]); | |
688 | return -1; | |
689 | } | |
690 | return 0; | |
691 | } | |
692 | ||
693 | static int validate_any(const struct config_state attribute((unused)) *cs, | |
694 | int attribute((unused)) nvec, | |
695 | char attribute((unused)) **vec) { | |
696 | return 0; | |
697 | } | |
698 | ||
699 | static int validate_url(const struct config_state attribute((unused)) *cs, | |
700 | int attribute((unused)) nvec, | |
701 | char **vec) { | |
702 | const char *s; | |
703 | int n; | |
704 | /* absoluteURI = scheme ":" ( hier_part | opaque_part ) | |
705 | scheme = alpha *( alpha | digit | "+" | "-" | "." ) */ | |
706 | s = vec[0]; | |
707 | n = strspn(s, ("abcdefghijklmnopqrstuvwxyz" | |
708 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
709 | "0123456789")); | |
710 | if(s[n] != ':') { | |
711 | error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]); | |
712 | return -1; | |
713 | } | |
714 | if(!strncmp(s, "http:", 5) | |
715 | || !strncmp(s, "https:", 6)) { | |
716 | s += n + 1; | |
717 | /* we only do a rather cursory check */ | |
718 | if(strncmp(s, "//", 2)) { | |
719 | error(0, "%s:%d: invalid url '%s'", cs->path, cs->line, vec[0]); | |
720 | return -1; | |
721 | } | |
722 | } | |
723 | return 0; | |
724 | } | |
725 | ||
726 | static int validate_alias(const struct config_state *cs, | |
727 | int nvec, | |
728 | char **vec) { | |
729 | const char *s; | |
730 | int in_brackets = 0, c; | |
731 | ||
732 | if(nvec < 1) { | |
733 | error(0, "%s:%d: missing argument", cs->path, cs->line); | |
734 | return -1; | |
735 | } | |
736 | if(nvec > 1) { | |
737 | error(0, "%s:%d: too many arguments", cs->path, cs->line); | |
738 | return -1; | |
739 | } | |
740 | s = vec[0]; | |
741 | while((c = (unsigned char)*s++)) { | |
742 | if(in_brackets) { | |
743 | if(c == '}') | |
744 | in_brackets = 0; | |
745 | else if(!isalnum(c)) { | |
746 | error(0, "%s:%d: invalid part name in alias expansion in '%s'", | |
747 | cs->path, cs->line, vec[0]); | |
748 | return -1; | |
749 | } | |
750 | } else { | |
751 | if(c == '{') { | |
752 | in_brackets = 1; | |
753 | if(*s == '/') | |
754 | ++s; | |
755 | } else if(c == '\\') { | |
756 | if(!(c = (unsigned char)*s++)) { | |
757 | error(0, "%s:%d: unterminated escape in alias expansion in '%s'", | |
758 | cs->path, cs->line, vec[0]); | |
759 | return -1; | |
760 | } else if(c != '\\' && c != '{') { | |
761 | error(0, "%s:%d: invalid escape in alias expansion in '%s'", | |
762 | cs->path, cs->line, vec[0]); | |
763 | return -1; | |
764 | } | |
765 | } | |
766 | } | |
767 | ++s; | |
768 | } | |
769 | if(in_brackets) { | |
770 | error(0, "%s:%d: unterminated part name in alias expansion in '%s'", | |
771 | cs->path, cs->line, vec[0]); | |
772 | return -1; | |
773 | } | |
774 | return 0; | |
775 | } | |
776 | ||
e83d0967 RK |
777 | static int validate_addrport(const struct config_state attribute((unused)) *cs, |
778 | int nvec, | |
779 | char attribute((unused)) **vec) { | |
780 | switch(nvec) { | |
781 | case 0: | |
782 | error(0, "%s:%d: missing address", | |
783 | cs->path, cs->line); | |
784 | return -1; | |
785 | case 1: | |
786 | error(0, "%s:%d: missing port name/number", | |
787 | cs->path, cs->line); | |
788 | return -1; | |
789 | case 2: | |
790 | return 0; | |
791 | default: | |
792 | error(0, "%s:%d: expected ADDRESS PORT", | |
793 | cs->path, cs->line); | |
794 | return -1; | |
795 | } | |
796 | } | |
797 | ||
ccf0aafa | 798 | static int validate_port(const struct config_state attribute((unused)) *cs, |
e83d0967 RK |
799 | int nvec, |
800 | char attribute((unused)) **vec) { | |
801 | switch(nvec) { | |
802 | case 0: | |
803 | error(0, "%s:%d: missing address", | |
804 | cs->path, cs->line); | |
805 | return -1; | |
806 | case 1: | |
807 | case 2: | |
808 | return 0; | |
809 | default: | |
ccf0aafa | 810 | error(0, "%s:%d: expected [ADDRESS] PORT", |
e83d0967 RK |
811 | cs->path, cs->line); |
812 | return -1; | |
813 | } | |
814 | } | |
815 | ||
637fdea3 RK |
816 | static int validate_algo(const struct config_state attribute((unused)) *cs, |
817 | int nvec, | |
818 | char **vec) { | |
819 | if(nvec != 1) { | |
820 | error(0, "%s:%d: invalid algorithm specification", cs->path, cs->line); | |
821 | return -1; | |
822 | } | |
823 | if(!valid_authhash(vec[0])) { | |
824 | error(0, "%s:%d: unsuported algorithm '%s'", cs->path, cs->line, vec[0]); | |
825 | return -1; | |
826 | } | |
827 | return 0; | |
828 | } | |
829 | ||
3f3bb97b | 830 | /** @brief Item name and and offset */ |
460b9539 | 831 | #define C(x) #x, offsetof(struct config, x) |
3f3bb97b | 832 | /** @brief Item name and and offset */ |
460b9539 | 833 | #define C2(x,y) #x, offsetof(struct config, y) |
834 | ||
3f3bb97b | 835 | /** @brief All configuration items */ |
460b9539 | 836 | static const struct conf conf[] = { |
837 | { C(alias), &type_string, validate_alias }, | |
838 | { C(allow), &type_stringlist_accum, validate_allow }, | |
637fdea3 | 839 | { C(authorization_algorithm), &type_string, validate_algo }, |
e83d0967 | 840 | { C(broadcast), &type_stringlist, validate_addrport }, |
ccf0aafa | 841 | { C(broadcast_from), &type_stringlist, validate_addrport }, |
460b9539 | 842 | { C(channel), &type_string, validate_channel }, |
843 | { C(checkpoint_kbyte), &type_integer, validate_non_negative }, | |
844 | { C(checkpoint_min), &type_integer, validate_non_negative }, | |
845 | { C(collection), &type_collections, validate_any }, | |
e83d0967 | 846 | { C(connect), &type_stringlist, validate_addrport }, |
460b9539 | 847 | { C(device), &type_string, validate_any }, |
848 | { C(gap), &type_integer, validate_non_negative }, | |
849 | { C(history), &type_integer, validate_positive }, | |
850 | { C(home), &type_string, validate_isdir }, | |
ccf0aafa | 851 | { C(listen), &type_stringlist, validate_port }, |
460b9539 | 852 | { C(lock), &type_boolean, validate_any }, |
853 | { C(mixer), &type_string, validate_ischr }, | |
23205f9c | 854 | { C(multicast_ttl), &type_integer, validate_non_negative }, |
460b9539 | 855 | { C(namepart), &type_namepart, validate_any }, |
856 | { C2(nice, nice_rescan), &type_integer, validate_non_negative }, | |
857 | { C(nice_rescan), &type_integer, validate_non_negative }, | |
858 | { C(nice_server), &type_integer, validate_any }, | |
859 | { C(nice_speaker), &type_integer, validate_any }, | |
2a10b70b | 860 | { C(noticed_history), &type_integer, validate_positive }, |
460b9539 | 861 | { C(password), &type_string, validate_any }, |
862 | { C(player), &type_stringlist_accum, validate_player }, | |
863 | { C(plugins), &type_string_accum, validate_isdir }, | |
864 | { C(prefsync), &type_integer, validate_positive }, | |
459d4402 | 865 | { C(queue_pad), &type_integer, validate_positive }, |
460b9539 | 866 | { C(refresh), &type_integer, validate_positive }, |
867 | { C2(restrict, restrictions), &type_restrict, validate_any }, | |
9d5da576 | 868 | { C(sample_format), &type_sample_format, validate_sample_format }, |
460b9539 | 869 | { C(scratch), &type_string_accum, validate_isreg }, |
870 | { C(signal), &type_signal, validate_any }, | |
5330d674 | 871 | { C(sox_generation), &type_integer, validate_non_negative }, |
e83d0967 | 872 | { C(speaker_backend), &type_backend, validate_any }, |
9d5da576 | 873 | { C(speaker_command), &type_string, validate_any }, |
460b9539 | 874 | { C(stopword), &type_string_accum, validate_any }, |
875 | { C(templates), &type_string_accum, validate_isdir }, | |
876 | { C(transform), &type_transform, validate_any }, | |
877 | { C(trust), &type_string_accum, validate_any }, | |
878 | { C(url), &type_string, validate_url }, | |
879 | { C(user), &type_string, validate_isauser }, | |
880 | { C(username), &type_string, validate_any }, | |
881 | }; | |
882 | ||
3f3bb97b | 883 | /** @brief Find a configuration item's definition by key */ |
460b9539 | 884 | static const struct conf *find(const char *key) { |
885 | int n; | |
886 | ||
887 | if((n = TABLE_FIND(conf, struct conf, name, key)) < 0) | |
888 | return 0; | |
889 | return &conf[n]; | |
890 | } | |
891 | ||
3f3bb97b | 892 | /** @brief Set a new configuration value */ |
460b9539 | 893 | static int config_set(const struct config_state *cs, |
894 | int nvec, char **vec) { | |
895 | const struct conf *which; | |
896 | ||
897 | D(("config_set %s", vec[0])); | |
898 | if(!(which = find(vec[0]))) { | |
899 | error(0, "%s:%d: unknown configuration key '%s'", | |
900 | cs->path, cs->line, vec[0]); | |
901 | return -1; | |
902 | } | |
903 | return (which->validate(cs, nvec - 1, vec + 1) | |
904 | || which->type->set(cs, which, nvec - 1, vec + 1)); | |
905 | } | |
906 | ||
0e4472a0 | 907 | /** @brief Error callback used by config_include() */ |
460b9539 | 908 | static void config_error(const char *msg, void *u) { |
909 | const struct config_state *cs = u; | |
910 | ||
911 | error(0, "%s:%d: %s", cs->path, cs->line, msg); | |
912 | } | |
913 | ||
3f3bb97b | 914 | /** @brief Include a file by name */ |
460b9539 | 915 | static int config_include(struct config *c, const char *path) { |
916 | FILE *fp; | |
917 | char *buffer, *inputbuffer, **vec; | |
918 | int n, ret = 0; | |
919 | struct config_state cs; | |
920 | ||
921 | cs.path = path; | |
922 | cs.line = 0; | |
923 | cs.config = c; | |
924 | D(("%s: reading configuration", path)); | |
925 | if(!(fp = fopen(path, "r"))) { | |
926 | error(errno, "error opening %s", path); | |
927 | return -1; | |
928 | } | |
929 | while(!inputline(path, fp, &inputbuffer, '\n')) { | |
930 | ++cs.line; | |
931 | if(!(buffer = mb2utf8(inputbuffer))) { | |
932 | error(errno, "%s:%d: cannot convert to UTF-8", cs.path, cs.line); | |
933 | ret = -1; | |
934 | xfree(inputbuffer); | |
935 | continue; | |
936 | } | |
937 | xfree(inputbuffer); | |
938 | if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES, | |
939 | config_error, &cs))) { | |
940 | ret = -1; | |
941 | xfree(buffer); | |
942 | continue; | |
943 | } | |
944 | if(n) { | |
945 | if(!strcmp(vec[0], "include")) { | |
946 | if(n != 2) { | |
947 | error(0, "%s:%d: must be 'include PATH'", cs.path, cs.line); | |
948 | ret = -1; | |
949 | } else | |
950 | config_include(c, vec[1]); | |
951 | } else | |
952 | ret |= config_set(&cs, n, vec); | |
953 | } | |
954 | for(n = 0; vec[n]; ++n) xfree(vec[n]); | |
955 | xfree(vec); | |
956 | xfree(buffer); | |
957 | } | |
958 | if(ferror(fp)) { | |
959 | error(errno, "error reading %s", path); | |
960 | ret = -1; | |
961 | } | |
962 | fclose(fp); | |
963 | return ret; | |
964 | } | |
965 | ||
3f3bb97b | 966 | /** @brief Make a new default configuration */ |
460b9539 | 967 | static struct config *config_default(void) { |
968 | struct config *c = xmalloc(sizeof *c); | |
969 | const char *logname; | |
970 | struct passwd *pw; | |
971 | ||
972 | /* Strings had better be xstrdup'd as they will get freed at some point. */ | |
973 | c->gap = 2; | |
974 | c->history = 60; | |
975 | c->home = xstrdup(pkgstatedir); | |
976 | if(!(pw = getpwuid(getuid()))) | |
977 | fatal(0, "cannot determine our username"); | |
978 | logname = pw->pw_name; | |
979 | c->username = xstrdup(logname); | |
980 | c->refresh = 15; | |
981 | c->prefsync = 3600; | |
982 | c->signal = SIGKILL; | |
983 | c->alias = xstrdup("{/artist}{/album}{/title}{ext}"); | |
984 | c->lock = 1; | |
985 | c->device = xstrdup("default"); | |
986 | c->nice_rescan = 10; | |
9d5da576 | 987 | c->speaker_command = 0; |
988 | c->sample_format.bits = 16; | |
989 | c->sample_format.rate = 44100; | |
990 | c->sample_format.channels = 2; | |
6d2d327c | 991 | c->sample_format.endian = ENDIAN_NATIVE; |
459d4402 | 992 | c->queue_pad = 10; |
e83d0967 | 993 | c->speaker_backend = -1; |
23205f9c | 994 | c->multicast_ttl = 1; |
637fdea3 | 995 | c->authorization_algorithm = xstrdup("sha1"); |
2a10b70b | 996 | c->noticed_history = 31; |
460b9539 | 997 | return c; |
998 | } | |
999 | ||
1000 | static char *get_file(struct config *c, const char *name) { | |
1001 | char *s; | |
1002 | ||
1003 | byte_xasprintf(&s, "%s/%s", c->home, name); | |
1004 | return s; | |
1005 | } | |
1006 | ||
3f3bb97b | 1007 | /** @brief Set the default configuration file */ |
460b9539 | 1008 | static void set_configfile(void) { |
1009 | if(!configfile) | |
1010 | byte_xasprintf(&configfile, "%s/config", pkgconfdir); | |
1011 | } | |
1012 | ||
3f3bb97b | 1013 | /** @brief Free a configuration object */ |
460b9539 | 1014 | static void config_free(struct config *c) { |
1015 | int n; | |
1016 | ||
1017 | if(c) { | |
1018 | for(n = 0; n < (int)(sizeof conf / sizeof *conf); ++n) | |
1019 | conf[n].type->free(c, &conf[n]); | |
1020 | for(n = 0; n < c->nparts; ++n) | |
1021 | xfree(c->parts[n]); | |
1022 | xfree(c->parts); | |
1023 | xfree(c); | |
1024 | } | |
1025 | } | |
1026 | ||
3f3bb97b | 1027 | /** @brief Set post-parse defaults */ |
c00fce3a RK |
1028 | static void config_postdefaults(struct config *c, |
1029 | int server) { | |
460b9539 | 1030 | struct config_state cs; |
1031 | const struct conf *whoami; | |
1032 | int n; | |
1033 | ||
1034 | static const char *namepart[][4] = { | |
88608992 | 1035 | { "title", "/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display" }, |
460b9539 | 1036 | { "title", "/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort" }, |
1037 | { "album", "/([^/]+)/[^/]+$", "$1", "*" }, | |
1038 | { "artist", "/([^/]+)/[^/]+/[^/]+$", "$1", "*" }, | |
1039 | { "ext", "(\\.[a-zA-Z0-9]+)$", "$1", "*" }, | |
1040 | }; | |
1041 | #define NNAMEPART (int)(sizeof namepart / sizeof *namepart) | |
1042 | ||
1043 | static const char *transform[][5] = { | |
88608992 | 1044 | { "track", "^.*/([0-9]+ *[-:] *)?([^/]+)\\.[a-zA-Z0-9]+$", "$2", "display", "" }, |
460b9539 | 1045 | { "track", "^.*/([^/]+)\\.[a-zA-Z0-9]+$", "$1", "sort", "" }, |
1046 | { "dir", "^.*/([^/]+)$", "$1", "*", "" }, | |
1047 | { "dir", "^(the) ([^/]*)", "$2, $1", "sort", "i", }, | |
1048 | { "dir", "[[:punct:]]", "", "sort", "g", } | |
1049 | }; | |
1050 | #define NTRANSFORM (int)(sizeof transform / sizeof *transform) | |
1051 | ||
1052 | cs.path = "<internal>"; | |
1053 | cs.line = 0; | |
1054 | cs.config = c; | |
1055 | if(!c->namepart.n) { | |
1056 | whoami = find("namepart"); | |
1057 | for(n = 0; n < NNAMEPART; ++n) | |
1058 | set_namepart(&cs, whoami, 4, (char **)namepart[n]); | |
1059 | } | |
1060 | if(!c->transform.n) { | |
1061 | whoami = find("transform"); | |
1062 | for(n = 0; n < NTRANSFORM; ++n) | |
1063 | set_transform(&cs, whoami, 5, (char **)transform[n]); | |
1064 | } | |
e83d0967 RK |
1065 | if(c->speaker_backend == -1) { |
1066 | if(c->speaker_command) | |
1067 | c->speaker_backend = BACKEND_COMMAND; | |
1068 | else if(c->broadcast.n) | |
1069 | c->speaker_backend = BACKEND_NETWORK; | |
1070 | else { | |
1071 | #if API_ALSA | |
1072 | c->speaker_backend = BACKEND_ALSA; | |
1073 | #else | |
1074 | c->speaker_backend = BACKEND_COMMAND; | |
1075 | #endif | |
1076 | } | |
1077 | } | |
c00fce3a RK |
1078 | if(server) { |
1079 | if(c->speaker_backend == BACKEND_COMMAND && !c->speaker_command) | |
1080 | fatal(0, "speaker_backend is command but speaker_command is not set"); | |
1081 | if(c->speaker_backend == BACKEND_NETWORK && !c->broadcast.n) | |
1082 | fatal(0, "speaker_backend is network but broadcast is not set"); | |
1083 | } | |
6d2d327c RK |
1084 | if(c->speaker_backend) { |
1085 | /* Override sample format */ | |
1086 | c->sample_format.rate = 44100; | |
1087 | c->sample_format.channels = 2; | |
1088 | c->sample_format.bits = 16; | |
1089 | c->sample_format.endian = ENDIAN_BIG; | |
1090 | } | |
460b9539 | 1091 | } |
1092 | ||
c00fce3a RK |
1093 | /** @brief (Re-)read the config file |
1094 | * @param server If set, do extra checking | |
1095 | */ | |
1096 | int config_read(int server) { | |
460b9539 | 1097 | struct config *c; |
1098 | char *privconf; | |
1099 | struct passwd *pw; | |
1100 | ||
1101 | set_configfile(); | |
1102 | c = config_default(); | |
9ade2319 | 1103 | /* standalone Disobedience installs might not have a global config file */ |
1104 | if(access(configfile, F_OK) == 0) | |
1105 | if(config_include(c, configfile)) | |
1106 | return -1; | |
460b9539 | 1107 | /* if we can read the private config file, do */ |
1108 | if((privconf = config_private()) | |
1109 | && access(privconf, R_OK) == 0 | |
1110 | && config_include(c, privconf)) | |
1111 | return -1; | |
1112 | xfree(privconf); | |
1113 | /* if there's a per-user system config file for this user, read it */ | |
1114 | if(!(pw = getpwuid(getuid()))) | |
1115 | fatal(0, "cannot determine our username"); | |
1116 | if((privconf = config_usersysconf(pw)) | |
1117 | && access(privconf, F_OK) == 0 | |
1118 | && config_include(c, privconf)) | |
1119 | return -1; | |
1120 | xfree(privconf); | |
1121 | /* if we have a password file, read it */ | |
1122 | if((privconf = config_userconf(getenv("HOME"), pw)) | |
1123 | && access(privconf, F_OK) == 0 | |
1124 | && config_include(c, privconf)) | |
1125 | return -1; | |
1126 | xfree(privconf); | |
1127 | /* install default namepart and transform settings */ | |
c00fce3a | 1128 | config_postdefaults(c, server); |
460b9539 | 1129 | /* everything is good so we shall use the new config */ |
1130 | config_free(config); | |
1131 | config = c; | |
1132 | return 0; | |
1133 | } | |
1134 | ||
3f3bb97b | 1135 | /** @brief Return the path to the private configuration file */ |
460b9539 | 1136 | char *config_private(void) { |
1137 | char *s; | |
1138 | ||
1139 | set_configfile(); | |
1140 | byte_xasprintf(&s, "%s.private", configfile); | |
1141 | return s; | |
1142 | } | |
1143 | ||
3f3bb97b | 1144 | /** @brief Return the path to user's personal configuration file */ |
460b9539 | 1145 | char *config_userconf(const char *home, const struct passwd *pw) { |
1146 | char *s; | |
1147 | ||
1148 | byte_xasprintf(&s, "%s/.disorder/passwd", home ? home : pw->pw_dir); | |
1149 | return s; | |
1150 | } | |
1151 | ||
3f3bb97b RK |
1152 | /** @brief Return the path to user-specific system configuration */ |
1153 | char *config_usersysconf(const struct passwd *pw) { | |
460b9539 | 1154 | char *s; |
1155 | ||
1156 | set_configfile(); | |
1157 | if(!strchr(pw->pw_name, '/')) { | |
1158 | byte_xasprintf(&s, "%s.%s", configfile, pw->pw_name); | |
1159 | return s; | |
1160 | } else | |
1161 | return 0; | |
1162 | } | |
1163 | ||
1164 | char *config_get_file(const char *name) { | |
1165 | return get_file(config, name); | |
1166 | } | |
1167 | ||
1168 | /* | |
1169 | Local Variables: | |
1170 | c-basic-offset:2 | |
1171 | comment-column:40 | |
1172 | fill-column:79 | |
1173 | End: | |
1174 | */ |