OSDN Git Service

modified comments
[opengatem/opengatem.git] / mngsrc / opengatemfwd.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3  opengatem forwarding CGI main
4
5  Program for responding to the first forwarding HTTP access.
6  'index.html.var' must be set to start this program
7  IPFW forwards HTTP packets to 'index.html.var' by the setting of httpd.conf.
8
9 ---sample index.html.var---
10 URI: /cgi-bin/opengate/opengatemfwd.cgi?en
11 Content-language: en
12 Content-type: text/html
13
14 URI: /cgi-bin/opengate/opengatemfwd.cgi?ja
15 Content-language: ja
16 Content-type: text/html
17
18 URI: /cgi-bin/opengate/opengatemfwd.cgi?en
19 Content-type: text/html
20 ---------------------------
21
22 Copyright (C) 2012 Opengate Project Team
23 Written by Yoshiaki Watanabe
24
25 This program is free software; you can redistribute it and/or
26 modify it under the terms of the GNU General Public License
27 as published by the Free Software Foundation; either version 2
28 of the License, or (at your option) any later version.
29
30 This program is distributed in the hope that it will be useful,
31 but WITHOUT ANY WARRANTY; without even the implied warranty of
32 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33 GNU General Public License for more details.
34
35 You should have received a copy of the GNU General Public License
36 along with this program; if not, write to the Free Software
37 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
38
39 Email: watanaby@is.saga-u.ac.jp
40 **************************************************/
41
42 #include "opengatemmng.h"
43
44 int  main(int argc, char **argv)
45 {
46   char htmlFile[BUFFMAXLN]="";   /* html file */
47   char lang[ADDRMAXLN]=""; /* client language */
48   char jumpCgiUrl[BUFFMAXLN];  /* url of forwarding destination */
49   char redirectedUrl[BUFFMAXLN]; /* url of redirected(requested) page */
50   char *protocol;
51   char *httpHost;
52   char *requestUri;
53   FILE *fp;
54   char buff[BUFFMAXLN];
55
56   /* drop root privilege */
57   seteuid(getuid());
58
59   /* if this is executed in shell with '-v' option, show makedir */  
60   if(argc>1){
61     if(strcmp(argv[1],"-v")==0){
62       printf("makedir: %s\n", MAKEDIR);
63     }else{
64       printf("This is cgi program\n");
65       printf("To show version, run this on console with '-v' option\n");
66     }
67     exit(0);
68   }
69
70   /* prepare config file */
71   if(OpenConfFile()==-1)return 0;
72  
73   /* start log */
74   errToSyslog(atoi(GetConfValue("Syslog/Enable")));
75   openlog(GetConfValue("FwdCgi"), LOG_PID, atoi(GetConfValue("Syslog/Facility")));
76
77   /* initialize config */
78   InitConf();
79
80   if(debug>1) err_msg("DEBUG: started");
81
82   /* favicon.ico request is ignored */
83   if(!isNull(getenv("REQUEST_URI"))){
84     if(strcmp(getenv("REQUEST_URI"), "/favicon.ico")==0){
85       PutMessageToClient("");
86       return 0;
87     }
88   }
89
90
91   /* create URL string for jump cgi */
92   snprintf(jumpCgiUrl, BUFFMAXLN, "%s%s%s/%s",
93            GetConfValue("OpengateServerName"),
94            GetConfValue("CgiDir"),
95            GetConfValue("OpengateDir"),
96            GetConfValue("JumpCgi"));
97
98   /* if null element is included, put error */
99   if(isNull(GetConfValue("OpengateServerName")) ||
100      isNull(GetConfValue("CgiDir")) ||
101      isNull(GetConfValue("OpengateDir")) ||
102      isNull(GetConfValue("JumpCgi")) ){
103     err_msg("ERR at %s#%d: jump url cannot setup properly from conf[%s]",__FILE__,__LINE__, jumpCgiUrl);
104       PutMessageToClient("error: refer conf file");
105       return 0;
106   }
107
108   /* get language from environment variable set with httpd(see index.html.var) */
109   if(isNull(getenv("QUERY_STRING"))){
110     lang[0]='\0';
111   }else{
112     strlcpy(lang, getenv("QUERY_STRING"), ADDRMAXLN);
113   }
114      
115   /* if not get, use default language at the top of language list */
116   if(isNull(lang)){
117     sscanf(GetConfValue("HtmlLangs"),"%s",lang);
118   }
119      
120   /* if the language is not registered in language list, */
121   /* set the first language in the list */
122   else if(strstr(GetConfValue("HtmlLangs"), lang)==NULL){
123     sscanf(GetConfValue("HtmlLangs"),"%s",lang);
124   }
125
126   /* construct redirected(requested) URL */
127   /* protocol */
128   if(!isNull(getenv("SERVER_PORT"))
129      && strcmp(getenv("SERVER_PORT"),GetServicePortStr("https"))==0) {
130     protocol="https";
131   }else{
132     protocol="http";
133   }
134
135   /* http-host */
136   if(!isNull(getenv("HTTP_HOST"))) httpHost=getenv("HTTP_HOST");
137   else httpHost="";
138
139   /* request-uri */
140   if(!isNull(getenv("REQUEST_URI"))) requestUri=getenv("REQUEST_URI");
141   else requestUri="";
142
143   /* concat above items to make redirect URL */
144   if(!isNull(httpHost)){
145     snprintf(redirectedUrl,BUFFMAXLN,"%s://%s%s", protocol,httpHost,requestUri);
146   }else redirectedUrl[0]='\0';
147
148   /* construct html template file path */
149   snprintf(htmlFile, BUFFMAXLN, "%s%s/%s/%s", 
150           GetConfValue("DocumentRoot"), 
151           GetConfValue("OpengateDir"),
152           lang, 
153           GetConfValue("FwdDoc"));
154
155   /* if null element is included, put error */
156   if(isNull(GetConfValue("DocumentRoot")) ||
157      isNull(GetConfValue("OpengateDir")) ||
158      isNull(GetConfValue("FwdDoc")) ){
159     err_msg("ERR at %s#%d: fwd doc path cannot setup properly from conf[%s]",__FILE__,__LINE__, htmlFile);
160       PutMessageToClient("error: refer conf file");
161       return 0;
162   }
163
164   /* send out header */
165   printf("Content-Type: text/html\r\n\r\n\r\n");
166
167   /* open html template file */
168   if((fp=fopen(htmlFile, "r"))==NULL){
169     err_msg("ERR at %s#%d: cannot find file [%s]",__FILE__,__LINE__, htmlFile);
170     return FALSE;
171   }
172
173   /* replace keywords in template file and send out */
174   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
175     HtmlReplace(buff, "%%JUMPCGIURL%%", jumpCgiUrl);
176     HtmlReplace(buff, "%%REDIRECTEDURL%%", redirectedUrl);
177     if(strstr(buff, "%%ERRORLIST%%")!=NULL){
178       InsertMessageToPage(lang);
179     }else{
180       printf("%s",buff);    
181     }
182   }
183   fclose(fp);
184
185   if(debug>1) err_msg("DEBUG: terminated");
186
187   return 0;
188 }