/
conffile.c
313 lines (258 loc) · 6.32 KB
/
conffile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright (C) 2009-2011 Julien BLACHE <jb@jblache.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <pwd.h>
#include <errno.h>
#include <confuse.h>
#include "logger.h"
#include "misc.h"
#include "conffile.h"
/* Forward */
static int cb_loglevel(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result);
/* general section structure */
static cfg_opt_t sec_general[] =
{
CFG_STR("uid", "nobody", CFGF_NONE),
CFG_STR("admin_password", NULL, CFGF_NONE),
CFG_STR("logfile", STATEDIR "/log/" PACKAGE ".log", CFGF_NONE),
CFG_STR("db_path", STATEDIR "/cache/" PACKAGE "/songs3.db", CFGF_NONE),
CFG_INT_CB("loglevel", E_LOG, CFGF_NONE, &cb_loglevel),
CFG_BOOL("ipv6", cfg_true, CFGF_NONE),
CFG_END()
};
/* library section structure */
static cfg_opt_t sec_library[] =
{
CFG_STR("name", "My Music on %h", CFGF_NONE),
CFG_INT("port", 3689, CFGF_NONE),
CFG_STR("password", NULL, CFGF_NONE),
CFG_STR_LIST("directories", NULL, CFGF_NONE),
CFG_STR_LIST("compilations", NULL, CFGF_NONE),
CFG_BOOL("itunes_overrides", cfg_false, CFGF_NONE),
CFG_STR_LIST("no_transcode", NULL, CFGF_NONE),
CFG_STR_LIST("force_transcode", NULL, CFGF_NONE),
CFG_END()
};
/* local audio section structure */
static cfg_opt_t sec_audio[] =
{
CFG_STR("nickname", "Computer", CFGF_NONE),
#ifdef __linux__
CFG_STR("card", "default", CFGF_NONE),
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
CFG_STR("card", "/dev/dsp", CFGF_NONE),
#endif
CFG_STR("mixer", NULL, CFGF_NONE),
CFG_END()
};
/* ApEx device section structure */
static cfg_opt_t sec_apex[] =
{
CFG_STR("password", NULL, CFGF_NONE),
CFG_END()
};
/* Config file structure */
static cfg_opt_t toplvl_cfg[] =
{
CFG_SEC("general", sec_general, CFGF_NONE),
CFG_SEC("library", sec_library, CFGF_NONE),
CFG_SEC("audio", sec_audio, CFGF_NONE),
CFG_SEC("apex", sec_apex, CFGF_MULTI | CFGF_TITLE),
CFG_END()
};
cfg_t *cfg;
uint64_t libhash;
uid_t runas_uid;
gid_t runas_gid;
static int
cb_loglevel(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
{
if (strcasecmp(value, "fatal") == 0)
*(long int *)result = E_FATAL;
else if (strcasecmp(value, "log") == 0)
*(long int *)result = E_LOG;
else if (strcasecmp(value, "warning") == 0)
*(long int *)result = E_WARN;
else if (strcasecmp(value, "info") == 0)
*(long int *)result = E_INFO;
else if (strcasecmp(value, "debug") == 0)
*(long int *)result = E_DBG;
else if (strcasecmp(value, "spam") == 0)
*(long int *)result = E_SPAM;
else
{
DPRINTF(E_WARN, L_CONF, "Unrecognised loglevel '%s'\n", value);
/* Default to warning */
*(long int *)result = 1;
}
return 0;
}
static int
conffile_expand_libname(cfg_t *lib)
{
char *libname;
char *hostname;
char *s;
char *d;
char *expanded;
struct utsname sysinfo;
size_t len;
size_t olen;
size_t hostlen;
size_t verlen;
int ret;
libname = cfg_getstr(lib, "name");
olen = strlen(libname);
/* Fast path */
s = strchr(libname, '%');
if (!s)
{
libhash = murmur_hash64(libname, olen, 0);
return 0;
}
/* Grab what we need */
ret = uname(&sysinfo);
if (ret != 0)
{
DPRINTF(E_WARN, L_CONF, "Could not get system name: %s\n", strerror(errno));
hostname = "Unknown host";
}
else
hostname = sysinfo.nodename;
hostlen = strlen(hostname);
verlen = strlen(VERSION);
/* Compute expanded size */
len = olen;
s = libname;
while (*s)
{
if (*s == '%')
{
s++;
switch (*s)
{
case 'h':
len += hostlen;
break;
case 'v':
len += verlen;
break;
}
}
s++;
}
expanded = (char *)malloc(len + 1);
if (!expanded)
{
DPRINTF(E_FATAL, L_CONF, "Out of memory\n");
return -1;
}
memset(expanded, 0, len + 1);
/* Do the actual expansion */
s = libname;
d = expanded;
while (*s)
{
if (*s == '%')
{
s++;
switch (*s)
{
case 'h':
strcat(d, hostname);
d += hostlen;
break;
case 'v':
strcat(d, VERSION);
d += verlen;
break;
}
s++;
}
else
{
*d = *s;
s++;
d++;
}
}
cfg_setstr(lib, "name", expanded);
libhash = murmur_hash64(expanded, strlen(expanded), 0);
free(expanded);
return 0;
}
int
conffile_load(char *file)
{
cfg_t *lib;
struct passwd *pw;
char *runas;
int ret;
cfg = cfg_init(toplvl_cfg, CFGF_NONE);
ret = cfg_parse(cfg, file);
if (ret == CFG_FILE_ERROR)
{
DPRINTF(E_FATAL, L_CONF, "Could not open config file %s\n", file);
goto out_fail;
}
else if (ret == CFG_PARSE_ERROR)
{
DPRINTF(E_FATAL, L_CONF, "Parse error in config file %s\n", file);
goto out_fail;
}
/* Resolve runas username */
runas = cfg_getstr(cfg_getsec(cfg, "general"), "uid");
pw = getpwnam(runas);
if (!pw)
{
DPRINTF(E_FATAL, L_CONF, "Could not lookup user %s: %s\n", runas, strerror(errno));
goto out_fail;
}
runas_uid = pw->pw_uid;
runas_gid = pw->pw_gid;
lib = cfg_getsec(cfg, "library");
if (cfg_size(lib, "directories") == 0)
{
DPRINTF(E_FATAL, L_CONF, "No directories specified for library\n");
goto out_fail;
}
/* Do keyword expansion on library names */
ret = conffile_expand_libname(lib);
if (ret != 0)
{
DPRINTF(E_FATAL, L_CONF, "Could not expand library name\n");
goto out_fail;
}
return 0;
out_fail:
cfg_free(cfg);
return -1;
}
void
conffile_unload(void)
{
cfg_free(cfg);
}