OSDN Git Service

Edit documentation and comment.
[opengate/opengate.git] / opengate / opengatesrv / comm-cgi.c
1 /**************************************************
2 opengate server
3  module for Communication through CGI 
4
5 Copyright (C) 1999 Opengate Project Team
6 Written by Yoshiaki Watanabe
7 Modfied Katsuhiko Eguchi, 2005 
8
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.
13
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.
18
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.
22
23 Email: watanaby@is.saga-u.ac.jp
24
25 Programmed by Yoshiaki WATANABE
26 Modified by Shin-ichi TADAKI
27 **************************************************/
28
29 #include        "opengatesrv.h"
30
31 /* convert two-char-hex "aa" to one-number 0Xaa */ 
32 #define hex2num(x)  ((x)>='A' ? ((x) & 0XDF) - 'A' +10 : ((x) - '0'))
33
34 void split(char content[], char *name[], char *value[], char *next[]);
35 void decode(char *string);
36
37 static char language[WORDMAXLN]; /* message language in java applet */
38
39 /*******************************/
40 /* get the client addr         */
41 /*******************************/
42 void getClientAddr(char *clientAddr)
43 {
44   strncpy(clientAddr, getenv("REMOTE_ADDR"), ADDRMAXLN);
45 }
46
47 /********************************************/
48 /* get Post data from the client  */
49 /********************************************/
50 void getPostData(char *userid, char *password, char *clientAddr4, int *durationPtr)
51 {
52   int contentLen;
53   char content[BUFFMAXLN];
54   char *name[1];
55   char *value[1];
56   char *next[1];
57   char *ptr;
58   int item;
59   char durationStr[WORDMAXLN];
60
61   /* get content sent from web input */
62   contentLen=atoi(getenv("CONTENT_LENGTH"));
63
64   contentLen++; /* for terminate ch */
65   if(contentLen > BUFFMAXLN) contentLen=BUFFMAXLN;
66   if(fgets(content, contentLen, stdin) == NULL){
67     content[0]='\0';
68   }
69
70   /* get items from string */
71   userid[0]='\0';
72   password[0]='\0';
73   clientAddr4[0]='\0';
74   language[0]='\0';
75   durationStr[0]='\0';
76
77   ptr=content;
78
79   for(item=0;item<4;item++){       /* get 4 items from string */
80     split(ptr, name, value, next);
81
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);
92     }
93     ptr=next[0];
94   }
95
96   /* decode the values */
97   decode(userid);
98   decode(password);
99   decode(clientAddr4);
100   decode(language);
101   decode(durationStr);
102
103   /* language check */
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';
108   }
109   
110   /* convert duration string to interger */
111   if(sscanf(durationStr,"%d",durationPtr)!=1){
112     *durationPtr=0;
113   }else{
114     *durationPtr *= 60;  /* convert minutes to seconds */
115   }
116       
117  return;
118 }
119
120 /*********************************************/
121 /* put deny message to the client            */
122 /*********************************************/
123 void putClientDeny(void)
124 {
125   FILE *fp;
126   char buff[BUFFMAXLN];
127   char denydoc[BUFFMAXLN];
128   
129   /* make path to the denydoc */
130   sprintf(denydoc,"%s/%s/%s",OGPATH,language,DENYDOC);
131
132   /* open 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");
136     return;
137   }
138
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){
142     fputs(buff,stdout);
143   }
144   fputs("\r\n\r\n",stdout);
145   fclose(fp);
146   return;
147 }
148
149 /*********************************************/
150 /* put some message to the client            */
151 /*********************************************/
152 void putClientMsg(char *message)
153 {
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");
159 }
160
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)
165 {
166   FILE *fp;
167   char buff[BUFFMAXLN];
168   int markfound=0;
169   char acceptdoc[BUFFMAXLN];
170   char *ptr;
171
172   /* make path to acceptdoc */
173   sprintf(acceptdoc,"%s/%s/%s",OGPATH,language,ACCEPTDOC);
174
175   /* open 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");
179     return;
180   }
181
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){
185
186     /* search insert mark and replace it */
187
188     if(strstr(buff,APPLETMARK)!=NULL){
189       /* applet insert mark found */
190
191       markfound=1;
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");
198
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");
220       }
221
222     }else if((ptr=strstr(buff,TERMINATEMARK))!=NULL){
223       /* terminate URL insert mark found */
224       /*  insert [http://<servaddr>:<port>/terminate<pid>] */
225
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);
230
231     }else if((ptr=strstr(buff,INFOMATIONMARK))!=NULL){
232       /* infomation URL insert mark found */
233       if(INFOMATION){
234         *ptr='\0'; printf("%s",buff);
235         printf("%s",INFOURL);
236         ptr=ptr+strlen(INFOMATIONMARK); printf("%s",ptr);
237       }else{
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);
241       }
242     }else{
243       /* normal http text */
244       fputs(buff,stdout);
245     }
246   }
247   fputs("\r\n\r\n",stdout);
248   fclose(fp);
249
250   if(markfound==0){
251     err_msg("ERR in comm-cgi: cannot find mark %s in %s",
252                    APPLETMARK, acceptdoc);
253     PutClientMsg("Cannot find applet mark");
254   }
255   return;
256 }
257
258 /************************************/
259 /* split value for indicated name   */
260 /* in content  "name=value&..."     */
261 /************************************/
262 void split(char content[], char *name[], char *value[], char *next[])
263 {
264   char *pstr;
265   
266   /* set default */
267   name[0]=content;
268   value[0]=content+strlen(content);
269   next[0]=value[0];
270
271   /* set name end */
272   if((pstr=strchr(name[0],(int)'='))==NULL) return;
273   *pstr='\0';
274   
275   /* set value start */
276   pstr++;
277   value[0]=pstr;
278   
279   /* set value end */
280   if((pstr=strchr(value[0],'&'))==NULL) return;
281   *pstr='\0';
282
283   /* set next start */
284   pstr++;
285   next[0]=pstr;
286
287   return;
288 }
289
290 /**********************************/
291 /* decode text coding in web post */
292 /**********************************/
293 void decode(char *string)
294 {
295   char *pcheck, *pinsert;
296
297   pcheck=pinsert=string;
298   while(*pcheck != '\0'){
299     if(*pcheck == '+'){
300       *pinsert = ' ';
301     }else if(*pcheck == '%'){
302       *pinsert=(char)(hex2num(*(pcheck+1))*16 + hex2num(*(pcheck+2)));
303       pcheck+=2;
304     }else{
305       *pinsert=*pcheck;
306     }
307     pcheck++;
308     pinsert++;
309   }
310   *pinsert=*pcheck;
311 }
312
313 /***************************************/
314 /* get HTTP_REFERER and check true url */
315 /***************************************/
316 int checkReferer(void)
317 {
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);
323       return FALSE;
324     }
325   }
326   return TRUE;
327 }
328
329 /*******************************/
330 /*******************************/
331 void GetClientAddr(char *clientAddr)
332 {
333   if(DEBUG) err_msg("DEBUG:=>getClientAddr( )");
334   getClientAddr(clientAddr);
335   if(DEBUG) err_msg("DEBUG:<=getClientAddr(%s)",clientAddr);
336 }
337
338
339 void GetPostData(char *userid, char *password, char *clientAddr4, int *durationPtr)
340 {
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);
344 }
345
346 void PutJavaApplet(char *userid, int port, int pid, char *clientAddr4, char *clientAddr6, int status)
347 {
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( )");
351 }
352
353 void PutClientDeny(void)
354 {
355   if(DEBUG) err_msg("DEBUG:=>putClientDeny( )");
356   putClientDeny();
357   if(DEBUG) err_msg("DEBUG:<=putClientDeny( )");
358 }
359
360 void PutClientMsg(char *message)
361 {
362   if(DEBUG) err_msg("DEBUG:=>putClientMsg( %s )",message);
363   putClientMsg(message);
364   if(DEBUG) err_msg("DEBUG:<=putClientMsg( )");
365 }
366
367 int CheckReferer(void)
368 {
369   int ret;
370   if(DEBUG) err_msg("DEBUG:=>checkReferer( )");
371   ret = checkReferer();
372   if(DEBUG) err_msg("DEBUG:(%d)<=checkReferer( )",ret);
373   return ret;
374 }