1 /* Gateway Email Transport Interface
5 * Revision 1.9 1995/04/21 16:37:43 adam
6 * Parent (eti) creates BOTH FIFOs. dtbsun is more happy now.
8 * Revision 1.8 1995/04/20 15:12:24 adam
11 * Revision 1.7 1995/04/19 16:01:57 adam
12 * Some hacks to get the FIFO communication work!! Isn't reliable.
13 * Resource gw.account added - default account info.
15 * Revision 1.6 1995/04/17 09:34:27 adam
16 * Timeout (idletime) adjustable. Minor changes in kernel.
18 * Revision 1.5 1995/03/31 09:43:13 adam
19 * Removed unused variable.
21 * Revision 1.4 1995/03/28 11:42:34 adam
22 * First use of string-queue utility.
24 * Revision 1.3 1995/03/28 08:01:23 adam
25 * FIFO existence is used to test for a running kernel.
27 * Revision 1.2 1995/03/27 12:51:05 adam
28 * New log level in use: GW_LOG_ERRNO.
30 * Revision 1.1 1995/03/27 08:24:00 adam
31 * First use of gip interface and gw-db.
32 * First work on eti program.
56 static char *module = "eti";
57 static jmp_buf retry_jmp;
59 static void pipe_handle (int dummy)
61 longjmp (retry_jmp, 1);
64 static int email_header (struct str_queue *sq,
65 char *from_str, char *subject_str)
72 while ((msg = str_queue_get (sq, index++)))
76 if (memcmp (msg, "From ", 5) == 0)
77 sscanf (msg+4, "%s", from_str);
78 if (memcmp (msg, "Subject: ", 9) == 0 &&
79 sscanf (msg+9, "%s", subject_str+1) == 1)
80 strcpy (subject_str, msg+9);
85 static void start_kernel (int argc, char **argv, int id)
90 char userid_option[20];
92 argv_p = malloc (sizeof(*argv_p)*(argc+2));
95 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
99 for (i = 1; i<argc; i++)
101 sprintf (userid_option, "-i%d", id);
102 argv_p[i++] = userid_option;
105 gw_log (GW_LOG_DEBUG, module, "Starting kernel");
109 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
114 execv ("kernel", argv_p);
115 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
120 static void deliver (struct str_queue *sq, GIP gip)
125 gip_wline (gip, "mail\n");
126 while ((msg = str_queue_get (sq, index++)))
127 gip_wline (gip, msg);
128 gip_wline (gip, "\001");
131 int main (int argc, char **argv)
133 char from_str[LINE_MAX+1];
134 char subject_str[LINE_MAX+1];
135 char line_str[LINE_MAX+1];
143 char fifo_client_name[1024];
144 char fifo_server_name[1024];
145 struct str_queue *queue;
148 gw_log_level (GW_LOG_ALL);
149 gw_log_file (GW_LOG_ALL, "eti.log");
151 if (!(queue = str_queue_mk ()))
153 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "str_queue_mk");
156 while (fgets (line_str, LINE_MAX, stdin))
157 str_queue_enq (queue, line_str);
159 r = email_header (queue, from_str, subject_str);
162 gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
165 gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
166 user_db = gw_db_open ("user.db", 1);
168 r = gw_db_lookup (user_db, from_str, strlen(from_str),
169 &user_info, &info_length);
172 gw_log (GW_LOG_STAT, module, "New user");
173 id = gw_db_seq_no (user_db);
174 r = gw_db_insert (user_db, from_str, strlen(from_str),
176 gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
180 if (sizeof(id) != info_length)
182 gw_log (GW_LOG_FATAL, module, "Bad id-size");
185 memcpy (&id, user_info, sizeof(id));
186 gw_log (GW_LOG_STAT, module, "Found with id %d", id);
190 gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
193 sprintf (fifo_server_name, "fifo.s.%d", id);
194 sprintf (fifo_client_name, "fifo.c.%d", id);
196 gip = gipc_initialize (fifo_client_name);
198 signal (SIGPIPE, pipe_handle);
201 signal (SIGPIPE, pipe_handle);
202 gw_log (GW_LOG_DEBUG, module, "Pass %d", pass);
204 r = gipc_open (gip, fifo_server_name, 0);
210 unlink (fifo_server_name);
211 unlink (fifo_client_name);
212 gip = gipc_initialize (fifo_client_name);
214 mknod (fifo_server_name, S_IFIFO|0666, 0);
215 start_kernel (argc, argv, id);
216 r = gipc_open (gip, fifo_server_name, 1);
220 gw_log (GW_LOG_FATAL, module, "Cannot start kernel");
226 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
227 longjmp (retry_jmp, 1);
231 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
232 longjmp (retry_jmp, 1);
236 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "gipc_open");
239 /* deliver message ... */
240 gw_log (GW_LOG_DEBUG, module, "Delivering mail");
241 deliver (queue, gip);
242 gw_log (GW_LOG_DEBUG, module, "Closing");
245 gw_db_close (user_db);
246 gw_log (GW_LOG_DEBUG, module, "Exiting");