1 /**************************************************
3 module for Communication through CGI
5 Copyright (C) 1999-2002 Yoshiaki Watanabe
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 Email: watanaby@is.saga-u.ac.jp
23 Programmed by Yoshiaki WATANABE
24 Modified by Shin-ichi TADAKI
25 **************************************************/
27 #include "opengatesrv.h"
29 /* convert two-char-hex "aa" to one-number 0Xaa */
30 #define hex2num(x) ((x)>='A' ? ((x) & 0XDF) - 'A' +10 : ((x) - '0'))
32 void split(char content[], char *name[], char *value[], char *next[]);
33 void decode(char *string);
35 static char language[WORDMAXLN]; /* message language in java applet */
37 /*******************************/
38 /* get the client addr */
39 /*******************************/
40 void getClientAddr(char *clientAddr)
42 strncpy(clientAddr, getenv("REMOTE_ADDR"), ADDRMAXLN);
45 /********************************************/
46 /* get userid and password from the client */
47 /********************************************/
48 void getUserInfo(char *userid, char *password, int *durationPtr)
51 char content[BUFFMAXLN];
57 char durationStr[WORDMAXLN];
59 /* get content sent from web input */
60 contentLen=atoi(getenv("CONTENT_LENGTH"));
62 contentLen++; /* for terminate ch */
63 if(contentLen > BUFFMAXLN) contentLen=BUFFMAXLN;
64 if(fgets(content, contentLen, stdin) == NULL){
68 /* get items from string */
76 for(item=0;item<4;item++){ /* get 4 items from string */
77 split(ptr, name, value, next);
79 if(strstr(name[0], "userid")!=NULL){
80 strncpy(userid, value[0], USERMAXLN);
81 }else if(strstr(name[0], "password")!=NULL){
82 strncpy(password, value[0], PASSMAXLN);
83 }else if(strstr(name[0], "language")!=NULL){
84 strncpy(language, value[0], WORDMAXLN);
85 }else if(strstr(name[0], "duration")!=NULL){
86 strncpy(durationStr, value[0], WORDMAXLN);
91 /* decode the values */
98 language[2]='\0'; /* shorten to two bytes */
99 ptr=HTMLLANGS; /* list of available languages */
100 if(strstr(ptr,language)==0){ /* if not available, use first lang */
101 language[0]=*ptr; language[1]=*(ptr+1); language[2]='\0';
104 /* convert duration string to interger */
105 if(sscanf(durationStr,"%d",durationPtr)!=1){
106 *durationPtr=DURATIONDEFAULT;
108 *durationPtr *= 60; /* convert minutes to seconds */
110 if(*durationPtr <= 0){
111 *durationPtr=DURATIONDEFAULT;
112 }else if(*durationPtr > DURATIONMAX){
113 *durationPtr=DURATIONMAX;
119 /*********************************************/
120 /* put deny message to the client */
121 /*********************************************/
122 void putClientDeny(void)
125 char buff[BUFFMAXLN];
126 char denydoc[BUFFMAXLN];
128 /* make path to the denydoc */
129 sprintf(denydoc,"%s/%s/%s",OGPATH,language,DENYDOC);
132 if((fp=fopen(denydoc, "r"))==NULL){
133 err_msg("ERR in comm-cgi: cannot open %s", denydoc);
134 PutClientMsg("Cannot find html document");
138 /* read html document from file and send to web */
139 printf("Content-type: text/html\r\n\r\n\r\n");
140 while(fgets(buff, BUFFMAXLN, fp)!=NULL){
143 fputs("\r\n\r\n",stdout);
148 /*********************************************/
149 /* put some message to the client */
150 /*********************************************/
151 void putClientMsg(char *message)
153 printf("Content-type: text/html\r\n\r\n\r\n");
154 printf("<HTML><HEAD><TITLE>OpengateMsg</TITLE></HEAD> \r\n");
155 printf("<BODY> \r\n");
156 printf("%s\r\n", message);
157 printf("</BODY></HTML> \r\n\r\n");
160 /*********************************************/
161 /* put accept message and java to the client */
162 /*********************************************/
163 void putJavaApplet(char *userid, int port, int pid)
166 char buff[BUFFMAXLN];
168 char acceptdoc[BUFFMAXLN];
171 /* make path to acceptdoc */
172 sprintf(acceptdoc,"%s/%s/%s",OGPATH,language,ACCEPTDOC);
175 if((fp=fopen(acceptdoc, "r"))==NULL){
176 err_msg("ERR in comm-cgi: cannot open %s", acceptdoc);
177 PutClientMsg("Cannot find html document");
181 /* read html document from file and send to web */
182 printf("Content-type: text/html\r\n\r\n\r\n");
183 while(fgets(buff, BUFFMAXLN, fp)!=NULL){
185 /* search insert mark and replace it */
187 if(strstr(buff,APPLETMARK)!=NULL){
188 /* applet insert mark found */
191 printf("<applet code='Opengate.class' archive='Opengate.jar' ");
192 printf("codebase='%s' width=600 height=30 > \r\n", OPENGATEDIR );
193 printf("<param name=port value='%d'>\r\n", port );
194 printf("<param name=user value='%s'>\r\n", userid );
195 printf("<param name=lang value='%s'>\r\n", language );
196 printf("</applet> \r\n");
198 }else if((ptr=strstr(buff,TERMINATEMARK))!=NULL){
199 /* terminate URL insert mark found */
200 /* insert [http://<servaddr>:<port>/terminate<pid>] */
202 *ptr='\0'; printf("%s",buff);
203 printf("http://%s:%d/terminate%d",
204 getenv("SERVER_ADDR"), port, pid);
205 ptr=ptr+strlen(TERMINATEMARK); printf("%s", ptr);
208 /* normal http text */
212 fputs("\r\n\r\n",stdout);
216 err_msg("ERR in comm-cgi: cannot find mark %s in %s",
217 APPLETMARK, acceptdoc);
218 PutClientMsg("Cannot find applet mark");
223 /************************************/
224 /* split value for indicated name */
225 /* in content "name=value&..." */
226 /************************************/
227 void split(char content[], char *name[], char *value[], char *next[])
233 value[0]=content+strlen(content);
237 if((pstr=strchr(name[0],(int)'='))==NULL) return;
240 /* set value start */
245 if((pstr=strchr(value[0],'&'))==NULL) return;
255 /**********************************/
256 /* decode text coding in web post */
257 /**********************************/
258 void decode(char *string)
260 char *pcheck, *pinsert;
262 pcheck=pinsert=string;
263 while(*pcheck != '\0'){
266 }else if(*pcheck == '%'){
267 *pinsert=(char)(hex2num(*(pcheck+1))*16 + hex2num(*(pcheck+2)));
278 /*******************************/
279 /*******************************/
280 void GetClientAddr(char *clientAddr)
282 if(DEBUG) err_msg("DEBUG:=>getClientAddr( )");
283 getClientAddr(clientAddr);
284 if(DEBUG) err_msg("DEBUG:<=getClientAddr(%s)",clientAddr);
288 void GetUserInfo(char *userid, char *password, int *durationPtr)
290 if(DEBUG) err_msg("DEBUG:=>getUserInfo( )");
291 getUserInfo(userid,password,durationPtr);
292 if(DEBUG) err_msg("DEBUG:<=getUserInfo(%s,passwd,%d)",userid,*durationPtr);
295 void PutJavaApplet(char *userid, int port, int pid)
297 if(DEBUG) err_msg("DEBUG:=>putJavaApplet(%s,%d,%d)",userid,port,pid);
298 putJavaApplet(userid,port,pid);
299 if(DEBUG) err_msg("DEBUG:<=putJavaApplet( )");
302 void PutClientDeny(void)
304 if(DEBUG) err_msg("DEBUG:=>putClientDeny( )");
306 if(DEBUG) err_msg("DEBUG:<=putClientDeny( )");
309 void PutClientMsg(char *message)
311 if(DEBUG) err_msg("DEBUG:=>putClientMsg( %s )",message);
312 putClientMsg(message);
313 if(DEBUG) err_msg("DEBUG:<=putClientMsg( )");