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