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