New log level in use: GW_LOG_ERRNO.
[egate.git] / kernel / eti.c
1 /* Gateway kernel
2  * Europagate, 1995
3  *
4  * $Log: eti.c,v $
5  * Revision 1.2  1995/03/27 12:51:05  adam
6  * New log level in use: GW_LOG_ERRNO.
7  *
8  * Revision 1.1  1995/03/27  08:24:00  adam
9  * First use of gip interface and gw-db.
10  * First work on eti program.
11  *
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <assert.h>
17 #include <ctype.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21
22 #include <gw-log.h>
23 #include <gw-db.h>
24 #include <gip.h>
25
26 #define LINE_MAX 1024
27 static char line_buf[LINE_MAX+1];
28
29 static char *module = "eti";
30
31 static int email_header (FILE *inf, char *from_str, char *subject_str)
32 {
33     *from_str = '\0';
34     *subject_str = '\0';
35     while (fgets (line_buf, LINE_MAX, inf))
36     {
37         if (line_buf[0] == '\n')
38             return 0;
39         if (strncmp (line_buf, "From ", 5) == 0)
40             sscanf (line_buf+4, "%s", from_str);
41         if (strncmp (line_buf, "Subject: ", 9) == 0 &&
42             sscanf (line_buf+9, "%s", subject_str+1) == 1)
43             strcpy (subject_str, line_buf+9);
44     }
45     return 1;
46 }
47
48 static int kernel_active (int id)
49 {
50     char active_name[1024];
51     int fd;
52
53     sprintf (active_name, "kernel.pid.%d", id);
54     fd = open (active_name, O_RDONLY);
55     if (fd == -1)
56         return 0;
57     close (fd);
58     return 1;
59 }
60
61 int main (int argc, char **argv)
62 {
63     char from_str[LINE_MAX+1];
64     char subject_str[LINE_MAX+1];
65     char line_str[LINE_MAX+1];
66     GW_DB user_db;
67     GIP  gip;
68     void *user_info;
69     size_t info_length;
70     int  id;
71     int  r;
72     char fifo_client_name[1024];
73     char fifo_server_name[1024];
74
75     gw_log_init (*argv);
76     gw_log_level (GW_LOG_ALL);
77     gw_log_file (GW_LOG_ALL, "eti.log");
78     r = email_header (stdin, from_str, subject_str);
79     if (! *from_str)
80     {
81         gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
82         exit (1);
83     }
84     gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
85     user_db = gw_db_open ("user.db", 1);
86
87     r = gw_db_lookup (user_db, from_str, strlen(from_str),
88                       &user_info, &info_length);
89     if (r == 0)
90     {
91         gw_log (GW_LOG_STAT, module, "New user");
92         id = gw_db_seq_no (user_db);
93         r = gw_db_insert (user_db, from_str, strlen(from_str),
94                           &id, sizeof(id));
95         gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
96     }
97     else if (r == 1)
98     {
99         if (sizeof(id) != info_length)
100         {
101             gw_log (GW_LOG_FATAL, module, "Bad id-size");
102             exit (1);
103         }
104         memcpy (&id, user_info, sizeof(id));
105         gw_log (GW_LOG_STAT, module, "Found with id %d", id);
106     }
107     else
108     {
109         gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
110     }
111     sprintf (fifo_server_name, "fifo.s.%d", id);
112     sprintf (fifo_client_name, "fifo.c.%d", id);
113
114     gip = gipc_initialize (fifo_client_name);
115     if (kernel_active (id))
116     {
117         gw_log (GW_LOG_DEBUG, module, "Kernel already active");
118         gipc_open (gip, fifo_server_name, 0);
119     }
120     else
121     {
122         pid_t pid;
123         int i;
124         char **argv_p;
125         char userid_option[20];
126
127         argv_p = malloc (sizeof(*argv_p)*(argc+2));
128         if (!argv_p)
129         {
130             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "Malloc fail");
131             exit (1);
132         }
133         argv_p[0] = "kernel";
134         for (i = 1; i<argc; i++)
135             argv_p[i] = argv[i];
136         sprintf (userid_option, "-i%d", id);
137         argv_p[i++] = userid_option;
138         argv_p[i++] = NULL;
139
140         gw_log (GW_LOG_DEBUG, module, "Kernel not active");
141         pid = fork ();
142         if (pid == -1)
143         {
144             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
145             exit (1);
146         }
147         if (!pid)
148         {
149             execvp ("kernel", argv_p);
150             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
151             exit (1);
152         }
153         gipc_open (gip, fifo_server_name, 1);
154     }
155     /* deliver message ... */
156     gw_log (GW_LOG_DEBUG, module, "Deliver mail header");
157     gip_wline (gip, "mail\n");
158     gip_wline (gip, "From ");
159     gip_wline (gip, from_str);
160     gip_wline (gip, "\n");
161     if (*subject_str)
162     {
163         gip_wline (gip, "Subject: ");
164         gip_wline (gip, subject_str);
165         gip_wline (gip, "\n");
166     }
167     gip_wline (gip, "\n");
168     gw_log (GW_LOG_DEBUG, module, "Deliver mail body");
169     while (fgets (line_str, LINE_MAX, stdin))
170         gip_wline (gip, line_str);
171     gip_wline (gip, "\001");
172     gw_log (GW_LOG_DEBUG, module, "Closing");
173     gipc_close (gip);
174     gipc_destroy (gip);
175     gw_db_close (user_db);
176     exit (0);
177 }
178