Record presentation.
[egate.git] / www / wcgi.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
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,
13  * provided that:
14  *
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.
18  *
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.
22  *
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.
28  * 
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.
32  *
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.
42  *
43  * $Log: wcgi.c,v $
44  * Revision 1.3  1995/10/31 16:56:24  adam
45  * Record presentation.
46  *
47  * Revision 1.2  1995/10/23  16:55:36  adam
48  * A lot of changes - really.
49  *
50  * Revision 1.1  1995/10/20  11:49:25  adam
51  * First version of www gateway.
52  *
53  */
54
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <unistd.h>
58 #include <fcntl.h>
59 #include <sys/stat.h>
60 #include <sys/time.h>
61 #include <sys/types.h>
62 #ifdef AIX
63 #include <sys/select.h>
64 #endif
65
66 #define DEADSTRING "Your database server has terminated. To reactivate \
67 the server, please reload the server's 'front page'."
68
69 #include "wproto.h"
70
71 #define CGIDIR "/usr/local/etc/httpd/cgi-bin"
72
73 static char *prog = "cgi";
74
75 static char serverp[256] = {'\0'};
76
77 static void fatal(char *p)
78 {
79     printf("Content-type: text/html\n\n<HTML><HEAD><TITLE>Server Failure</TITLE></HEAD>\n");
80     printf("<BODY>%s</BODY>\n", p);
81     if (*serverp)
82         unlink(serverp);
83     exit(0);
84 }
85
86 static int spawn(char *sprog)
87 {
88     int r;
89     char path[256];
90
91     sprintf(path, "%s/%s", CGIDIR, sprog);
92     switch(r = fork())
93     {
94         case -1: 
95             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "fork"); 
96             exit(1);
97         case 0: 
98             close (0);
99             close (1);
100             gw_log (GW_LOG_DEBUG, prog, "execl %s", path);
101             execl (path, sprog, 0); 
102             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "execl %s", path);
103             exit(0);
104         default: 
105             return r;
106     }
107 }
108
109
110 /*
111  * NOTE: In the (perhaps odd) terminology used within this software,
112  * the 'server' is the present program, which is executed by the httpd
113  * server. The 'client' is the process running outside.
114  * Protocol is long(len)<serverfifo>\0<extrapath>\0<envvars>\0<INFO>\0
115  */
116 int main()
117 {
118     char clientp[256], tmp[256], *path_info, *p, *operation, *t;
119     char combuf[COMBUF];
120     int linein = -1, lineout, data, childpid;
121
122     gw_log_init ("egw");
123     gw_log_file (GW_LOG_ALL, "/usr/local/etc/httpd/logs/egwcgi_log");
124     gw_log_level (GW_LOG_ALL);
125     gw_log (GW_LOG_STAT, prog, "Europagate www cgi server");
126
127     sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR);
128     if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0)
129     {
130         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp);
131         fatal("Internal error in server.");
132     }
133     sprintf(serverp, "%s/srv%d", tmp, getpid());
134     if (access(serverp, R_OK|W_OK) == 0)
135     {
136         if (unlink(serverp) < 0)
137         {
138             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog,
139                     "Failed to delete stale fifo.");
140             fatal("Internal error in server.");
141         }
142         else
143             gw_log (GW_LOG_WARN, prog, "Removed stale server fifo.");
144     }
145     if (mkfifo(serverp, 0666 | S_IFIFO) < 0)
146     {
147         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "mkfifo(%s)", serverp);
148         fatal("Internal error in server.");
149     }
150     if (!(path_info = getenv("PATH_INFO")))
151     {
152         gw_log (GW_LOG_FATAL, prog, "Must set PATH_INFO.");
153         fatal("Internal error in server.");
154     }
155     operation = ++path_info;
156     while (*path_info && *path_info != '/')
157         path_info++;
158     if (*path_info)
159         *(path_info++) = '\0';
160     if ((childpid = atoi(operation)) <= 0)
161     {
162         childpid = spawn(operation);
163         /* synchronize with client. */
164         gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client.");
165         if ((linein = open(serverp, O_RDONLY)) < 0)
166         {
167             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open server %s", serverp);
168             fatal("Internal error in server.");
169         }
170         if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
171         {
172             gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client.");
173             fatal("Internal error in server");
174         }
175         gw_log (GW_LOG_DEBUG, prog, "Synchronized.");
176     }
177     sprintf(clientp, "%s/clt%d", tmp, childpid);
178     gw_log (GW_LOG_DEBUG, prog, "Opening %s", clientp);
179     if ((lineout = open(clientp, O_WRONLY)) < 0)
180     {
181         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp);
182         fatal(DEADSTRING);
183     }
184     gw_log (GW_LOG_DEBUG, prog, "Decoding user data.");
185     p = combuf + sizeof(data);
186     strcpy(p, serverp);
187     p += strlen(p) + 1;
188     strcpy(p, path_info);
189     p += strlen(p) + 1;
190     *(p++) = '\0';               /* no envvars tranferred at present */
191     if ((t = getenv("CONTENT_LENGTH")) && (data = atoi(t)) > 0)
192     {
193         if (read(0, p, data) < data)
194         {
195             gw_log (GW_LOG_FATAL, prog, "Failed to read input.");
196             fatal("Internal error in server.");
197         }
198     }
199     p += data;
200     *(p++) = '\0';
201     data = (p - combuf);
202     memcpy(combuf, &data, sizeof(data));
203     gw_log (GW_LOG_DEBUG, prog, "Writing data.");
204     if (write(lineout, combuf, data) < data)
205     {
206         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
207         fatal("Internal server error.");
208     }
209     if (linein < 0 && (linein = open(serverp, O_RDONLY)) < 0)
210     {
211         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open server %s", serverp);
212         fatal("Internal error in server.");
213     }
214     gw_log (GW_LOG_DEBUG, prog, "Reading response.");
215
216 #if 1
217     while ((data = read(linein, combuf, COMBUF)) > 0)
218     {
219         gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
220         if (write(1, combuf, data) < data)
221         {
222             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
223             exit (1);
224         }
225     }
226 #else
227     fcntl (linein, F_SETFL, O_NONBLOCK);
228     while (1)
229     {
230         fd_set s_input, s_output;
231         int r;
232     
233         FD_ZERO(&s_input);
234         FD_ZERO(&s_output);
235         FD_SET(linein, &s_input);
236 #if 0
237         FD_SET(1, &s_output);
238 #endif
239         r = select (linein + 1, &s_input, &s_output, NULL, 0);
240         if (r <= 0)
241         {
242             gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog, "select");
243             exit(1);
244         }
245         if (FD_ISSET (linein, &s_input))
246         {
247             data = read(linein, combuf, COMBUF);
248             if (data == 0)
249                 break;
250             else if (data < 0)
251             {
252                 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
253                 exit (1);
254             }
255             gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
256             if (write(1, combuf, data) < data)
257             {
258                 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
259                 exit (1);
260             }
261         }
262         if (FD_ISSET (1, &s_output))
263         {
264             gw_log (GW_LOG_DEBUG, prog, "stdout closed");
265             break;
266         }
267     }
268 #endif
269     if (data < 0)
270     {
271         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
272         exit (1);
273     }
274     gw_log (GW_LOG_DEBUG, prog, "Cleaning up.");
275     close(linein);
276     unlink(serverp);
277     close(lineout);
278     return 0;
279 }