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