OSDN Git Service

Ver.0.8.1
[opengatem/opengatem.git] / mngsrc / getparam.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3  module for getting parameters from conf file
4
5 Copyright (C) 2006 Opengate Project Team
6 Written by Yoshiaki Watanabe
7
8
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.
13
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.
18
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.
22
23 Email: watanaby@is.saga-u.ac.jp
24
25 Programmed by Yoshiaki WATANABE
26
27 **************************************************/
28 #include "opengatemmng.h"
29 #include "../ezxml/ezxml.h"
30
31 #define CONFIG_VERSION "0.7.5"
32 #define SEPARATOR "/"
33
34 int debug=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;
40
41 char *getConfValueExtra(char *name);
42 char *getConfValue(char *name);
43 char *convertToFacilityRaw(char *pValue);
44 int selectNextAuthServer(void);
45 char *GetConfAuthServer(char *name);
46
47 /**************************************************/
48 /* Prepare Conf file to use                       */
49 /* this is called before syslog setup             */
50 /**************************************************/
51 int openConfFile(void)
52 {
53   char buff[BUFFMAXLN];
54   char *s;
55   char *errMsg;
56
57   /* parse file and make tree */
58   if((xmlRoot = ezxml_parse_file(CONFIGFILE))==NULL){
59
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);
65
66     return -1;
67   }
68
69   /* to check error, convert to xml */
70   s=ezxml_toxml(xmlRoot);  free(s);
71   
72   /* if failed, show error message */
73   errMsg=(char *)ezxml_error(xmlRoot);
74
75   if(*errMsg!='\0'){
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);
84
85     return -1;
86   }
87
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);
97
98     return -1;
99   }
100
101   /* check the syslog */
102   if(atoi(GetConfValue("Syslog/Enable")) &&
103      atoi(GetConfValue("Syslog/Facility"))==0){
104
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);
110
111     return -1;
112   }
113
114   return 0;
115 }
116
117 /**************************************************/
118 /*  initialize the Config                         */
119 /**************************************************/
120 void initConf(void)
121 {
122   /* as debug flag is used many times, put it in gloval variable */
123   debug=atoi(getConfValue("Debug"));
124 }
125
126 /**************************************************/
127 /* Finish Conf file usage                         */
128 /**************************************************/
129 void closeConfFile(void)
130 {
131   if(xmlRoot!=NULL)ezxml_free(xmlRoot);
132 }
133
134 /**************************************************/
135 /* Setup pointer to the matched ExtraSet          */ 
136 /**************************************************/
137 void setupConfExtra(char * userId,char *extraId)
138 {
139   char* progName;
140   char useridfull[USERMAXLN]; /* userid@extraid */
141
142   /* setup long userid (userid@extraid) */
143   ConcatUserId(useridfull, userId, extraId);
144
145   /* init as no ExtraSet */
146   xmlExtraSet=NULL;
147
148   /* search the matching extra set (first match is employed) */
149   for(xml=ezxml_child(xmlRoot, "ExtraSet"); xml; xml=xml->next){
150     
151     /* if ExtraId is exist, check it */
152     if(!isNull(ezxml_attr(xml, "ExtraId"))){
153
154       /* if not match, go to next ExtraSet */
155       /* ('default' indicated in conf matchs to Null-extraId) */
156       if(isNull(extraId)){
157         if(strcmp("default", ezxml_attr(xml, "ExtraId"))!=0)continue;
158       }else{
159         if(strcmp(extraId, ezxml_attr(xml, "ExtraId"))!=0)continue;
160       }
161     }
162
163     /* if userID pattern is exist, check it */
164     if(!isNull(ezxml_attr(xml, "UserIdPattern"))){
165
166       /* if not matched, go to next ExtraSet. last-arg 0 means ingore-case */
167       if(RegExMatch(userId,ezxml_attr(xml,"UserIdPattern"),0)==FALSE){
168         continue;
169       }
170     }
171
172     /* if UserExtraPattern is exist, check it */
173     /*  UserExtraPattern = REGEX pattern matching to "userid@extraid" */
174     if(!isNull(ezxml_attr(xml, "UserExtraPattern"))){
175
176       /* if not matched, go to next ExtraSet. last-arg 0 means ingore-case */
177       if(RegExMatch(useridfull,ezxml_attr(xml,"UserExtraPattern"),0)==FALSE){
178         continue;
179       }
180     }
181
182     /* if UserExtraPtternNot is exist, check it */
183     /*  UserExtraPatternNot = REGEX pattern NOT matching to "userid@extraid" */
184     if(!isNull(ezxml_attr(xml, "UserExtraPatternNot"))){
185
186       /* if matched, go to next ExtraSet. last-arg 0 means ingore-case */
187       if(RegExMatch(useridfull,ezxml_attr(xml,"UserExtraPatternNot"),0)==TRUE){
188         continue;
189       }
190     }
191
192     /* found matched ExtraSet */
193     break;
194   }
195
196   /* if found a matched ExtraSet, save the pointer */
197   if(xml!=NULL) xmlExtraSet=xml;
198
199   /* change syslog setting */
200   errToSyslog(atoi(GetConfValue("Syslog/Enable")));
201   progName=getProgramName();
202   openlog(progName, LOG_PID, atoi(GetConfValue("Syslog/Facility")));
203
204   /* reset config setting */
205   InitConf();
206 }
207
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)
215 {
216   regex_t reg;
217   int errcode;
218   int match;
219   char errbuff[WORDMAXLN];
220
221   /* compile regex */
222   if(caseSensitive){
223     errcode=regcomp(&reg, regEx, REG_NOSUB|REG_EXTENDED);
224   }else{
225     errcode=regcomp(&reg, regEx, REG_NOSUB|REG_EXTENDED|REG_ICASE);
226   }
227
228   /* if error, return false */
229   if(errcode!=0){
230     regerror(errcode, &reg, errbuff, WORDMAXLN);
231     err_msg("ERR at %s#%d: regex message=%s",__FILE__,__LINE__,errbuff);
232     match=FALSE;
233   }
234   
235   /* if compile is success, check the input string */
236   else{
237     if(regexec(&reg, inStr, (size_t)0, NULL, 0)==0) match=TRUE;
238     else match=FALSE;
239   }
240
241   regfree(&reg);
242
243   return match;
244 }
245
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)
252 {
253   char *pValue;
254   char *pValueExtra;
255   char *pStr;
256   char buff[BUFFMAXLN];
257
258   /* AuthServer setting is done in other routine */
259   if(strstr(name,"AuthServer/")==name) return GetConfAuthServer(name);
260
261   /* copy name to work area */
262   strncpy(buff,name,BUFFMAXLN);
263
264   /* get first token */
265   pStr=strtok(buff, SEPARATOR);
266
267   /* set search start to root of tree */
268   xml=xmlRoot;
269
270   /* search the tree node for the name */
271   while(pStr!=NULL){
272     xml=ezxml_child(xml, pStr);
273     pStr=strtok(NULL, SEPARATOR);
274   }
275
276   /* get the node value */
277   pValue= ezxml_txt(xml);
278
279   /* if not get, write error message */
280   if(pValue==NULL){
281     err_msg("ERR at %s#%d: cannot get %s from conf file",__FILE__,__LINE__,name);
282   }
283
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))){
288     pValue=pValueExtra;
289   }
290
291   /* if syslog facility, the id is converted to raw value */
292   if(strcmp(name,"Syslog/Facility")==0){
293     pValue=convertToFacilityRaw(pValue);
294   }
295
296   /* return found value */
297   return pValue;
298 }
299
300 /************************************************/
301 /* get the value in extra set matched to ID     */
302 /************************************************/
303 char *getConfValueExtra(char *name)
304 {
305   char *pStr;
306   char buff[BUFFMAXLN];
307   ezxml_t xml;
308
309   if(xmlExtraSet==NULL) return "";
310
311   /* extract first token in name */
312   strncpy(buff,name,BUFFMAXLN);
313   pStr=strtok(buff, SEPARATOR);  
314
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 "";
320
321   /* search the node matched to name */
322   pStr=strtok(NULL, SEPARATOR);
323   while(pStr!=NULL){
324     xml=ezxml_child(xml, pStr);
325     pStr=strtok(NULL, SEPARATOR);
326   }
327
328   /* return the found value */
329   return ezxml_txt(xml);
330 }
331
332
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)
338 {
339   char *pValue;
340   char *pStr;
341   char buff[BUFFMAXLN];
342   ezxml_t xml;
343
344   /* copy name to work area */
345   strncpy(buff,name,BUFFMAXLN);
346
347   /* get first token */
348   pStr=strtok(buff, SEPARATOR);
349
350   /* it must be AuthServer. if not return */
351   if(strcmp(pStr, "AuthServer")!=0)return NULL;
352
353   /* if authserver pointer is not set, set it */
354   if(xmlAuthServer==NULL){
355     if(!selectNextAuthServer()) return NULL;
356   }
357
358   /* set search start to the saved pointer */
359   xml=xmlAuthServer;
360
361   /* search the tree node for the name */
362   pStr=strtok(NULL, SEPARATOR);
363   while(pStr!=NULL){
364     xml=ezxml_child(xml, pStr);
365     pStr=strtok(NULL, SEPARATOR);
366   }
367
368   /* get the node value */
369   pValue= ezxml_txt(xml);
370
371   /* if not get Protocol, write error message */
372   if(isNull(pValue)
373      && (strcmp(name,"AuthServer/Protocol")==0) ){
374     err_msg("ERR at %s#%d: cannot get %s from conf file",__FILE__,__LINE__,name);
375   }
376
377   /* return found value */
378   return pValue;
379 }
380
381 /**********************************/
382 /* select next authserver setting */
383 /**********************************/
384 int selectNextAuthServer(void){
385
386   ezxml_t xmlTmp; /* temporary variable */
387
388   /* first call (initialize) */
389   /* xmlAuthPointer is the static variable to save authserver pointer */
390   if(xmlAuthServer==NULL){
391
392     /* if not set, search the first authserver pointer */
393      xmlAuthServer=ezxml_child(xmlRoot, "AuthServer");
394      
395      /* if authserver is found in extra set, pointer is moved to it */ 
396      if(xmlExtraSet!=NULL){
397        xmlTmp=ezxml_child(xmlExtraSet, "AuthServer");
398        if(xmlTmp!=NULL){
399          xmlAuthServer=xmlTmp;
400        }
401      }
402   }
403
404   /* successive calls */
405   /* pointer is moved to next */
406   else{
407     xmlAuthServer=ezxml_next(xmlAuthServer);
408   }
409
410   /* if not found return False */
411   if(xmlAuthServer==NULL){
412     return FALSE;
413   }else{
414     return TRUE;
415   }
416 }
417
418 /**********************************************
419 reset pointer for auth server list
420 **********************************************/
421 void resetAuthServerPointer(void){
422   xmlAuthServer=NULL;
423 }
424
425 /***********************************************/
426 /* Convart the syslog facility id to raw value */
427 /***********************************************/
428 char *convertToFacilityRaw(char *pValue)
429 {
430   static char facility[WORDMAXLN];
431   int rawValue;
432
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;
441   else rawValue=0;
442
443   snprintf(facility, WORDMAXLN, "%d", rawValue);
444
445   return facility;
446 }
447
448 /**************************************************/
449 /*  get the first value as previous call           */
450 /*  (next node of the lowest level of tree)       */  
451 /**************************************************/
452 char *getFirstConfValue(char* name)
453 {
454    char *pValue;
455    pValue=GetConfValue(name);
456
457   /* save the pointer now */
458    xmlSave=xml;
459
460   /* return found value */
461   return pValue;
462 }
463
464 /**************************************************/
465 /*  get the next value as previous call           */
466 /*  (next node of the lowest level of tree)       */  
467 /**************************************************/
468 char *getNextConfValue(void)
469 {
470   char *pValue;
471
472   /* recover previous pointer */
473   xml=xmlSave;
474
475   /* get next node */
476   if(xml==NULL) return "";
477   xml = ezxml_next(xml);
478
479   /* save for next call */
480   xmlSave=xml;
481
482   /* get the node value */
483   pValue= ezxml_txt(xml);
484
485   /* if not get, write error message */
486   if(pValue==NULL) return "";
487
488   /* return found value */
489   return pValue;
490 }
491
492
493 /***********************************************/
494 /***********************************************/
495 int OpenConfFile(void){
496   int ret;
497   if(debug>1) err_msg("DEBUG:=>openConfFile( )");
498   ret = openConfFile();
499   if(debug>1) err_msg("DEBUG:(%d)<=openConfFile( )",ret);
500   return ret;
501 }
502
503 void CloseConfFile(void){
504   if(debug>1) err_msg("DEBUG:=>closeConfFile( )");
505   closeConfFile();
506   if(debug>1) err_msg("DEBUG:<=closeConfFile( )");
507 }
508
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( )");
513 }
514
515 char *GetConfValue(char *name){
516   char *ret;
517   if(debug>1) err_msg("DEBUG:=>getConfValue(%s)",name);
518   ret=getConfValue(name);
519   if(debug>1) err_msg("DEBUG:(%s)<=getConfValue( )",ret);
520   return ret;
521 }
522
523 char *GetConfValueExtra(char *name){
524   char *ret;
525   if(debug>1) err_msg("DEBUG:=>getConfValueExtra(%s)",name);
526   ret=getConfValueExtra(name);
527   if(debug>1) err_msg("DEBUG:(%s)<=getConfValueExtra( )",ret);
528   return ret;
529 }
530
531 char *GetConfAuthServer(char *name){
532   char *ret;
533   if(debug>1) err_msg("DEBUG:=>getConfAuthServer(%s)",name);
534   ret=getConfAuthServer(name);
535   if(debug>1) err_msg("DEBUG:(%s)<=getConfAuthServer( )",ret);
536   return ret;
537 }
538
539 int SelectNextAuthServer(void){
540   int ret;
541   if(debug>1) err_msg("DEBUG:=>selectNextAuthServer( )");
542   ret=selectNextAuthServer();
543   if(debug>1) err_msg("DEBUG:(%d)<=selectNextAuthServer( )",ret);
544   return ret;
545 }
546
547 void InitConf(void){
548   if(debug>1) err_msg("DEBUG:=>initConf( )");
549   initConf();
550   if(debug>1) err_msg("DEBUG:<=initConf( )");
551 }
552
553 int RegExMatch(const char *inStr, const char *regEx, int caseSensitive){
554   int ret;
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);
558   return ret;
559 }
560   
561 void ResetAuthServerPointer(void){
562   if(debug>1) err_msg("DEBUG:=>resetAuthServerPointer( )");
563   resetAuthServerPointer();
564   if(debug>1) err_msg("DEBUG:<=resetAuthServerPointer( )");
565 }
566
567 char *GetFirstConfValue(char* name){
568   char *ret;
569   if(debug>1) err_msg("DEBUG:=>getFirstConfValue( )");
570   ret=getFirstConfValue(name);
571   if(debug>1) err_msg("DEBUG:(%s)<=getFirstConfValue( )",ret);
572   return ret;
573 }
574
575 char *GetNextConfValue(){
576   char *ret;
577   if(debug>1) err_msg("DEBUG:=>getNextConfValue( )");
578   ret=getNextConfValue();
579   if(debug>1) err_msg("DEBUG:(%s)<=getNextConfValue( )",ret);
580   return ret;
581 }
582