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