Added descriptive text field in target info.
[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.7  1995/11/08 12:42:18  adam
45  * Added descriptive text field in target info.
46  * Added authentication field in target info.
47  *
48  * Revision 1.6  1995/11/06  17:44:22  adam
49  * State reestablised when shell restarts. History of previous
50  * result sets.
51  *
52  * Revision 1.5  1995/11/06  10:51:15  adam
53  * End of response marker in response from wsh/wproto to wcgi.
54  * Shells are respawned when necessary.
55  *
56  * Revision 1.4  1995/11/02  16:35:37  adam
57  * Bug fixes and select on FIFOs in wcgi - doesn't really work!
58  *
59  * Revision 1.3  1995/10/31  16:56:24  adam
60  * Record presentation.
61  *
62  * Revision 1.2  1995/10/23  16:55:36  adam
63  * A lot of changes - really.
64  *
65  * Revision 1.1  1995/10/20  11:49:25  adam
66  * First version of www gateway.
67  *
68  */
69
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <unistd.h>
73 #include <fcntl.h>
74 #include <sys/stat.h>
75 #include <sys/time.h>
76 #include <sys/types.h>
77 #ifdef AIX
78 #include <sys/select.h>
79 #endif
80
81 #define DEADSTRING "Your database server has terminated. To reactivate \
82 the server, please reload the server's 'front page'."
83
84 #include <gw-db.h>
85 #include "wproto.h"
86
87 #define CGIDIR "/usr/local/etc/httpd/cgi-bin"
88
89 static char *prog = "cgi";
90
91 static char serverp[256] = {'\0'};
92 static GW_DB gw_db = NULL;
93
94 static void fatal(char *p)
95 {
96     printf("Content-type: text/html\n\n<HTML><HEAD><TITLE>Server Failure</TITLE></HEAD>\n");
97     printf("<BODY>%s</BODY>\n", p);
98     if (gw_db)
99         gw_db_close (gw_db);
100     if (*serverp)
101         unlink(serverp);
102     exit(0);
103 }
104
105 static int spawn (char *sprog, int id)
106 {
107     int r;
108     char path[256];
109     char envstr[80];
110
111     sprintf (envstr, "GWID=%d", id);
112     putenv (envstr);
113     sprintf(path, "%s/%s", CGIDIR, sprog);
114     switch(r = fork())
115     {
116         case -1: 
117             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "fork"); 
118             exit(1);
119         case 0: 
120             close (0);
121             close (1);
122             gw_log (GW_LOG_DEBUG, prog, "execl %s", path);
123             execl (path, sprog, 0); 
124             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "execl %s", path);
125             exit(0);
126         default: 
127             return r;
128     }
129 }
130
131 /*
132  * NOTE: In the (perhaps odd) terminology used within this software,
133  * the 'server' is the present program, which is executed by the httpd
134  * server. The 'client' is the process running outside.
135  * Protocol is long(len)<serverfifo>\0<extrapath>\0<envvars>\0<INFO>\0
136  */
137 int main()
138 {
139     char clientp[256], tmp[256], *path_info, *p, *operation, *t;
140     char combuf[COMBUF];
141     int linein = -1, lineout, data, gw_id;
142
143     gw_log_init ("egw");
144     gw_log_file (GW_LOG_ALL, "/usr/local/etc/httpd/logs/egwcgi_log");
145     gw_log_level (GW_LOG_ALL);
146     gw_log (GW_LOG_STAT, prog, "Europagate www cgi server");
147
148     sprintf(tmp, "%s/%s", FIFOROOT, FIFODIR);
149     if (access(tmp, R_OK|W_OK) < 0 && mkdir(tmp, 0777) < 0)
150     {
151         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "Failed to create %s", tmp);
152         fatal("Internal error in server.");
153     }
154     sprintf(serverp, "%s/srv%d", tmp, getpid());
155     if (access(serverp, R_OK|W_OK) == 0)
156     {
157         if (unlink(serverp) < 0)
158         {
159             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog,
160                     "Failed to delete stale fifo.");
161             fatal("Internal error in server.");
162         }
163         else
164             gw_log (GW_LOG_WARN, prog, "Removed stale server fifo.");
165     }
166     if (mkfifo(serverp, 0666 | S_IFIFO) < 0)
167     {
168         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "mkfifo(%s)", serverp);
169         fatal("Internal error in server.");
170     }
171     if (!(path_info = getenv("PATH_INFO")))
172     {
173         gw_log (GW_LOG_FATAL, prog, "Must set PATH_INFO.");
174         fatal("Internal error in server.");
175     }
176     operation = ++path_info;
177     while (*path_info && *path_info != '/')
178         path_info++;
179     if (*path_info)
180         *(path_info++) = '\0';
181     if (!(gw_db = gw_db_open ("user.db", 1)))
182     {
183         gw_log (GW_LOG_FATAL, prog, "gw_db_open");
184         exit (1);
185     }
186     if ((gw_id = atoi(operation)) <= 0)
187     {
188         int r;
189         char gw_id_str[16];
190
191         gw_id = gw_db_seq_no (gw_db);
192         sprintf (gw_id_str, "%d", gw_id);
193         
194         spawn(operation, gw_id);
195         r = gw_db_insert (gw_db, gw_id_str, strlen(gw_id_str)+1,
196                           operation, strlen(operation)+1);
197         if (r)
198         {
199             gw_log (GW_LOG_FATAL, prog, "gw_db_insert: %d", r);
200             gw_db_close (gw_db);
201             exit (1);
202         }
203         gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
204         if ((linein = open(serverp, O_RDONLY)) < 0)
205         {
206             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
207             fatal("Internal error in server.");
208         }
209         if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
210         {
211             gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client");
212             fatal("Internal error in server");
213         }
214         gw_log (GW_LOG_DEBUG, prog, "Synchronized");
215     }
216     sprintf(clientp, "%s/clt%d", tmp, gw_id);
217     gw_log (GW_LOG_DEBUG, prog, "Opening %s", clientp);
218     if ((lineout = open(clientp, O_WRONLY)) < 0)
219     {
220         char gw_id_str[16];
221         void *sprog;
222         size_t sprog_size;
223         int r;
224
225         sprintf (gw_id_str, "%d", gw_id);
226         r = gw_db_lookup (gw_db, gw_id_str, strlen(gw_id_str)+1,  
227                           &sprog, &sprog_size);
228         if (r != 1)
229         {
230             gw_log (GW_LOG_FATAL, prog, "gw_db_lookup %s", gw_id_str);
231             fatal("Internal error in server");
232         }
233         gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "open %s restart", clientp);
234         spawn (sprog, gw_id);
235         gw_log (GW_LOG_DEBUG, prog, "Synchronizing with client");
236         if ((linein = open(serverp, O_RDONLY)) < 0)
237         {
238             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
239             fatal("Internal error in server");
240         }
241         if (read(linein, combuf, 2) < 2 || strcmp(combuf, "OK"))
242         {
243             gw_log (GW_LOG_FATAL, prog, "Failed to synchronize with client.");
244             fatal("Internal error in server");
245         }
246         gw_log (GW_LOG_DEBUG, prog, "Synchronized.");
247         if ((lineout = open(clientp, O_WRONLY)) < 0)
248         {
249             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "%s", clientp);
250             fatal("Internal error in server");
251         }
252     }
253     gw_db_close (gw_db);
254     gw_log (GW_LOG_DEBUG, prog, "Decoding user data");
255     p = combuf + sizeof(data);
256     strcpy(p, serverp);
257     p += strlen(p) + 1;
258     strcpy(p, path_info);
259     p += strlen(p) + 1;
260     *(p++) = '\0';               /* no envvars tranferred at present */
261     if ((t = getenv("CONTENT_LENGTH")) && (data = atoi(t)) > 0)
262     {
263         if (read(0, p, data) < data)
264         {
265             gw_log (GW_LOG_FATAL, prog, "Failed to read input");
266             fatal("Internal error in server");
267         }
268     }
269     p += data;
270     *(p++) = '\0';
271     data = (p - combuf);
272     memcpy(combuf, &data, sizeof(data));
273     gw_log (GW_LOG_DEBUG, prog, "Writing data");
274     if (write(lineout, combuf, data) < data)
275     {
276         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
277         fatal("Internal server error");
278     }
279     if (linein < 0 && (linein = open(serverp, O_RDONLY)) < 0)
280     {
281         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "open %s", serverp);
282         fatal("Internal error in server");
283     }
284     gw_log (GW_LOG_DEBUG, prog, "Reading response");
285
286 #if 0
287     while ((data = read(linein, combuf, COMBUF)) > 0)
288     {
289         gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
290         if (write(1, combuf, data) < data)
291         {
292             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
293             exit (1);
294         }
295     }
296     if (data < 0)
297     {
298         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
299         exit (1);
300     }
301 #else
302 #  if 1
303     fcntl (linein, F_SETFL, O_NONBLOCK);
304 #  endif
305     while (1)
306     {
307         fd_set s_input;
308         struct timeval t;
309         int r, eof_flag = 0;
310     
311         t.tv_sec = 10;
312         t.tv_usec = 0;
313         FD_ZERO(&s_input);
314         FD_SET(linein, &s_input);
315 #  if 0
316         FD_SET(1, &s_input);
317 #  endif
318         gw_log (GW_LOG_DEBUG, prog, "select");
319         r = select (linein + 1, &s_input, NULL, NULL, &t);
320         if (r < 0)
321         {
322             gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, prog, "select");
323             exit(1);
324         }
325         if (r == 0 || FD_ISSET (linein, &s_input))
326         {
327             if (r == 0)
328                 gw_log (GW_LOG_DEBUG, prog, "poll");
329             if ((data = read (linein, combuf, COMBUF)) > 0)
330             {
331                 if (combuf[data-1] == '\0')
332                 {
333                     --data;
334                     eof_flag = 1;
335                 }
336                 gw_log (GW_LOG_DEBUG, prog, "Got %d bytes", data);
337                 if (data > 0 && write(1, combuf, data) < data)
338                 {
339                     gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "write");
340                     exit (1);
341                 }
342             }
343             else if (data == -1)
344             {
345                 if (r > 0)
346                 {
347                     gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, prog, "read");
348                     exit (1);
349                 }
350                 gw_log (GW_LOG_DEBUG, prog, "poll read");
351             }
352             else
353                 break;
354         }
355         if (eof_flag)
356             break;
357         if (r > 0 && FD_ISSET (1, &s_input))
358         {
359             data = read (1, combuf, COMBUF);
360             if (data == -1)
361             {
362                 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, prog, "stdout closed");
363                 break;
364             }
365             if (data == 0)
366             {
367                 gw_log (GW_LOG_DEBUG, prog, "stdout closed");
368                 break;
369             }
370             gw_log (GW_LOG_DEBUG, prog, "stdout got %d bytes", data);
371         }
372     }
373 #endif
374     gw_log (GW_LOG_DEBUG, prog, "Cleaning up.");
375     close(linein);
376     unlink(serverp);
377     close(lineout);
378     return 0;
379 }