CGI script passes name of lock file to the shell. The server will not close
[egate.git] / www / wproto.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: wproto.c,v $
44  * Revision 1.14  1996/01/12 13:08:07  adam
45  * CGI script passes name of lock file to the shell. The server will not close
46  * the response FIFO until this file becomes unlocked. This method handles
47  * cancel operations much better.
48  *
49  * Revision 1.13  1996/01/12  10:05:20  adam
50  * If script name ends with ';' HTTP/GET/Expires will be defined.
51  * The cgi interface only reads final handshake if response from
52  * server (shell) was zero-terminated [If it isn't it probably died].
53  *
54  * Revision 1.12  1996/01/05  16:35:02  adam
55  * Minor changes.
56  *
57  * Revision 1.11  1996/01/05  16:21:21  adam
58  * Bug fix: shell (wproto) sometimes closed server FIFO before cgi
59  * program opened it - solution: cgi sends OK when response has been read.
60  *
61  * Revision 1.10  1995/12/22  14:21:16  adam
62  * More work on scan. The search.egw script takes care of cached
63  * query page (doesn't always increment nextSetNo). To make new search set
64  * either 'New query' must be selected or the query page must be reloaded.
65  * The msearch script doesn't do this yet, however.
66  *
67  * Revision 1.9  1995/11/14  16:31:36  adam
68  * Temporary remove of ccl entry.
69  *
70  * Revision 1.8  1995/11/13  15:41:45  adam
71  * Arrow gifs.
72  * Gateway uses record element set names B(rief) and F(ull).
73  * Bug fix. Didn't save idAuthentication correctly.
74  *
75  * Revision 1.7  1995/11/10  14:47:32  adam
76  * Plus (+) characters automatically converted to space in forms.
77  * Work on search in multiple targets. Doesn't work well - yet.
78  * Presentation formats enhanced.
79  *
80  * Revision 1.6  1995/11/06  10:51:17  adam
81  * End of response marker in response from wsh/wproto to wcgi.
82  * Shells are respawned when necessary.
83  *
84  * Revision 1.5  1995/11/02  16:35:37  adam
85  * Bug fixes and select on FIFOs in wcgi - doesn't really work!
86  *
87  * Revision 1.4  1995/10/31  16:56:25  adam
88  * Record presentation.
89  *
90  * Revision 1.3  1995/10/27  15:12:10  adam
91  * IrTcl incorporated in the gateway.
92  * Better separation of script types.
93  * Z39.50 gateway scripts entered.
94  *
95  * Revision 1.2  1995/10/23  16:55:39  adam
96  * A lot of changes - really.
97  *
98  * Revision 1.1  1995/10/20  11:49:26  adam
99  * First version of www gateway.
100  *
101  */
102
103 #include <stdio.h>
104 #include <string.h>
105 #include <stdlib.h>
106 #include <sys/time.h>
107 #include <sys/types.h>
108 #include <sys/stat.h>
109 #include <fcntl.h>
110 #include <unistd.h>
111 #include <stdarg.h>
112 #include <ctype.h>
113 #include <errno.h>
114
115 #include "wproto.h"
116
117 static int wproto_dumpcache(WCLIENT wc, int level);
118 static int wproto_findcache(WCLIENT wc, char *name);
119 static void wproto_uncache(WCLIENT wc, int level);
120
121 static char *mod = "wproto";
122
123 void wo_write (WCLIENT wc, const char *s, size_t len)
124 {
125     if (wc->outbuffer_offset + len >= wc->outbuffer_size)
126         wc->outbuffer = realloc(wc->outbuffer, wc->outbuffer_size +=
127         OUTBUFFER_CHUNK);
128     memcpy(wc->outbuffer + wc->outbuffer_offset, s, len);
129     wc->outbuffer_offset += len;
130 }
131
132 void wo_puts (WCLIENT wc, const char *s)
133 {
134     wo_write (wc, s, strlen(s));
135 }
136
137 void wo_printf (WCLIENT wc, const char *fmt, ...)
138 {
139     va_list ap;
140     char tmpbuf[4048];
141
142     va_start(ap, fmt);
143     vsprintf(tmpbuf, fmt, ap);
144     wo_puts(wc, tmpbuf);
145     va_end(ap);
146 }
147
148 void wo_clear (WCLIENT wc, const char *type)
149 {
150     if (!wc->outbuffer)
151         wc->outbuffer = malloc(wc->outbuffer_size = OUTBUFFER_CHUNK);
152     wc->outbuffer_offset = 0;
153     if (type)
154         wo_printf(wc, "Content-type: %s\n\n", type);
155 }
156
157 int wo_puthtml (WCLIENT wc, char *name)
158 {
159     FILE *f; 
160     char ch;
161
162     wo_clear(wc, "text/html");
163     if (!(f = fopen(name, "r")))
164     {
165         wo_printf(wc, "<BR>Failed to open file: %s<BR>", name);
166         return 0;
167     }
168     while (ch = getc(f), !feof(f))
169     {
170         if (wo_putc(wc, ch) < 0)
171         {
172             fclose(f);
173             return -1;
174         }
175     }
176     fclose(f);
177     return 0;
178 }
179
180 int wo_flush(WCLIENT wc)
181 {
182     int wrote, towrite;
183
184     if (!(wc->outbuffer_offset))
185         return 0;
186     towrite = wc->outbuffer_offset;
187     wc->outbuffer_offset = 0;
188     for (;;)
189     {
190         int w_chunk;
191
192         w_chunk = towrite;
193         wrote = write(wc->lineout, wc->outbuffer + wc->outbuffer_offset,
194             w_chunk);
195         if (wrote <= 0)
196         {
197             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write response");
198             return -1;
199         }
200         gw_log (GW_LOG_DEBUG, mod, "wrote %d bytes", wrote);
201         if (wc->cache_fd >= 0)
202             if (write(wc->cache_fd, wc->outbuffer + wc->outbuffer_offset,
203                 towrite) < 0)
204             {   
205                 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write cache");
206                 return -1;
207             }
208         towrite -= wrote;
209         if (!towrite)
210             break;
211         wc->outbuffer_offset += wrote;
212     }
213     wc->outbuffer_offset = 0;
214     return 0;
215 }
216
217 int wo_overflow(WCLIENT wc, char ch)
218 {
219     gw_log (GW_LOG_DEBUG, mod, "wo_overflow");
220     if (wo_flush(wc) < 0)
221         return -1;
222     return wo_putc(wc, ch);
223 }
224
225 int wo_finish(WCLIENT wc)
226 {
227     int fd;
228     gw_log (GW_LOG_DEBUG, mod, "wo_finish");
229
230     wo_putc (wc, 0);
231     if (wo_flush(wc) < 0)
232         return -1;
233
234     fd = open (wc->wf_serverf, O_RDONLY);
235     if (fd != -1)
236     {
237         struct flock area;
238         area.l_type = F_RDLCK;
239         area.l_whence = SEEK_SET;
240         area.l_start = 0L;
241         area.l_len = 0L;
242         fcntl (fd, F_SETLKW, &area);
243         close (fd);
244     }
245     close(wc->lineout);
246     wc->lineout = -1;
247     if (wc->cache_fd >= 0)
248     {
249         close(wc->cache_fd);
250         wc->cache_fd = -1;
251     }
252     return 0;
253 }
254
255 static void descramble(char *t, const char *o)
256 {
257     unsigned int v;
258
259     while (*o)
260     {
261         if (*o == '%' && isxdigit(*(o + 1)) && isxdigit(*(o + 2)))
262         {
263             sscanf(o + 1, "%2x", &v);
264             o += 3;
265             if (v == '+')
266                 *t = ' ';
267             else
268                 *t = (char) v;
269             t++;
270         }
271         else
272         {
273             if (*o == '+')
274                 *t = ' ';
275             else
276                 *t = *o;
277             t++;
278             o++;
279         }
280     }
281     *t = '\0';
282 }
283
284 static void decode_form(wform_data *form, char *buf)
285 {
286     int i = 0;
287     char *p;
288     char tmp[512];
289
290     while (*buf)
291     {
292         for (p = form[i].name; *buf && *buf != '='; buf++)
293             *(p++) = *buf;
294         *p = '\0';
295         if (*buf)
296             buf++;
297         for (p = tmp; *buf && *buf != '&'; buf++)
298             *(p++) = *buf;
299         *p = '\0';
300         descramble(form[i].value, tmp);
301         if (*buf)
302             buf++;
303         i++;
304     }
305     *form[i].name = '\0';
306 }
307
308 char *wgetval(WCLIENT wc, char *name)
309 {
310     int i;
311
312     for (i = 0; *wc->wf_data[i].name; i++)
313         if (!strcmp(name, wc->wf_data[i].name))
314             return wc->wf_data[i].value;
315     return 0;
316 }
317
318 int wproto_process(WCLIENT wc, int timeout)
319 {
320     int toread, rs, level;
321     char combuf[COMBUF], *p,*t;
322     fd_set input;
323     struct timeval to, *top;
324
325     for (;;)
326     {
327         gw_log (GW_LOG_DEBUG, mod, "process waiting for input.");
328         if (timeout > 0)
329         {
330             to.tv_usec = 0;
331             to.tv_sec = timeout;
332             top = &to;
333         }
334         else
335             top = 0;
336         FD_ZERO(&input);
337         FD_SET(wc->linein, &input);
338         /* go through select handle list */
339         while ((rs = select(wc->linein + 1, &input, 0, 0, top)) < 0 &&
340             errno == EINTR)
341             ;
342         if (rs < 0)
343         {
344             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "select");
345             return -1;
346         }
347         if (rs == 0)
348         {
349             gw_log (GW_LOG_STAT, mod, 
350                     "wproto_process returning 0 after %d second timeout.",
351                     timeout);
352             unlink (wc->wf_serverp);
353             return 0;
354         }
355         /* determine handle (fifo or user) */
356         if (read(wc->linein, &toread, sizeof(toread)) < sizeof(toread))
357         {
358             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "wp_proc:len read failed");
359             exit(1);
360         }
361         toread -= sizeof(toread);
362         if (read(wc->linein, combuf, toread) < toread)
363         {
364             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "wp_proc: data read failed");
365             exit(1);
366         }
367         p = combuf;
368         for (t = wc->wf_serverp; (*t = *p); t++, p++);
369         p++;
370         for (t = wc->wf_serverf; (*t = *p); t++, p++);
371         p++;
372         for (t = wc->wf_parms; (*t = *p); t++, p++);
373         p++;
374         p++;         /* we don't deal with envvars yet */
375         decode_form(wc->wf_data, p);
376         if (wc->lineout < 0)
377         {
378             gw_log (GW_LOG_DEBUG, mod, "open %s", wc->wf_serverp);
379             if ((wc->lineout = open(wc->wf_serverp, O_WRONLY)) < 0)
380             {
381                 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s", 
382                         wc->wf_serverp);
383                 exit(1);
384             }
385         }
386         /* look in cache only if request carries no forms data. */
387         if (!*wc->wf_data[0].name && (level = wproto_findcache(wc,
388             wc->wf_parms)) >= 0)
389         {
390             gw_log (GW_LOG_DEBUG, mod, "wproto_dumpcache");
391             wproto_dumpcache(wc, level);
392             wo_finish(wc);
393             
394         }
395         else
396         {
397             return 1;
398         }
399     }
400 }
401
402 WCLIENT wproto_init(void)
403 {
404     char *val, path2[256];
405     wclient_data *new;
406
407     gw_log (GW_LOG_DEBUG, mod, "wproto_init");
408     close(1);    /* release us from the wserver */
409     if (!(new = malloc(sizeof(*new))))
410     {
411         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc");
412         exit (1);
413     }
414     if (!(val = getenv ("GWID")))
415     {
416         gw_log (GW_LOG_FATAL, mod, "GWID not set");
417         exit (1);
418     }
419     new->id = atoi (val);
420     sprintf(new->path, "%s/%s/clt%d", FIFOROOT, FIFODIR, new->id);
421     if (mkfifo(new->path, 0666 | S_IFIFO) < 0)
422         gw_log (GW_LOG_WARN|GW_LOG_ERRNO, mod, "mkfifo(%s)", new->path);
423     gw_log (GW_LOG_DEBUG, mod, "Synchronizing with server.");
424     sprintf(path2, "%s/%s/srv%d", FIFOROOT, FIFODIR, getppid());
425     if ((new->lineout = open(path2, O_WRONLY)) < 0)
426     {
427         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open out %s", path2);
428         exit(1);
429     }
430     if (write(new->lineout, "OK", 2) < 2)
431     {
432         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write");
433         exit(1);
434     }
435     gw_log (GW_LOG_DEBUG, mod, "Synchronized.");
436     if ((new->linein = open(new->path, O_RDONLY)) < 0)
437     {
438         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open input %s", new->path);
439         exit(1);
440     }
441     gw_log (GW_LOG_DEBUG, mod, "init. linein=%d lineout=%d",
442             new->linein, new->lineout);
443     /* we put a handle on this so we get a blocking read when no peer */
444     if (open(new->path, O_WRONLY | O_NDELAY) < 0)
445     {
446         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open dummy %s", new->path);
447         exit(1);
448     }
449     new->outbuffer = 0;
450     new->cache_level = -1;
451     new->cache_fd = -1;
452     return new;
453 }
454
455 static void wproto_uncache(WCLIENT wc, int level)
456 {
457     for (;wc->cache_level >= level; wc->cache_level--)
458         unlink(wc->cache[wc->cache_level].path);
459 }
460
461 void wproto_terminate(WCLIENT wc)
462 {
463     close(wc->linein);
464     unlink(wc->path);
465     wproto_uncache(wc, 0);
466     free(wc);
467 }
468
469 int wproto_cache(WCLIENT wc, int level)
470 {
471     cache_data *p;
472
473     if (level > wc->cache_level + 1)
474     {
475         gw_log (GW_LOG_FATAL, mod, "Illegal cache level increment.");
476         exit(1);
477     }
478     wproto_uncache(wc, level);
479     p = &wc->cache[++wc->cache_level];
480     sprintf(p->path, "%s/%s/csh%d.%d", FIFOROOT, FIFODIR, wc->id, level);
481     if ((wc->cache_fd = open(p->path, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0)
482     {
483         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "open %s", p->path);
484         return -1;
485     }
486     strcpy(p->name, wc->wf_parms);
487     return 0;
488 }
489
490 static int wproto_findcache(WCLIENT wc, char *name)
491 {
492     int i;
493
494     for (i = 0; i <= wc->cache_level; i++)
495         if (!strcmp(wc->cache[i].name, name))
496             return i;
497     return -1;
498 }
499
500 static int wproto_dumpcache(WCLIENT wc, int level)
501 {
502     int fd, rd;
503
504     gw_log (GW_LOG_STAT, mod, "Using Cache: %s", wc->cache[level].name);
505     if ((fd = open(wc->cache[level].path, O_RDONLY)) < 0)
506     {
507         gw_log (GW_LOG_FATAL, mod, "open (R) %s", wc->cache[level].path);
508         return -1;
509     }
510     while ((rd = read(fd, wc->outbuffer, OUTBUFFER_CHUNK)) > 0)
511         if (write(wc->lineout, wc->outbuffer, rd) < rd)
512         {
513             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "write toline");
514             return -1;
515         }
516     if (rd < 0)
517     {
518         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "read");
519         return -1;
520     }
521     wproto_uncache(wc, level + 1);
522     return 0;
523 }