c36edd8e4f181a4af9fee8e6a328c90b25fe8335
[egate.git] / kernel / eti.c
1 /* Gateway Email Transport Interface
2  * Europagate, 1995
3  *
4  * $Log: eti.c,v $
5  * Revision 1.6  1995/04/17 09:34:27  adam
6  * Timeout (idletime) adjustable. Minor changes in kernel.
7  *
8  * Revision 1.5  1995/03/31  09:43:13  adam
9  * Removed unused variable.
10  *
11  * Revision 1.4  1995/03/28  11:42:34  adam
12  * First use of string-queue utility.
13  *
14  * Revision 1.3  1995/03/28  08:01:23  adam
15  * FIFO existence is used to test for a running kernel.
16  *
17  * Revision 1.2  1995/03/27  12:51:05  adam
18  * New log level in use: GW_LOG_ERRNO.
19  *
20  * Revision 1.1  1995/03/27  08:24:00  adam
21  * First use of gip interface and gw-db.
22  * First work on eti program.
23  *
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <setjmp.h>
34 #include <signal.h>
35
36 #include <gw-log.h>
37 #include <gw-db.h>
38 #include <gip.h>
39 #include <strqueue.h>
40
41 #define LINE_MAX 1024
42
43 static char *module = "eti";
44 static jmp_buf retry_jmp;
45
46 static void pipe_handle (int dummy)
47 {
48     longjmp (retry_jmp, 1);
49 }
50
51 static int email_header (struct str_queue *sq,
52                          char *from_str, char *subject_str)
53 {
54     char *msg;
55     int index = 0;
56
57     *from_str = '\0';
58     *subject_str = '\0';    
59     while ((msg = str_queue_get (sq, index++)))
60     {
61         if (msg[0] == '\n')
62             return 0;
63         if (memcmp (msg, "From ", 5) == 0)
64             sscanf (msg+4, "%s", from_str);
65         if (memcmp (msg, "Subject: ", 9) == 0 &&
66             sscanf (msg+9, "%s", subject_str+1) == 1)
67             strcpy (subject_str, msg+9);
68     }
69     return 1;
70 }
71
72 static void start_kernel (int argc, char **argv, int id)
73 {
74     pid_t pid;
75     int i;
76     char **argv_p;
77     char userid_option[20];
78
79     argv_p = malloc (sizeof(*argv_p)*(argc+2));
80     if (!argv_p)
81     {
82         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
83         exit (1);
84     }
85     argv_p[0] = "kernel";
86     for (i = 1; i<argc; i++)
87         argv_p[i] = argv[i];
88     sprintf (userid_option, "-i%d", id);
89     argv_p[i++] = userid_option;
90     argv_p[i++] = NULL;
91
92     gw_log (GW_LOG_DEBUG, module, "Starting kernel");
93     pid = fork ();
94     if (pid == -1)
95     {
96         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
97         exit (1);
98     }
99     if (!pid)
100     {
101         execvp ("kernel", argv_p);
102         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
103         exit (1);
104     }
105 }
106
107 static void deliver (struct str_queue *sq, GIP gip)
108 {
109     int index = 0;
110     char *msg;
111
112     gip_wline (gip, "mail\n");
113     while ((msg = str_queue_get (sq, index++)))
114         gip_wline (gip, msg);
115     gip_wline (gip, "\001");
116 }
117
118 int main (int argc, char **argv)
119 {
120     char from_str[LINE_MAX+1];
121     char subject_str[LINE_MAX+1];
122     char line_str[LINE_MAX+1];
123     GW_DB user_db;
124     GIP  gip;
125     void *user_info;
126     size_t info_length;
127     int  id;
128     int  r;
129     static int pass = 0;
130     char fifo_client_name[1024];
131     char fifo_server_name[1024];
132     struct str_queue *queue;
133
134     gw_log_init (*argv);
135     gw_log_level (GW_LOG_ALL);
136     gw_log_file (GW_LOG_ALL, "eti.log");
137
138     if (!(queue = str_queue_mk ()))
139     {
140         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "str_queue_mk");
141         exit (1);
142     }
143     while (fgets (line_str, LINE_MAX, stdin))
144         str_queue_enq (queue, line_str);
145
146     r = email_header (queue, from_str, subject_str);
147     if (! *from_str)
148     {
149         gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
150         exit (1);
151     }
152     gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
153     user_db = gw_db_open ("user.db", 1);
154
155     r = gw_db_lookup (user_db, from_str, strlen(from_str),
156                       &user_info, &info_length);
157     if (r == 0)
158     {
159         gw_log (GW_LOG_STAT, module, "New user");
160         id = gw_db_seq_no (user_db);
161         r = gw_db_insert (user_db, from_str, strlen(from_str),
162                           &id, sizeof(id));
163         gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
164     }
165     else if (r == 1)
166     {
167         if (sizeof(id) != info_length)
168         {
169             gw_log (GW_LOG_FATAL, module, "Bad id-size");
170             exit (1);
171         }
172         memcpy (&id, user_info, sizeof(id));
173         gw_log (GW_LOG_STAT, module, "Found with id %d", id);
174     }
175     else
176     {
177         gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
178         exit (1);
179     }
180     sprintf (fifo_server_name, "fifo.s.%d", id);
181     sprintf (fifo_client_name, "fifo.c.%d", id);
182
183     gip = gipc_initialize (fifo_client_name);
184
185     signal (SIGPIPE, pipe_handle);
186     setjmp (retry_jmp);
187     ++pass;
188     gw_log (GW_LOG_DEBUG, module, "Pass %d", pass);
189     if (pass == 1)
190         r = gipc_open (gip, fifo_server_name, 0);
191     else if (pass == 2)
192     {
193         gipc_close (gip);
194         start_kernel (argc, argv, id);
195         r = gipc_open (gip, fifo_server_name, 1);
196     }
197     else if (pass == 3)
198     {
199         gw_log (GW_LOG_FATAL, module, "Cannot start kernel");
200         exit (1);
201     }
202     if (r < 0)
203         if (r == -2)
204         {
205             gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
206             longjmp (retry_jmp, 1);
207         }
208         else if (r == -1)
209         {
210             gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
211             longjmp (retry_jmp, 1);
212         }
213         else
214         {
215             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "gipc_open");
216             exit (1);
217         }
218     /* deliver message ... */
219     gw_log (GW_LOG_DEBUG, module, "Delivering mail");
220     deliver (queue, gip);
221     gw_log (GW_LOG_DEBUG, module, "Closing");
222     gipc_close (gip);
223     gipc_destroy (gip);
224     gw_db_close (user_db);
225     gw_log (GW_LOG_DEBUG, module, "Exiting");
226     exit (0);
227 }
228