Handles method GET.
[egate.git] / www / wcgi.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  * $Log: wcgi.c,v $
44  * Revision 1.11  1996/01/08 08:42:19  adam
45  * Handles method GET.
46  *
47  * Revision 1.10  1996/01/05  16:21:20  adam
48  * Bug fix: shell (wproto) sometimes closed server FIFO before cgi
49  * program opened it - solution: cgi sends OK when response has been read.
50  *
51  * Revision 1.9  1995/12/20  16:31:33  adam
52  * Bug fix: shell might terminate even though new request was initiated
53  * by the cgi interface program.
54  * Work on more simple user interface and Europagate buttons.
55  *
56  * Revision 1.8  1995/11/08  16:14:35  adam
57  * Many improvements and bug fixes.
58  * First version that ran on dtbsun.
59  *
60  * Revision 1.7  1995/11/08  12:42:18  adam
61  * Added descriptive text field in target info.
62  * Added authentication field in target info.
63  *
64  * Revision 1.6  1995/11/06  17:44:22  adam
65  * State reestablised when shell restarts. History of previous
66  * result sets.
67  *
68  * Revision 1.5  1995/11/06  10:51:15  adam
69  * End of response marker in response from wsh/wproto to wcgi.
70  * Shells are respawned when necessary.
71  *
72  * Revision 1.4  1995/11/02  16:35:37  adam
73  * Bug fixes and select on FIFOs in wcgi - doesn't really work!
74  *
75  * Revision 1.3  1995/10/31  16:56:24  adam
76  * Record presentation.
77  *
78  * Revision 1.2  1995/10/23  16:55:36  adam
79  * A lot of changes - really.
80  *
81  * Revision 1.1  1995/10/20  11:49:25  adam
82  * First version of www gateway.
83  *
84  */
85
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <unistd.h>
89 #include <fcntl.h>
90 #include <sys/stat.h>
91 #include <sys/time.h>
92 #include <sys/types.h>
93 #ifdef AIX
94 #include <sys/select.h>
95 #endif
96
97 #define DEADSTRING "Your database server has terminated. To reactivate \
98 the server, please reload the server's 'front page'."
99
100 #include <gw-db.h>
101 #include "wproto.h"
102
103 #define CGIDIR "/usr/local/etc/httpd/cgi-bin"
104
105 static char *prog = "cgi";
106
107 static char serverp[256] = {'\0'};
108 static GW_DB gw_db = NULL;
109
110 static void fatal(char *p)
111 {
112     printf("Content-type: text/html\n\n<HTML><HEAD><TITLE>Server Failure</TITLE></HEAD>\n");
113     printf("<BODY>%s</BODY>\n", p);
114     if (gw_db)
115         gw_db_close (gw_db);
116     if (*serverp)
117         unlink(serverp);
118     exit(0);
119 }
120
121 static int spawn (char *sprog, int id)
122 {
123     int r;
124     char path[256];
125     char envstr[80];
126
127     sprintf (envstr, "GWID=%d", id);
128     putenv (envstr);
129     sprintf(path, "%s/%s", CGIDIR, sprog);
130     switch(r = fork())
131     {
132         case -1: 
133             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "fork"); 
134             exit(1);
135         case 0: 
136             close (0);
137             close (1);
138             close (2);
139             gw_log (GW_LOG_DEBUG, prog, "execl %s", path);
140             execl (path, sprog, 0); 
141             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "execl %s", path);
142             exit(0);
143         default: 
144             return r;
145     }
146 }
147
148 #if 0
149 static void print_environ (void)
150 {
151     extern char **environ;
152     int i;
153
154     for (i = 0; environ[i]; i++)
155         gw_log (GW_LOG_DEBUG, prog, "e: %s", environ[i]);
156 }
157 #endif
158
159 /*
160  * NOTE: In the (perhaps odd) terminology used within this software,
161  * the 'server' is the present program, which is executed by the httpd
162  * server. The 'client' is the process running outside.
163  * Protocol is long(len)<serverfifo>\0<extrapath>\0<envvars>\0<INFO>\0
164  */
165 int main()
166 {
167     char clientp[256], tmp[256], *path_info, *p, *operation, *t;
168     char combuf[COMBUF];
169     int linein = -1, lineout, data, gw_id;
170
171     chdir ("/usr/local/etc/httpd/cgi-bin");
172     gw_log_init ("egw");
173     gw_log_file (GW_LOG_ALL, "/usr/local/etc/httpd/logs/egwcgi_log");
174     gw_log_level (GW_LOG_ALL);
175     gw_log (GW_LOG_STAT, prog, "Europagate www cgi server");
176
177     sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR);
178     if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0)
179     {
180         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp);
181         fatal("Internal error in server.");
182     }
183     sprintf(serverp, "%s/srv%d", tmp, getpid());
184     if (access(serverp, R_OK|W_OK) == 0)
185     {
186         if (unlink(serverp) < 0)
187         {
188             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog,
189                     "Failed to delete stale fifo.");
190             fatal("Internal error in server.");
191         }
192         else
193             gw_log (GW_LOG_WARN, prog, "Removed stale server fifo.");
194     }
195     if (mkfifo(serverp, 0666 | S_IFIFO) < 0)
196     {
197         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "mkfifo(%s)", serverp);
198         fatal("Internal error in server.");
199     }
200     if (!(path_info = getenv("PATH_INFO")))
201     {
202         gw_log (GW_LOG_FATAL, prog, "Must set PATH_INFO.");
203         fatal("Internal error in server.");
204     }
205     operation = ++path_info;
206     while (*path_info && *path_info != '/')
207         path_info++;
208     if (*path_info)
209         *(path_info++) = '\0';
210     if (!(gw_db = gw_db_open ("www.db", 1, 1)))
211     {
212         gw_log (GW_LOG_FATAL, prog, "gw_db_open");
213         exit (1);
214     }
215     if ((gw_id = atoi(operation)) <= 0)
216     {
217         int r;
218         char gw_id_str[16];
219
220         gw_id = gw_db_seq_no (gw_db);
221         sprintf (gw_id_str, "%d", gw_id);
222         
223         spawn(operation, gw_id);
224         r = gw_db_insert (gw_db, gw_id_str, strlen(gw_id_str)+1,
225                           operation, strlen(operation)+1);
226         if (r)
227         {
228             gw_log (GW_LOG_FATAL, prog, "gw_db_insert: %d", r);
229             gw_db_close (gw_db);
230             exit (1);
231         }
232         gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
233         if ((linein = open(serverp, O_RDONLY)) < 0)
234         {
235             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
236             fatal("Internal error in server.");
237         }
238         if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
239         {
240             gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client");
241             fatal("Internal error in server");
242         }
243         gw_log (GW_LOG_DEBUG, prog, "Synchronized");
244     }
245     sprintf(clientp, "%s/clt%d", tmp, gw_id);
246     gw_log (GW_LOG_DEBUG, prog, "Opening %s", clientp);
247     if ((lineout = open(clientp, O_WRONLY)) < 0)
248     {
249         char gw_id_str[16];
250         void *sprog;
251         size_t sprog_size;
252         int r;
253
254         sprintf (gw_id_str, "%d", gw_id);
255         r = gw_db_lookup (gw_db, gw_id_str, strlen(gw_id_str)+1,  
256                           &sprog, &sprog_size);
257         if (r != 1)
258         {
259             gw_log (GW_LOG_FATAL, prog, "gw_db_lookup %s", gw_id_str);
260             fatal("Internal error in server");
261         }
262         gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "open %s restart", clientp);
263         spawn (sprog, gw_id);
264         gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
265         if ((linein = open(serverp, O_RDONLY)) < 0)
266         {
267             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
268             fatal("Internal error in server");
269         }
270         if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
271         {
272             gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client.");
273             fatal("Internal error in server");
274         }
275         gw_log (GW_LOG_DEBUG, prog, "Synchronized.");
276         if ((lineout = open(clientp, O_WRONLY)) < 0)
277         {
278             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp);
279             fatal("Internal error in server");
280         }
281     }
282     gw_db_close (gw_db);
283     gw_log (GW_LOG_DEBUG, prog, "Decoding user data");
284     p = combuf + sizeof(data);
285     strcpy(p, serverp);
286     p += strlen(p) + 1;
287     strcpy(p, path_info);
288     gw_log (GW_LOG_DEBUG, prog, "P:%s", p);
289     p += strlen(p) + 1;
290     *(p++) = '\0';               /* no envvars tranferred at present */
291     if ((t = getenv("CONTENT_LENGTH")) && (data = atoi(t)) > 0)
292     {
293         int j, i = 0;
294         while (i < data)
295         {
296             j = read(0, p + i, data - i);
297             if (j == -1)
298             {
299                 gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog,
300                         "Failed to read input");
301                 fatal("Internal error in server");
302             }
303             else if (j == 0)
304             {
305                 gw_log (GW_LOG_ERRNO, prog, "Failed to read input");
306                 fatal("Internal error in server");
307             }
308             i += j;
309         }
310     }
311     else if ((t = getenv("QUERY_STRING")))
312     {
313         strcpy (p, t);
314         data = strlen(p);
315     }
316     p[data] = '\0';
317     gw_log (GW_LOG_DEBUG, prog, "C:%s", p);
318     p += data+1;
319     data = (p - combuf);
320     memcpy(combuf, &data, sizeof(data));
321     gw_log (GW_LOG_DEBUG, prog, "Writing data");
322     if (write(lineout, combuf, data) < data)
323     {
324         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
325         fatal("Internal server error");
326     }
327     if (linein < 0)
328     {
329         gw_log (GW_LOG_DEBUG, prog, "open %s", serverp);
330         if ((linein = open(serverp, O_RDONLY)) < 0)
331         {
332             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
333             fatal("Internal error in server");
334         }
335     }
336     gw_log (GW_LOG_DEBUG, prog, "Reading response");
337
338 #if 1
339     while ((data = read(linein, combuf, COMBUF)) > 0)
340     {
341         gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
342         if (combuf[data-1] == '\0')
343             break;
344         if (write(1, combuf, data) < data)
345         {
346             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
347             exit (1);
348         }
349     }
350     if (data < 0)
351     {
352         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
353         exit (1);
354     }
355     if (data > 0)
356     {
357         --data;
358         if (write(1, combuf, data) < data)
359         {
360             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
361             exit (1);
362         }
363     }
364 #else
365 #  if 1
366     fcntl (linein, F_SETFL, O_NONBLOCK);
367 #  endif
368     while (1)
369     {
370         fd_set s_input;
371         struct timeval t;
372         int r, eof_flag = 0;
373     
374         t.tv_sec = 10;
375         t.tv_usec = 0;
376         FD_ZERO(&s_input);
377         FD_SET(linein, &s_input);
378 #  if 0
379         FD_SET(1, &s_input);
380 #  endif
381         gw_log (GW_LOG_DEBUG, prog, "select");
382         r = select (linein + 1, &s_input, NULL, NULL, &t);
383         if (r < 0)
384         {
385             gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog, "select");
386             exit(1);
387         }
388         if (r == 0 || FD_ISSET (linein, &s_input))
389         {
390             if (r == 0)
391                 gw_log (GW_LOG_DEBUG, prog, "poll");
392             if ((data = read (linein, combuf, COMBUF)) > 0)
393             {
394                 if (combuf[data-1] == '\0')
395                 {
396                     --data;
397                     eof_flag = 1;
398                 }
399                 gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
400                 if (data > 0 && write(1, combuf, data) < data)
401                 {
402                     gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
403                     exit (1);
404                 }
405             }
406             else if (data == -1)
407             {
408                 if (r > 0)
409                 {
410                     gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
411                     exit (1);
412                 }
413                 gw_log (GW_LOG_DEBUG, prog, "poll read");
414             }
415             else
416                 break;
417         }
418         if (eof_flag)
419             break;
420         if (r > 0 && FD_ISSET (1, &s_input))
421         {
422             data = read (1, combuf, COMBUF);
423             if (data == -1)
424             {
425                 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "stdout closed");
426                 break;
427             }
428             if (data == 0)
429             {
430                 gw_log (GW_LOG_DEBUG, prog, "stdout closed");
431                 break;
432             }
433             gw_log (GW_LOG_DEBUG, prog, "stdout got %d bytes", data);
434         }
435     }
436 #endif
437
438 #if 1
439     gw_log (GW_LOG_DEBUG, prog, "writing ack");
440     if (write(lineout, "OK", 2) < 2)
441         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
442 #endif
443     gw_log (GW_LOG_DEBUG, prog, "Cleaning up.");
444     close(linein);
445     unlink(serverp);
446     close(lineout);
447     return 0;
448 }