/**************************************************
OpengateM - MAC address authentication system 
 module for update cgi main

 This is used for updating the mac registration.
 When accessing this, the list of registration address is shown
 and update/delete/pause of the mac can be selected in the list. 
 the device name and mail address can also be edited.

Copyright (C) 2011 Opengate Project Team
Written by Yoshiaki Watanabe

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

Email: watanaby@is.saga-u.ac.jp
**************************************************/

#include	"opengatemmng.h"

/***************************************************/
/*  main routine called as cgi from Web server     */
/***************************************************/
int  main(int argc, char **argv)
{
  char language[WORDMAXLN]="";  /* browser prefered language(e.g.:ja,en) */
  char userId[USERMAXLN]="";    /* user id */
  char extraId[USERMAXLN]="";   /* extra id used as user@extra */
  char requestStr[BUFFMAXLN]=""; /* http request string */
  char* progName="";             /* the name of this program in argv[0] */
  char mailDefault[BUFFMAXLN]=""; /* default mail address to get warning */

  /* drop root privilege */
  seteuid(getuid());

  /* if this is executed in shell with '-v' option, show makedir */
  /* this is prepared to show information about version */
  if(argc>1){
    if(strcmp(argv[1],"-v")==0){
      printf("makedir: %s\n", MAKEDIR);
    }else{
      printf("This is cgi program\n");
      printf("To show version, run this on console with '-v' option\n");
    }
    exit(0);
  }

  /* save program load path */
  saveLoadPath(argv[0]);
  progName = getProgramName();

  /* prepare configuration file */
  if(OpenConfFile()==-1){
    PutMessageToClient("Check config file by running this cgi on console");
    return 0;
  }
 
  /* start log */
  errToSyslog(atoi(GetConfValue("Syslog/Enable")));
  openlog(progName, LOG_PID, atoi(GetConfValue("Syslog/Facility")));

  /* initialize config */
  InitConf();
  if(!InitMngDb()) return 0;
  if(!InitWorkDb()) return 0;

  /* get language from query string */
  GetLangFromQueryString(language);

  /* get post data */
  GetPostData(requestStr, BUFFMAXLN);

  /* get userid. if not get, exit */
  if(!GetUserId(requestStr, userId, extraId, language, NORMALUSER,
		GetConfValue("UpdateCgi"),mailDefault, "")){
    CloseMngDb();
    return 0;
  }

  /* if many modify requests from the user per one day, ignore the request */
  if(CountMacModifyPerDayInMngDb(userId, extraId, "") 
     > atoi(GetConfValue("MaxMacModifyPerDay"))){
    SetMessage(ModifyCountOver);
    PutDenyToClient(language);
    CloseMngDb();
    return 0;
  }

  /* if any request is sent from client */
  if(!isNull(requestStr)){
    
    /* analize string and execute requests */
    if(AnalyzeUpdateRequestAndExecute(requestStr, userId, extraId)){
      SetMessage(UpdateSuccess);
    }
  }

  /* prepare response and send to client */
  PutUpdatePageToClient(language, userId, extraId, ADMIN, "");

  /* finalize */
  CloseMngDb();
  return 0;
}
