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