c69e67d650b0023442348ceccf7846b2721e0fa5
[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.17  1995/12/20 16:27:24  adam
49  * Extra parameter block to gw_db_open.
50  *
51  * Revision 1.16  1995/07/11  11:49:11  adam
52  * LINE_MAX renamed to STR_LINE_MAX.
53  *
54  * Revision 1.15  1995/07/03  12:59:28  adam
55  * New option for eti: -c dir to chdir before start.
56  * Setting change: gw.max.show defaults to 100.
57  *
58  * Revision 1.14  1995/05/19  13:25:59  adam
59  * Bug fixes. Better command line options.
60  *
61  * Revision 1.13  1995/05/16  09:40:41  adam
62  * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
63  *
64  * Revision 1.12  1995/05/03  07:37:35  adam
65  * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
66  * are used when possible.
67  *
68  * Revision 1.11  1995/05/01  16:26:56  adam
69  * More work on resource monitor.
70  *
71  * Revision 1.10  1995/05/01  12:43:29  adam
72  * First work on resource monitor program.
73  *
74  * Revision 1.9  1995/04/21  16:37:43  adam
75  * Parent (eti) creates BOTH FIFOs. dtbsun is more happy now.
76  *
77  * Revision 1.8  1995/04/20  15:12:24  adam
78  * Minor hacks really.
79  *
80  * Revision 1.7  1995/04/19  16:01:57  adam
81  * Some hacks to get the FIFO communication work!! Isn't reliable.
82  * Resource gw.account added - default account info.
83  *
84  * Revision 1.6  1995/04/17  09:34:27  adam
85  * Timeout (idletime) adjustable. Minor changes in kernel.
86  *
87  * Revision 1.5  1995/03/31  09:43:13  adam
88  * Removed unused variable.
89  *
90  * Revision 1.4  1995/03/28  11:42:34  adam
91  * First use of string-queue utility.
92  *
93  * Revision 1.3  1995/03/28  08:01:23  adam
94  * FIFO existence is used to test for a running kernel.
95  *
96  * Revision 1.2  1995/03/27  12:51:05  adam
97  * New log level in use: GW_LOG_ERRNO.
98  *
99  * Revision 1.1  1995/03/27  08:24:00  adam
100  * First use of gip interface and gw-db.
101  * First work on eti program.
102  *
103  */
104
105 #include <stdio.h>
106 #include <stdlib.h>
107 #include <assert.h>
108 #include <ctype.h>
109 #include <string.h>
110 #include <unistd.h>
111 #include <fcntl.h>
112 #include <setjmp.h>
113 #include <signal.h>
114
115 #include <sys/file.h>
116 #include <sys/stat.h>
117
118 #include <gw-log.h>
119 #include <gw-db.h>
120 #include <gw-res.h>
121 #include <gip.h>
122 #include <strqueue.h>
123
124 #define STR_LINE_MAX 1024
125
126 static char *module = "eti";
127 static jmp_buf retry_jmp;
128
129 static void pipe_handle (int dummy)
130 {
131     longjmp (retry_jmp, 1);
132 }
133
134 static int email_header (struct str_queue *sq,
135                          char *from_str, char *subject_str)
136 {
137     char *msg;
138     int index = 0;
139
140     *from_str = '\0';
141     *subject_str = '\0';    
142     while ((msg = str_queue_get (sq, index++)))
143     {
144         if (msg[0] == '\n')
145             return 0;
146         if (memcmp (msg, "From ", 5) == 0)
147             sscanf (msg+4, "%s", from_str);
148         if (memcmp (msg, "Subject: ", 9) == 0 &&
149             sscanf (msg+9, "%s", subject_str+1) == 1)
150             strcpy (subject_str, msg+9);
151     }
152     return 1;
153 }
154
155 static void start_kernel (int argc, char **argv, int id)
156 {
157     pid_t pid;
158     int i;
159     char **argv_p;
160     char userid_option[20];
161
162     argv_p = malloc (sizeof(*argv_p)*(argc+2));
163     if (!argv_p)
164     {
165         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
166         exit (1);
167     }
168     argv_p[0] = "kernel";
169     for (i = 1; i<argc; i++)
170         argv_p[i] = argv[i];
171     sprintf (userid_option, "-i%d", id);
172     argv_p[i++] = userid_option;
173     argv_p[i++] = NULL;
174
175     gw_log (GW_LOG_DEBUG, module, "Starting kernel");
176     pid = fork ();
177     if (pid == -1)
178     {
179         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
180         exit (1);
181     }
182     if (!pid)
183     {
184         execv ("kernel", argv_p);
185         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
186         exit (1);
187     }
188 }
189
190 static void deliver (struct str_queue *sq, GIP gip, const char *msg)
191 {
192     int index = 0;
193
194     gip_wline (gip, msg);
195     while ((msg = str_queue_get (sq, index++)))
196         gip_wline (gip, msg);
197     gip_wline (gip, "\001");
198 }
199
200 int main (int argc, char **argv)
201 {
202     char from_str[STR_LINE_MAX+1];
203     char subject_str[STR_LINE_MAX+1];
204     char line_str[STR_LINE_MAX+1];
205     char msg[20];
206     GW_DB user_db;
207     GIP  gip;
208     void *user_info;
209     size_t info_length;
210     int  id;
211     int  r;
212     static int pass = 0;
213     char fifo_client_name[1024];
214     char fifo_server_name[1024];
215     struct str_queue *queue;
216     int monitor_flag = 1;
217     int argno = 0;
218
219     gw_log_init (*argv);
220     while (++argno < argc)
221     {
222         if (argv[argno][0] == '-')
223         {
224             if (argv[argno][1] == '-')
225             {
226                 monitor_flag = 0;
227                 break;
228             }
229             switch (argv[argno][1])
230             {
231             case 'h':
232             case 'H':
233                 fprintf (stderr, "eti [options] -- [kernelOptions]\n");
234                 fprintf (stderr, "Options:\n");
235                 fprintf (stderr, " -l log  Set Log file\n");
236                 fprintf (stderr, " -d      Enable debugging log\n");
237                 fprintf (stderr, " -D      Enable more debugging log\n");
238                 fprintf (stderr, " -c dir  Change to directory\n");
239                 fprintf (stderr, " --      Precedes kernel options "
240                          "(kernel is invoked instead of monitor)\n");
241                 exit (1);
242             case 'l':
243                 if (argv[argno][2])
244                     gw_log_file (GW_LOG_ALL, argv[argno]+2);
245                 else if (++argno < argc)
246                     gw_log_file (GW_LOG_ALL, argv[argno]);
247                 else
248                 {
249                     fprintf (stderr, "%s: missing log filename\n", *argv);
250                     exit (1);
251                 }
252                 break;
253             case 'd':
254                 gw_log_level (GW_LOG_ALL & ~RES_DEBUG);
255                 break;
256             case 'D':
257                 gw_log_level (GW_LOG_ALL);
258                 break;
259             case 'c':
260                 if (argv[argno][2])
261                 {
262                     if (chdir (argv[argno]+2)) 
263                         gw_log (GW_LOG_WARN|GW_LOG_ERRNO, module, "chdir");
264                 } 
265                 else if (++argno < argc)
266                 {
267                     if (chdir (argv[argno])) 
268                         gw_log (GW_LOG_WARN|GW_LOG_ERRNO, module, "chdir");
269                 }
270                 else
271                 {
272                     fprintf (stderr, "%s: missing chdir name\n", *argv);
273                     exit (1);
274                 }
275                 break;
276             default:
277                 fprintf (stderr, "%s: unknown option `%s'; use -H for help\n",
278                          *argv, argv[argno]);
279                 exit (1);
280             }
281         }
282     }
283     if (!(queue = str_queue_mk ()))
284     {
285         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "str_queue_mk");
286         exit (1);
287     }
288     while (fgets (line_str, STR_LINE_MAX, stdin))
289         str_queue_enq (queue, line_str);
290
291     r = email_header (queue, from_str, subject_str);
292     if (! *from_str)
293     {
294         gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
295         exit (1);
296     }
297     gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
298     user_db = gw_db_open ("user.db", 1, 1);
299
300     r = gw_db_lookup (user_db, from_str, strlen(from_str),
301                       &user_info, &info_length);
302     if (r == 0)
303     {
304         gw_log (GW_LOG_STAT, module, "New user");
305         id = gw_db_seq_no (user_db);
306         r = gw_db_insert (user_db, from_str, strlen(from_str),
307                           &id, sizeof(id));
308         gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
309     }
310     else if (r == 1)
311     {
312         if (sizeof(id) != info_length)
313         {
314             gw_log (GW_LOG_FATAL, module, "Bad id-size");
315             exit (1);
316         }
317         memcpy (&id, user_info, sizeof(id));
318         gw_log (GW_LOG_STAT, module, "Found with id %d", id);
319     }
320     else
321     {
322         gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
323         exit (1);
324     }
325     if (monitor_flag)
326     {
327         sprintf (fifo_server_name, "fifo.s.m");
328         sprintf (fifo_client_name, "fifo.c.m");
329     }
330     else
331     {
332         sprintf (fifo_server_name, "fifo.s.%d", id);
333         sprintf (fifo_client_name, "fifo.c.%d", id);
334     }
335
336     gip = gipc_initialize (fifo_client_name);
337
338     signal (SIGPIPE, pipe_handle);
339     setjmp (retry_jmp);
340     ++pass;
341     signal (SIGPIPE, pipe_handle);
342     gw_log (GW_LOG_DEBUG, module, "Pass %d", pass);
343     if (pass == 1)
344     {
345         gipc_close (gip);
346         r = gipc_open (gip, fifo_server_name, 0);
347     }
348     else if (pass == 2)
349     {
350         if (monitor_flag)
351         {
352             gw_log (GW_LOG_FATAL, module, "Cannot contact monitor");
353             exit (1);
354         }
355         mknod (fifo_server_name, S_IFIFO|0666, 0);
356         start_kernel (argc-argno, argv+argno, id);
357         r = gipc_open (gip, fifo_server_name, 1);
358     }
359     else if (pass == 3)
360     {
361         gw_log (GW_LOG_FATAL, module, "Cannot start kernel");
362         exit (1);
363     }
364     if (r < 0)
365         if (r == -2)
366         {
367             gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
368             longjmp (retry_jmp, 1);
369         }
370         else if (r == -1)
371         {
372             gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
373             longjmp (retry_jmp, 1);
374         }
375         else
376         {
377             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "gipc_open");
378             exit (1);
379         }
380     /* deliver message ... */
381     gw_log (GW_LOG_DEBUG, module, "Delivering mail");
382     if (monitor_flag)
383         sprintf (msg, "eti %d\n", id);
384     else
385         strcpy (msg, "mail\n");
386     deliver (queue, gip, msg);
387     gw_log (GW_LOG_DEBUG, module, "Closing");
388     gipc_close (gip);
389     gipc_destroy (gip);
390     gw_db_close (user_db);
391     gw_log (GW_LOG_DEBUG, module, "Exiting");
392     exit (0);
393 }
394