Email gateway obeys 'Reply-To:' in header.
[egate.git] / kernel / main.c
index 9398e2b..f1f1694 100644 (file)
@@ -1,8 +1,100 @@
+/*
+ * Copyright (c) 1995, the EUROPAGATE consortium (see below).
+ *
+ * The EUROPAGATE consortium members are:
+ *
+ *    University College Dublin
+ *    Danmarks Teknologiske Videnscenter
+ *    An Chomhairle Leabharlanna
+ *    Consejo Superior de Investigaciones Cientificas
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation, in whole or in part, for any purpose, is hereby granted,
+ * provided that:
+ *
+ * 1. This copyright and permission notice appear in all copies of the
+ * software and its documentation. Notices of copyright or attribution
+ * which appear at the beginning of any file must remain unchanged.
+ *
+ * 2. The names of EUROPAGATE or the project partners may not be used to
+ * endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * 3. Users of this software (implementors and gateway operators) agree to
+ * inform the EUROPAGATE consortium of their use of the software. This
+ * information will be used to evaluate the EUROPAGATE project and the
+ * software, and to plan further developments. The consortium may use
+ * the information in later publications.
+ * 
+ * 4. Users of this software agree to make their best efforts, when
+ * documenting their use of the software, to acknowledge the EUROPAGATE
+ * consortium, and the role played by the software in their work.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
+ * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
+ * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
+ * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
+ * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
 /* Gateway kernel - Main
  * Europagate, 1995
  *
  * $Log: main.c,v $
- * Revision 1.15  1995/04/17 09:34:30  adam
+ * Revision 1.30  1996/02/12 10:04:54  adam
+ * The gateway doesn't try to reconnect if it is already known that
+ * it will fail (connect_fail flag introduced).
+ *
+ * Revision 1.29  1995/07/28  10:51:08  adam
+ * Bug fix: account from previous session had effect when new target command
+ * was executed.
+ *
+ * Revision 1.28  1995/07/03  08:20:15  adam
+ * More help information and better diagnostics.
+ *
+ * Revision 1.27  1995/05/19  13:25:59  adam
+ * Bug fixes. Better command line options.
+ *
+ * Revision 1.26  1995/05/18  12:03:08  adam
+ * Bug fixes and minor improvements.
+ *
+ * Revision 1.25  1995/05/16  09:40:42  adam
+ * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
+ *
+ * Revision 1.24  1995/05/04  10:40:07  adam
+ * More work on Def-settings.
+ *
+ * Revision 1.23  1995/05/03  16:34:18  adam
+ * CCL def command, i.e. user definitions - saved as resource files.
+ *
+ * Revision 1.22  1995/05/03  07:37:39  adam
+ * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
+ * are used when possible.
+ *
+ * Revision 1.21  1995/05/01  16:26:56  adam
+ * More work on resource monitor.
+ *
+ * Revision 1.20  1995/05/01  12:43:32  adam
+ * First work on resource monitor program.
+ *
+ * Revision 1.19  1995/04/19  16:01:58  adam
+ * Some hacks to get the FIFO communication work!! Isn't reliable.
+ * Resource gw.account added - default account info.
+ *
+ * Revision 1.18  1995/04/19  13:19:09  adam
+ * New command: account - for authentication.
+ *
+ * Revision 1.17  1995/04/19  10:46:18  adam
+ * Persistency works much better now. New command: status - history-like
+ *
+ * Revision 1.16  1995/04/19  07:31:07  adam
+ * First work on Z39.50 persistence.
+ *
+ * Revision 1.15  1995/04/17  09:34:30  adam
  * Timeout (idletime) adjustable. Minor changes in kernel.
  *
  * Revision 1.14  1995/03/28  11:42:34  adam
@@ -73,7 +165,7 @@ FILE *reply_fd = stdout;
 
 struct gw_kernel_info info;
 
-static void kernel_events (struct str_queue *queue, int userid)
+static void kernel_events (struct str_queue *queue)
 {
     char fifo_client_name[1024];
     char fifo_server_name[1024];
@@ -84,22 +176,30 @@ static void kernel_events (struct str_queue *queue, int userid)
     struct timeval tv;
     int timeout;
     int continuation = 0;
+    int extra_fd;
+    int persist_flag;
+    int stop_flag = 0;
 
-    timeout = atoi(gw_res_get (info.kernel_res, "gw.timeout", "600"));
-    gw_log (GW_LOG_DEBUG, KERNEL_LOG, "kernel event loop");
+    persist_flag = gw_res_bool (info.kernel_res, "gw.persist", 0);
+    timeout = gw_res_int (info.kernel_res, "gw.timeout", 600);
+    gw_log (GW_LOG_DEBUG, KERNEL_LOG, "event loop");
 
-    sprintf (fifo_client_name, "fifo.c.%d", userid);
-    sprintf (fifo_server_name, "fifo.s.%d", userid);
+    sprintf (fifo_client_name, "fifo.c.%d", info.userid);
+    sprintf (fifo_server_name, "fifo.s.%d", info.userid);
 
     gip = gips_initialize (fifo_server_name);
-    gips_open (gip, fifo_client_name);
+    gips_open (gip, fifo_client_name, 1);
     gip_fd = gip_infileno (gip);
+    extra_fd = open (fifo_server_name, O_WRONLY);
 
     while (1)
     {
         FD_ZERO (&set_r);
        FD_SET (gip_fd, &set_r);
-        tv.tv_sec = timeout;
+        if (stop_flag)
+            tv.tv_sec = 1;
+        else
+            tv.tv_sec = timeout;
        tv.tv_usec = 0;
 
        gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
@@ -111,8 +211,13 @@ static void kernel_events (struct str_queue *queue, int userid)
        }
        if (r == 0)
        {
-           gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout after %d seconds", 
-                   timeout);
+            if (stop_flag)
+                gw_log (GW_LOG_STAT, KERNEL_LOG, "Stopping");
+            else
+                gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout after %d seconds", 
+                        timeout);
+           if (info.zass && persist_flag)
+               save_p_state (info.userid);
            break;
        }
        if (FD_ISSET (gip_fd, &set_r))
@@ -129,11 +234,21 @@ static void kernel_events (struct str_queue *queue, int userid)
            gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
             if (!strcmp (command, "mail"))
            {
-               gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail...\n");
+               gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail");
                 while (lgets (line_buf, sizeof(line_buf)-1, gip_fd))
                     str_queue_enq (queue, line_buf);
                urp_start (continuation, queue);
-               urp_command (queue);
+                if (persist_flag && !continuation)
+                   load_p_state (info.userid); 
+               r = urp_command (queue);
+               if (r == 1)                         /* stop? */
+                {
+                    info.zass = NULL;               /* delete association */
+                    *info.target = 0;               /* indicate no target */
+                    read_kernel_res();              /* reread resources */
+                    if (persist_flag)
+                        del_p_state (info.userid);  /* remove persist file */
+                }
                urp_end ();
                 while (str_queue_deq (queue, 0, 0))
                     ;
@@ -141,30 +256,30 @@ static void kernel_events (struct str_queue *queue, int userid)
            else if (!strcmp (command, "stop"))
            {
                 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
-               break;
+                while (lgets (line_buf, sizeof(line_buf)-1, gip_fd))
+                    ;
+                break;
            }
            else 
-           {
                 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
-           }
            continuation = 1;
        }
     }
+    close (extra_fd);
     gips_close (gip);
     gips_destroy (gip);
-    unlink (fifo_client_name);
-    unlink (fifo_server_name);
 }
 
 int main (int argc, char **argv)
 {
-    int userid = -1;
     struct str_queue *queue;
 
     info.kernel_res = NULL;
     info.default_res = "default.res";
     info.override_res = NULL;
     *info.target = 0;
+    *info.account = 0;
+    info.account_in_session = 1;
     info.lang = NULL;
     info.bibset = NULL;
     info.zass = NULL;
@@ -172,6 +287,9 @@ int main (int argc, char **argv)
     info.override_hostname = NULL;
     info.databases = NULL;
     info.database = NULL;
+    info.setno = -1;
+    info.userid = -1;
+    info.connect_failed = 0;
 #if USE_FML
     info.fml = NULL;
 #endif
@@ -186,8 +304,8 @@ int main (int argc, char **argv)
             switch (argv[0][1])
             {
             case 'H':
-                fprintf (stderr, "kernel [option..] [resource]\n");
-                fprintf (stderr, "If no resource file is given");
+                fprintf (stderr, "kernel [options] [resourceFile]\n");
+                fprintf (stderr, "If no resource file is specified");
                 fprintf (stderr, " default.res is used\n");
                 fprintf (stderr, "Options:\n");
                 fprintf (stderr, " -d           Enable debugging log\n");
@@ -200,6 +318,9 @@ int main (int argc, char **argv)
                 fprintf (stderr, " -i id        Set IPC userid\n");
                 exit (1);
             case 'd':
+                gw_log_level (GW_LOG_ALL & ~RES_DEBUG);
+                break;
+            case 'D':
                 gw_log_level (GW_LOG_ALL);
                 break;
             case 't':
@@ -288,37 +409,42 @@ int main (int argc, char **argv)
                 break;
            case 'i':
                 if (argv[0][2])
-                   userid = atoi (argv[0]+2);
+                   info.userid = atoi (argv[0]+2);
                 else if (argc > 0)
                 {
                     --argc;
-                   userid = atoi (*++argv);
+                   info.userid = atoi (*++argv);
                 }
                 else
                 {
                     gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
                    exit (1);
                }
+                gw_log_session (info.userid);
                break;
             default:
-                gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s", *argv);
+                gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s;"
+                        " use -H for help", *argv);
                 exit (1);
             }
         }
         else
             info.default_res = *argv;
     }
-    read_kernel_res ();
     if (!(queue = str_queue_mk ()))
     {
         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "str_queue_mk");
         exit (1);
     }
-    if (userid != -1)
-        kernel_events (queue, userid);
+    if (info.userid != -1)
+    {
+       read_kernel_res ();
+        kernel_events (queue);
+    }
     else
     {
         char line_buf[512];
+        read_kernel_res ();
         while (lgets (line_buf, sizeof(line_buf)-1, 0))
             str_queue_enq (queue, line_buf);
        urp_start (0, queue);
@@ -328,7 +454,11 @@ int main (int argc, char **argv)
     return 0;
 }
 
-struct gw_user_set *user_set_add (const char *name, int hits)
+struct gw_user_set *user_set_add (const char *name, int hits, 
+                                 const char *database, 
+                                 struct ccl_rpn_node *rpn,
+                                 int present_flag,
+                                  const char *search_str)
 {
     struct gw_user_set *s;
 
@@ -337,6 +467,10 @@ struct gw_user_set *user_set_add (const char *name, int hits)
 
     s->name = gw_strdup (name);
     s->hits = hits;
+    s->database = gw_strdup (database);
+    s->rpn = rpn;
+    s->present_flag = present_flag;
+    s->search_str = gw_strdup (search_str);
     s->prev = info.sets;
     info.sets = s;
     return s;
@@ -349,6 +483,8 @@ void user_set_init (void)
     for (s = info.sets; s; s = s1)
     {
         free (s->name);
+       free (s->database);
+       ccl_rpn_delete (s->rpn);
         s1 = s->prev;
         free (s);
     }
@@ -382,8 +518,6 @@ static int fml_inf_read (void)
 
 void read_kernel_res (void)
 {
-    char path_prefix[128];
-    char fname[160];
     const char *v;
     char *cp;
     char resource_name[256];
@@ -407,78 +541,92 @@ void read_kernel_res (void)
         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
                 info.lang);
 
+    /* read default resources. These should exist */
     if (gw_res_merge (info.kernel_res, info.default_res))
     {
         gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
                 info.default_res);
         return;
     }
-    strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
-    
+    /* fetch target definitions (if defined at all) */
     if (*info.target)
     {
         sprintf (resource_name, "gw.target.%s", info.target);
         v = gw_res_get (info.kernel_res, resource_name, NULL);
         if (v)
-        {
-            sprintf (fname, "%s/%s", path_prefix, v);
-            gw_res_merge (info.kernel_res, fname);
-        }
+            gw_res_merge (info.kernel_res, v);
     }
-    if (info.lang)
+    /* fetch user definitions (if user-id is specified) */
+    if (info.userid >= 0)
+    {
+        char fname[250];
+        sprintf (fname, "user.%d.r", info.userid);
+        gw_res_merge (info.kernel_res, fname);
+    }
+    /* fetch language definitions (if specified at all) */
+    v = gw_res_get (info.kernel_res, "gw.language", info.lang);
+    if (v)
     {
-        sprintf (resource_name, "gw.lang.%s", info.lang);
+        sprintf (resource_name, "gw.lang.%s", v);
         v = gw_res_get (info.kernel_res, resource_name, NULL);
         if (v)
         {
-            sprintf (fname, "%s/%s", path_prefix, v);
-            gw_res_merge (info.kernel_res, fname);
+            gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Reading language resources %s",
+                    v);
+            gw_res_merge (info.kernel_res, v);
         }
     }
+    /* fetch overriding resources from file (if specified) */
     if (info.override_res)
-    {
-        sprintf (fname, "%s/%s", path_prefix, info.override_res);
-        gw_res_merge (info.kernel_res, fname);        
-    }
+        gw_res_merge (info.kernel_res, info.override_res);        
+
+    /* read bibset definition for ccl */
     v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
     if (v)
     {
         FILE *bib_inf;
 
-        sprintf (fname, "%s/%s", path_prefix, v);
-        bib_inf = fopen (fname, "r");
+        bib_inf = fopen (v, "r");
         if (!bib_inf)
-            gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", fname);
+            gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", v);
         else
         {
-            gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", fname);
+            gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", v);
             ccl_qual_file (info.bibset, bib_inf);
             fclose (bib_inf);
         }
     }
+
+    /* determine host name and port no */
     sprintf (resource_name, "gw.target.%s", info.target);
     if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
     {
-        /* target is there, and there is no sub-resource for it... */
-        char *split;
+        /* target is specified, and there is no sub-resource for it... */
+        const char *split;
 
         if ((split = strchr (info.target, ':')))
-            *split++ = '\0';
-        strncpy (info.hostname, info.target, sizeof(info.hostname)-1);
-        if (split)
-            info.port = atoi (split);
+        {
+            memcpy (info.hostname, info.target, split-info.target);
+            info.hostname[split-info.target] = '\0';
+            info.port = atoi (split+1);
+        }
         else
-            info.port = atoi (gw_res_get
-                              (info.kernel_res, "gw.portno", "210"));
+        {
+            strcpy (info.hostname, info.target);
+            info.port = gw_res_int (info.kernel_res, "gw.portno", 210);
+        }
     }
     else
-    {
+    {   /* resources gw.hostname and gw.portno will be used */
         strncpy (info.hostname, gw_res_get (info.kernel_res,
                                             "gw.hostname", "localhost"),
                  sizeof(info.hostname)-1);
-        info.port = atoi (gw_res_get (info.kernel_res,
-                                      "gw.portno", "210"));
+        info.port = gw_res_int (info.kernel_res, "gw.portno", 210);
+        strcpy (info.account, gw_res_get (info.kernel_res, "gw.account", ""));
+       info.account_in_session = 0;
     }
+    /* set info.databases (all available databases for target) */
+    /* set info.database (first database for target) */
     if (info.databases)
         free (info.databases);
     if (info.database)
@@ -505,20 +653,33 @@ void read_kernel_res (void)
         for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
             *cp = ',';
     }
+
+    /* the port no can be explicitly overridden by a command line option */
     if (info.override_portno)
         info.port = atoi (info.override_portno);
+
+    /* the hostname can be explicitly overridden by a command line option */
     if (info.override_hostname)
         strncpy (info.hostname, info.override_hostname,
                  sizeof(info.hostname)-1);
+
+    ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
+    ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
+    ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
+    ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
+
+    /* determine if more than one result-set names is supported */
+    if (gw_res_bool (info.kernel_res, "gw.result.set", 1))
+        info.setno = 0;
+    else
+        info.setno = -1;
 #if USE_FML
     if (!info.fml)
     {
         v = gw_res_get (info.kernel_res, "gw.fml", "default.fml");    
-        sprintf (fname, "%s/%s", path_prefix, v);
-        fml_inf = fopen (fname, "r");
+        fml_inf = fopen (v, "r");
         if (!fml_inf)
-            gw_log (GW_LOG_WARN, KERNEL_LOG,
-                    "cannot open fml script %s", fname);
+            gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open fml script %s", v);
         else
         {
             info.fml = fml_open ();