2 * Copyright (c) 1995, the EUROPAGATE consortium (see below).
4 * The EUROPAGATE consortium members are:
6 * University College Dublin
7 * Danmarks Teknologiske Videnscenter
8 * An Chomhairle Leabharlanna
9 * Consejo Superior de Investigaciones Cientificas
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,
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.
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.
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.
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.
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.
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.
49 * Revision 1.8 1995/11/08 16:14:35 adam
50 * Many improvements and bug fixes.
51 * First version that ran on dtbsun.
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.
57 * Revision 1.6 1995/11/06 17:44:22 adam
58 * State reestablised when shell restarts. History of previous
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.
65 * Revision 1.4 1995/11/02 16:35:37 adam
66 * Bug fixes and select on FIFOs in wcgi - doesn't really work!
68 * Revision 1.3 1995/10/31 16:56:24 adam
69 * Record presentation.
71 * Revision 1.2 1995/10/23 16:55:36 adam
72 * A lot of changes - really.
74 * Revision 1.1 1995/10/20 11:49:25 adam
75 * First version of www gateway.
85 #include <sys/types.h>
87 #include <sys/select.h>
90 #define DEADSTRING "Your database server has terminated. To reactivate \
91 the server, please reload the server's 'front page'."
96 #define CGIDIR "/usr/local/etc/httpd/cgi-bin"
98 static char *prog = "cgi";
100 static char serverp[256] = {'\0'};
101 static GW_DB gw_db = NULL;
103 static void fatal(char *p)
105 printf("Content-type: text/html\n\n<HTML><HEAD><TITLE>Server Failure</TITLE></HEAD>\n");
106 printf("<BODY>%s</BODY>\n", p);
114 static int spawn (char *sprog, int id)
120 sprintf (envstr, "GWID=%d", id);
122 sprintf(path, "%s/%s", CGIDIR, sprog);
126 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "fork");
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);
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
149 char clientp[256], tmp[256], *path_info, *p, *operation, *t;
151 int linein = -1, lineout, data, gw_id;
153 chdir ("/usr/local/etc/httpd/cgi-bin");
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");
159 sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR);
160 if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0)
162 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp);
163 fatal("Internal error in server.");
165 sprintf(serverp, "%s/srv%d", tmp, getpid());
166 if (access(serverp, R_OK|W_OK) == 0)
168 if (unlink(serverp) < 0)
170 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog,
171 "Failed to delete stale fifo.");
172 fatal("Internal error in server.");
175 gw_log (GW_LOG_WARN, prog, "Removed stale server fifo.");
177 if (mkfifo(serverp, 0666 | S_IFIFO) < 0)
179 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "mkfifo(%s)", serverp);
180 fatal("Internal error in server.");
182 if (!(path_info = getenv("PATH_INFO")))
184 gw_log (GW_LOG_FATAL, prog, "Must set PATH_INFO.");
185 fatal("Internal error in server.");
187 operation = ++path_info;
188 while (*path_info && *path_info != '/')
191 *(path_info++) = '\0';
192 if (!(gw_db = gw_db_open ("www.db", 1, 1)))
194 gw_log (GW_LOG_FATAL, prog, "gw_db_open");
197 if ((gw_id = atoi(operation)) <= 0)
202 gw_id = gw_db_seq_no (gw_db);
203 sprintf (gw_id_str, "%d", gw_id);
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);
210 gw_log (GW_LOG_FATAL, prog, "gw_db_insert: %d", r);
214 gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
215 if ((linein = open(serverp, O_RDONLY)) < 0)
217 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
218 fatal("Internal error in server.");
220 if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
222 gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client");
223 fatal("Internal error in server");
225 gw_log (GW_LOG_DEBUG, prog, "Synchronized");
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)
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);
241 gw_log (GW_LOG_FATAL, prog, "gw_db_lookup %s", gw_id_str);
242 fatal("Internal error in server");
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)
249 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
250 fatal("Internal error in server");
252 if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
254 gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client.");
255 fatal("Internal error in server");
257 gw_log (GW_LOG_DEBUG, prog, "Synchronized.");
258 if ((lineout = open(clientp, O_WRONLY)) < 0)
260 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp);
261 fatal("Internal error in server");
265 gw_log (GW_LOG_DEBUG, prog, "Decoding user data");
266 p = combuf + sizeof(data);
269 strcpy(p, path_info);
271 *(p++) = '\0'; /* no envvars tranferred at present */
272 if ((t = getenv("CONTENT_LENGTH")) && (data = atoi(t)) > 0)
277 j = read(0, p + i, data - i);
280 gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog,
281 "Failed to read input");
282 fatal("Internal error in server");
286 gw_log (GW_LOG_ERRNO, prog, "Failed to read input");
287 fatal("Internal error in server");
295 memcpy(combuf, &data, sizeof(data));
296 gw_log (GW_LOG_DEBUG, prog, "Writing data");
297 if (write(lineout, combuf, data) < data)
299 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
300 fatal("Internal server error");
302 if (linein < 0 && (linein = open(serverp, O_RDONLY)) < 0)
304 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
305 fatal("Internal error in server");
307 gw_log (GW_LOG_DEBUG, prog, "Reading response");
310 while ((data = read(linein, combuf, COMBUF)) > 0)
312 gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
313 if (write(1, combuf, data) < data)
315 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
321 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
326 fcntl (linein, F_SETFL, O_NONBLOCK);
337 FD_SET(linein, &s_input);
341 gw_log (GW_LOG_DEBUG, prog, "select");
342 r = select (linein + 1, &s_input, NULL, NULL, &t);
345 gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog, "select");
348 if (r == 0 || FD_ISSET (linein, &s_input))
351 gw_log (GW_LOG_DEBUG, prog, "poll");
352 if ((data = read (linein, combuf, COMBUF)) > 0)
354 if (combuf[data-1] == '\0')
359 gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
360 if (data > 0 && write(1, combuf, data) < data)
362 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
370 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
373 gw_log (GW_LOG_DEBUG, prog, "poll read");
380 if (r > 0 && FD_ISSET (1, &s_input))
382 data = read (1, combuf, COMBUF);
385 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "stdout closed");
390 gw_log (GW_LOG_DEBUG, prog, "stdout closed");
393 gw_log (GW_LOG_DEBUG, prog, "stdout got %d bytes", data);
397 gw_log (GW_LOG_DEBUG, prog, "Cleaning up.");