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