1 /**************************************************
2 OpengateM - MAC address authentication system
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 "opengatemmng.h"
29 #include "../ezxml/ezxml.h"
31 #define CONFIG_VERSION "0.7.5"
35 static ezxml_t xmlRoot=NULL;
36 static ezxml_t xmlExtraSet=NULL;
37 static ezxml_t xmlAuthServer=NULL;
38 static ezxml_t xml=NULL;
39 static ezxml_t xmlSave=NULL;
41 char *getConfValueExtra(char *name);
42 char *getConfValue(char *name);
43 char *convertToFacilityRaw(char *pValue);
44 int selectNextAuthServer(void);
45 char *GetConfAuthServer(char *name);
47 /**************************************************/
48 /* Prepare Conf file to use */
49 /* this is called before syslog setup */
50 /**************************************************/
51 int openConfFile(void)
57 /* parse file and make tree */
58 if((xmlRoot = ezxml_parse_file(CONFIGFILE))==NULL){
60 /* as the syslog is not prepared, error is send to web*/
61 strncpy(buff, "<H3>Error: Opengate configuration file ",BUFFMAXLN);
62 strncat(buff, CONFIGFILE,BUFFMAXLN);
63 strncat(buff, " is not found. Call the administrator.</H3><BR>",BUFFMAXLN);
64 PutMessageToClient(buff);
69 /* to check error, convert to xml */
70 s=ezxml_toxml(xmlRoot); free(s);
72 /* if failed, show error message */
73 errMsg=(char *)ezxml_error(xmlRoot);
76 /* as the syslog is not prepared, error is send to web*/
77 strncpy(buff, "<H3>Error: Opengate configuration file ",BUFFMAXLN);
78 strncat(buff, CONFIGFILE,BUFFMAXLN);
79 strncat(buff, " is illegal. Call the administrator.</H3><HR>",BUFFMAXLN);
80 strncat(buff, "XML parser message: ", BUFFMAXLN);
81 strncat(buff, errMsg, BUFFMAXLN);
82 strncat(buff, "<HR>", BUFFMAXLN);
83 PutMessageToClient(buff);
88 /* check the config file version */
89 if(isNull(ezxml_attr(xmlRoot, "ConfigVersion"))||
90 (strcmp(CONFIG_VERSION, ezxml_attr(xmlRoot, "ConfigVersion"))!=0)){
91 strncpy(buff, "<H3>Error: Opengate configuration file ",BUFFMAXLN);
92 strncat(buff, CONFIGFILE, BUFFMAXLN);
93 strncat(buff, " has mismatch version.<br> Please update it with ",BUFFMAXLN);
94 strncat(buff, CONFIGFILE, BUFFMAXLN);
95 strncat(buff, ".sample.",BUFFMAXLN);
96 PutMessageToClient(buff);
101 /* check the syslog */
102 if(atoi(GetConfValue("Syslog/Enable")) &&
103 atoi(GetConfValue("Syslog/Facility"))==0){
105 /* as the syslog is not prepared, error is send to web*/
106 strncpy(buff, "<H3>Error: correct SYSLOG setting(local0-local7) is not found in Opengate configuration file ",BUFFMAXLN);
107 strncat(buff, CONFIGFILE,BUFFMAXLN);
108 strncat(buff, ". Call the administrator.</H3><BR>",BUFFMAXLN);
109 PutMessageToClient(buff);
117 /**************************************************/
118 /* initialize the Config */
119 /**************************************************/
122 /* as debug flag is used many times, put it in gloval variable */
123 debug=atoi(getConfValue("Debug"));
126 /**************************************************/
127 /* Finish Conf file usage */
128 /**************************************************/
129 void closeConfFile(void)
131 if(xmlRoot!=NULL)ezxml_free(xmlRoot);
134 /**************************************************/
135 /* Setup pointer to the matched ExtraSet */
136 /**************************************************/
137 void setupConfExtra(char * userId,char *extraId)
140 char useridfull[USERMAXLN]; /* userid@extraid */
142 /* setup long userid (userid@extraid) */
143 ConcatUserId(useridfull, userId, extraId);
145 /* init as no ExtraSet */
148 /* search the matching extra set (first match is employed) */
149 for(xml=ezxml_child(xmlRoot, "ExtraSet"); xml; xml=xml->next){
151 /* if ExtraId is exist, check it */
152 if(!isNull(ezxml_attr(xml, "ExtraId"))){
154 /* if not match, go to next ExtraSet */
155 /* ('default' indicated in conf matchs to Null-extraId) */
157 if(strcmp("default", ezxml_attr(xml, "ExtraId"))!=0)continue;
159 if(strcmp(extraId, ezxml_attr(xml, "ExtraId"))!=0)continue;
163 /* if userID pattern is exist, check it */
164 if(!isNull(ezxml_attr(xml, "UserIdPattern"))){
166 /* if not matched, go to next ExtraSet. last-arg 0 means ingore-case */
167 if(RegExMatch(userId,ezxml_attr(xml,"UserIdPattern"),0)==FALSE){
172 /* if UserExtraPattern is exist, check it */
173 /* UserExtraPattern = REGEX pattern matching to "userid@extraid" */
174 if(!isNull(ezxml_attr(xml, "UserExtraPattern"))){
176 /* if not matched, go to next ExtraSet. last-arg 0 means ingore-case */
177 if(RegExMatch(useridfull,ezxml_attr(xml,"UserExtraPattern"),0)==FALSE){
182 /* if UserExtraPtternNot is exist, check it */
183 /* UserExtraPatternNot = REGEX pattern NOT matching to "userid@extraid" */
184 if(!isNull(ezxml_attr(xml, "UserExtraPatternNot"))){
186 /* if matched, go to next ExtraSet. last-arg 0 means ingore-case */
187 if(RegExMatch(useridfull,ezxml_attr(xml,"UserExtraPatternNot"),0)==TRUE){
192 /* found matched ExtraSet */
196 /* if found a matched ExtraSet, save the pointer */
197 if(xml!=NULL) xmlExtraSet=xml;
199 /* change syslog setting */
200 errToSyslog(atoi(GetConfValue("Syslog/Enable")));
201 progName=getProgramName();
202 openlog(progName, LOG_PID, atoi(GetConfValue("Syslog/Facility")));
204 /* reset config setting */
208 /***********************************************/
209 /* regular expression matching */
210 /* inStr : string to match */
211 /* regEx : regular expression */
212 /* caseSensitive : 0=ignore case, 1=sensitive */
213 /***********************************************/
214 int regExMatch(const char *inStr, const char *regEx, int caseSensitive)
219 char errbuff[WORDMAXLN];
223 errcode=regcomp(®, regEx, REG_NOSUB|REG_EXTENDED);
225 errcode=regcomp(®, regEx, REG_NOSUB|REG_EXTENDED|REG_ICASE);
228 /* if error, return false */
230 regerror(errcode, ®, errbuff, WORDMAXLN);
231 err_msg("ERR at %s#%d: regex message=%s",__FILE__,__LINE__,errbuff);
235 /* if compile is success, check the input string */
237 if(regexec(®, inStr, (size_t)0, NULL, 0)==0) match=TRUE;
246 /**************************************************/
247 /* get a value for name from Conf file */
248 /* the name[aa/bb/cc] means the path */
249 /* if ID is set, extraSet value is overlayed */
250 /**************************************************/
251 char *getConfValue(char *name)
256 char buff[BUFFMAXLN];
258 /* AuthServer setting is done in other routine */
259 if(strstr(name,"AuthServer/")==name) return GetConfAuthServer(name);
261 /* copy name to work area */
262 strncpy(buff,name,BUFFMAXLN);
264 /* get first token */
265 pStr=strtok(buff, SEPARATOR);
267 /* set search start to root of tree */
270 /* search the tree node for the name */
272 xml=ezxml_child(xml, pStr);
273 pStr=strtok(NULL, SEPARATOR);
276 /* get the node value */
277 pValue= ezxml_txt(xml);
279 /* if not get, write error message */
281 err_msg("ERR at %s#%d: cannot get %s from conf file",__FILE__,__LINE__,name);
284 /* get value in extra set matched to ID */
285 /* if name is matched in first level, reset all child setting */
286 /* in this section, many parameters are not set */
287 if(!isNull(pValueExtra=getConfValueExtra(name))){
291 /* if syslog facility, the id is converted to raw value */
292 if(strcmp(name,"Syslog/Facility")==0){
293 pValue=convertToFacilityRaw(pValue);
296 /* return found value */
300 /************************************************/
301 /* get the value in extra set matched to ID */
302 /************************************************/
303 char *getConfValueExtra(char *name)
306 char buff[BUFFMAXLN];
309 if(xmlExtraSet==NULL) return "";
311 /* extract first token in name */
312 strncpy(buff,name,BUFFMAXLN);
313 pStr=strtok(buff, SEPARATOR);
315 /* get a first level matched node in extra set */
316 /* the first level is not included in the following loop */
317 /* as to prevent partial overlay of sub level value */
318 xml=ezxml_child(xmlExtraSet, pStr);
319 if(xml==NULL) return "";
321 /* search the node matched to name */
322 pStr=strtok(NULL, SEPARATOR);
324 xml=ezxml_child(xml, pStr);
325 pStr=strtok(NULL, SEPARATOR);
328 /* return the found value */
329 return ezxml_txt(xml);
333 /***************************************************/
334 /* get a value for AuthServer param from Conf file*/
335 /* the name[AuthServer/bb/cc] means the path */
336 /***************************************************/
337 char *getConfAuthServer(char *name)
341 char buff[BUFFMAXLN];
344 /* copy name to work area */
345 strncpy(buff,name,BUFFMAXLN);
347 /* get first token */
348 pStr=strtok(buff, SEPARATOR);
350 /* it must be AuthServer. if not return */
351 if(strcmp(pStr, "AuthServer")!=0)return NULL;
353 /* if authserver pointer is not set, set it */
354 if(xmlAuthServer==NULL){
355 if(!selectNextAuthServer()) return NULL;
358 /* set search start to the saved pointer */
361 /* search the tree node for the name */
362 pStr=strtok(NULL, SEPARATOR);
364 xml=ezxml_child(xml, pStr);
365 pStr=strtok(NULL, SEPARATOR);
368 /* get the node value */
369 pValue= ezxml_txt(xml);
371 /* if not get Protocol, write error message */
373 && (strcmp(name,"AuthServer/Protocol")==0) ){
374 err_msg("ERR at %s#%d: cannot get %s from conf file",__FILE__,__LINE__,name);
377 /* return found value */
381 /**********************************/
382 /* select next authserver setting */
383 /**********************************/
384 int selectNextAuthServer(void){
386 ezxml_t xmlTmp; /* temporary variable */
388 /* first call (initialize) */
389 /* xmlAuthPointer is the static variable to save authserver pointer */
390 if(xmlAuthServer==NULL){
392 /* if not set, search the first authserver pointer */
393 xmlAuthServer=ezxml_child(xmlRoot, "AuthServer");
395 /* if authserver is found in extra set, pointer is moved to it */
396 if(xmlExtraSet!=NULL){
397 xmlTmp=ezxml_child(xmlExtraSet, "AuthServer");
399 xmlAuthServer=xmlTmp;
404 /* successive calls */
405 /* pointer is moved to next */
407 xmlAuthServer=ezxml_next(xmlAuthServer);
410 /* if not found return False */
411 if(xmlAuthServer==NULL){
418 /**********************************************
419 reset pointer for auth server list
420 **********************************************/
421 void resetAuthServerPointer(void){
425 /***********************************************/
426 /* Convart the syslog facility id to raw value */
427 /***********************************************/
428 char *convertToFacilityRaw(char *pValue)
430 static char facility[WORDMAXLN];
433 if (strcmp(pValue, "local0")==0) rawValue=LOG_LOCAL0;
434 else if(strcmp(pValue, "local1")==0) rawValue=LOG_LOCAL1;
435 else if(strcmp(pValue, "local2")==0) rawValue=LOG_LOCAL2;
436 else if(strcmp(pValue, "local3")==0) rawValue=LOG_LOCAL3;
437 else if(strcmp(pValue, "local4")==0) rawValue=LOG_LOCAL4;
438 else if(strcmp(pValue, "local5")==0) rawValue=LOG_LOCAL5;
439 else if(strcmp(pValue, "local6")==0) rawValue=LOG_LOCAL6;
440 else if(strcmp(pValue, "local7")==0) rawValue=LOG_LOCAL7;
443 snprintf(facility, WORDMAXLN, "%d", rawValue);
448 /**************************************************/
449 /* get the first value as previous call */
450 /* (next node of the lowest level of tree) */
451 /**************************************************/
452 char *getFirstConfValue(char* name)
455 pValue=GetConfValue(name);
457 /* save the pointer now */
460 /* return found value */
464 /**************************************************/
465 /* get the next value as previous call */
466 /* (next node of the lowest level of tree) */
467 /**************************************************/
468 char *getNextConfValue(void)
472 /* recover previous pointer */
476 if(xml==NULL) return "";
477 xml = ezxml_next(xml);
479 /* save for next call */
482 /* get the node value */
483 pValue= ezxml_txt(xml);
485 /* if not get, write error message */
486 if(pValue==NULL) return "";
488 /* return found value */
493 /***********************************************/
494 /***********************************************/
495 int OpenConfFile(void){
497 if(debug>1) err_msg("DEBUG:=>openConfFile( )");
498 ret = openConfFile();
499 if(debug>1) err_msg("DEBUG:(%d)<=openConfFile( )",ret);
503 void CloseConfFile(void){
504 if(debug>1) err_msg("DEBUG:=>closeConfFile( )");
506 if(debug>1) err_msg("DEBUG:<=closeConfFile( )");
509 void SetupConfExtra(char *userId, char *extraId){
510 if(debug>1) err_msg("DEBUG:=>setupConfExtra(%s,%s)",userId, extraId);
511 setupConfExtra(userId, extraId);
512 if(debug>1) err_msg("DEBUG:<=setupConfExtra( )");
515 char *GetConfValue(char *name){
517 if(debug>1) err_msg("DEBUG:=>getConfValue(%s)",name);
518 ret=getConfValue(name);
519 if(debug>1) err_msg("DEBUG:(%s)<=getConfValue( )",ret);
523 char *GetConfValueExtra(char *name){
525 if(debug>1) err_msg("DEBUG:=>getConfValueExtra(%s)",name);
526 ret=getConfValueExtra(name);
527 if(debug>1) err_msg("DEBUG:(%s)<=getConfValueExtra( )",ret);
531 char *GetConfAuthServer(char *name){
533 if(debug>1) err_msg("DEBUG:=>getConfAuthServer(%s)",name);
534 ret=getConfAuthServer(name);
535 if(debug>1) err_msg("DEBUG:(%s)<=getConfAuthServer( )",ret);
539 int SelectNextAuthServer(void){
541 if(debug>1) err_msg("DEBUG:=>selectNextAuthServer( )");
542 ret=selectNextAuthServer();
543 if(debug>1) err_msg("DEBUG:(%d)<=selectNextAuthServer( )",ret);
548 if(debug>1) err_msg("DEBUG:=>initConf( )");
550 if(debug>1) err_msg("DEBUG:<=initConf( )");
553 int RegExMatch(const char *inStr, const char *regEx, int caseSensitive){
555 if(debug>1) err_msg("DEBUG:=>regExMatch(%s,%s)",inStr,regEx,caseSensitive);
556 ret=regExMatch(inStr, regEx,caseSensitive);
557 if(debug>1) err_msg("DEBUG:(%d)<=regExMatch( )",ret);
561 void ResetAuthServerPointer(void){
562 if(debug>1) err_msg("DEBUG:=>resetAuthServerPointer( )");
563 resetAuthServerPointer();
564 if(debug>1) err_msg("DEBUG:<=resetAuthServerPointer( )");
567 char *GetFirstConfValue(char* name){
569 if(debug>1) err_msg("DEBUG:=>getFirstConfValue( )");
570 ret=getFirstConfValue(name);
571 if(debug>1) err_msg("DEBUG:(%s)<=getFirstConfValue( )",ret);
575 char *GetNextConfValue(){
577 if(debug>1) err_msg("DEBUG:=>getNextConfValue( )");
578 ret=getNextConfValue();
579 if(debug>1) err_msg("DEBUG:(%s)<=getNextConfValue( )",ret);