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

Revision 120, 7.9 KB (checked in by aprilp, 9 years ago)

* New parameter ExternalInterface?
* Made possible to count inbound traffic by inserting new rules

  • 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
43#define DEFAULT_CONFIGFILE "/etc/wifidog.conf"
44#define DEFAULT_DAEMON 1
45#define DEFAULT_DEBUGLEVEL LOG_INFO
46#define DEFAULT_HTTPDMAXCONN 10
47#define DEFAULT_GATEWAYID "default"
48#define DEFAULT_GATEWAYPORT 2060
49#define DEFAULT_AUTHSERVPORT 80
50#define DEFAULT_HTTPDNAME "WiFiDog"
51#define DEFAULT_CLIENTTIMEOUT 5
52#define DEFAULT_CHECKINTERVAL 5
53#define DEFAULT_LOG_SYSLOG 0
54#define DEFAULT_SYSLOG_FACILITY LOG_DAEMON
55
56s_config config;
57int missing_parms;
58
59typedef enum {
60        oBadOption,
61        oDaemon,
62        oDebugLevel,
63        oExternalInterface,
64        oGatewayID,
65        oGatewayInterface,
66        oGatewayAddress,
67        oGatewayPort,
68        oAuthservHostname,
69        oAuthservPort,
70        oAuthservPath,
71        oAuthservLoginUrl,
72        oHTTPDMaxConn,
73        oHTTPDName,
74        oClientTimeout,
75        oCheckInterval,
76    oSyslogFacility
77} OpCodes;
78
79struct {
80        const char *name;
81        OpCodes opcode;
82        int required;
83} keywords[] = {
84        { "daemon",             oDaemon },
85        { "debuglevel",         oDebugLevel },
86        { "externalinterface",  oExternalInterface },
87        { "gatewayid",          oGatewayID },
88        { "gatewayinterface",   oGatewayInterface },
89        { "gatewayaddress",     oGatewayAddress },
90        { "gatewayport",        oGatewayPort },
91        { "authservhostname",   oAuthservHostname },
92        { "authservport",       oAuthservPort },
93        { "authservpath",       oAuthservPath },
94        { "authservloginurl",   oAuthservLoginUrl },
95        { "httpdmaxconn",       oHTTPDMaxConn },
96        { "httpdname",          oHTTPDName },
97        { "clienttimeout",      oClientTimeout },
98        { "checkinterval",      oCheckInterval },
99        { "syslogfacility",     oSyslogFacility },
100        { NULL,                 oBadOption },
101};
102
103void
104config_init(void)
105{
106        debug(LOG_DEBUG, "Setting default config parameters");
107        config.configfile = (char *)malloc(255);
108        strcpy(config.configfile, DEFAULT_CONFIGFILE);
109        config.debuglevel = DEFAULT_DEBUGLEVEL;
110        config.httpdmaxconn = DEFAULT_HTTPDMAXCONN;
111        config.external_interface = NULL;
112        config.gw_id = DEFAULT_GATEWAYID;
113        config.gw_interface = NULL;
114        config.gw_address = NULL;
115        config.gw_port = DEFAULT_GATEWAYPORT;
116        config.authserv_hostname = NULL;
117        config.authserv_port = DEFAULT_AUTHSERVPORT;
118        config.authserv_path = NULL;
119        config.authserv_loginurl = NULL;
120        config.httpdname = NULL;
121        config.clienttimeout = DEFAULT_CLIENTTIMEOUT;
122        config.checkinterval = DEFAULT_CHECKINTERVAL;
123        config.syslog_facility = DEFAULT_SYSLOG_FACILITY;
124    config.daemon = -1;
125    config.log_syslog = DEFAULT_LOG_SYSLOG;
126}
127
128/**
129 * @brief Initialize the variables we override with the command line
130 *
131 *
132 * Initialize the variables we override with the command line after the config has been read
133 * if they haven't been initialized by the configuration file
134 */
135void
136config_init_override(void)
137{
138    if (!config.daemon) config.daemon = DEFAULT_DAEMON;
139}
140
141OpCodes
142parse_token(const char *cp, const char *filename, int linenum)
143{
144        int i;
145
146        for (i = 0; keywords[i].name; i++)
147                if (strcasecmp(cp, keywords[i].name) == 0)
148                        return keywords[i].opcode;
149
150        debug(LOG_ERR, "%s: line %d: Bad configuration option: %s", 
151                        filename, linenum, cp);
152        return oBadOption;
153}
154
155void
156config_read(char *filename)
157{
158        FILE *fd;
159        char line[MAX_BUF], *s, *p1, *p2;
160        int linenum = 0, opcode, value;
161
162        debug(LOG_INFO, "Reading configuration file '%s'", filename);
163
164        if (!(fd = fopen(filename, "r"))) {
165                debug(LOG_ERR, "Could not open configuration file '%s', "
166                                "exiting...", filename);
167                exit(1);
168        }
169
170        while (!feof(fd) && fgets(line, MAX_BUF, fd)) {
171                s = line;
172
173                if (s[strlen(s) - 1] == '\n')
174                        s[strlen(s) - 1] = '\0';
175
176                if ((p1 = strchr(s, ' '))) {
177                        p1[0] = '\0';
178                } else if ((p1 = strchr(s, '\t'))) {
179                        p1[0] = '\0';
180                }
181
182                if (p1) {
183                        p1++;
184
185                        if ((p2 = strchr(p1, ' '))) {
186                                p2[0] = '\0';
187                        } else if ((p2 = strstr(p1, "\r\n"))) {
188                                p2[0] = '\0';
189                        } else if ((p2 = strchr(p1, '\n'))) {
190                                p2[0] = '\0';
191                        }
192                }
193
194                if (p1 && p1[0] != '\0') {
195                        /* Strip trailing spaces */
196                        /* Strip tailing spaces */
197
198                        if ((strncmp(s, "#", 1)) != 0) {
199                                debug(LOG_DEBUG, "Parsing token: %s, "
200                                                "value: %s", s, p1);
201                                opcode = parse_token(s, filename, linenum);
202
203                                switch(opcode) {
204                                case oDaemon:
205                                        if (config.daemon == -1 && ((value = parse_value(p1)) != -1)) {
206                                                config.daemon = value;
207                                        }
208                                        break;
209                                case oExternalInterface:
210                                        config.external_interface = get_string(p1);
211                                        break;
212                                case oGatewayID:
213                                        config.gw_id = get_string(p1);
214                                        break;
215                                case oGatewayInterface:
216                                        config.gw_interface = get_string(p1);
217                                        break;
218                                case oGatewayAddress:
219                                        config.gw_address = get_string(p1);
220                                        break;
221                                case oGatewayPort:
222                                        sscanf(p1, "%d", &config.gw_port);
223                                        break;
224                                case oAuthservHostname:
225                                        config.authserv_hostname = 
226                                                get_string(p1);
227                                        break;
228                                case oHTTPDName:
229                                        config.httpdname = get_string(p1);
230                                        break;
231                                case oHTTPDMaxConn:
232                                        sscanf(p1, "%d", &config.httpdmaxconn);
233                                        break;
234                                case oAuthservPath:
235                                        config.authserv_path = get_string(p1);
236                                        break;
237                                case oAuthservLoginUrl:
238                                        config.authserv_loginurl = 
239                                                get_string(p1);
240                                        break;
241                                case oBadOption:
242                    debug(LOG_ERR, "Exiting...");
243                                        exit(-1);
244                                        break;
245                                case oCheckInterval:
246                                        sscanf(p1, "%d", &config.checkinterval);
247                                        break;
248                                case oClientTimeout:
249                                        sscanf(p1, "%d", &config.clienttimeout);
250                                        break;
251                case oSyslogFacility:
252                                        sscanf(p1, "%d", &config.syslog_facility);
253                                        break;
254                                }
255                        }
256                }
257        }
258
259        fclose(fd);
260}
261
262int
263parse_value(char *line)
264{
265        if (strcasecmp(line, "yes") == 0) {
266                return 1;
267        }
268        if (strcasecmp(line, "no") == 0) {
269                return 0;
270        }
271        if (strcmp(line, "1") == 0) {
272                return 1;
273        }
274        if (strcmp(line, "0") == 0) {
275                return 0;
276        }
277
278        return -1;
279}
280
281char *
282get_string(char *ptr)
283{
284        char *buf;
285
286        buf = strdup(ptr);
287        return buf;
288}
289
290void
291config_validate(void)
292{
293        config_notnull(config.external_interface, "ExternalInterface");
294        config_notnull(config.gw_id, "GatewayID");
295        config_notnull(config.gw_interface, "GatewayInterface");
296        config_notnull(config.gw_address, "GatewayAddress");
297        config_notnull(config.authserv_hostname, "AuthservHostname");
298        config_notnull(config.authserv_path, "AuthservPath");
299        config_notnull(config.authserv_loginurl, "AuthservLoginUrl");
300
301        if (missing_parms) {
302                debug(LOG_ERR, "Configuration is not complete, exiting...");
303                exit(-1);
304        }
305}
306
307void
308config_notnull(void *parm, char *parmname)
309{
310        if (parm == NULL) {
311                debug(LOG_ERR, "%s is not set", parmname);
312                missing_parms = 1;
313        }
314}
315
Note: See TracBrowser for help on using the browser.