Minor hacks really.
[egate.git] / kernel / eti.c
1 /* Gateway Email Transport Interface
2  * Europagate, 1995
3  *
4  * $Log: eti.c,v $
5  * Revision 1.8  1995/04/20 15:12:24  adam
6  * Minor hacks really.
7  *
8  * Revision 1.7  1995/04/19  16:01:57  adam
9  * Some hacks to get the FIFO communication work!! Isn't reliable.
10  * Resource gw.account added - default account info.
11  *
12  * Revision 1.6  1995/04/17  09:34:27  adam
13  * Timeout (idletime) adjustable. Minor changes in kernel.
14  *
15  * Revision 1.5  1995/03/31  09:43:13  adam
16  * Removed unused variable.
17  *
18  * Revision 1.4  1995/03/28  11:42:34  adam
19  * First use of string-queue utility.
20  *
21  * Revision 1.3  1995/03/28  08:01:23  adam
22  * FIFO existence is used to test for a running kernel.
23  *
24  * Revision 1.2  1995/03/27  12:51:05  adam
25  * New log level in use: GW_LOG_ERRNO.
26  *
27  * Revision 1.1  1995/03/27  08:24:00  adam
28  * First use of gip interface and gw-db.
29  * First work on eti program.
30  *
31  */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <assert.h>
36 #include <ctype.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <setjmp.h>
41 #include <signal.h>
42
43 #include <gw-log.h>
44 #include <gw-db.h>
45 #include <gip.h>
46 #include <strqueue.h>
47
48 #define LINE_MAX 1024
49
50 static char *module = "eti";
51 static jmp_buf retry_jmp;
52
53 static void pipe_handle (int dummy)
54 {
55     longjmp (retry_jmp, 1);
56 }
57
58 static int email_header (struct str_queue *sq,
59                          char *from_str, char *subject_str)
60 {
61     char *msg;
62     int index = 0;
63
64     *from_str = '\0';
65     *subject_str = '\0';    
66     while ((msg = str_queue_get (sq, index++)))
67     {
68         if (msg[0] == '\n')
69             return 0;
70         if (memcmp (msg, "From ", 5) == 0)
71             sscanf (msg+4, "%s", from_str);
72         if (memcmp (msg, "Subject: ", 9) == 0 &&
73             sscanf (msg+9, "%s", subject_str+1) == 1)
74             strcpy (subject_str, msg+9);
75     }
76     return 1;
77 }
78
79 static void start_kernel (int argc, char **argv, int id)
80 {
81     pid_t pid;
82     int i;
83     char **argv_p;
84     char userid_option[20];
85
86     argv_p = malloc (sizeof(*argv_p)*(argc+2));
87     if (!argv_p)
88     {
89         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
90         exit (1);
91     }
92     argv_p[0] = "kernel";
93     for (i = 1; i<argc; i++)
94         argv_p[i] = argv[i];
95     sprintf (userid_option, "-i%d", id);
96     argv_p[i++] = userid_option;
97     argv_p[i++] = NULL;
98
99     gw_log (GW_LOG_DEBUG, module, "Starting kernel");
100     pid = fork ();
101     if (pid == -1)
102     {
103         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
104         exit (1);
105     }
106     if (!pid)
107     {
108         execv ("kernel", argv_p);
109         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
110         exit (1);
111     }
112 }
113
114 static void deliver (struct str_queue *sq, GIP gip)
115 {
116     int index = 0;
117     char *msg;
118
119     gip_wline (gip, "mail\n");
120     while ((msg = str_queue_get (sq, index++)))
121         gip_wline (gip, msg);
122     gip_wline (gip, "\001");
123 }
124
125 int main (int argc, char **argv)
126 {
127     char from_str[LINE_MAX+1];
128     char subject_str[LINE_MAX+1];
129     char line_str[LINE_MAX+1];
130     GW_DB user_db;
131     GIP  gip;
132     void *user_info;
133     size_t info_length;
134     int  id;
135     int  r;
136     static int pass = 0;
137     char fifo_client_name[1024];
138     char fifo_server_name[1024];
139     struct str_queue *queue;
140
141     gw_log_init (*argv);
142     gw_log_level (GW_LOG_ALL);
143     gw_log_file (GW_LOG_ALL, "eti.log");
144
145     if (!(queue = str_queue_mk ()))
146     {
147         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "str_queue_mk");
148         exit (1);
149     }
150     while (fgets (line_str, LINE_MAX, stdin))
151         str_queue_enq (queue, line_str);
152
153     r = email_header (queue, from_str, subject_str);
154     if (! *from_str)
155     {
156         gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
157         exit (1);
158     }
159     gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
160     user_db = gw_db_open ("user.db", 1);
161
162     r = gw_db_lookup (user_db, from_str, strlen(from_str),
163                       &user_info, &info_length);
164     if (r == 0)
165     {
166         gw_log (GW_LOG_STAT, module, "New user");
167         id = gw_db_seq_no (user_db);
168         r = gw_db_insert (user_db, from_str, strlen(from_str),
169                           &id, sizeof(id));
170         gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
171     }
172     else if (r == 1)
173     {
174         if (sizeof(id) != info_length)
175         {
176             gw_log (GW_LOG_FATAL, module, "Bad id-size");
177             exit (1);
178         }
179         memcpy (&id, user_info, sizeof(id));
180         gw_log (GW_LOG_STAT, module, "Found with id %d", id);
181     }
182     else
183     {
184         gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
185         exit (1);
186     }
187     sprintf (fifo_server_name, "fifo.s.%d", id);
188     sprintf (fifo_client_name, "fifo.c.%d", id);
189
190     gip = gipc_initialize (fifo_client_name);
191
192     signal (SIGPIPE, pipe_handle);
193     setjmp (retry_jmp);
194     ++pass;
195     signal (SIGPIPE, pipe_handle);
196     gw_log (GW_LOG_DEBUG, module, "Pass %d", pass);
197     if (pass == 1)
198         r = gipc_open (gip, fifo_server_name, 0);
199     else if (pass == 2)
200     {
201         gipc_close (gip);
202         gipc_destroy (gip);
203         unlink (fifo_server_name);
204         unlink (fifo_client_name);
205         gip = gipc_initialize (fifo_client_name);
206         start_kernel (argc, argv, id);
207         r = gipc_open (gip, fifo_server_name, 1);
208     }
209     else if (pass == 3)
210     {
211         gw_log (GW_LOG_FATAL, module, "Cannot start kernel");
212         exit (1);
213     }
214     if (r < 0)
215         if (r == -2)
216         {
217             gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
218             longjmp (retry_jmp, 1);
219         }
220         else if (r == -1)
221         {
222             gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
223             longjmp (retry_jmp, 1);
224         }
225         else
226         {
227             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "gipc_open");
228             exit (1);
229         }
230     /* deliver message ... */
231     gw_log (GW_LOG_DEBUG, module, "Delivering mail");
232     deliver (queue, gip);
233     gw_log (GW_LOG_DEBUG, module, "Closing");
234     gipc_close (gip);
235     gipc_destroy (gip);
236     gw_db_close (user_db);
237     gw_log (GW_LOG_DEBUG, module, "Exiting");
238     exit (0);
239 }
240