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

Revision 135, 7.5 KB (checked in by alexcv, 9 years ago)

UNTESTED!!! Massive update based on the code review wed. It compiles...

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