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