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