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