Patch by J Sisson: sprintf -> snprintf
[umurmur.git] / src / conf.c
1 /* Copyright (C) 2009-2010, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2010, 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_CONFIG "/etc/umurmur.conf"
44 #define DEFAULT_WELCOME "Welcome to uMurmur!"
45 #define DEFAULT_MAX_CLIENTS 10
46 #define DEFAULT_MAX_BANDWIDTH 48000
47 #define DEFAULT_BINDPORT 64738
48
49 const char defaultconfig[] = DEFAULT_CONFIG;
50
51 int Conf_init(const char *conffile)
52 {
53         const char *conf;
54         
55         config_init(&configuration);
56         if (conffile == NULL)
57                 conf = defaultconfig;
58         else
59                 conf = conffile;
60         if (config_read_file(&configuration, conf) != CONFIG_TRUE) {
61                 fprintf(stderr, "Error in config file %s: %s at line %d\n", conffile,
62                                 config_error_text(&configuration), config_error_line(&configuration));
63                 exit(1);
64         }
65         return 0;
66 }
67
68 void Conf_deinit()
69 {
70         config_destroy(&configuration);
71 }
72
73 const char *getStrConf(param_t param)
74 {
75         config_setting_t *setting = NULL;
76         const char *strsetting = NULL;
77         
78         switch (param) {
79         case CERTIFICATE:
80                 setting = config_lookup(&configuration, "certificate");
81                 if (!setting)
82                         return "/etc/umurmur/certificate.crt";
83                 else {
84                         if ((strsetting = config_setting_get_string(setting)) != NULL)
85                                 return strsetting;
86                         else
87                                 return "/etc/umurmur/certificate.crt";
88                 }
89                 break;
90         case KEY:
91                 setting = config_lookup(&configuration, "private_key");
92                 if (!setting)
93                         return "/etc/umurmur/private_key.key";
94                 else {
95                         if ((strsetting = config_setting_get_string(setting)) != NULL)
96                                 return strsetting;
97                         else
98                                 return "/etc/umurmur/private_key.key";
99                 }
100                 break;
101         case PASSPHRASE:
102                 setting = config_lookup(&configuration, "password");
103                 if (!setting)
104                         return "";
105                 else {
106                         if ((strsetting = config_setting_get_string(setting)) != NULL)
107                                 return strsetting;
108                         else
109                                 return "";
110                 }
111                 break;
112         case BINDADDR:
113                 setting = config_lookup(&configuration, "bindaddr");
114                 if (!setting)
115                         return "";
116                 else {
117                         if ((strsetting = config_setting_get_string(setting)) != NULL)
118                                 return strsetting;
119                         else
120                                 return "";
121                 }
122                 break;
123         case WELCOMETEXT:
124                 setting = config_lookup(&configuration, "welcometext");
125                 if (!setting)
126                         return DEFAULT_WELCOME;
127                 else {
128                         if ((strsetting = config_setting_get_string(setting)) != NULL)
129                                 return strsetting;
130                         else
131                         return DEFAULT_WELCOME;
132                 }
133                 break;
134         case DEFAULT_CHANNEL:
135                 setting = config_lookup(&configuration, "default_channel");
136                 if (!setting)
137                         return "";
138                 else {
139                         if ((strsetting = config_setting_get_string(setting)) != NULL)
140                                 return strsetting;
141                         else
142                         return "";
143                 }
144                 break;
145         default:
146                 doAssert(false);
147                 break;
148         }
149         return NULL;
150 }
151
152 int getIntConf(param_t param)
153 {
154         config_setting_t *setting = NULL;
155         
156         switch (param) {
157         case BINDPORT:
158                 setting = config_lookup(&configuration, "bindport");
159                 if (!setting)
160                         return DEFAULT_BINDPORT;
161                 else {
162                         return config_setting_get_int(setting);
163                 }
164                 break;
165         case MAX_BANDWIDTH:
166                 setting = config_lookup(&configuration, "max_bandwidth");
167                 if (!setting)
168                         return DEFAULT_MAX_BANDWIDTH;
169                 else {
170                         return config_setting_get_int(setting);
171                 }
172                 break;
173         case MAX_CLIENTS:
174                 setting = config_lookup(&configuration, "max_users");
175                 if (!setting)
176                         return DEFAULT_MAX_CLIENTS;
177                 else {
178                         return config_setting_get_int(setting);
179                 }
180                 break;
181         default:
182                 doAssert(false);
183         }
184 }
185
186 int Conf_getNextChannel(conf_channel_t *chdesc, int index)
187 {
188         config_setting_t *setting = NULL;
189         int maxconfig = 64, ret = 0;
190         char configstr[maxconfig];
191         
192         ret = snprintf(configstr, maxconfig, "channels.[%d].name", index);
193         setting = config_lookup(&configuration, configstr);
194         if (ret >= maxconfig || ret < 0 || setting == NULL)
195                 return -1; /* Required */
196         chdesc->name =  config_setting_get_string(setting);
197         
198         ret = snprintf(configstr, maxconfig, "channels.[%d].parent", index);
199         setting = config_lookup(&configuration, configstr);
200         if (ret >= maxconfig || ret < 0 || setting == NULL)
201                 return -1; /* Required */
202         chdesc->parent = config_setting_get_string(setting);
203         
204         ret = snprintf(configstr, maxconfig, "channels.[%d].description", index);
205         setting = config_lookup(&configuration, configstr);
206         if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
207                 chdesc->description = NULL;
208         else
209                 chdesc->description = config_setting_get_string(setting);
210         
211         ret = snprintf(configstr, maxconfig, "channels.[%d].noenter", index);
212         setting = config_lookup(&configuration, configstr);
213         if (ret >= maxconfig || ret < 0 || setting == NULL) /* Optional */
214                 chdesc->noenter = false;
215         else
216                 chdesc->noenter = config_setting_get_bool(setting);
217
218         return 0;
219 }
220
221 int Conf_getNextChannelLink(conf_channel_link_t *chlink, int index)
222 {
223         config_setting_t *setting = NULL;
224         int maxconfig = 64, ret = 0;
225         char configstr[maxconfig];
226         
227         ret = snprintf(configstr, maxconfig, "channel_links.[%d].source", index);
228         setting = config_lookup(&configuration, configstr);
229         if (ret >= maxconfig || ret < 0 || setting == NULL)
230                 return -1;
231         chlink->source = config_setting_get_string(setting);
232
233         ret = snprintf(configstr, maxconfig, "channel_links.[%d].destination", index);
234         setting = config_lookup(&configuration, configstr);
235         if (ret >= maxconfig || ret < 0 || setting == NULL)
236                 return -1;
237         chlink->destination = config_setting_get_string(setting);
238
239         return 0;
240 }