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.5 1995/11/06 10:51:15 adam
45 * End of response marker in response from wsh/wproto to wcgi.
46 * Shells are respawned when necessary.
48 * Revision 1.4 1995/11/02 16:35:37 adam
49 * Bug fixes and select on FIFOs in wcgi - doesn't really work!
51 * Revision 1.3 1995/10/31 16:56:24 adam
52 * Record presentation.
54 * Revision 1.2 1995/10/23 16:55:36 adam
55 * A lot of changes - really.
57 * Revision 1.1 1995/10/20 11:49:25 adam
58 * First version of www gateway.
68 #include <sys/types.h>
70 #include <sys/select.h>
73 #define DEADSTRING "Your database server has terminated. To reactivate \
74 the server, please reload the server's 'front page'."
79 #define CGIDIR "/usr/local/etc/httpd/cgi-bin"
81 static char *prog = "cgi";
83 static char serverp[256] = {'\0'};
84 static GW_DB gw_db = NULL;
86 static void fatal(char *p)
88 printf("Content-type: text/html\n\n<HTML><HEAD><TITLE>Server Failure</TITLE></HEAD>\n");
89 printf("<BODY>%s</BODY>\n", p);
97 static int spawn (char *sprog, int id)
103 sprintf (envstr, "GWID=%d", id);
105 sprintf(path, "%s/%s", CGIDIR, sprog);
109 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "fork");
114 gw_log (GW_LOG_DEBUG, prog, "execl %s", path);
115 execl (path, sprog, 0);
116 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "execl %s", path);
124 * NOTE: In the (perhaps odd) terminology used within this software,
125 * the 'server' is the present program, which is executed by the httpd
126 * server. The 'client' is the process running outside.
127 * Protocol is long(len)<serverfifo>\0<extrapath>\0<envvars>\0<INFO>\0
131 char clientp[256], tmp[256], *path_info, *p, *operation, *t;
133 int linein = -1, lineout, data, gw_id;
136 gw_log_file (GW_LOG_ALL, "/usr/local/etc/httpd/logs/egwcgi_log");
137 gw_log_level (GW_LOG_ALL);
138 gw_log (GW_LOG_STAT, prog, "Europagate www cgi server");
140 sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR);
141 if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0)
143 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp);
144 fatal("Internal error in server.");
146 sprintf(serverp, "%s/srv%d", tmp, getpid());
147 if (access(serverp, R_OK|W_OK) == 0)
149 if (unlink(serverp) < 0)
151 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog,
152 "Failed to delete stale fifo.");
153 fatal("Internal error in server.");
156 gw_log (GW_LOG_WARN, prog, "Removed stale server fifo.");
158 if (mkfifo(serverp, 0666 | S_IFIFO) < 0)
160 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "mkfifo(%s)", serverp);
161 fatal("Internal error in server.");
163 if (!(path_info = getenv("PATH_INFO")))
165 gw_log (GW_LOG_FATAL, prog, "Must set PATH_INFO.");
166 fatal("Internal error in server.");
168 operation = ++path_info;
169 while (*path_info && *path_info != '/')
172 *(path_info++) = '\0';
173 if (!(gw_db = gw_db_open ("user.db", 1)))
175 gw_log (GW_LOG_FATAL, prog, "gw_db_open");
178 if ((gw_id = atoi(operation)) <= 0)
183 gw_id = gw_db_seq_no (gw_db);
184 sprintf (gw_id_str, "%d", gw_id);
186 spawn(operation, gw_id);
187 r = gw_db_insert (gw_db, gw_id_str, strlen(gw_id_str)+1,
188 operation, strlen(operation)+1);
191 gw_log (GW_LOG_FATAL, prog, "gw_db_insert: %d", r);
195 gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
196 if ((linein = open(serverp, O_RDONLY)) < 0)
198 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open server %s", serverp);
199 fatal("Internal error in server.");
201 if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
203 gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client.");
204 fatal("Internal error in server");
206 gw_log (GW_LOG_DEBUG, prog, "Synchronized.");
208 sprintf(clientp, "%s/clt%d", tmp, gw_id);
209 gw_log (GW_LOG_DEBUG, prog, "Opening %s", clientp);
210 if ((lineout = open(clientp, O_WRONLY)) < 0)
217 sprintf (gw_id_str, "%d", gw_id);
218 r = gw_db_lookup (gw_db, gw_id_str, strlen(gw_id_str)+1,
219 &sprog, &sprog_size);
222 gw_log (GW_LOG_FATAL, prog, "gw_db_lookup %s", gw_id_str);
223 fatal("Internal error in server");
225 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "open %s restart", clientp);
226 spawn (sprog, gw_id);
227 gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
228 if ((linein = open(serverp, O_RDONLY)) < 0)
230 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open server %s", serverp);
231 fatal("Internal error in server");
233 if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
235 gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client.");
236 fatal("Internal error in server");
238 gw_log (GW_LOG_DEBUG, prog, "Synchronized.");
239 if ((lineout = open(clientp, O_WRONLY)) < 0)
241 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp);
247 gw_log (GW_LOG_DEBUG, prog, "Decoding user data.");
248 p = combuf + sizeof(data);
251 strcpy(p, path_info);
253 *(p++) = '\0'; /* no envvars tranferred at present */
254 if ((t = getenv("CONTENT_LENGTH")) && (data = atoi(t)) > 0)
256 if (read(0, p, data) < data)
258 gw_log (GW_LOG_FATAL, prog, "Failed to read input.");
259 fatal("Internal error in server.");
265 memcpy(combuf, &data, sizeof(data));
266 gw_log (GW_LOG_DEBUG, prog, "Writing data.");
267 if (write(lineout, combuf, data) < data)
269 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
270 fatal("Internal server error.");
272 if (linein < 0 && (linein = open(serverp, O_RDONLY)) < 0)
274 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open server %s", serverp);
275 fatal("Internal error in server.");
277 gw_log (GW_LOG_DEBUG, prog, "Reading response.");
280 while ((data = read(linein, combuf, COMBUF)) > 0)
282 gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
283 if (write(1, combuf, data) < data)
285 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
291 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
296 fcntl (linein, F_SETFL, O_NONBLOCK);
307 FD_SET(linein, &s_input);
311 gw_log (GW_LOG_DEBUG, prog, "select");
312 r = select (linein + 1, &s_input, NULL, NULL, &t);
315 gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog, "select");
318 if (r == 0 || FD_ISSET (linein, &s_input))
321 gw_log (GW_LOG_DEBUG, prog, "poll");
322 if ((data = read (linein, combuf, COMBUF)) > 0)
324 if (combuf[data-1] == '\0')
329 gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
330 if (data > 0 && write(1, combuf, data) < data)
332 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
340 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
343 gw_log (GW_LOG_DEBUG, prog, "poll read");
350 if (r > 0 && FD_ISSET (1, &s_input))
352 gw_log (GW_LOG_DEBUG, prog, "stdout closed");
357 gw_log (GW_LOG_DEBUG, prog, "Cleaning up.");