1 |
lefcha |
1.1 |
#include <stdio.h> |
2 |
lefcha |
1.53 |
#include <stdlib.h> |
3 |
lefcha |
1.55 |
#include <sys/types.h> |
4 |
|
|
#include <unistd.h> |
5 |
lefcha |
1.52 |
#include <string.h> |
6 |
|
|
#include <errno.h> |
7 |
lefcha |
1.7 |
#include <limits.h> |
8 |
lefcha |
1.58 |
#include <fcntl.h> |
9 |
lefcha |
1.35 |
#include <setjmp.h> |
10 |
lefcha |
1.40 |
#include <locale.h> |
11 |
lefcha |
1.1 |
|
12 |
lefcha |
1.39 |
#include "config.h" |
13 |
|
|
#include "imapfilter.h" |
14 |
lefcha |
1.52 |
#include "version.h" |
15 |
|
|
#include "account.h" |
16 |
|
|
#include "filter.h" |
17 |
|
|
#include "buffer.h" |
18 |
lefcha |
1.39 |
|
19 |
lefcha |
1.53 |
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5 |
20 |
lefcha |
1.55 |
#include <openssl/opensslv.h> |
21 |
lefcha |
1.38 |
#include <openssl/crypto.h> |
22 |
|
|
#endif |
23 |
lefcha |
1.1 |
|
24 |
|
|
|
25 |
lefcha |
1.7 |
extern account_t *accounts; |
26 |
lefcha |
1.11 |
extern filter_t *filters; |
27 |
lefcha |
1.50 |
extern buffer_t ibuf, obuf; |
28 |
lefcha |
1.7 |
|
29 |
lefcha |
1.64 |
opts_t opts; /* Program opts. */ |
30 |
lefcha |
1.29 |
unsigned int flags = 0; /* Program flags. */ |
31 |
lefcha |
1.64 |
|
32 |
lefcha |
1.51 |
conn_t connpri, connaux; /* Primary and auxiliary IMAP connection. */ |
33 |
lefcha |
1.41 |
|
34 |
lefcha |
1.25 |
char *home = NULL; /* User's home directory. */ |
35 |
lefcha |
1.42 |
|
36 |
lefcha |
1.46 |
jmp_buf acctloop; /* Non-local exit in case of network error. */ |
37 |
lefcha |
1.1 |
|
38 |
lefcha |
1.29 |
|
39 |
lefcha |
1.52 |
void usage(void); |
40 |
|
|
void version(void); |
41 |
|
|
|
42 |
|
|
|
43 |
lefcha |
1.2 |
/* |
44 |
lefcha |
1.46 |
* IMAPFilter: an IMAP mail filtering utility. |
45 |
lefcha |
1.2 |
*/ |
46 |
lefcha |
1.42 |
int |
47 |
|
|
main(int argc, char *argv[]) |
48 |
lefcha |
1.1 |
{ |
49 |
lefcha |
1.58 |
int c, r; |
50 |
lefcha |
1.64 |
char *cf; /* Configuration file. */ |
51 |
lefcha |
1.42 |
account_t *ca; /* Current account. */ |
52 |
|
|
mbox_t *cm; /* Current mailbox. */ |
53 |
lefcha |
1.33 |
|
54 |
lefcha |
1.45 |
setlocale(LC_ALL, ""); |
55 |
lefcha |
1.40 |
|
56 |
lefcha |
1.64 |
opts.debug = 0; |
57 |
|
|
opts.verbosity = 0; |
58 |
|
|
opts.timeout = -1; |
59 |
|
|
opts.daemon = 0; |
60 |
|
|
opts.headers = 0; |
61 |
|
|
opts.errors = 0; |
62 |
|
|
opts.namespace = 1; |
63 |
|
|
opts.expunge = 0; |
64 |
|
|
opts.subscribe = 0; |
65 |
|
|
opts.peek = 1; |
66 |
|
|
opts.passwd_editor = 0; |
67 |
|
|
opts.charset[0] = '\0'; |
68 |
|
|
opts.logfile[0] = '\0'; |
69 |
|
|
|
70 |
lefcha |
1.42 |
home = getenv("HOME"); |
71 |
lefcha |
1.64 |
cf = NULL; |
72 |
lefcha |
1.55 |
connpri.sock = connaux.sock = -1; |
73 |
lefcha |
1.33 |
|
74 |
lefcha |
1.63 |
while ((c = getopt(argc, argv, "DVc:d:kl:" |
75 |
lefcha |
1.29 |
#ifdef ENCRYPTED_PASSWORDS |
76 |
lefcha |
1.42 |
"p" |
77 |
lefcha |
1.25 |
#endif |
78 |
lefcha |
1.63 |
"qv")) != -1) { |
79 |
lefcha |
1.42 |
switch (c) { |
80 |
lefcha |
1.63 |
case 'D': |
81 |
lefcha |
1.64 |
if (opts.debug < 2) |
82 |
|
|
opts.debug++; |
83 |
lefcha |
1.63 |
break; |
84 |
|
|
case 'V': |
85 |
|
|
version(); |
86 |
|
|
/* NOTREACHED */ |
87 |
lefcha |
1.42 |
case 'c': |
88 |
lefcha |
1.64 |
cf = optarg; |
89 |
lefcha |
1.42 |
break; |
90 |
|
|
case 'd': |
91 |
|
|
errno = 0; |
92 |
lefcha |
1.64 |
opts.daemon = strtoul(optarg, NULL, 10); |
93 |
lefcha |
1.42 |
if (errno) |
94 |
lefcha |
1.64 |
opts.daemon = 0; |
95 |
lefcha |
1.42 |
break; |
96 |
|
|
case 'k': |
97 |
|
|
kill_imapfilter(); |
98 |
|
|
break; |
99 |
|
|
case 'l': |
100 |
lefcha |
1.64 |
strncat(opts.logfile, optarg, PATH_MAX - 1); |
101 |
lefcha |
1.42 |
break; |
102 |
lefcha |
1.25 |
#ifdef ENCRYPTED_PASSWORDS |
103 |
lefcha |
1.42 |
case 'p': |
104 |
lefcha |
1.64 |
opts.passwd_editor = 1; |
105 |
lefcha |
1.42 |
break; |
106 |
|
|
#endif |
107 |
|
|
case 'q': |
108 |
lefcha |
1.64 |
if (opts.verbosity > -2) |
109 |
|
|
opts.verbosity--; |
110 |
lefcha |
1.42 |
break; |
111 |
|
|
case 'v': |
112 |
lefcha |
1.64 |
if (opts.verbosity < 2) |
113 |
|
|
opts.verbosity++; |
114 |
lefcha |
1.42 |
break; |
115 |
|
|
default: |
116 |
lefcha |
1.63 |
case '?': |
117 |
lefcha |
1.42 |
usage(); |
118 |
lefcha |
1.63 |
/* NOTREACHED */ |
119 |
lefcha |
1.42 |
} |
120 |
lefcha |
1.1 |
} |
121 |
lefcha |
1.32 |
|
122 |
lefcha |
1.60 |
debug_start(); |
123 |
|
|
|
124 |
lefcha |
1.42 |
create_homedir(); |
125 |
lefcha |
1.33 |
|
126 |
lefcha |
1.42 |
lockfile_check(); |
127 |
|
|
lockfile_create(); |
128 |
lefcha |
1.28 |
|
129 |
lefcha |
1.64 |
if (!opts.debug) |
130 |
lefcha |
1.60 |
corefile_disable(); |
131 |
lefcha |
1.33 |
|
132 |
lefcha |
1.42 |
tty_store(); |
133 |
|
|
catch_signals(); |
134 |
lefcha |
1.24 |
|
135 |
lefcha |
1.64 |
read_config(cf); |
136 |
lefcha |
1.33 |
|
137 |
lefcha |
1.25 |
#ifdef ENCRYPTED_PASSWORDS |
138 |
lefcha |
1.42 |
read_passwords(); |
139 |
lefcha |
1.33 |
|
140 |
lefcha |
1.64 |
if (opts.passwd_editor) { |
141 |
lefcha |
1.42 |
password_editor(); |
142 |
lefcha |
1.33 |
|
143 |
lefcha |
1.42 |
secmem_clear(); |
144 |
|
|
lockfile_remove(); |
145 |
lefcha |
1.33 |
|
146 |
lefcha |
1.42 |
exit(0); |
147 |
|
|
} |
148 |
lefcha |
1.25 |
#endif |
149 |
lefcha |
1.33 |
|
150 |
lefcha |
1.60 |
log_start(); |
151 |
lefcha |
1.32 |
|
152 |
lefcha |
1.50 |
init_buffer(&ibuf); |
153 |
|
|
init_buffer(&obuf); |
154 |
lefcha |
1.32 |
|
155 |
lefcha |
1.64 |
if (opts.daemon) |
156 |
|
|
opts.verbosity = -2; |
157 |
lefcha |
1.62 |
|
158 |
lefcha |
1.42 |
do { |
159 |
|
|
for (ca = accounts; ca != NULL; ca = ca->next) { |
160 |
lefcha |
1.7 |
|
161 |
lefcha |
1.42 |
if (setjmp(acctloop)) |
162 |
|
|
continue; |
163 |
lefcha |
1.35 |
|
164 |
lefcha |
1.51 |
if (init_connection(&connpri, ca->server, ca->port, |
165 |
lefcha |
1.42 |
ca->ssl)) |
166 |
|
|
continue; |
167 |
lefcha |
1.21 |
|
168 |
lefcha |
1.51 |
r = greeting_response(&connpri); |
169 |
lefcha |
1.18 |
|
170 |
lefcha |
1.64 |
if (opts.debug) |
171 |
lefcha |
1.60 |
test(&connpri); |
172 |
lefcha |
1.45 |
|
173 |
lefcha |
1.51 |
if (check_capabilities(&connpri)) |
174 |
lefcha |
1.42 |
continue; |
175 |
lefcha |
1.1 |
|
176 |
lefcha |
1.49 |
#ifdef SSL_TLS |
177 |
|
|
if (ca->ssl == SSL_DISABLED && |
178 |
lefcha |
1.51 |
connpri.caps & CAPABILITY_STARTTLS) |
179 |
lefcha |
1.52 |
if (negotiate_tls(&connpri) == RESPONSE_OK) |
180 |
lefcha |
1.51 |
check_capabilities(&connpri); |
181 |
lefcha |
1.49 |
#endif |
182 |
|
|
|
183 |
lefcha |
1.42 |
log_info(LOG_ACCOUNT, ca->key); |
184 |
lefcha |
1.1 |
|
185 |
lefcha |
1.42 |
if (r != RESPONSE_PREAUTH) { |
186 |
|
|
if (ca->passwdattr == PASSWORD_NONE) { |
187 |
|
|
printf("Enter password for %s@%s: ", |
188 |
|
|
ca->username, ca->server); |
189 |
|
|
get_password(ca->password, PASSWORD_LEN); |
190 |
|
|
ca->passwdattr = PASSWORD_PLAIN; |
191 |
|
|
} |
192 |
lefcha |
1.48 |
#ifdef CRAM_MD5 |
193 |
lefcha |
1.51 |
if (connpri.caps & CAPABILITY_AUTH_CRAM_MD5) |
194 |
lefcha |
1.52 |
r = auth_cram_md5(&connpri, |
195 |
lefcha |
1.48 |
ca->username, ca->password); |
196 |
|
|
else |
197 |
|
|
#endif |
198 |
lefcha |
1.51 |
r = login(&connpri, ca->username, |
199 |
lefcha |
1.48 |
ca->password); |
200 |
|
|
|
201 |
|
|
if (r == RESPONSE_NO) { |
202 |
lefcha |
1.42 |
error("username %s or password rejected " |
203 |
|
|
"at %s\n", ca->username, ca->server); |
204 |
|
|
continue; |
205 |
|
|
} |
206 |
|
|
} |
207 |
lefcha |
1.51 |
check_namespace(&connpri); |
208 |
lefcha |
1.42 |
|
209 |
|
|
for (cm = ca->mboxes; cm != NULL; cm = cm->next) |
210 |
|
|
if (*cm->filters == NULL) |
211 |
lefcha |
1.51 |
mailbox_status(&connpri, cm->name); |
212 |
|
|
else if (!select_mailbox(&connpri, cm->name)) { |
213 |
lefcha |
1.43 |
apply_filters(cm->name, cm->filters); |
214 |
lefcha |
1.51 |
close_mailbox(&connpri); |
215 |
lefcha |
1.42 |
} |
216 |
lefcha |
1.51 |
logout(&connpri); |
217 |
lefcha |
1.33 |
|
218 |
lefcha |
1.51 |
close_connection(&connpri); |
219 |
lefcha |
1.33 |
} |
220 |
lefcha |
1.11 |
|
221 |
lefcha |
1.42 |
/* Fork if in daemon mode. */ |
222 |
lefcha |
1.64 |
if (opts.daemon && !(flags & FLAG_DAEMON_MODE)) { |
223 |
lefcha |
1.58 |
switch (fork()) { |
224 |
|
|
case -1: |
225 |
|
|
fatal(ERROR_FORK, "forking; %s\n", |
226 |
|
|
strerror(errno)); |
227 |
|
|
break; |
228 |
|
|
case 0: |
229 |
|
|
break; |
230 |
|
|
default: |
231 |
lefcha |
1.60 |
log_stop(); |
232 |
lefcha |
1.58 |
secmem_clear(); |
233 |
lefcha |
1.60 |
debug_stop(); |
234 |
lefcha |
1.58 |
exit(0); |
235 |
|
|
break; |
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
if (setsid() == -1) |
239 |
|
|
fatal(ERROR_FORK, "creating session; %s\n", |
240 |
|
|
strerror(errno)); |
241 |
|
|
|
242 |
|
|
switch (fork()) { |
243 |
lefcha |
1.42 |
case -1: |
244 |
|
|
fatal(ERROR_FORK, "forking; %s\n", |
245 |
|
|
strerror(errno)); |
246 |
|
|
break; |
247 |
|
|
case 0: |
248 |
|
|
break; |
249 |
|
|
default: |
250 |
lefcha |
1.60 |
log_stop(); |
251 |
lefcha |
1.42 |
secmem_clear(); |
252 |
lefcha |
1.60 |
debug_stop(); |
253 |
lefcha |
1.42 |
exit(0); |
254 |
|
|
break; |
255 |
|
|
} |
256 |
lefcha |
1.58 |
|
257 |
|
|
close(STDIN_FILENO); |
258 |
|
|
close(STDOUT_FILENO); |
259 |
|
|
close(STDERR_FILENO); |
260 |
|
|
if (open("/dev/null", O_RDWR) != -1) { |
261 |
|
|
dup(STDIN_FILENO); |
262 |
|
|
dup(STDIN_FILENO); |
263 |
|
|
} |
264 |
|
|
lockfile_create(); |
265 |
|
|
corefile_disable(); |
266 |
|
|
|
267 |
|
|
flags |= FLAG_DAEMON_MODE; |
268 |
lefcha |
1.46 |
} |
269 |
lefcha |
1.64 |
if (opts.daemon && flags & FLAG_SIGUSR1_RECEIVED) { |
270 |
|
|
reread_config(cf); |
271 |
lefcha |
1.46 |
continue; |
272 |
lefcha |
1.42 |
} |
273 |
lefcha |
1.64 |
if (opts.daemon) |
274 |
|
|
sleep(opts.daemon); |
275 |
|
|
} while (opts.daemon); |
276 |
lefcha |
1.17 |
|
277 |
lefcha |
1.60 |
log_stop(); |
278 |
lefcha |
1.42 |
secmem_clear(); |
279 |
lefcha |
1.33 |
|
280 |
lefcha |
1.42 |
lockfile_remove(); |
281 |
lefcha |
1.33 |
|
282 |
lefcha |
1.60 |
debug_stop(); |
283 |
|
|
|
284 |
lefcha |
1.55 |
exit(0); |
285 |
lefcha |
1.1 |
} |
286 |
|
|
|
287 |
|
|
|
288 |
|
|
/* |
289 |
lefcha |
1.11 |
* Print a very brief usage message. |
290 |
lefcha |
1.2 |
*/ |
291 |
lefcha |
1.42 |
void |
292 |
|
|
usage(void) |
293 |
lefcha |
1.2 |
{ |
294 |
lefcha |
1.42 |
fprintf(stderr, |
295 |
lefcha |
1.63 |
"usage: imapfilter [-DVbk" |
296 |
lefcha |
1.26 |
#ifdef ENCRYPTED_PASSWORDS |
297 |
|
|
"p" |
298 |
lefcha |
1.33 |
#endif |
299 |
lefcha |
1.63 |
"qv] [-c configfile] [-d interval] [-l logfile]\n"); |
300 |
|
|
|
301 |
|
|
exit(ERROR_UNDEFINED); |
302 |
lefcha |
1.38 |
} |
303 |
|
|
|
304 |
|
|
|
305 |
|
|
/* |
306 |
|
|
* Print program's version, and if it is built in, OpenSSL's version number. |
307 |
|
|
*/ |
308 |
lefcha |
1.42 |
void |
309 |
|
|
version(void) |
310 |
lefcha |
1.38 |
{ |
311 |
lefcha |
1.42 |
fprintf(stderr, "IMAPFilter %s" |
312 |
lefcha |
1.53 |
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5 |
313 |
lefcha |
1.38 |
", OpenSSL 0x%8.8lx" |
314 |
|
|
#endif |
315 |
|
|
"\n", IMAPFILTER_VERSION |
316 |
lefcha |
1.53 |
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5 |
317 |
lefcha |
1.38 |
,SSLeay() |
318 |
|
|
#endif |
319 |
lefcha |
1.42 |
); |
320 |
lefcha |
1.63 |
|
321 |
|
|
exit(ERROR_UNDEFINED); |
322 |
lefcha |
1.1 |
} |