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