1 /**************************************************
3 module for Communication through CGI
5 Copyright (C) 1999 Opengate Project Team
6 Written by Yoshiaki Watanabe
7 Modfied Katsuhiko Eguchi, 2005
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 Email: watanaby@is.saga-u.ac.jp
25 Programmed by Yoshiaki WATANABE
26 Modified by Shin-ichi TADAKI
27 **************************************************/
29 #include "opengatesrv.h"
31 /* convert two-char-hex "aa" to one-number 0Xaa */
32 #define hex2num(x) ((x)>='A' ? ((x) & 0XDF) - 'A' +10 : ((x) - '0'))
34 void split(char content[], char *name[], char *value[], char *next[]);
35 void decode(char *string);
37 static char language[WORDMAXLN]; /* message language in java applet */
39 /*******************************/
40 /* get the client addr */
41 /*******************************/
42 void getClientAddr(char *clientAddr)
44 strncpy(clientAddr, getenv("REMOTE_ADDR"), ADDRMAXLN);
47 /********************************************/
48 /* get Post data from the client */
49 /********************************************/
50 void getPostData(char *userid, char *password, char *clientAddr4, int *durationPtr)
53 char content[BUFFMAXLN];
59 char durationStr[WORDMAXLN];
61 /* get content sent from web input */
62 contentLen=atoi(getenv("CONTENT_LENGTH"));
64 contentLen++; /* for terminate ch */
65 if(contentLen > BUFFMAXLN) contentLen=BUFFMAXLN;
66 if(fgets(content, contentLen, stdin) == NULL){
70 /* get items from string */
79 for(item=0;item<4;item++){ /* get 4 items from string */
80 split(ptr, name, value, next);
82 if(strstr(name[0], "userid")!=NULL){
83 strncpy(userid, value[0], USERMAXLN);
84 }else if(strstr(name[0], "password")!=NULL){
85 strncpy(password, value[0], PASSMAXLN);
86 }else if(strstr(name[0],"remote_addr")!=NULL){
87 strncpy(clientAddr4,value[0],ADDRMAXLN);
88 }else if(strstr(name[0], "language")!=NULL){
89 strncpy(language, value[0], WORDMAXLN);
90 }else if(strstr(name[0], "duration")!=NULL){
91 strncpy(durationStr, value[0], WORDMAXLN);
96 /* decode the values */
104 language[2]='\0'; /* shorten to two bytes */
105 ptr=HTMLLANGS; /* list of available languages */
106 if(strstr(ptr,language)==0){ /* if not available, use first lang */
107 language[0]=*ptr; language[1]=*(ptr+1); language[2]='\0';
110 /* convert duration string to interger */
111 if(sscanf(durationStr,"%d",durationPtr)!=1){
114 *durationPtr *= 60; /* convert minutes to seconds */
120 /*********************************************/
121 /* put deny message to the client */
122 /*********************************************/
123 void putClientDeny(void)
126 char buff[BUFFMAXLN];
127 char denydoc[BUFFMAXLN];
129 /* make path to the denydoc */
130 sprintf(denydoc,"%s/%s/%s",OGPATH,language,DENYDOC);
133 if((fp=fopen(denydoc, "r"))==NULL){
134 err_msg("ERR in comm-cgi: cannot open %s", denydoc);
135 PutClientMsg("Cannot find html document");
139 /* read html document from file and send to web */
140 printf("Content-type: text/html\r\n\r\n\r\n");
141 while(fgets(buff, BUFFMAXLN, fp)!=NULL){
144 fputs("\r\n\r\n",stdout);
149 /*********************************************/
150 /* put some message to the client */
151 /*********************************************/
152 void putClientMsg(char *message)
154 printf("Content-type: text/html\r\n\r\n\r\n");
155 printf("<HTML><HEAD><TITLE>OpengateMsg</TITLE></HEAD> \r\n");
156 printf("<BODY> \r\n");
157 printf("%s\r\n", message);
158 printf("</BODY></HTML> \r\n\r\n");
161 /*********************************************/
162 /* put accept message and java to the client */
163 /*********************************************/
164 void putJavaApplet(char *userid, int port, int pid, char *clientAddr4, char *clientAddr6, int status)
167 char buff[BUFFMAXLN];
169 char acceptdoc[BUFFMAXLN];
172 /* make path to acceptdoc */
173 sprintf(acceptdoc,"%s/%s/%s",OGPATH,language,ACCEPTDOC);
176 if((fp=fopen(acceptdoc, "r"))==NULL){
177 err_msg("ERR in comm-cgi: cannot open %s", acceptdoc);
178 PutClientMsg("Cannot find html document");
182 /* read html document from file and send to web */
183 printf("Content-type: text/html\r\n\r\n\r\n");
184 while(fgets(buff, BUFFMAXLN, fp)!=NULL){
186 /* search insert mark and replace it */
188 if(strstr(buff,APPLETMARK)!=NULL){
189 /* applet insert mark found */
192 printf("<applet code='Opengate.class' archive='Opengate.jar' ");
193 printf("codebase='%s' width=600 height=30 > \r\n", OPENGATEDIR );
194 printf("<param name=port value='%d'>\r\n", port );
195 printf("<param name=user value='%s'>\r\n", userid );
196 printf("<param name=lang value='%s'>\r\n", language );
197 printf("</applet> \r\n");
199 if(status==IPV4ONLY){
200 printf("<table border=0>\r\n");
201 printf("<tr><td>\r\n");
202 printf("IPv4 address : [%s]\r\n",clientAddr4);
203 printf("</td></tr>\r\n");
204 printf("</table>\r\n");
205 }else if(status=IPV46DUAL){
206 printf("<table border=0>\r\n");
207 printf("<tr><td>\r\n");
208 printf("IPv4 address : [%s]\r\n",clientAddr4);
209 printf("</td></tr>\r\n");
210 printf("<tr><td>\r\n");
211 printf("IPv6 address : [%s]\r\n",clientAddr6);
212 printf("</td></tr>\r\n");
213 printf("</table>\r\n");
214 }else if(status==IPV6ONLY){
215 printf("<table border=0>\r\n");
216 printf("<tr><td>\r\n");
217 printf("IPv6 address : [%s]\r\n",clientAddr6);
218 printf("</td></tr>\r\n");
219 printf("</table>\r\n");
222 }else if((ptr=strstr(buff,TERMINATEMARK))!=NULL){
223 /* terminate URL insert mark found */
224 /* insert [http://<servaddr>:<port>/terminate<pid>] */
226 *ptr='\0'; printf("%s",buff);
227 printf("http://%s:%d/terminate%d",
228 getenv("SERVER_NAME"), port, pid);
229 ptr=ptr+strlen(TERMINATEMARK); printf("%s", ptr);
231 }else if((ptr=strstr(buff,INFOMATIONMARK))!=NULL){
232 /* infomation URL insert mark found */
234 *ptr='\0'; printf("%s",buff);
235 printf("%s",INFOURL);
236 ptr=ptr+strlen(INFOMATIONMARK); printf("%s",ptr);
238 *ptr='\0'; printf("%s",buff);
239 printf("http://%s%s/%s/%s",HOSTNAME,OPENGATEDIR,language,ACCEPTDOC2);
240 ptr=ptr+strlen(INFOMATIONMARK); printf("%s",ptr);
243 /* normal http text */
247 fputs("\r\n\r\n",stdout);
251 err_msg("ERR in comm-cgi: cannot find mark %s in %s",
252 APPLETMARK, acceptdoc);
253 PutClientMsg("Cannot find applet mark");
258 /************************************/
259 /* split value for indicated name */
260 /* in content "name=value&..." */
261 /************************************/
262 void split(char content[], char *name[], char *value[], char *next[])
268 value[0]=content+strlen(content);
272 if((pstr=strchr(name[0],(int)'='))==NULL) return;
275 /* set value start */
280 if((pstr=strchr(value[0],'&'))==NULL) return;
290 /**********************************/
291 /* decode text coding in web post */
292 /**********************************/
293 void decode(char *string)
295 char *pcheck, *pinsert;
297 pcheck=pinsert=string;
298 while(*pcheck != '\0'){
301 }else if(*pcheck == '%'){
302 *pinsert=(char)(hex2num(*(pcheck+1))*16 + hex2num(*(pcheck+2)));
313 /***************************************/
314 /* get HTTP_REFERER and check true url */
315 /***************************************/
316 int checkReferer(void)
318 char url[BUFFMAXLN]="";
319 if(getenv("HTTP_REFERER")!=NULL){
320 strncpy(url,getenv("HTTP_REFERER"),BUFFMAXLN);
321 if(strstr(url,HOSTNAME4)==NULL && strstr(url,HOSTNAME)==NULL){
322 printf("location:http://%s/\r\n\r\n",HOSTNAME);
329 /*******************************/
330 /*******************************/
331 void GetClientAddr(char *clientAddr)
333 if(DEBUG) err_msg("DEBUG:=>getClientAddr( )");
334 getClientAddr(clientAddr);
335 if(DEBUG) err_msg("DEBUG:<=getClientAddr(%s)",clientAddr);
339 void GetPostData(char *userid, char *password, char *clientAddr4, int *durationPtr)
341 if(DEBUG) err_msg("DEBUG:=>getPostData( )");
342 getPostData(userid,password,clientAddr4,durationPtr);
343 if(DEBUG) err_msg("DEBUG:<=getPostData(%s,passwd,%s,%d)",userid,clientAddr4,*durationPtr);
346 void PutJavaApplet(char *userid, int port, int pid, char *clientAddr4, char *clientAddr6, int status)
348 if(DEBUG) err_msg("DEBUG:=>putJavaApplet(%s,%d,%d,%s,%s,%d)",userid,port,pid,clientAddr4,clientAddr6,status);
349 putJavaApplet(userid,port,pid,clientAddr4,clientAddr6,status);
350 if(DEBUG) err_msg("DEBUG:<=putJavaApplet( )");
353 void PutClientDeny(void)
355 if(DEBUG) err_msg("DEBUG:=>putClientDeny( )");
357 if(DEBUG) err_msg("DEBUG:<=putClientDeny( )");
360 void PutClientMsg(char *message)
362 if(DEBUG) err_msg("DEBUG:=>putClientMsg( %s )",message);
363 putClientMsg(message);
364 if(DEBUG) err_msg("DEBUG:<=putClientMsg( )");
367 int CheckReferer(void)
370 if(DEBUG) err_msg("DEBUG:=>checkReferer( )");
371 ret = checkReferer();
372 if(DEBUG) err_msg("DEBUG:(%d)<=checkReferer( )",ret);