1 /**************************************************
3 module for getting parameters from conf file
5 Copyright (C) 2006 Opengate Project Team
6 Written by Yoshiaki Watanabe
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 Email: watanaby@is.saga-u.ac.jp
25 Programmed by Yoshiaki WATANABE
27 **************************************************/
28 #include "opengatesrv.h"
29 #include "../ezxml/ezxml.h"
31 #define CONFIG_VERSION "1.4.10"
35 static ezxml_t xmlRoot=NULL;
36 static ezxml_t xmlExtraSet=NULL;
38 char *getConfValueExtra(char *name);
39 char *getConfValue(char *name);
40 char *convertToFacilityRaw(char *pValue);
42 /**************************************************/
43 /* Prepare Conf file to use */
44 /* this is called before syslog setup */
45 /**************************************************/
46 int openConfFile(void)
52 /* parse file and make tree */
53 if((xmlRoot = ezxml_parse_file(CONFIGFILE))==NULL){
55 /* as the syslog is not prepared, error is send to web*/
56 strncpy(buff, "<H3>Error: Opengate configuration file ",BUFFMAXLN);
57 strncat(buff, CONFIGFILE,BUFFMAXLN);
58 strncat(buff, " is not found. Call the administrator.</H3><BR>",BUFFMAXLN);
64 /* to check error, convert to xml */
65 s=ezxml_toxml(xmlRoot); free(s);
67 /* if failed, show error message */
68 errMsg=(char *)ezxml_error(xmlRoot);
71 /* as the syslog is not prepared, error is send to web*/
72 strncpy(buff, "<H3>Error: Opengate configuration file ",BUFFMAXLN);
73 strncat(buff, CONFIGFILE,BUFFMAXLN);
74 strncat(buff, " is illegal. Call the administrator.</H3><HR>",BUFFMAXLN);
75 strncat(buff, "XML parser message: ", BUFFMAXLN);
76 strncat(buff, errMsg, BUFFMAXLN);
77 strncat(buff, "<HR>", BUFFMAXLN);
83 /* check the config file version */
84 if(isNull(ezxml_attr(xmlRoot, "ConfigVersion"))||
85 (strcmp(CONFIG_VERSION, ezxml_attr(xmlRoot, "ConfigVersion"))!=0)){
86 strncpy(buff, "<H3>Error: Opengate configuration file ",BUFFMAXLN);
87 strncat(buff, CONFIGFILE, BUFFMAXLN);
88 strncat(buff, " has mismatch version.<br> Please update it with ",BUFFMAXLN);
89 strncat(buff, CONFIGFILE, BUFFMAXLN);
90 strncat(buff, ".sample.",BUFFMAXLN);
96 /* check the syslog */
97 if(atoi(GetConfValue("Syslog/Enable")) &&
98 atoi(GetConfValue("Syslog/Facility"))==0){
100 /* as the syslog is not prepared, error is send to web*/
101 strncpy(buff, "<H3>Error: correct SYSLOG setting(local0-local7) is not found in Opengate configuration file ",BUFFMAXLN);
102 strncat(buff, CONFIGFILE,BUFFMAXLN);
103 strncat(buff, ". Call the administrator.</H3><BR>",BUFFMAXLN);
112 /**************************************************/
113 /* initialize the Config */
114 /**************************************************/
117 /* as debug flag is used many times, put it in gloval variable */
118 debug=atoi(getConfValue("Debug"));
121 /**************************************************/
122 /* Finish Conf file usage */
123 /**************************************************/
124 void closeConfFile(void)
126 if(xmlRoot!=NULL)ezxml_free(xmlRoot);
129 /**************************************************/
130 /* Setup pointer to the matched ExtraSet */
131 /**************************************************/
132 void setupConfExtra(char * userId,char *extraId)
136 /* init as no ExtraSet */
139 if(isNull(extraId)) return;
141 /* search the matching extra set (first match is employed) */
142 for(xml=ezxml_child(xmlRoot, "ExtraSet"); xml; xml=xml->next){
144 /* if extraID is not match, go to next ExtraSet */
145 if(strcmp(extraId, ezxml_attr(xml, "ExtraId"))!=0) continue;
147 /* if userID pattern is exist, check it */
148 if(!isNull(ezxml_attr(xml, "UserIdPattern"))){
150 /* if not matched, go to next ExtraSet */
151 if(RegExMatch(userId, ezxml_attr(xml, "UserIdPattern"))==FALSE)continue;
154 /* found matched ExtraSet */
158 /* if found a matched ExtraSet, save the pointer */
159 if(xml!=NULL) xmlExtraSet=xml;
161 /* change syslog setting */
162 errToSyslog(atoi(GetConfValue("Syslog/Enable")));
163 openlog(GetConfValue("MainCgi"), LOG_PID, atoi(GetConfValue("Syslog/Facility")));
165 /* reset config setting */
169 /***********************************************/
170 /* regular expression matching */
171 /* inStr : string to match */
172 /* regEx : regular expression */
173 /***********************************************/
174 int regExMatch(const char *inStr, const char *regEx)
179 char errbuff[WORDMAXLN];
182 if((errcode=regcomp(®, regEx, REG_NOSUB|REG_EXTENDED|REG_ICASE))!=0){
183 regerror(errcode, ®, errbuff, WORDMAXLN);
184 err_msg("ERR at %s#%d: regex message=%s",__FILE__,__LINE__,errbuff);
188 /* if compile is success, check the input string */
190 if(regexec(®, inStr, (size_t)0, NULL, 0)==0) match=TRUE;
199 /**************************************************/
200 /* get a value for name from Conf file */
201 /* the name[aa/bb/cc] means the path */
202 /* if ID is set, extraSet value is overlayed */
203 /**************************************************/
204 char *getConfValue(char *name)
209 char buff[BUFFMAXLN];
212 /* copy name to work area */
213 strncpy(buff,name,BUFFMAXLN);
215 /* get first token */
216 pStr=strtok(buff, SEPARATOR);
218 /* set search start to root of tree */
221 /* search the tree node for the name */
223 xml=ezxml_child(xml, pStr);
224 pStr=strtok(NULL, SEPARATOR);
227 /* get the node value */
228 pValue= ezxml_txt(xml);
230 /* if not get, write error message */
232 /* AuthServer parameters (except Protocol) might be omitted */
233 if((strstr(name,"AuthServer")==name)&&
234 strstr(name,"AuthServer/Protocol")==NULL){
236 /* other parameters must be set */
237 err_msg("ERR at %s#%d: cannot get %s from conf file",__FILE__,__LINE__,name);
241 /* get value in extra set matched to ID */
242 /* if name is matched in first level, reset all child setting */
243 /* in this section, many parameters are not set */
244 if(!isNull(pValueExtra=getConfValueExtra(name))){
248 /* if syslog facility, the id is converted to raw value */
249 if(strcmp(name,"Syslog/Facility")==0){
250 pValue=convertToFacilityRaw(pValue);
253 /* return found value */
256 /************************************************/
257 /* get the value in extra set matched to ID */
258 /************************************************/
259 char *getConfValueExtra(char *name)
262 char buff[BUFFMAXLN];
265 if(xmlExtraSet==NULL) return "";
267 /* extract first token in name */
268 strncpy(buff,name,BUFFMAXLN);
269 pStr=strtok(buff, SEPARATOR);
271 /* get a first level matched node in extra set */
272 /* the first level is not included in the following loop */
273 /* as to prevent partial overlay of sub level value */
274 xml=ezxml_child(xmlExtraSet, pStr);
275 if(xml==NULL) return "";
277 /* search the node matched to name */
278 pStr=strtok(NULL, SEPARATOR);
280 xml=ezxml_child(xml, pStr);
281 pStr=strtok(NULL, SEPARATOR);
284 /* return the found value */
285 return ezxml_txt(xml);
288 /***********************************************/
289 /* Convart the syslog facility id to raw value */
290 /***********************************************/
291 char *convertToFacilityRaw(char *pValue)
293 static char facility[WORDMAXLN];
296 if (strcmp(pValue, "local0")==0) rawValue=LOG_LOCAL0;
297 else if(strcmp(pValue, "local1")==0) rawValue=LOG_LOCAL1;
298 else if(strcmp(pValue, "local2")==0) rawValue=LOG_LOCAL2;
299 else if(strcmp(pValue, "local3")==0) rawValue=LOG_LOCAL3;
300 else if(strcmp(pValue, "local4")==0) rawValue=LOG_LOCAL4;
301 else if(strcmp(pValue, "local5")==0) rawValue=LOG_LOCAL5;
302 else if(strcmp(pValue, "local6")==0) rawValue=LOG_LOCAL6;
303 else if(strcmp(pValue, "local7")==0) rawValue=LOG_LOCAL7;
306 snprintf(facility, WORDMAXLN, "%d", rawValue);
311 /***********************************************/
312 /***********************************************/
313 int OpenConfFile(void){
315 if(debug>1) err_msg("DEBUG:=>openConfFile( )");
316 ret = openConfFile();
317 if(debug>1) err_msg("DEBUG:(%d)<=openConfFile( )",ret);
321 void CloseConfFile(void){
322 if(debug>1) err_msg("DEBUG:=>closeConfFile( )");
324 if(debug>1) err_msg("DEBUG:<=closeConfFile( )");
327 void SetupConfExtra(char *userId, char *extraId){
328 if(debug>1) err_msg("DEBUG:=>setupConfExtra(%s,%s)",userId, extraId);
329 setupConfExtra(userId, extraId);
330 if(debug>1) err_msg("DEBUG:<=setupConfExtra( )");
333 char *GetConfValue(char *name){
335 if(debug>1) err_msg("DEBUG:=>getConfValue(%s)",name);
336 ret=getConfValue(name);
337 if(debug>1) err_msg("DEBUG:(%s)<=getConfValue( )",ret);
341 char *GetConfValueExtra(char *name){
343 if(debug>1) err_msg("DEBUG:=>getConfValueExtra(%s)",name);
344 ret=getConfValueExtra(name);
345 if(debug>1) err_msg("DEBUG:(%s)<=getConfValueExtra( )",ret);
350 if(debug>1) err_msg("DEBUG:=>initConf( )");
352 if(debug>1) err_msg("DEBUG:<=initConf( )");
355 int RegExMatch(const char *inStr, const char *regEx){
357 if(debug>1) err_msg("DEBUG:=>regExMatch(%s,%s)", inStr, regEx);
358 ret=regExMatch(inStr, regEx);
359 if(debug>1) err_msg("DEBUG:(%d)<=regExMatch( )",ret);