Bug fix: shell (wproto) sometimes closed server FIFO before cgi
[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.10  1996/01/05 16:21:20  adam
45  * Bug fix: shell (wproto) sometimes closed server FIFO before cgi
46  * program opened it - solution: cgi sends OK when response has been read.
47  *
48  * Revision 1.9  1995/12/20  16:31:33  adam
49  * Bug fix: shell might terminate even though new request was initiated
50  * by the cgi interface program.
51  * Work on more simple user interface and Europagate buttons.
52  *
53  * Revision 1.8  1995/11/08  16:14:35  adam
54  * Many improvements and bug fixes.
55  * First version that ran on dtbsun.
56  *
57  * Revision 1.7  1995/11/08  12:42:18  adam
58  * Added descriptive text field in target info.
59  * Added authentication field in target info.
60  *
61  * Revision 1.6  1995/11/06  17:44:22  adam
62  * State reestablised when shell restarts. History of previous
63  * result sets.
64  *
65  * Revision 1.5  1995/11/06  10:51:15  adam
66  * End of response marker in response from wsh/wproto to wcgi.
67  * Shells are respawned when necessary.
68  *
69  * Revision 1.4  1995/11/02  16:35:37  adam
70  * Bug fixes and select on FIFOs in wcgi - doesn't really work!
71  *
72  * Revision 1.3  1995/10/31  16:56:24  adam
73  * Record presentation.
74  *
75  * Revision 1.2  1995/10/23  16:55:36  adam
76  * A lot of changes - really.
77  *
78  * Revision 1.1  1995/10/20  11:49:25  adam
79  * First version of www gateway.
80  *
81  */
82
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <unistd.h>
86 #include <fcntl.h>
87 #include <sys/stat.h>
88 #include <sys/time.h>
89 #include <sys/types.h>
90 #ifdef AIX
91 #include <sys/select.h>
92 #endif
93
94 #define DEADSTRING "Your database server has terminated. To reactivate \
95 the server, please reload the server's 'front page'."
96
97 #include <gw-db.h>
98 #include "wproto.h"
99
100 #define CGIDIR "/usr/local/etc/httpd/cgi-bin"
101
102 static char *prog = "cgi";
103
104 static char serverp[256] = {'\0'};
105 static GW_DB gw_db = NULL;
106
107 static void fatal(char *p)
108 {
109     printf("Content-type: text/html\n\n<HTML><HEAD><TITLE>Server Failure</TITLE></HEAD>\n");
110     printf("<BODY>%s</BODY>\n", p);
111     if (gw_db)
112         gw_db_close (gw_db);
113     if (*serverp)
114         unlink(serverp);
115     exit(0);
116 }
117
118 static int spawn (char *sprog, int id)
119 {
120     int r;
121     char path[256];
122     char envstr[80];
123
124     sprintf (envstr, "GWID=%d", id);
125     putenv (envstr);
126     sprintf(path, "%s/%s", CGIDIR, sprog);
127     switch(r = fork())
128     {
129         case -1: 
130             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "fork"); 
131             exit(1);
132         case 0: 
133             close (0);
134             close (1);
135             close (2);
136             gw_log (GW_LOG_DEBUG, prog, "execl %s", path);
137             execl (path, sprog, 0); 
138             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "execl %s", path);
139             exit(0);
140         default: 
141             return r;
142     }
143 }
144
145 /*
146  * NOTE: In the (perhaps odd) terminology used within this software,
147  * the 'server' is the present program, which is executed by the httpd
148  * server. The 'client' is the process running outside.
149  * Protocol is long(len)<serverfifo>\0<extrapath>\0<envvars>\0<INFO>\0
150  */
151 int main()
152 {
153     char clientp[256], tmp[256], *path_info, *p, *operation, *t;
154     char combuf[COMBUF];
155     int linein = -1, lineout, data, gw_id;
156
157     chdir ("/usr/local/etc/httpd/cgi-bin");
158     gw_log_init ("egw");
159     gw_log_file (GW_LOG_ALL, "/usr/local/etc/httpd/logs/egwcgi_log");
160     gw_log_level (GW_LOG_ALL);
161     gw_log (GW_LOG_STAT, prog, "Europagate www cgi server");
162
163     sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR);
164     if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0)
165     {
166         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp);
167         fatal("Internal error in server.");
168     }
169     sprintf(serverp, "%s/srv%d", tmp, getpid());
170     if (access(serverp, R_OK|W_OK) == 0)
171     {
172         if (unlink(serverp) < 0)
173         {
174             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog,
175                     "Failed to delete stale fifo.");
176             fatal("Internal error in server.");
177         }
178         else
179             gw_log (GW_LOG_WARN, prog, "Removed stale server fifo.");
180     }
181     if (mkfifo(serverp, 0666 | S_IFIFO) < 0)
182     {
183         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "mkfifo(%s)", serverp);
184         fatal("Internal error in server.");
185     }
186     if (!(path_info = getenv("PATH_INFO")))
187     {
188         gw_log (GW_LOG_FATAL, prog, "Must set PATH_INFO.");
189         fatal("Internal error in server.");
190     }
191     operation = ++path_info;
192     while (*path_info && *path_info != '/')
193         path_info++;
194     if (*path_info)
195         *(path_info++) = '\0';
196     if (!(gw_db = gw_db_open ("www.db", 1, 1)))
197     {
198         gw_log (GW_LOG_FATAL, prog, "gw_db_open");
199         exit (1);
200     }
201     if ((gw_id = atoi(operation)) <= 0)
202     {
203         int r;
204         char gw_id_str[16];
205
206         gw_id = gw_db_seq_no (gw_db);
207         sprintf (gw_id_str, "%d", gw_id);
208         
209         spawn(operation, gw_id);
210         r = gw_db_insert (gw_db, gw_id_str, strlen(gw_id_str)+1,
211                           operation, strlen(operation)+1);
212         if (r)
213         {
214             gw_log (GW_LOG_FATAL, prog, "gw_db_insert: %d", r);
215             gw_db_close (gw_db);
216             exit (1);
217         }
218         gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
219         if ((linein = open(serverp, O_RDONLY)) < 0)
220         {
221             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
222             fatal("Internal error in server.");
223         }
224         if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
225         {
226             gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client");
227             fatal("Internal error in server");
228         }
229         gw_log (GW_LOG_DEBUG, prog, "Synchronized");
230     }
231     sprintf(clientp, "%s/clt%d", tmp, gw_id);
232     gw_log (GW_LOG_DEBUG, prog, "Opening %s", clientp);
233     if ((lineout = open(clientp, O_WRONLY)) < 0)
234     {
235         char gw_id_str[16];
236         void *sprog;
237         size_t sprog_size;
238         int r;
239
240         sprintf (gw_id_str, "%d", gw_id);
241         r = gw_db_lookup (gw_db, gw_id_str, strlen(gw_id_str)+1,  
242                           &sprog, &sprog_size);
243         if (r != 1)
244         {
245             gw_log (GW_LOG_FATAL, prog, "gw_db_lookup %s", gw_id_str);
246             fatal("Internal error in server");
247         }
248         gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "open %s restart", clientp);
249         spawn (sprog, gw_id);
250         gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
251         if ((linein = open(serverp, O_RDONLY)) < 0)
252         {
253             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
254             fatal("Internal error in server");
255         }
256         if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
257         {
258             gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client.");
259             fatal("Internal error in server");
260         }
261         gw_log (GW_LOG_DEBUG, prog, "Synchronized.");
262         if ((lineout = open(clientp, O_WRONLY)) < 0)
263         {
264             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp);
265             fatal("Internal error in server");
266         }
267     }
268     gw_db_close (gw_db);
269     gw_log (GW_LOG_DEBUG, prog, "Decoding user data");
270     p = combuf + sizeof(data);
271     strcpy(p, serverp);
272     p += strlen(p) + 1;
273     strcpy(p, path_info);
274     p += strlen(p) + 1;
275     *(p++) = '\0';               /* no envvars tranferred at present */
276     if ((t = getenv("CONTENT_LENGTH")) && (data = atoi(t)) > 0)
277     {
278         int j, i = 0;
279         while (i < data)
280         {
281             j = read(0, p + i, data - i);
282             if (j == -1)
283             {
284                 gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog,
285                         "Failed to read input");
286                 fatal("Internal error in server");
287             }
288             else if (j == 0)
289             {
290                 gw_log (GW_LOG_ERRNO, prog, "Failed to read input");
291                 fatal("Internal error in server");
292             }
293             i += j;
294         }
295     }
296     p += data;
297     *(p++) = '\0';
298     data = (p - combuf);
299     memcpy(combuf, &data, sizeof(data));
300     gw_log (GW_LOG_DEBUG, prog, "Writing data");
301     if (write(lineout, combuf, data) < data)
302     {
303         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
304         fatal("Internal server error");
305     }
306     if (linein < 0)
307     {
308         gw_log (GW_LOG_DEBUG, prog, "open %s", serverp);
309         if ((linein = open(serverp, O_RDONLY)) < 0)
310         {
311             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
312             fatal("Internal error in server");
313         }
314     }
315     gw_log (GW_LOG_DEBUG, prog, "Reading response");
316
317 #if 1
318     while ((data = read(linein, combuf, COMBUF)) > 0)
319     {
320         gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
321         if (combuf[data-1] == '\0')
322             break;
323         if (write(1, combuf, data) < data)
324         {
325             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
326             exit (1);
327         }
328     }
329     if (data < 0)
330     {
331         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
332         exit (1);
333     }
334     if (data > 0)
335     {
336         --data;
337         if (write(1, combuf, data) < data)
338         {
339             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
340             exit (1);
341         }
342     }
343 #else
344 #  if 1
345     fcntl (linein, F_SETFL, O_NONBLOCK);
346 #  endif
347     while (1)
348     {
349         fd_set s_input;
350         struct timeval t;
351         int r, eof_flag = 0;
352     
353         t.tv_sec = 10;
354         t.tv_usec = 0;
355         FD_ZERO(&s_input);
356         FD_SET(linein, &s_input);
357 #  if 0
358         FD_SET(1, &s_input);
359 #  endif
360         gw_log (GW_LOG_DEBUG, prog, "select");
361         r = select (linein + 1, &s_input, NULL, NULL, &t);
362         if (r < 0)
363         {
364             gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog, "select");
365             exit(1);
366         }
367         if (r == 0 || FD_ISSET (linein, &s_input))
368         {
369             if (r == 0)
370                 gw_log (GW_LOG_DEBUG, prog, "poll");
371             if ((data = read (linein, combuf, COMBUF)) > 0)
372             {
373                 if (combuf[data-1] == '\0')
374                 {
375                     --data;
376                     eof_flag = 1;
377                 }
378                 gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
379                 if (data > 0 && write(1, combuf, data) < data)
380                 {
381                     gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
382                     exit (1);
383                 }
384             }
385             else if (data == -1)
386             {
387                 if (r > 0)
388                 {
389                     gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
390                     exit (1);
391                 }
392                 gw_log (GW_LOG_DEBUG, prog, "poll read");
393             }
394             else
395                 break;
396         }
397         if (eof_flag)
398             break;
399         if (r > 0 && FD_ISSET (1, &s_input))
400         {
401             data = read (1, combuf, COMBUF);
402             if (data == -1)
403             {
404                 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "stdout closed");
405                 break;
406             }
407             if (data == 0)
408             {
409                 gw_log (GW_LOG_DEBUG, prog, "stdout closed");
410                 break;
411             }
412             gw_log (GW_LOG_DEBUG, prog, "stdout got %d bytes", data);
413         }
414     }
415 #endif
416
417 #if 1
418     gw_log (GW_LOG_DEBUG, prog, "writing ack");
419     if (write(lineout, "OK", 2) < 2)
420         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
421 #endif
422     gw_log (GW_LOG_DEBUG, prog, "Cleaning up.");
423     close(linein);
424     unlink(serverp);
425     close(lineout);
426     return 0;
427 }