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.13 1996/01/12 10:05:17 adam
45 * If script name ends with ';' HTTP/GET/Expires will be defined.
46 * The cgi interface only reads final handshake if response from
47 * server (shell) was zero-terminated [If it isn't it probably died].
49 * Revision 1.12 1996/01/09 10:46:49 adam
50 * New defines: LOGDIR/EGWDIR/CGIDIR set in Makefile.
52 * Revision 1.11 1996/01/08 08:42:19 adam
55 * Revision 1.10 1996/01/05 16:21:20 adam
56 * Bug fix: shell (wproto) sometimes closed server FIFO before cgi
57 * program opened it - solution: cgi sends OK when response has been read.
59 * Revision 1.9 1995/12/20 16:31:33 adam
60 * Bug fix: shell might terminate even though new request was initiated
61 * by the cgi interface program.
62 * Work on more simple user interface and Europagate buttons.
64 * Revision 1.8 1995/11/08 16:14:35 adam
65 * Many improvements and bug fixes.
66 * First version that ran on dtbsun.
68 * Revision 1.7 1995/11/08 12:42:18 adam
69 * Added descriptive text field in target info.
70 * Added authentication field in target info.
72 * Revision 1.6 1995/11/06 17:44:22 adam
73 * State reestablised when shell restarts. History of previous
76 * Revision 1.5 1995/11/06 10:51:15 adam
77 * End of response marker in response from wsh/wproto to wcgi.
78 * Shells are respawned when necessary.
80 * Revision 1.4 1995/11/02 16:35:37 adam
81 * Bug fixes and select on FIFOs in wcgi - doesn't really work!
83 * Revision 1.3 1995/10/31 16:56:24 adam
84 * Record presentation.
86 * Revision 1.2 1995/10/23 16:55:36 adam
87 * A lot of changes - really.
89 * Revision 1.1 1995/10/20 11:49:25 adam
90 * First version of www gateway.
100 #include <sys/types.h>
102 #include <sys/select.h>
105 #define DEADSTRING "Your database server has terminated. To reactivate \
106 the server, please reload the server's 'front page'."
111 static char *prog = "cgi";
113 static char serverp[256] = {'\0'};
114 static GW_DB gw_db = NULL;
116 static void fatal(char *p)
118 printf("Content-type: text/html\n\n<HTML><HEAD><TITLE>Server Failure</TITLE></HEAD>\n");
119 printf("<BODY>%s</BODY>\n", p);
127 static int spawn (char *sprog, int id)
133 sprintf (envstr, "GWID=%d", id);
135 sprintf(path, "%s/%s", CGIDIR, sprog);
139 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "fork");
145 gw_log (GW_LOG_DEBUG, prog, "execl %s", path);
146 execl (path, sprog, 0);
147 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "execl %s", path);
155 static void print_environ (void)
157 extern char **environ;
160 for (i = 0; environ[i]; i++)
161 gw_log (GW_LOG_DEBUG, prog, "e: %s", environ[i]);
166 * NOTE: In the (perhaps odd) terminology used within this software,
167 * the 'server' is the present program, which is executed by the httpd
168 * server. The 'client' is the process running outside.
169 * Protocol is long(len)<serverfifo>\0<extrapath>\0<envvars>\0<INFO>\0
173 char clientp[256], tmp[256], *path_info, *p, *operation, *t;
175 int linein = -1, lineout, data, gw_id;
177 chdir ("/usr/local/etc/httpd/cgi-bin");
179 gw_log_file (GW_LOG_ALL, LOGDIR "/egwcgi_log");
180 gw_log_level (GW_LOG_ALL);
181 gw_log (GW_LOG_STAT, prog, "Europagate www cgi server");
183 sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR);
184 if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0)
186 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp);
187 fatal("Internal error in server.");
189 sprintf(serverp, "%s/srv%d", tmp, getpid());
190 if (access(serverp, R_OK|W_OK) == 0)
192 if (unlink(serverp) < 0)
194 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog,
195 "Failed to delete stale fifo.");
196 fatal("Internal error in server.");
199 gw_log (GW_LOG_WARN, prog, "Removed stale server fifo.");
201 if (mkfifo(serverp, 0666 | S_IFIFO) < 0)
203 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "mkfifo(%s)", serverp);
204 fatal("Internal error in server.");
206 if (!(path_info = getenv("PATH_INFO")))
208 gw_log (GW_LOG_FATAL, prog, "Must set PATH_INFO.");
209 fatal("Internal error in server.");
211 operation = ++path_info;
212 while (*path_info && *path_info != '/')
215 *(path_info++) = '\0';
216 if (!(gw_db = gw_db_open (EGWDIR "/www.db", 1, 1)))
218 gw_log (GW_LOG_FATAL, prog, "gw_db_open");
221 if ((gw_id = atoi(operation)) <= 0)
226 gw_id = gw_db_seq_no (gw_db);
227 sprintf (gw_id_str, "%d", gw_id);
229 spawn(operation, gw_id);
230 r = gw_db_insert (gw_db, gw_id_str, strlen(gw_id_str)+1,
231 operation, strlen(operation)+1);
234 gw_log (GW_LOG_FATAL, prog, "gw_db_insert: %d", r);
238 gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
239 if ((linein = open(serverp, O_RDONLY)) < 0)
241 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
242 fatal("Internal error in server.");
244 if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
246 gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client");
247 fatal("Internal error in server");
249 gw_log (GW_LOG_DEBUG, prog, "Synchronized");
251 sprintf(clientp, "%s/clt%d", tmp, gw_id);
252 gw_log (GW_LOG_DEBUG, prog, "Opening %s", clientp);
253 if ((lineout = open(clientp, O_WRONLY)) < 0)
260 sprintf (gw_id_str, "%d", gw_id);
261 r = gw_db_lookup (gw_db, gw_id_str, strlen(gw_id_str)+1,
262 &sprog, &sprog_size);
265 gw_log (GW_LOG_FATAL, prog, "gw_db_lookup %s", gw_id_str);
266 fatal("Internal error in server");
268 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "open %s restart", clientp);
269 spawn (sprog, gw_id);
270 gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
271 if ((linein = open(serverp, O_RDONLY)) < 0)
273 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
274 fatal("Internal error in server");
276 if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
278 gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client.");
279 fatal("Internal error in server");
281 gw_log (GW_LOG_DEBUG, prog, "Synchronized.");
282 if ((lineout = open(clientp, O_WRONLY)) < 0)
284 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp);
285 fatal("Internal error in server");
289 gw_log (GW_LOG_DEBUG, prog, "Decoding user data");
290 p = combuf + sizeof(data);
293 strcpy(p, path_info);
294 gw_log (GW_LOG_DEBUG, prog, "P:%s", p);
296 *(p++) = '\0'; /* no envvars tranferred at present */
297 if ((t = getenv("CONTENT_LENGTH")) && (data = atoi(t)) > 0)
302 j = read(0, p + i, data - i);
305 gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog,
306 "Failed to read input");
307 fatal("Internal error in server");
311 gw_log (GW_LOG_ERRNO, prog, "Failed to read input");
312 fatal("Internal error in server");
317 else if ((t = getenv("QUERY_STRING")))
323 gw_log (GW_LOG_DEBUG, prog, "C:%s", p);
326 memcpy(combuf, &data, sizeof(data));
327 gw_log (GW_LOG_DEBUG, prog, "Writing data");
328 if (write(lineout, combuf, data) < data)
330 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
331 fatal("Internal server error");
335 gw_log (GW_LOG_DEBUG, prog, "open %s", serverp);
336 if ((linein = open(serverp, O_RDONLY)) < 0)
338 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
339 fatal("Internal error in server");
342 gw_log (GW_LOG_DEBUG, prog, "Reading response");
344 while ((data = read(linein, combuf, COMBUF)) > 0)
346 gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
347 if (combuf[data-1] == '\0')
349 if (write(1, combuf, data) < data)
351 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
357 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
363 if (write(1, combuf, data) < data)
365 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
368 gw_log (GW_LOG_DEBUG, prog, "writing ack");
369 if (write(lineout, "OK", 2) < 2)
370 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
372 gw_log (GW_LOG_DEBUG, prog, "Cleaning up.");