OSDN Git Service

4ebcd160c5acb96feb39368f7f1bd66580c2ff66
[opengate/opengate.git] / opengate / opengatesrv / get-param.c
1 /**************************************************
2 opengate server
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 "opengatesrv.h"
29 #include "../ezxml/ezxml.h"
30
31 #define CONFIG_VERSION "1.4.10"
32 #define SEPARATOR "/"
33
34 int debug=0;
35 static ezxml_t xmlRoot=NULL;
36 static ezxml_t xmlExtraSet=NULL;
37
38 char *getConfValueExtra(char *name);
39 char *getConfValue(char *name);
40 char *convertToFacilityRaw(char *pValue);
41
42 /**************************************************/
43 /* Prepare Conf file to use                       */
44 /* this is called before syslog setup             */
45 /**************************************************/
46 int openConfFile(void)
47 {
48   char buff[BUFFMAXLN];
49   char *s;
50   char *errMsg;
51
52   /* parse file and make tree */
53   if((xmlRoot = ezxml_parse_file(CONFIGFILE))==NULL){
54
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);
59      PutClientMsg(buff);
60
61     return -1;
62   }
63
64   /* to check error, convert to xml */
65   s=ezxml_toxml(xmlRoot);  free(s);
66   
67   /* if failed, show error message */
68   errMsg=(char *)ezxml_error(xmlRoot);
69
70   if(*errMsg!='\0'){
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);
78      PutClientMsg(buff);
79
80     return -1;
81   }
82
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);
91      PutClientMsg(buff);
92
93     return -1;
94   }
95
96   /* check the syslog */
97   if(atoi(GetConfValue("Syslog/Enable")) &&
98      atoi(GetConfValue("Syslog/Facility"))==0){
99
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);
104      PutClientMsg(buff);
105
106     return -1;
107   }
108
109   return 0;
110 }
111
112 /**************************************************/
113 /*  initialize the Config                         */
114 /**************************************************/
115 void initConf(void)
116 {
117   /* as debug flag is used many times, put it in gloval variable */
118   debug=atoi(getConfValue("Debug"));
119 }
120
121 /**************************************************/
122 /* Finish Conf file usage                         */
123 /**************************************************/
124 void closeConfFile(void)
125 {
126   if(xmlRoot!=NULL)ezxml_free(xmlRoot);
127 }
128
129 /**************************************************/
130 /* Setup pointer to the matched ExtraSet          */ 
131 /**************************************************/
132 void setupConfExtra(char * userId,char *extraId)
133 {
134   ezxml_t xml;
135
136   /* init as no ExtraSet */
137   xmlExtraSet=NULL;
138
139   if(isNull(extraId)) return;
140
141   /* search the matching extra set (first match is employed) */
142   for(xml=ezxml_child(xmlRoot, "ExtraSet"); xml; xml=xml->next){
143     
144     /* if extraID is not match, go to next ExtraSet */
145     if(strcmp(extraId, ezxml_attr(xml, "ExtraId"))!=0) continue;
146
147     /* if userID pattern is exist, check it */
148     if(!isNull(ezxml_attr(xml, "UserIdPattern"))){
149
150       /* if not matched, go to next ExtraSet */
151       if(RegExMatch(userId, ezxml_attr(xml, "UserIdPattern"))==FALSE)continue;
152     }
153
154     /* found matched ExtraSet */
155     break;
156   }
157
158   /* if found a matched ExtraSet, save the pointer */
159   if(xml!=NULL) xmlExtraSet=xml;
160
161   /* change syslog setting */
162   errToSyslog(atoi(GetConfValue("Syslog/Enable")));
163   openlog(GetConfValue("MainCgi"), LOG_PID, atoi(GetConfValue("Syslog/Facility")));
164
165   /* reset config setting */
166   InitConf();
167 }
168
169 /***********************************************/
170 /* regular expression matching                 */
171 /*  inStr : string to match                    */
172 /*  regEx : regular expression                 */
173 /***********************************************/
174 int regExMatch(const char *inStr, const char *regEx)
175 {
176   regex_t reg;
177   int errcode;
178   int match;
179   char errbuff[WORDMAXLN];
180
181   /* compile regex */
182   if((errcode=regcomp(&reg, regEx, REG_NOSUB|REG_EXTENDED|REG_ICASE))!=0){
183     regerror(errcode, &reg, errbuff, WORDMAXLN);
184     err_msg("ERR at %s#%d: regex message=%s",__FILE__,__LINE__,errbuff);
185     match=FALSE;
186   }
187   
188   /* if compile is success, check the input string */
189   else{
190     if(regexec(&reg, inStr, (size_t)0, NULL, 0)==0) match=TRUE;
191     else match=FALSE;
192   }
193
194   regfree(&reg);
195
196   return match;
197 }
198
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)
205 {
206   char *pValue;
207   char *pValueExtra;
208   char *pStr;
209   char buff[BUFFMAXLN];
210   ezxml_t xml;
211
212   /* copy name to work area */
213   strncpy(buff,name,BUFFMAXLN);
214
215   /* get first token */
216   pStr=strtok(buff, SEPARATOR);
217
218   /* set search start to root of tree */
219   xml=xmlRoot;
220
221   /* search the tree node for the name */
222   while(pStr!=NULL){
223     xml=ezxml_child(xml, pStr);
224     pStr=strtok(NULL, SEPARATOR);
225   }
226
227   /* get the node value */
228   pValue= ezxml_txt(xml);
229
230   /* if not get, write error message */
231   if(isNull(pValue)){
232     /* AuthServer parameters (except Protocol) might be omitted */
233     if((strstr(name,"AuthServer")==name)&&
234        strstr(name,"AuthServer/Protocol")==NULL){
235     }else{
236       /* other parameters must be set */
237       err_msg("ERR at %s#%d: cannot get %s from conf file",__FILE__,__LINE__,name);
238     }
239   }
240
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))){
245     pValue=pValueExtra;
246   }
247
248   /* if syslog facility, the id is converted to raw value */
249   if(strcmp(name,"Syslog/Facility")==0){
250     pValue=convertToFacilityRaw(pValue);
251   }
252
253   /* return found value */
254   return pValue;
255 }
256 /************************************************/
257 /* get the value in extra set matched to ID     */
258 /************************************************/
259 char *getConfValueExtra(char *name)
260 {
261   char *pStr;
262   char buff[BUFFMAXLN];
263   ezxml_t xml;
264
265   if(xmlExtraSet==NULL) return "";
266
267   /* extract first token in name */
268   strncpy(buff,name,BUFFMAXLN);
269   pStr=strtok(buff, SEPARATOR);  
270
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 "";
276
277   /* search the node matched to name */
278   pStr=strtok(NULL, SEPARATOR);
279   while(pStr!=NULL){
280     xml=ezxml_child(xml, pStr);
281     pStr=strtok(NULL, SEPARATOR);
282   }
283
284   /* return the found value */
285   return ezxml_txt(xml);
286 }
287
288 /***********************************************/
289 /* Convart the syslog facility id to raw value */
290 /***********************************************/
291 char *convertToFacilityRaw(char *pValue)
292 {
293   static char facility[WORDMAXLN];
294   int rawValue;
295
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;
304   else rawValue=0;
305
306   snprintf(facility, WORDMAXLN, "%d", rawValue);
307
308   return facility;
309 }
310
311 /***********************************************/
312 /***********************************************/
313 int OpenConfFile(void){
314   int ret;
315   if(debug>1) err_msg("DEBUG:=>openConfFile( )");
316   ret = openConfFile();
317   if(debug>1) err_msg("DEBUG:(%d)<=openConfFile( )",ret);
318   return ret;
319 }
320
321 void CloseConfFile(void){
322   if(debug>1) err_msg("DEBUG:=>closeConfFile( )");
323   closeConfFile();
324   if(debug>1) err_msg("DEBUG:<=closeConfFile( )");
325 }
326
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( )");
331 }
332
333 char *GetConfValue(char *name){
334   char *ret;
335   if(debug>1) err_msg("DEBUG:=>getConfValue(%s)",name);
336   ret=getConfValue(name);
337   if(debug>1) err_msg("DEBUG:(%s)<=getConfValue( )",ret);
338   return ret;
339 }
340
341 char *GetConfValueExtra(char *name){
342   char *ret;
343   if(debug>1) err_msg("DEBUG:=>getConfValueExtra(%s)",name);
344   ret=getConfValueExtra(name);
345   if(debug>1) err_msg("DEBUG:(%s)<=getConfValueExtra( )",ret);
346   return ret;
347 }
348
349 void InitConf(void){
350   if(debug>1) err_msg("DEBUG:=>initConf( )");
351   initConf();
352   if(debug>1) err_msg("DEBUG:<=initConf( )");
353 }
354
355 int RegExMatch(const char *inStr, const char *regEx){
356   int ret;
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);
360   return ret;
361 }
362