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.11 1996/01/08 08:42:19 adam
47 * Revision 1.10 1996/01/05 16:21:20 adam
48 * Bug fix: shell (wproto) sometimes closed server FIFO before cgi
49 * program opened it - solution: cgi sends OK when response has been read.
51 * Revision 1.9 1995/12/20 16:31:33 adam
52 * Bug fix: shell might terminate even though new request was initiated
53 * by the cgi interface program.
54 * Work on more simple user interface and Europagate buttons.
56 * Revision 1.8 1995/11/08 16:14:35 adam
57 * Many improvements and bug fixes.
58 * First version that ran on dtbsun.
60 * Revision 1.7 1995/11/08 12:42:18 adam
61 * Added descriptive text field in target info.
62 * Added authentication field in target info.
64 * Revision 1.6 1995/11/06 17:44:22 adam
65 * State reestablised when shell restarts. History of previous
68 * Revision 1.5 1995/11/06 10:51:15 adam
69 * End of response marker in response from wsh/wproto to wcgi.
70 * Shells are respawned when necessary.
72 * Revision 1.4 1995/11/02 16:35:37 adam
73 * Bug fixes and select on FIFOs in wcgi - doesn't really work!
75 * Revision 1.3 1995/10/31 16:56:24 adam
76 * Record presentation.
78 * Revision 1.2 1995/10/23 16:55:36 adam
79 * A lot of changes - really.
81 * Revision 1.1 1995/10/20 11:49:25 adam
82 * First version of www gateway.
92 #include <sys/types.h>
94 #include <sys/select.h>
97 #define DEADSTRING "Your database server has terminated. To reactivate \
98 the server, please reload the server's 'front page'."
103 #define CGIDIR "/usr/local/etc/httpd/cgi-bin"
105 static char *prog = "cgi";
107 static char serverp[256] = {'\0'};
108 static GW_DB gw_db = NULL;
110 static void fatal(char *p)
112 printf("Content-type: text/html\n\n<HTML><HEAD><TITLE>Server Failure</TITLE></HEAD>\n");
113 printf("<BODY>%s</BODY>\n", p);
121 static int spawn (char *sprog, int id)
127 sprintf (envstr, "GWID=%d", id);
129 sprintf(path, "%s/%s", CGIDIR, sprog);
133 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "fork");
139 gw_log (GW_LOG_DEBUG, prog, "execl %s", path);
140 execl (path, sprog, 0);
141 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "execl %s", path);
149 static void print_environ (void)
151 extern char **environ;
154 for (i = 0; environ[i]; i++)
155 gw_log (GW_LOG_DEBUG, prog, "e: %s", environ[i]);
160 * NOTE: In the (perhaps odd) terminology used within this software,
161 * the 'server' is the present program, which is executed by the httpd
162 * server. The 'client' is the process running outside.
163 * Protocol is long(len)<serverfifo>\0<extrapath>\0<envvars>\0<INFO>\0
167 char clientp[256], tmp[256], *path_info, *p, *operation, *t;
169 int linein = -1, lineout, data, gw_id;
171 chdir ("/usr/local/etc/httpd/cgi-bin");
173 gw_log_file (GW_LOG_ALL, "/usr/local/etc/httpd/logs/egwcgi_log");
174 gw_log_level (GW_LOG_ALL);
175 gw_log (GW_LOG_STAT, prog, "Europagate www cgi server");
177 sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR);
178 if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0)
180 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp);
181 fatal("Internal error in server.");
183 sprintf(serverp, "%s/srv%d", tmp, getpid());
184 if (access(serverp, R_OK|W_OK) == 0)
186 if (unlink(serverp) < 0)
188 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog,
189 "Failed to delete stale fifo.");
190 fatal("Internal error in server.");
193 gw_log (GW_LOG_WARN, prog, "Removed stale server fifo.");
195 if (mkfifo(serverp, 0666 | S_IFIFO) < 0)
197 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "mkfifo(%s)", serverp);
198 fatal("Internal error in server.");
200 if (!(path_info = getenv("PATH_INFO")))
202 gw_log (GW_LOG_FATAL, prog, "Must set PATH_INFO.");
203 fatal("Internal error in server.");
205 operation = ++path_info;
206 while (*path_info && *path_info != '/')
209 *(path_info++) = '\0';
210 if (!(gw_db = gw_db_open ("www.db", 1, 1)))
212 gw_log (GW_LOG_FATAL, prog, "gw_db_open");
215 if ((gw_id = atoi(operation)) <= 0)
220 gw_id = gw_db_seq_no (gw_db);
221 sprintf (gw_id_str, "%d", gw_id);
223 spawn(operation, gw_id);
224 r = gw_db_insert (gw_db, gw_id_str, strlen(gw_id_str)+1,
225 operation, strlen(operation)+1);
228 gw_log (GW_LOG_FATAL, prog, "gw_db_insert: %d", r);
232 gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
233 if ((linein = open(serverp, O_RDONLY)) < 0)
235 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
236 fatal("Internal error in server.");
238 if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
240 gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client");
241 fatal("Internal error in server");
243 gw_log (GW_LOG_DEBUG, prog, "Synchronized");
245 sprintf(clientp, "%s/clt%d", tmp, gw_id);
246 gw_log (GW_LOG_DEBUG, prog, "Opening %s", clientp);
247 if ((lineout = open(clientp, O_WRONLY)) < 0)
254 sprintf (gw_id_str, "%d", gw_id);
255 r = gw_db_lookup (gw_db, gw_id_str, strlen(gw_id_str)+1,
256 &sprog, &sprog_size);
259 gw_log (GW_LOG_FATAL, prog, "gw_db_lookup %s", gw_id_str);
260 fatal("Internal error in server");
262 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "open %s restart", clientp);
263 spawn (sprog, gw_id);
264 gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
265 if ((linein = open(serverp, O_RDONLY)) < 0)
267 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
268 fatal("Internal error in server");
270 if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
272 gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client.");
273 fatal("Internal error in server");
275 gw_log (GW_LOG_DEBUG, prog, "Synchronized.");
276 if ((lineout = open(clientp, O_WRONLY)) < 0)
278 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp);
279 fatal("Internal error in server");
283 gw_log (GW_LOG_DEBUG, prog, "Decoding user data");
284 p = combuf + sizeof(data);
287 strcpy(p, path_info);
288 gw_log (GW_LOG_DEBUG, prog, "P:%s", p);
290 *(p++) = '\0'; /* no envvars tranferred at present */
291 if ((t = getenv("CONTENT_LENGTH")) && (data = atoi(t)) > 0)
296 j = read(0, p + i, data - i);
299 gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog,
300 "Failed to read input");
301 fatal("Internal error in server");
305 gw_log (GW_LOG_ERRNO, prog, "Failed to read input");
306 fatal("Internal error in server");
311 else if ((t = getenv("QUERY_STRING")))
317 gw_log (GW_LOG_DEBUG, prog, "C:%s", p);
320 memcpy(combuf, &data, sizeof(data));
321 gw_log (GW_LOG_DEBUG, prog, "Writing data");
322 if (write(lineout, combuf, data) < data)
324 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
325 fatal("Internal server error");
329 gw_log (GW_LOG_DEBUG, prog, "open %s", serverp);
330 if ((linein = open(serverp, O_RDONLY)) < 0)
332 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
333 fatal("Internal error in server");
336 gw_log (GW_LOG_DEBUG, prog, "Reading response");
339 while ((data = read(linein, combuf, COMBUF)) > 0)
341 gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
342 if (combuf[data-1] == '\0')
344 if (write(1, combuf, data) < data)
346 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
352 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
358 if (write(1, combuf, data) < data)
360 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
366 fcntl (linein, F_SETFL, O_NONBLOCK);
377 FD_SET(linein, &s_input);
381 gw_log (GW_LOG_DEBUG, prog, "select");
382 r = select (linein + 1, &s_input, NULL, NULL, &t);
385 gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog, "select");
388 if (r == 0 || FD_ISSET (linein, &s_input))
391 gw_log (GW_LOG_DEBUG, prog, "poll");
392 if ((data = read (linein, combuf, COMBUF)) > 0)
394 if (combuf[data-1] == '\0')
399 gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
400 if (data > 0 && write(1, combuf, data) < data)
402 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
410 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
413 gw_log (GW_LOG_DEBUG, prog, "poll read");
420 if (r > 0 && FD_ISSET (1, &s_input))
422 data = read (1, combuf, COMBUF);
425 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "stdout closed");
430 gw_log (GW_LOG_DEBUG, prog, "stdout closed");
433 gw_log (GW_LOG_DEBUG, prog, "stdout got %d bytes", data);
439 gw_log (GW_LOG_DEBUG, prog, "writing ack");
440 if (write(lineout, "OK", 2) < 2)
441 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
443 gw_log (GW_LOG_DEBUG, prog, "Cleaning up.");