OSDN Git Service

modified up/reg pages
[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   strlcpy(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])) strlcpy(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   strlcpy(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])) strlcpy(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   strlcpy(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       strlcpy(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       strlcpy(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         strlcat(userid, GetConfValue("UserIdSeparator"), USERMAXLN);
184         strlcat(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         strlcat(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       strlcpy(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   strlcpy(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       strlcpy(userid, value[0], USERMAXLN);
248       decode(userid);
249       ret=TRUE;
250     }
251     else if(strcmp(name[0], "password")==0){
252       strlcpy(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       strlcpy(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       strlcpy(macAddress, value[0], ADDRMAXLN);
331       decode(macAddress);
332
333     }
334     else if(strcmp(name[0], "device")==0){
335       strlcpy(deviceName, value[0], WORDMAXLN);
336       decode(deviceName);
337     }
338     else if(strcmp(name[0], "mailaddr")==0){
339       strlcpy(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   char* maxDevicesStr;
573
574   /* get Maximum count of devices for an user from conf file */
575   maxDevicesStr=GetConfValue("MaxDevices");
576
577   /* setup cgi and doc */
578   if(ownReg){
579     regCgi=GetConfValue("OwnCgi");
580     regDoc=GetConfValue("OwnRegisterDoc");
581   }else{
582     regCgi=GetConfValue("RegisterCgi");
583     regDoc=GetConfValue("RegisterDoc");
584   }
585   if(isNull(regCgi) || isNull(regDoc)){
586     err_msg("ERR at %s#%d: cannot find cgi/doc for reg in conf",__FILE__,__LINE__);
587     return FALSE;
588   }
589
590   /* make read in path to the document */
591   snprintf(responsedoc, BUFFMAXLN, "%s%s/%s/%s",GetConfValue("DocumentRoot"),
592           GetConfValue("OpengateDir"),language,regDoc);
593
594   /* send header */
595   printf("Content-type: text/html\r\n");
596
597   /* if no cookie, make, send, and save cookie */
598   if(!GetHttpCookie(cookie, GetConfValue("AuthUserCookie"))){
599     CreateCookie(cookie);
600     printf("Set-Cookie: %s=%s;path=/;\r\n", GetConfValue("AuthUserCookie"), cookie);
601     SaveCookieToWorkDb(cookie, userId, extraId, NORMALUSER);
602     SaveMailDefalutForCookieToWorkDb(cookie, mailAddress);
603   }
604
605   /* end of http header */
606   printf("\r\n");
607
608   if((fp=fopen(responsedoc, "r"))==NULL){
609     err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__, responsedoc);
610     return FALSE;
611   }
612
613   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
614     HtmlReplace(buff, "%%REGISTERCGI%%", regCgi);
615     HtmlReplace(buff, "%%MACADDR%%", macAddress);
616     HtmlReplace(buff, "%%DEVICE%%", deviceName);
617     HtmlReplace(buff, "%%MAILADDR%%", mailAddress);
618     HtmlReplace(buff, "%%USERID%%", userId);
619     HtmlReplace(buff, "%%REDIRECTEDURL%%", redirectedUrl);
620     HtmlReplace(buff, "%%MAXDEVICES%%", maxDevicesStr);
621
622     if(strstr(buff, "%%MACREGLIST%%")!=NULL){
623       PutMacRegListToClient(userId, extraId);
624     }
625     else if(strstr(buff, "%%ERRORLIST%%")!=NULL){
626       InsertMessageToPage(language);
627     }
628     else{
629       printf("%s",buff);    
630     }
631   }
632   fclose(fp);
633   return TRUE;
634 }
635
636 /*********************************************/
637 /* put mac list in mac check table to the client */
638 /*********************************************/
639 void putMacCheckListToClient(void){
640
641   char macAddress[ADDRMAXLN];
642   char vendor[WORDMAXLN];
643   char ipv4[ADDRMAXLN];
644   char ipv6[ADDRMAXLN];
645   int firstRow=TRUE;
646   int inUse=FALSE;
647   int foundOnDb=FALSE;
648
649   /* get mac list from db and insert into html table */
650   while(GetNextRowInMacCheckTable(macAddress, ipv4, ipv6)){
651
652     /* get nic vendor from management db */
653     GetNicVendorFromMngDb(macAddress, vendor, WORDMAXLN);
654
655     /* is the terminal in use */
656     if(IsSessionFoundInSessionTable(macAddress) ||
657        IsActiveSessionFoundInOpengateSessionTable(macAddress)) inUse=TRUE;
658     else inUse = FALSE;
659     
660     /* is the terminal's MAC is registered in DB */
661     foundOnDb = IsMacAddrFoundInMngDb(macAddress);
662
663     /* print out table row */
664     /* the row is colored, if it is a candidate for registering */
665     /* the terminals inUse/foundInDb may be not the candidate */
666     if(inUse || foundOnDb){
667       printf("<tr align=middle>\n");
668     }else{
669       printf("<tr style='background-color: rgb(255,255,204);' align=middle>\n");
670     }
671
672     /* check radio button in first row */ 
673     if(firstRow){
674       printf("<td><input type='radio' name='macaddr' value='%s' checked></td>\n", macAddress);
675       firstRow=FALSE;
676     }else{
677       printf("<td><input type='radio' name='macaddr' value='%s'></td>\n", macAddress);
678     }
679
680     /* show macAddress, vendor, ipv4,ipv6 */
681     printf("<td>%s</td>\n", macAddress);
682     printf("<td>%s</td>\n",vendor);
683     printf("<td>%s</td>\n",ipv4);
684     printf("<td>%s</td>\n",ipv6);
685
686     /* show flags for inUse/foundInDb */
687     if(inUse) printf("<td>*</td>\n");
688     else  printf("<td><br></td>\n");
689     if(foundOnDb) printf("<td>*</td>\n");
690     else  printf("<td><br></td>\n");
691     printf("</tr>\n");
692   }
693 }
694
695 /*********************************************/
696 /* put mac regsitered list the client            */
697 /*********************************************/
698 void putMacRegListToClient(char* userId, char* extraId){
699
700   char deviceName[WORDMAXLN]="";
701   char entryDate[WORDMAXLN]="";
702   char limitDate[WORDMAXLN]="";
703   char status[WORDMAXLN]="";
704   char macAddress[ADDRMAXLN]="";
705   char mailAddress[BUFFMAXLN]="";
706
707   /* get registered mac list form db and insert */
708   while(GetNextMacAddrFromMngDb(userId,extraId,macAddress,deviceName,
709                                 entryDate,limitDate,status,mailAddress)){
710
711
712     printf("<tr align=middle>\n");
713     printf("<td>%s</td>\n", macAddress);
714     printf("<td>%s</td>\n",deviceName);
715     printf("<td>%s</td>\n",entryDate);
716     printf("<td>%s</td>\n",limitDate);
717     printf("<td>%s</td>\n",status);
718     printf("<td>%s</td>\n",mailAddress);
719     printf("</tr>\n");
720   }
721 }
722
723 /*********************************************/
724 /* put some message to the client            */
725 /*********************************************/
726 void putMessageToClient(char *message)
727 {
728   printf("Content-type: text/html\r\n\r\n");
729   printf("<HTML><HEAD><TITLE>OpengateMsg</TITLE></HEAD> \r\n");
730   printf("<BODY>\r\n");
731   printf("%s\r\n",     message);
732   printf("</BODY></HTML> \r\n\r\n");
733 }
734
735 /************************************************/
736 /* send page for returning to the previous page */
737 /************************************************/
738 void returnToRedirectedPage(char* redirectedUrl, char* language){
739   
740   char returndoc[BUFFMAXLN];
741   FILE *fp;
742   char buff[BUFFMAXLN];
743   char* waitTime="10";
744  
745   /* make read in path to the document */
746   snprintf(returndoc, BUFFMAXLN, "%s%s/%s/%s",GetConfValue("DocumentRoot"),
747           GetConfValue("OpengateDir"),language,GetConfValue("ReturnDoc"));
748
749   /* send header */
750   printf("Content-type: text/html\r\n\r\n");
751   
752   if((fp=fopen(returndoc, "r"))==NULL){
753     err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__, returndoc);
754     printf("Error! \r\n\r\n");
755     return;
756   }
757
758   if(!isNull(GetConfValue("ReturnWaitTime"))){
759     waitTime=GetConfValue("ReturnWaitTime");
760   }
761   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
762     HtmlReplace(buff, "%%WAITTIME%%", waitTime);
763     HtmlReplace(buff, "%%REDIRECTEDURL%%", redirectedUrl);
764     printf("%s",buff);    
765   }
766   fclose(fp);
767 }
768
769 /************************************/
770 /* split value for indicated name   */
771 /* in content  "name=value&..."     */
772 /************************************/
773 void split(char content[], char *name[], char *value[], char *next[])
774 {
775   char *pstr;
776   
777   /* set default */
778   name[0]=content;
779   value[0]=content+strlen(content);
780   next[0]=value[0];
781
782   /* set name end */
783   if((pstr=strchr(name[0],(int)'='))==NULL){
784     next[0]=NULL;
785     return;
786   }
787   *pstr='\0';
788   
789   /* set value start */
790   pstr++;
791   value[0]=pstr;
792   
793   /* set value end */
794   if((pstr=strchr(value[0],'&'))==NULL){
795     next[0]=NULL;
796     return;
797   }
798   *pstr='\0';
799
800   /* set next start */
801   pstr++;
802   next[0]=pstr;
803
804   return;
805 }
806
807 /**********************************/
808 /* decode text coding in web post */
809 /**********************************/
810 void decode(char *string)
811 {
812   char *pcheck, *pinsert;
813
814   pcheck=pinsert=string;
815   while(*pcheck != '\0'){
816     if(*pcheck == '+'){
817       *pinsert = ' ';
818     }else if(*pcheck == '%'){
819       *pinsert=(char)(hex2num(*(pcheck+1))*16 + hex2num(*(pcheck+2)));
820       pcheck+=2;
821     }else{
822       *pinsert=*pcheck;
823     }
824     pcheck++;
825     pinsert++;
826   }
827   *pinsert=*pcheck;
828 }
829
830
831 /*****************************************************/
832 /* replace beforeStr to afterStr in string in buff   */
833 /*****************************************************/
834 int htmlReplace(char* buff,char *beforeStr,char *afterStr)
835 {
836   char *pBuff , *pNext;
837   char tempBuff[BUFFMAXLN];
838   
839   if(buff==NULL) return 1;
840
841   strlcpy(tempBuff, buff, BUFFMAXLN);
842   strlcpy(buff,"",BUFFMAXLN);
843   
844   for(pBuff = tempBuff;
845       (pNext=StrSplit(pBuff, beforeStr)) != NULL;
846       pBuff = pNext){
847     strlcat(buff,pBuff,BUFFMAXLN);
848     strlcat(buff,afterStr,BUFFMAXLN);
849   }
850   strlcat(buff,pBuff,BUFFMAXLN);
851   
852   return 0;
853 }
854
855 /*****************************************************/
856 /* split a str at delimStr and return the point      */
857 /*****************************************************/
858 char* strSplit(char* str,const char* delimStr)
859 {
860     char* delimPoint = strstr(str,delimStr);
861     const size_t delimLen = strlen(delimStr);
862
863     if(delimPoint == NULL) return NULL;
864     else{
865         *delimPoint = '\0';
866         delimPoint += delimLen; 
867     }
868     return delimPoint;
869 }
870
871
872 /**********************/
873 /* get HTTP-Cookie    */
874 /**********************/
875   /* cookie string examples 
876   "OpengateMmng=de..ac1&Userid=user1"
877   "OpengateMmng=de..ac1&Userid=user1; xxx=..; yyy=.."
878   "xxx=..; yyy=..; OpengateMmng=de..ac1&Userid=user1"
879   */
880 int getHttpCookie(char *cookie, char* cookieName){
881   char content[BUFFMAXLN];
882   char *name[1];
883   char *value[1];
884   char *next[1];
885   char *ptr=NULL;
886   char *ptrNext=NULL;
887
888   /* reset buffer */
889   cookie[0]='\0';
890
891  /* if exist cookie, copy it to work area */
892   if(isNull(getenv("HTTP_COOKIE"))) return FALSE;
893   strlcpy(content, getenv("HTTP_COOKIE"), BUFFMAXLN);
894   ptr=content;
895
896   /* search 'OpengateMmng' cookie string (terminated by ; or \0) */
897   while(ptr!=NULL){
898     if((ptrNext=strstr(ptr, "; "))==NULL) break;          /* search "; " */
899     *ptrNext='\0';                               /* overwrite string end */
900     ptrNext++;                                 /* pointer to next string */
901     while(!isNull(ptrNext)&&*ptrNext==' ') ptrNext++;     /* skip spaces */
902     if(strstr(ptr, cookieName)==ptr) break;          /* exit at matching */
903     ptr=ptrNext;                                    /* check next string */
904   }
905
906   /* get valuses of cookie from "OpengateMmng=de..ac1" */
907   while(ptr!=NULL){
908     split(ptr, name, value, next);
909
910     if(strstr(name[0], cookieName)!=NULL){
911       strlcpy(cookie, value[0], SIDMAXLN);
912     }
913     ptr=next[0];
914   }
915   
916   if(isNull(cookie)) return FALSE;
917   else return TRUE;
918 }
919
920 /*************************************
921 compare received cookie to previously saved one
922 *************************************/
923 int isCorrectCookie(char* cookie, int userType){
924   char userId[USERMAXLN];
925   char extraId[USERMAXLN];
926
927   /* compare http received cookie and DB readin cookie */
928   switch(userType){
929   case NORMALUSER:
930     GetHttpCookie(cookie, GetConfValue("AuthUserCookie"));
931     if(IsCookieFoundInWorkDb(cookie,userId,extraId,NORMALUSER)) return TRUE;
932     else return FALSE;
933   case ADMINUSER:
934     GetHttpCookie(cookie, GetConfValue("AuthAdminCookie"));
935     if(IsCookieFoundInWorkDb(cookie,userId,extraId,ADMINUSER)) return TRUE;
936     else return FALSE;
937   }
938   return FALSE;
939 }
940
941 /********************************************/
942 /* analyze update request and execute request    */
943 /********************************************/
944 int analyzeUpdateRequestAndExecute(char *requestStr, char* userId, char* extraId)
945 {
946   char *name[1];
947   char *value[1];
948   char *next[1];
949   char *ptr;
950   char macAddr[ADDRMAXLN];
951   int modified=FALSE;    /* database modification is executed */
952   int ret;
953   char content[BUFFMAXLN];
954   char deviceName[WORDMAXLN]="";
955   char mailAddress[BUFFMAXLN]="";
956
957   /* if null string, return */
958   if(isNull(requestStr)) return FALSE;
959
960   /* copy request string to work area */
961   strlcpy(content, requestStr, BUFFMAXLN);
962
963   /* split request item and execute the request */
964   /* <input type=radio name=11:22:33:44:55:66 value=extend> */
965   /* <input type=radio name=11:22:33:44:55:66 value=pause> */
966   /* <input type=radio name=11:22:33:44:55:66 value=delete> */
967   /* <input type=hidden name=11:22:33:44:55:66 value=mail:wata@foo.bar> */
968   /* <input type=hidden name=11:22:33:44:55:66 value=name:iPhone01> */
969
970   ptr=content;
971   while(ptr!=NULL){
972
973     /* pick up next item */
974     split(ptr, name, value, next);
975     
976     /* if item=delete, execute delete */
977     if(strcmp(value[0], "delete")==0){
978       strlcpy(macAddr, name[0], ADDRMAXLN);
979       decode(macAddr);
980       if(ConvertMacAddr(macAddr)){
981         ret=DelMacAddrFromMngDb(macAddr);
982         if(ret){
983           modified=TRUE;
984           PutMacModifyLogToMngDb(userId, extraId, macAddr, 'D');
985           PutMacAddressToServers(macAddr);
986         }
987       }
988     }
989
990     /* if item = extend, execute extend */
991     else if(strcmp(value[0], "extend")==0){
992       strlcpy(macAddr, name[0], ADDRMAXLN);
993       decode(macAddr);
994       if(ConvertMacAddr(macAddr)){
995         ret=RenewMacAddrInMngDb(macAddr);
996         if(ret){
997           modified=TRUE;
998           PutMacModifyLogToMngDb(userId, extraId, macAddr, 'E');
999           PutMacAddressToServers(macAddr);
1000         }
1001       }
1002     }
1003
1004     /* if item = pause, execute pause */
1005     else if(strcmp(value[0], "pause")==0){
1006       strlcpy(macAddr, name[0], ADDRMAXLN);
1007       decode(macAddr);
1008       if(ConvertMacAddr(macAddr)){
1009         ret=PauseMacAddrInMngDb(macAddr);
1010         if(ret){
1011           modified=TRUE;
1012           PutMacModifyLogToMngDb(userId, extraId, macAddr, 'P');
1013           PutMacAddressToServers(macAddr);
1014         }
1015       }
1016     }
1017
1018     /* if item = name xxxx, execute renaming the device name */
1019     else if(strstr(value[0], "name+")==value[0]){
1020       strlcpy(macAddr, name[0], ADDRMAXLN);
1021       decode(macAddr);
1022       if(ConvertMacAddr(macAddr)){
1023
1024         /* when value=name xxxx, rename device-name to xxxx */
1025         /* copy limitted length and decode */
1026         strlcpy(deviceName, value[0]+5, WORDMAXLN);
1027         decode(deviceName);
1028
1029         /* if illegal device name, return */
1030         if(*deviceName=='\0'){
1031           SetMessage(EmptyDeviceName); 
1032           return FALSE;
1033         }
1034         if(!IsSafeString(deviceName, WORDMAXLN)){
1035           SetMessage(IllegalCharInDevice);
1036           return FALSE;
1037         }
1038
1039         /* update the device name in db */
1040         RenameDeviceNameInMngDb(macAddr, deviceName);
1041       }
1042     }
1043
1044     /* if item = mail xxxx, execute renaming the mail address */
1045     else if(strstr(value[0], "mail+")==value[0]){
1046       strlcpy(macAddr, name[0], ADDRMAXLN);
1047       decode(macAddr);
1048       if(ConvertMacAddr(macAddr)){
1049
1050         /* when value=mail xxxx, rename mail-address to xxxx */
1051         /* copy limitted length and decode */
1052         strlcpy(mailAddress, value[0]+5, BUFFMAXLN);
1053         decode(mailAddress);
1054
1055         /* if illegal mail address, return */
1056         if(*mailAddress!='\0'){
1057           if(!IsSafeString(mailAddress, BUFFMAXLN)){
1058             SetMessage(IllegalCharInMailAddr);
1059             return FALSE;
1060           }
1061         }
1062         
1063         /* update the device name in db */
1064         RenameMailAddressInMngDb(macAddr, mailAddress);
1065       }
1066     }
1067     
1068     /* shift pointer to next item */
1069     ptr=next[0];
1070   }
1071
1072   return modified;
1073 }
1074
1075 /*********************************************/
1076 /* put response to client            */
1077 /* ownUpdate:1=page is controlled by owner, 0=under admin */
1078 /*********************************************/
1079 int putUpdatePageToClient(char *language, char* userId, char* extraId, int ownUpdate, char* redirectedUrl)
1080 {
1081   char responsedoc[BUFFMAXLN];
1082   FILE *fp;
1083   char buff[BUFFMAXLN];
1084   char cookie[SIDMAXLN];
1085   char* updateCgi="";
1086   char* updateDoc="";
1087   char mailDefault[BUFFMAXLN];
1088
1089   /* select update page for owner or administrator */
1090   if(ownUpdate){
1091     updateCgi=GetConfValue("OwnCgi");
1092     updateDoc=GetConfValue("OwnUpdateDoc");
1093   }else{
1094     updateCgi=GetConfValue("UpdateCgi");
1095     updateDoc=GetConfValue("UpdateDoc");
1096   }
1097   if(isNull(updateCgi) || isNull(updateDoc)){
1098     err_msg("ERR at %s#%d: cannot find cgi/doc for update in conf",__FILE__,__LINE__);
1099     return FALSE;
1100   }
1101
1102   /* make read in path to the retry document */
1103   snprintf(responsedoc, BUFFMAXLN, "%s%s/%s/%s",GetConfValue("DocumentRoot"),
1104           GetConfValue("OpengateDir"),language,updateDoc);
1105
1106   /* send header */
1107   printf("Content-type: text/html\r\n");
1108
1109   /* if no cookie, make, send, and save cookie */
1110   if(!GetHttpCookie(cookie, GetConfValue("AuthUserCookie"))){
1111     CreateCookie(cookie);
1112     printf("Set-Cookie: %s=%s;path=/;\r\n", GetConfValue("AuthUserCookie"), cookie);
1113     SaveCookieToWorkDb(cookie,userId, extraId, NORMALUSER);
1114     MakeMailDefault(userId, extraId, mailDefault);
1115     SaveMailDefalutForCookieToWorkDb(cookie, mailDefault);
1116   }
1117
1118   /* end of http header */
1119   printf("\r\n");
1120
1121   if((fp=fopen(responsedoc, "r"))==NULL){
1122     err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__, responsedoc);
1123     return FALSE;
1124   }
1125
1126   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
1127
1128     HtmlReplace(buff, "%%CGINAME%%", updateCgi);
1129     HtmlReplace(buff, "%%USERID%%", userId);
1130     HtmlReplace(buff, "%%REDIRECTEDURL%%", redirectedUrl);
1131
1132     if(strstr(buff, "%%MACLIST%%")!=NULL){
1133       PutMacListToClient(userId,extraId);
1134     }
1135     else if(strstr(buff, "%%USAGELOG%%")!=NULL){
1136       PutUsageLogToClient(userId, extraId, language);
1137     }
1138     else if(strstr(buff, "%%ERRORLIST%%")!=NULL){
1139       InsertMessageToPage(language);
1140     }
1141     else{
1142       printf("%s",buff);    
1143     }
1144   }
1145   fclose(fp);
1146   return TRUE;
1147 }
1148
1149 /*********************************************/
1150 /* put mac list the client            */
1151 /*********************************************/
1152 void putMacListToClient(char* userId, char* extraId){
1153
1154   char deviceName[WORDMAXLN];
1155   char entryDate[WORDMAXLN];
1156   char limitDate[WORDMAXLN];
1157   char status[WORDMAXLN];
1158   char macAddr[ADDRMAXLN];
1159   char mailAddress[BUFFMAXLN];
1160
1161   /* make rows for deletion and extending */
1162   /* get registered mac list form db and insert */
1163   while(GetNextMacAddrFromMngDb(userId,extraId,macAddr,deviceName,
1164                                 entryDate,limitDate,status,mailAddress)){
1165     printf("<tr align=middle>\n");
1166     if((status[0]=='A')||(status[0]=='P')){
1167       printf("<td><input type=radio name=%s value=extend checked></td>\n",
1168              macAddr);
1169       printf("<td><input type=radio name=%s value=pause></td>\n",macAddr);
1170       printf("<td><input type=radio name=%s value=delete></td>\n",macAddr);
1171     }else{
1172       printf("<td>-</td>\n");
1173       printf("<td>-</td>\n");
1174       printf("<td>-</td>\n");
1175     }
1176     printf("<td>%s</td>\n", macAddr);
1177     printf("<td onclick=\"editstring('%s','name',this)\">%s</td>\n",
1178            macAddr,deviceName);
1179     printf("<td onclick=\"editstring('%s','mail',this)\">%s</td>\n",
1180            macAddr,mailAddress);
1181     printf("<td>%s</td>\n",entryDate);
1182     printf("<td>%s</td>\n",limitDate);
1183     printf("<td>%s</td>\n",status);
1184     printf("</tr>\n");
1185   }
1186 }
1187
1188 /*********************************************/
1189 /* put usage log to the client            */
1190 /*********************************************/
1191 void putUsageLogToClient(char *userId, char* extraId, char* language){
1192
1193   char macAddr[ADDRMAXLN]="";
1194   char deviceName[WORDMAXLN]="";
1195   char openTime[WORDMAXLN]="";
1196   char closeTime[WORDMAXLN]="";
1197   char gatewayName[WORDMAXLN]="";
1198   char beginTime[WORDMAXLN]="";
1199   char endTime[WORDMAXLN]="";
1200   int dateCount=0;
1201   char* p;
1202   int count=0;
1203
1204   /* get usage log from db and insert as JSON format */
1205   while(GetNextUsageLogFromMngDb(userId,extraId,macAddr,deviceName,
1206                                  openTime,closeTime,gatewayName)){
1207
1208     /* split hostname only */
1209     if((p=strchr(gatewayName,'.'))!=NULL) *p='\0';
1210
1211     /* put out table row */
1212     printf("\t %d:{\n", count);
1213     printf("\t\t macAddress: '%s',\n", macAddr);
1214     printf("\t\t device: '%s',\n",deviceName);
1215     printf("\t\t openTime: '%s',\n",openTime);
1216     printf("\t\t closeTime: '%s',\n",closeTime);
1217     printf("\t\t gateway: '%s'\n",gatewayName);
1218     printf("\t },\n");
1219
1220     /* increment log item counter */
1221     count++;
1222   }
1223
1224   /* put out informations for the list */
1225   GetTimeRangeToShowLog(beginTime, endTime, &dateCount);
1226   printf("\t info:{\n");
1227   printf("\t\t beginTime: '%s',\n", beginTime);
1228   printf("\t\t endTime: '%s',\n", endTime);
1229   printf("\t\t listCount: %d,\n", count);
1230   printf("\t\t dateCount: %d\n", dateCount);
1231   printf("\t }\n");
1232 }
1233
1234 /**********************************************/
1235 /* check allowable http-agent defined in conf */
1236 /**********************************************/
1237 int isAccessedFromAllowableAgent(void){
1238
1239   char* pAgent=NULL;
1240   char* pRegExPattern=NULL;
1241   int found=FALSE;
1242
1243   /* get agent string. if not, return false */
1244   if( isNull(pAgent=getenv("HTTP_USER_AGENT")) ) return FALSE;
1245
1246   /* get first reg expression in conf. if not, return true(not define=allow all) */
1247   if(isNull(pRegExPattern=GetFirstConfValue("AllowableAgentPattern"))){
1248     return TRUE;
1249   }
1250
1251   /* loop for patterns in conf */
1252   while(!isNull(pRegExPattern)){
1253
1254     /* if agent is matched to reg ex, return true. */
1255     /* last-arg 1 means case sensitive */
1256     if(RegExMatch(pAgent, pRegExPattern, 1)){
1257       found=TRUE;
1258       break;
1259     }
1260
1261     /* get next reg expression in conf */
1262     pRegExPattern=GetNextConfValue();
1263   }
1264
1265   /* if fail, print message */
1266   if(!found){
1267     err_msg("ERR at %s#%d: http-agent[%s] is not allowed in conf file",__FILE__,__LINE__, pAgent);
1268   }
1269
1270   return found;
1271 }
1272
1273 /*******************************/
1274 /*******************************/
1275
1276 int GetPostData(char *content, int contentMaxLength){
1277   int ret;
1278   if(debug>1) err_msg("DEBUG:=>getPostData(%d)", contentMaxLength);
1279   ret=getPostData(content, contentMaxLength);
1280   if(debug>1) err_msg("DEBUG:%d<=getPostData(..)",ret);
1281   return ret;
1282 }
1283
1284 int GetLangFromQueryString(char* language){
1285   int ret;
1286   if(debug>1) err_msg("DEBUG:=>getLangFromQueryString( )");
1287   ret=getLangFromQueryString(language);
1288   if(debug>1) err_msg("DEBUG:%d<=getLangFromQueryString(%s)",ret,language);
1289   return ret;
1290 }
1291
1292 int GetRedirectedUrlFromQueryString(char* redirectedUrl){
1293   int ret;
1294   if(debug>1) err_msg("DEBUG:=>getRedirectedUrlFromQueryString( )");
1295   ret=getRedirectedUrlFromQueryString(redirectedUrl);
1296   if(debug>1) err_msg("DEBUG:%d<=getRedirectedUrlFromQueryString(%s)",ret,redirectedUrl);
1297   return ret;
1298 }
1299
1300 int GetMacAddrFromQueryString(char* macAddress){
1301   int ret;
1302   if(debug>1) err_msg("DEBUG:=>getMacAddrFromQueryString( )");
1303   ret=getMacAddrFromQueryString(macAddress);
1304   if(debug>1) err_msg("DEBUG:%d<=getMacAddrFromQueryString(%s)",ret,macAddress);
1305   return ret;
1306 }
1307
1308 int AnalyzeCheckRequest(char *content, int* status, char* macAddress){
1309   int ret;
1310   if(debug>1) err_msg("DEBUG:=>analyzeCheckRequest(%s)", content);
1311   ret=analyzeCheckRequest(content, status, macAddress);
1312   if(debug>1) err_msg("DEBUG:%d<=analyzeCheckRequest(%d,%s)",ret,*status, macAddress);
1313   return ret;
1314 }
1315
1316 int AnalyzeRegisterRequest(char *content, char* macAddress, char* deviceName, char* mailAddress){
1317   int ret;
1318   if(debug>1) err_msg("DEBUG:=>analyzeRegisterRequest(%s)", content);
1319   ret=analyzeRegisterRequest(content, macAddress, deviceName, mailAddress);
1320   if(debug>1) err_msg("DEBUG:%d<=analyzeRegisterRequest(%s,%s,%s)",ret, macAddress, deviceName, mailAddress);
1321   return ret;
1322 }
1323
1324 int PutDenyToClient(char *language){
1325   int ret;
1326   if(debug>1) err_msg("DEBUG:=>putDenyToClient(%s)",language);
1327   ret=putDenyToClient(language);
1328   if(debug>1) err_msg("DEBUG:(%d)<=putDenyToClient( )",ret);
1329   return ret;
1330 }
1331
1332 int PutCheckPageToClient(char *language, char* userId, char* extraId){
1333   int ret;
1334   if(debug>1) err_msg("DEBUG:=>putCheckPageToClient(%s,%s,%s)", 
1335                       language,userId,extraId);
1336   ret=putCheckPageToClient(language,userId,extraId);
1337   if(debug>1) err_msg("DEBUG:(%d)<=putCheckPageToClient( )",ret);
1338   return ret;
1339 }
1340
1341 int PutRegisterPageToClient(char *language, char* macAddress, char* deviceName, char* mailAddress, char* userId, char* extraId, int ownReg, char* redirectedUrl){
1342   int ret;
1343   if(debug>1) err_msg("DEBUG:=>putRegisterPageToClient(%s,%s,%s,%s,%s,%s,%d,%s)", language,macAddress,deviceName,mailAddress,userId,extraId,ownReg, redirectedUrl);
1344   ret=putRegisterPageToClient(language, macAddress, deviceName, mailAddress, userId, extraId, ownReg, redirectedUrl);
1345   if(debug>1) err_msg("DEBUG:(%d)<=putRegisterPageToClient( )",ret);
1346   return ret;
1347 }
1348
1349 void PutMacCheckListToClient(void){
1350   if(debug>1) err_msg("DEBUG:=>putMacCheckListToClient( )");
1351   putMacCheckListToClient();
1352   if(debug>1) err_msg("DEBUG:<=putMacCheckListToClient( )");
1353 }
1354
1355 void PutMacRegListToClient(char* userId, char* extraId){
1356   if(debug>1) err_msg("DEBUG:=>putMacRegListToClient( )");
1357   putMacRegListToClient(userId, extraId);
1358   if(debug>1) err_msg("DEBUG:<=putMacRegListToClient( )");
1359 }
1360
1361 void PutMessageToClient(char *message){
1362   if(debug>1) err_msg("DEBUG:=>putMessageToClient(%s)",message);
1363   putMessageToClient(message);
1364   if(debug>1) err_msg("DEBUG:<=putMessageToClient( )");
1365 }
1366
1367 void ReturnToRedirectedPage(char* redirectedUrl, char* language){
1368   if(debug>1) err_msg("DEBUG:=>returnToRedirectedPage(%s,%s)",redirectedUrl,language);
1369   returnToRedirectedPage(redirectedUrl, language);
1370   if(debug>1) err_msg("DEBUG:<=returnToRedirectedPage( )");
1371 }
1372
1373 int ConvertMacAddr(char* macAddr){
1374   int ret;
1375   if(debug>1) err_msg("DEBUG:=>convertMacAddr(%s)", macAddr);
1376   ret=convertMacAddr(macAddr);
1377   if(debug>1) err_msg("DEBUG:(%d)<=convertMacAddr(%s)",ret,macAddr);
1378   return ret;
1379 }
1380
1381 int IsSafeString(char* str, int length){
1382   int ret;
1383   if(debug>1) err_msg("DEBUG:=>isSafeString(%s,%d)",str,length);
1384   ret=isSafeString(str,length);
1385   if(debug>1) err_msg("DEBUG:(%d)<=isSafeString( )",ret);
1386   return ret;
1387 }
1388
1389 int HtmlReplace(char* buff,char *beforeStr,char *afterStr){
1390   int ret;
1391   if(debug>2) err_msg("DEBUG:=>htmlReplace(%s,%s,%s)",buff,beforeStr,afterStr);
1392   ret = htmlReplace(buff, beforeStr, afterStr);
1393   if(debug>2) err_msg("DEBUG:(%d)<=htmlReplace( )",ret);
1394   return ret;
1395 }
1396
1397 char* StrSplit(char* str,const char* delimStr){
1398   char* ret;
1399   if(debug>2) err_msg("DEBUG:=>strSplit(%s,%s)",str,delimStr);
1400   ret = strSplit(str, delimStr);
1401   if(debug>2) err_msg("DEBUG:(%s)<=strSplit( )",ret);
1402   return ret;
1403 }
1404
1405 int GetHttpCookie(char *cookie, char* cookieName){
1406   int ret;
1407   if(debug>1) err_msg("DEBUG:=>getHttpCookie(%s)", cookieName);
1408   ret = getHttpCookie(cookie, cookieName);
1409   if(debug>1) err_msg("DEBUG:(%d)<=getHttpCookie(%s)",ret, cookie);
1410   return ret;
1411 }
1412
1413 int IsCorrectCookie(char* cookie, int userType){
1414   int ret;
1415   if(debug>1) err_msg("DEBUG:=> isCorrectCookie(%d)", userType);
1416   ret =  isCorrectCookie(cookie,userType);
1417   if(debug>1) err_msg("DEBUG:(%d)<= isCorrectCookie(%s)",ret,cookie);
1418   return ret;
1419 }
1420
1421 int GetUserIdFromEnv(char *userid){
1422   int ret;
1423   if(debug>1) err_msg("DEBUG:=>getUserIdFromEnv(%s)",userid);
1424   ret = getUserIdFromEnv(userid);
1425   if(debug>1) err_msg("DEBUG:(%d)<=getUserIdFromEnv( )",ret);
1426   return ret;
1427 }
1428
1429 int GetUserIdFromPostData(char* requestStr, char* userid, char* password){
1430   int ret;
1431   if(debug>1) err_msg("DEBUG:=>getUserIdFromPostData(..)");
1432   ret = getUserIdFromPostData(requestStr,userid,password);
1433   if(debug>1) err_msg("DEBUG:(%d)<=getUserIdFromPostData(,%s,password)",ret,userid);
1434   return ret;
1435 }
1436
1437 int PutAuthRequestPageToClient(char *language, char* cgiName, char* docName, char* redirectedUrl){
1438   int ret;
1439   if(debug>1) err_msg("DEBUG:=>putAuthRequestPageToClient(%s,%s,%s)",language,cgiName, docName, redirectedUrl);
1440   ret=putAuthRequestPageToClient(language,cgiName, docName, redirectedUrl);
1441   if(debug>1) err_msg("DEBUG:(%d)<=putAuthRequestPageToClient( )",ret);
1442   return ret;
1443 }
1444
1445 int AnalyzeUpdateRequestAndExecute(char *content, char* userId, char* extraId){
1446   int ret;
1447
1448   if(debug>1) err_msg("DEBUG:=>analyzeUpdateRequestAndExecute(%s,%s,%s)", content,userId,extraId);
1449   ret=analyzeUpdateRequestAndExecute(content,userId,extraId);
1450   if(debug>1) err_msg("DEBUG:%d<=analyzeUpdateRequestAndExecute( )",ret);
1451   return ret;
1452 }
1453
1454 int PutUpdatePageToClient(char *language, char* userId, char* extraId, int ownUpdate, char* redirectedUrl){
1455   int ret;
1456   if(debug>1) err_msg("DEBUG:=>putUpdatePageToClient(%s,%s,%s,%d,%s)", 
1457                       language,userId,extraId,ownUpdate,redirectedUrl);
1458   ret=putUpdatePageToClient(language,userId,extraId,ownUpdate,redirectedUrl);
1459   if(debug>1) err_msg("DEBUG:(%d)<=putUpdatePageToClient( )",ret);
1460   return ret;
1461 }
1462
1463 void PutMacListToClient(char *userId, char* extraId){
1464   if(debug>1) err_msg("DEBUG:=>putMacListToClient(%s,%s)",userId,extraId);
1465   putMacListToClient(userId,extraId);
1466   if(debug>1) err_msg("DEBUG:<=putMacListToClient( )");
1467 }
1468
1469 void PutUsageLogToClient(char *userId, char* extraId, char* language){
1470   if(debug>1) err_msg("DEBUG:=>putUsageLogToClient(%s,%s,%s)",userId,extraId,language);
1471   putUsageLogToClient(userId,extraId,language);
1472   if(debug>1) err_msg("DEBUG:<=putUsageLogToClient( )");
1473 }
1474
1475 int IsAccessedFromAllowableAgent(void){
1476   int ret;
1477   if(debug>1) err_msg("DEBUG:=>isAccessedFromAllowableAgent( )");
1478   ret=isAccessedFromAllowableAgent();
1479   if(debug>1) err_msg("DEBUG:(%d)<=isAccessedFromAllowableAgent( )",ret);
1480   return ret;
1481 }