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