1 /* Gateway kernel - User Request Processor
5 * Revision 1.32 1995/05/03 07:37:46 adam
6 * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
7 * are used when possible.
9 * Revision 1.31 1995/05/01 12:43:38 adam
10 * First work on resource monitor program.
12 * Revision 1.30 1995/04/20 16:10:47 adam
13 * Modified to work with non-blocking zass-api. Not using non-blocking
16 * Revision 1.29 1995/04/19 16:01:58 adam
17 * Some hacks to get the FIFO communication work!! Isn't reliable.
18 * Resource gw.account added - default account info.
20 * Revision 1.28 1995/04/19 13:19:09 adam
21 * New command: account - for authentication.
23 * Revision 1.27 1995/04/19 10:46:19 adam
24 * Persistency works much better now. New command: status - history-like
26 * Revision 1.26 1995/04/19 07:31:12 adam
27 * First work on Z39.50 persistence.
29 * Revision 1.25 1995/04/17 09:34:33 adam
30 * Timeout (idletime) adjustable. Minor changes in kernel.
32 * Revision 1.24 1995/03/28 11:42:35 adam
33 * First use of string-queue utility.
35 * Revision 1.23 1995/03/28 08:01:28 adam
36 * FIFO existence is used to test for a running kernel.
38 * Revision 1.22 1995/03/27 12:51:05 adam
39 * New log level in use: GW_LOG_ERRNO.
41 * Revision 1.21 1995/03/27 08:24:04 adam
42 * First use of gip interface and gw-db.
43 * First work on eti program.
45 * Revision 1.20 1995/03/03 17:19:17 adam
46 * Smarter presentation. Bug fix in email header interpretation.
48 * Revision 1.19 1995/03/02 09:32:11 adam
49 * New presentation formats. f0=full, f1=brief, f2=mid
51 * Revision 1.18 1995/03/01 14:32:26 adam
52 * Better diagnostics. Default is, that only one database selected when
55 * Revision 1.17 1995/02/28 13:16:26 adam
56 * Configurable From: added.
58 * Revision 1.16 1995/02/23 10:08:20 adam
59 * Added logging of all user commands.
61 * Revision 1.15 1995/02/23 08:32:17 adam
64 * Revision 1.13 1995/02/22 16:54:42 adam
65 * Qualifiers of LOC target updated. More logging messages.
67 * Revision 1.12 1995/02/22 15:51:51 adam
68 * Bug fix: swap of parameter number and offset in function present.
70 * Revision 1.11 1995/02/22 15:22:33 adam
71 * Much more checking of run-time state. Show command never retrieves
72 * more records than indicated by the previous search request. Help
73 * command available. The maximum number of records retrieved can be
76 * Revision 1.10 1995/02/22 08:51:35 adam
77 * Output function can be customized in fml, which is used to print
78 * the reply to reply_fd.
80 * Revision 1.9 1995/02/21 17:46:21 adam
83 * Revision 1.8 1995/02/21 12:12:00 adam
84 * Diagnostic record with error info. observed.
86 * Revision 1.7 1995/02/20 21:16:20 adam
87 * FML support. Bug fixes. Profile for drewdb.
89 * Revision 1.6 1995/02/17 14:41:14 quinn
90 * Added simple display of records.
92 * Revision 1.5 1995/02/17 14:22:13 adam
93 * First steps of CCL show command. Not finished yet.
95 * Revision 1.4 1995/02/17 09:08:36 adam
96 * Reply with subject. CCL base command implemented.
98 * Revision 1.3 1995/02/16 18:35:09 adam
99 * First use of Zdist library. Search requests are supported.
100 * Present requests are not supported yet.
102 * Revision 1.2 1995/02/16 13:21:00 adam
103 * Organization of resource files for targets and conversion
104 * language implemented.
106 * Revision 1.1 1995/02/15 17:45:30 adam
107 * First version of email gateway kernel. Email requests are read
108 * from stdin. The output is transferred to an MTA if 'From' is
109 * found in the header - or stdout if absent. No Z39.50 client is used.
115 stop/continue commands
116 info/status (other name?)
118 better persistence diagnostics
119 show - position continuation
120 resource gw.path - use chdir call instead
123 automatic information about target-aliases: name, query-support, etc.
136 #include <strqueue.h>
139 static char line_buf[LINE_MAX+1];
141 static void put_esc_str (const char *s)
147 int reopen_target (void)
151 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
152 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reopen_target");
153 info.zass = zass_open (info.hostname, info.port, NULL, /* complete */
154 *info.account ? info.account : NULL);
157 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Cannot connect to target %s:%d",
158 info.hostname, info.port);
159 fprintf (reply_fd, "%s %s:%d\n",
160 gw_res_get (info.kernel_res, "gw.err.connect",
161 "Cannot connect to target"),
162 info.hostname, info.port);
165 v = gw_res_get (info.kernel_res, "gw.description", NULL);
169 fprintf (reply_fd, "\n");
171 fprintf (reply_fd, "%s %s:%d\n",
172 gw_res_get (info.kernel_res, "gw.msg.connect",
173 "Connection established to"),
174 info.hostname, info.port);
176 fprintf (reply_fd, "%s:\n%s\n",
177 gw_res_get (info.kernel_res, "gw.msg.databases",
178 "Available databases"),
181 fprintf (reply_fd, "%s:\n%s\n",
182 gw_res_get (info.kernel_res, "gw.msg.database",
183 "Selected databases"),
186 fprintf (reply_fd, "set=%d\n", info.setno);
188 fprintf (reply_fd, "set=Default\n");
193 static struct command_word {
195 char *resource_suffix;
203 { "continue", "continue" },
204 { "status", "status" },
206 { "target", "target" },
207 { "cancel", "cancel" },
208 { "account", "account" },
212 static int command_search (struct command_word *tab, struct ccl_token *cmd,
213 const char *resource_prefix)
217 assert (resource_prefix);
220 while (tab->default_value)
222 char *cp, command_names[60];
223 char resource_name[60];
226 sprintf (resource_name, "%s%s", resource_prefix,
227 tab->resource_suffix);
228 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
230 strcpy (command_names, v);
236 if ((split = strchr (cp, ' ')))
238 if (cmd->len == strlen(cp) &&
239 !memcmp (cmd->name, cp, cmd->len))
251 static struct error_no_struct {
254 } error_ccl_tab[] = {
256 { CCL_ERR_TERM_EXPECTED, "term.expected" },
257 { CCL_ERR_RP_EXPECTED, "rp.expected" },
258 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
259 { CCL_ERR_OP_EXPECTED, "op.expected" },
260 { CCL_ERR_BAD_RP, "bad.rp" },
261 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
262 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
263 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
264 { CCL_ERR_BAD_RELATION, "bad.relation" },
265 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
266 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
267 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
271 static char *error_no_search (struct error_no_struct *tab, int no)
273 struct error_no_struct *p = tab;
274 while (p->resource_name)
277 return p->resource_name;
283 static int email_header (struct str_queue *sq,
284 char *from_str, char *subject_str)
288 while (str_queue_deq (sq, line_buf, LINE_MAX))
290 if (line_buf[0] == '\n')
292 if (memcmp (line_buf, "From ", 5) == 0)
293 sscanf (line_buf+4, "%s", from_str);
294 if (memcmp (line_buf, "Subject: ", 9) == 0 &&
295 sscanf (line_buf+9, "%s", subject_str+1) == 1)
296 strcpy (subject_str, line_buf+9);
301 static void help_general (void)
303 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
304 "Commands available in this service:\n"));
307 static int exec_help (struct ccl_token *list)
309 static char *sep = "-------------------------------\\n";
313 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
314 "target <name> - selects a given target\n"));
317 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
318 "base <base>.. - selects databases\n"));
321 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
322 "find <query> - performs a search request\n"));
325 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
326 "show <spec> - retrieves and displays "
331 static void display_diag_error (int code, const char *addinfo)
335 sprintf (str, "gw.bib1.diag.%d", code);
336 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
337 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
340 gw_res_get (info.kernel_res, str, ""), addinfo);
343 static int exec_find (struct ccl_token *list, const char *search_str)
345 const struct zass_searchent *p;
346 struct gw_user_set *us;
349 struct ccl_rpn_node *rpn;
353 if (info.setno == -1)
354 strcpy (setname, "Default");
356 sprintf (setname, "%d", info.setno);
357 info.next_position = 1;
360 rpn = ccl_find (info.bibset, list, &error, &pos);
363 const char *v = NULL, *n;
366 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
368 n = error_no_search (error_ccl_tab, error);
371 sprintf (name, "gw.err.%s", n);
372 v = gw_res_get (info.kernel_res, name, NULL);
375 v = ccl_err_msg (error);
376 fprintf (reply_fd, "%s\n", v);
379 ccl_pr_tree (rpn, reply_fd);
380 fprintf (reply_fd, "\n");
382 if (!*info.database )
384 fprintf (reply_fd, "%s\n",
385 gw_res_get (info.kernel_res, "gw.err.no.database",
386 "You must select database"));
389 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
392 fprintf (reply_fd, "Searching in database %s\n", info.database);
393 p = zass_p_search (info.zass, rpn, setname, info.database, info.sets);
396 fprintf (reply_fd, "Search fail\n");
399 if (p->errcode != -1)
401 display_diag_error (p->errcode, p->errstring);
404 fprintf (reply_fd, "%d %s\n", p->num,
405 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
406 us = user_set_add (setname, p->num, info.database, rpn, 1, search_str);
407 fprintf (reply_fd, "Result-set %s created\n", setname);
411 static int exec_account (struct ccl_token *list)
413 if (list->kind != CCL_TOK_EOL)
416 memcpy (info.account, list->name, len);
417 info.target[len] = '\0';
420 *info.account = '\0';
424 static int exec_target (struct ccl_token *list)
427 if (list->kind == CCL_TOK_EOL)
430 memcpy (info.target, list->name, len);
431 info.target [len] = '\0';
434 return reopen_target ();
437 static void exec_status_r (struct gw_user_set *sp)
441 exec_status_r (sp->prev);
442 fprintf (reply_fd, "%6s %7d %12.12s %.50s\n", sp->name, sp->hits,
443 sp->database, sp->search_str);
446 static int exec_status (struct ccl_token *list)
448 fprintf (reply_fd, " Name Hits Database Find\n");
449 exec_status_r (info.sets);
453 static int exec_base (struct ccl_token *list)
455 struct ccl_token *li = list;
459 if (list->kind == CCL_TOK_EOL)
461 free (info.database);
462 while (li->kind != CCL_TOK_EOL)
466 if (li->kind == CCL_TOK_COMMA)
469 info.database = malloc (len);
470 assert (info.database );
473 while (li->kind != CCL_TOK_EOL)
475 memcpy (info.database+len, li->name, li->len);
477 info.database[len++] = ',';
479 if (li->kind == CCL_TOK_COMMA)
482 info.database[len-1] = '\0';
486 struct command_word show_tab [] =
493 static void present (const char *set, int offset, int number,
494 struct ccl_token *format_token)
496 const struct zass_presentent *zp;
501 max_number = gw_res_int (info.kernel_res, "gw.max.show", 200);
502 if (number > max_number)
504 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
505 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
507 zp = zass_p_present(info.zass, (char *) set, offset, number);
513 int record_log_fd = -1;
514 const char *record_log_name;
516 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
520 sprintf (path, "%s/%s", gw_res_get (info.kernel_res,
523 record_log_fd = open (path, O_WRONLY|O_CREAT|O_APPEND, 0666);
524 if (record_log_fd == -1)
525 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s", path);
527 fprintf (reply_fd, gw_res_get (info.kernel_res,
529 "Got %d records from set %s"),
531 fprintf (reply_fd, "\n");
532 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
536 const char *arg_ar[5];
538 fprintf (reply_fd, "--- %d/%d ---\n",
539 i+offset, offset+zp->num-1);
540 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
542 if (pp->which == ZASS_REC_DIAG)
544 display_diag_error (pp->errcode, pp->errstring);
547 else if (pp->which != ZASS_REC_USMARC)
549 fprintf (reply_fd, "Unknown record kind %d\n",
554 if (record_log_fd != -1)
555 write (record_log_fd, pp->record, strlen(pp->record));
556 rec = iso2709_cvt (pp->record);
562 len = format_token->len;
563 if (len >= sizeof(format_str))
564 len = sizeof(format_str)-1;
565 memcpy (format_str, format_token->name, len);
566 format_str[len] = '\0';
568 if (info.fml && format_token &&
569 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
570 || !strcmp(format_str, "2")))
573 arg_ar[1] = format_str;
574 arg_ar[2] = " \\list";
575 arg_ar[3] = marc_to_str (info.fml, rec);
577 fml_exec_call_argv (info.fml, arg_ar);
580 iso2709_display (rec, reply_fd);
582 iso2709_display (rec, reply_fd);
587 fprintf (reply_fd, "Not a MARC record\n");
589 if (record_log_fd != -1)
590 close (record_log_fd);
594 static int exec_show (struct ccl_token *list)
597 struct ccl_token *set_token = NULL;
598 struct ccl_token *format_token = NULL;
599 struct ccl_token *li = list;
600 int no_of_present = 0;
603 while (li->kind != CCL_TOK_EOL)
606 if (li->next->kind == CCL_TOK_EQ)
608 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
615 modifier_no = command_search (show_tab, li, "ccl.token.");
618 fprintf (reply_fd, "Unknown modifier in show\n");
622 if (modifier_no == 1) /* f = <name> ? */
624 else if (modifier_no == 2) /* p = <name> ? */
626 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
627 && li->next->kind == CCL_TOK_MINUS
628 && li->next->next != CCL_TOK_EOL)
634 fprintf (reply_fd, "%s\n", "Missing token after '='");
643 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
646 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
650 while (li->kind != CCL_TOK_EOL)
656 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
658 modifier_no = command_search (show_tab, li, "ccl.token.");
660 if (modifier_no == 2) /* p = <name> ? */
662 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
663 && li->next->kind == CCL_TOK_MINUS
664 && li->next->next != CCL_TOK_EOL)
667 memcpy (tmp_str, li->name, len);
668 tmp_str [len] = '\0';
669 offset = atoi (tmp_str);
673 memcpy (tmp_str, li->name, len);
674 tmp_str [len] = '\0';
675 number = atoi (tmp_str) - offset + 1;
680 memcpy (tmp_str, li->name, len);
681 tmp_str [len] = '\0';
682 offset = atoi (tmp_str);
691 memcpy (tmp_str, li->name, len);
693 number = atoi (tmp_str);
697 if (offset > 0 && number > 0)
699 struct gw_user_set *us;
703 len = set_token->len;
704 memcpy (tmp_str, set_token->name, len);
706 us = user_set_search (tmp_str);
709 us = user_set_search (NULL);
710 if (us && us->hits != -1) /* proper result-set? */
712 if (offset <= us->hits)
714 if (offset+number-1 > us->hits)
715 number = us->hits - offset+1;
716 present (us->name, offset, number, format_token);
719 else if (!no_of_present) /* display error message once! */
721 fprintf (reply_fd, "%s\n",
722 gw_res_get (info.kernel_res, "gw.err.no.set",
723 "No result-set generated"));
728 if (!no_of_present) /* no records shown so far? */
730 struct gw_user_set *us;
733 us = user_set_search (NULL);
734 if (us && us->hits != -1) /* proper result-set? */
736 default_show = gw_res_int (info.kernel_res, "gw.default.show", 20);
737 if (us->hits >= info.next_position + default_show)
739 present (us->name, info.next_position, default_show,
741 info.next_position += default_show;
743 else if (us->hits >= info.next_position)
745 present (us->name, info.next_position,
746 us->hits - info.next_position + 1,
748 info.next_position = us->hits + 1;
751 else /* display error message */
753 fprintf (reply_fd, "%s\n",
754 gw_res_get (info.kernel_res, "gw.err.no.set",
755 "No result-set generated"));
763 * exec_command: parse and execute ccl command in str.
764 * str: ccl command string
765 * stop_flag pointer to integer. On completion the integer
766 * is 1 (stop) or 2 (continue); 0 (other command)
767 * return: 0 success; non-zero otherwise
769 static int exec_command (const char *str, int *stop_flag)
771 struct ccl_token *cmd = ccl_tokenize (str);
775 if (cmd->kind != CCL_TOK_EOL &&
776 (no = command_search (command_tab, cmd, "ccl.command.")))
778 if (!info.zass && no != 9 && no != 4 && no != 11 && no != 7)
780 fprintf (reply_fd, "\n> %s\n", str);
781 if (!info.zass && (no == 1 || no == 2 || no == 3))
783 fprintf (reply_fd, "%s\n",
784 gw_res_get (info.kernel_res, "gw.err.no.target",
785 "No connection established - "
790 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
791 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
792 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
793 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
798 return exec_find (cmd->next, str);
800 return exec_show (cmd->next);
802 return exec_base (cmd->next);
804 return exec_help (cmd->next);
805 case 6: /* continue */
809 return exec_status (cmd->next);
817 return exec_target (cmd->next);
819 return exec_account (cmd->next);
821 fprintf (reply_fd, "%s\n",
822 gw_res_get (info.kernel_res, "gw.err.unimplemented",
823 "Not implemented yet"));
828 fprintf (reply_fd, "\n> %s\n", str);
829 fprintf (reply_fd, " ^ %s\n",
830 gw_res_get (info.kernel_res, "gw.err.unknown.command",
832 "Use help to see list of commands"));
837 int urp_start (int continuation, struct str_queue *queue)
839 char subject_str[128];
842 info.reply_fname = NULL;
844 if (email_header (queue, info.from_str, subject_str))
846 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
849 tty_init (stdout, 40, 70);
852 info.reply_fname = tempnam (gw_res_get (info.kernel_res,
853 "gw.reply.tmp.dir", NULL),
854 gw_res_get (info.kernel_res,
855 "gw.reply.tmp.prefix", "gwr"));
857 reply_fd = fopen (info.reply_fname, "w");
860 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
864 tty_init (reply_fd, 0, 0);
865 fprintf (reply_fd, "From: %s\n",
866 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
867 fprintf (reply_fd, "Subject: ");
869 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
871 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
874 fprintf (reply_fd, "\n");
875 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", info.from_str);
878 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
879 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
880 "Email->Z39.50 gateway"));
882 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
888 int urp_command (struct str_queue *queue)
893 while (str_queue_deq (queue, line_buf, LINE_MAX))
895 if (line_buf[0] == '\n')
898 while (str_queue_deq (queue, 0, 0))
904 if ((cp = strchr (line_buf, '\n')))
906 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
907 if (isalpha (line_buf[0]))
909 exec_command (line_buf, &stop_flag);
912 info.command_no++; /* prevent help! */
913 while (str_queue_deq (queue, line_buf, LINE_MAX))
925 if (!info.command_no)
927 fprintf (reply_fd, "%s\n",
928 gw_res_get (info.kernel_res, "gw.err.nullbody", "No body"));
937 assert (info.reply_fname);
941 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
942 "/usr/lib/sendmail");
943 sprintf (cmd, "%s %s < %s", mta, info.from_str, info.reply_fname);
945 mta_code = system (cmd);
947 gw_log (GW_LOG_FATAL, KERNEL_LOG,
948 "Reply '%s' got exit code %d", cmd, mta_code);
950 unlink (info.reply_fname);
951 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", info.from_str);