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