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.12 1996/01/09 10:46:49 adam
45 * New defines: LOGDIR/EGWDIR/CGIDIR set in Makefile.
47 * Revision 1.11 1996/01/08 08:42:19 adam
50 * Revision 1.10 1996/01/05 16:21:20 adam
51 * Bug fix: shell (wproto) sometimes closed server FIFO before cgi
52 * program opened it - solution: cgi sends OK when response has been read.
54 * Revision 1.9 1995/12/20 16:31:33 adam
55 * Bug fix: shell might terminate even though new request was initiated
56 * by the cgi interface program.
57 * Work on more simple user interface and Europagate buttons.
59 * Revision 1.8 1995/11/08 16:14:35 adam
60 * Many improvements and bug fixes.
61 * First version that ran on dtbsun.
63 * Revision 1.7 1995/11/08 12:42:18 adam
64 * Added descriptive text field in target info.
65 * Added authentication field in target info.
67 * Revision 1.6 1995/11/06 17:44:22 adam
68 * State reestablised when shell restarts. History of previous
71 * Revision 1.5 1995/11/06 10:51:15 adam
72 * End of response marker in response from wsh/wproto to wcgi.
73 * Shells are respawned when necessary.
75 * Revision 1.4 1995/11/02 16:35:37 adam
76 * Bug fixes and select on FIFOs in wcgi - doesn't really work!
78 * Revision 1.3 1995/10/31 16:56:24 adam
79 * Record presentation.
81 * Revision 1.2 1995/10/23 16:55:36 adam
82 * A lot of changes - really.
84 * Revision 1.1 1995/10/20 11:49:25 adam
85 * First version of www gateway.
95 #include <sys/types.h>
97 #include <sys/select.h>
100 #define DEADSTRING "Your database server has terminated. To reactivate \
101 the server, please reload the server's 'front page'."
106 static char *prog = "cgi";
108 static char serverp[256] = {'\0'};
109 static GW_DB gw_db = NULL;
111 static void fatal(char *p)
113 printf("Content-type: text/html\n\n<HTML><HEAD><TITLE>Server Failure</TITLE></HEAD>\n");
114 printf("<BODY>%s</BODY>\n", p);
122 static int spawn (char *sprog, int id)
128 sprintf (envstr, "GWID=%d", id);
130 sprintf(path, "%s/%s", CGIDIR, sprog);
134 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "fork");
140 gw_log (GW_LOG_DEBUG, prog, "execl %s", path);
141 execl (path, sprog, 0);
142 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "execl %s", path);
150 static void print_environ (void)
152 extern char **environ;
155 for (i = 0; environ[i]; i++)
156 gw_log (GW_LOG_DEBUG, prog, "e: %s", environ[i]);
161 * NOTE: In the (perhaps odd) terminology used within this software,
162 * the 'server' is the present program, which is executed by the httpd
163 * server. The 'client' is the process running outside.
164 * Protocol is long(len)<serverfifo>\0<extrapath>\0<envvars>\0<INFO>\0
168 char clientp[256], tmp[256], *path_info, *p, *operation, *t;
170 int linein = -1, lineout, data, gw_id;
172 chdir ("/usr/local/etc/httpd/cgi-bin");
174 gw_log_file (GW_LOG_ALL, LOGDIR "/egwcgi_log");
175 gw_log_level (GW_LOG_ALL);
176 gw_log (GW_LOG_STAT, prog, "Europagate www cgi server");
178 sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR);
179 if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0)
181 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp);
182 fatal("Internal error in server.");
184 sprintf(serverp, "%s/srv%d", tmp, getpid());
185 if (access(serverp, R_OK|W_OK) == 0)
187 if (unlink(serverp) < 0)
189 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog,
190 "Failed to delete stale fifo.");
191 fatal("Internal error in server.");
194 gw_log (GW_LOG_WARN, prog, "Removed stale server fifo.");
196 if (mkfifo(serverp, 0666 | S_IFIFO) < 0)
198 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "mkfifo(%s)", serverp);
199 fatal("Internal error in server.");
201 if (!(path_info = getenv("PATH_INFO")))
203 gw_log (GW_LOG_FATAL, prog, "Must set PATH_INFO.");
204 fatal("Internal error in server.");
206 operation = ++path_info;
207 while (*path_info && *path_info != '/')
210 *(path_info++) = '\0';
211 if (!(gw_db = gw_db_open (EGWDIR "/www.db", 1, 1)))
213 gw_log (GW_LOG_FATAL, prog, "gw_db_open");
216 if ((gw_id = atoi(operation)) <= 0)
221 gw_id = gw_db_seq_no (gw_db);
222 sprintf (gw_id_str, "%d", gw_id);
224 spawn(operation, gw_id);
225 r = gw_db_insert (gw_db, gw_id_str, strlen(gw_id_str)+1,
226 operation, strlen(operation)+1);
229 gw_log (GW_LOG_FATAL, prog, "gw_db_insert: %d", r);
233 gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
234 if ((linein = open(serverp, O_RDONLY)) < 0)
236 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
237 fatal("Internal error in server.");
239 if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
241 gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client");
242 fatal("Internal error in server");
244 gw_log (GW_LOG_DEBUG, prog, "Synchronized");
246 sprintf(clientp, "%s/clt%d", tmp, gw_id);
247 gw_log (GW_LOG_DEBUG, prog, "Opening %s", clientp);
248 if ((lineout = open(clientp, O_WRONLY)) < 0)
255 sprintf (gw_id_str, "%d", gw_id);
256 r = gw_db_lookup (gw_db, gw_id_str, strlen(gw_id_str)+1,
257 &sprog, &sprog_size);
260 gw_log (GW_LOG_FATAL, prog, "gw_db_lookup %s", gw_id_str);
261 fatal("Internal error in server");
263 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "open %s restart", clientp);
264 spawn (sprog, gw_id);
265 gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
266 if ((linein = open(serverp, O_RDONLY)) < 0)
268 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
269 fatal("Internal error in server");
271 if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
273 gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client.");
274 fatal("Internal error in server");
276 gw_log (GW_LOG_DEBUG, prog, "Synchronized.");
277 if ((lineout = open(clientp, O_WRONLY)) < 0)
279 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp);
280 fatal("Internal error in server");
284 gw_log (GW_LOG_DEBUG, prog, "Decoding user data");
285 p = combuf + sizeof(data);
288 strcpy(p, path_info);
289 gw_log (GW_LOG_DEBUG, prog, "P:%s", p);
291 *(p++) = '\0'; /* no envvars tranferred at present */
292 if ((t = getenv("CONTENT_LENGTH")) && (data = atoi(t)) > 0)
297 j = read(0, p + i, data - i);
300 gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog,
301 "Failed to read input");
302 fatal("Internal error in server");
306 gw_log (GW_LOG_ERRNO, prog, "Failed to read input");
307 fatal("Internal error in server");
312 else if ((t = getenv("QUERY_STRING")))
318 gw_log (GW_LOG_DEBUG, prog, "C:%s", p);
321 memcpy(combuf, &data, sizeof(data));
322 gw_log (GW_LOG_DEBUG, prog, "Writing data");
323 if (write(lineout, combuf, data) < data)
325 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
326 fatal("Internal server error");
330 gw_log (GW_LOG_DEBUG, prog, "open %s", serverp);
331 if ((linein = open(serverp, O_RDONLY)) < 0)
333 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
334 fatal("Internal error in server");
337 gw_log (GW_LOG_DEBUG, prog, "Reading response");
340 while ((data = read(linein, combuf, COMBUF)) > 0)
342 gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
343 if (combuf[data-1] == '\0')
345 if (write(1, combuf, data) < data)
347 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
353 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
359 if (write(1, combuf, data) < data)
361 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
367 fcntl (linein, F_SETFL, O_NONBLOCK);
378 FD_SET(linein, &s_input);
382 gw_log (GW_LOG_DEBUG, prog, "select");
383 r = select (linein + 1, &s_input, NULL, NULL, &t);
386 gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog, "select");
389 if (r == 0 || FD_ISSET (linein, &s_input))
392 gw_log (GW_LOG_DEBUG, prog, "poll");
393 if ((data = read (linein, combuf, COMBUF)) > 0)
395 if (combuf[data-1] == '\0')
400 gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
401 if (data > 0 && write(1, combuf, data) < data)
403 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
411 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
414 gw_log (GW_LOG_DEBUG, prog, "poll read");
421 if (r > 0 && FD_ISSET (1, &s_input))
423 data = read (1, combuf, COMBUF);
426 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "stdout closed");
431 gw_log (GW_LOG_DEBUG, prog, "stdout closed");
434 gw_log (GW_LOG_DEBUG, prog, "stdout got %d bytes", data);
440 gw_log (GW_LOG_DEBUG, prog, "writing ack");
441 if (write(lineout, "OK", 2) < 2)
442 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
444 gw_log (GW_LOG_DEBUG, prog, "Cleaning up.");