1 |
#include <stdio.h> |
2 |
#include <unistd.h> |
3 |
#include <errno.h> |
4 |
#include <sys/types.h> |
5 |
#include <regex.h> |
6 |
#include <string.h> |
7 |
#include <stdlib.h> |
8 |
#include <ctype.h> |
9 |
#include <limits.h> |
10 |
#include <sys/stat.h> |
11 |
#include <fcntl.h> |
12 |
#include <time.h> |
13 |
|
14 |
#include "config.h" |
15 |
#include "imapfilter.h" |
16 |
#include "data.h" |
17 |
|
18 |
#ifdef SSL_TLS |
19 |
#include <openssl/ssl.h> |
20 |
#include <openssl/x509.h> |
21 |
#include <openssl/pem.h> |
22 |
#endif |
23 |
|
24 |
|
25 |
extern char logfile[PATH_MAX]; |
26 |
extern unsigned int options; |
27 |
extern char charset[CHARSET_LEN]; |
28 |
extern unsigned int flags; |
29 |
extern unsigned int interval; |
30 |
extern long timeout; |
31 |
extern char *home; |
32 |
|
33 |
#ifdef ENCRYPTED_PASSWORDS |
34 |
char *passphr; /* Master password to access the passwords |
35 |
* file. */ |
36 |
|
37 |
#endif |
38 |
|
39 |
|
40 |
/* |
41 |
* Find the path to configuration file, open it and call parse_config(). |
42 |
*/ |
43 |
int |
44 |
read_config(char *cfg) |
45 |
{ |
46 |
int r; |
47 |
FILE *fd; |
48 |
char *c; |
49 |
|
50 |
c = NULL; |
51 |
|
52 |
if (cfg == NULL) { |
53 |
cfg = c = (char *)xmalloc(PATH_MAX * sizeof(char)); |
54 |
snprintf(cfg, PATH_MAX, "%s/%s", home, ".imapfilterrc"); |
55 |
} |
56 |
#ifdef DEBUG |
57 |
fprintf(stderr, "debug: configuration file: '%s'\n", cfg); |
58 |
#endif |
59 |
#ifdef CHECK_PERMISSIONS |
60 |
check_file_perms(cfg, S_IRUSR | S_IWUSR); |
61 |
#endif |
62 |
fd = fopen(cfg, "r"); |
63 |
if (fd == NULL) |
64 |
fatal(ERROR_FILE_OPEN, "opening config file %s; %s\n", cfg, |
65 |
strerror(errno)); |
66 |
|
67 |
if (c != NULL) |
68 |
xfree(c); |
69 |
|
70 |
if ((r = parse_config(fd))) |
71 |
fatal(ERROR_CONFIG_PARSE, |
72 |
"parse error in config file at row %d\n", r); |
73 |
|
74 |
fclose(fd); |
75 |
|
76 |
#ifdef DEBUG |
77 |
fprintf(stderr, "debug: options: %0#10x '%s'\n", options, charset); |
78 |
#endif |
79 |
|
80 |
return 0; |
81 |
} |
82 |
|
83 |
|
84 |
/* |
85 |
* Parse configuration file. |
86 |
*/ |
87 |
int |
88 |
parse_config(FILE * fd) |
89 |
{ |
90 |
int i, r; |
91 |
unsigned int row; |
92 |
char line[LINE_MAX]; |
93 |
regex_t creg[13]; |
94 |
regmatch_t match[11]; |
95 |
const char *reg[13] = { |
96 |
"^([[:blank:]]*\n|#.*\n)$", |
97 |
|
98 |
"^[[:blank:]]*ACCOUNT[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+" |
99 |
"(([[:graph:]]+):([[:graph:]]*)|([[:graph:]]+))@" |
100 |
"([[:alnum:].-]+)(:[[:digit:]]+)?[[:blank:]]*" |
101 |
"([[:blank:]]SSL|[[:blank:]]SSL2|[[:blank:]]SSL3|" |
102 |
"[[:blank:]]TLS1)?[[:blank:]]*\n$", |
103 |
|
104 |
"^[[:blank:]]*FOLDER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+" |
105 |
"([[:print:]]+)[[:blank:]]*\n$", |
106 |
|
107 |
"^[[:blank:]]*FILTER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]*" |
108 |
"([[:blank:]]OR|[[:blank:]]AND)?[[:blank:]]*\n$", |
109 |
|
110 |
"^[[:blank:]]*ACTION[[:blank:]]+(DELETE|" |
111 |
"COPY[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)|" |
112 |
"MOVE[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)|" |
113 |
"RCOPY[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+" |
114 |
"(\"[[:print:]]*\"|[[:graph:]]+)|" |
115 |
"RMOVE[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+" |
116 |
"(\"[[:print:]]*\"|[[:graph:]]+)|" |
117 |
"FLAG[[:blank:]]+(REPLACE|ADD|REMOVE)[[:blank:]]+" |
118 |
"([[:alpha:],]+)|" |
119 |
"LIST)[[:blank:]]*([[:graph:]]*)[[:blank:]]*\n$", |
120 |
|
121 |
"^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|" |
122 |
"AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*" |
123 |
"(ANSWERED|DELETED|DRAFT|FLAGGED|NEW|OLD|RECENT|SEEN|" |
124 |
"UNANSWERED|UNDELETED|UNDRAFT|UNFLAGGED|UNSEEN)[[:blank:]]*\n$", |
125 |
|
126 |
"^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|" |
127 |
"AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*" |
128 |
"(BCC|BODY|CC|FROM|SUBJECT|TEXT|TO)[[:blank:]]+" |
129 |
"(\"[[:print:]]*\"|[[:graph:]]+)[[:blank:]]*\n$", |
130 |
|
131 |
"^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|" |
132 |
"AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*" |
133 |
"(HEADER)[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)" |
134 |
"[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)[[:blank:]]*\n$", |
135 |
|
136 |
"^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|" |
137 |
"AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*" |
138 |
"(LARGER|SMALLER|OLDER|NEWER)[[:blank:]]+([[:digit:]]+)" |
139 |
"[[:blank:]]*\n$", |
140 |
|
141 |
"^[[:blank:]]*JOB[[:blank:]]+([[:alnum:],_-]+)[[:blank:]]+" |
142 |
"([[:alnum:],_-]+)[[:blank:]]*\n$", |
143 |
|
144 |
"^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(LOGFILE|CHARSET)" |
145 |
"[[:blank:]]*=[[:blank:]]*(\"[[:print:]]*\"|[[:graph:]]+)" |
146 |
"[[:blank:]]*\n$", |
147 |
|
148 |
"^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(ERRORS|EXPUNGE|" |
149 |
"HEADERS|NAMESPACE|SUBSCRIBE|WARNING)[[:blank:]]*=[[:blank:]]*" |
150 |
"(YES|NO)[[:blank:]]*\n$", |
151 |
|
152 |
"^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(DAEMON|TIMEOUT)" |
153 |
"[[:blank:]]*=[[:blank:]]*([[:digit:]]+)\n$" |
154 |
}; |
155 |
|
156 |
r = row = 0; |
157 |
|
158 |
for (i = 0; i < 13; i++) |
159 |
regcomp(&creg[i], reg[i], REG_EXTENDED | REG_ICASE); |
160 |
|
161 |
/* First process all the variables. This is done because some |
162 |
* variables' arguments are used before the other commands are |
163 |
* processed. */ |
164 |
while (fgets(line, LINE_MAX - 1, fd)) |
165 |
if (!regexec(&creg[10], line, 4, match, 0) || |
166 |
!regexec(&creg[11], line, 4, match, 0) || |
167 |
!regexec(&creg[12], line, 4, match, 0)) |
168 |
set_options(line, match); |
169 |
|
170 |
/* Then rewind and process everything else. */ |
171 |
fseek(fd, 0L, SEEK_SET); |
172 |
while (fgets(line, LINE_MAX - 1, fd)) { |
173 |
row++; |
174 |
if (!regexec(&creg[0], line, 0, match, 0)) |
175 |
continue; |
176 |
else if (!regexec(&creg[1], line, 9, match, 0)) |
177 |
set_account(line, match); |
178 |
else if (!regexec(&creg[2], line, 3, match, 0)) |
179 |
r = set_mboxgrp(line, match); |
180 |
else if (!regexec(&creg[3], line, 3, match, 0)) |
181 |
r = set_filter(line, match); |
182 |
else if (!regexec(&creg[4], line, 11, match, 0)) |
183 |
r = set_action(line, match); |
184 |
else if (!regexec(&creg[5], line, 5, match, 0)) |
185 |
r = set_mask(line, match, MASK_MATCH_1); |
186 |
else if (!regexec(&creg[6], line, 6, match, 0)) |
187 |
r = set_mask(line, match, MASK_MATCH_2); |
188 |
else if (!regexec(&creg[7], line, 7, match, 0)) |
189 |
r = set_mask(line, match, MASK_MATCH_3); |
190 |
else if (!regexec(&creg[8], line, 6, match, 0)) |
191 |
r = set_mask(line, match, MASK_MATCH_4); |
192 |
else if (!regexec(&creg[9], line, 3, match, 0)) |
193 |
r = set_job(line, match); |
194 |
/* Skip variable processing. */ |
195 |
else if (!regexec(&creg[10], line, 4, match, 0) || |
196 |
!regexec(&creg[11], line, 4, match, 0) || |
197 |
!regexec(&creg[12], line, 4, match, 0)); |
198 |
else |
199 |
return row; |
200 |
|
201 |
if (r == ERROR_CONFIG_PARSE) |
202 |
return row; |
203 |
} |
204 |
|
205 |
for (i = 0; i < 13; i++) |
206 |
regfree(&creg[i]); |
207 |
|
208 |
destroy_data(); |
209 |
|
210 |
return 0; |
211 |
} |
212 |
|
213 |
|
214 |
/* |
215 |
* Set other options found in config file. |
216 |
*/ |
217 |
void |
218 |
set_options(char *line, regmatch_t * m) |
219 |
{ |
220 |
if (!strncasecmp(line + m[2].rm_so, "logfile", 7)) { |
221 |
if (*logfile == '\0') { |
222 |
if (*(line + m[3].rm_so) == '"' && |
223 |
*(line + m[3].rm_eo - 1) == '"') |
224 |
strncat(logfile, line + m[3].rm_so + 1, |
225 |
min((m[3].rm_eo - m[3].rm_so - 2), |
226 |
PATH_MAX - 1)); |
227 |
else |
228 |
strncat(logfile, line + m[3].rm_so, |
229 |
min((m[3].rm_eo - m[3].rm_so), |
230 |
PATH_MAX - 1)); |
231 |
} |
232 |
} else if (!strncasecmp(line + m[2].rm_so, "charset", 7)) { |
233 |
if (*(line + m[3].rm_so) == '"' && |
234 |
*(line + m[3].rm_eo - 1) == '"') |
235 |
strncat(charset, line + m[3].rm_so + 1, |
236 |
min((m[3].rm_eo - m[3].rm_so - 2), |
237 |
CHARSET_LEN - 1)); |
238 |
else |
239 |
strncat(charset, line + m[3].rm_so, |
240 |
min((m[3].rm_eo - m[3].rm_so), CHARSET_LEN - 1)); |
241 |
} else if (!strncasecmp(line + m[2].rm_so, "errors", 6)) { |
242 |
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
243 |
options |= OPTION_ERRORS; |
244 |
else |
245 |
options &= ~(OPTION_ERRORS); |
246 |
} else if (!strncasecmp(line + m[2].rm_so, "expunge", 7)) { |
247 |
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
248 |
options |= OPTION_EXPUNGE; |
249 |
else |
250 |
options &= ~(OPTION_EXPUNGE); |
251 |
} else if (!strncasecmp(line + m[2].rm_so, "header", 6)) { |
252 |
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
253 |
options |= OPTION_HEADERS; |
254 |
else |
255 |
options &= ~(OPTION_HEADERS); |
256 |
} else if (!strncasecmp(line + m[2].rm_so, "namespace", 9)) { |
257 |
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
258 |
options |= OPTION_NAMESPACE; |
259 |
else |
260 |
options &= ~(OPTION_NAMESPACE); |
261 |
} else if (!strncasecmp(line + m[2].rm_so, "subscribe", 9)) { |
262 |
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
263 |
options |= OPTION_SUBSCRIBE; |
264 |
else |
265 |
options &= ~(OPTION_SUBSCRIBE); |
266 |
} else if (!strncasecmp(line + m[2].rm_so, "warning", 7)) { |
267 |
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
268 |
options |= OPTION_WARNING; |
269 |
else |
270 |
options &= ~(OPTION_WARNING); |
271 |
} else if (!strncasecmp(line + m[2].rm_so, "timeout", 7)) { |
272 |
errno = 0; |
273 |
timeout = strtol(line + m[3].rm_so, NULL, 10); |
274 |
if (errno) |
275 |
timeout = 0; |
276 |
} else if (!strncasecmp(line + m[2].rm_so, "daemon", 6) && |
277 |
!(options & OPTION_DAEMON_MODE)) { |
278 |
options |= OPTION_DAEMON_MODE; |
279 |
errno = 0; |
280 |
interval = strtoul(line + m[3].rm_so, NULL, 10); |
281 |
if (errno) |
282 |
interval = 0; |
283 |
} |
284 |
} |
285 |
|
286 |
|
287 |
#ifdef ENCRYPTED_PASSWORDS |
288 |
/* |
289 |
* Open password file and call parse_passwords(). |
290 |
*/ |
291 |
int |
292 |
read_passwords(void) |
293 |
{ |
294 |
FILE *fd; |
295 |
char pwfile[PATH_MAX]; |
296 |
|
297 |
if (!(flags & FLAG_BLANK_PASSWORD)) |
298 |
return ERROR_CONFIG_PARSE; |
299 |
|
300 |
passphr = (char *)smalloc(PASSPHRASE_LEN); |
301 |
*passphr = '\0'; |
302 |
|
303 |
snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords"); |
304 |
#ifdef DEBUG |
305 |
fprintf(stderr, "debug: passwords file: '%s'\n", pwfile); |
306 |
#endif |
307 |
|
308 |
if (!exists_file(pwfile)) |
309 |
return ERROR_FILE_OPEN; |
310 |
|
311 |
#ifdef CHECK_PERMISSIONS |
312 |
check_file_perms(pwfile, S_IRUSR | S_IWUSR); |
313 |
#endif |
314 |
|
315 |
fd = fopen(pwfile, "r"); |
316 |
if (fd == NULL) |
317 |
fatal(ERROR_FILE_OPEN, "opening passwords file %s; %s\n", |
318 |
pwfile, strerror(errno)); |
319 |
|
320 |
parse_passwords(fd); |
321 |
|
322 |
fclose(fd); |
323 |
|
324 |
return 0; |
325 |
} |
326 |
|
327 |
|
328 |
/* |
329 |
* Parse unencrypted password file. |
330 |
*/ |
331 |
int |
332 |
parse_passwords(FILE * fd) |
333 |
{ |
334 |
int t; |
335 |
char *pe; |
336 |
char user[USERNAME_LEN], serv[SERVER_LEN]; |
337 |
unsigned char *buf; |
338 |
char *c, *cp, *line; |
339 |
regex_t creg; |
340 |
regmatch_t match[4]; |
341 |
const char *reg; |
342 |
int r; |
343 |
|
344 |
t = 3; |
345 |
pe = NULL; |
346 |
reg = "([[:alnum:].-]+) ([[:graph:]]+) ([[:graph:]]+)"; |
347 |
|
348 |
|
349 |
do { |
350 |
fseek(fd, 0L, SEEK_SET); |
351 |
printf("Enter master passphrase: "); |
352 |
get_password(passphr, PASSPHRASE_LEN); |
353 |
} while ((r = decrypt_passwords(&buf, fd)) && --t != 0); |
354 |
|
355 |
if (!t) |
356 |
return ERROR_PASSPHRASE; |
357 |
|
358 |
c = cp = sstrdup(buf); |
359 |
|
360 |
regcomp(&creg, reg, REG_EXTENDED | REG_ICASE); |
361 |
|
362 |
line = strtok_r(c, "\n", &c); |
363 |
while (line != NULL && !regexec(&creg, line, 4, match, 0)) { |
364 |
user[0] = serv[0] = '\0'; |
365 |
|
366 |
strncat(serv, line + match[1].rm_so, |
367 |
min(match[1].rm_eo - match[1].rm_so, SERVER_LEN - 1)); |
368 |
strncat(user, line + match[2].rm_so, |
369 |
min(match[2].rm_eo - match[2].rm_so, USERNAME_LEN - 1)); |
370 |
|
371 |
if ((pe = (char *)find_password(user, serv))) |
372 |
strncat(pe, line + match[3].rm_so, |
373 |
min(match[3].rm_eo - match[3].rm_so, |
374 |
PASSWORD_LEN - 1)); |
375 |
|
376 |
line = strtok_r(NULL, "\n", &c); |
377 |
} |
378 |
|
379 |
regfree(&creg); |
380 |
sfree(cp); |
381 |
sfree(buf); |
382 |
|
383 |
return 0; |
384 |
} |
385 |
|
386 |
|
387 |
/* |
388 |
* Store encrypted passwords to file. |
389 |
*/ |
390 |
int |
391 |
store_passwords(account_t * accts[]) |
392 |
{ |
393 |
char pwfile[PATH_MAX]; |
394 |
FILE *fd; |
395 |
|
396 |
snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords"); |
397 |
|
398 |
create_file(pwfile, S_IRUSR | S_IWUSR); |
399 |
|
400 |
fd = fopen(pwfile, "w"); |
401 |
|
402 |
if (fd == NULL) |
403 |
fatal(ERROR_FILE_OPEN, "opening passwords file %s; %s\n", |
404 |
pwfile, strerror(errno)); |
405 |
|
406 |
encrypt_passwords(fd, accts); |
407 |
|
408 |
fclose(fd); |
409 |
|
410 |
return 0; |
411 |
} |
412 |
|
413 |
#endif /* ENCRYPTED_PASSWORDS */ |
414 |
|
415 |
|
416 |
/* |
417 |
* Create $HOME/.imapfilter directory. |
418 |
*/ |
419 |
int |
420 |
create_homedir(void) |
421 |
{ |
422 |
char *hdn; |
423 |
|
424 |
hdn = ".imapfilter"; |
425 |
|
426 |
if (home != NULL) |
427 |
if (chdir(home)) |
428 |
error("could not change directory; %s\n", |
429 |
strerror(errno)); |
430 |
|
431 |
if (!exists_dir(hdn)) { |
432 |
if (mkdir(hdn, S_IRUSR | S_IWUSR | S_IXUSR)) |
433 |
error("could not create directory %s; %s\n", hdn, |
434 |
strerror(errno)); |
435 |
} |
436 |
#ifdef CHECK_PERMISSIONS |
437 |
else { |
438 |
check_dir_perms(hdn, S_IRUSR | S_IWUSR | S_IXUSR); |
439 |
} |
440 |
#endif |
441 |
|
442 |
return 0; |
443 |
} |
444 |
|
445 |
|
446 |
/* |
447 |
* Check if a file exists. |
448 |
*/ |
449 |
int |
450 |
exists_file(char *fname) |
451 |
{ |
452 |
struct stat fs; |
453 |
|
454 |
if (access(fname, F_OK)) |
455 |
return 0; |
456 |
|
457 |
stat(fname, &fs); |
458 |
if (!S_ISREG(fs.st_mode)) { |
459 |
error("file %s not a regular file\n", fname); |
460 |
return ERROR_FILE_OPEN; |
461 |
} |
462 |
return 1; |
463 |
} |
464 |
|
465 |
|
466 |
/* |
467 |
* Check if a directory exists. |
468 |
*/ |
469 |
int |
470 |
exists_dir(char *dname) |
471 |
{ |
472 |
struct stat ds; |
473 |
|
474 |
if (access(dname, F_OK)) |
475 |
return 0; |
476 |
|
477 |
stat(dname, &ds); |
478 |
if (!S_ISDIR(ds.st_mode)) { |
479 |
error("file %s not a directory\n", dname); |
480 |
return ERROR_FILE_OPEN; |
481 |
} |
482 |
return 1; |
483 |
} |
484 |
|
485 |
|
486 |
/* |
487 |
* Create a file with the specified permissions. |
488 |
*/ |
489 |
int |
490 |
create_file(char *fname, mode_t mode) |
491 |
{ |
492 |
int fd; |
493 |
|
494 |
fd = 0; |
495 |
|
496 |
if (!exists_file(fname)) { |
497 |
fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, mode); |
498 |
if (fd == -1) { |
499 |
error("could not create file %s; %s\n", fname, |
500 |
strerror(errno)); |
501 |
return ERROR_FILE_OPEN; |
502 |
} |
503 |
close(fd); |
504 |
} |
505 |
return 0; |
506 |
} |
507 |
|
508 |
|
509 |
#ifdef CHECK_PERMISSIONS |
510 |
/* |
511 |
* Check the permissions of a file. |
512 |
*/ |
513 |
int |
514 |
check_file_perms(char *fname, mode_t mode) |
515 |
{ |
516 |
struct stat fs; |
517 |
|
518 |
if (stat(fname, &fs)) { |
519 |
error("getting file %s status; %s\n", fname, |
520 |
strerror(errno)); |
521 |
return ERROR_TRIVIAL; |
522 |
} |
523 |
if (!S_ISREG(fs.st_mode)) { |
524 |
error("file %s not a regular file\n", fname); |
525 |
return ERROR_TRIVIAL; |
526 |
} |
527 |
if ((fs.st_mode & 00777) != mode) { |
528 |
error("warning: improper file %s permissions\n", fname); |
529 |
error("warning: file's mode should be %o not %o\n", mode, |
530 |
fs.st_mode & 00777); |
531 |
return ERROR_TRIVIAL; |
532 |
} |
533 |
return 0; |
534 |
} |
535 |
|
536 |
|
537 |
/* |
538 |
* Check the permissions of a directory. |
539 |
*/ |
540 |
int |
541 |
check_dir_perms(char *dname, mode_t mode) |
542 |
{ |
543 |
struct stat ds; |
544 |
|
545 |
if (stat(dname, &ds)) { |
546 |
error("getting file %s status; %s\n", dname, |
547 |
strerror(errno)); |
548 |
return ERROR_TRIVIAL; |
549 |
} |
550 |
if (!S_ISDIR(ds.st_mode)) { |
551 |
error("file %s not a directory\n", dname); |
552 |
return ERROR_TRIVIAL; |
553 |
} |
554 |
if ((ds.st_mode & 00777) != mode) { |
555 |
error("warning: improper dir %s permissions\n", dname); |
556 |
error("warning: dir's mode should be %o not %o\n", mode, |
557 |
ds.st_mode & 00777); |
558 |
return ERROR_TRIVIAL; |
559 |
} |
560 |
return 0; |
561 |
} |
562 |
|
563 |
#endif /* CHECK_PERMISSIONS */ |
564 |
|
565 |
|
566 |
#ifdef SSL_TLS |
567 |
/* |
568 |
* Get SSL/TLS certificate check it, maybe ask user about it and act |
569 |
* accordingly. |
570 |
*/ |
571 |
int |
572 |
imf_ssl_cert(SSL * ssl) |
573 |
{ |
574 |
X509 *cert; |
575 |
unsigned char md[EVP_MAX_MD_SIZE]; |
576 |
unsigned int mdlen; |
577 |
|
578 |
mdlen = 0; |
579 |
|
580 |
if (!(cert = SSL_get_peer_certificate(ssl))) |
581 |
return ERROR_SSL; |
582 |
|
583 |
if (!(X509_digest(cert, EVP_md5(), md, &mdlen))) |
584 |
return ERROR_SSL; |
585 |
|
586 |
switch (imf_ssl_check_cert(cert, md, &mdlen)) { |
587 |
case SSL_CERT_NONEXISTENT: |
588 |
imf_ssl_print_cert(cert, md, &mdlen); |
589 |
if (flags & FLAG_DAEMON_MODE || |
590 |
imf_ssl_new_cert(cert) == SSL_CERT_ACTION_REJECT) |
591 |
goto abort; |
592 |
break; |
593 |
case SSL_CERT_MISMATCH: |
594 |
imf_ssl_print_cert(cert, md, &mdlen); |
595 |
if (flags & FLAG_DAEMON_MODE || |
596 |
imf_ssl_cert_mismatch() == SSL_CERT_ACTION_ABORT) |
597 |
goto abort; |
598 |
break; |
599 |
case SSL_CERT_OK: |
600 |
if (options & OPTION_DETAILS_VERBOSE) |
601 |
imf_ssl_print_cert(cert, md, &mdlen); |
602 |
} |
603 |
|
604 |
X509_free(cert); |
605 |
return 0; |
606 |
|
607 |
abort: |
608 |
X509_free(cert); |
609 |
return ERROR_SSL; |
610 |
} |
611 |
|
612 |
|
613 |
/* |
614 |
* Check if the SSL/TLS certificate exists in the certificates file. |
615 |
*/ |
616 |
int |
617 |
imf_ssl_check_cert(X509 * pcert, unsigned char *pmd, unsigned int *pmdlen) |
618 |
{ |
619 |
int r; |
620 |
FILE *fd; |
621 |
X509 *cert; |
622 |
unsigned char md[EVP_MAX_MD_SIZE]; |
623 |
unsigned int mdlen; |
624 |
char *cfn; |
625 |
|
626 |
r = SSL_CERT_NONEXISTENT; |
627 |
cert = NULL; |
628 |
cfn = ".imapfilter/certificates"; |
629 |
|
630 |
if (!exists_file(cfn)) |
631 |
return SSL_CERT_NONEXISTENT; |
632 |
|
633 |
fd = fopen(cfn, "r"); |
634 |
if (fd == NULL) |
635 |
return ERROR_FILE_OPEN; |
636 |
|
637 |
while ((cert = PEM_read_X509(fd, &cert, NULL, NULL)) != NULL) { |
638 |
if (X509_subject_name_cmp(cert, pcert) != 0 || |
639 |
X509_issuer_name_cmp(cert, pcert) != 0) |
640 |
continue; |
641 |
|
642 |
if (!X509_digest(cert, EVP_md5(), md, &mdlen) || |
643 |
*pmdlen != mdlen) |
644 |
continue; |
645 |
|
646 |
if (memcmp(pmd, md, mdlen) != 0) { |
647 |
r = SSL_CERT_MISMATCH; |
648 |
break; |
649 |
} |
650 |
r = SSL_CERT_OK; |
651 |
break; |
652 |
} |
653 |
|
654 |
fclose(fd); |
655 |
X509_free(cert); |
656 |
|
657 |
return r; |
658 |
} |
659 |
|
660 |
|
661 |
/* |
662 |
* Print information about the SSL/TLS certificate. |
663 |
*/ |
664 |
void |
665 |
imf_ssl_print_cert(X509 * cert, unsigned char *md, unsigned int *mdlen) |
666 |
{ |
667 |
unsigned int i; |
668 |
char *c; |
669 |
|
670 |
c = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0); |
671 |
printf("Server certificate subject: %s\n", c); |
672 |
xfree(c); |
673 |
|
674 |
c = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0); |
675 |
printf("Server certificate issuer: %s\n", c); |
676 |
xfree(c); |
677 |
|
678 |
printf("Server key fingerprint: "); |
679 |
for (i = 0; i < *mdlen; i++) |
680 |
printf(i != *mdlen - 1 ? "%02X:" : "%02X\n", md[i]); |
681 |
} |
682 |
|
683 |
|
684 |
/* |
685 |
* Ask the user to reject/accept the SSL/TLS certificate. |
686 |
*/ |
687 |
int |
688 |
imf_ssl_new_cert(X509 * cert) |
689 |
{ |
690 |
FILE *fd; |
691 |
char c, *cfn, buf[LINE_MAX]; |
692 |
|
693 |
do { |
694 |
printf("(R)eject, accept (t)emporarily or " |
695 |
"accept (p)ermanently? "); |
696 |
fgets(buf, LINE_MAX, stdin); |
697 |
c = tolower(*buf); |
698 |
} while (c != 'r' && c != 't' && c != 'p'); |
699 |
|
700 |
if (c == 'r') |
701 |
return SSL_CERT_ACTION_REJECT; |
702 |
else if (c == 't') |
703 |
return SSL_CERT_ACTION_ACCEPT; |
704 |
|
705 |
cfn = ".imapfilter/certificates"; |
706 |
|
707 |
create_file(cfn, S_IRUSR | S_IWUSR); |
708 |
|
709 |
fd = fopen(cfn, "a"); |
710 |
if (fd == NULL) |
711 |
return SSL_CERT_ACTION_REJECT; |
712 |
|
713 |
PEM_write_X509(fd, cert); |
714 |
|
715 |
fclose(fd); |
716 |
|
717 |
return SSL_CERT_ACTION_ACCEPT; |
718 |
} |
719 |
|
720 |
|
721 |
/* |
722 |
* Ask user to proceed, while a fingerprint mismatch in the SSL/TLS |
723 |
* certificate was found. |
724 |
*/ |
725 |
int |
726 |
imf_ssl_cert_mismatch(void) |
727 |
{ |
728 |
char c, buf[LINE_MAX]; |
729 |
|
730 |
do { |
731 |
printf("WARNING: SSL/TLS certificate fingerprint mismatch.\n" |
732 |
"Proceed with the connection (y/n)? "); |
733 |
fgets(buf, LINE_MAX, stdin); |
734 |
c = tolower(*buf); |
735 |
} while (c != 'y' && c != 'n'); |
736 |
|
737 |
if (c == 'y') |
738 |
return SSL_CERT_ACTION_CONTINUE; |
739 |
else |
740 |
return SSL_CERT_ACTION_ABORT; |
741 |
} |
742 |
|
743 |
#endif /* SSL_TLS */ |