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