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