1 /**************************************************
2 OpengateM - MAC address authentication system
3 opengatem forwarding CGI main
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.
9 ---sample index.html.var---
10 URI: /cgi-bin/opengate/opengatemfwd.cgi?en
12 Content-type: text/html
14 URI: /cgi-bin/opengate/opengatemfwd.cgi?ja
16 Content-type: text/html
18 URI: /cgi-bin/opengate/opengatemfwd.cgi?en
19 Content-type: text/html
20 ---------------------------
22 Copyright (C) 2012 Opengate Project Team
23 Written by Yoshiaki Watanabe
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.
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.
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.
39 Email: watanaby@is.saga-u.ac.jp
40 **************************************************/
42 #include "opengatemmng.h"
44 int main(int argc, char **argv)
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 */
56 /* drop root privilege */
59 /* if this is executed in shell with '-v' option, show makedir */
61 if(strcmp(argv[1],"-v")==0){
62 printf("makedir: %s\n", MAKEDIR);
64 printf("This is cgi program\n");
65 printf("To show version, run this on console with '-v' option\n");
70 /* prepare config file */
71 if(OpenConfFile()==-1)return 0;
74 errToSyslog(atoi(GetConfValue("Syslog/Enable")));
75 openlog(GetConfValue("FwdCgi"), LOG_PID, atoi(GetConfValue("Syslog/Facility")));
77 /* initialize config */
80 if(debug>1) err_msg("DEBUG: started");
82 /* favicon.ico request is ignored */
83 if(!isNull(getenv("REQUEST_URI"))){
84 if(strcmp(getenv("REQUEST_URI"), "/favicon.ico")==0){
85 PutMessageToClient("");
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"));
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");
108 /* get language from environment variable set with httpd(see index.html.var) */
109 if(isNull(getenv("QUERY_STRING"))){
112 strlcpy(lang, getenv("QUERY_STRING"), ADDRMAXLN);
115 /* if not get, use default language at the top of language list */
117 sscanf(GetConfValue("HtmlLangs"),"%s",lang);
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);
126 /* construct redirected(requested) URL */
128 if(!isNull(getenv("SERVER_PORT"))
129 && strcmp(getenv("SERVER_PORT"),GetServicePortStr("https"))==0) {
136 if(!isNull(getenv("HTTP_HOST"))) httpHost=getenv("HTTP_HOST");
140 if(!isNull(getenv("REQUEST_URI"))) requestUri=getenv("REQUEST_URI");
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';
148 /* construct html template file path */
149 snprintf(htmlFile, BUFFMAXLN, "%s%s/%s/%s",
150 GetConfValue("DocumentRoot"),
151 GetConfValue("OpengateDir"),
153 GetConfValue("FwdDoc"));
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");
164 /* send out header */
165 printf("Content-Type: text/html\r\n\r\n\r\n");
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);
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);
185 if(debug>1) err_msg("DEBUG: terminated");