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