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