OSDN Git Service

modified comments
[opengatem/opengatem.git] / mngsrc / cgi.c
index 553bf4b..1bc3034 100644 (file)
@@ -26,7 +26,7 @@ Programmed by Yoshiaki WATANABE
 
 #include       "opengatemmng.h"
 
-/* convert one-char-hex "a" to one-number 0Xa */ 
+/* convert one-char-hex "a" to one-number 0Xa */
 #define hex2num(x)  ((x)>='A' ? ((x) & 0XDF) - 'A' +10 : ((x) - '0'))
 
 void split(char content[], char *name[], char *value[], char *next[]);
@@ -35,10 +35,9 @@ void decode(char *string);
 char macAddress[ADDRMAXLN]="";
 
 /********************************************/
-/* get Post data from the client  */
+/* get Post data sent from the client       */
 /********************************************/
-int getPostData(char *content, int contentMaxLength)
-{
+int getPostData(char *content, int contentMaxLength){
   int contentLen;
 
   /* get content sent from web input */
@@ -55,9 +54,9 @@ int getPostData(char *content, int contentMaxLength)
   return TRUE;
 }
 
-/********************************************/
+/****************************************************/
 /* get language form query string (in url lang=ja)  */
-/********************************************/
+/****************************************************/
 int getLangFromQueryString(char* language){
 
   int found=FALSE;
@@ -89,9 +88,9 @@ int getLangFromQueryString(char* language){
   return found;
 }
 
-/********************************************/
-/* get redirected url form query string     */
-/********************************************/
+/*********************************************************************/
+/* get redirected url form query string (in url redirectedurl=xxxxx) */
+/*********************************************************************/
 int getRedirectedUrlFromQueryString(char* redirectedUrl){
 
   int found=FALSE;
@@ -123,9 +122,9 @@ int getRedirectedUrlFromQueryString(char* redirectedUrl){
   return found;
 }
 
-/********************************************/
-/* get mac address form query string (in url lang=ja)  */
-/********************************************/
+/*************************************************************/
+/* get mac address form query string (in url macaddr=xxxxx)  */
+/*************************************************************/
 int getMacAddrFromQueryString(char* macAddress){
 
   int found=FALSE;
@@ -159,25 +158,25 @@ int getMacAddrFromQueryString(char* macAddress){
 
 }
 
-/********************************************/
-/* get userid from environment variable     */
-/*  long userid: "userid@extraid" is set    */
-/********************************************/
+/**************************************************/
+/* get userid from environment variable           */
+/*  long userid: "userid@extraid" is set in env   */
+/**************************************************/
 int getUserIdFromEnv(char *userid){
 
   int ret=FALSE;
   char* pEnv=NULL;
 
   /* if shibboleth or httpbasic, get uid from environment var */
-  /* shibboleth */
+  /*** case of shibboleth */
   if(strcmp(GetConfValue("AuthServer/Protocol"), "shibboleth")==0){
 
-    /* get data from env-variables for uid and org attribute */
+    /* get data from env-variables for uid and org attribute, if env-var exists */
     pEnv=getenvEx(GetConfValue("AuthServer/UidAttribute"),TRUE,TRUE); 
     if(!isNull(pEnv)){
       strlcpy(userid, pEnv, USERMAXLN);
 
-      /* if orp string can be get from env var, concatenate it as uid@org */
+      /* if orp string can be get from env-var, concatenate it as uid@org */
       pEnv=getenvEx(GetConfValue("AuthServer/OrgAttribute"),TRUE,TRUE);
       if(!isNull(pEnv)){
        strlcat(userid, GetConfValue("UserIdSeparator"), USERMAXLN);
@@ -186,7 +185,7 @@ int getUserIdFromEnv(char *userid){
       ret=TRUE;
     }
 
-    /* get from env-variable for EPPN(edu person principal name) attribute */
+    /* else, get name from env-variable for EPPN(edu person principal name) attribute */
     else{
       pEnv=getenvEx(GetConfValue("AuthServer/EppnAttribute"),TRUE,FALSE);
       if(!isNull(pEnv)){
@@ -195,6 +194,7 @@ int getUserIdFromEnv(char *userid){
       }
     }
 
+    /* if not get, error exit */
     if(ret==FALSE){
       err_msg("ERR at %s#%d: Cannot get user info from shibboleth",__FILE__,__LINE__);
       PutMessageToClient("Cannot get user info from shibboleth<br>Check shibboleth setting in .htaccess, conf and others");
@@ -202,7 +202,7 @@ int getUserIdFromEnv(char *userid){
     }    
   }
 
-  /* httpbasic */
+  /*** case of httpbasic */
   else if(strcmp(GetConfValue("AuthServer/Protocol"), "httpbasic")==0){
     if(!isNull(getenv("REMOTE_USER"))){
       strlcpy(userid,getenv("REMOTE_USER"),USERMAXLN);
@@ -210,16 +210,16 @@ int getUserIdFromEnv(char *userid){
     }else{
       err_msg("ERR at %s#%d: Cannot get user info from httpbasic",__FILE__,__LINE__);
       ret=FALSE;
-      PutMessageToClient("Cannot get user info from http basic<br>Check http basic setting in .htaccess and other");
+      PutMessageToClient("Cannot get user info from http basic<br>Check http-basic setting in .htaccess and other");
       exit(0);
     }
   }
   return ret;
 }
 
-/*******************************
-get userid and password from post string
-*******************************/
+/********************************************/
+/* get userid and password from post string */
+/********************************************/
 int getUserIdFromPostData(char* requestStr, char* userid, char* password){
 
   char *name[1];
@@ -235,7 +235,7 @@ int getUserIdFromPostData(char* requestStr, char* userid, char* password){
   /* copy it to work area */
   strlcpy(content, requestStr, BUFFMAXLN);
 
-  /* split request item and execute the request */
+  /* split request item and pick up userid and password */
   ptr=content;
   while(ptr!=NULL){
 
@@ -263,11 +263,13 @@ int getUserIdFromPostData(char* requestStr, char* userid, char* password){
   return ret;
 }
 
-/********************************************/
-/* analyze request for checking    */
-/********************************************/
-int analyzeCheckRequest(char *content, int* status, char* macAddress)
-{
+/*********************************************/
+/* analyze request from mac check page       */
+/* content: (input) request string           */
+/* status: (output) OPEN(1)/CLOSE(0)/NONE(-1)*/
+/* macAddress: (output) mac addr             */
+/*********************************************/
+int analyzeCheckRequest(char *content, int* status, char* macAddress){
   char *name[1];
   char *value[1];
   char *next[1];
@@ -277,7 +279,7 @@ int analyzeCheckRequest(char *content, int* status, char* macAddress)
   *status=NONE;
   *macAddress='\0';
 
-  /* split request item and execute the request */
+  /* split request item and pick up status(open/close) and macAddress */
   ptr=content;
   while(!isNull(ptr)){
 
@@ -304,21 +306,24 @@ int analyzeCheckRequest(char *content, int* status, char* macAddress)
 }
 
 /********************************************/
-/* analyze request and execute request for registering   */
+/* analyze request from mac register page   */
 /********************************************/
-int analyzeRegisterRequest(char *content, char* macAddress, char* deviceName, char* mailAddress)
-{
+int analyzeRegisterRequest(char *content, char* macAddress, char* deviceName, char* mailAddress){
   char *name[1];
   char *value[1];
   char *next[1];
   char *ptr;
 
+  /* if null request, return false */
+  if(isNull(content)) return FALSE;
+  
   /* set default */
   *macAddress='\0';
   *deviceName='\0';
   *mailAddress='\0';
 
-  /* split request item and execute the request */
+  /*** split item in request and pick up macAddress,deviceName,mailAddress */
+  /* scan the content string of request */
   ptr=content;
   while(!isNull(ptr)){
 
@@ -344,7 +349,7 @@ int analyzeRegisterRequest(char *content, char* macAddress, char* deviceName, ch
     ptr=next[0];
   }
 
-  /* if illegal device name, return */
+  /* if null or illegal device name, return false */
   if(*deviceName=='\0'){
     SetMessage(EmptyDeviceName); 
     return FALSE;
@@ -354,7 +359,8 @@ int analyzeRegisterRequest(char *content, char* macAddress, char* deviceName, ch
     return FALSE;
   }
 
-  /* if illegal mail address, return */
+  /* if illegal mail address, return false */
+  /* (null mail address is permitted) */
   if(*mailAddress!='\0'){
     if(!IsSafeString(mailAddress, BUFFMAXLN)){
       SetMessage(IllegalCharInMailAddr);
@@ -362,10 +368,10 @@ int analyzeRegisterRequest(char *content, char* macAddress, char* deviceName, ch
     }
   }
 
-  /* if illegal mac address, return */
+  /* if illegal mac address, return false */
   if(!ConvertMacAddr(macAddress)) return FALSE;
 
-  /* if already registered in db, return */
+  /* if already registered in db, return false */
   if(IsMacAddrFoundInMngDb(macAddress)){
     SetMessage(ExistentMacAddr);
     return FALSE;
@@ -374,13 +380,14 @@ int analyzeRegisterRequest(char *content, char* macAddress, char* deviceName, ch
   return TRUE;
 }
   
-/******************************
- convert mac address to regular format
-  [01:23:45:67:89:0a]
-******************************/
+/*****************************************/
+/* convert mac address to regular format */
+/*  [01:23:45:67:89:0a]                  */
+/* (colon separate, lower case letters)  */
+/*****************************************/
 int convertMacAddr(char* macAddr){
 
-  int m[6]; /* mac address sequence */
+  int m[6]; /* mac address sequence 6-bytes */
   int i;
 
   /* normal form is hex:hex:hex:hex:hex:hex */
@@ -407,9 +414,9 @@ int convertMacAddr(char* macAddr){
   return TRUE;
 }
 
-/******************************
-check safe characters string 
-******************************/
+/********************************/
+/* check safe characters string */
+/********************************/
 int isSafeString(char* str, int length){
   char allowableChar[]=" !#$&*+,-./:=?[]^_{|}@";
   int i;
@@ -419,43 +426,45 @@ int isSafeString(char* str, int length){
   for(i=0; i<length; i++){
     ch=str[i];
     
-    /* if reached to the end of string, return true */
+    /* if reached to the end of string, break loop */
     if(ch=='\0') break;
 
-    /* if alpha or numeric is found, goto next char */
+    /* if char is alpha or numeric, goto next char */
     if(isalnum(ch)) continue;
 
-    /* if control or non ascii is found, return false */
+    /* if char is control or non-ascii, return false */
     if( (unsigned)ch < 0x20|| 0x7f < (unsigned)ch) return FALSE;
     
-    /* if one of above list is found, goto next char */
+    /* if char is not allowable, return false */
     if(strchr(allowableChar, ch)==NULL) return FALSE;
   }
 
+  /* all char is safe */
   return TRUE;
 }
 
 /*********************************************/
-/* put auth request page to client            */
+/* put auth request page to client           */
 /*********************************************/
-int putAuthRequestPageToClient(char *language, char* cgiName, char* docName, char* redirectedUrl)
-{
+int putAuthRequestPageToClient(char *language, char* cgiName, char* docName, char* redirectedUrl){
   char authdoc[BUFFMAXLN];
   FILE *fp;
   char buff[BUFFMAXLN];
 
-  /* make read in path to the retry document */
+  /* make directory path to the auth page document template file */
   snprintf(authdoc, BUFFMAXLN, "%s%s/%s/%s",GetConfValue("DocumentRoot"),
          GetConfValue("OpengateDir"),language,docName);
 
   /* replace keyword and send out the file */
   printf("Content-type: text/html\r\n\r\n");
 
+  /* open the template file */
   if((fp=fopen(authdoc, "r"))==NULL){
     err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__, authdoc);
     return FALSE;
   }
 
+  /* read line from template file, replace variables, and write to client */
   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
     HtmlReplace(buff, "%%CGINAME%%", cgiName);
     HtmlReplace(buff, "%%REDIRECTEDURL%%", redirectedUrl);
@@ -469,26 +478,28 @@ int putAuthRequestPageToClient(char *language, char* cgiName, char* docName, cha
   return TRUE;
 }
 
-/*********************************************/
-/* deny message to the client            */
-/*********************************************/
+/*****************************************/
+/* deny message page to the client       */
+/*****************************************/
 int putDenyToClient(char *language){
   char denydoc[BUFFMAXLN];
   FILE *fp;
   char buff[BUFFMAXLN];
 
-  /* make read in path to the retry document */
+  /* make read in path to the deny document */
   snprintf(denydoc, BUFFMAXLN, "%s%s/%s/%s",GetConfValue("DocumentRoot"),
          GetConfValue("OpengateDir"),language,GetConfValue("DenyDoc"));
 
   /* replace keyword and send out the file */
   printf("Content-type: text/html\r\n\r\n");
 
+  /* open the template file */
   if((fp=fopen(denydoc, "r"))==NULL){
     err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__, denydoc);
     return FALSE;
   }
 
+  /* read line from template file, replace variables, and write to client */
   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
     if(strstr(buff, "%%ERRORLIST%%")!=NULL){
       InsertMessageToPage(language);
@@ -500,11 +511,11 @@ int putDenyToClient(char *language){
   return TRUE;
 }
 
-/*********************************************/
-/* put response to client for check request   */
-/*********************************************/
-int putCheckPageToClient(char *language, char* userId, char* extraId)
-{
+/*****************************************/
+/* put mac address check page to client  */
+/* the page includes detected mac list   */ 
+/*****************************************/
+int putCheckPageToClient(char *language, char* userId, char* extraId){
   char responsedoc[BUFFMAXLN];
   FILE *fp;
   char buff[BUFFMAXLN];
@@ -530,13 +541,13 @@ int putCheckPageToClient(char *language, char* userId, char* extraId)
   /* end of http header */
   printf("\r\n");
 
-  /* replace keyword and send out the file */
+  /* open the template file */
   if((fp=fopen(responsedoc, "r"))==NULL){
     err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__, responsedoc);
     return FALSE;
   }
 
-
+  /* read line from template file, replace variables, and write to client */
   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
     HtmlReplace(buff, "%%CHECKCGI%%", chkCgi);
     HtmlReplace(buff, "%%REGISTERCGI%%", regCgi);
@@ -549,7 +560,6 @@ int putCheckPageToClient(char *language, char* userId, char* extraId)
     else if(strstr(buff, "%%ERRORLIST%%")!=NULL){
       InsertMessageToPage(language);
     }
-
     else{
       printf("%s",buff);    
     }
@@ -557,19 +567,21 @@ int putCheckPageToClient(char *language, char* userId, char* extraId)
   fclose(fp);
   return TRUE;
 }
-/*********************************************/
-/* put response to client for register request */
+/***********************************************************/
+/* put mac register page to client                         */
 /* ownReg:1=the page is controlled by owner, 0=under admin */
-/*********************************************/
-int putRegisterPageToClient(char *language, char* macAddress, char* deviceName, char* mailAddress, char* userId, char* extraId, int ownReg, char* redirectedUrl)
-{
+/***********************************************************/
+int putRegisterPageToClient(char *language, char* macAddress, char* deviceName, char* mailAddress, char* userId, char* extraId, int ownReg, char* redirectedUrl){
   char responsedoc[BUFFMAXLN];
   FILE *fp;
   char buff[BUFFMAXLN];
   char cookie[SIDMAXLN];
   char* regCgi="";
   char* regDoc="";
+  char* maxDevicesStr;
 
+  /* get Maximum count of devices for an user from conf file */
+  maxDevicesStr=GetConfValue("MaxDevices");
 
   /* setup cgi and doc */
   if(ownReg){
@@ -584,7 +596,7 @@ int putRegisterPageToClient(char *language, char* macAddress, char* deviceName,
     return FALSE;
   }
 
-  /* make read in path to the document */
+  /* make path to the document template file */
   snprintf(responsedoc, BUFFMAXLN, "%s%s/%s/%s",GetConfValue("DocumentRoot"),
          GetConfValue("OpengateDir"),language,regDoc);
 
@@ -602,11 +614,13 @@ int putRegisterPageToClient(char *language, char* macAddress, char* deviceName,
   /* end of http header */
   printf("\r\n");
 
+  /* open the template file */
   if((fp=fopen(responsedoc, "r"))==NULL){
     err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__, responsedoc);
     return FALSE;
   }
 
+  /* read line from template file, replace variables, and write to client */
   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
     HtmlReplace(buff, "%%REGISTERCGI%%", regCgi);
     HtmlReplace(buff, "%%MACADDR%%", macAddress);
@@ -614,6 +628,7 @@ int putRegisterPageToClient(char *language, char* macAddress, char* deviceName,
     HtmlReplace(buff, "%%MAILADDR%%", mailAddress);
     HtmlReplace(buff, "%%USERID%%", userId);
     HtmlReplace(buff, "%%REDIRECTEDURL%%", redirectedUrl);
+    HtmlReplace(buff, "%%MAXDEVICES%%", maxDevicesStr);
 
     if(strstr(buff, "%%MACREGLIST%%")!=NULL){
       PutMacRegListToClient(userId, extraId);
@@ -629,9 +644,12 @@ int putRegisterPageToClient(char *language, char* macAddress, char* deviceName,
   return TRUE;
 }
 
-/*********************************************/
-/* put mac list in mac check table to the client */
-/*********************************************/
+/********************************************************/
+/* put mac-check table in mac-check page                */
+/* mac-check table is prepared in work db with arp/ndp  */
+/* it is used for candidates of mac to register         */
+/* (at registration by admin)                           */
+/********************************************************/
 void putMacCheckListToClient(void){
 
   char macAddress[ADDRMAXLN];
@@ -658,14 +676,14 @@ void putMacCheckListToClient(void){
 
     /* print out table row */
     /* the row is colored, if it is a candidate for registering */
-    /* the terminals inUse/foundInDb may be not the candidate */
+    /* the terminals inUse/foundInDb is probably not the candidate */
     if(inUse || foundOnDb){
       printf("<tr align=middle>\n");
     }else{
       printf("<tr style='background-color: rgb(255,255,204);' align=middle>\n");
     }
 
-    /* check radio button in first row */ 
+    /* show radio button, checked on first row */ 
     if(firstRow){
       printf("<td><input type='radio' name='macaddr' value='%s' checked></td>\n", macAddress);
       firstRow=FALSE;
@@ -688,9 +706,9 @@ void putMacCheckListToClient(void){
   }
 }
 
-/*********************************************/
-/* put mac regsitered list the client            */
-/*********************************************/
+/************************************************/
+/* put list of mac address regisitered in db    */
+/************************************************/
 void putMacRegListToClient(char* userId, char* extraId){
 
   char deviceName[WORDMAXLN]="";
@@ -704,7 +722,6 @@ void putMacRegListToClient(char* userId, char* extraId){
   while(GetNextMacAddrFromMngDb(userId,extraId,macAddress,deviceName,
                                entryDate,limitDate,status,mailAddress)){
 
-
     printf("<tr align=middle>\n");
     printf("<td>%s</td>\n", macAddress);
     printf("<td>%s</td>\n",deviceName);
@@ -728,9 +745,10 @@ void putMessageToClient(char *message)
   printf("</BODY></HTML> \r\n\r\n");
 }
 
-/************************************************/
-/* send page for returning to the previous page */
-/************************************************/
+/*************************************************/
+/* send page that auto-returns to redirectedUrl  */
+/* after delay (ReturnWaitTime is in conf)       */
+/*************************************************/
 void returnToRedirectedPage(char* redirectedUrl, char* language){
   
   char returndoc[BUFFMAXLN];
@@ -744,16 +762,21 @@ void returnToRedirectedPage(char* redirectedUrl, char* language){
 
   /* send header */
   printf("Content-type: text/html\r\n\r\n");
-  
+
+  /* open the template file */
   if((fp=fopen(returndoc, "r"))==NULL){
     err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__, returndoc);
     printf("Error! \r\n\r\n");
     return;
   }
 
+  /* get return wait time from conf */
+  /* (it is inserted as Refresh time in the page) */
   if(!isNull(GetConfValue("ReturnWaitTime"))){
     waitTime=GetConfValue("ReturnWaitTime");
   }
+  
+  /* read line from template file, replace variables, and write to client */
   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
     HtmlReplace(buff, "%%WAITTIME%%", waitTime);
     HtmlReplace(buff, "%%REDIRECTEDURL%%", redirectedUrl);
@@ -762,12 +785,11 @@ void returnToRedirectedPage(char* redirectedUrl, char* language){
   fclose(fp);
 }
 
-/************************************/
-/* split value for indicated name   */
-/* in content  "name=value&..."     */
-/************************************/
-void split(char content[], char *name[], char *value[], char *next[])
-{
+/*************************************************************/
+/* split name and value in content such as "name=value&..."  */
+/*  input:content, output:name,value,next                    */
+/*************************************************************/
+void split(char content[], char *name[], char *value[], char *next[]){
   char *pstr;
   
   /* set default */
@@ -800,9 +822,11 @@ void split(char content[], char *name[], char *value[], char *next[])
   return;
 }
 
-/**********************************/
-/* decode text coding in web post */
-/**********************************/
+/*****************************************/
+/* decode text coding in web post        */
+/* eg. "+%21" => " !"(space,exclamation) */
+/* string: input and output              */
+/*****************************************/
 void decode(char *string)
 {
   char *pcheck, *pinsert;
@@ -824,11 +848,13 @@ void decode(char *string)
 }
 
 
-/*****************************************************/
-/* replace beforeStr to afterStr in string in buff   */
-/*****************************************************/
-int htmlReplace(char* buff,char *beforeStr,char *afterStr)
-{
+/********************************************/
+/* replace beforeStr to afterStr in buff    */
+/* buff: input and output                   */
+/* beforeStr,afterStr: input                */
+/* if afterStr is too long, tail is cut-off */
+/********************************************/
+int htmlReplace(char* buff,char *beforeStr,char *afterStr){
   char *pBuff , *pNext;
   char tempBuff[BUFFMAXLN];
   
@@ -849,10 +875,14 @@ int htmlReplace(char* buff,char *beforeStr,char *afterStr)
 }
 
 /*****************************************************/
-/* split a str at delimStr and return the point      */
+/* split a str at delimStr and return next pointer   */
+/*  str: input and output                            */
+/*  delimStr: input                                  */
+/* eg. IN: str="abcdefghijk" delimStr="def"          */
+/*    OUT: str="abc###ghijk"    '#' means null(\0)   */
+/*                    |________ pointer returned     */
 /*****************************************************/
-char* strSplit(char* str,const char* delimStr)
-{
+char* strSplit(char* str, const char* delimStr){
     char* delimPoint = strstr(str,delimStr);
     const size_t delimLen = strlen(delimStr);
 
@@ -868,11 +898,14 @@ char* strSplit(char* str,const char* delimStr)
 /**********************/
 /* get HTTP-Cookie    */
 /**********************/
-  /* cookie string examples 
-  "OpengateMmng=de..ac1&Userid=user1"
-  "OpengateMmng=de..ac1&Userid=user1; xxx=..; yyy=.."
-  "xxx=..; yyy=..; OpengateMmng=de..ac1&Userid=user1"
-  */
+/********************************************************/
+/*cookie is picked out from the cookie string such as   */
+/*  "OpengateMmng=de..ac1&Userid=user1"                 */
+/*  "OpengateMmng=de..ac1&Userid=user1; xxx=..; yyy=.." */
+/*  "xxx=..; yyy=..; OpengateMmng=de..ac1&Userid=user1" */
+/*  cookieName:(input) "OpengateMmng" defined in conf   */
+/*  cookie:(output) value of above cookie name          */
+/********************************************************/
 int getHttpCookie(char *cookie, char* cookieName){
   char content[BUFFMAXLN];
   char *name[1];
@@ -913,9 +946,9 @@ int getHttpCookie(char *cookie, char* cookieName){
   else return TRUE;
 }
 
-/*************************************
-compare received cookie to previously saved one
-*************************************/
+/*****************************************************/
+/* compare received cookie with previously saved one */
+/*****************************************************/
 int isCorrectCookie(char* cookie, int userType){
   char userId[USERMAXLN];
   char extraId[USERMAXLN];
@@ -934,35 +967,35 @@ int isCorrectCookie(char* cookie, int userType){
   return FALSE;
 }
 
-/********************************************/
-/* analyze update request and execute request    */
-/********************************************/
-int analyzeUpdateRequestAndExecute(char *requestStr, char* userId, char* extraId)
-{
+/************************************************/
+/* analyze update request and execute request   */
+/************************************************/
+int analyzeUpdateRequestAndExecute(char *requestStr, char* userId, char* extraId){
   char *name[1];
   char *value[1];
   char *next[1];
   char *ptr;
   char macAddr[ADDRMAXLN];
-  int modified=FALSE;    /* database modification is executed */
+  int modified=FALSE;    /* status of database modification */
   int ret;
   char content[BUFFMAXLN];
   char deviceName[WORDMAXLN]="";
   char mailAddress[BUFFMAXLN]="";
 
-  /* if null string, return */
+  /* if null string, return false */
   if(isNull(requestStr)) return FALSE;
 
   /* copy request string to work area */
   strlcpy(content, requestStr, BUFFMAXLN);
 
-  /* split request item and execute the request */
+  /*** split item and execute the request(from following input forms) */
   /* <input type=radio name=11:22:33:44:55:66 value=extend> */
   /* <input type=radio name=11:22:33:44:55:66 value=pause> */
   /* <input type=radio name=11:22:33:44:55:66 value=delete> */
   /* <input type=hidden name=11:22:33:44:55:66 value=mail:wata@foo.bar> */
   /* <input type=hidden name=11:22:33:44:55:66 value=name:iPhone01> */
 
+  /* point to the head of string and scan the string to the tail */
   ptr=content;
   while(ptr!=NULL){
 
@@ -971,14 +1004,14 @@ int analyzeUpdateRequestAndExecute(char *requestStr, char* userId, char* extraId
     
     /* if item=delete, execute delete */
     if(strcmp(value[0], "delete")==0){
-      strlcpy(macAddr, name[0], ADDRMAXLN);
-      decode(macAddr);
-      if(ConvertMacAddr(macAddr)){
-       ret=DelMacAddrFromMngDb(macAddr);
+      strlcpy(macAddr, name[0], ADDRMAXLN); /* copy mac to work area */
+      decode(macAddr);                      /* decode html encoding */
+      if(ConvertMacAddr(macAddr)){          /* convert to colon & lower-char str */
+       ret=DelMacAddrFromMngDb(macAddr);   /* remove mac from db */
        if(ret){
          modified=TRUE;
-         PutMacModifyLogToMngDb(userId, extraId, macAddr, 'D');
-         PutMacAddressToServers(macAddr);
+         PutMacModifyLogToMngDb(userId, extraId, macAddr, 'D'); /* save change log */
+         PutMacAddressToServers(macAddr);  /* send mac to opengatemd daemons */
        }
       }
     }
@@ -1068,12 +1101,11 @@ int analyzeUpdateRequestAndExecute(char *requestStr, char* userId, char* extraId
   return modified;
 }
 
-/*********************************************/
-/* put response to client            */
+/**********************************************************/
+/* put update page to client                              */
 /* ownUpdate:1=page is controlled by owner, 0=under admin */
-/*********************************************/
-int putUpdatePageToClient(char *language, char* userId, char* extraId, int ownUpdate, char* redirectedUrl)
-{
+/**********************************************************/
+int putUpdatePageToClient(char *language, char* userId, char* extraId, int ownUpdate, char* redirectedUrl){
   char responsedoc[BUFFMAXLN];
   FILE *fp;
   char buff[BUFFMAXLN];
@@ -1114,13 +1146,14 @@ int putUpdatePageToClient(char *language, char* userId, char* extraId, int ownUp
   /* end of http header */
   printf("\r\n");
 
+  /* open the template file */
   if((fp=fopen(responsedoc, "r"))==NULL){
     err_msg("ERR at %s#%d: cannot find file %s",__FILE__,__LINE__, responsedoc);
     return FALSE;
   }
 
+  /* read line from template file, replace variables, and write to client */
   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
-
     HtmlReplace(buff, "%%CGINAME%%", updateCgi);
     HtmlReplace(buff, "%%USERID%%", userId);
     HtmlReplace(buff, "%%REDIRECTEDURL%%", redirectedUrl);
@@ -1142,8 +1175,9 @@ int putUpdatePageToClient(char *language, char* userId, char* extraId, int ownUp
   return TRUE;
 }
 
+
 /*********************************************/
-/* put mac list the client            */
+/* put mac list for the user to the client   */
 /*********************************************/
 void putMacListToClient(char* userId, char* extraId){
 
@@ -1154,10 +1188,29 @@ void putMacListToClient(char* userId, char* extraId){
   char macAddr[ADDRMAXLN];
   char mailAddress[BUFFMAXLN];
 
-  /* make rows for deletion and extending */
-  /* get registered mac list form db and insert */
+  /*** make html-table for deletion and extending the registration   */
+  /* following code inserts table row of a mac address for the user  */
+  /* (table skeleton is prepared in in template file)                */
+  /* --example of the row--                                          */
+  /* <tr align=middle>                                               */
+  /* <td><input type=radio name=11:22:33:44:55:66 value=extend></td> */
+  /* <td><input type=radio name=11:22:33:44:55:66 value=pause></td>  */
+  /* <td><input type=radio name=11:22:33:44:55:66 value=delete></td> */
+  /* <td>11:22:33:44:55:66</td>                                      */
+  /* <td>onclick="editstring('11:22:33:44:55:66','name',this)">iPhone1</td>      */
+  /* <td>onclick="editstring('11:22:33:44:55:66','mail',this)">wata@foo.bar</td> */
+  /* <td>2000/01/03 01:23:45</td>                                    */
+  /* <td>2000/02/01 00:00:00</td>                                    */
+  /* <td>A</td>                                                      */
+  /* </tr>                                                           */
+  /* ----                                                            */
+  /* editstring is a javascript function to edit string in html-item */
+
+  /* get registered mac form db */
   while(GetNextMacAddrFromMngDb(userId,extraId,macAddr,deviceName,
                                entryDate,limitDate,status,mailAddress)){
+
+    /* put out a html table row to the client */
     printf("<tr align=middle>\n");
     if((status[0]=='A')||(status[0]=='P')){
       printf("<td><input type=radio name=%s value=extend checked></td>\n",
@@ -1197,14 +1250,16 @@ void putUsageLogToClient(char *userId, char* extraId, char* language){
   char* p;
   int count=0;
 
-  /* get usage log from db and insert as JSON format */
+  /*** get usage log and insert it to html doc as JSON format */
+  /* get one log form management db */
   while(GetNextUsageLogFromMngDb(userId,extraId,macAddr,deviceName,
                                 openTime,closeTime,gatewayName)){
 
-    /* split hostname only */
+    /* split hostname only (opengate01.example.com -> opengate01) */
     if((p=strchr(gatewayName,'.'))!=NULL) *p='\0';
 
-    /* put out table row */
+    /* put out the log as a JSON item */
+    /* table skeleton is prepared in template file */
     printf("\t %d:{\n", count);
     printf("\t\t macAddress: '%s',\n", macAddr);
     printf("\t\t device: '%s',\n",deviceName);
@@ -1213,11 +1268,11 @@ void putUsageLogToClient(char *userId, char* extraId, char* language){
     printf("\t\t gateway: '%s'\n",gatewayName);
     printf("\t },\n");
 
-    /* increment log item counter */
+    /* increment log counter */
     count++;
   }
 
-  /* put out informations for the list */
+  /* put out the JSON item for summary info of the list */
   GetTimeRangeToShowLog(beginTime, endTime, &dateCount);
   printf("\t info:{\n");
   printf("\t\t beginTime: '%s',\n", beginTime);
@@ -1227,9 +1282,12 @@ void putUsageLogToClient(char *userId, char* extraId, char* language){
   printf("\t }\n");
 }
 
-/**********************************************/
-/* check allowable http-agent defined in conf */
-/**********************************************/
+/********************************************************/
+/* the client is an allowable http-user-agent or not    */
+/* the allowable http-user-agent can be defined in conf */
+/* (if no definition, all agents are allowed)           */
+/* the agent is described with Regular Expression       */
+/********************************************************/
 int isAccessedFromAllowableAgent(void){
 
   char* pAgent=NULL;