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

Revision 39, 7.0 KB (checked in by alexcv, 9 years ago)

formatting

  • 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", 
122                        filename, linenum, cp);
123        return oBadOption;
124}
125
126void
127config_read(char *filename)
128{
129        FILE *fd;
130        char line[MAX_BUF], *s, *p1, *p2;
131        int linenum = 0, opcode, value;
132
133        debug(D_LOG_INFO, "Reading configuration file '%s'", filename);
134
135        if (!(fd = fopen(filename, "r"))) {
136                debug(D_LOG_ERR, "Could not open configuration file '%s', "
137                                "exiting...", filename);
138                exit(1);
139        }
140
141        while (!feof(fd) && fgets(line, MAX_BUF, fd)) {
142                s = line;
143
144                if (s[strlen(s) - 1] == '\n')
145                        s[strlen(s) - 1] = '\0';
146
147                if ((p1 = strchr(s, ' '))) {
148                        p1[0] = '\0';
149                } else if ((p1 = strchr(s, '\t'))) {
150                        p1[0] = '\0';
151                }
152
153                if (p1) {
154                        p1++;
155
156                        if ((p2 = strchr(p1, ' '))) {
157                                p2[0] = '\0';
158                        } else if ((p2 = strstr(p1, "\r\n"))) {
159                                p2[0] = '\0';
160                        } else if ((p2 = strchr(p1, '\n'))) {
161                                p2[0] = '\0';
162                        }
163                }
164
165                if (p1 && p1[0] != '\0') {
166                        /* Strip trailing spaces */
167                        /* Strip tailing spaces */
168
169                        if ((strncmp(s, "#", 1)) != 0) {
170                                debug(D_LOG_DEBUG, "Parsing token: %s, "
171                                                "value: %s", s, p1);
172                                opcode = parse_token(s, filename, linenum);
173
174                                switch(opcode) {
175                                case oDaemon:
176                                        if (((value = parse_value(p1)) != -1)) {
177                                                config.daemon = value;
178                                        }
179                                        break;
180                                case oGatewayID:
181                                        config.gw_id = get_string(p1);
182                                        break;
183                                case oGatewayInterface:
184                                        config.gw_interface = get_string(p1);
185                                        break;
186                                case oGatewayAddress:
187                                        config.gw_address = get_string(p1);
188                                        break;
189                                case oGatewayPort:
190                                        sscanf(p1, "%d", &config.gw_port);
191                                        break;
192                                case oAuthservHostname:
193                                        config.authserv_hostname = 
194                                                get_string(p1);
195                                        break;
196                                case oHTTPDName:
197                                        config.httpdname = get_string(p1);
198                                        break;
199                                case oHTTPDMaxConn:
200                                        sscanf(p1, "%d", &config.httpdmaxconn);
201                                        break;
202                                case oAuthservPath:
203                                        config.authserv_path = get_string(p1);
204                                        break;
205                                case oAuthservLoginUrl:
206                                        config.authserv_loginurl = 
207                                                get_string(p1);
208                                        break;
209                                case oBadOption:
210                                        exit(-1);
211                                        break;
212                                case oCheckInterval:
213                                        sscanf(p1, "%d", &config.checkinterval);
214                                        break;
215                                case oClientTimeout:
216                                        sscanf(p1, "%d", &config.clienttimeout);
217                                        break;
218                                }
219                        }
220                }
221        }
222
223        fclose(fd);
224}
225
226int
227parse_value(char *line)
228{
229        if (strcasecmp(line, "yes") == 0) {
230                return 1;
231        }
232        if (strcasecmp(line, "no") == 0) {
233                return 0;
234        }
235        if (strcmp(line, "1") == 0) {
236                return 1;
237        }
238        if (strcmp(line, "0") == 0) {
239                return 0;
240        }
241
242        return -1;
243}
244
245        char *
246get_string(char *ptr)
247{
248        char *buf;
249
250        buf = (char *)malloc(MAX_BUF);
251
252        strncpy(buf, ptr, MAX_BUF);
253        return buf;
254}
255
256void
257config_validate(void)
258{
259        config_notnull(config.gw_id, "GatewayID");
260        config_notnull(config.gw_interface, "GatewayInterface");
261        config_notnull(config.gw_address, "GatewayAddress");
262        config_notnull(config.authserv_hostname, "AuthservHostname");
263        config_notnull(config.authserv_path, "AuthservPath");
264        config_notnull(config.authserv_loginurl, "AuthservLoginUrl");
265
266        if (missing_parms) {
267                debug(D_LOG_ERR, "Configuration is not complete, exiting...");
268                exit(-1);
269        }
270}
271
272void
273config_notnull(void *parm, char *parmname)
274{
275        if (parm == NULL) {
276                debug(D_LOG_ERR, "%s is not set", parmname);
277                missing_parms = 1;
278        }
279}
280
Note: See TracBrowser for help on using the browser.