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

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

2004-05-27 Benoit Gr�goire <bock@…>

  • Massive Doxygen update in all files. IMPORTANT: The new convention is: @brief in the .h, long description and parameters in the .c
  • Cleaned up some more issues in my notes taken at the formal review
  • client_list.c,h: Make client_list_free_node() private, define and document client_list_mutex here
  • config.c: Start the hunt for evil globals: Get rid of the config global
  • doc/doxygen.cfg.in: Enable generation of internal doc, a few other tweaks
  • Documentation now generates a TODO list and DEPRECATED list, please look at them
  • 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/** @file conf.c
23  @brief Config file parsing
24  @author Copyright (C) 2004 Philippe April <papril777@yahoo.com>
25 */
26
27#define _GNU_SOURCE
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <syslog.h>
32
33#include <string.h>
34
35#include "common.h"
36
37#include "debug.h"
38#include "conf.h"
39#include "http.h"
40#include "auth.h"
41
42/** @internal
43 * Holds the current configuration of the gateway */
44static s_config config;
45
46/** @internal
47 * A flag.  If set to 1, there are missing or empty mandatory parameters in the config
48 */
49static int missing_parms;
50
51/** @internal
52 The different configuration options */
53typedef enum {
54        oBadOption,
55        oDaemon,
56        oDebugLevel,
57        oExternalInterface,
58        oGatewayID,
59        oGatewayInterface,
60        oGatewayAddress,
61        oGatewayPort,
62        oAuthservHostname,
63        oAuthservPort,
64        oAuthservPath,
65        oAuthservLoginUrl,
66        oHTTPDMaxConn,
67        oHTTPDName,
68        oClientTimeout,
69        oCheckInterval,
70        oSyslogFacility
71} OpCodes;
72
73/** @internal
74 The config file keywords for the different configuration options */
75static const struct {
76        const char *name;
77        OpCodes opcode;
78        int required;
79} keywords[] = {
80        { "daemon",             oDaemon },
81        { "debuglevel",         oDebugLevel },
82        { "externalinterface",  oExternalInterface },
83        { "gatewayid",          oGatewayID },
84        { "gatewayinterface",   oGatewayInterface },
85        { "gatewayaddress",     oGatewayAddress },
86        { "gatewayport",        oGatewayPort },
87        { "authservhostname",   oAuthservHostname },
88        { "authservport",       oAuthservPort },
89        { "authservpath",       oAuthservPath },
90        { "authservloginurl",   oAuthservLoginUrl },
91        { "httpdmaxconn",       oHTTPDMaxConn },
92        { "httpdname",          oHTTPDName },
93        { "clienttimeout",      oClientTimeout },
94        { "checkinterval",      oCheckInterval },
95        { "syslogfacility",     oSyslogFacility },
96        { NULL,                 oBadOption },
97};
98
99static void config_notnull(void *parm, char *parmname);
100static int parse_boolean_value(char *);
101
102/** Accessor for the current gateway configuration
103@return:  A pointer to the current config.  The pointer isn't opaque, but should be treated as READ-ONLY
104 */
105s_config *
106config_get_config(void)
107{
108    return &config;
109}
110
111/** Sets the default config parameters and initialises the configuration system */
112void
113config_init(void)
114{
115        debug(LOG_DEBUG, "Setting default config parameters");
116        strncpy(config.configfile, DEFAULT_CONFIGFILE, sizeof(config.configfile));
117        config.debuglevel = DEFAULT_DEBUGLEVEL;
118        config.httpdmaxconn = DEFAULT_HTTPDMAXCONN;
119        config.external_interface = NULL;
120        config.gw_id = DEFAULT_GATEWAYID;
121        config.gw_interface = NULL;
122        config.gw_address = NULL;
123        config.gw_port = DEFAULT_GATEWAYPORT;
124        config.authserv_hostname = NULL;
125        config.authserv_port = DEFAULT_AUTHSERVPORT;
126        config.authserv_path = NULL;
127        config.authserv_loginurl = NULL;
128        config.httpdname = NULL;
129        config.clienttimeout = DEFAULT_CLIENTTIMEOUT;
130        config.checkinterval = DEFAULT_CHECKINTERVAL;
131        config.syslog_facility = DEFAULT_SYSLOG_FACILITY;
132        config.daemon = -1;
133        config.log_syslog = DEFAULT_LOG_SYSLOG;
134}
135
136/**
137 * If the command-line didn't provide a config, use the default.
138 */
139void
140config_init_override(void)
141{
142    if (config.daemon == -1) config.daemon = DEFAULT_DAEMON;
143}
144
145/** @internal
146Parses a single token from the config file
147*/
148static OpCodes
149config_parse_token(const char *cp, const char *filename, int linenum)
150{
151        int i;
152
153        for (i = 0; keywords[i].name; i++)
154                if (strcasecmp(cp, keywords[i].name) == 0)
155                        return keywords[i].opcode;
156
157        debug(LOG_ERR, "%s: line %d: Bad configuration option: %s", 
158                        filename, linenum, cp);
159        return oBadOption;
160}
161
162/**
163@param filename Full path of the configuration file to be read
164*/
165void
166config_read(char *filename)
167{
168        FILE *fd;
169        char line[MAX_BUF], *s, *p1, *p2;
170        int linenum = 0, opcode, value;
171
172        debug(LOG_INFO, "Reading configuration file '%s'", filename);
173
174        if (!(fd = fopen(filename, "r"))) {
175                debug(LOG_ERR, "Could not open configuration file '%s', "
176                                "exiting...", filename);
177                exit(1);
178        }
179
180        while (!feof(fd) && fgets(line, MAX_BUF, fd)) {
181                s = line;
182
183                if (s[strlen(s) - 1] == '\n')
184                        s[strlen(s) - 1] = '\0';
185
186                if ((p1 = strchr(s, ' '))) {
187                        p1[0] = '\0';
188                } else if ((p1 = strchr(s, '\t'))) {
189                        p1[0] = '\0';
190                }
191
192                if (p1) {
193                        p1++;
194
195                        if ((p2 = strchr(p1, ' '))) {
196                                p2[0] = '\0';
197                        } else if ((p2 = strstr(p1, "\r\n"))) {
198                                p2[0] = '\0';
199                        } else if ((p2 = strchr(p1, '\n'))) {
200                                p2[0] = '\0';
201                        }
202                }
203
204                if (p1 && p1[0] != '\0') {
205                        /* Strip trailing spaces */
206                        /* Strip tailing spaces */
207
208                        if ((strncmp(s, "#", 1)) != 0) {
209                                debug(LOG_DEBUG, "Parsing token: %s, "
210                                                "value: %s", s, p1);
211                                opcode = config_parse_token(s, filename, linenum);
212
213                                switch(opcode) {
214                                case oDaemon:
215                                        if (config.daemon == -1 && ((value = parse_boolean_value(p1)) != -1)) {
216                                                config.daemon = value;
217                                        }
218                                        break;
219                                case oExternalInterface:
220                                        config.external_interface = strdup(p1);
221                                        break;
222                                case oGatewayID:
223                                        config.gw_id = strdup(p1);
224                                        break;
225                                case oGatewayInterface:
226                                        config.gw_interface = strdup(p1);
227                                        break;
228                                case oGatewayAddress:
229                                        config.gw_address = strdup(p1);
230                                        break;
231                                case oGatewayPort:
232                                        sscanf(p1, "%d", &config.gw_port);
233                                        break;
234                                case oAuthservHostname:
235                                        config.authserv_hostname = 
236                                                strdup(p1);
237                                        break;
238                                case oHTTPDName:
239                                        config.httpdname = strdup(p1);
240                                        break;
241                                case oHTTPDMaxConn:
242                                        sscanf(p1, "%d", &config.httpdmaxconn);
243                                        break;
244                                case oAuthservPath:
245                                        config.authserv_path = strdup(p1);
246                                        break;
247                                case oAuthservLoginUrl:
248                                        config.authserv_loginurl = strdup(p1);
249                                        break;
250                                case oBadOption:
251                    debug(LOG_ERR, "Exiting...");
252                                        exit(-1);
253                                        break;
254                                case oCheckInterval:
255                                        sscanf(p1, "%d", &config.checkinterval);
256                                        break;
257                                case oClientTimeout:
258                                        sscanf(p1, "%d", &config.clienttimeout);
259                                        break;
260                case oSyslogFacility:
261                                        sscanf(p1, "%d", &config.syslog_facility);
262                                        break;
263                                }
264                        }
265                }
266        }
267
268        fclose(fd);
269}
270
271/** @internal
272Parses a boolean value from the config file
273*/
274static int
275parse_boolean_value(char *line)
276{
277        if (strcasecmp(line, "yes") == 0) {
278                return 1;
279        }
280        if (strcasecmp(line, "no") == 0) {
281                return 0;
282        }
283        if (strcmp(line, "1") == 0) {
284                return 1;
285        }
286        if (strcmp(line, "0") == 0) {
287                return 0;
288        }
289
290        return -1;
291}
292
293/** Verifies if the configuration is complete and valid.  Terminates the program if it isn't */
294void
295config_validate(void)
296{
297        config_notnull(config.external_interface, "ExternalInterface");
298        config_notnull(config.gw_id, "GatewayID");
299        config_notnull(config.gw_interface, "GatewayInterface");
300        config_notnull(config.gw_address, "GatewayAddress");
301        config_notnull(config.authserv_hostname, "AuthservHostname");
302        config_notnull(config.authserv_path, "AuthservPath");
303        config_notnull(config.authserv_loginurl, "AuthservLoginUrl");
304
305        if (missing_parms) {
306                debug(LOG_ERR, "Configuration is not complete, exiting...");
307                exit(-1);
308        }
309}
310
311/** @internal
312    Verifies that a required parameter is not a null pointer
313*/
314static void
315config_notnull(void *parm, char *parmname)
316{
317        if (parm == NULL) {
318                debug(LOG_ERR, "%s is not set", parmname);
319                missing_parms = 1;
320        }
321}
322
Note: See TracBrowser for help on using the browser.