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

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

2004-03-08 Benoit Gr�goire <bock@…>

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