OSDN Git Service

Ver.0.8.1
[opengatem/opengatem.git] / mngsrc / cgi.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3  module for Communication through CGI 
4
5 Copyright (C) 2011 Opengate Project Team
6 Written by Yoshiaki Watanabe
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
22 Email: watanaby@is.saga-u.ac.jp
23
24 Programmed by Yoshiaki WATANABE
25 **************************************************/
26
27 #include        "opengatemmng.h"
28
29 /* convert one-char-hex "a" to one-number 0Xa */ 
30 #define hex2num(x)  ((x)>='A' ? ((x) & 0XDF) - 'A' +10 : ((x) - '0'))
31
32 void split(char content[], char *name[], char *value[], char *next[]);
33 void decode(char *string);
34
35 char macAddress[ADDRMAXLN]="";
36
37 /********************************************/
38 /* get Post data from the client  */
39 /********************************************/
40 int getPostData(char *content, int contentMaxLength)
41 {
42   int contentLen;
43
44   /* get content sent from web input */
45   if(isNull(getenv("CONTENT_LENGTH"))) return FALSE;
46   contentLen=atoi(getenv("CONTENT_LENGTH"));
47   contentLen++; /* for terminate ch */
48
49   /* if larger than buffer, cut off */
50   if(contentLen > contentMaxLength) contentLen=contentMaxLength;
51   if(fgets(content, contentLen, stdin) == NULL){
52     content[0]='\0';
53   }
54
55   return TRUE;
56 }
57
58 /********************************************/
59 /* get language form query string (in url lang=ja)  */
60 /********************************************/
61 int getLangFromQueryString(char* language){
62
63   int found=FALSE;
64   char queryStr[BUFFMAXLN];
65   char *name[1];
66   char *value[1];
67   char *next[1];
68   char *ptr;
69
70   /* get default language at the top of lang list */
71   sscanf(GetConfValue("HtmlLangs"), "%s", language);
72
73   /* if no string, return default */
74   if(isNull(getenv("QUERY_STRING"))) return FALSE;
75
76   /* get html access parameter string */
77   strncpy(queryStr, getenv("QUERY_STRING"), BUFFMAXLN);
78
79   /* split language in string [....&lang=ja&.....] */
80   ptr=queryStr;
81   while(ptr!=NULL){
82     split(ptr, name, value, next);
83     if(strstr(name[0], "lang")!=NULL){
84       if(!isNull(value[0])) strncpy(language, value[0], WORDMAXLN);
85       found=TRUE;
86     }
87     ptr=next[0];
88   }
89   return found;
90 }
91
92 /********************************************/
93 /* get redirected url form query string     */
94 /********************************************/
95 int getRedirectedUrlFromQueryString(char* redirectedUrl){
96
97   int found=FALSE;
98   char queryStr[BUFFMAXLN];
99   char *name[1];
100   char *value[1];
101   char *next[1];
102   char *ptr;
103
104   /* default is null */
105   redirectedUrl[0]='\0';
106
107   /* if no string, return default */
108   if(isNull(getenv("QUERY_STRING"))) return FALSE;
109
110   /* get html access parameter string */
111   strncpy(queryStr, getenv("QUERY_STRING"), BUFFMAXLN);
112
113   /* split language in string [....&redirectedurl=xxxx&.....] */
114   ptr=queryStr;
115   while(ptr!=NULL){
116     split(ptr, name, value, next);
117     if(strstr(name[0], "redirectedurl")!=NULL){
118       if(!isNull(value[0])) strncpy(redirectedUrl, value[0], BUFFMAXLN);
119       found=TRUE;
120     }
121     ptr=next[0];
122   }
123   return found;
124 }
125
126 /********************************************/
127 /* get mac address form query string (in url lang=ja)  */
128 /********************************************/
129 int getMacAddrFromQueryString(char* macAddress){
130
131   int found=FALSE;
132   char queryStr[BUFFMAXLN];
133   char *name[1];
134   char *value[1];
135   char *next[1];
136   char *ptr;
137
138   /* set default */
139   macAddress[0]='\0';
140
141   /* if no string returns, return default */
142   if(isNull(getenv("QUERY_STRING"))) return FALSE;
143
144   /* get html access parameter string */
145   strncpy(queryStr, getenv("QUERY_STRING"), BUFFMAXLN);
146
147   /* split in string [....&macaddr=xxxxx&.....] */
148   ptr=queryStr;
149   while(ptr!=NULL){
150     split(ptr, name, value, next);
151     if(strstr(name[0], "macaddr")!=NULL){
152       strncpy(macAddress, value[0], WORDMAXLN);
153       found=TRUE;
154     }
155     ptr=next[0];
156   }
157   decode(macAddress);
158   return found;
159
160 }
161
162 /********************************************/
163 /* get userid from environment variable     */
164 /*  long userid: "userid@extraid" is set    */
165 /********************************************/
166 int getUserIdFromEnv(char *userid){
167
168   int ret=FALSE;
169   char* pEnv=NULL;
170
171   /* if shibboleth or httpbasic, get uid from environment var */
172   /* shibboleth */
173   if(strcmp(GetConfValue("AuthServer/Protocol"), "shibboleth")==0){
174
175     /* get data from env-variables for uid and org attribute */
176     pEnv=getenvEx(GetConfValue("AuthServer/UidAttribute"),TRUE,TRUE); 
177     if(!isNull(pEnv)){
178       strncpy(userid, pEnv, USERMAXLN);
179
180       /* if orp string can be get from env var, concatenate it as uid@org */
181       pEnv=getenvEx(GetConfValue("AuthServer/OrgAttribute"),TRUE,TRUE);
182       if(!isNull(pEnv)){
183         strncat(userid, GetConfValue("UserIdSeparator"), USERMAXLN);
184         strncat(userid, pEnv, USERMAXLN);
185       } 
186       ret=TRUE;
187     }
188
189     /* get from env-variable for EPPN(edu person principal name) attribute */
190     else{
191       pEnv=getenvEx(GetConfValue("AuthServer/EppnAttribute"),TRUE,FALSE);
192       if(!isNull(pEnv)){
193         strncat(userid, pEnv, USERMAXLN);
194         ret=TRUE;
195       }
196     }
197
198     if(ret==FALSE){
199       err_msg("ERR at %s#%d: Cannot get user info from shibboleth",__FILE__,__LINE__);
200       PutMessageToClient("Cannot get user info from shibboleth<br>Check shibboleth setting in .htaccess, conf and others");
201       exit(0);
202     }    
203   }
204
205   /* httpbasic */
206   else if(strcmp(GetConfValue("AuthServer/Protocol"), "httpbasic")==0){
207     if(!isNull(getenv("REMOTE_USER"))){
208       strncpy(userid,getenv("REMOTE_USER"),USERMAXLN);
209       ret=TRUE;
210     }else{
211       err_msg("ERR at %s#%d: Cannot get user info from httpbasic",__FILE__,__LINE__);
212       ret=FALSE;
213       PutMessageToClient("Cannot get user info from http basic<br>Check http basic setting in .htaccess and other");
214       exit(0);
215     }
216   }
217   return ret;
218 }
219
220 /*******************************
221 get userid and password from post string
222 *******************************/
223 int getUserIdFromPostData(char* requestStr, char* userid, char* password){
224
225   char *name[1];
226   char *value[1];
227   char *next[1];
228   char *ptr;
229   int ret=FALSE;
230   char content[BUFFMAXLN];
231
232   /* if null string, return */
233   if(isNull(requestStr)) return FALSE;
234
235   /* copy it to work area */
236   strncpy(content, requestStr, BUFFMAXLN);
237
238   /* split request item and execute the request */
239   ptr=content;
240   while(ptr!=NULL){
241
242     /* pick up next item */
243     split(ptr, name, value, next);
244     
245     /* copy to var */
246     if(strcmp(name[0], "userid")==0){
247       strncpy(userid, value[0], USERMAXLN);
248       decode(userid);
249       ret=TRUE;
250     }
251     else if(strcmp(name[0], "password")==0){
252       strncpy(password, value[0], USERMAXLN);
253       decode(password);
254     }
255     
256     /* shift pointer to next item */
257     ptr=next[0];
258   }
259
260   /* if password is found in request string, clear the string */
261   if(!isNull(password)) requestStr[0]='\0';
262
263   return ret;
264 }
265
266 /********************************************/
267 /* analyze request for checking    */
268 /********************************************/
269 int analyzeCheckRequest(char *content, int* status, char* macAddress)
270 {
271   char *name[1];
272   char *value[1];
273   char *next[1];
274   char *ptr;
275
276   /* set default */
277   *status=NONE;
278   *macAddress='\0';
279
280   /* split request item and execute the request */
281   ptr=content;
282   while(!isNull(ptr)){
283
284     /* pick up next item */
285     split(ptr, name, value, next);
286
287     /* if item =status */
288     if(strcmp(name[0], "status")==0){
289       if(strcmp(value[0], "open")==0) *status=OPEN;
290       if(strcmp(value[0], "close")==0) *status=CLOSE;
291     }
292
293     /* if item = macaddr */
294     else if(strcmp(name[0], "macaddr")==0){
295       strncpy(macAddress, value[0], ADDRMAXLN);
296       decode(macAddress);
297     }
298     
299     /* shift pointer to next item */
300     ptr=next[0];
301   }
302
303   return TRUE;
304 }
305
306 /********************************************/
307 /* analyze request and execute request for registering   */
308 /********************************************/
309 int analyzeRegisterRequest(char *content, char* macAddress, char* deviceName, char* mailAddress)
310 {
311   char *name[1];
312   char *value[1];
313   char *next[1];
314   char *ptr;
315
316   /* set default */
317   *macAddress='\0';
318   *deviceName='\0';
319   *mailAddress='\0';
320
321   /* split request item and execute the request */
322   ptr=content;
323   while(!isNull(ptr)){
324
325     /* pick up next item */
326     split(ptr, name, value, next);
327
328     /* if item = macaddr */
329     if(strcmp(name[0], "macaddr")==0){
330       strncpy(macAddress, value[0], ADDRMAXLN);
331       decode(macAddress);
332
333     }
334     else if(strcmp(name[0], "device")==0){
335       strncpy(deviceName, value[0], WORDMAXLN);
336       decode(deviceName);
337     }
338     else if(strcmp(name[0], "mailaddr")==0){
339       strncpy(mailAddress, value[0], BUFFMAXLN);
340       decode(mailAddress);
341     }
342
343     /* shift pointer to next item */
344     ptr=next[0];
345   }
346
347   /* if illegal device name, return */
348   if(*deviceName=='\0'){
349     SetMessage(EmptyDeviceName); 
350     return FALSE;
351   }
352   if(!IsSafeString(deviceName, WORDMAXLN)){
353     SetMessage(IllegalCharInDevice);
354     return FALSE;
355   }
356
357   /* if illegal mail address, return */
358   if(*mailAddress!='\0'){
359     if(!IsSafeString(mailAddress, BUFFMAXLN)){
360       SetMessage(IllegalCharInMailAddr);
361       return FALSE;
362     }
363   }
364
365   /* if illegal mac address, return */
366   if(!ConvertMacAddr(macAddress)) return FALSE;
367
368   /* if already registered in db, return */
369   if(IsMacAddrFoundInMngDb(macAddress)){
370     SetMessage(ExistentMacAddr);
371     return FALSE;
372   }
373   
374   return TRUE;
375 }
376   
377 /******************************
378  convert mac address to regular format
379   [01:23:45:67:89:0a]
380 ******************************/
381 int convertMacAddr(char* macAddr){
382
383   int m[6]; /* mac address sequence */
384   int i;
385
386   /* normal form is hex:hex:hex:hex:hex:hex */
387   /*             or hex-hex-hex-hex-hex-hex */
388   /* and hex<256 */
389   if((sscanf(macAddr, "%x:%x:%x:%x:%x:%x", 
390              &m[0],&m[1],&m[2],&m[3],&m[4],&m[5])!=6)
391      && (sscanf(macAddr, "%x-%x-%x-%x-%x-%x", 
392                 &m[0],&m[1],&m[2],&m[3],&m[4],&m[5])!=6)){
393     SetMessage(IllegalMacAddrForm);
394     return FALSE;
395   }
396   for(i=0;i<6;i++){
397     if(m[i]>255){
398       SetMessage(IllegalMacAddr);
399       return FALSE;
400     }
401   }
402
403   /* format to [01:23:45:67:89:0a] */
404   snprintf(macAddr, ADDRMAXLN, "%02x:%02x:%02x:%02x:%02x:%02x",
405            m[0],m[1],m[2],m[3],m[4],m[5]);
406
407   return TRUE;
408 }
409
410 /******************************
411 check safe characters string 
412 ******************************/
413 int isSafeString(char* str, int length){
414   char allowableChar[]=" !#$&*+,-./:=?[]^_{|}@";
415   int i;
416   char ch;
417
418   /* scan all char in str */
419   for(i=0; i<length; i++){
420     ch=str[i];
421     
422     /* if reached to the end of string, return true */
423     if(ch=='\0') break;
424
425     /* if alpha or numeric is found, goto next char */
426     if(isalnum(ch)) continue;
427
428     /* if control or non ascii is found, return false */
429     if( (unsigned)ch < 0x20|| 0x7f < (unsigned)ch) return FALSE;
430     
431     /* if one of above list is found, goto next char */
432     if(strchr(allowableChar, ch)==NULL) return FALSE;
433   }
434
435   return TRUE;
436 }
437
438 /*********************************************/
439 /* put auth request page to client            */
440 /*********************************************/
441 int putAuthRequestPageToClient(char *language, char* cgiName, char* docName, char* redirectedUrl)
442 {
443   char authdoc[BUFFMAXLN];
444   FILE *fp;
445   char buff[BUFFMAXLN];
446
447   /* make read in path to the retry document */
448   snprintf(authdoc, BUFFMAXLN, "%s%s/%s/%s",GetConfValue("DocumentRoot"),
449           GetConfValue("OpengateDir"),language,docName);
450
451   /* replace keyword and send out the file */
452   printf("Content-type: text/html\r\n\r\n");
453
454   if((fp=fopen(authdoc, "r"))==NULL){
455     err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__, authdoc);
456     return FALSE;
457   }
458
459   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
460     HtmlReplace(buff, "%%CGINAME%%", cgiName);
461     HtmlReplace(buff, "%%REDIRECTEDURL%%", redirectedUrl);
462     if(strstr(buff, "%%ERRORLIST%%")!=NULL){
463       InsertMessageToPage(language);
464     }else{
465       printf("%s",buff);    
466     }
467   }
468   fclose(fp);
469   return TRUE;
470 }
471
472 /*********************************************/
473 /* deny message to the client            */
474 /*********************************************/
475 int putDenyToClient(char *language){
476   char denydoc[BUFFMAXLN];
477   FILE *fp;
478   char buff[BUFFMAXLN];
479
480   /* make read in path to the retry document */
481   snprintf(denydoc, BUFFMAXLN, "%s%s/%s/%s",GetConfValue("DocumentRoot"),
482           GetConfValue("OpengateDir"),language,GetConfValue("DenyDoc"));
483
484   /* replace keyword and send out the file */
485   printf("Content-type: text/html\r\n\r\n");
486
487   if((fp=fopen(denydoc, "r"))==NULL){
488     err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__, denydoc);
489     return FALSE;
490   }
491
492   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
493     if(strstr(buff, "%%ERRORLIST%%")!=NULL){
494       InsertMessageToPage(language);
495     }else{
496       printf("%s",buff);
497     }
498   }
499   fclose(fp);
500   return TRUE;
501 }
502
503 /*********************************************/
504 /* put response to client for check request   */
505 /*********************************************/
506 int putCheckPageToClient(char *language, char* userId, char* extraId)
507 {
508   char responsedoc[BUFFMAXLN];
509   FILE *fp;
510   char buff[BUFFMAXLN];
511   char cookie[SIDMAXLN];
512   char* chkCgi=GetConfValue("CheckCgi");
513   char* regCgi=GetConfValue("RegisterCgi");
514   char* timeout=GetConfValue("OpenTimeout");
515
516   /* make read in path to the document */
517   snprintf(responsedoc, BUFFMAXLN, "%s%s/%s/%s",GetConfValue("DocumentRoot"),
518           GetConfValue("OpengateDir"),language,GetConfValue("CheckDoc"));
519
520   /* send header */
521   printf("Content-type: text/html\r\n");
522
523   /* if no cookie, make, send, and save cookie */
524   if(!GetHttpCookie(cookie,GetConfValue("AuthAdminCookie"))){
525     CreateCookie(cookie);
526     printf("Set-Cookie: %s=%s;path=/;\r\n", GetConfValue("AuthAdminCookie"), cookie);
527     SaveCookieToWorkDb(cookie,userId, extraId, ADMINUSER);
528   }
529
530   /* end of http header */
531   printf("\r\n");
532
533   /* replace keyword and send out the file */
534   if((fp=fopen(responsedoc, "r"))==NULL){
535     err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__, responsedoc);
536     return FALSE;
537   }
538
539
540   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
541     HtmlReplace(buff, "%%CHECKCGI%%", chkCgi);
542     HtmlReplace(buff, "%%REGISTERCGI%%", regCgi);
543     HtmlReplace(buff, "%%TIMEOUT%%", timeout);
544     HtmlReplace(buff, "%%USERID%%", userId);
545
546     if(strstr(buff, "%%MACCHECKLIST%%")!=NULL){
547       PutMacCheckListToClient();
548     }
549     else if(strstr(buff, "%%ERRORLIST%%")!=NULL){
550       InsertMessageToPage(language);
551     }
552
553     else{
554       printf("%s",buff);    
555     }
556   }
557   fclose(fp);
558   return TRUE;
559 }
560 /*********************************************/
561 /* put response to client for register request */
562 /* ownReg:1=the page is controlled by owner, 0=under admin */
563 /*********************************************/
564 int putRegisterPageToClient(char *language, char* macAddress, char* deviceName, char* mailAddress, char* userId, char* extraId, int ownReg, char* redirectedUrl)
565 {
566   char responsedoc[BUFFMAXLN];
567   FILE *fp;
568   char buff[BUFFMAXLN];
569   char cookie[SIDMAXLN];
570   char* regCgi="";
571   char* regDoc="";
572
573
574   /* setup cgi and doc */
575   if(ownReg){
576     regCgi=GetConfValue("OwnCgi");
577     regDoc=GetConfValue("OwnRegisterDoc");
578   }else{
579     regCgi=GetConfValue("RegisterCgi");
580     regDoc=GetConfValue("RegisterDoc");
581   }
582   if(isNull(regCgi) || isNull(regDoc)){
583     err_msg("ERR at %s#%d: cannot find cgi/doc for reg in conf",__FILE__,__LINE__);
584     return FALSE;
585   }
586
587   /* make read in path to the document */
588   snprintf(responsedoc, BUFFMAXLN, "%s%s/%s/%s",GetConfValue("DocumentRoot"),
589           GetConfValue("OpengateDir"),language,regDoc);
590
591   /* send header */
592   printf("Content-type: text/html\r\n");
593
594   /* if no cookie, make, send, and save cookie */
595   if(!GetHttpCookie(cookie, GetConfValue("AuthUserCookie"))){
596     CreateCookie(cookie);
597     printf("Set-Cookie: %s=%s;path=/;\r\n", GetConfValue("AuthUserCookie"), cookie);
598     SaveCookieToWorkDb(cookie, userId, extraId, NORMALUSER);
599     SaveMailDefalutForCookieToWorkDb(cookie, mailAddress);
600   }
601
602   /* end of http header */
603   printf("\r\n");
604
605   if((fp=fopen(responsedoc, "r"))==NULL){
606     err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__, responsedoc);
607     return FALSE;
608   }
609
610   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
611     HtmlReplace(buff, "%%REGISTERCGI%%", regCgi);
612     HtmlReplace(buff, "%%MACADDR%%", macAddress);
613     HtmlReplace(buff, "%%DEVICE%%", deviceName);
614     HtmlReplace(buff, "%%MAILADDR%%", mailAddress);
615     HtmlReplace(buff, "%%USERID%%", userId);
616     HtmlReplace(buff, "%%REDIRECTEDURL%%", redirectedUrl);
617
618     if(strstr(buff, "%%MACREGLIST%%")!=NULL){
619       PutMacRegListToClient(userId, extraId);
620     }
621     else if(strstr(buff, "%%ERRORLIST%%")!=NULL){
622       InsertMessageToPage(language);
623     }
624     else{
625       printf("%s",buff);    
626     }
627   }
628   fclose(fp);
629   return TRUE;
630 }
631
632 /*********************************************/
633 /* put mac list in mac check table to the client */
634 /*********************************************/
635 void putMacCheckListToClient(void){
636
637   char macAddress[ADDRMAXLN];
638   char vendor[WORDMAXLN];
639   char ipv4[ADDRMAXLN];
640   char ipv6[ADDRMAXLN];
641   int firstRow=TRUE;
642   int inUse=FALSE;
643   int foundOnDb=FALSE;
644
645   /* get mac list from db and insert into html table */
646   while(GetNextRowInMacCheckTable(macAddress, ipv4, ipv6)){
647
648     /* get nic vendor from management db */
649     GetNicVendorFromMngDb(macAddress, vendor, WORDMAXLN);
650
651     /* is the terminal in use */
652     if(IsSessionFoundInSessionTable(macAddress) ||
653        IsActiveSessionFoundInOpengateSessionTable(macAddress)) inUse=TRUE;
654     else inUse = FALSE;
655     
656     /* is the terminal's MAC is registered in DB */
657     foundOnDb = IsMacAddrFoundInMngDb(macAddress);
658
659     /* print out table row */
660     /* the row is colored, if it is a candidate for registering */
661     /* the terminals inUse/foundInDb may be not the candidate */
662     if(inUse || foundOnDb){
663       printf("<tr align=middle>\n");
664     }else{
665       printf("<tr style='background-color: rgb(255,255,204);' align=middle>\n");
666     }
667
668     /* check radio button in first row */ 
669     if(firstRow){
670       printf("<td><input type='radio' name='macaddr' value='%s' checked></td>\n", macAddress);
671       firstRow=FALSE;
672     }else{
673       printf("<td><input type='radio' name='macaddr' value='%s'></td>\n", macAddress);
674     }
675
676     /* show macAddress, vendor, ipv4,ipv6 */
677     printf("<td>%s</td>\n", macAddress);
678     printf("<td>%s</td>\n",vendor);
679     printf("<td>%s</td>\n",ipv4);
680     printf("<td>%s</td>\n",ipv6);
681
682     /* show flags for inUse/foundInDb */
683     if(inUse) printf("<td>*</td>\n");
684     else  printf("<td><br></td>\n");
685     if(foundOnDb) printf("<td>*</td>\n");
686     else  printf("<td><br></td>\n");
687     printf("</tr>\n");
688   }
689 }
690
691 /*********************************************/
692 /* put mac regsitered list the client            */
693 /*********************************************/
694 void putMacRegListToClient(char* userId, char* extraId){
695
696   char deviceName[WORDMAXLN]="";
697   char entryDate[WORDMAXLN]="";
698   char limitDate[WORDMAXLN]="";
699   char status[WORDMAXLN]="";
700   char macAddress[ADDRMAXLN]="";
701   char mailAddress[BUFFMAXLN]="";
702
703   /* get registered mac list form db and insert */
704   while(GetNextMacAddrFromMngDb(userId,extraId,macAddress,deviceName,
705                                 entryDate,limitDate,status,mailAddress)){
706
707
708     printf("<tr align=middle>\n");
709     printf("<td>%s</td>\n", macAddress);
710     printf("<td>%s</td>\n",deviceName);
711     printf("<td>%s</td>\n",entryDate);
712     printf("<td>%s</td>\n",limitDate);
713     printf("<td>%s</td>\n",status);
714     printf("<td>%s</td>\n",mailAddress);
715     printf("</tr>\n");
716   }
717 }
718
719 /*********************************************/
720 /* put some message to the client            */
721 /*********************************************/
722 void putMessageToClient(char *message)
723 {
724   printf("Content-type: text/html\r\n\r\n");
725   printf("<HTML><HEAD><TITLE>OpengateMsg</TITLE></HEAD> \r\n");
726   printf("<BODY>\r\n");
727   printf("%s\r\n",     message);
728   printf("</BODY></HTML> \r\n\r\n");
729 }
730
731 /************************************************/
732 /* send page for returning to the previous page */
733 /************************************************/
734 void returnToRedirectedPage(char* redirectedUrl, char* language){
735   
736   char returndoc[BUFFMAXLN];
737   FILE *fp;
738   char buff[BUFFMAXLN];
739   char* waitTime="10";
740  
741   /* make read in path to the document */
742   snprintf(returndoc, BUFFMAXLN, "%s%s/%s/%s",GetConfValue("DocumentRoot"),
743           GetConfValue("OpengateDir"),language,GetConfValue("ReturnDoc"));
744
745   /* send header */
746   printf("Content-type: text/html\r\n\r\n");
747   
748   if((fp=fopen(returndoc, "r"))==NULL){
749     err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__, returndoc);
750     printf("Error! \r\n\r\n");
751     return;
752   }
753
754   if(!isNull(GetConfValue("ReturnWaitTime"))){
755     waitTime=GetConfValue("ReturnWaitTime");
756   }
757   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
758     HtmlReplace(buff, "%%WAITTIME%%", waitTime);
759     HtmlReplace(buff, "%%REDIRECTEDURL%%", redirectedUrl);
760     printf("%s",buff);    
761   }
762   fclose(fp);
763 }
764
765 /************************************/
766 /* split value for indicated name   */
767 /* in content  "name=value&..."     */
768 /************************************/
769 void split(char content[], char *name[], char *value[], char *next[])
770 {
771   char *pstr;
772   
773   /* set default */
774   name[0]=content;
775   value[0]=content+strlen(content);
776   next[0]=value[0];
777
778   /* set name end */
779   if((pstr=strchr(name[0],(int)'='))==NULL){
780     next[0]=NULL;
781     return;
782   }
783   *pstr='\0';
784   
785   /* set value start */
786   pstr++;
787   value[0]=pstr;
788   
789   /* set value end */
790   if((pstr=strchr(value[0],'&'))==NULL){
791     next[0]=NULL;
792     return;
793   }
794   *pstr='\0';
795
796   /* set next start */
797   pstr++;
798   next[0]=pstr;
799
800   return;
801 }
802
803 /**********************************/
804 /* decode text coding in web post */
805 /**********************************/
806 void decode(char *string)
807 {
808   char *pcheck, *pinsert;
809
810   pcheck=pinsert=string;
811   while(*pcheck != '\0'){
812     if(*pcheck == '+'){
813       *pinsert = ' ';
814     }else if(*pcheck == '%'){
815       *pinsert=(char)(hex2num(*(pcheck+1))*16 + hex2num(*(pcheck+2)));
816       pcheck+=2;
817     }else{
818       *pinsert=*pcheck;
819     }
820     pcheck++;
821     pinsert++;
822   }
823   *pinsert=*pcheck;
824 }
825
826
827 /*****************************************************/
828 /* replace beforeStr to afterStr in string in buff   */
829 /*****************************************************/
830 int htmlReplace(char* buff,char *beforeStr,char *afterStr)
831 {
832   char *pBuff , *pNext;
833   char tempBuff[BUFFMAXLN];
834   
835   if(buff==NULL) return 1;
836
837   strncpy(tempBuff, buff, BUFFMAXLN);
838   strncpy(buff,"",BUFFMAXLN);
839   
840   for(pBuff = tempBuff;
841       (pNext=StrSplit(pBuff, beforeStr)) != NULL;
842       pBuff = pNext){
843     strncat(buff,pBuff,BUFFMAXLN);
844     strncat(buff,afterStr,BUFFMAXLN);
845   }
846   strncat(buff,pBuff,BUFFMAXLN);
847   
848   return 0;
849 }
850
851 /*****************************************************/
852 /* split a str at delimStr and return the point      */
853 /*****************************************************/
854 char* strSplit(char* str,const char* delimStr)
855 {
856     char* delimPoint = strstr(str,delimStr);
857     const size_t delimLen = strlen(delimStr);
858
859     if(delimPoint == NULL) return NULL;
860     else{
861         *delimPoint = '\0';
862         delimPoint += delimLen; 
863     }
864     return delimPoint;
865 }
866
867
868 /**********************/
869 /* get HTTP-Cookie    */
870 /**********************/
871   /* cookie string examples 
872   "OpengateMmng=de..ac1&Userid=user1"
873   "OpengateMmng=de..ac1&Userid=user1; xxx=..; yyy=.."
874   "xxx=..; yyy=..; OpengateMmng=de..ac1&Userid=user1"
875   */
876 int getHttpCookie(char *cookie, char* cookieName){
877   char content[BUFFMAXLN];
878   char *name[1];
879   char *value[1];
880   char *next[1];
881   char *ptr=NULL;
882   char *ptrNext=NULL;
883
884   /* reset buffer */
885   cookie[0]='\0';
886
887  /* if exist cookie, copy it to work area */
888   if(isNull(getenv("HTTP_COOKIE"))) return FALSE;
889   strncpy(content, getenv("HTTP_COOKIE"), BUFFMAXLN);
890   ptr=content;
891
892   /* search 'OpengateMmng' cookie string (terminated by ; or \0) */
893   while(ptr!=NULL){
894     if((ptrNext=strstr(ptr, "; "))==NULL) break;          /* search "; " */
895     *ptrNext='\0';                               /* overwrite string end */
896     ptrNext++;                                 /* pointer to next string */
897     while(!isNull(ptrNext)&&*ptrNext==' ') ptrNext++;     /* skip spaces */
898     if(strstr(ptr, cookieName)==ptr) break;          /* exit at matching */
899     ptr=ptrNext;                                    /* check next string */
900   }
901
902   /* get valuses of cookie from "OpengateMmng=de..ac1" */
903   while(ptr!=NULL){
904     split(ptr, name, value, next);
905
906     if(strstr(name[0], cookieName)!=NULL){
907       strncpy(cookie, value[0], SIDMAXLN);
908     }
909     ptr=next[0];
910   }
911   
912   if(isNull(cookie)) return FALSE;
913   else return TRUE;
914 }
915
916 /*************************************
917 compare received cookie to previously saved one
918 *************************************/
919 int isCorrectCookie(char* cookie, int userType){
920   char userId[USERMAXLN];
921   char extraId[USERMAXLN];
922
923   /* compare http received cookie and DB readin cookie */
924   switch(userType){
925   case NORMALUSER:
926     GetHttpCookie(cookie, GetConfValue("AuthUserCookie"));
927     if(IsCookieFoundInWorkDb(cookie,userId,extraId,NORMALUSER)) return TRUE;
928     else return FALSE;
929   case ADMINUSER:
930     GetHttpCookie(cookie, GetConfValue("AuthAdminCookie"));
931     if(IsCookieFoundInWorkDb(cookie,userId,extraId,ADMINUSER)) return TRUE;
932     else return FALSE;
933   }
934   return FALSE;
935 }
936
937 /********************************************/
938 /* analyze update request and execute request    */
939 /********************************************/
940 int analyzeUpdateRequestAndExecute(char *requestStr, char* userId, char* extraId)
941 {
942   char *name[1];
943   char *value[1];
944   char *next[1];
945   char *ptr;
946   char macAddr[ADDRMAXLN];
947   int modified=FALSE;    /* database modification is executed */
948   int ret;
949   char content[BUFFMAXLN];
950
951   /* if null string, return */
952   if(isNull(requestStr)) return FALSE;
953
954   /* copy request string to work area */
955   strncpy(content, requestStr, BUFFMAXLN);
956
957   /* split request item and execute the request */
958   ptr=content;
959   while(ptr!=NULL){
960
961     /* pick up next item */
962     split(ptr, name, value, next);
963     
964     /* if item=delete, execute delete */
965     if(strcmp(value[0], "delete")==0){
966       strncpy(macAddr, name[0], ADDRMAXLN);
967       decode(macAddr);
968       if(ConvertMacAddr(macAddr)){
969         ret=DelMacAddrFromMngDb(macAddr);
970         if(ret){
971           modified=TRUE;
972           PutMacModifyLogToMngDb(userId, extraId, macAddr, 'D');
973           PutMacAddressToServers(macAddr);
974         }
975       }
976     }
977
978     /* if item = extend, execute extend */
979     else if(strcmp(value[0], "extend")==0){
980       strncpy(macAddr, name[0], ADDRMAXLN);
981       decode(macAddr);
982       if(ConvertMacAddr(macAddr)){
983         ret=RenewMacAddrInMngDb(macAddr);
984         if(ret){
985           modified=TRUE;
986           PutMacModifyLogToMngDb(userId, extraId, macAddr, 'E');
987           PutMacAddressToServers(macAddr);
988         }
989       }
990     }
991
992     /* if item = pause, execute pause */
993     else if(strcmp(value[0], "pause")==0){
994       strncpy(macAddr, name[0], ADDRMAXLN);
995       decode(macAddr);
996       if(ConvertMacAddr(macAddr)){
997         ret=PauseMacAddrInMngDb(macAddr);
998         if(ret){
999           modified=TRUE;
1000           PutMacModifyLogToMngDb(userId, extraId, macAddr, 'P');
1001           PutMacAddressToServers(macAddr);
1002         }
1003       }
1004     }
1005     
1006     /* shift pointer to next item */
1007     ptr=next[0];
1008   }
1009
1010   return modified;
1011 }
1012
1013
1014 /*********************************************/
1015 /* put response to client            */
1016 /* ownUpdate:1=page is controlled by owner, 0=under admin */
1017 /*********************************************/
1018 int putUpdatePageToClient(char *language, char* userId, char* extraId, int ownUpdate, char* redirectedUrl)
1019 {
1020   char responsedoc[BUFFMAXLN];
1021   FILE *fp;
1022   char buff[BUFFMAXLN];
1023   char cookie[SIDMAXLN];
1024   char* updateCgi="";
1025   char* updateDoc="";
1026   char mailDefault[BUFFMAXLN];
1027
1028   /* select update page for owner or administrator */
1029   if(ownUpdate){
1030     updateCgi=GetConfValue("OwnCgi");
1031     updateDoc=GetConfValue("OwnUpdateDoc");
1032   }else{
1033     updateCgi=GetConfValue("UpdateCgi");
1034     updateDoc=GetConfValue("UpdateDoc");
1035   }
1036   if(isNull(updateCgi) || isNull(updateDoc)){
1037     err_msg("ERR at %s#%d: cannot find cgi/doc for update in conf",__FILE__,__LINE__);
1038     return FALSE;
1039   }
1040
1041   /* make read in path to the retry document */
1042   snprintf(responsedoc, BUFFMAXLN, "%s%s/%s/%s",GetConfValue("DocumentRoot"),
1043           GetConfValue("OpengateDir"),language,updateDoc);
1044
1045   /* send header */
1046   printf("Content-type: text/html\r\n");
1047
1048   /* if no cookie, make, send, and save cookie */
1049   if(!GetHttpCookie(cookie, GetConfValue("AuthUserCookie"))){
1050     CreateCookie(cookie);
1051     printf("Set-Cookie: %s=%s;path=/;\r\n", GetConfValue("AuthUserCookie"), cookie);
1052     SaveCookieToWorkDb(cookie,userId, extraId, NORMALUSER);
1053     MakeMailDefault(userId, extraId, mailDefault);
1054     SaveMailDefalutForCookieToWorkDb(cookie, mailDefault);
1055   }
1056
1057   /* end of http header */
1058   printf("\r\n");
1059
1060   if((fp=fopen(responsedoc, "r"))==NULL){
1061     err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__, responsedoc);
1062     return FALSE;
1063   }
1064
1065   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
1066     HtmlReplace(buff, "%%CGINAME%%", updateCgi);
1067     HtmlReplace(buff, "%%USERID%%", userId);
1068     HtmlReplace(buff, "%%REDIRECTEDURL%%", redirectedUrl);
1069
1070     if(strstr(buff, "%%MACLIST%%")!=NULL){
1071       PutMacListToClient(userId,extraId);
1072     }
1073     else if(strstr(buff, "%%USAGELOG%%")!=NULL){
1074       PutUsageLogToClient(userId, extraId, language);
1075     }
1076     else if(strstr(buff, "%%ERRORLIST%%")!=NULL){
1077       InsertMessageToPage(language);
1078     }
1079     else{
1080       printf("%s",buff);    
1081     }
1082   }
1083   fclose(fp);
1084   return TRUE;
1085 }
1086
1087 /*********************************************/
1088 /* put mac list the client            */
1089 /*********************************************/
1090 void putMacListToClient(char* userId, char* extraId){
1091
1092   char deviceName[WORDMAXLN];
1093   char entryDate[WORDMAXLN];
1094   char limitDate[WORDMAXLN];
1095   char status[WORDMAXLN];
1096   char macAddr[ADDRMAXLN];
1097   char mailAddress[BUFFMAXLN];
1098
1099   /* make rows for deletion and extending */
1100   /* get registered mac list form db and insert */
1101   while(GetNextMacAddrFromMngDb(userId,extraId,macAddr,deviceName,
1102                                 entryDate,limitDate,status,mailAddress)){
1103     printf("<tr align=middle>\n");
1104     if(status[0]=='A'){
1105       printf("<td><input type=radio name=%s value=extend checked></td>\n",macAddr);
1106       printf("<td><input type=radio name=%s value=pause></td>\n",macAddr);
1107       printf("<td><input type=radio name=%s value=delete></td>\n",macAddr);
1108     }else{
1109       printf("<td>-</td>\n");
1110       printf("<td>-</td>\n");
1111       printf("<td>-</td>\n");
1112     }
1113     printf("<td>%s</td>\n", macAddr);
1114     printf("<td>%s</td>\n",deviceName);
1115     printf("<td>%s</td>\n",entryDate);
1116     printf("<td>%s</td>\n",limitDate);
1117     printf("<td>%s</td>\n",status);
1118     printf("</tr>\n");
1119   }
1120 }
1121
1122 /*********************************************/
1123 /* put usage log to the client            */
1124 /*********************************************/
1125 void putUsageLogToClient(char *userId, char* extraId, char* language){
1126
1127   char macAddr[ADDRMAXLN];
1128   char deviceName[WORDMAXLN];
1129   char openTime[WORDMAXLN];
1130   char gatewayName[WORDMAXLN];
1131   int weekday;
1132   char str[WORDMAXLN];
1133   char* p;
1134
1135   /* get usage log form db and insert as html table row */
1136   while(GetNextUsageLogFromMngDb(userId,extraId,macAddr,deviceName,
1137                                  openTime,gatewayName,&weekday)){
1138
1139     /* split hostname only */
1140     if((p=strchr(gatewayName,'.'))!=NULL) *p='\0';
1141
1142     /* put out table row */
1143     printf("<tr align=middle>\n");
1144     printf("<td>%s</td>\n", macAddr);
1145     printf("<td>%s</td>\n",deviceName);
1146     printf("<td>%s</td>\n",openTime);
1147     printf("<td>%s</td>\n",WeekdayStr(weekday, str, language));
1148     printf("<td>%s</td>\n",gatewayName);
1149     printf("</tr>\n");
1150   }
1151 }
1152
1153 /**********************************************/
1154 /* check allowable http-agent defined in conf */
1155 /**********************************************/
1156 int isAccessedFromAllowableAgent(void){
1157
1158   char* pAgent=NULL;
1159   char* pRegExPattern=NULL;
1160   int found=FALSE;
1161
1162   /* get agent string. if not, return false */
1163   if( isNull(pAgent=getenv("HTTP_USER_AGENT")) ) return FALSE;
1164
1165   /* get first reg expression in conf. if not, return true(not define=allow all) */
1166   if(isNull(pRegExPattern=GetFirstConfValue("AllowableAgentPattern"))){
1167     return TRUE;
1168   }
1169
1170   /* loop for patterns in conf */
1171   while(!isNull(pRegExPattern)){
1172
1173     /* if agent is matched to reg ex, return true. */
1174     /* last-arg 1 means case sensitive */
1175     if(RegExMatch(pAgent, pRegExPattern, 1)){
1176       found=TRUE;
1177       break;
1178     }
1179
1180     /* get next reg expression in conf */
1181     pRegExPattern=GetNextConfValue();
1182   }
1183
1184   /* if fail, print message */
1185   if(!found){
1186     err_msg("ERR at %s#%d: http-agent[%s] is not allowed in conf file",__FILE__,__LINE__, pAgent);
1187   }
1188
1189   return found;
1190 }
1191
1192
1193 /*******************************/
1194 /*******************************/
1195
1196 int GetPostData(char *content, int contentMaxLength){
1197   int ret;
1198   if(debug>1) err_msg("DEBUG:=>getPostData(%d)", contentMaxLength);
1199   ret=getPostData(content, contentMaxLength);
1200   if(debug>1) err_msg("DEBUG:%d<=getPostData(..)",ret);
1201   return ret;
1202 }
1203
1204 int GetLangFromQueryString(char* language){
1205   int ret;
1206   if(debug>1) err_msg("DEBUG:=>getLangFromQueryString( )");
1207   ret=getLangFromQueryString(language);
1208   if(debug>1) err_msg("DEBUG:%d<=getLangFromQueryString(%s)",ret,language);
1209   return ret;
1210 }
1211
1212 int GetRedirectedUrlFromQueryString(char* redirectedUrl){
1213   int ret;
1214   if(debug>1) err_msg("DEBUG:=>getRedirectedUrlFromQueryString( )");
1215   ret=getRedirectedUrlFromQueryString(redirectedUrl);
1216   if(debug>1) err_msg("DEBUG:%d<=getRedirectedUrlFromQueryString(%s)",ret,redirectedUrl);
1217   return ret;
1218 }
1219
1220 int GetMacAddrFromQueryString(char* macAddress){
1221   int ret;
1222   if(debug>1) err_msg("DEBUG:=>getMacAddrFromQueryString( )");
1223   ret=getMacAddrFromQueryString(macAddress);
1224   if(debug>1) err_msg("DEBUG:%d<=getMacAddrFromQueryString(%s)",ret,macAddress);
1225   return ret;
1226 }
1227
1228 int AnalyzeCheckRequest(char *content, int* status, char* macAddress){
1229   int ret;
1230   if(debug>1) err_msg("DEBUG:=>analyzeCheckRequest(%s)", content);
1231   ret=analyzeCheckRequest(content, status, macAddress);
1232   if(debug>1) err_msg("DEBUG:%d<=analyzeCheckRequest(%d,%s)",ret,*status, macAddress);
1233   return ret;
1234 }
1235
1236 int AnalyzeRegisterRequest(char *content, char* macAddress, char* deviceName, char* mailAddress){
1237   int ret;
1238   if(debug>1) err_msg("DEBUG:=>analyzeRegisterRequest(%s)", content);
1239   ret=analyzeRegisterRequest(content, macAddress, deviceName, mailAddress);
1240   if(debug>1) err_msg("DEBUG:%d<=analyzeRegisterRequest(%s,%s,%s)",ret, macAddress, deviceName, mailAddress);
1241   return ret;
1242 }
1243
1244 int PutDenyToClient(char *language){
1245   int ret;
1246   if(debug>1) err_msg("DEBUG:=>putDenyToClient(%s)",language);
1247   ret=putDenyToClient(language);
1248   if(debug>1) err_msg("DEBUG:(%d)<=putDenyToClient( )",ret);
1249   return ret;
1250 }
1251
1252 int PutCheckPageToClient(char *language, char* userId, char* extraId){
1253   int ret;
1254   if(debug>1) err_msg("DEBUG:=>putCheckPageToClient(%s,%s,%s)", 
1255                       language,userId,extraId);
1256   ret=putCheckPageToClient(language,userId,extraId);
1257   if(debug>1) err_msg("DEBUG:(%d)<=putCheckPageToClient( )",ret);
1258   return ret;
1259 }
1260
1261 int PutRegisterPageToClient(char *language, char* macAddress, char* deviceName, char* mailAddress, char* userId, char* extraId, int ownReg, char* redirectedUrl){
1262   int ret;
1263   if(debug>1) err_msg("DEBUG:=>putRegisterPageToClient(%s,%s,%s,%s,%s,%s,%d,%s)", language,macAddress,deviceName,mailAddress,userId,extraId,ownReg, redirectedUrl);
1264   ret=putRegisterPageToClient(language, macAddress, deviceName, mailAddress, userId, extraId, ownReg, redirectedUrl);
1265   if(debug>1) err_msg("DEBUG:(%d)<=putRegisterPageToClient( )",ret);
1266   return ret;
1267 }
1268
1269 void PutMacCheckListToClient(void){
1270   if(debug>1) err_msg("DEBUG:=>putMacCheckListToClient( )");
1271   putMacCheckListToClient();
1272   if(debug>1) err_msg("DEBUG:<=putMacCheckListToClient( )");
1273 }
1274
1275 void PutMacRegListToClient(char* userId, char* extraId){
1276   if(debug>1) err_msg("DEBUG:=>putMacRegListToClient( )");
1277   putMacRegListToClient(userId, extraId);
1278   if(debug>1) err_msg("DEBUG:<=putMacRegListToClient( )");
1279 }
1280
1281 void PutMessageToClient(char *message){
1282   if(debug>1) err_msg("DEBUG:=>putMessageToClient(%s)",message);
1283   putMessageToClient(message);
1284   if(debug>1) err_msg("DEBUG:<=putMessageToClient( )");
1285 }
1286
1287 void ReturnToRedirectedPage(char* redirectedUrl, char* language){
1288   if(debug>1) err_msg("DEBUG:=>returnToRedirectedPage(%s,%s)",redirectedUrl,language);
1289   returnToRedirectedPage(redirectedUrl, language);
1290   if(debug>1) err_msg("DEBUG:<=returnToRedirectedPage( )");
1291 }
1292
1293 int ConvertMacAddr(char* macAddr){
1294   int ret;
1295   if(debug>1) err_msg("DEBUG:=>convertMacAddr(%s)", macAddr);
1296   ret=convertMacAddr(macAddr);
1297   if(debug>1) err_msg("DEBUG:(%d)<=convertMacAddr(%s)",ret,macAddr);
1298   return ret;
1299 }
1300
1301 int IsSafeString(char* str, int length){
1302   int ret;
1303   if(debug>1) err_msg("DEBUG:=>isSafeString(%s,%d)",str,length);
1304   ret=isSafeString(str,length);
1305   if(debug>1) err_msg("DEBUG:(%d)<=isSafeString( )",ret);
1306   return ret;
1307 }
1308
1309 int HtmlReplace(char* buff,char *beforeStr,char *afterStr){
1310   int ret;
1311   if(debug>2) err_msg("DEBUG:=>htmlReplace(%s,%s,%s)",buff,beforeStr,afterStr);
1312   ret = htmlReplace(buff, beforeStr, afterStr);
1313   if(debug>2) err_msg("DEBUG:(%d)<=htmlReplace( )",ret);
1314   return ret;
1315 }
1316
1317 char* StrSplit(char* str,const char* delimStr){
1318   char* ret;
1319   if(debug>2) err_msg("DEBUG:=>strSplit(%s,%s)",str,delimStr);
1320   ret = strSplit(str, delimStr);
1321   if(debug>2) err_msg("DEBUG:(%s)<=strSplit( )",ret);
1322   return ret;
1323 }
1324
1325 int GetHttpCookie(char *cookie, char* cookieName){
1326   int ret;
1327   if(debug>1) err_msg("DEBUG:=>getHttpCookie(%s)", cookieName);
1328   ret = getHttpCookie(cookie, cookieName);
1329   if(debug>1) err_msg("DEBUG:(%d)<=getHttpCookie(%s)",ret, cookie);
1330   return ret;
1331 }
1332
1333 int IsCorrectCookie(char* cookie, int userType){
1334   int ret;
1335   if(debug>1) err_msg("DEBUG:=> isCorrectCookie(%d)", userType);
1336   ret =  isCorrectCookie(cookie,userType);
1337   if(debug>1) err_msg("DEBUG:(%d)<= isCorrectCookie(%s)",ret,cookie);
1338   return ret;
1339 }
1340
1341 int GetUserIdFromEnv(char *userid){
1342   int ret;
1343   if(debug>1) err_msg("DEBUG:=>getUserIdFromEnv(%s)",userid);
1344   ret = getUserIdFromEnv(userid);
1345   if(debug>1) err_msg("DEBUG:(%d)<=getUserIdFromEnv( )",ret);
1346   return ret;
1347 }
1348
1349 int GetUserIdFromPostData(char* requestStr, char* userid, char* password){
1350   int ret;
1351   if(debug>1) err_msg("DEBUG:=>getUserIdFromPostData(..)");
1352   ret = getUserIdFromPostData(requestStr,userid,password);
1353   if(debug>1) err_msg("DEBUG:(%d)<=getUserIdFromPostData(,%s,password)",ret,userid);
1354   return ret;
1355 }
1356
1357 int PutAuthRequestPageToClient(char *language, char* cgiName, char* docName, char* redirectedUrl){
1358   int ret;
1359   if(debug>1) err_msg("DEBUG:=>putAuthRequestPageToClient(%s,%s,%s)",language,cgiName, docName, redirectedUrl);
1360   ret=putAuthRequestPageToClient(language,cgiName, docName, redirectedUrl);
1361   if(debug>1) err_msg("DEBUG:(%d)<=putAuthRequestPageToClient( )",ret);
1362   return ret;
1363 }
1364
1365 int AnalyzeUpdateRequestAndExecute(char *content, char* userId, char* extraId){
1366   int ret;
1367
1368   if(debug>1) err_msg("DEBUG:=>analyzeUpdateRequestAndExecute(%s,%s,%s)", content,userId,extraId);
1369   ret=analyzeUpdateRequestAndExecute(content,userId,extraId);
1370   if(debug>1) err_msg("DEBUG:%d<=analyzeUpdateRequestAndExecute( )",ret);
1371   return ret;
1372 }
1373
1374 int PutUpdatePageToClient(char *language, char* userId, char* extraId, int ownUpdate, char* redirectedUrl){
1375   int ret;
1376   if(debug>1) err_msg("DEBUG:=>putUpdatePageToClient(%s,%s,%s,%d,%s)", 
1377                       language,userId,extraId,ownUpdate,redirectedUrl);
1378   ret=putUpdatePageToClient(language,userId,extraId,ownUpdate,redirectedUrl);
1379   if(debug>1) err_msg("DEBUG:(%d)<=putUpdatePageToClient( )",ret);
1380   return ret;
1381 }
1382
1383 void PutMacListToClient(char *userId, char* extraId){
1384   if(debug>1) err_msg("DEBUG:=>putMacListToClient(%s,%s)",userId,extraId);
1385   putMacListToClient(userId,extraId);
1386   if(debug>1) err_msg("DEBUG:<=putMacListToClient( )");
1387 }
1388
1389 void PutUsageLogToClient(char *userId, char* extraId, char* language){
1390   if(debug>1) err_msg("DEBUG:=>putUsageLogToClient(%s,%s,%s)",userId,extraId,language);
1391   putUsageLogToClient(userId,extraId,language);
1392   if(debug>1) err_msg("DEBUG:<=putUsageLogToClient( )");
1393 }
1394
1395 int IsAccessedFromAllowableAgent(void){
1396   int ret;
1397   if(debug>1) err_msg("DEBUG:=>isAccessedFromAllowableAgent( )");
1398   ret=isAccessedFromAllowableAgent();
1399   if(debug>1) err_msg("DEBUG:(%d)<=isAccessedFromAllowableAgent( )",ret);
1400   return ret;
1401 }