OSDN Git Service

Ver.0.8.1
[opengatem/opengatem.git] / mngsrc / opengatemup.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3  module for 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 userId[USERMAXLN]="";    /* user id */
34   char extraId[USERMAXLN]="";   /* extra id used as user@extra */
35   char requestStr[BUFFMAXLN]=""; /* http request string */
36   char* progName="";             /* the name of this program in argv[0] */
37   char mailDefault[BUFFMAXLN]=""; /* default mail address to get warning */
38
39   /* drop root privilege */
40   seteuid(getuid());
41
42   /* if this is executed in shell with '-v' option, show makedir */
43   /* this is prepared to show information about version */
44   if(argc>1){
45     if(strcmp(argv[1],"-v")==0){
46       printf("makedir: %s\n", MAKEDIR);
47     }else{
48       printf("This is cgi program\n");
49       printf("To show version, run this on console with '-v' option\n");
50     }
51     exit(0);
52   }
53
54   /* save program load path */
55   saveLoadPath(argv[0]);
56   progName = getProgramName();
57
58   /* prepare configuration file */
59   if(OpenConfFile()==-1){
60     PutMessageToClient("Check config file by running this cgi on console");
61     return 0;
62   }
63  
64   /* start log */
65   errToSyslog(atoi(GetConfValue("Syslog/Enable")));
66   openlog(progName, LOG_PID, atoi(GetConfValue("Syslog/Facility")));
67
68   /* initialize config */
69   InitConf();
70   if(!InitMngDb()) return 0;
71   if(!InitWorkDb()) return 0;
72
73   /* get language from query string */
74   GetLangFromQueryString(language);
75
76   /* get post data */
77   GetPostData(requestStr, BUFFMAXLN);
78
79   /* get userid. if not get, exit */
80   if(!GetUserId(requestStr, userId, extraId, language, NORMALUSER,
81                 GetConfValue("UpdateCgi"),mailDefault, "")){
82     CloseMngDb();
83     return 0;
84   }
85
86   /* if many modify requests per one day, ignore the request */
87   if(CountMacModifyPerDayInMngDb(userId,extraId) 
88      > atoi(GetConfValue("MaxMacModifyPerDay"))){
89     SetMessage(ModifyCountOver);
90     PutDenyToClient(language);
91     CloseMngDb();
92     return 0;
93   }
94
95   /* check null request from client */
96   if(!isNull(requestStr)){
97     
98     /* analize string and execute requests */
99     if(AnalyzeUpdateRequestAndExecute(requestStr, userId, extraId)){
100       SetMessage(UpdateSuccess);
101     }
102   }
103
104   /* prepare response and send to client */
105   PutUpdatePageToClient(language, userId, extraId, ADMIN, "");
106
107   /* finalize */
108   CloseMngDb();
109   return 0;
110 }