f901a1084ab5a0a7302dca8ca9bb0f3bcb9f9062
[egate.git] / kernel / eti.c
1 /* Gateway kernel
2  * Europagate, 1995
3  *
4  * $Log: eti.c,v $
5  * Revision 1.1  1995/03/27 08:24:00  adam
6  * First use of gip interface and gw-db.
7  * First work on eti program.
8  *
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <assert.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18
19 #include <gw-log.h>
20 #include <gw-db.h>
21 #include <gip.h>
22
23 #define LINE_MAX 1024
24 static char line_buf[LINE_MAX+1];
25
26 static char *module = "eti";
27
28 static int email_header (FILE *inf, char *from_str, char *subject_str)
29 {
30     *from_str = '\0';
31     *subject_str = '\0';
32     while (fgets (line_buf, LINE_MAX, inf))
33     {
34         if (line_buf[0] == '\n')
35             return 0;
36         if (strncmp (line_buf, "From ", 5) == 0)
37             sscanf (line_buf+4, "%s", from_str);
38         if (strncmp (line_buf, "Subject: ", 9) == 0 &&
39             sscanf (line_buf+9, "%s", subject_str+1) == 1)
40             strcpy (subject_str, line_buf+9);
41     }
42     return 1;
43 }
44
45 static int kernel_active (int id)
46 {
47     char active_name[1024];
48     int fd;
49
50     sprintf (active_name, "kernel.pid.%d", id);
51     fd = open (active_name, O_RDONLY);
52     if (fd == -1)
53         return 0;
54     close (fd);
55     return 1;
56 }
57
58 int main (int argc, char **argv)
59 {
60     char from_str[LINE_MAX+1];
61     char subject_str[LINE_MAX+1];
62     char line_str[LINE_MAX+1];
63     GW_DB user_db;
64     GIP  gip;
65     void *user_info;
66     size_t info_length;
67     int  id;
68     int  r;
69     char fifo_client_name[1024];
70     char fifo_server_name[1024];
71
72     gw_log_init (*argv);
73     gw_log_level (GW_LOG_ALL);
74     gw_log_file (GW_LOG_ALL, "eti.log");
75     r = email_header (stdin, from_str, subject_str);
76     if (! *from_str)
77     {
78         gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
79         exit (1);
80     }
81     gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
82     user_db = gw_db_open ("user.db", 1);
83
84     r = gw_db_lookup (user_db, from_str, strlen(from_str),
85                       &user_info, &info_length);
86     if (r == 0)
87     {
88         gw_log (GW_LOG_STAT, module, "New user");
89         id = gw_db_seq_no (user_db);
90         r = gw_db_insert (user_db, from_str, strlen(from_str),
91                           &id, sizeof(id));
92         gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
93     }
94     else if (r == 1)
95     {
96         if (sizeof(id) != info_length)
97         {
98             gw_log (GW_LOG_FATAL, module, "Bad id-size");
99             exit (1);
100         }
101         memcpy (&id, user_info, sizeof(id));
102         gw_log (GW_LOG_STAT, module, "Found with id %d", id);
103     }
104     else
105     {
106         gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
107     }
108     sprintf (fifo_server_name, "fifo.s.%d", id);
109     sprintf (fifo_client_name, "fifo.c.%d", id);
110
111     gip = gipc_initialize (fifo_client_name);
112     if (kernel_active (id))
113     {
114         gw_log (GW_LOG_DEBUG, module, "Kernel already active");
115         gipc_open (gip, fifo_server_name, 0);
116     }
117     else
118     {
119         pid_t pid;
120         int i;
121         char **argv_p;
122         char userid_option[20];
123
124         argv_p = malloc (sizeof(*argv_p)*(argc+2));
125         if (!argv_p)
126         {
127             gw_log (GW_LOG_FATAL, module, "Malloc fail");
128             exit (1);
129         }
130         argv_p[0] = "kernel";
131         for (i = 1; i<argc; i++)
132             argv_p[i] = argv[i];
133         sprintf (userid_option, "-i%d", id);
134         argv_p[i++] = userid_option;
135         argv_p[i++] = NULL;
136
137         gw_log (GW_LOG_DEBUG, module, "Kernel not active");
138         pid = fork ();
139         if (pid == -1)
140         {
141             gw_log (GW_LOG_FATAL, module, "fork");
142             exit (1);
143         }
144         if (!pid)
145         {
146             execvp ("kernel", argv_p);
147             gw_log (GW_LOG_FATAL, module, "execvp");
148             exit (1);
149         }
150         gipc_open (gip, fifo_server_name, 1);
151     }
152     /* deliver message ... */
153     gw_log (GW_LOG_DEBUG, module, "Deliver mail header");
154     gip_wline (gip, "mail\n");
155     gip_wline (gip, "From ");
156     gip_wline (gip, from_str);
157     gip_wline (gip, "\n");
158     if (*subject_str)
159     {
160         gip_wline (gip, "Subject: ");
161         gip_wline (gip, subject_str);
162         gip_wline (gip, "\n");
163     }
164     gip_wline (gip, "\n");
165     gw_log (GW_LOG_DEBUG, module, "Deliver mail body");
166     while (fgets (line_str, LINE_MAX, stdin))
167         gip_wline (gip, line_str);
168     gip_wline (gip, "\001");
169     gw_log (GW_LOG_DEBUG, module, "Closing");
170     gipc_close (gip);
171     gipc_destroy (gip);
172     gw_db_close (user_db);
173     exit (0);
174 }
175