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