OSDN Git Service

503e0f6ec718b18c4d954ca5b8ffbf40424b0009
[opengate/opengate.git] / opengate / opengatesrv / opengatefwd.c
1 /**************************************************
2 opengate forwarding CGI main
3
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.
7
8 ---sample index.himl.var---
9 URI: /cgi-bin/opengate/opengatefwd.cgi?en
10 Content-language: en
11 Content-type: text/html
12
13 URI: /cgi-bin/opengate/opengatefwd.cgi?ja
14 Content-language: ja
15 Content-type: text/html
16
17 URI: /cgi-bin/opengate/opengatefwd.cgi?en
18 Content-type: text/html
19 ---------------------------
20
21 Copyright (C) 2006 Opengate Project Team
22 Written by Yoshiaki Watanabe
23
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.
28
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.
33
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.
37
38 Email: watanaby@is.saga-u.ac.jp
39 **************************************************/
40
41 #include "opengatesrv.h"
42
43 int  main(int argc, char **argv)
44 {
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 */
50
51   /* keyword pairs */
52   /*  the left key is replaced by the right value */
53   struct html_key keys[]=
54     {
55       {"%%ADDR4%%", encodeAddr},
56       {"%%AUTHCGIURL%%", authCgiUrl},
57       {"",""}  /* DON'T REMOVE THIS LINE */
58     };
59
60   /* prepare config file */
61   if(OpenConfFile()==-1)return 0;
62  
63   /* start log */
64   errToSyslog(atoi(GetConfValue("Syslog/Enable")));
65   openlog(GetConfValue("FwdCgi"), LOG_PID, atoi(GetConfValue("Syslog/Facility")));
66
67   /* initialize config */
68   InitConf();
69
70   if(debug>1) err_msg("DEBUG: started");
71
72   /* favicon.ico request is ignored */
73   if(!isNull(getenv("REQUEST_URI"))){
74     if(strcmp(getenv("REQUEST_URI"), "/favicon.ico")==0){
75       PutClientMsg("");
76       return 0;
77     }
78   }
79
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"));
86
87   /* get lang from httpd */
88   if(isNull(getenv("QUERY_STRING"))){
89     lang[0]='\0';
90   }else{
91     strncpy(lang, getenv("QUERY_STRING"), ADDRMAXLN);
92   }
93      
94   /* if not get, use default lang at the top of lang list */
95   if(isNull(lang)){
96     sscanf(GetConfValue("HtmlLangs"),"%s",lang);
97   }
98      
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);
102   }
103
104   /* get client address */
105   if(isNull(getenv("REMOTE_ADDR"))){
106     clientAddr[0]='\0';
107   }else{
108     strncpy(clientAddr,getenv("REMOTE_ADDR"),ADDRMAXLN);
109   }
110
111   /* encode the address(if IPv6 addr, fail) */
112   if(AddrEncode(encodeAddr, clientAddr)==1){
113     encodeAddr[0]='\0';
114   }
115
116   /* construct readin html file path */
117   snprintf(htmlFile, BUFFMAXLN, "%s%s/%s/%s", 
118           GetConfValue("DocumentRoot"), 
119           GetConfValue("OpengateDir"),
120           lang, 
121           GetConfValue("FwdDoc"));
122
123   /* send out header */
124   printf("Content-Type: text/html\r\n\r\n\r\n");
125
126   /* relpace keywords and send out */
127   HtmlTemplate(htmlFile, keys);
128
129   if(debug>1) err_msg("DEBUG: terminated");
130
131   return 0;
132 }