root/trunk/wifidog/src/conf.c @ 133

Revision 133, 8.0 KB (checked in by aprilp, 9 years ago)

Fixed some static statements

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/********************************************************************\
2 * This program is free software; you can redistribute it and/or    *
3 * modify it under the terms of the GNU General Public License as   *
4 * published by the Free Software Foundation; either version 2 of   *
5 * the License, or (at your option) any later version.              *
6 *                                                                  *
7 * This program is distributed in the hope that it will be useful,  *
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
10 * GNU General Public License for more details.                     *
11 *                                                                  *
12 * You should have received a copy of the GNU General Public License*
13 * along with this program; if not, contact:                        *
14 *                                                                  *
15 * Free Software Foundation           Voice:  +1-617-542-5942       *
16 * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
17 * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
18 *                                                                  *
19 \********************************************************************/
20
21/* $Header$ */
22/** @internal
23  @file conf.c
24  @brief Config file parsing
25  @author Copyright (C) 2004 Philippe April <papril777@yahoo.com>
26 */
27
28#define _GNU_SOURCE
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <syslog.h>
33
34#include <string.h>
35
36#include "common.h"
37
38#include "debug.h"
39#include "conf.h"
40#include "http.h"
41#include "auth.h"
42
43static void config_notnull(void *parm, char *parmname);
44static int parse_value(char *);
45static char *get_string(char *ptr);
46
47#define DEFAULT_CONFIGFILE "/etc/wifidog.conf"
48#define DEFAULT_DAEMON 1
49#define DEFAULT_DEBUGLEVEL LOG_INFO
50#define DEFAULT_HTTPDMAXCONN 10
51#define DEFAULT_GATEWAYID "default"
52#define DEFAULT_GATEWAYPORT 2060
53#define DEFAULT_AUTHSERVPORT 80
54#define DEFAULT_HTTPDNAME "WiFiDog"
55#define DEFAULT_CLIENTTIMEOUT 5
56#define DEFAULT_CHECKINTERVAL 5
57#define DEFAULT_LOG_SYSLOG 0
58#define DEFAULT_SYSLOG_FACILITY LOG_DAEMON
59
60s_config config;
61int missing_parms;
62
63typedef enum {
64        oBadOption,
65        oDaemon,
66        oDebugLevel,
67        oExternalInterface,
68        oGatewayID,
69        oGatewayInterface,
70        oGatewayAddress,
71        oGatewayPort,
72        oAuthservHostname,
73        oAuthservPort,
74        oAuthservPath,
75        oAuthservLoginUrl,
76        oHTTPDMaxConn,
77        oHTTPDName,
78        oClientTimeout,
79        oCheckInterval,
80    oSyslogFacility
81} OpCodes;
82
83static struct {
84        const char *name;
85        OpCodes opcode;
86        int required;
87} keywords[] = {
88        { "daemon",             oDaemon },
89        { "debuglevel",         oDebugLevel },
90        { "externalinterface",  oExternalInterface },
91        { "gatewayid",          oGatewayID },
92        { "gatewayinterface",   oGatewayInterface },
93        { "gatewayaddress",     oGatewayAddress },
94        { "gatewayport",        oGatewayPort },
95        { "authservhostname",   oAuthservHostname },
96        { "authservport",       oAuthservPort },
97        { "authservpath",       oAuthservPath },
98        { "authservloginurl",   oAuthservLoginUrl },
99        { "httpdmaxconn",       oHTTPDMaxConn },
100        { "httpdname",          oHTTPDName },
101        { "clienttimeout",      oClientTimeout },
102        { "checkinterval",      oCheckInterval },
103        { "syslogfacility",     oSyslogFacility },
104        { NULL,                 oBadOption },
105};
106
107void
108config_init(void)
109{
110        debug(LOG_DEBUG, "Setting default config parameters");
111        config.configfile = (char *)malloc(255);
112        strcpy(config.configfile, DEFAULT_CONFIGFILE);
113        config.debuglevel = DEFAULT_DEBUGLEVEL;
114        config.httpdmaxconn = DEFAULT_HTTPDMAXCONN;
115        config.external_interface = NULL;
116        config.gw_id = DEFAULT_GATEWAYID;
117        config.gw_interface = NULL;
118        config.gw_address = NULL;
119        config.gw_port = DEFAULT_GATEWAYPORT;
120        config.authserv_hostname = NULL;
121        config.authserv_port = DEFAULT_AUTHSERVPORT;
122        config.authserv_path = NULL;
123        config.authserv_loginurl = NULL;
124        config.httpdname = NULL;
125        config.clienttimeout = DEFAULT_CLIENTTIMEOUT;
126        config.checkinterval = DEFAULT_CHECKINTERVAL;
127        config.syslog_facility = DEFAULT_SYSLOG_FACILITY;
128    config.daemon = -1;
129    config.log_syslog = DEFAULT_LOG_SYSLOG;
130}
131
132/**
133 * @brief Initialize the variables we override with the command line
134 *
135 *
136 * Initialize the variables we override with the command line after the config has been read
137 * if they haven't been initialized by the configuration file
138 */
139void
140config_init_override(void)
141{
142    if (!config.daemon) config.daemon = DEFAULT_DAEMON;
143}
144
145static OpCodes
146parse_token(const char *cp, const char *filename, int linenum)
147{
148        int i;
149
150        for (i = 0; keywords[i].name; i++)
151                if (strcasecmp(cp, keywords[i].name) == 0)
152                        return keywords[i].opcode;
153
154        debug(LOG_ERR, "%s: line %d: Bad configuration option: %s", 
155                        filename, linenum, cp);
156        return oBadOption;
157}
158
159void
160config_read(char *filename)
161{
162        FILE *fd;
163        char line[MAX_BUF], *s, *p1, *p2;
164        int linenum = 0, opcode, value;
165
166        debug(LOG_INFO, "Reading configuration file '%s'", filename);
167
168        if (!(fd = fopen(filename, "r"))) {
169                debug(LOG_ERR, "Could not open configuration file '%s', "
170                                "exiting...", filename);
171                exit(1);
172        }
173
174        while (!feof(fd) && fgets(line, MAX_BUF, fd)) {
175                s = line;
176
177                if (s[strlen(s) - 1] == '\n')
178                        s[strlen(s) - 1] = '\0';
179
180                if ((p1 = strchr(s, ' '))) {
181                        p1[0] = '\0';
182                } else if ((p1 = strchr(s, '\t'))) {
183                        p1[0] = '\0';
184                }
185
186                if (p1) {
187                        p1++;
188
189                        if ((p2 = strchr(p1, ' '))) {
190                                p2[0] = '\0';
191                        } else if ((p2 = strstr(p1, "\r\n"))) {
192                                p2[0] = '\0';
193                        } else if ((p2 = strchr(p1, '\n'))) {
194                                p2[0] = '\0';
195                        }
196                }
197
198                if (p1 && p1[0] != '\0') {
199                        /* Strip trailing spaces */
200                        /* Strip tailing spaces */
201
202                        if ((strncmp(s, "#", 1)) != 0) {
203                                debug(LOG_DEBUG, "Parsing token: %s, "
204                                                "value: %s", s, p1);
205                                opcode = parse_token(s, filename, linenum);
206
207                                switch(opcode) {
208                                case oDaemon:
209                                        if (config.daemon == -1 && ((value = parse_value(p1)) != -1)) {
210                                                config.daemon = value;
211                                        }
212                                        break;
213                                case oExternalInterface:
214                                        config.external_interface = get_string(p1);
215                                        break;
216                                case oGatewayID:
217                                        config.gw_id = get_string(p1);
218                                        break;
219                                case oGatewayInterface:
220                                        config.gw_interface = get_string(p1);
221                                        break;
222                                case oGatewayAddress:
223                                        config.gw_address = get_string(p1);
224                                        break;
225                                case oGatewayPort:
226                                        sscanf(p1, "%d", &config.gw_port);
227                                        break;
228                                case oAuthservHostname:
229                                        config.authserv_hostname = 
230                                                get_string(p1);
231                                        break;
232                                case oHTTPDName:
233                                        config.httpdname = get_string(p1);
234                                        break;
235                                case oHTTPDMaxConn:
236                                        sscanf(p1, "%d", &config.httpdmaxconn);
237                                        break;
238                                case oAuthservPath:
239                                        config.authserv_path = get_string(p1);
240                                        break;
241                                case oAuthservLoginUrl:
242                                        config.authserv_loginurl = 
243                                                get_string(p1);
244                                        break;
245                                case oBadOption:
246                    debug(LOG_ERR, "Exiting...");
247                                        exit(-1);
248                                        break;
249                                case oCheckInterval:
250                                        sscanf(p1, "%d", &config.checkinterval);
251                                        break;
252                                case oClientTimeout:
253                                        sscanf(p1, "%d", &config.clienttimeout);
254                                        break;
255                case oSyslogFacility:
256                                        sscanf(p1, "%d", &config.syslog_facility);
257                                        break;
258                                }
259                        }
260                }
261        }
262
263        fclose(fd);
264}
265
266static int
267parse_value(char *line)
268{
269        if (strcasecmp(line, "yes") == 0) {
270                return 1;
271        }
272        if (strcasecmp(line, "no") == 0) {
273                return 0;
274        }
275        if (strcmp(line, "1") == 0) {
276                return 1;
277        }
278        if (strcmp(line, "0") == 0) {
279                return 0;
280        }
281
282        return -1;
283}
284
285static char *
286get_string(char *ptr)
287{
288        char *buf;
289
290        buf = strdup(ptr);
291        return buf;
292}
293
294void
295config_validate(void)
296{
297        config_notnull(config.external_interface, "ExternalInterface");
298        config_notnull(config.gw_id, "GatewayID");
299        config_notnull(config.gw_interface, "GatewayInterface");
300        config_notnull(config.gw_address, "GatewayAddress");
301        config_notnull(config.authserv_hostname, "AuthservHostname");
302        config_notnull(config.authserv_path, "AuthservPath");
303        config_notnull(config.authserv_loginurl, "AuthservLoginUrl");
304
305        if (missing_parms) {
306                debug(LOG_ERR, "Configuration is not complete, exiting...");
307                exit(-1);
308        }
309}
310
311static void
312config_notnull(void *parm, char *parmname)
313{
314        if (parm == NULL) {
315                debug(LOG_ERR, "%s is not set", parmname);
316                missing_parms = 1;
317        }
318}
319
Note: See TracBrowser for help on using the browser.