Minor changes.
[egate.git] / kernel / eti.c
index 0398f56..de0634b 100644 (file)
@@ -1,8 +1,99 @@
-/* Gateway kernel
+/*
+ * 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 Email Transport Interface
  * Europagate, 1995
  *
  * $Log: eti.c,v $
- * Revision 1.3  1995/03/28 08:01:23  adam
+ * Revision 1.18  1996/03/01 14:25:28  adam
+ * Email gateway obeys 'Reply-To:' in header.
+ *
+ * Revision 1.17  1995/12/20  16:27:24  adam
+ * Extra parameter block to gw_db_open.
+ *
+ * Revision 1.16  1995/07/11  11:49:11  adam
+ * LINE_MAX renamed to STR_LINE_MAX.
+ *
+ * Revision 1.15  1995/07/03  12:59:28  adam
+ * New option for eti: -c dir to chdir before start.
+ * Setting change: gw.max.show defaults to 100.
+ *
+ * Revision 1.14  1995/05/19  13:25:59  adam
+ * Bug fixes. Better command line options.
+ *
+ * Revision 1.13  1995/05/16  09:40:41  adam
+ * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
+ *
+ * Revision 1.12  1995/05/03  07:37:35  adam
+ * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
+ * are used when possible.
+ *
+ * Revision 1.11  1995/05/01  16:26:56  adam
+ * More work on resource monitor.
+ *
+ * Revision 1.10  1995/05/01  12:43:29  adam
+ * First work on resource monitor program.
+ *
+ * Revision 1.9  1995/04/21  16:37:43  adam
+ * Parent (eti) creates BOTH FIFOs. dtbsun is more happy now.
+ *
+ * Revision 1.8  1995/04/20  15:12:24  adam
+ * Minor hacks really.
+ *
+ * Revision 1.7  1995/04/19  16:01:57  adam
+ * Some hacks to get the FIFO communication work!! Isn't reliable.
+ * Resource gw.account added - default account info.
+ *
+ * Revision 1.6  1995/04/17  09:34:27  adam
+ * Timeout (idletime) adjustable. Minor changes in kernel.
+ *
+ * Revision 1.5  1995/03/31  09:43:13  adam
+ * Removed unused variable.
+ *
+ * Revision 1.4  1995/03/28  11:42:34  adam
+ * First use of string-queue utility.
+ *
+ * Revision 1.3  1995/03/28  08:01:23  adam
  * FIFO existence is used to test for a running kernel.
  *
  * Revision 1.2  1995/03/27  12:51:05  adam
 #include <setjmp.h>
 #include <signal.h>
 
+#include <sys/file.h>
+#include <sys/stat.h>
+
 #include <gw-log.h>
 #include <gw-db.h>
+#include <gw-res.h>
 #include <gip.h>
+#include <strqueue.h>
 
-#define LINE_MAX 1024
-static char line_buf[LINE_MAX+1];
+#define STR_LINE_MAX 1024
 
 static char *module = "eti";
 static jmp_buf retry_jmp;
@@ -39,19 +134,25 @@ static void pipe_handle (int dummy)
     longjmp (retry_jmp, 1);
 }
 
-static int email_header (FILE *inf, char *from_str, char *subject_str)
+static int email_header (struct str_queue *sq,
+                         char *from_str, char *subject_str)
 {
+    char *msg;
+    int index = 0;
+
     *from_str = '\0';
-    *subject_str = '\0';
-    while (fgets (line_buf, LINE_MAX, inf))
+    *subject_str = '\0';    
+    while ((msg = str_queue_get (sq, index++)))
     {
-        if (line_buf[0] == '\n')
+        if (msg[0] == '\n')
             return 0;
-        if (strncmp (line_buf, "From ", 5) == 0)
-            sscanf (line_buf+4, "%s", from_str);
-        if (strncmp (line_buf, "Subject: ", 9) == 0 &&
-            sscanf (line_buf+9, "%s", subject_str+1) == 1)
-            strcpy (subject_str, line_buf+9);
+        if (memcmp (msg, "From ", 5) == 0)
+            sscanf (msg+4, "%s", from_str);
+        if (memcmp (msg, "Reply-To: ", 10) == 0)
+            sscanf (msg+10, "%s", from_str);
+        if (memcmp (msg, "Subject: ", 9) == 0 &&
+            sscanf (msg+9, "%s", subject_str+1) == 1)
+            strcpy (subject_str, msg+9);
     }
     return 1;
 }
@@ -85,17 +186,28 @@ static void start_kernel (int argc, char **argv, int id)
     }
     if (!pid)
     {
-        execvp ("kernel", argv_p);
+        execv ("kernel", argv_p);
        gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
        exit (1);
     }
 }
 
+static void deliver (struct str_queue *sq, GIP gip, const char *msg)
+{
+    int index = 0;
+
+    gip_wline (gip, msg);
+    while ((msg = str_queue_get (sq, index++)))
+        gip_wline (gip, msg);
+    gip_wline (gip, "\001");
+}
+
 int main (int argc, char **argv)
 {
-    char from_str[LINE_MAX+1];
-    char subject_str[LINE_MAX+1];
-    char line_str[LINE_MAX+1];
+    char from_str[STR_LINE_MAX+1];
+    char subject_str[STR_LINE_MAX+1];
+    char line_str[STR_LINE_MAX+1];
+    char msg[20];
     GW_DB user_db;
     GIP  gip;
     void *user_info;
@@ -105,18 +217,90 @@ int main (int argc, char **argv)
     static int pass = 0;
     char fifo_client_name[1024];
     char fifo_server_name[1024];
+    struct str_queue *queue;
+    int monitor_flag = 1;
+    int argno = 0;
 
     gw_log_init (*argv);
-    gw_log_level (GW_LOG_ALL);
-    gw_log_file (GW_LOG_ALL, "eti.log");
-    r = email_header (stdin, from_str, subject_str);
+    while (++argno < argc)
+    {
+        if (argv[argno][0] == '-')
+        {
+            if (argv[argno][1] == '-')
+            {
+                monitor_flag = 0;
+                break;
+            }
+            switch (argv[argno][1])
+            {
+            case 'h':
+            case 'H':
+                fprintf (stderr, "eti [options] -- [kernelOptions]\n");
+                fprintf (stderr, "Options:\n");
+                fprintf (stderr, " -l log  Set Log file\n");
+                fprintf (stderr, " -d      Enable debugging log\n");
+                fprintf (stderr, " -D      Enable more debugging log\n");
+                fprintf (stderr, " -c dir  Change to directory\n");
+                fprintf (stderr, " --      Precedes kernel options "
+                         "(kernel is invoked instead of monitor)\n");
+                exit (1);
+            case 'l':
+                if (argv[argno][2])
+                    gw_log_file (GW_LOG_ALL, argv[argno]+2);
+                else if (++argno < argc)
+                    gw_log_file (GW_LOG_ALL, argv[argno]);
+                else
+                {
+                    fprintf (stderr, "%s: missing log filename\n", *argv);
+                    exit (1);
+                }
+                break;
+            case 'd':
+                gw_log_level (GW_LOG_ALL & ~RES_DEBUG);
+                break;
+            case 'D':
+                gw_log_level (GW_LOG_ALL);
+                break;
+            case 'c':
+                if (argv[argno][2])
+                {
+                    if (chdir (argv[argno]+2)) 
+                        gw_log (GW_LOG_WARN|GW_LOG_ERRNO, module, "chdir");
+                } 
+                else if (++argno < argc)
+                {
+                    if (chdir (argv[argno])) 
+                        gw_log (GW_LOG_WARN|GW_LOG_ERRNO, module, "chdir");
+                }
+                else
+                {
+                    fprintf (stderr, "%s: missing chdir name\n", *argv);
+                    exit (1);
+                }
+                break;
+            default:
+                fprintf (stderr, "%s: unknown option `%s'; use -H for help\n",
+                         *argv, argv[argno]);
+                exit (1);
+            }
+        }
+    }
+    if (!(queue = str_queue_mk ()))
+    {
+        gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "str_queue_mk");
+        exit (1);
+    }
+    while (fgets (line_str, STR_LINE_MAX, stdin))
+        str_queue_enq (queue, line_str);
+
+    r = email_header (queue, from_str, subject_str);
     if (! *from_str)
     {
         gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
         exit (1);
     }
     gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
-    user_db = gw_db_open ("user.db", 1);
+    user_db = gw_db_open ("user.db", 1, 1);
 
     r = gw_db_lookup (user_db, from_str, strlen(from_str),
                       &user_info, &info_length);
@@ -141,22 +325,40 @@ int main (int argc, char **argv)
     else
     {
         gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
+       exit (1);
+    }
+    if (monitor_flag)
+    {
+        sprintf (fifo_server_name, "fifo.s.m");
+        sprintf (fifo_client_name, "fifo.c.m");
+    }
+    else
+    {
+        sprintf (fifo_server_name, "fifo.s.%d", id);
+        sprintf (fifo_client_name, "fifo.c.%d", id);
     }
-    sprintf (fifo_server_name, "fifo.s.%d", id);
-    sprintf (fifo_client_name, "fifo.c.%d", id);
 
     gip = gipc_initialize (fifo_client_name);
 
     signal (SIGPIPE, pipe_handle);
     setjmp (retry_jmp);
     ++pass;
+    signal (SIGPIPE, pipe_handle);
     gw_log (GW_LOG_DEBUG, module, "Pass %d", pass);
     if (pass == 1)
+    {
+        gipc_close (gip);
         r = gipc_open (gip, fifo_server_name, 0);
+    }
     else if (pass == 2)
     {
-        gipc_close (gip);
-        start_kernel (argc, argv, id);
+        if (monitor_flag)
+        {
+            gw_log (GW_LOG_FATAL, module, "Cannot contact monitor");
+            exit (1);
+        }
+       mknod (fifo_server_name, S_IFIFO|0666, 0);
+        start_kernel (argc-argno, argv+argno, id);
         r = gipc_open (gip, fifo_server_name, 1);
     }
     else if (pass == 3)
@@ -167,12 +369,12 @@ int main (int argc, char **argv)
     if (r < 0)
         if (r == -2)
        {
-           gw_log (GW_LOG_WARN|GW_LOG_ERRNO, module, "r==-2");
+           gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
            longjmp (retry_jmp, 1);
        }
        else if (r == -1)
        {
-           gw_log (GW_LOG_WARN|GW_LOG_ERRNO, module, "r==-1");
+           gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
            longjmp (retry_jmp, 1);
        }
        else
@@ -181,26 +383,17 @@ int main (int argc, char **argv)
            exit (1);
        }
     /* deliver message ... */
-    gw_log (GW_LOG_DEBUG, module, "Delivering mail header");
-    gip_wline (gip, "mail\n");
-    gip_wline (gip, "From ");
-    gip_wline (gip, from_str);
-    gip_wline (gip, "\n");
-    if (*subject_str)
-    {
-        gip_wline (gip, "Subject: ");
-       gip_wline (gip, subject_str);
-       gip_wline (gip, "\n");
-    }
-    gip_wline (gip, "\n");
-    gw_log (GW_LOG_DEBUG, module, "Delivering mail body");
-    while (fgets (line_str, LINE_MAX, stdin))
-        gip_wline (gip, line_str);
-    gip_wline (gip, "\001");
+    gw_log (GW_LOG_DEBUG, module, "Delivering mail");
+    if (monitor_flag)
+        sprintf (msg, "eti %d\n", id);
+    else
+        strcpy (msg, "mail\n");
+    deliver (queue, gip, msg);
     gw_log (GW_LOG_DEBUG, module, "Closing");
     gipc_close (gip);
     gipc_destroy (gip);
     gw_db_close (user_db);
+    gw_log (GW_LOG_DEBUG, module, "Exiting");
     exit (0);
 }