1 /**************************************************
2 OpengateM - MAC address authentication system
3 module for mac address checking cgi main
5 Copyright (C) 2011 Opengate Project Team
6 Written by Yoshiaki Watanabe
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.
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.
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.
22 Email: watanaby@is.saga-u.ac.jp
23 **************************************************/
25 #include "opengatemmng.h"
27 /***************************************************/
28 /* main routine called as cgi from Web server */
29 /***************************************************/
30 int main(int argc, char **argv)
32 char language[WORDMAXLN]=""; /* browser prefered language(e.g.:ja,en) */
33 char requestStr[BUFFMAXLN]=""; /* http request string */
34 int status=NONE; /* request offlag for open/close: OPEN/CLOSE/NONE */
35 char macAddress[ADDRMAXLN]=""; /* client MAC address */
36 char userId[USERMAXLN]=""; /* user id */
37 char extraId[USERMAXLN]=""; /* extra id used as user@extra */
38 char* progName=""; /* the name of this program in argv[0] */
39 char mailDefault[BUFFMAXLN]=""; /* default mail address to get warning */
41 /* drop root privilege to prevent dangerous actions */
44 /* if this is executed in shell with '-v' option, show makedir */
45 /* this is prepared to show information about version */
47 if(strcmp(argv[1],"-v")==0){
48 printf("makedir: %s\n", MAKEDIR);
50 printf("This is cgi program\n");
51 printf("To show version, run this on console with '-v' option\n");
56 /* save program load path */
57 saveLoadPath(argv[0]);
58 progName = getProgramName();
60 /* prepare configuration file */
61 if(OpenConfFile()==-1){
62 PutMessageToClient("Check config file by running this cgi on console");
67 errToSyslog(atoi(GetConfValue("Syslog/Enable")));
68 openlog(progName, LOG_PID, atoi(GetConfValue("Syslog/Facility")));
70 /* initialize config */
72 if(!InitWorkDb()) return 0;
73 if(!InitMngDb()) return 0;
75 /* get request language indicated in query string */
76 GetLangFromQueryString(language);
79 GetPostData(requestStr, BUFFMAXLN);
81 /* get userid. if not get, exit */
82 if(!GetUserId(requestStr, userId, extraId, language, ADMINUSER,
83 GetConfValue("CheckCgi"), mailDefault, "")){
88 /* analyze request from client */
89 if(!isNull(requestStr)){
90 AnalyzeCheckRequest(requestStr, &status, macAddress);
93 /* send response to client and control watch process */
94 /* if status = OPEN/CLOSE, it is ajax request coded in html/macchk.js */
95 /* in this case, send back short message */
97 PutMessageToClient("open");
98 StartChildProc(macAddress);
100 else if(status==CLOSE){
101 PutMessageToClient("close");
102 StopChildProc(macAddress);
105 /* if not OPEN/CLOSE, send back new page */
108 /* make work table for checking client mac address */
109 CreateMacCheckTableInWorkDb();
111 /* put page to client */
112 PutCheckPageToClient(language,userId,extraId);