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