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