1 /**************************************************
2 OpengateM - a 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 "opengatemd.h"
29 #include "../ezxml/ezxml.h"
31 #define CONFIG_VERSION "0.6.0"
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);
48 void PutMessageToClient(char* buff){
49 err_msg("ERR at %s#%d: %s",__FILE__,__LINE__,buff);
51 char* getProgramName(void){
56 /**************************************************/
57 /* Prepare Conf file to use */
58 /* this is called before syslog setup */
59 /**************************************************/
60 int openConfFile(void)
66 /* parse file and make tree */
67 if((xmlRoot = ezxml_parse_file(CONFIGFILE))==NULL){
69 /* as the syslog is not prepared, error is send to web*/
70 strlcpy(buff, "<H3>Error: Opengate configuration file ",BUFFMAXLN);
71 strlcat(buff, CONFIGFILE,BUFFMAXLN);
72 strlcat(buff, " is not found. Call the administrator.</H3><BR>",BUFFMAXLN);
73 PutMessageToClient(buff);
78 /* to check error, convert to xml */
79 s=ezxml_toxml(xmlRoot); free(s);
81 /* if failed, show error message */
82 errMsg=(char *)ezxml_error(xmlRoot);
85 /* as the syslog is not prepared, error is send to web*/
86 strlcpy(buff, "<H3>Error: Opengate configuration file ",BUFFMAXLN);
87 strlcat(buff, CONFIGFILE,BUFFMAXLN);
88 strlcat(buff, " is illegal. Call the administrator.</H3><HR>",BUFFMAXLN);
89 strlcat(buff, "XML parser message: ", BUFFMAXLN);
90 strlcat(buff, errMsg, BUFFMAXLN);
91 strlcat(buff, "<HR>", BUFFMAXLN);
92 PutMessageToClient(buff);
97 /* check the config file version */
98 if(isNull(ezxml_attr(xmlRoot, "ConfigVersion"))||
99 (strcmp(CONFIG_VERSION, ezxml_attr(xmlRoot, "ConfigVersion"))!=0)){
100 strlcpy(buff, "<H3>Error: Opengate configuration file ",BUFFMAXLN);
101 strlcat(buff, CONFIGFILE, BUFFMAXLN);
102 strlcat(buff, " has mismatch version.<br> Please update it with ",BUFFMAXLN);
103 strlcat(buff, CONFIGFILE, BUFFMAXLN);
104 strlcat(buff, ".sample.",BUFFMAXLN);
105 PutMessageToClient(buff);
110 /* check the syslog */
111 if(atoi(GetConfValue("Syslog/Enable")) &&
112 atoi(GetConfValue("Syslog/Facility"))==0){
114 /* as the syslog is not prepared, error is send to web*/
115 strlcpy(buff, "<H3>Error: correct SYSLOG setting(local0-local7) is not found in Opengate configuration file ",BUFFMAXLN);
116 strlcat(buff, CONFIGFILE,BUFFMAXLN);
117 strlcat(buff, ". Call the administrator.</H3><BR>",BUFFMAXLN);
118 PutMessageToClient(buff);
126 /**************************************************/
127 /* initialize the Config */
128 /**************************************************/
131 /* as debug flag is used many times, put it in gloval variable */
132 debug=atoi(getConfValue("Debug"));
135 /**************************************************/
136 /* Finish Conf file usage */
137 /**************************************************/
138 void closeConfFile(void)
140 if(xmlRoot!=NULL)ezxml_free(xmlRoot);
143 /**************************************************/
144 /* Setup pointer to the matched ExtraSet */
145 /**************************************************/
146 void setupConfExtra(char * userId,char *extraId)
150 /* init as no ExtraSet */
153 /* search the matching extra set (first match is employed) */
154 for(xml=ezxml_child(xmlRoot, "ExtraSet"); xml; xml=xml->next){
156 /* if ExtraId is exist, check it */
157 if(!isNull(ezxml_attr(xml, "ExtraId"))){
159 /* if not match, go to next ExtraSet */
160 /* ('default' indicated in conf matchs to Null-extraId) */
162 if(strcmp("default", ezxml_attr(xml, "ExtraId"))!=0)continue;
164 if(strcmp(extraId, ezxml_attr(xml, "ExtraId"))!=0)continue;
168 /* if userID pattern is exist, check it */
169 if(!isNull(ezxml_attr(xml, "UserIdPattern"))){
171 /* if not matched, go to next ExtraSet */
172 if(RegExMatch(userId, ezxml_attr(xml, "UserIdPattern"))==FALSE) continue;
175 /* found matched ExtraSet */
179 /* if found a matched ExtraSet, save the pointer */
180 if(xml!=NULL) xmlExtraSet=xml;
182 /* change syslog setting */
183 errToSyslog(atoi(GetConfValue("Syslog/Enable")));
184 progName=getProgramName();
185 openlog(progName, LOG_PID, atoi(GetConfValue("Syslog/Facility")));
187 /* reset config setting */
191 /***********************************************/
192 /* regular expression matching */
193 /* inStr : string to match */
194 /* regEx : regular expression */
195 /***********************************************/
196 int regExMatch(const char *inStr, const char *regEx)
201 char errbuff[WORDMAXLN];
204 if((errcode=regcomp(®, regEx, REG_NOSUB|REG_EXTENDED|REG_ICASE))!=0){
205 regerror(errcode, ®, errbuff, WORDMAXLN);
206 err_msg("ERR at %s#%d: regex message=%s",__FILE__,__LINE__,errbuff);
210 /* if compile is success, check the input string */
212 if(regexec(®, inStr, (size_t)0, NULL, 0)==0) match=TRUE;
221 /**************************************************/
222 /* get a value for name from Conf file */
223 /* the name[aa/bb/cc] means the path */
224 /* if ID is set, extraSet value is overlayed */
225 /**************************************************/
226 char *getConfValue(char *name)
231 char buff[BUFFMAXLN];
233 /* AuthServer setting is done in other routine */
234 if(strstr(name,"AuthServer/")==name) return GetConfAuthServer(name);
236 /* copy name to work area */
237 strlcpy(buff,name,BUFFMAXLN);
239 /* get first token */
240 pStr=strtok(buff, SEPARATOR);
242 /* set search start to root of tree */
245 /* search the tree node for the name */
247 xml=ezxml_child(xml, pStr);
248 pStr=strtok(NULL, SEPARATOR);
251 /* get the node value */
252 pValue= ezxml_txt(xml);
254 /* if not get, write error message */
256 err_msg("ERR at %s#%d: cannot get %s from conf file",__FILE__,__LINE__,name);
259 /* get value in extra set matched to ID */
260 /* if name is matched in first level, reset all child setting */
261 /* in this section, many parameters are not set */
262 if(!isNull(pValueExtra=getConfValueExtra(name))){
266 /* if syslog facility, the id is converted to raw value */
267 if(strcmp(name,"Syslog/Facility")==0){
268 pValue=convertToFacilityRaw(pValue);
271 /* return found value */
275 /************************************************/
276 /* get the value in extra set matched to ID */
277 /************************************************/
278 char *getConfValueExtra(char *name)
281 char buff[BUFFMAXLN];
284 if(xmlExtraSet==NULL) return "";
286 /* extract first token in name */
287 strlcpy(buff,name,BUFFMAXLN);
288 pStr=strtok(buff, SEPARATOR);
290 /* get a first level matched node in extra set */
291 /* the first level is not included in the following loop */
292 /* as to prevent partial overlay of sub level value */
293 xml=ezxml_child(xmlExtraSet, pStr);
294 if(xml==NULL) return "";
296 /* search the node matched to name */
297 pStr=strtok(NULL, SEPARATOR);
299 xml=ezxml_child(xml, pStr);
300 pStr=strtok(NULL, SEPARATOR);
303 /* return the found value */
304 return ezxml_txt(xml);
308 /***************************************************/
309 /* get a value for AuthServer param from Conf file*/
310 /* the name[AuthServer/bb/cc] means the path */
311 /***************************************************/
312 char *getConfAuthServer(char *name)
316 char buff[BUFFMAXLN];
319 /* copy name to work area */
320 strlcpy(buff,name,BUFFMAXLN);
322 /* get first token */
323 pStr=strtok(buff, SEPARATOR);
325 /* it must be AuthServer. if not return */
326 if(strcmp(pStr, "AuthServer")!=0)return NULL;
328 /* if authserver pointer is not set, set it */
329 if(xmlAuthServer==NULL){
330 if(!selectNextAuthServer()) return NULL;
333 /* set search start to the saved pointer */
336 /* search the tree node for the name */
337 pStr=strtok(NULL, SEPARATOR);
339 xml=ezxml_child(xml, pStr);
340 pStr=strtok(NULL, SEPARATOR);
343 /* get the node value */
344 pValue= ezxml_txt(xml);
346 /* if not get Protocol, write error message */
348 && (strcmp(name,"AuthServer/Protocol")==0) ){
349 err_msg("ERR at %s#%d: cannot get %s from conf file",__FILE__,__LINE__,name);
352 /* return found value */
356 /**********************************/
357 /* select next authserver setting */
358 /**********************************/
359 int selectNextAuthServer(void){
361 ezxml_t xmlTmp; /* temporary variable */
363 /* first call (initialize) */
364 /* xmlAuthPointer is the static variable to save authserver pointer */
365 if(xmlAuthServer==NULL){
367 /* if not set, search the first authserver pointer */
368 xmlAuthServer=ezxml_child(xmlRoot, "AuthServer");
370 /* if authserver is found in extra set, pointer is moved to it */
371 if(xmlExtraSet!=NULL){
372 xmlTmp=ezxml_child(xmlExtraSet, "AuthServer");
374 xmlAuthServer=xmlTmp;
379 /* successive calls */
380 /* pointer is moved to next */
382 xmlAuthServer=ezxml_next(xmlAuthServer);
385 /* if not found return False */
386 if(xmlAuthServer==NULL){
393 /**********************************************
394 reset pointer for auth server list
395 **********************************************/
396 void resetAuthServerPointer(void){
400 /***********************************************/
401 /* Convart the syslog facility id to raw value */
402 /***********************************************/
403 char *convertToFacilityRaw(char *pValue)
405 static char facility[WORDMAXLN];
408 if (strcmp(pValue, "local0")==0) rawValue=LOG_LOCAL0;
409 else if(strcmp(pValue, "local1")==0) rawValue=LOG_LOCAL1;
410 else if(strcmp(pValue, "local2")==0) rawValue=LOG_LOCAL2;
411 else if(strcmp(pValue, "local3")==0) rawValue=LOG_LOCAL3;
412 else if(strcmp(pValue, "local4")==0) rawValue=LOG_LOCAL4;
413 else if(strcmp(pValue, "local5")==0) rawValue=LOG_LOCAL5;
414 else if(strcmp(pValue, "local6")==0) rawValue=LOG_LOCAL6;
415 else if(strcmp(pValue, "local7")==0) rawValue=LOG_LOCAL7;
418 snprintf(facility, WORDMAXLN, "%d", rawValue);
423 /**************************************************/
424 /* get the first value as previous call */
425 /* (next node of the lowest level of tree) */
426 /**************************************************/
427 char *getFirstConfValue(char* name)
430 pValue=GetConfValue(name);
432 /* save the pointer now */
435 /* return found value */
439 /**************************************************/
440 /* get the next value as previous call */
441 /* (next node of the lowest level of tree) */
442 /**************************************************/
443 char *getNextConfValue(void)
447 /* recover previous pointer */
451 if(xml==NULL) return "";
452 xml = ezxml_next(xml);
454 /* save for next call */
457 /* get the node value */
458 pValue= ezxml_txt(xml);
460 /* if not get, write error message */
461 if(pValue==NULL) return "";
463 /* return found value */
468 /***********************************************/
469 /***********************************************/
470 int OpenConfFile(void){
472 if(debug>1) err_msg("DEBUG:=>openConfFile( )");
473 ret = openConfFile();
474 if(debug>1) err_msg("DEBUG:(%d)<=openConfFile( )",ret);
478 void CloseConfFile(void){
479 if(debug>1) err_msg("DEBUG:=>closeConfFile( )");
481 if(debug>1) err_msg("DEBUG:<=closeConfFile( )");
484 void SetupConfExtra(char *userId, char *extraId){
485 if(debug>1) err_msg("DEBUG:=>setupConfExtra(%s,%s)",userId, extraId);
486 setupConfExtra(userId, extraId);
487 if(debug>1) err_msg("DEBUG:<=setupConfExtra( )");
490 char *GetConfValue(char *name){
492 if(debug>1) err_msg("DEBUG:=>getConfValue(%s)",name);
493 ret=getConfValue(name);
494 if(debug>1) err_msg("DEBUG:(%s)<=getConfValue( )",ret);
498 char *GetConfValueExtra(char *name){
500 if(debug>1) err_msg("DEBUG:=>getConfValueExtra(%s)",name);
501 ret=getConfValueExtra(name);
502 if(debug>1) err_msg("DEBUG:(%s)<=getConfValueExtra( )",ret);
506 char *GetConfAuthServer(char *name){
508 if(debug>1) err_msg("DEBUG:=>getConfAuthServer(%s)",name);
509 ret=getConfAuthServer(name);
510 if(debug>1) err_msg("DEBUG:(%s)<=getConfAuthServer( )",ret);
514 int SelectNextAuthServer(void){
516 if(debug>1) err_msg("DEBUG:=>selectNextAuthServer( )");
517 ret=selectNextAuthServer();
518 if(debug>1) err_msg("DEBUG:(%d)<=selectNextAuthServer( )",ret);
523 if(debug>1) err_msg("DEBUG:=>initConf( )");
525 if(debug>1) err_msg("DEBUG:<=initConf( )");
528 int RegExMatch(const char *inStr, const char *regEx){
530 if(debug>1) err_msg("DEBUG:=>regExMatch(%s,%s)", inStr, regEx);
531 ret=regExMatch(inStr, regEx);
532 if(debug>1) err_msg("DEBUG:(%d)<=regExMatch( )",ret);
536 void ResetAuthServerPointer(void){
537 if(debug>1) err_msg("DEBUG:=>resetAuthServerPointer( )");
538 resetAuthServerPointer();
539 if(debug>1) err_msg("DEBUG:<=resetAuthServerPointer( )");
542 char *GetFirstConfValue(char* name){
544 if(debug>1) err_msg("DEBUG:=>getFirstConfValue( )");
545 ret=getFirstConfValue(name);
546 if(debug>1) err_msg("DEBUG:(%s)<=getFirstConfValue( )",ret);
550 char *GetNextConfValue(void){
552 if(debug>1) err_msg("DEBUG:=>getNextConfValue( )");
553 ret=getNextConfValue();
554 if(debug>1) err_msg("DEBUG:(%s)<=getNextConfValue( )",ret);