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