1 /**************************************************
2 Opengate authentication CGI main
4 This program is accessed as [http(s)://xx.yy/opengateauth.cgi?0-0-0&ja]
5 It send out the authentication page after keywords replacement.
7 Copyright (C) 2005 Opengate Project Team
8 Written by Katsuhiko Eguchi, 2005
9 Modified by Yoshiaki Watanabe
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 Email: watanaby@is.saga-u.ac.jp
26 **************************************************/
28 #include "opengatesrv.h"
30 int main(int argc, char **argv)
33 char htmlFile[BUFFMAXLN]=""; /* html file */
34 char lang[WORDMAXLN]=""; /* language */
35 char clientAddr[ADDRMAXLN]=""; /* client ip address */
36 char paramString[BUFFMAXLN]=""; /* parameters in html access */
37 char* pLang; /* pointer to language string */
38 char* pAddr4; /* pointer to addr4 string */
39 char authCgiUrl[BUFFMAXLN]; /* url of opengateauth.cgi */
40 char mainCgiUrl[BUFFMAXLN]; /* url of opengatesrv.cgi */
41 char durationMax[WORDMAXLN]; /* usage duration maximum */
42 char opengateServerName[BUFFMAXLN]; /* opengate server domain name */
43 char opengateDir[BUFFMAXLN]; /* opengate doc directory in httpd doc */
46 /* the left key is replaced by the right value */
47 struct html_key keys[]=
49 {"%%OPENGATESERVERNAME%%", opengateServerName},
50 {"%%OPENGATEDIR%%", opengateDir},
51 {"%%AUTHCGIURL%%", authCgiUrl},
52 {"%%CGIURL%%", mainCgiUrl},
53 {"%%ADDR4%%", clientAddr},
54 {"%%DURATIONMAX%%",durationMax},
55 {"",""} /* DON'T REMOVE THIS LINE */
58 /* prepare config file */
59 if(OpenConfFile()==-1) return 0;
62 errToSyslog(atoi(GetConfValue("Syslog/Enable")));
63 openlog(GetConfValue("AuthCgi"), LOG_PID, atoi(GetConfValue("Syslog/Facility")));
65 /* initialize config */
68 if(debug>1) err_msg("DEBUG: started");
70 snprintf(opengateServerName,BUFFMAXLN,GetConfValue("OpengateServerName"));
71 snprintf(opengateDir,BUFFMAXLN,GetConfValue("OpengateDir"));
73 /* create URL string */
74 snprintf(authCgiUrl, BUFFMAXLN, "%s%s%s/%s",
76 GetConfValue("CgiDir"),
78 GetConfValue("AuthCgi"));
79 snprintf(mainCgiUrl, BUFFMAXLN, "%s%s%s/%s",
81 GetConfValue("CgiDir"),
83 GetConfValue("MainCgi"));
85 /* setup usage duration maximum and default (saved as seconds) */
86 snprintf(durationMax,WORDMAXLN,"%d",
87 atoi(GetConfValue("Duration/Max"))/60);
90 if(!isNull(getenv("QUERY_STRING"))){
91 /* get html access parameter string (xx.cgi?addr4&lang) */
92 strncpy(paramString, getenv("QUERY_STRING"), BUFFMAXLN);
95 /* split language and address in paramString[addr=0-0-0&lang=ja] */
97 if((pLang=strnstr(paramString, "&", BUFFMAXLN))!=NULL){
103 if(strnstr(pAddr4, "addr=", BUFFMAXLN)==pAddr4) pAddr4+=5;
104 if(strnstr(pLang, "lang=", BUFFMAXLN)==pLang) pLang+=5;
106 /* copy clientAddr(encoded) */
110 strncpy(clientAddr, pAddr4, ADDRMAXLN);
113 /* get language and check its correctness */
114 if(!isNull(pLang) && strstr(GetConfValue("HtmlLangs"), pLang)!=NULL){
116 /* if corrrect, set it */
117 strncpy(lang, pLang, WORDMAXLN);
120 /* if not correct, get default language at the top of lang list */
121 sscanf(GetConfValue("HtmlLangs"), "%s", lang);
124 /* send out header */
125 printf("Content-Type: text/html\r\n\r\n\r\n");
127 /* construct html file path */
128 sprintf(htmlFile, "%s%s/%s/", GetConfValue("DocumentRoot"),
131 /* ssl or non-ssl file */
132 if(!isNull(getenv("SERVER_PORT"))
133 && strcmp(getenv("SERVER_PORT"),GetServicePortStr("https"))==0) {
134 strncat(htmlFile, GetConfValue("AuthDocSsl"), BUFFMAXLN);
136 strncat(htmlFile, GetConfValue("AuthDoc"), BUFFMAXLN);
139 /* replace keywords and send out */
140 HtmlTemplate(htmlFile, keys);
142 if(debug>1) err_msg("DEBUG: terminated");