Add ban enable switch. Default is disabled.
[umurmur.git] / src / conf.c
1 /* Copyright (C) 2009-2012, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2012, 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
49 const char defaultconfig[] = DEFAULT_CONFIG;
50
51 void Conf_init(const char *conffile)
52 {
53         config_init(&configuration);
54         if (conffile == NULL)
55                 conffile = defaultconfig;
56         if (config_read_file(&configuration, conffile) != CONFIG_TRUE) {
57                 Log_fatal("Error in config file %s line %d: %s", conffile,
58                                   config_error_line(&configuration), config_error_text(&configuration));
59         }
60 }
61
62 bool_t Conf_ok(const char *conffile)
63 {
64         bool_t rc = true;
65         config_init(&configuration);
66         if (conffile == NULL)
67                 conffile = defaultconfig;
68         if (config_read_file(&configuration, conffile) != CONFIG_TRUE) {
69                 fprintf(stderr, "Error in config file %s line %d: %s\n", conffile,
70                         config_error_line(&configuration), config_error_text(&configuration));
71                 rc = false;
72         }
73         config_destroy(&configuration);
74         return rc;
75 }
76
77 void Conf_deinit()
78 {
79         config_destroy(&configuration);
80 }
81
82 const char *getStrConf(param_t param)
83 {
84         config_setting_t *setting = NULL;
85         const char *strsetting = NULL;
86         
87         switch (param) {
88         case CERTIFICATE:
89                 setting = config_lookup(&configuration, "certificate");
90                 if (!setting)
91                         return "/etc/umurmur/certificate.crt";
92                 else {
93                         if ((strsetting = config_setting_get_string(setting)) != NULL)
94                                 return strsetting;
95                         else
96                                 return "/etc/umurmur/certificate.crt";
97                 }
98                 break;
99         case KEY:
100                 setting = config_lookup(&configuration, "private_key");
101                 if (!setting)
102                         return "/etc/umurmur/private_key.key";
103                 else {
104                         if ((strsetting = config_setting_get_string(setting)) != NULL)
105                                 return strsetting;
106                         else
107                                 return "/etc/umurmur/private_key.key";
108                 }
109                 break;
110         case PASSPHRASE:
111                 setting = config_lookup(&configuration, "password");
112                 if (!setting)
113                         return "";
114                 else {
115                         if ((strsetting = config_setting_get_string(setting)) != NULL)
116                                 return strsetting;
117                         else
118                                 return "";
119                 }
120                 break;
121         case ADMIN_PASSPHRASE:
122                 setting = config_lookup(&configuration, "admin_password");
123                 if (!setting)
124                         return "";
125                 else {
126                         if ((strsetting = config_setting_get_string(setting)) != NULL)
127                                 return strsetting;
128                         else
129                                 return "";
130                 }
131                 break;
132         case BINDADDR:
133                 setting = config_lookup(&configuration, "bindaddr");
134                 if (!setting)
135                         return "";
136                 else {
137                         if ((strsetting = config_setting_get_string(setting)) != NULL)
138                                 return strsetting;
139                         else
140                                 return "";
141                 }
142                 break;
143         case WELCOMETEXT:
144                 setting = config_lookup(&configuration, "welcometext");
145                 if (!setting)
146                         return DEFAULT_WELCOME;
147                 else {
148                         if ((strsetting = config_setting_get_string(setting)) != NULL)
149                                 return strsetting;
150                         else
151                         return DEFAULT_WELCOME;
152                 }
153                 break;
154         case DEFAULT_CHANNEL:
155                 setting = config_lookup(&configuration, "default_channel");
156                 if (!setting)
157                         return "";
158                 else {
159                         if ((strsetting = config_setting_get_string(setting)) != NULL)
160                                 return strsetting;
161                         else
162                         return "";
163                 }
164                 break;
165         case USERNAME:
166                 setting = config_lookup(&configuration, "username");
167                 if (!setting)
168                         return "";
169                 else {
170                         if ((strsetting = config_setting_get_string(setting)) != NULL)
171                                 return strsetting;
172                         else
173                         return "";
174                 }
175                 break;
176         case GROUPNAME:
177                 setting = config_lookup(&configuration, "groupname");
178                 if (!setting)
179                         return "";
180                 else {
181                         if ((strsetting = config_setting_get_string(setting)) != NULL)
182                                 return strsetting;
183                         else
184                         return "";
185                 }
186                 break;
187         case LOGFILE:
188                 setting = config_lookup(&configuration, "logfile");
189                 if (!setting)
190                         return NULL;
191                 else {
192                         if ((strsetting = config_setting_get_string(setting)) != NULL)
193                                 return strsetting;
194                         else
195                         return NULL;
196                 }
197                 break;
198         default:
199                 doAssert(false);
200                 break;
201         }
202         return NULL;
203 }
204
205 int getIntConf(param_t param)
206 {
207         config_setting_t *setting = NULL;
208         
209         switch (param) {
210         case BINDPORT:
211                 setting = config_lookup(&configuration, "bindport");
212                 if (!setting)
213                         return DEFAULT_BINDPORT;
214                 else {
215                         return config_setting_get_int(setting);
216                 }
217                 break;
218         case BAN_LENGTH:
219                 setting = config_lookup(&configuration, "ban_length");
220                 if (!setting)
221                         return DEFAULT_BAN_LENGTH;
222                 else {
223                         return config_setting_get_int(setting);
224                 }
225                 break;
226         case MAX_BANDWIDTH:
227                 setting = config_lookup(&configuration, "max_bandwidth");
228                 if (!setting)
229                         return DEFAULT_MAX_BANDWIDTH;
230                 else {
231                         return config_setting_get_int(setting);
232                 }
233                 break;
234         case MAX_CLIENTS:
235                 setting = config_lookup(&configuration, "max_users");
236                 if (!setting)
237                         return DEFAULT_MAX_CLIENTS;
238                 else {
239                         return config_setting_get_int(setting);
240                 }
241                 break;
242         default:
243                 doAssert(false);
244         }
245 }
246
247 bool_t getBoolConf(param_t param)
248 {
249         config_setting_t *setting = NULL;
250         
251         switch (param) {
252         case ALLOW_TEXTMESSAGE:
253                 setting = config_lookup(&configuration, "allow_textmessage");
254                 if (!setting)
255                         return true;
256                 else
257                         return config_setting_get_bool(setting);
258                 break;
259         case ENABLE_BAN:
260                 setting = config_lookup(&configuration, "enable_ban");
261                 if (!setting)
262                         return false;
263                 else
264                         return config_setting_get_bool(setting);
265                 break;
266         default:
267                 doAssert(false);
268         }
269 }
270
271 int Conf_getNextChannel(conf_channel_t *chdesc, int index)
272 {
273         config_setting_t *setting = NULL;
274         int maxconfig = 64, ret = 0;
275         char configstr[maxconfig];
276         
277         ret = snprintf(configstr, maxconfig, "channels.[%d].name", index);
278         setting = config_lookup(&configuration, configstr);
279         if (ret >= maxconfig || ret < 0 || setting == NULL)
280                 return -1; /* Required */
281         chdesc->name =  config_setting_get_string(setting);
282         
283         ret = snprintf(configstr, maxconfig, "channels.[%d].parent", index);
284         setting = config_lookup(&configuration, configstr);
285         if (ret >= maxconfig || ret < 0 || setting == NULL)
286                 return -1; /* Required */
287         chdesc->parent = config_setting_get_string(setting);
288         
289         ret = snprintf(configstr, maxconfig, "channels.[%d].description", index);
290         setting = config_lookup(&configuration, configstr);
291         if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
292                 chdesc->description = NULL;
293         else
294                 chdesc->description = config_setting_get_string(setting);
295         
296         ret = snprintf(configstr, maxconfig, "channels.[%d].password", index);
297         setting = config_lookup(&configuration, configstr);
298         if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
299                 chdesc->password = NULL;
300         else
301                 chdesc->password = config_setting_get_string(setting);
302         
303         ret = snprintf(configstr, maxconfig, "channels.[%d].noenter", index);
304         setting = config_lookup(&configuration, configstr);
305         if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
306                 chdesc->noenter = false;
307         else
308                 chdesc->noenter = config_setting_get_bool(setting);
309
310         return 0;
311 }
312
313 int Conf_getNextChannelLink(conf_channel_link_t *chlink, int index)
314 {
315         config_setting_t *setting = NULL;
316         int maxconfig = 64, ret = 0;
317         char configstr[maxconfig];
318         
319         ret = snprintf(configstr, maxconfig, "channel_links.[%d].source", index);
320         setting = config_lookup(&configuration, configstr);
321         if (ret >= maxconfig || ret < 0 || setting == NULL)
322                 return -1;
323         chlink->source = config_setting_get_string(setting);
324
325         ret = snprintf(configstr, maxconfig, "channel_links.[%d].destination", index);
326         setting = config_lookup(&configuration, configstr);
327         if (ret >= maxconfig || ret < 0 || setting == NULL)
328                 return -1;
329         chlink->destination = config_setting_get_string(setting);
330
331         return 0;
332 }