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