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