1aaaf71953fe730b0efe877bc655cf44870a483b
[egate.git] / kernel / eti.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  */
44 /* Gateway Email Transport Interface
45  * Europagate, 1995
46  *
47  * $Log: eti.c,v $
48  * Revision 1.15  1995/07/03 12:59:28  adam
49  * New option for eti: -c dir to chdir before start.
50  * Setting change: gw.max.show defaults to 100.
51  *
52  * Revision 1.14  1995/05/19  13:25:59  adam
53  * Bug fixes. Better command line options.
54  *
55  * Revision 1.13  1995/05/16  09:40:41  adam
56  * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
57  *
58  * Revision 1.12  1995/05/03  07:37:35  adam
59  * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
60  * are used when possible.
61  *
62  * Revision 1.11  1995/05/01  16:26:56  adam
63  * More work on resource monitor.
64  *
65  * Revision 1.10  1995/05/01  12:43:29  adam
66  * First work on resource monitor program.
67  *
68  * Revision 1.9  1995/04/21  16:37:43  adam
69  * Parent (eti) creates BOTH FIFOs. dtbsun is more happy now.
70  *
71  * Revision 1.8  1995/04/20  15:12:24  adam
72  * Minor hacks really.
73  *
74  * Revision 1.7  1995/04/19  16:01:57  adam
75  * Some hacks to get the FIFO communication work!! Isn't reliable.
76  * Resource gw.account added - default account info.
77  *
78  * Revision 1.6  1995/04/17  09:34:27  adam
79  * Timeout (idletime) adjustable. Minor changes in kernel.
80  *
81  * Revision 1.5  1995/03/31  09:43:13  adam
82  * Removed unused variable.
83  *
84  * Revision 1.4  1995/03/28  11:42:34  adam
85  * First use of string-queue utility.
86  *
87  * Revision 1.3  1995/03/28  08:01:23  adam
88  * FIFO existence is used to test for a running kernel.
89  *
90  * Revision 1.2  1995/03/27  12:51:05  adam
91  * New log level in use: GW_LOG_ERRNO.
92  *
93  * Revision 1.1  1995/03/27  08:24:00  adam
94  * First use of gip interface and gw-db.
95  * First work on eti program.
96  *
97  */
98
99 #include <stdio.h>
100 #include <stdlib.h>
101 #include <assert.h>
102 #include <ctype.h>
103 #include <string.h>
104 #include <unistd.h>
105 #include <fcntl.h>
106 #include <setjmp.h>
107 #include <signal.h>
108
109 #include <sys/file.h>
110 #include <sys/stat.h>
111
112 #include <gw-log.h>
113 #include <gw-db.h>
114 #include <gw-res.h>
115 #include <gip.h>
116 #include <strqueue.h>
117
118 #define LINE_MAX 1024
119
120 static char *module = "eti";
121 static jmp_buf retry_jmp;
122
123 static void pipe_handle (int dummy)
124 {
125     longjmp (retry_jmp, 1);
126 }
127
128 static int email_header (struct str_queue *sq,
129                          char *from_str, char *subject_str)
130 {
131     char *msg;
132     int index = 0;
133
134     *from_str = '\0';
135     *subject_str = '\0';    
136     while ((msg = str_queue_get (sq, index++)))
137     {
138         if (msg[0] == '\n')
139             return 0;
140         if (memcmp (msg, "From ", 5) == 0)
141             sscanf (msg+4, "%s", from_str);
142         if (memcmp (msg, "Subject: ", 9) == 0 &&
143             sscanf (msg+9, "%s", subject_str+1) == 1)
144             strcpy (subject_str, msg+9);
145     }
146     return 1;
147 }
148
149 static void start_kernel (int argc, char **argv, int id)
150 {
151     pid_t pid;
152     int i;
153     char **argv_p;
154     char userid_option[20];
155
156     argv_p = malloc (sizeof(*argv_p)*(argc+2));
157     if (!argv_p)
158     {
159         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
160         exit (1);
161     }
162     argv_p[0] = "kernel";
163     for (i = 1; i<argc; i++)
164         argv_p[i] = argv[i];
165     sprintf (userid_option, "-i%d", id);
166     argv_p[i++] = userid_option;
167     argv_p[i++] = NULL;
168
169     gw_log (GW_LOG_DEBUG, module, "Starting kernel");
170     pid = fork ();
171     if (pid == -1)
172     {
173         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
174         exit (1);
175     }
176     if (!pid)
177     {
178         execv ("kernel", argv_p);
179         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
180         exit (1);
181     }
182 }
183
184 static void deliver (struct str_queue *sq, GIP gip, const char *msg)
185 {
186     int index = 0;
187
188     gip_wline (gip, msg);
189     while ((msg = str_queue_get (sq, index++)))
190         gip_wline (gip, msg);
191     gip_wline (gip, "\001");
192 }
193
194 int main (int argc, char **argv)
195 {
196     char from_str[LINE_MAX+1];
197     char subject_str[LINE_MAX+1];
198     char line_str[LINE_MAX+1];
199     char msg[20];
200     GW_DB user_db;
201     GIP  gip;
202     void *user_info;
203     size_t info_length;
204     int  id;
205     int  r;
206     static int pass = 0;
207     char fifo_client_name[1024];
208     char fifo_server_name[1024];
209     struct str_queue *queue;
210     int monitor_flag = 1;
211     int argno = 0;
212
213     gw_log_init (*argv);
214     while (++argno < argc)
215     {
216         if (argv[argno][0] == '-')
217         {
218             if (argv[argno][1] == '-')
219             {
220                 monitor_flag = 0;
221                 break;
222             }
223             switch (argv[argno][1])
224             {
225             case 'h':
226             case 'H':
227                 fprintf (stderr, "eti [options] -- [kernelOptions]\n");
228                 fprintf (stderr, "Options:\n");
229                 fprintf (stderr, " -l log  Set Log file\n");
230                 fprintf (stderr, " -d      Enable debugging log\n");
231                 fprintf (stderr, " -D      Enable more debugging log\n");
232                 fprintf (stderr, " -c dir  Change to directory\n");
233                 fprintf (stderr, " --      Precedes kernel options "
234                          "(kernel is invoked instead of monitor)\n");
235                 exit (1);
236             case 'l':
237                 if (argv[argno][2])
238                     gw_log_file (GW_LOG_ALL, argv[argno]+2);
239                 else if (++argno < argc)
240                     gw_log_file (GW_LOG_ALL, argv[argno]);
241                 else
242                 {
243                     fprintf (stderr, "%s: missing log filename\n", *argv);
244                     exit (1);
245                 }
246                 break;
247             case 'd':
248                 gw_log_level (GW_LOG_ALL & ~RES_DEBUG);
249                 break;
250             case 'D':
251                 gw_log_level (GW_LOG_ALL);
252                 break;
253             case 'c':
254                 if (argv[argno][2])
255                 {
256                     if (chdir (argv[argno]+2)) 
257                         gw_log (GW_LOG_WARN|GW_LOG_ERRNO, module, "chdir");
258                 } 
259                 else if (++argno < argc)
260                 {
261                     if (chdir (argv[argno])) 
262                         gw_log (GW_LOG_WARN|GW_LOG_ERRNO, module, "chdir");
263                 }
264                 else
265                 {
266                     fprintf (stderr, "%s: missing chdir name\n", *argv);
267                     exit (1);
268                 }
269                 break;
270             default:
271                 fprintf (stderr, "%s: unknown option `%s'; use -H for help\n",
272                          *argv, argv[argno]);
273                 exit (1);
274             }
275         }
276     }
277     if (!(queue = str_queue_mk ()))
278     {
279         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "str_queue_mk");
280         exit (1);
281     }
282     while (fgets (line_str, LINE_MAX, stdin))
283         str_queue_enq (queue, line_str);
284
285     r = email_header (queue, from_str, subject_str);
286     if (! *from_str)
287     {
288         gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
289         exit (1);
290     }
291     gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
292     user_db = gw_db_open ("user.db", 1);
293
294     r = gw_db_lookup (user_db, from_str, strlen(from_str),
295                       &user_info, &info_length);
296     if (r == 0)
297     {
298         gw_log (GW_LOG_STAT, module, "New user");
299         id = gw_db_seq_no (user_db);
300         r = gw_db_insert (user_db, from_str, strlen(from_str),
301                           &id, sizeof(id));
302         gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
303     }
304     else if (r == 1)
305     {
306         if (sizeof(id) != info_length)
307         {
308             gw_log (GW_LOG_FATAL, module, "Bad id-size");
309             exit (1);
310         }
311         memcpy (&id, user_info, sizeof(id));
312         gw_log (GW_LOG_STAT, module, "Found with id %d", id);
313     }
314     else
315     {
316         gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
317         exit (1);
318     }
319     if (monitor_flag)
320     {
321         sprintf (fifo_server_name, "fifo.s.m");
322         sprintf (fifo_client_name, "fifo.c.m");
323     }
324     else
325     {
326         sprintf (fifo_server_name, "fifo.s.%d", id);
327         sprintf (fifo_client_name, "fifo.c.%d", id);
328     }
329
330     gip = gipc_initialize (fifo_client_name);
331
332     signal (SIGPIPE, pipe_handle);
333     setjmp (retry_jmp);
334     ++pass;
335     signal (SIGPIPE, pipe_handle);
336     gw_log (GW_LOG_DEBUG, module, "Pass %d", pass);
337     if (pass == 1)
338     {
339         gipc_close (gip);
340         r = gipc_open (gip, fifo_server_name, 0);
341     }
342     else if (pass == 2)
343     {
344         if (monitor_flag)
345         {
346             gw_log (GW_LOG_FATAL, module, "Cannot contact monitor");
347             exit (1);
348         }
349         mknod (fifo_server_name, S_IFIFO|0666, 0);
350         start_kernel (argc-argno, argv+argno, id);
351         r = gipc_open (gip, fifo_server_name, 1);
352     }
353     else if (pass == 3)
354     {
355         gw_log (GW_LOG_FATAL, module, "Cannot start kernel");
356         exit (1);
357     }
358     if (r < 0)
359         if (r == -2)
360         {
361             gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
362             longjmp (retry_jmp, 1);
363         }
364         else if (r == -1)
365         {
366             gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
367             longjmp (retry_jmp, 1);
368         }
369         else
370         {
371             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "gipc_open");
372             exit (1);
373         }
374     /* deliver message ... */
375     gw_log (GW_LOG_DEBUG, module, "Delivering mail");
376     if (monitor_flag)
377         sprintf (msg, "eti %d\n", id);
378     else
379         strcpy (msg, "mail\n");
380     deliver (queue, gip, msg);
381     gw_log (GW_LOG_DEBUG, module, "Closing");
382     gipc_close (gip);
383     gipc_destroy (gip);
384     gw_db_close (user_db);
385     gw_log (GW_LOG_DEBUG, module, "Exiting");
386     exit (0);
387 }
388