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