OSDN Git Service

Initial import.
[opengate/opengate.git] / opengate / opengatesrv / comm-cgi.c
1 /**************************************************
2 opengate server
3  module for Communication through CGI 
4
5 Copyright (C) 1999-2002 Yoshiaki Watanabe
6
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.
11
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.
16
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.
20
21 Email: watanaby@is.saga-u.ac.jp
22
23 Programmed by Yoshiaki WATANABE
24 Modified by Shin-ichi TADAKI
25 **************************************************/
26
27 #include        "opengatesrv.h"
28
29 /* convert two-char-hex "aa" to one-number 0Xaa */ 
30 #define hex2num(x)  ((x)>='A' ? ((x) & 0XDF) - 'A' +10 : ((x) - '0'))
31
32 void split(char content[], char *name[], char *value[], char *next[]);
33 void decode(char *string);
34
35 static char language[WORDMAXLN]; /* message language in java applet */
36
37 /*******************************/
38 /* get the client addr         */
39 /*******************************/
40 void getClientAddr(char *clientAddr)
41 {
42   strncpy(clientAddr, getenv("REMOTE_ADDR"), ADDRMAXLN);
43 }
44
45 /********************************************/
46 /* get userid and password from the client  */
47 /********************************************/
48 void getUserInfo(char *userid, char *password, int *durationPtr)
49 {
50   int contentLen;
51   char content[BUFFMAXLN];
52   char *name[1];
53   char *value[1];
54   char *next[1];
55   char *ptr;
56   int item;
57   char durationStr[WORDMAXLN];
58
59   /* get content sent from web input */
60   contentLen=atoi(getenv("CONTENT_LENGTH"));
61
62   contentLen++; /* for terminate ch */
63   if(contentLen > BUFFMAXLN) contentLen=BUFFMAXLN;
64   if(fgets(content, contentLen, stdin) == NULL){
65     content[0]='\0';
66   }
67
68   /* get items from string */
69   userid[0]='\0';
70   password[0]='\0';
71   language[0]='\0';
72   durationStr[0]='\0';
73
74   ptr=content;
75
76   for(item=0;item<4;item++){       /* get 4 items from string */
77     split(ptr, name, value, next);
78
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);
87     }
88     ptr=next[0];
89   }
90
91   /* decode the values */
92   decode(userid);
93   decode(password);
94   decode(language);
95   decode(durationStr);
96
97   /* language check */
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';
102   }
103   
104   /* convert duration string to interger */
105   if(sscanf(durationStr,"%d",durationPtr)!=1){
106     *durationPtr=DURATIONDEFAULT;
107   }else{
108     *durationPtr *= 60;  /* convert minutes to seconds */
109   }
110  if(*durationPtr <= 0){
111    *durationPtr=DURATIONDEFAULT;
112  }else if(*durationPtr > DURATIONMAX){
113    *durationPtr=DURATIONMAX;
114  }
115       
116  return;
117 }
118
119 /*********************************************/
120 /* put deny message to the client            */
121 /*********************************************/
122 void putClientDeny(void)
123 {
124   FILE *fp;
125   char buff[BUFFMAXLN];
126   char denydoc[BUFFMAXLN];
127   
128   /* make path to the denydoc */
129   sprintf(denydoc,"%s/%s/%s",OGPATH,language,DENYDOC);
130
131   /* open 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");
135     return;
136   }
137
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){
141     fputs(buff,stdout);
142   }
143   fputs("\r\n\r\n",stdout);
144   fclose(fp);
145   return;
146 }
147
148 /*********************************************/
149 /* put some message to the client            */
150 /*********************************************/
151 void putClientMsg(char *message)
152 {
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");
158 }
159
160 /*********************************************/
161 /* put accept message and java to the client */
162 /*********************************************/
163 void putJavaApplet(char *userid, int port, int pid)
164 {
165   FILE *fp;
166   char buff[BUFFMAXLN];
167   int markfound=0;
168   char acceptdoc[BUFFMAXLN];
169   char *ptr;
170
171   /* make path to acceptdoc */
172   sprintf(acceptdoc,"%s/%s/%s",OGPATH,language,ACCEPTDOC);
173
174   /* open 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");
178     return;
179   }
180
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){
184
185     /* search insert mark and replace it */
186
187     if(strstr(buff,APPLETMARK)!=NULL){
188       /* applet insert mark found */
189
190       markfound=1;
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");
197
198     }else if((ptr=strstr(buff,TERMINATEMARK))!=NULL){
199       /* terminate URL insert mark found */
200       /*  insert [http://<servaddr>:<port>/terminate<pid>] */
201
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);
206
207     }else{
208       /* normal http text */
209       fputs(buff,stdout);
210     }
211   }
212   fputs("\r\n\r\n",stdout);
213   fclose(fp);
214
215   if(markfound==0){
216     err_msg("ERR in comm-cgi: cannot find mark %s in %s",
217                    APPLETMARK, acceptdoc);
218     PutClientMsg("Cannot find applet mark");
219   }
220   return;
221 }
222
223 /************************************/
224 /* split value for indicated name   */
225 /* in content  "name=value&..."     */
226 /************************************/
227 void split(char content[], char *name[], char *value[], char *next[])
228 {
229   char *pstr;
230   
231   /* set default */
232   name[0]=content;
233   value[0]=content+strlen(content);
234   next[0]=value[0];
235
236   /* set name end */
237   if((pstr=strchr(name[0],(int)'='))==NULL) return;
238   *pstr='\0';
239   
240   /* set value start */
241   pstr++;
242   value[0]=pstr;
243   
244   /* set value end */
245   if((pstr=strchr(value[0],'&'))==NULL) return;
246   *pstr='\0';
247
248   /* set next start */
249   pstr++;
250   next[0]=pstr;
251
252   return;
253 }
254
255 /**********************************/
256 /* decode text coding in web post */
257 /**********************************/
258 void decode(char *string)
259 {
260   char *pcheck, *pinsert;
261
262   pcheck=pinsert=string;
263   while(*pcheck != '\0'){
264     if(*pcheck == '+'){
265       *pinsert = ' ';
266     }else if(*pcheck == '%'){
267       *pinsert=(char)(hex2num(*(pcheck+1))*16 + hex2num(*(pcheck+2)));
268       pcheck+=2;
269     }else{
270       *pinsert=*pcheck;
271     }
272     pcheck++;
273     pinsert++;
274   }
275   *pinsert=*pcheck;
276 }
277
278 /*******************************/
279 /*******************************/
280 void GetClientAddr(char *clientAddr)
281 {
282   if(DEBUG) err_msg("DEBUG:=>getClientAddr( )");
283   getClientAddr(clientAddr);
284   if(DEBUG) err_msg("DEBUG:<=getClientAddr(%s)",clientAddr);
285 }
286
287
288 void GetUserInfo(char *userid, char *password, int *durationPtr)
289 {
290   if(DEBUG) err_msg("DEBUG:=>getUserInfo( )");
291   getUserInfo(userid,password,durationPtr);
292   if(DEBUG) err_msg("DEBUG:<=getUserInfo(%s,passwd,%d)",userid,*durationPtr);
293 }
294
295 void PutJavaApplet(char *userid, int port, int pid)
296 {
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( )");
300 }
301
302 void PutClientDeny(void)
303 {
304   if(DEBUG) err_msg("DEBUG:=>putClientDeny( )");
305   putClientDeny();
306   if(DEBUG) err_msg("DEBUG:<=putClientDeny( )");
307 }
308
309 void PutClientMsg(char *message)
310 {
311   if(DEBUG) err_msg("DEBUG:=>putClientMsg( %s )",message);
312   putClientMsg(message);
313   if(DEBUG) err_msg("DEBUG:<=putClientMsg( )");
314 }
315