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.2 1995/10/23 16:55:36 adam
45 * A lot of changes - really.
47 * Revision 1.1 1995/10/20 11:49:25 adam
48 * First version of www gateway.
55 #include <sys/types.h>
59 #define DEADSTRING "Your database server has terminated. To reactivate \
60 the server, please reload the server's 'front page'."
64 #define CGIDIR "/usr/local/etc/httpd/cgi-bin"
66 static char *prog = "cgi";
68 static char serverp[256] = {'\0'};
70 static void fatal(char *p)
72 printf("Content-type: text/html\n\n<HTML><HEAD><TITLE>Server Failure</TITLE></HEAD>\n");
73 printf("<BODY>%s</BODY>\n", p);
79 static int spawn(char *sprog)
84 sprintf(path, "%s/%s", CGIDIR, sprog);
88 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "fork");
93 gw_log (GW_LOG_DEBUG, prog, "execl %s", path);
94 execl (path, sprog, 0);
95 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "execl %s", path);
104 * NOTE: In the (perhaps odd) terminology used within this software,
105 * the 'server' is the present program, which is executed by the httpd
106 * server. The 'client' is the process running outside.
107 * Protocol is long(len)<serverfifo>\0<extrapath>\0<envvars>\0<INFO>\0
111 char clientp[256], tmp[256], *path_info, *p, *operation, *t;
113 int linein = -1, lineout, data, childpid;
116 gw_log_file (GW_LOG_ALL, "/usr/local/etc/httpd/logs/egwcgi_log");
117 gw_log_level (GW_LOG_ALL);
118 gw_log (GW_LOG_STAT, prog, "Europagate www cgi server");
120 sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR);
121 if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0)
123 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp);
124 fatal("Internal error in server.");
126 sprintf(serverp, "%s/srv%d", tmp, getpid());
127 if (access(serverp, R_OK|W_OK) == 0)
129 if (unlink(serverp) < 0)
131 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog,
132 "Failed to delete stale fifo.");
133 fatal("Internal error in server.");
136 gw_log (GW_LOG_WARN, prog, "Removed stale server fifo.");
138 if (mkfifo(serverp, 0666 | S_IFIFO) < 0)
140 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "mkfifo(%s)", serverp);
141 fatal("Internal error in server.");
143 if (!(path_info = getenv("PATH_INFO")))
145 gw_log (GW_LOG_FATAL, prog, "Must set PATH_INFO.");
146 fatal("Internal error in server.");
148 operation = ++path_info;
149 while (*path_info && *path_info != '/')
152 *(path_info++) = '\0';
153 if ((childpid = atoi(operation)) <= 0)
155 childpid = spawn(operation);
156 /* synchronize with client. */
157 gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client.");
158 if ((linein = open(serverp, O_RDONLY)) < 0)
160 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open server %s", serverp);
161 fatal("Internal error in server.");
163 if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
165 gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client.");
166 fatal("Internal error in server");
168 gw_log (GW_LOG_DEBUG, prog, "Synchronized.");
170 sprintf(clientp, "%s/clt%d", tmp, childpid);
171 gw_log (GW_LOG_DEBUG, prog, "Opening %s", clientp);
172 if ((lineout = open(clientp, O_WRONLY)) < 0)
174 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp);
177 gw_log (GW_LOG_DEBUG, prog, "Decoding user data.");
178 p = combuf + sizeof(data);
181 strcpy(p, path_info);
183 *(p++) = '\0'; /* no envvars tranferred at present */
184 if ((t = getenv("CONTENT_LENGTH")) && (data = atoi(t)) > 0)
186 if (read(0, p, data) < data)
188 gw_log (GW_LOG_FATAL, prog, "Failed to read input.");
189 fatal("Internal error in server.");
195 memcpy(combuf, &data, sizeof(data));
196 gw_log (GW_LOG_DEBUG, prog, "Writing data.");
197 if (write(lineout, combuf, data) < data)
199 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
200 fatal("Internal server error.");
202 if (linein < 0 && (linein = open(serverp, O_RDONLY)) < 0)
204 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open server %s", serverp);
205 fatal("Internal error in server.");
207 gw_log (GW_LOG_DEBUG, prog, "Reading response.");
208 while ((data = read(linein, combuf, COMBUF)) > 0)
210 if (write(1, combuf, data) < data)
212 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
213 fatal("Internal server error.");
218 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
219 fatal("Internal server error.");
221 gw_log (GW_LOG_DEBUG, prog, "Cleaning up.");