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