1 /**************************************************
2 opengate forwarding CGI main
4 Program responding to the first forwarding HTTP access.
5 DocumentRoot/index.html.var must be set to start this program
6 at forwarding HTTP access from IPFW.
8 ---sample index.himl.var---
9 URI: /cgi-bin/opengate/opengatefwd.cgi?en
11 Content-type: text/html
13 URI: /cgi-bin/opengate/opengatefwd.cgi?ja
15 Content-type: text/html
17 URI: /cgi-bin/opengate/opengatefwd.cgi?en
18 Content-type: text/html
19 ---------------------------
21 Copyright (C) 2006 Opengate Project Team
22 Written by Yoshiaki Watanabe
24 This program is free software; you can redistribute it and/or
25 modify it under the terms of the GNU General Public License
26 as published by the Free Software Foundation; either version 2
27 of the License, or (at your option) any later version.
29 This program is distributed in the hope that it will be useful,
30 but WITHOUT ANY WARRANTY; without even the implied warranty of
31 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 GNU General Public License for more details.
34 You should have received a copy of the GNU General Public License
35 along with this program; if not, write to the Free Software
36 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 Email: watanaby@is.saga-u.ac.jp
39 **************************************************/
41 #include "opengatesrv.h"
43 int main(int argc, char **argv)
45 char htmlFile[BUFFMAXLN]=""; /* html file */
46 char clientAddr[ADDRMAXLN]=""; /* client ip address */
47 char encodeAddr[ADDRMAXLN]=""; /* encoded ip address */
48 char lang[ADDRMAXLN]=""; /* client language */
49 char authCgiUrl[BUFFMAXLN]; /* url of opengateauth.cgi */
52 /* the left key is replaced by the right value */
53 struct html_key keys[]=
55 {"%%ADDR4%%", encodeAddr},
56 {"%%AUTHCGIURL%%", authCgiUrl},
57 {"",""} /* DON'T REMOVE THIS LINE */
60 /* prepare config file */
61 if(OpenConfFile()==-1)return 0;
64 errToSyslog(atoi(GetConfValue("Syslog/Enable")));
65 openlog(GetConfValue("FwdCgi"), LOG_PID, atoi(GetConfValue("Syslog/Facility")));
67 /* initialize config */
70 if(debug>1) err_msg("DEBUG: started");
72 /* favicon.ico request is ignored */
73 if(!isNull(getenv("REQUEST_URI"))){
74 if(strcmp(getenv("REQUEST_URI"), "/favicon.ico")==0){
80 /* create authcgi URL string */
81 snprintf(authCgiUrl, BUFFMAXLN, "%s%s%s/%s",
82 GetConfValue("OpengateServerName"),
83 GetConfValue("CgiDir"),
84 GetConfValue("OpengateDir"),
85 GetConfValue("AuthCgi"));
87 /* get lang from httpd */
88 if(isNull(getenv("QUERY_STRING"))){
91 strncpy(lang, getenv("QUERY_STRING"), ADDRMAXLN);
94 /* if not get, use default lang at the top of lang list */
96 sscanf(GetConfValue("HtmlLangs"),"%s",lang);
99 /* if the lang is not registered in lang list, set the default lang */
100 else if(strstr(GetConfValue("HtmlLangs"), lang)==NULL){
101 sscanf(GetConfValue("HtmlLangs"),"%s",lang);
104 /* get client address */
105 if(isNull(getenv("REMOTE_ADDR"))){
108 strncpy(clientAddr,getenv("REMOTE_ADDR"),ADDRMAXLN);
111 /* encode the address(if IPv6 addr, fail) */
112 if(AddrEncode(encodeAddr, clientAddr)==1){
116 /* construct readin html file path */
117 snprintf(htmlFile, BUFFMAXLN, "%s%s/%s/%s",
118 GetConfValue("DocumentRoot"),
119 GetConfValue("OpengateDir"),
121 GetConfValue("FwdDoc"));
123 /* send out header */
124 printf("Content-Type: text/html\r\n\r\n\r\n");
126 /* relpace keywords and send out */
127 HtmlTemplate(htmlFile, keys);
129 if(debug>1) err_msg("DEBUG: terminated");