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