Bug fixes and minor improvements.
[egate.git] / kernel / main.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  */
44 /* Gateway kernel - Main
45  * Europagate, 1995
46  *
47  * $Log: main.c,v $
48  * Revision 1.26  1995/05/18 12:03:08  adam
49  * Bug fixes and minor improvements.
50  *
51  * Revision 1.25  1995/05/16  09:40:42  adam
52  * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
53  *
54  * Revision 1.24  1995/05/04  10:40:07  adam
55  * More work on Def-settings.
56  *
57  * Revision 1.23  1995/05/03  16:34:18  adam
58  * CCL def command, i.e. user definitions - saved as resource files.
59  *
60  * Revision 1.22  1995/05/03  07:37:39  adam
61  * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
62  * are used when possible.
63  *
64  * Revision 1.21  1995/05/01  16:26:56  adam
65  * More work on resource monitor.
66  *
67  * Revision 1.20  1995/05/01  12:43:32  adam
68  * First work on resource monitor program.
69  *
70  * Revision 1.19  1995/04/19  16:01:58  adam
71  * Some hacks to get the FIFO communication work!! Isn't reliable.
72  * Resource gw.account added - default account info.
73  *
74  * Revision 1.18  1995/04/19  13:19:09  adam
75  * New command: account - for authentication.
76  *
77  * Revision 1.17  1995/04/19  10:46:18  adam
78  * Persistency works much better now. New command: status - history-like
79  *
80  * Revision 1.16  1995/04/19  07:31:07  adam
81  * First work on Z39.50 persistence.
82  *
83  * Revision 1.15  1995/04/17  09:34:30  adam
84  * Timeout (idletime) adjustable. Minor changes in kernel.
85  *
86  * Revision 1.14  1995/03/28  11:42:34  adam
87  * First use of string-queue utility.
88  *
89  * Revision 1.13  1995/03/28  08:01:25  adam
90  * FIFO existence is used to test for a running kernel.
91  *
92  * Revision 1.12  1995/03/27  12:51:05  adam
93  * New log level in use: GW_LOG_ERRNO.
94  *
95  * Revision 1.11  1995/03/27  08:24:02  adam
96  * First use of gip interface and gw-db.
97  * First work on eti program.
98  *
99  * Revision 1.10  1995/03/01  14:32:25  adam
100  * Better diagnostics. Default is, that only one database selected when
101  * several are known.
102  *
103  * Revision 1.9  1995/02/23  08:32:17  adam
104  * Changed header.
105  *
106  * Revision 1.7  1995/02/22  15:22:33  adam
107  * Much more checking of run-time state. Show command never retrieves
108  * more records than indicated by the previous search request. Help
109  * command available. The maximum number of records retrieved can be
110  * controlled now.
111  *
112  * Revision 1.6  1995/02/22  08:51:34  adam
113  * Output function can be customized in fml, which is used to print
114  * the reply to reply_fd.
115  *
116  * Revision 1.5  1995/02/20  21:16:20  adam
117  * FML support. Bug fixes. Profile for drewdb.
118  *
119  * Revision 1.4  1995/02/17  17:06:16  adam
120  * Minor changes.
121  *
122  * Revision 1.3  1995/02/16  18:35:09  adam
123  * First use of Zdist library. Search requests are supported.
124  * Present requests are not supported yet.
125  *
126  * Revision 1.2  1995/02/16  13:21:00  adam
127  * Organization of resource files for targets and conversion
128  * language implemented.
129  *
130  * Revision 1.1  1995/02/15  17:45:29  adam
131  * First version of email gateway kernel. Email requests are read
132  * from stdin. The output is transferred to an MTA if 'From' is
133  * found in the header - or stdout if absent. No Z39.50 client is used.
134  *
135  */
136
137 #include <stdio.h>
138 #include <stdlib.h>
139 #include <string.h>
140 #include <assert.h>
141 #include <unistd.h>
142 #include <sys/types.h>
143 #include <sys/time.h>
144 #include <fcntl.h>
145
146 #include <gip.h>
147 #include <strqueue.h>
148 #include "kernel.h"
149
150 FILE *reply_fd = stdout;
151
152 struct gw_kernel_info info;
153
154 static void kernel_events (struct str_queue *queue)
155 {
156     char fifo_client_name[1024];
157     char fifo_server_name[1024];
158     char line_buf[1024];
159     GIP gip;
160     fd_set set_r;
161     int r, gip_fd;
162     struct timeval tv;
163     int timeout;
164     int continuation = 0;
165     int extra_fd;
166     int persist_flag;
167
168     persist_flag = gw_res_bool (info.kernel_res, "gw.persist", 0);
169     timeout = gw_res_int (info.kernel_res, "gw.timeout", 600);
170     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "event loop");
171
172     sprintf (fifo_client_name, "fifo.c.%d", info.userid);
173     sprintf (fifo_server_name, "fifo.s.%d", info.userid);
174
175     gip = gips_initialize (fifo_server_name);
176     gips_open (gip, fifo_client_name);
177     gip_fd = gip_infileno (gip);
178     extra_fd = open (fifo_server_name, O_WRONLY);
179
180     while (1)
181     {
182         FD_ZERO (&set_r);
183         FD_SET (gip_fd, &set_r);
184         tv.tv_sec = timeout;
185         tv.tv_usec = 0;
186
187         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
188         r = select (gip_fd+1, &set_r, NULL, NULL, &tv);
189         if (r == -1)
190         {
191             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "select");
192             exit (1);
193         }
194         if (r == 0)
195         {
196             gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout after %d seconds", 
197                     timeout);
198             if (info.zass && persist_flag)
199                 save_p_state (info.userid);
200             break;
201         }
202         if (FD_ISSET (gip_fd, &set_r))
203         {
204             char command[128], *cp;
205
206             if (!(lgets (command, 127, gip_fd)))
207             {
208                 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unexpected close");
209                 break;
210             }
211             if ((cp = strchr (command, '\n')))
212                 *cp = '\0';
213             gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
214             if (!strcmp (command, "mail"))
215             {
216                 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail");
217                 while (lgets (line_buf, sizeof(line_buf)-1, gip_fd))
218                     str_queue_enq (queue, line_buf);
219                 urp_start (continuation, queue);
220                 if (persist_flag && !continuation)
221                     load_p_state (info.userid); 
222                 r = urp_command (queue);
223                 if (r == 1)                         /* stop? */
224                 {
225                     info.zass = NULL;               /* delete association */
226                     *info.target = 0;               /* indicate no target */
227                     read_kernel_res();              /* reread resources */
228                     if (persist_flag)
229                         del_p_state (info.userid);  /* remove persist file */
230                 }
231                 urp_end ();
232                 while (str_queue_deq (queue, 0, 0))
233                     ;
234             }
235             else if (!strcmp (command, "stop"))
236             {
237                 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
238                 break;
239             }
240             else 
241             {
242                 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
243             }
244             continuation = 1;
245         }
246     }
247     close (extra_fd);
248     gips_close (gip);
249     gips_destroy (gip);
250
251     unlink (fifo_client_name);
252     unlink (fifo_server_name);
253 }
254
255 int main (int argc, char **argv)
256 {
257     struct str_queue *queue;
258
259     info.kernel_res = NULL;
260     info.default_res = "default.res";
261     info.override_res = NULL;
262     *info.target = 0;
263     *info.account = 0;
264     info.lang = NULL;
265     info.bibset = NULL;
266     info.zass = NULL;
267     info.override_portno = NULL;
268     info.override_hostname = NULL;
269     info.databases = NULL;
270     info.database = NULL;
271     info.setno = -1;
272     info.userid = -1;
273 #if USE_FML
274     info.fml = NULL;
275 #endif
276     info.sets = NULL;
277
278     gw_log_init (*argv);
279     info.kernel_res = gw_res_init ();
280     while (--argc > 0)
281     {
282         if (**++argv == '-')
283         {
284             switch (argv[0][1])
285             {
286             case 'H':
287                 fprintf (stderr, "kernel [option..] [resource]\n");
288                 fprintf (stderr, "If no resource file is given");
289                 fprintf (stderr, " default.res is used\n");
290                 fprintf (stderr, "Options:\n");
291                 fprintf (stderr, " -d           Enable debugging log\n");
292                 fprintf (stderr, " -t target    Open target immediately\n");
293                 fprintf (stderr, " -g lang      Set language\n");
294                 fprintf (stderr, " -o resource  Override with resource\n");
295                 fprintf (stderr, " -h host      Override host\n");
296                 fprintf (stderr, " -p port      Override port\n");
297                 fprintf (stderr, " -l log       Set Log file\n");
298                 fprintf (stderr, " -i id        Set IPC userid\n");
299                 exit (1);
300             case 'd':
301                 gw_log_level (GW_LOG_ALL & ~RES_DEBUG);
302                 break;
303             case 'D':
304                 gw_log_level (GW_LOG_ALL);
305                 break;
306             case 't':
307                 if (argv[0][2])
308                     strcpy (info.target, argv[0]+2);
309                 else if (argc > 0)
310                 {
311                     --argc;
312                     strcpy (info.target, *++argv);
313                 }
314                 else
315                 {
316                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing target name");
317                     exit (1);
318                 }
319                 break;
320             case 'g':
321                 if (argv[0][2])
322                     info.lang = argv[0]+2;
323                 else if (argc > 0)
324                 {
325                     --argc;
326                     info.lang = *++argv;
327                 }
328                 else
329                 {
330                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
331                     exit (1);
332                 }
333                 break;
334             case 'o':
335                 if (argv[0][2])
336                     info.override_res = argv[0]+2;
337                 else if (argc > 0)
338                 {
339                     --argc;
340                     info.override_res = *++argv;
341                 }
342                 else
343                 {
344                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing override name");
345                     exit (1);
346                 }
347                 break;
348             case 'p':
349                 if (argv[0][2])
350                     info.override_portno = argv[0]+2;
351                 else if (argc > 0)
352                 {
353                     --argc;
354                     info.override_portno = *++argv;
355                 }
356                 else
357                 {
358                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing portno");
359                     exit (1);
360                 }
361                 break;
362             case 'h':
363                 if (argv[0][2])
364                     info.override_hostname = argv[0]+2;
365                 else if (argc > 0)
366                 {
367                     --argc;
368                     info.override_hostname = *++argv;
369                 }
370                 else
371                 {
372                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing hostname");
373                     exit (1);
374                 }
375                 break;
376             case 'l':
377                 if (argv[0][2])
378                     gw_log_file (GW_LOG_ALL, argv[0]+2);
379                 else if (argc > 0)
380                 {
381                     --argc;
382                     gw_log_file (GW_LOG_ALL, *++argv);
383                 }
384                 else
385                 {
386                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing log filename");
387                     exit (1);
388                 }
389                 break;
390             case 'i':
391                 if (argv[0][2])
392                     info.userid = atoi (argv[0]+2);
393                 else if (argc > 0)
394                 {
395                     --argc;
396                     info.userid = atoi (*++argv);
397                 }
398                 else
399                 {
400                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
401                     exit (1);
402                 }
403                 gw_log_session (info.userid);
404                 break;
405             default:
406                 gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s", *argv);
407                 exit (1);
408             }
409         }
410         else
411             info.default_res = *argv;
412     }
413     if (!(queue = str_queue_mk ()))
414     {
415         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "str_queue_mk");
416         exit (1);
417     }
418     if (info.userid != -1)
419     {
420         read_kernel_res ();
421         kernel_events (queue);
422     }
423     else
424     {
425         char line_buf[512];
426         read_kernel_res ();
427         while (lgets (line_buf, sizeof(line_buf)-1, 0))
428             str_queue_enq (queue, line_buf);
429         urp_start (0, queue);
430         urp_command (queue);
431         urp_end ();
432     }
433     return 0;
434 }
435
436 struct gw_user_set *user_set_add (const char *name, int hits, 
437                                   const char *database, 
438                                   struct ccl_rpn_node *rpn,
439                                   int present_flag,
440                                   const char *search_str)
441 {
442     struct gw_user_set *s;
443
444     s = malloc (sizeof (*s));
445     assert (s);
446
447     s->name = gw_strdup (name);
448     s->hits = hits;
449     s->database = gw_strdup (database);
450     s->rpn = rpn;
451     s->present_flag = present_flag;
452     s->search_str = gw_strdup (search_str);
453     s->prev = info.sets;
454     info.sets = s;
455     return s;
456 }
457
458 void user_set_init (void)
459 {
460     struct gw_user_set *s, *s1;
461
462     for (s = info.sets; s; s = s1)
463     {
464         free (s->name);
465         free (s->database);
466         ccl_rpn_delete (s->rpn);
467         s1 = s->prev;
468         free (s);
469     }
470     info.sets = NULL;
471 }
472
473 struct gw_user_set *user_set_search (const char *name)
474 {
475     struct gw_user_set *s;
476
477     if (!name)
478         return info.sets;
479     for (s = info.sets; s; s = s->prev)
480         if (!strcmp (s->name, name))
481             return s;
482     return NULL;
483 }
484
485 #if USE_FML
486 static void fml_inf_write (int ch)
487 {
488     putc (ch, reply_fd);
489 }
490 static FILE *fml_inf;
491
492 static int fml_inf_read (void)
493 {
494     return getc (fml_inf);
495 }
496 #endif
497
498 void read_kernel_res (void)
499 {
500     char path_prefix[128];
501     char fname[160];
502     const char *v;
503     char *cp;
504     char resource_name[256];
505
506     user_set_init ();
507
508     if (info.bibset)
509         ccl_qual_rm (&info.bibset);
510     info.bibset = ccl_qual_mk ();
511
512     if (info.kernel_res)
513         gw_res_close (info.kernel_res);
514     info.kernel_res = gw_res_init ();
515
516     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, default %s",
517             info.default_res);
518     if (*info.target)
519         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, target %s",
520                 info.target);
521     if (info.lang)
522         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
523                 info.lang);
524
525     /* read default resources. These should exist */
526     if (gw_res_merge (info.kernel_res, info.default_res))
527     {
528         gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
529                 info.default_res);
530         return;
531     }
532     strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
533     
534     /* fetch target definitions (if defined at all) */
535     if (*info.target)
536     {
537         sprintf (resource_name, "gw.target.%s", info.target);
538         v = gw_res_get (info.kernel_res, resource_name, NULL);
539         if (v)
540         {
541             sprintf (fname, "%s/%s", path_prefix, v);
542             gw_res_merge (info.kernel_res, fname);
543         }
544     }
545     /* fetch user definitions (if user-id is specified) */
546     if (info.userid >= 0)
547     {
548         sprintf (fname, "%s/user.%d.r", path_prefix, info.userid);
549         gw_res_merge (info.kernel_res, fname);
550     }
551     /* fetch language definitions (if specified at all) */
552     v = gw_res_get (info.kernel_res, "gw.language", info.lang);
553     if (v)
554     {
555         sprintf (resource_name, "gw.lang.%s", v);
556         v = gw_res_get (info.kernel_res, resource_name, NULL);
557         if (v)
558         {
559             sprintf (fname, "%s/%s", path_prefix, v);
560             gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Reading language resources %s",
561                     fname);
562             gw_res_merge (info.kernel_res, fname);
563         }
564     }
565     /* fetch overriding resources from file (if specified) */
566     if (info.override_res)
567     {
568         sprintf (fname, "%s/%s", path_prefix, info.override_res);
569         gw_res_merge (info.kernel_res, fname);        
570     }
571
572     /* read bibset definition for ccl */
573     v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
574     if (v)
575     {
576         FILE *bib_inf;
577
578         sprintf (fname, "%s/%s", path_prefix, v);
579         bib_inf = fopen (fname, "r");
580         if (!bib_inf)
581             gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", fname);
582         else
583         {
584             gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", fname);
585             ccl_qual_file (info.bibset, bib_inf);
586             fclose (bib_inf);
587         }
588     }
589
590     /* determine host name and port no */
591     sprintf (resource_name, "gw.target.%s", info.target);
592     if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
593     {
594         /* target is specified, and there is no sub-resource for it... */
595         const char *split;
596
597         if ((split = strchr (info.target, ':')))
598         {
599             memcpy (info.hostname, info.target, split-info.target);
600             info.hostname[split-info.target] = '\0';
601             info.port = atoi (split+1);
602         }
603         else
604         {
605             strcpy (info.hostname, info.target);
606             info.port = gw_res_int (info.kernel_res, "gw.portno", 210);
607         }
608     }
609     else
610     {   /* resources gw.hostname and gw.portno will be used */
611         strncpy (info.hostname, gw_res_get (info.kernel_res,
612                                             "gw.hostname", "localhost"),
613                  sizeof(info.hostname)-1);
614         info.port = gw_res_int (info.kernel_res, "gw.portno", 210);
615         strcpy (info.account, gw_res_get (info.kernel_res, "gw.account", ""));
616     }
617     /* set info.databases (all available databases for target) */
618     /* set info.database (first database for target) */
619     if (info.databases)
620         free (info.databases);
621     if (info.database)
622         free (info.database);
623     v = gw_res_get (info.kernel_res, "gw.databases", "");
624     info.databases = gw_strdup (v);
625     for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
626         *cp = ',';
627     v = gw_res_get (info.kernel_res, "gw.database", "");
628     if (*v == '\0' && *info.databases)
629     {
630         int len;
631         cp = strchr (info.databases, ',');
632         
633         len = cp ? (cp-info.databases) : strlen (info.databases);
634         info.database = malloc (len+1);
635         assert (info.database);
636         memcpy (info.database, info.databases, len);
637         info.database[len] = '\0';
638     }
639     else
640     {
641         info.database = gw_strdup (v);
642         for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
643             *cp = ',';
644     }
645
646     /* the port no can be explicitly overridden by a command line option */
647     if (info.override_portno)
648         info.port = atoi (info.override_portno);
649
650     /* the hostname can be explicitly overridden by a command line option */
651     if (info.override_hostname)
652         strncpy (info.hostname, info.override_hostname,
653                  sizeof(info.hostname)-1);
654
655     ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
656     ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
657     ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
658     ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
659
660     /* determine if more than one result-set names is supported */
661     if (gw_res_bool (info.kernel_res, "gw.result.set", 1))
662         info.setno = 0;
663     else
664         info.setno = -1;
665 #if USE_FML
666     if (!info.fml)
667     {
668         v = gw_res_get (info.kernel_res, "gw.fml", "default.fml");    
669         sprintf (fname, "%s/%s", path_prefix, v);
670         fml_inf = fopen (fname, "r");
671         if (!fml_inf)
672             gw_log (GW_LOG_WARN, KERNEL_LOG,
673                     "cannot open fml script %s", fname);
674         else
675         {
676             info.fml = fml_open ();
677             info.fml->read_func = fml_inf_read;
678             info.fml->write_func = fml_inf_write;
679             fml_preprocess (info.fml);
680             fml_exec (info.fml);
681             fclose (fml_inf);
682         }
683     }
684 #endif
685 }