d716312e444b90615e0be9a1b59efc92f9c0f6fe
[umurmur.git] / src / conf.c
1 /* Copyright (C) 2009-2014, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2014, Thorvald Natvig <thorvald@natvig.com>
3
4    All rights reserved.
5
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9
10    - Redistributions of source code must retain the above copyright notice,
11      this list of conditions and the following disclaimer.
12    - Redistributions in binary form must reproduce the above copyright notice,
13      this list of conditions and the following disclaimer in the documentation
14      and/or other materials provided with the distribution.
15    - Neither the name of the Developers nor the names of its contributors may
16      be used to endorse or promote products derived from this software without
17      specific prior written permission.
18
19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <libconfig.h>
36
37 #include "types.h"
38 #include "conf.h"
39 #include "log.h"
40
41 static config_t configuration;
42
43 #define DEFAULT_WELCOME "Welcome to uMurmur!"
44 #define DEFAULT_MAX_CLIENTS 10
45 #define DEFAULT_MAX_BANDWIDTH 48000
46 #define DEFAULT_BINDPORT 64738
47 #define DEFAULT_BAN_LENGTH (60*60)
48 #define DEFAULT_OPUS_THRESHOLD 100
49
50 const char defaultconfig[] = DEFAULT_CONFIG;
51
52 void Conf_init(const char *conffile)
53 {
54         config_init(&configuration);
55         if (conffile == NULL)
56                 conffile = defaultconfig;
57         if (config_read_file(&configuration, conffile) != CONFIG_TRUE) {
58                 Log_fatal("Error in config file %s line %d: %s", conffile,
59                                   config_error_line(&configuration), config_error_text(&configuration));
60         }
61 }
62
63 bool_t Conf_ok(const char *conffile)
64 {
65         bool_t rc = true;
66         config_init(&configuration);
67         if (conffile == NULL)
68                 conffile = defaultconfig;
69         if (config_read_file(&configuration, conffile) != CONFIG_TRUE) {
70                 fprintf(stderr, "Error in config file %s line %d: %s\n", conffile,
71                         config_error_line(&configuration), config_error_text(&configuration));
72                 rc = false;
73         }
74         config_destroy(&configuration);
75         return rc;
76 }
77
78 void Conf_deinit()
79 {
80         config_destroy(&configuration);
81 }
82
83 const char *getStrConf(param_t param)
84 {
85         config_setting_t *setting = NULL;
86         const char *strsetting = NULL;
87
88         switch (param) {
89         case CERTIFICATE:
90                 setting = config_lookup(&configuration, "certificate");
91                 if (!setting)
92                         return "/etc/umurmur/certificate.crt";
93                 else {
94                         if ((strsetting = config_setting_get_string(setting)) != NULL)
95                                 return strsetting;
96                         else
97                                 return "/etc/umurmur/certificate.crt";
98                 }
99                 break;
100         case KEY:
101                 setting = config_lookup(&configuration, "private_key");
102                 if (!setting)
103                         return "/etc/umurmur/private_key.key";
104                 else {
105                         if ((strsetting = config_setting_get_string(setting)) != NULL)
106                                 return strsetting;
107                         else
108                                 return "/etc/umurmur/private_key.key";
109                 }
110                 break;
111         case CAPATH:
112                 setting = config_lookup(&configuration, "ca_path");
113                 if (!setting)
114                   return NULL;
115                 else {
116                         if ((strsetting = config_setting_get_string(setting)) != NULL)
117                                 return strsetting;
118                         else
119                                 return NULL;
120                 }
121                 break;
122         case PASSPHRASE:
123                 setting = config_lookup(&configuration, "password");
124                 if (!setting)
125                         return "";
126                 else {
127                         if ((strsetting = config_setting_get_string(setting)) != NULL)
128                                 return strsetting;
129                         else
130                                 return "";
131                 }
132                 break;
133         case ADMIN_PASSPHRASE:
134                 setting = config_lookup(&configuration, "admin_password");
135                 if (!setting)
136                         return "";
137                 else {
138                         if ((strsetting = config_setting_get_string(setting)) != NULL)
139                                 return strsetting;
140                         else
141                                 return "";
142                 }
143                 break;
144         case BINDADDR:
145                 setting = config_lookup(&configuration, "bindaddr");
146                 if (!setting)
147                         return NULL;
148                 else {
149                         if ((strsetting = config_setting_get_string(setting)) != NULL)
150                                 return strsetting;
151                         else
152                                 return NULL;
153                 }
154                 break;
155         case BINDADDR6:
156                 setting = config_lookup(&configuration, "bindaddr6");
157                 if (!setting)
158                         return NULL;
159                 else {
160                         if ((strsetting = config_setting_get_string(setting)) != NULL)
161                                 return strsetting;
162                         else
163                                 return NULL;
164                 }
165                 break;
166         case WELCOMETEXT:
167                 setting = config_lookup(&configuration, "welcometext");
168                 if (!setting)
169                         return DEFAULT_WELCOME;
170                 else {
171                         if ((strsetting = config_setting_get_string(setting)) != NULL)
172                                 return strsetting;
173                         else
174                         return DEFAULT_WELCOME;
175                 }
176                 break;
177         case DEFAULT_CHANNEL:
178                 setting = config_lookup(&configuration, "default_channel");
179                 if (!setting)
180                         return "";
181                 else {
182                         if ((strsetting = config_setting_get_string(setting)) != NULL)
183                                 return strsetting;
184                         else
185                         return "";
186                 }
187                 break;
188         case USERNAME:
189                 setting = config_lookup(&configuration, "username");
190                 if (!setting)
191                         return "";
192                 else {
193                         if ((strsetting = config_setting_get_string(setting)) != NULL)
194                                 return strsetting;
195                         else
196                         return "";
197                 }
198                 break;
199         case GROUPNAME:
200                 setting = config_lookup(&configuration, "groupname");
201                 if (!setting)
202                         return "";
203                 else {
204                         if ((strsetting = config_setting_get_string(setting)) != NULL)
205                                 return strsetting;
206                         else
207                         return "";
208                 }
209                 break;
210         case LOGFILE:
211                 setting = config_lookup(&configuration, "logfile");
212                 if (!setting)
213                         return NULL;
214                 else {
215                         if ((strsetting = config_setting_get_string(setting)) != NULL)
216                                 return strsetting;
217                         else
218                         return NULL;
219                 }
220                 break;
221         case BANFILE:
222                 setting = config_lookup(&configuration, "banfile");
223                 if (!setting)
224                         return NULL;
225                 else {
226                         if ((strsetting = config_setting_get_string(setting)) != NULL)
227                                 return strsetting;
228                         else
229                         return NULL;
230                 }
231                 break;
232         default:
233                 doAssert(false);
234                 break;
235         }
236         return NULL;
237 }
238
239 int getIntConf(param_t param)
240 {
241         config_setting_t *setting = NULL;
242
243         switch (param) {
244         case BINDPORT:
245                 setting = config_lookup(&configuration, "bindport");
246                 if (!setting)
247                         return DEFAULT_BINDPORT;
248                 else {
249                         return config_setting_get_int(setting);
250                 }
251                 break;
252         case BINDPORT6:
253                 setting = config_lookup(&configuration, "bindport6");
254                 if (!setting)
255                         return DEFAULT_BINDPORT;
256                 else {
257                         return config_setting_get_int(setting);
258                 }
259                 break;
260         case BAN_LENGTH:
261                 setting = config_lookup(&configuration, "ban_length");
262                 if (!setting)
263                         return DEFAULT_BAN_LENGTH;
264                 else {
265                         return config_setting_get_int(setting);
266                 }
267                 break;
268         case MAX_BANDWIDTH:
269                 setting = config_lookup(&configuration, "max_bandwidth");
270                 if (!setting)
271                         return DEFAULT_MAX_BANDWIDTH;
272                 else {
273                         return config_setting_get_int(setting);
274                 }
275                 break;
276         case MAX_CLIENTS:
277                 setting = config_lookup(&configuration, "max_users");
278                 if (!setting)
279                         return DEFAULT_MAX_CLIENTS;
280                 else {
281                         return config_setting_get_int(setting);
282                 }
283                 break;
284         case OPUS_THRESHOLD:
285                 setting = config_lookup(&configuration, "opus_threshold");
286                 if (!setting)
287                         return DEFAULT_OPUS_THRESHOLD;
288                 else {
289                         return config_setting_get_int(setting);
290                 }
291                 break;
292         default:
293                 doAssert(false);
294         }
295 }
296
297 bool_t getBoolConf(param_t param)
298 {
299         config_setting_t *setting = NULL;
300
301         switch (param) {
302         case ALLOW_TEXTMESSAGE:
303                 setting = config_lookup(&configuration, "allow_textmessage");
304                 if (!setting)
305                         return true;
306                 else
307                         return config_setting_get_bool(setting);
308                 break;
309         case ENABLE_BAN:
310                 setting = config_lookup(&configuration, "enable_ban");
311                 if (!setting)
312                         return false;
313                 else
314                         return config_setting_get_bool(setting);
315                 break;
316         case SYNC_BANFILE:
317                 setting = config_lookup(&configuration, "sync_banfile");
318                 if (!setting)
319                         return false;
320                 else
321                         return config_setting_get_bool(setting);
322                 break;
323         default:
324                 doAssert(false);
325         }
326 }
327
328 int Conf_getNextChannel(conf_channel_t *chdesc, int index)
329 {
330         config_setting_t *setting = NULL;
331         int maxconfig = 64, ret = 0;
332         char configstr[maxconfig];
333
334         ret = snprintf(configstr, maxconfig, "channels.[%d].name", index);
335         setting = config_lookup(&configuration, configstr);
336         if (ret >= maxconfig || ret < 0 || setting == NULL)
337                 return -1; /* Required */
338         chdesc->name =  config_setting_get_string(setting);
339
340         ret = snprintf(configstr, maxconfig, "channels.[%d].parent", index);
341         setting = config_lookup(&configuration, configstr);
342         if (ret >= maxconfig || ret < 0 || setting == NULL)
343                 return -1; /* Required */
344         chdesc->parent = config_setting_get_string(setting);
345
346         ret = snprintf(configstr, maxconfig, "channels.[%d].description", index);
347         setting = config_lookup(&configuration, configstr);
348         if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
349                 chdesc->description = NULL;
350         else
351                 chdesc->description = config_setting_get_string(setting);
352
353         ret = snprintf(configstr, maxconfig, "channels.[%d].password", index);
354         setting = config_lookup(&configuration, configstr);
355         if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
356                 chdesc->password = NULL;
357         else
358                 chdesc->password = config_setting_get_string(setting);
359
360         ret = snprintf(configstr, maxconfig, "channels.[%d].noenter", index);
361         setting = config_lookup(&configuration, configstr);
362         if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
363                 chdesc->noenter = false;
364         else
365                 chdesc->noenter = config_setting_get_bool(setting);
366
367         ret = snprintf(configstr, maxconfig, "channels.[%d].silent", index);
368         setting = config_lookup(&configuration, configstr);
369         if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
370                 chdesc->silent = false;
371         else
372                 chdesc->silent = config_setting_get_bool(setting);
373
374         ret = snprintf(configstr, maxconfig, "channels.[%d].position", index);
375         setting = config_lookup(&configuration, configstr);
376         if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
377                 chdesc->position = 0;
378         else
379                 chdesc->position = config_setting_get_int(setting);
380
381         return 0;
382 }
383
384 int Conf_getNextChannelLink(conf_channel_link_t *chlink, int index)
385 {
386         config_setting_t *setting = NULL;
387         int maxconfig = 64, ret = 0;
388         char configstr[maxconfig];
389
390         ret = snprintf(configstr, maxconfig, "channel_links.[%d].source", index);
391         setting = config_lookup(&configuration, configstr);
392         if (ret >= maxconfig || ret < 0 || setting == NULL)
393                 return -1;
394         chlink->source = config_setting_get_string(setting);
395
396         ret = snprintf(configstr, maxconfig, "channel_links.[%d].destination", index);
397         setting = config_lookup(&configuration, configstr);
398         if (ret >= maxconfig || ret < 0 || setting == NULL)
399                 return -1;
400         chlink->destination = config_setting_get_string(setting);
401
402         return 0;
403 }