OSDN Git Service

Ver.1.4.19: Modify control of favicon.ico.
[opengate/opengate.git] / opengate / opengatesrv / opengateauth.c
1 /**************************************************
2 Opengate authentication CGI main
3
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.
6
7 Copyright (C) 2005 Opengate Project Team
8 Written by Katsuhiko Eguchi, 2005 
9 Modified by Yoshiaki Watanabe
10
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.
15
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.
20
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.
24
25 Email: watanaby@is.saga-u.ac.jp
26 **************************************************/
27
28 #include "opengatesrv.h"
29
30 int  main(int argc, char **argv)
31 {
32
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 */
44
45   /* keyword pairs */
46   /*  the left key is replaced by the right value */
47   struct html_key keys[]=
48     {
49       {"%%OPENGATESERVERNAME%%", opengateServerName},
50       {"%%OPENGATEDIR%%", opengateDir},
51       {"%%AUTHCGIURL%%", authCgiUrl},
52       {"%%CGIURL%%", mainCgiUrl},
53       {"%%ADDR4%%", clientAddr},
54       {"%%DURATIONMAX%%",durationMax},
55       {"",""}  /* DON'T REMOVE THIS LINE */
56     };
57
58   /* prepare config file */
59   if(OpenConfFile()==-1) return 0;
60  
61   /* start log */
62   errToSyslog(atoi(GetConfValue("Syslog/Enable")));
63   openlog(GetConfValue("AuthCgi"), LOG_PID, atoi(GetConfValue("Syslog/Facility")));
64
65   /* initialize config */
66   InitConf();
67
68   if(debug>1) err_msg("DEBUG: started");
69
70   snprintf(opengateServerName,BUFFMAXLN,GetConfValue("OpengateServerName"));
71   snprintf(opengateDir,BUFFMAXLN,GetConfValue("OpengateDir"));
72
73   /* create URL string */
74   snprintf(authCgiUrl, BUFFMAXLN, "%s%s%s/%s",
75            opengateServerName,
76            GetConfValue("CgiDir"),
77            opengateDir,
78            GetConfValue("AuthCgi"));
79   snprintf(mainCgiUrl, BUFFMAXLN, "%s%s%s/%s",
80            opengateServerName,
81            GetConfValue("CgiDir"),
82            opengateDir,
83            GetConfValue("MainCgi"));
84   
85   /* setup usage duration maximum and default (saved as seconds) */
86   snprintf(durationMax,WORDMAXLN,"%d",
87            atoi(GetConfValue("Duration/Max"))/60);
88
89   /* get paremeters, */
90   if(!isNull(getenv("QUERY_STRING"))){
91     /* get html access parameter string (xx.cgi?addr4&lang) */
92     strncpy(paramString, getenv("QUERY_STRING"), BUFFMAXLN);
93   }
94
95   /* split language and address in paramString[addr=0-0-0&lang=ja] */
96   pAddr4=paramString;
97   if((pLang=strnstr(paramString, "&", BUFFMAXLN))!=NULL){
98     *pLang='\0'; pLang++;
99   }else{
100     pLang=paramString;
101   }
102
103   if(strnstr(pAddr4, "addr=", BUFFMAXLN)==pAddr4) pAddr4+=5;
104   if(strnstr(pLang, "lang=", BUFFMAXLN)==pLang)  pLang+=5;
105
106   /* copy clientAddr(encoded) */
107   if(isNull(pAddr4)){
108     clientAddr[0]='\0';
109   }else{
110     strncpy(clientAddr, pAddr4, ADDRMAXLN);
111   }
112
113   /* get language and check its correctness */
114   if(!isNull(pLang) && strstr(GetConfValue("HtmlLangs"), pLang)!=NULL){
115
116     /* if corrrect, set it */
117     strncpy(lang, pLang, WORDMAXLN);
118   }else{
119
120     /* if not correct, get default language at the top of lang list */
121     sscanf(GetConfValue("HtmlLangs"), "%s", lang);
122   }
123
124   /* send out header */
125   printf("Content-Type: text/html\r\n\r\n\r\n");
126
127   /* construct html file path */
128   sprintf(htmlFile, "%s%s/%s/", GetConfValue("DocumentRoot"),
129           opengateDir, lang);
130
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);
135   }else{
136     strncat(htmlFile, GetConfValue("AuthDoc"), BUFFMAXLN);
137   }
138
139   /* replace keywords and send out */
140   HtmlTemplate(htmlFile, keys);
141
142   if(debug>1) err_msg("DEBUG: terminated");  
143
144   return 0;
145 }