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