First use of gip interface and gw-db.
[egate.git] / kernel / eti.c
diff --git a/kernel/eti.c b/kernel/eti.c
new file mode 100644 (file)
index 0000000..f901a10
--- /dev/null
@@ -0,0 +1,175 @@
+/* Gateway kernel
+ * Europagate, 1995
+ *
+ * $Log: eti.c,v $
+ * Revision 1.1  1995/03/27 08:24:00  adam
+ * First use of gip interface and gw-db.
+ * First work on eti program.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <ctype.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <gw-log.h>
+#include <gw-db.h>
+#include <gip.h>
+
+#define LINE_MAX 1024
+static char line_buf[LINE_MAX+1];
+
+static char *module = "eti";
+
+static int email_header (FILE *inf, char *from_str, char *subject_str)
+{
+    *from_str = '\0';
+    *subject_str = '\0';
+    while (fgets (line_buf, LINE_MAX, inf))
+    {
+        if (line_buf[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);
+    }
+    return 1;
+}
+
+static int kernel_active (int id)
+{
+    char active_name[1024];
+    int fd;
+
+    sprintf (active_name, "kernel.pid.%d", id);
+    fd = open (active_name, O_RDONLY);
+    if (fd == -1)
+        return 0;
+    close (fd);
+    return 1;
+}
+
+int main (int argc, char **argv)
+{
+    char from_str[LINE_MAX+1];
+    char subject_str[LINE_MAX+1];
+    char line_str[LINE_MAX+1];
+    GW_DB user_db;
+    GIP  gip;
+    void *user_info;
+    size_t info_length;
+    int  id;
+    int  r;
+    char fifo_client_name[1024];
+    char fifo_server_name[1024];
+
+    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);
+    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);
+
+    r = gw_db_lookup (user_db, from_str, strlen(from_str),
+                      &user_info, &info_length);
+    if (r == 0)
+    {
+        gw_log (GW_LOG_STAT, module, "New user");
+        id = gw_db_seq_no (user_db);
+        r = gw_db_insert (user_db, from_str, strlen(from_str),
+                         &id, sizeof(id));
+        gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
+    }
+    else if (r == 1)
+    {
+        if (sizeof(id) != info_length)
+       {
+           gw_log (GW_LOG_FATAL, module, "Bad id-size");
+           exit (1);
+       }
+        memcpy (&id, user_info, sizeof(id));
+        gw_log (GW_LOG_STAT, module, "Found with id %d", id);
+    }
+    else
+    {
+        gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
+    }
+    sprintf (fifo_server_name, "fifo.s.%d", id);
+    sprintf (fifo_client_name, "fifo.c.%d", id);
+
+    gip = gipc_initialize (fifo_client_name);
+    if (kernel_active (id))
+    {
+        gw_log (GW_LOG_DEBUG, module, "Kernel already active");
+        gipc_open (gip, fifo_server_name, 0);
+    }
+    else
+    {
+        pid_t pid;
+       int i;
+       char **argv_p;
+       char userid_option[20];
+
+        argv_p = malloc (sizeof(*argv_p)*(argc+2));
+       if (!argv_p)
+       {
+           gw_log (GW_LOG_FATAL, module, "Malloc fail");
+           exit (1);
+       }
+       argv_p[0] = "kernel";
+        for (i = 1; i<argc; i++)
+            argv_p[i] = argv[i];
+       sprintf (userid_option, "-i%d", id);
+       argv_p[i++] = userid_option;
+       argv_p[i++] = NULL;
+
+        gw_log (GW_LOG_DEBUG, module, "Kernel not active");
+       pid = fork ();
+       if (pid == -1)
+       {
+           gw_log (GW_LOG_FATAL, module, "fork");
+           exit (1);
+       }
+       if (!pid)
+       {
+            execvp ("kernel", argv_p);
+           gw_log (GW_LOG_FATAL, module, "execvp");
+           exit (1);
+       }
+        gipc_open (gip, fifo_server_name, 1);
+    }
+    /* deliver message ... */
+    gw_log (GW_LOG_DEBUG, module, "Deliver 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, "Deliver mail body");
+    while (fgets (line_str, LINE_MAX, stdin))
+        gip_wline (gip, line_str);
+    gip_wline (gip, "\001");
+    gw_log (GW_LOG_DEBUG, module, "Closing");
+    gipc_close (gip);
+    gipc_destroy (gip);
+    gw_db_close (user_db);
+    exit (0);
+}
+