OSDN Git Service

0c77331c2db08912478291387e83f22a39668c20
[opengatem/opengatem.git] / mdsrc / getparam.c
1 /**************************************************
2 OpengateM - a 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 "opengatemd.h"
29 #include "../ezxml/ezxml.h"
30
31 #define CONFIG_VERSION "0.6.0"
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 /*** dummys ***/
48 void PutMessageToClient(char* buff){
49     err_msg("ERR at %s#%d: %s",__FILE__,__LINE__,buff);
50 }
51 char* getProgramName(void){
52   return "";
53 }
54
55
56 /**************************************************/
57 /* Prepare Conf file to use                       */
58 /* this is called before syslog setup             */
59 /**************************************************/
60 int openConfFile(void)
61 {
62   char buff[BUFFMAXLN];
63   char *s;
64   char *errMsg;
65
66   /* parse file and make tree */
67   if((xmlRoot = ezxml_parse_file(CONFIGFILE))==NULL){
68
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);
74
75     return -1;
76   }
77
78   /* to check error, convert to xml */
79   s=ezxml_toxml(xmlRoot);  free(s);
80   
81   /* if failed, show error message */
82   errMsg=(char *)ezxml_error(xmlRoot);
83
84   if(*errMsg!='\0'){
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);
93
94     return -1;
95   }
96
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);
106
107     return -1;
108   }
109
110   /* check the syslog */
111   if(atoi(GetConfValue("Syslog/Enable")) &&
112      atoi(GetConfValue("Syslog/Facility"))==0){
113
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);
119
120     return -1;
121   }
122
123   return 0;
124 }
125
126 /**************************************************/
127 /*  initialize the Config                         */
128 /**************************************************/
129 void initConf(void)
130 {
131   /* as debug flag is used many times, put it in gloval variable */
132   debug=atoi(getConfValue("Debug"));
133 }
134
135 /**************************************************/
136 /* Finish Conf file usage                         */
137 /**************************************************/
138 void closeConfFile(void)
139 {
140   if(xmlRoot!=NULL)ezxml_free(xmlRoot);
141 }
142
143 /**************************************************/
144 /* Setup pointer to the matched ExtraSet          */ 
145 /**************************************************/
146 void setupConfExtra(char * userId,char *extraId)
147 {
148   char* progName;
149
150   /* init as no ExtraSet */
151   xmlExtraSet=NULL;
152
153   /* search the matching extra set (first match is employed) */
154   for(xml=ezxml_child(xmlRoot, "ExtraSet"); xml; xml=xml->next){
155     
156     /* if ExtraId is exist, check it */
157     if(!isNull(ezxml_attr(xml, "ExtraId"))){
158
159       /* if not match, go to next ExtraSet */
160       /* ('default' indicated in conf matchs to Null-extraId) */
161       if(isNull(extraId)){
162         if(strcmp("default", ezxml_attr(xml, "ExtraId"))!=0)continue;
163       }else{
164         if(strcmp(extraId, ezxml_attr(xml, "ExtraId"))!=0)continue;
165       }
166     }
167
168     /* if userID pattern is exist, check it */
169     if(!isNull(ezxml_attr(xml, "UserIdPattern"))){
170
171       /* if not matched, go to next ExtraSet */
172       if(RegExMatch(userId, ezxml_attr(xml, "UserIdPattern"))==FALSE) continue;
173     }
174
175     /* found matched ExtraSet */
176     break;
177   }
178
179   /* if found a matched ExtraSet, save the pointer */
180   if(xml!=NULL) xmlExtraSet=xml;
181
182   /* change syslog setting */
183   errToSyslog(atoi(GetConfValue("Syslog/Enable")));
184   progName=getProgramName();
185   openlog(progName, LOG_PID, atoi(GetConfValue("Syslog/Facility")));
186
187   /* reset config setting */
188   InitConf();
189 }
190
191 /***********************************************/
192 /* regular expression matching                 */
193 /*  inStr : string to match                    */
194 /*  regEx : regular expression                 */
195 /***********************************************/
196 int regExMatch(const char *inStr, const char *regEx)
197 {
198   regex_t reg;
199   int errcode;
200   int match;
201   char errbuff[WORDMAXLN];
202
203   /* compile regex */
204   if((errcode=regcomp(&reg, regEx, REG_NOSUB|REG_EXTENDED|REG_ICASE))!=0){
205     regerror(errcode, &reg, errbuff, WORDMAXLN);
206     err_msg("ERR at %s#%d: regex message=%s",__FILE__,__LINE__,errbuff);
207     match=FALSE;
208   }
209   
210   /* if compile is success, check the input string */
211   else{
212     if(regexec(&reg, inStr, (size_t)0, NULL, 0)==0) match=TRUE;
213     else match=FALSE;
214   }
215
216   regfree(&reg);
217
218   return match;
219 }
220
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)
227 {
228   char *pValue;
229   char *pValueExtra;
230   char *pStr;
231   char buff[BUFFMAXLN];
232
233   /* AuthServer setting is done in other routine */
234   if(strstr(name,"AuthServer/")==name) return GetConfAuthServer(name);
235
236   /* copy name to work area */
237   strlcpy(buff,name,BUFFMAXLN);
238
239   /* get first token */
240   pStr=strtok(buff, SEPARATOR);
241
242   /* set search start to root of tree */
243   xml=xmlRoot;
244
245   /* search the tree node for the name */
246   while(pStr!=NULL){
247     xml=ezxml_child(xml, pStr);
248     pStr=strtok(NULL, SEPARATOR);
249   }
250
251   /* get the node value */
252   pValue= ezxml_txt(xml);
253
254   /* if not get, write error message */
255   if(pValue==NULL){
256     err_msg("ERR at %s#%d: cannot get %s from conf file",__FILE__,__LINE__,name);
257   }
258
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))){
263     pValue=pValueExtra;
264   }
265
266   /* if syslog facility, the id is converted to raw value */
267   if(strcmp(name,"Syslog/Facility")==0){
268     pValue=convertToFacilityRaw(pValue);
269   }
270
271   /* return found value */
272   return pValue;
273 }
274
275 /************************************************/
276 /* get the value in extra set matched to ID     */
277 /************************************************/
278 char *getConfValueExtra(char *name)
279 {
280   char *pStr;
281   char buff[BUFFMAXLN];
282   ezxml_t xml;
283
284   if(xmlExtraSet==NULL) return "";
285
286   /* extract first token in name */
287   strlcpy(buff,name,BUFFMAXLN);
288   pStr=strtok(buff, SEPARATOR);  
289
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 "";
295
296   /* search the node matched to name */
297   pStr=strtok(NULL, SEPARATOR);
298   while(pStr!=NULL){
299     xml=ezxml_child(xml, pStr);
300     pStr=strtok(NULL, SEPARATOR);
301   }
302
303   /* return the found value */
304   return ezxml_txt(xml);
305 }
306
307
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)
313 {
314   char *pValue;
315   char *pStr;
316   char buff[BUFFMAXLN];
317   ezxml_t xml;
318
319   /* copy name to work area */
320   strlcpy(buff,name,BUFFMAXLN);
321
322   /* get first token */
323   pStr=strtok(buff, SEPARATOR);
324
325   /* it must be AuthServer. if not return */
326   if(strcmp(pStr, "AuthServer")!=0)return NULL;
327
328   /* if authserver pointer is not set, set it */
329   if(xmlAuthServer==NULL){
330     if(!selectNextAuthServer()) return NULL;
331   }
332
333   /* set search start to the saved pointer */
334   xml=xmlAuthServer;
335
336   /* search the tree node for the name */
337   pStr=strtok(NULL, SEPARATOR);
338   while(pStr!=NULL){
339     xml=ezxml_child(xml, pStr);
340     pStr=strtok(NULL, SEPARATOR);
341   }
342
343   /* get the node value */
344   pValue= ezxml_txt(xml);
345
346   /* if not get Protocol, write error message */
347   if(isNull(pValue)
348      && (strcmp(name,"AuthServer/Protocol")==0) ){
349     err_msg("ERR at %s#%d: cannot get %s from conf file",__FILE__,__LINE__,name);
350   }
351
352   /* return found value */
353   return pValue;
354 }
355
356 /**********************************/
357 /* select next authserver setting */
358 /**********************************/
359 int selectNextAuthServer(void){
360
361   ezxml_t xmlTmp; /* temporary variable */
362
363   /* first call (initialize) */
364   /* xmlAuthPointer is the static variable to save authserver pointer */
365   if(xmlAuthServer==NULL){
366
367     /* if not set, search the first authserver pointer */
368      xmlAuthServer=ezxml_child(xmlRoot, "AuthServer");
369      
370      /* if authserver is found in extra set, pointer is moved to it */ 
371      if(xmlExtraSet!=NULL){
372        xmlTmp=ezxml_child(xmlExtraSet, "AuthServer");
373        if(xmlTmp!=NULL){
374          xmlAuthServer=xmlTmp;
375        }
376      }
377   }
378
379   /* successive calls */
380   /* pointer is moved to next */
381   else{
382     xmlAuthServer=ezxml_next(xmlAuthServer);
383   }
384
385   /* if not found return False */
386   if(xmlAuthServer==NULL){
387     return FALSE;
388   }else{
389     return TRUE;
390   }
391 }
392
393 /**********************************************
394 reset pointer for auth server list
395 **********************************************/
396 void resetAuthServerPointer(void){
397   xmlAuthServer=NULL;
398 }
399
400 /***********************************************/
401 /* Convart the syslog facility id to raw value */
402 /***********************************************/
403 char *convertToFacilityRaw(char *pValue)
404 {
405   static char facility[WORDMAXLN];
406   int rawValue;
407
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;
416   else rawValue=0;
417
418   snprintf(facility, WORDMAXLN, "%d", rawValue);
419
420   return facility;
421 }
422
423 /**************************************************/
424 /*  get the first value as previous call           */
425 /*  (next node of the lowest level of tree)       */  
426 /**************************************************/
427 char *getFirstConfValue(char* name)
428 {
429    char *pValue;
430    pValue=GetConfValue(name);
431
432   /* save the pointer now */
433    xmlSave=xml;
434
435   /* return found value */
436   return pValue;
437 }
438
439 /**************************************************/
440 /*  get the next value as previous call           */
441 /*  (next node of the lowest level of tree)       */  
442 /**************************************************/
443 char *getNextConfValue(void)
444 {
445   char *pValue;
446
447   /* recover previous pointer */
448   xml=xmlSave;
449
450   /* get next node */
451   if(xml==NULL) return "";
452   xml = ezxml_next(xml);
453
454   /* save for next call */
455   xmlSave=xml;
456
457   /* get the node value */
458   pValue= ezxml_txt(xml);
459
460   /* if not get, write error message */
461   if(pValue==NULL) return "";
462
463   /* return found value */
464   return pValue;
465 }
466
467
468 /***********************************************/
469 /***********************************************/
470 int OpenConfFile(void){
471   int ret;
472   if(debug>1) err_msg("DEBUG:=>openConfFile( )");
473   ret = openConfFile();
474   if(debug>1) err_msg("DEBUG:(%d)<=openConfFile( )",ret);
475   return ret;
476 }
477
478 void CloseConfFile(void){
479   if(debug>1) err_msg("DEBUG:=>closeConfFile( )");
480   closeConfFile();
481   if(debug>1) err_msg("DEBUG:<=closeConfFile( )");
482 }
483
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( )");
488 }
489
490 char *GetConfValue(char *name){
491   char *ret;
492   if(debug>1) err_msg("DEBUG:=>getConfValue(%s)",name);
493   ret=getConfValue(name);
494   if(debug>1) err_msg("DEBUG:(%s)<=getConfValue( )",ret);
495   return ret;
496 }
497
498 char *GetConfValueExtra(char *name){
499   char *ret;
500   if(debug>1) err_msg("DEBUG:=>getConfValueExtra(%s)",name);
501   ret=getConfValueExtra(name);
502   if(debug>1) err_msg("DEBUG:(%s)<=getConfValueExtra( )",ret);
503   return ret;
504 }
505
506 char *GetConfAuthServer(char *name){
507   char *ret;
508   if(debug>1) err_msg("DEBUG:=>getConfAuthServer(%s)",name);
509   ret=getConfAuthServer(name);
510   if(debug>1) err_msg("DEBUG:(%s)<=getConfAuthServer( )",ret);
511   return ret;
512 }
513
514 int SelectNextAuthServer(void){
515   int ret;
516   if(debug>1) err_msg("DEBUG:=>selectNextAuthServer( )");
517   ret=selectNextAuthServer();
518   if(debug>1) err_msg("DEBUG:(%d)<=selectNextAuthServer( )",ret);
519   return ret;
520 }
521
522 void InitConf(void){
523   if(debug>1) err_msg("DEBUG:=>initConf( )");
524   initConf();
525   if(debug>1) err_msg("DEBUG:<=initConf( )");
526 }
527
528 int RegExMatch(const char *inStr, const char *regEx){
529   int ret;
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);
533   return ret;
534 }
535   
536 void ResetAuthServerPointer(void){
537   if(debug>1) err_msg("DEBUG:=>resetAuthServerPointer( )");
538   resetAuthServerPointer();
539   if(debug>1) err_msg("DEBUG:<=resetAuthServerPointer( )");
540 }
541
542 char *GetFirstConfValue(char* name){
543   char *ret;
544   if(debug>1) err_msg("DEBUG:=>getFirstConfValue( )");
545   ret=getFirstConfValue(name);
546   if(debug>1) err_msg("DEBUG:(%s)<=getFirstConfValue( )",ret);
547   return ret;
548 }
549
550 char *GetNextConfValue(void){
551   char *ret;
552   if(debug>1) err_msg("DEBUG:=>getNextConfValue( )");
553   ret=getNextConfValue();
554   if(debug>1) err_msg("DEBUG:(%s)<=getNextConfValue( )",ret);
555   return ret;
556 }
557