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