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