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