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

Revision 82, 7.7 KB (checked in by aprilp, 9 years ago)

Fixed firewall scripts to make them standard and some firewall functions

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