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