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