OSDN Git Service

modified comments
[opengatem/opengatem.git] / mngsrc / opengatemchk.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3
4  module for mac address checking cgi main
5  use for the registration of the device without Web.
6  (Web device can be registered by owner oneself (opengatemown.c))
7  
8  CGI gets addresses from arp/ndp, and show the list on web page.
9  Administrator select a candidate address from the list.
10  The gate from/to the address is opened for a while and closed.
11  Try the network access from the bring-in device at open time.
12  If the access is passed, go to the registration(opengatemreg.c).
13  Else select other address for candidate.
14
15 Copyright (C) 2011 Opengate Project Team
16 Written by Yoshiaki Watanabe
17
18 This program is free software; you can redistribute it and/or
19 modify it under the terms of the GNU General Public License
20 as published by the Free Software Foundation; either version 2
21 of the License, or (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
31
32 Email: watanaby@is.saga-u.ac.jp
33 **************************************************/
34
35 #include        "opengatemmng.h"
36
37 /***************************************************/
38 /*  main routine called as cgi from Web server     */
39 /***************************************************/
40 int  main(int argc, char **argv)
41 {
42   char language[WORDMAXLN]="";  /* browser prefered language(e.g.:ja,en) */
43   char requestStr[BUFFMAXLN]=""; /* http request string */
44   int status=NONE;   /* request offlag for open/close: OPEN/CLOSE/NONE */
45   char macAddress[ADDRMAXLN]="";  /* client MAC address */
46   char userId[USERMAXLN]="";      /* user id */
47   char extraId[USERMAXLN]="";     /* extra id used as user@extra */
48   char* progName="";              /* the name of this program in argv[0] */
49   char mailDefault[BUFFMAXLN]=""; /* default mail address to get warning */
50
51   /* drop root privilege to prevent dangerous actions */
52   seteuid(getuid());
53   
54   /* if this is executed in shell with '-v' option, show makedir */
55   /* this is prepared to show the information about version */
56   if(argc>1){
57     if(strcmp(argv[1],"-v")==0){
58       printf("makedir: %s\n", MAKEDIR);
59     }else{
60       printf("This is cgi program\n");
61       printf("To show version, run this on console with '-v' option\n");
62     }
63     exit(0);
64   }
65
66   /* save program load path */
67   saveLoadPath(argv[0]);
68   progName = getProgramName();
69
70   /* prepare configuration file */
71   if(OpenConfFile()==-1){
72     PutMessageToClient("Check config file by running this cgi on console");
73     return 0;
74   }
75  
76   /* start log */
77   errToSyslog(atoi(GetConfValue("Syslog/Enable")));
78   openlog(progName, LOG_PID, atoi(GetConfValue("Syslog/Facility")));
79
80   /* initialize config */
81   InitConf();
82   if(!InitWorkDb()) return 0;
83   if(!InitMngDb()) return 0;
84
85   /* get request language indicated in query string */
86   GetLangFromQueryString(language);
87
88   /* get post data */
89   GetPostData(requestStr, BUFFMAXLN);
90   
91   /* authentication. get userid. if not get, exit */
92   if(!GetUserId(requestStr, userId, extraId, language, ADMINUSER, 
93                 GetConfValue("CheckCgi"), mailDefault, "")){
94     CloseMngDb();
95     return 0;
96   }
97
98   /* analyze request from client */
99   if(!isNull(requestStr)){
100     AnalyzeCheckRequest(requestStr, &status, macAddress);
101   }
102
103   /* from second request (AJAX from html/macchk.js, status=OPEN/CLOSE), */
104   /* send back short message, and exec child process to open/close network */
105   if(status==OPEN){
106     PutMessageToClient("open");
107     StartChildProc(macAddress);
108   }    
109   else if(status==CLOSE){
110     PutMessageToClient("close");
111     StopChildProc(macAddress);
112   }
113
114   /* at first request (null status), mac list is send back */
115   else{
116
117     /* make work table for checking client mac address */
118     CreateMacCheckTableInWorkDb();
119   
120     /* put page to client */
121     PutCheckPageToClient(language,userId,extraId);
122   }
123
124   return 0;
125 }