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