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.10 1996/01/05 16:21:20 adam
45 * Bug fix: shell (wproto) sometimes closed server FIFO before cgi
46 * program opened it - solution: cgi sends OK when response has been read.
48 * Revision 1.9 1995/12/20 16:31:33 adam
49 * Bug fix: shell might terminate even though new request was initiated
50 * by the cgi interface program.
51 * Work on more simple user interface and Europagate buttons.
53 * Revision 1.8 1995/11/08 16:14:35 adam
54 * Many improvements and bug fixes.
55 * First version that ran on dtbsun.
57 * Revision 1.7 1995/11/08 12:42:18 adam
58 * Added descriptive text field in target info.
59 * Added authentication field in target info.
61 * Revision 1.6 1995/11/06 17:44:22 adam
62 * State reestablised when shell restarts. History of previous
65 * Revision 1.5 1995/11/06 10:51:15 adam
66 * End of response marker in response from wsh/wproto to wcgi.
67 * Shells are respawned when necessary.
69 * Revision 1.4 1995/11/02 16:35:37 adam
70 * Bug fixes and select on FIFOs in wcgi - doesn't really work!
72 * Revision 1.3 1995/10/31 16:56:24 adam
73 * Record presentation.
75 * Revision 1.2 1995/10/23 16:55:36 adam
76 * A lot of changes - really.
78 * Revision 1.1 1995/10/20 11:49:25 adam
79 * First version of www gateway.
89 #include <sys/types.h>
91 #include <sys/select.h>
94 #define DEADSTRING "Your database server has terminated. To reactivate \
95 the server, please reload the server's 'front page'."
100 #define CGIDIR "/usr/local/etc/httpd/cgi-bin"
102 static char *prog = "cgi";
104 static char serverp[256] = {'\0'};
105 static GW_DB gw_db = NULL;
107 static void fatal(char *p)
109 printf("Content-type: text/html\n\n<HTML><HEAD><TITLE>Server Failure</TITLE></HEAD>\n");
110 printf("<BODY>%s</BODY>\n", p);
118 static int spawn (char *sprog, int id)
124 sprintf (envstr, "GWID=%d", id);
126 sprintf(path, "%s/%s", CGIDIR, sprog);
130 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "fork");
136 gw_log (GW_LOG_DEBUG, prog, "execl %s", path);
137 execl (path, sprog, 0);
138 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "execl %s", path);
146 * NOTE: In the (perhaps odd) terminology used within this software,
147 * the 'server' is the present program, which is executed by the httpd
148 * server. The 'client' is the process running outside.
149 * Protocol is long(len)<serverfifo>\0<extrapath>\0<envvars>\0<INFO>\0
153 char clientp[256], tmp[256], *path_info, *p, *operation, *t;
155 int linein = -1, lineout, data, gw_id;
157 chdir ("/usr/local/etc/httpd/cgi-bin");
159 gw_log_file (GW_LOG_ALL, "/usr/local/etc/httpd/logs/egwcgi_log");
160 gw_log_level (GW_LOG_ALL);
161 gw_log (GW_LOG_STAT, prog, "Europagate www cgi server");
163 sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR);
164 if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0)
166 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp);
167 fatal("Internal error in server.");
169 sprintf(serverp, "%s/srv%d", tmp, getpid());
170 if (access(serverp, R_OK|W_OK) == 0)
172 if (unlink(serverp) < 0)
174 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog,
175 "Failed to delete stale fifo.");
176 fatal("Internal error in server.");
179 gw_log (GW_LOG_WARN, prog, "Removed stale server fifo.");
181 if (mkfifo(serverp, 0666 | S_IFIFO) < 0)
183 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "mkfifo(%s)", serverp);
184 fatal("Internal error in server.");
186 if (!(path_info = getenv("PATH_INFO")))
188 gw_log (GW_LOG_FATAL, prog, "Must set PATH_INFO.");
189 fatal("Internal error in server.");
191 operation = ++path_info;
192 while (*path_info && *path_info != '/')
195 *(path_info++) = '\0';
196 if (!(gw_db = gw_db_open ("www.db", 1, 1)))
198 gw_log (GW_LOG_FATAL, prog, "gw_db_open");
201 if ((gw_id = atoi(operation)) <= 0)
206 gw_id = gw_db_seq_no (gw_db);
207 sprintf (gw_id_str, "%d", gw_id);
209 spawn(operation, gw_id);
210 r = gw_db_insert (gw_db, gw_id_str, strlen(gw_id_str)+1,
211 operation, strlen(operation)+1);
214 gw_log (GW_LOG_FATAL, prog, "gw_db_insert: %d", r);
218 gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
219 if ((linein = open(serverp, O_RDONLY)) < 0)
221 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
222 fatal("Internal error in server.");
224 if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
226 gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client");
227 fatal("Internal error in server");
229 gw_log (GW_LOG_DEBUG, prog, "Synchronized");
231 sprintf(clientp, "%s/clt%d", tmp, gw_id);
232 gw_log (GW_LOG_DEBUG, prog, "Opening %s", clientp);
233 if ((lineout = open(clientp, O_WRONLY)) < 0)
240 sprintf (gw_id_str, "%d", gw_id);
241 r = gw_db_lookup (gw_db, gw_id_str, strlen(gw_id_str)+1,
242 &sprog, &sprog_size);
245 gw_log (GW_LOG_FATAL, prog, "gw_db_lookup %s", gw_id_str);
246 fatal("Internal error in server");
248 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "open %s restart", clientp);
249 spawn (sprog, gw_id);
250 gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
251 if ((linein = open(serverp, O_RDONLY)) < 0)
253 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
254 fatal("Internal error in server");
256 if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
258 gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client.");
259 fatal("Internal error in server");
261 gw_log (GW_LOG_DEBUG, prog, "Synchronized.");
262 if ((lineout = open(clientp, O_WRONLY)) < 0)
264 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp);
265 fatal("Internal error in server");
269 gw_log (GW_LOG_DEBUG, prog, "Decoding user data");
270 p = combuf + sizeof(data);
273 strcpy(p, path_info);
275 *(p++) = '\0'; /* no envvars tranferred at present */
276 if ((t = getenv("CONTENT_LENGTH")) && (data = atoi(t)) > 0)
281 j = read(0, p + i, data - i);
284 gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog,
285 "Failed to read input");
286 fatal("Internal error in server");
290 gw_log (GW_LOG_ERRNO, prog, "Failed to read input");
291 fatal("Internal error in server");
299 memcpy(combuf, &data, sizeof(data));
300 gw_log (GW_LOG_DEBUG, prog, "Writing data");
301 if (write(lineout, combuf, data) < data)
303 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
304 fatal("Internal server error");
308 gw_log (GW_LOG_DEBUG, prog, "open %s", serverp);
309 if ((linein = open(serverp, O_RDONLY)) < 0)
311 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
312 fatal("Internal error in server");
315 gw_log (GW_LOG_DEBUG, prog, "Reading response");
318 while ((data = read(linein, combuf, COMBUF)) > 0)
320 gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
321 if (combuf[data-1] == '\0')
323 if (write(1, combuf, data) < data)
325 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
331 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
337 if (write(1, combuf, data) < data)
339 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
345 fcntl (linein, F_SETFL, O_NONBLOCK);
356 FD_SET(linein, &s_input);
360 gw_log (GW_LOG_DEBUG, prog, "select");
361 r = select (linein + 1, &s_input, NULL, NULL, &t);
364 gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog, "select");
367 if (r == 0 || FD_ISSET (linein, &s_input))
370 gw_log (GW_LOG_DEBUG, prog, "poll");
371 if ((data = read (linein, combuf, COMBUF)) > 0)
373 if (combuf[data-1] == '\0')
378 gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
379 if (data > 0 && write(1, combuf, data) < data)
381 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
389 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
392 gw_log (GW_LOG_DEBUG, prog, "poll read");
399 if (r > 0 && FD_ISSET (1, &s_input))
401 data = read (1, combuf, COMBUF);
404 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "stdout closed");
409 gw_log (GW_LOG_DEBUG, prog, "stdout closed");
412 gw_log (GW_LOG_DEBUG, prog, "stdout got %d bytes", data);
418 gw_log (GW_LOG_DEBUG, prog, "writing ack");
419 if (write(lineout, "OK", 2) < 2)
420 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
422 gw_log (GW_LOG_DEBUG, prog, "Cleaning up.");