📄 conf.c.svn-base
字号:
/********************************************************************\ * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 2 of * * the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License* * along with this program; if not, contact: * * * * Free Software Foundation Voice: +1-617-542-5942 * * 59 Temple Place - Suite 330 Fax: +1-617-542-2652 * * Boston, MA 02111-1307, USA gnu@gnu.org * * * \********************************************************************//* $Id$ *//** @file conf.c @brief Config file parsing @author Copyright (C) 2004 Philippe April <papril777@yahoo.com> @author Copyright (C) 2007 Benoit Grégoire, Technologies Coeus inc. */#define _GNU_SOURCE#include <stdio.h>#include <stdlib.h>#include <syslog.h>#include <pthread.h>#include <string.h>#include <ctype.h>#include "common.h"#include "safe.h"#include "debug.h"#include "conf.h"#include "http.h"#include "auth.h"#include "firewall.h"#include "util.h"/** @internal * Holds the current configuration of the gateway */static s_config config;/** * Mutex for the configuration file, used by the auth_servers related * functions. */pthread_mutex_t config_mutex = PTHREAD_MUTEX_INITIALIZER;/** @internal * A flag. If set to 1, there are missing or empty mandatory parameters in the config */static int missing_parms;/** @internal The different configuration options */typedef enum { oBadOption, oDaemon, oDebugLevel, oExternalInterface, oGatewayID, oGatewayInterface, oGatewayAddress, oGatewayPort, oAuthServer, oAuthServHostname, oAuthServSSLAvailable, oAuthServSSLPort, oAuthServHTTPPort, oAuthServPath, oAuthServLoginScriptPathFragment, oAuthServPortalScriptPathFragment, oAuthServMsgScriptPathFragment, oAuthServPingScriptPathFragment, oAuthServAuthScriptPathFragment, oHTTPDMaxConn, oHTTPDName, oClientTimeout, oCheckInterval, oWdctlSocket, oSyslogFacility, oFirewallRule, oFirewallRuleSet, oTrustedMACList} OpCodes;/** @internal The config file keywords for the different configuration options */static const struct { const char *name; OpCodes opcode; int required;} keywords[] = { { "daemon", oDaemon }, { "debuglevel", oDebugLevel }, { "externalinterface", oExternalInterface }, { "gatewayid", oGatewayID }, { "gatewayinterface", oGatewayInterface }, { "gatewayaddress", oGatewayAddress }, { "gatewayport", oGatewayPort }, { "authserver", oAuthServer }, { "httpdmaxconn", oHTTPDMaxConn }, { "httpdname", oHTTPDName }, { "clienttimeout", oClientTimeout }, { "checkinterval", oCheckInterval }, { "syslogfacility", oSyslogFacility }, { "wdctlsocket", oWdctlSocket }, { "hostname", oAuthServHostname }, { "sslavailable", oAuthServSSLAvailable }, { "sslport", oAuthServSSLPort }, { "httpport", oAuthServHTTPPort }, { "path", oAuthServPath }, { "loginscriptpathfragment", oAuthServLoginScriptPathFragment }, { "portalscriptpathfragment", oAuthServPortalScriptPathFragment }, { "msgscriptpathfragment", oAuthServMsgScriptPathFragment }, { "pingscriptpathfragment", oAuthServPingScriptPathFragment }, { "authscriptpathfragment", oAuthServAuthScriptPathFragment }, { "firewallruleset", oFirewallRuleSet }, { "firewallrule", oFirewallRule }, { "trustedmaclist", oTrustedMACList }, { NULL, oBadOption },};static OpCodes config_parse_token(const char *cp, const char *filename, int linenum);/** Accessor for the current gateway configuration@return: A pointer to the current config. The pointer isn't opaque, but should be treated as READ-ONLY */s_config *config_get_config(void){ return &config;}/** Sets the default config parameters and initialises the configuration system */voidconfig_init(void){ debug(LOG_DEBUG, "Setting default config parameters"); strncpy(config.configfile, DEFAULT_CONFIGFILE, sizeof(config.configfile)); config.debuglevel = DEFAULT_DEBUGLEVEL; config.httpdmaxconn = DEFAULT_HTTPDMAXCONN; config.external_interface = NULL; config.gw_id = DEFAULT_GATEWAYID; config.gw_interface = NULL; config.gw_address = NULL; config.gw_port = DEFAULT_GATEWAYPORT; config.auth_servers = NULL; config.httpdname = NULL; config.clienttimeout = DEFAULT_CLIENTTIMEOUT; config.checkinterval = DEFAULT_CHECKINTERVAL; config.syslog_facility = DEFAULT_SYSLOG_FACILITY; config.daemon = -1; config.log_syslog = DEFAULT_LOG_SYSLOG; config.wdctl_sock = safe_strdup(DEFAULT_WDCTL_SOCK); config.internal_sock = safe_strdup(DEFAULT_INTERNAL_SOCK); config.rulesets = NULL; config.trustedmaclist = NULL;}/** * If the command-line didn't provide a config, use the default. */voidconfig_init_override(void){ if (config.daemon == -1) config.daemon = DEFAULT_DAEMON;}/** @internalParses a single token from the config file*/static OpCodesconfig_parse_token(const char *cp, const char *filename, int linenum){ int i; for (i = 0; keywords[i].name; i++) if (strcasecmp(cp, keywords[i].name) == 0) return keywords[i].opcode; debug(LOG_ERR, "%s: line %d: Bad configuration option: %s", filename, linenum, cp); return oBadOption;}/** @internalParses auth server information*/static voidparse_auth_server(FILE *file, char *filename, int *linenum){ char *host = NULL, *path = NULL, *loginscriptpathfragment = NULL, *portalscriptpathfragment = NULL, *msgscriptpathfragment = NULL, *pingscriptpathfragment = NULL, *authscriptpathfragment = NULL, line[MAX_BUF], *p1, *p2; int http_port, ssl_port, ssl_available, opcode; t_auth_serv *new, *tmp; /* Defaults */ path = safe_strdup(DEFAULT_AUTHSERVPATH); loginscriptpathfragment = safe_strdup(DEFAULT_AUTHSERVLOGINPATHFRAGMENT); portalscriptpathfragment = safe_strdup(DEFAULT_AUTHSERVPORTALPATHFRAGMENT); msgscriptpathfragment = safe_strdup(DEFAULT_AUTHSERVMSGPATHFRAGMENT); pingscriptpathfragment = safe_strdup(DEFAULT_AUTHSERVPINGPATHFRAGMENT); authscriptpathfragment = safe_strdup(DEFAULT_AUTHSERVAUTHPATHFRAGMENT); http_port = DEFAULT_AUTHSERVPORT; ssl_port = DEFAULT_AUTHSERVSSLPORT; ssl_available = DEFAULT_AUTHSERVSSLAVAILABLE; /* Read first line */ memset(line, 0, MAX_BUF); fgets(line, MAX_BUF - 1, file); (*linenum)++; /* increment line counter. */ /* Parsing loop */ while ((line[0] != '\0') && (strchr(line, '}') == NULL)) { /* skip leading blank spaces */ for (p1 = line; isblank(*p1); p1++); /* End at end of line */ if ((p2 = strchr(p1, '#')) != NULL) { *p2 = '\0'; } else if ((p2 = strchr(p1, '\r')) != NULL) { *p2 = '\0'; } else if ((p2 = strchr(p1, '\n')) != NULL) { *p2 = '\0'; } /* next, we coopt the parsing of the regular config */ if (strlen(p1) > 0) { p2 = p1; /* keep going until word boundary is found. */ while ((*p2 != '\0') && (!isblank(*p2))) p2++; /* Terminate first word. */ *p2 = '\0'; p2++; /* skip all further blanks. */ while (isblank(*p2)) p2++; /* Get opcode */ opcode = config_parse_token(p1, filename, *linenum); switch (opcode) { case oAuthServHostname: host = safe_strdup(p2); break; case oAuthServPath: free(path); path = safe_strdup(p2); break; case oAuthServLoginScriptPathFragment: free(loginscriptpathfragment); loginscriptpathfragment = safe_strdup(p2); break; case oAuthServPortalScriptPathFragment: free(portalscriptpathfragment); portalscriptpathfragment = safe_strdup(p2); break; case oAuthServMsgScriptPathFragment: free(msgscriptpathfragment); msgscriptpathfragment = safe_strdup(p2); break; case oAuthServPingScriptPathFragment: free(pingscriptpathfragment); pingscriptpathfragment = safe_strdup(p2); break; case oAuthServAuthScriptPathFragment: free(authscriptpathfragment); authscriptpathfragment = safe_strdup(p2); break; case oAuthServSSLPort: ssl_port = atoi(p2); break; case oAuthServHTTPPort: http_port = atoi(p2); break; case oAuthServSSLAvailable: ssl_available = parse_boolean_value(p2); if (ssl_available < 0) ssl_available = 0; break; case oBadOption: default: debug(LOG_ERR, "Bad option on line %d " "in %s.", *linenum, filename); debug(LOG_ERR, "Exiting..."); exit(-1); break; } } /* Read next line */ memset(line, 0, MAX_BUF); fgets(line, MAX_BUF - 1, file); (*linenum)++; /* increment line counter. */ } /* only proceed if we have an host and a path */ if (host == NULL) return; debug(LOG_DEBUG, "Adding %s:%d (SSL: %d) %s to the auth server list", host, http_port, ssl_port, path); /* Allocate memory */ new = safe_malloc(sizeof(t_auth_serv)); /* Fill in struct */ memset(new, 0, sizeof(t_auth_serv)); /*< Fill all with NULL */ new->authserv_hostname = host; new->authserv_use_ssl = ssl_available; new->authserv_path = path; new->authserv_login_script_path_fragment = loginscriptpathfragment; new->authserv_portal_script_path_fragment = portalscriptpathfragment; new->authserv_msg_script_path_fragment = msgscriptpathfragment; new->authserv_ping_script_path_fragment = pingscriptpathfragment; new->authserv_auth_script_path_fragment = authscriptpathfragment; new->authserv_http_port = http_port; new->authserv_ssl_port = ssl_port; /* If it's the first, add to config, else append to last server */ if (config.auth_servers == NULL) { config.auth_servers = new; } else { for (tmp = config.auth_servers; tmp->next != NULL; tmp = tmp->next); tmp->next = new; } debug(LOG_DEBUG, "Auth server added");}/**Advance to the next word@param s string to parse, this is the next_word pointer, the value of s when the macro is called is the current word, after the macro completes, s contains the beginning of the NEXT word, so you need to save s to something else before doing TO_NEXT_WORD@param e should be 0 when calling TO_NEXT_WORD(), it'll be changed to 1 if the end of the string is reached.*/#define TO_NEXT_WORD(s, e) do { \ while (*s != '\0' && !isblank(*s)) { \ s++; \ } \ if (*s != '\0') { \ *s = '\0'; \ s++; \ while (isblank(*s)) \ s++; \ } else { \ e = 1; \ } \} while (0)/** @internalParses firewall rule set information*/static voidparse_firewall_ruleset(char *ruleset, FILE *file, char *filename, int *linenum){ char line[MAX_BUF], *p1, *p2; int opcode; debug(LOG_DEBUG, "Adding Firewall Rule Set %s", ruleset); /* Read first line */ memset(line, 0, MAX_BUF); fgets(line, MAX_BUF - 1, file); (*linenum)++; /* increment line counter. */ /* Parsing loop */ while ((line[0] != '\0') && (strchr(line, '}') == NULL)) { /* skip leading blank spaces */ for (p1 = line; isblank(*p1); p1++); /* End at end of line */ if ((p2 = strchr(p1, '#')) != NULL) { *p2 = '\0'; } else if ((p2 = strchr(p1, '\r')) != NULL) { *p2 = '\0'; } else if ((p2 = strchr(p1, '\n')) != NULL) { *p2 = '\0'; } /* next, we coopt the parsing of the regular config */ if (strlen(p1) > 0) { p2 = p1; /* keep going until word boundary is found. */ while ((*p2 != '\0') && (!isblank(*p2))) p2++; /* Terminate first word. */ *p2 = '\0'; p2++; /* skip all further blanks. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -