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

Revision 18, 8.2 KB (checked in by aprilp, 9 years ago)

* Modified the way firewall scripts are called so we can configure
them in the config file (a bit more modular than it was)
* Added simple linked list to keep track of clients and to
keep a counter of the utilization and send it to the auth
server
* Fixed CRLF/formatting in phpauth/auth/index.php
* Hacked phpauth/auth/index.php to handle very basic utilization tracking

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