OSDN Git Service

Ver.0.8.1
[opengatem/opengatem.git] / mngsrc / opengatemchk.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3  module for mac address checking cgi main
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
25 #include        "opengatemmng.h"
26
27 /***************************************************/
28 /*  main routine called as cgi from Web server     */
29 /***************************************************/
30 int  main(int argc, char **argv)
31 {
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 */
40
41   /* drop root privilege to prevent dangerous actions */
42   seteuid(getuid());
43   
44   /* if this is executed in shell with '-v' option, show makedir */
45   /* this is prepared to show information about version */
46   if(argc>1){
47     if(strcmp(argv[1],"-v")==0){
48       printf("makedir: %s\n", MAKEDIR);
49     }else{
50       printf("This is cgi program\n");
51       printf("To show version, run this on console with '-v' option\n");
52     }
53     exit(0);
54   }
55
56   /* save program load path */
57   saveLoadPath(argv[0]);
58   progName = getProgramName();
59
60   /* prepare configuration file */
61   if(OpenConfFile()==-1){
62     PutMessageToClient("Check config file by running this cgi on console");
63     return 0;
64   }
65  
66   /* start log */
67   errToSyslog(atoi(GetConfValue("Syslog/Enable")));
68   openlog(progName, LOG_PID, atoi(GetConfValue("Syslog/Facility")));
69
70   /* initialize config */
71   InitConf();
72   if(!InitWorkDb()) return 0;
73   if(!InitMngDb()) return 0;
74
75   /* get request language indicated in query string */
76   GetLangFromQueryString(language);
77
78   /* get post data */
79   GetPostData(requestStr, BUFFMAXLN);
80   
81   /* get userid. if not get, exit */
82   if(!GetUserId(requestStr, userId, extraId, language, ADMINUSER, 
83                 GetConfValue("CheckCgi"), mailDefault, "")){
84     CloseMngDb();
85     return 0;
86   }
87
88   /* analyze request from client */
89   if(!isNull(requestStr)){
90     AnalyzeCheckRequest(requestStr, &status, macAddress);
91   }
92
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 */
96   if(status==OPEN){
97     PutMessageToClient("open");
98     StartChildProc(macAddress);
99   }    
100   else if(status==CLOSE){
101     PutMessageToClient("close");
102     StopChildProc(macAddress);
103   }
104
105   /* if not OPEN/CLOSE, send back new page */
106   else{
107
108     /* make work table for checking client mac address */
109     CreateMacCheckTableInWorkDb();
110   
111     /* put page to client */
112     PutCheckPageToClient(language,userId,extraId);
113   }
114
115   return 0;
116 }