/[imapfilter]/imapfilter/file.c
ViewVC logotype

Annotation of /imapfilter/file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.50 - (hide annotations)
Sat Mar 22 15:09:33 2003 UTC (21 years ago) by lefcha
Branch: MAIN
Changes since 1.49: +186 -1 lines
File MIME type: text/plain
Added SSL/TLS certificate checking.

1 lefcha 1.1 #include <stdio.h>
2 lefcha 1.28 #include <unistd.h>
3 lefcha 1.1 #include <errno.h>
4 lefcha 1.5 #include <sys/types.h>
5 lefcha 1.1 #include <regex.h>
6     #include <string.h>
7     #include <stdlib.h>
8 lefcha 1.50 #include <ctype.h>
9 lefcha 1.1 #include <limits.h>
10 lefcha 1.10 #include <sys/stat.h>
11 lefcha 1.28 #include <fcntl.h>
12 lefcha 1.29 #include <time.h>
13 lefcha 1.1
14     #include "config.h"
15     #include "imapfilter.h"
16 lefcha 1.13 #include "data.h"
17 lefcha 1.1
18 lefcha 1.50 #ifdef SSL_TLS
19     #include <openssl/ssl.h>
20     #include <openssl/x509.h>
21     #include <openssl/pem.h>
22     #endif
23    
24 lefcha 1.1
25 lefcha 1.6 extern char logfile[PATH_MAX];
26 lefcha 1.11 extern unsigned int options;
27 lefcha 1.46 extern char charset[CHARSET_LEN];
28 lefcha 1.34 extern unsigned int flags;
29     extern unsigned int interval;
30 lefcha 1.28 extern long timeout;
31 lefcha 1.29 extern char *home;
32    
33     #ifdef ENCRYPTED_PASSWORDS
34 lefcha 1.38 char *passphr; /* Master password to access the passwords
35 lefcha 1.48 * file. */
36    
37 lefcha 1.28 #endif
38 lefcha 1.27
39 lefcha 1.1
40     /*
41 lefcha 1.13 * Find the path to configuration file, open it and call parse_config().
42 lefcha 1.1 */
43 lefcha 1.48 int
44     read_config(char *cfg)
45 lefcha 1.1 {
46 lefcha 1.48 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 lefcha 1.1 #ifdef DEBUG
57 lefcha 1.48 fprintf(stderr, "debug: configuration file: '%s'\n", cfg);
58 lefcha 1.1 #endif
59 lefcha 1.13 #ifdef CHECK_PERMISSIONS
60 lefcha 1.48 check_file_perms(cfg, S_IRUSR | S_IWUSR);
61 lefcha 1.12 #endif
62 lefcha 1.48 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 lefcha 1.1
74 lefcha 1.48 fclose(fd);
75 lefcha 1.1
76 lefcha 1.11 #ifdef DEBUG
77 lefcha 1.48 fprintf(stderr, "debug: options: %0#10x '%s'\n", options, charset);
78 lefcha 1.11 #endif
79    
80 lefcha 1.48 return 0;
81 lefcha 1.1 }
82    
83 lefcha 1.6
84 lefcha 1.10 /*
85 lefcha 1.13 * Parse configuration file.
86 lefcha 1.1 */
87 lefcha 1.48 int
88     parse_config(FILE * fd)
89 lefcha 1.1 {
90 lefcha 1.48 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 lefcha 1.3
170 lefcha 1.48 /* 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 lefcha 1.18
205 lefcha 1.48 for (i = 0; i < 13; i++)
206     regfree(&creg[i]);
207 lefcha 1.6
208 lefcha 1.48 destroy_data();
209    
210     return 0;
211 lefcha 1.1 }
212    
213    
214     /*
215 lefcha 1.13 * Set other options found in config file.
216 lefcha 1.11 */
217 lefcha 1.48 void
218     set_options(char *line, regmatch_t * m)
219 lefcha 1.11 {
220 lefcha 1.48 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 lefcha 1.1 }
285 lefcha 1.29
286    
287     #ifdef ENCRYPTED_PASSWORDS
288     /*
289     * Open password file and call parse_passwords().
290     */
291 lefcha 1.48 int
292     read_passwords(void)
293 lefcha 1.29 {
294 lefcha 1.48 FILE *fd;
295     char pwfile[PATH_MAX];
296 lefcha 1.38
297 lefcha 1.48 if (!(flags & FLAG_BLANK_PASSWORD))
298     return ERROR_CONFIG_PARSE;
299 lefcha 1.38
300 lefcha 1.48 passphr = (char *)smalloc(PASSPHRASE_LEN);
301     *passphr = '\0';
302 lefcha 1.29
303 lefcha 1.48 snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords");
304 lefcha 1.29 #ifdef DEBUG
305 lefcha 1.48 fprintf(stderr, "debug: passwords file: '%s'\n", pwfile);
306 lefcha 1.29 #endif
307 lefcha 1.38
308 lefcha 1.48 if (!exists_file(pwfile))
309     return ERROR_FILE_OPEN;
310 lefcha 1.38
311 lefcha 1.29 #ifdef CHECK_PERMISSIONS
312 lefcha 1.48 check_file_perms(pwfile, S_IRUSR | S_IWUSR);
313 lefcha 1.29 #endif
314    
315 lefcha 1.48 fd = fopen(pwfile, "r");
316     if (fd == NULL)
317     fatal(ERROR_FILE_OPEN, "opening passwords file %s; %s\n",
318     pwfile, strerror(errno));
319 lefcha 1.29
320 lefcha 1.48 parse_passwords(fd);
321 lefcha 1.29
322 lefcha 1.48 fclose(fd);
323 lefcha 1.29
324 lefcha 1.48 return 0;
325 lefcha 1.29 }
326    
327    
328     /*
329     * Parse unencrypted password file.
330     */
331 lefcha 1.48 int
332     parse_passwords(FILE * fd)
333 lefcha 1.29 {
334 lefcha 1.48 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 lefcha 1.29
376 lefcha 1.48 line = strtok_r(NULL, "\n", &c);
377     }
378 lefcha 1.38
379 lefcha 1.48 regfree(&creg);
380     sfree(cp);
381     sfree(buf);
382 lefcha 1.29
383 lefcha 1.48 return 0;
384 lefcha 1.29 }
385    
386    
387     /*
388     * Store encrypted passwords to file.
389     */
390 lefcha 1.48 int
391     store_passwords(account_t * accts[])
392 lefcha 1.29 {
393 lefcha 1.48 char pwfile[PATH_MAX];
394     FILE *fd;
395 lefcha 1.29
396 lefcha 1.48 snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords");
397 lefcha 1.38
398 lefcha 1.48 create_file(pwfile, S_IRUSR | S_IWUSR);
399 lefcha 1.29
400 lefcha 1.48 fd = fopen(pwfile, "w");
401 lefcha 1.29
402 lefcha 1.48 if (fd == NULL)
403     fatal(ERROR_FILE_OPEN, "opening passwords file %s; %s\n",
404     pwfile, strerror(errno));
405 lefcha 1.38
406 lefcha 1.48 encrypt_passwords(fd, accts);
407 lefcha 1.38
408 lefcha 1.48 fclose(fd);
409 lefcha 1.29
410 lefcha 1.48 return 0;
411 lefcha 1.29 }
412 lefcha 1.48
413 lefcha 1.47 #endif /* ENCRYPTED_PASSWORDS */
414 lefcha 1.31
415    
416     /*
417     * Create $HOME/.imapfilter directory.
418     */
419 lefcha 1.48 int
420     create_homedir(void)
421 lefcha 1.31 {
422 lefcha 1.48 char *hdn;
423 lefcha 1.38
424 lefcha 1.48 hdn = ".imapfilter";
425 lefcha 1.31
426 lefcha 1.48 if (home != NULL)
427     if (chdir(home))
428     error("could not change directory; %s\n",
429     strerror(errno));
430    
431 lefcha 1.49 if (!exists_dir(hdn)) {
432 lefcha 1.48 if (mkdir(hdn, S_IRUSR | S_IWUSR | S_IXUSR))
433     error("could not create directory %s; %s\n", hdn,
434     strerror(errno));
435 lefcha 1.49 }
436     #ifdef CHECK_PERMISSIONS
437     else {
438     check_dir_perms(hdn, S_IRUSR | S_IWUSR | S_IXUSR);
439     }
440     #endif
441 lefcha 1.36
442 lefcha 1.48 return 0;
443 lefcha 1.31 }
444    
445    
446     /*
447     * Check if a file exists.
448     */
449 lefcha 1.48 int
450     exists_file(char *fname)
451 lefcha 1.31 {
452 lefcha 1.48 struct stat fs;
453 lefcha 1.38
454 lefcha 1.48 if (access(fname, F_OK))
455     return 0;
456 lefcha 1.38
457 lefcha 1.48 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 lefcha 1.31 }
464    
465    
466 lefcha 1.38 /*
467 lefcha 1.31 * Check if a directory exists.
468     */
469 lefcha 1.48 int
470     exists_dir(char *dname)
471 lefcha 1.31 {
472 lefcha 1.48 struct stat ds;
473 lefcha 1.38
474 lefcha 1.48 if (access(dname, F_OK))
475     return 0;
476 lefcha 1.38
477 lefcha 1.48 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 lefcha 1.31 }
484    
485    
486     /*
487 lefcha 1.36 * Create a file with the specified permissions.
488 lefcha 1.31 */
489 lefcha 1.48 int
490     create_file(char *fname, mode_t mode)
491 lefcha 1.31 {
492 lefcha 1.48 int fd;
493    
494     fd = 0;
495 lefcha 1.38
496 lefcha 1.48 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 lefcha 1.31 }
507    
508    
509     #ifdef CHECK_PERMISSIONS
510     /*
511     * Check the permissions of a file.
512     */
513 lefcha 1.48 int
514     check_file_perms(char *fname, mode_t mode)
515 lefcha 1.31 {
516 lefcha 1.48 struct stat fs;
517 lefcha 1.38
518 lefcha 1.48 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 lefcha 1.31 }
535    
536    
537     /*
538     * Check the permissions of a directory.
539     */
540 lefcha 1.48 int
541     check_dir_perms(char *dname, mode_t mode)
542 lefcha 1.31 {
543 lefcha 1.48 struct stat ds;
544 lefcha 1.31
545 lefcha 1.48 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 lefcha 1.50 error("warning: dir's mode should be %o not %o\n", mode,
557 lefcha 1.48 ds.st_mode & 00777);
558     return ERROR_TRIVIAL;
559     }
560     return 0;
561 lefcha 1.31 }
562 lefcha 1.38
563 lefcha 1.47 #endif /* CHECK_PERMISSIONS */
564 lefcha 1.50
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 (imf_ssl_new_cert(cert) == SSL_CERT_ACTION_REJECT)
590     goto abort;
591     break;
592     case SSL_CERT_MISMATCH:
593     imf_ssl_print_cert(cert, md, &mdlen);
594     if (imf_ssl_cert_mismatch() == SSL_CERT_ACTION_ABORT)
595     goto abort;
596     break;
597     case SSL_CERT_OK:
598     if (options & OPTION_DETAILS_VERBOSE)
599     imf_ssl_print_cert(cert, md, &mdlen);
600     }
601    
602     X509_free(cert);
603     return 0;
604    
605     abort:
606     X509_free(cert);
607     return ERROR_SSL;
608     }
609    
610    
611     /*
612     * Check if the SSL/TLS certificate exists in the certificates file.
613     */
614     int
615     imf_ssl_check_cert(X509 * pcert, unsigned char *pmd, unsigned int *pmdlen)
616     {
617     int r;
618     FILE *fd;
619     X509 *cert;
620     unsigned char md[EVP_MAX_MD_SIZE];
621     unsigned int mdlen;
622     char *cfn;
623    
624     r = SSL_CERT_NONEXISTENT;
625     cert = NULL;
626     cfn = ".imapfilter/certificates";
627    
628     if (!exists_file(cfn))
629     return SSL_CERT_NONEXISTENT;
630    
631     fd = fopen(cfn, "r");
632     if (fd == NULL)
633     return ERROR_FILE_OPEN;
634    
635     while ((cert = PEM_read_X509(fd, &cert, NULL, NULL)) != NULL) {
636     if (X509_subject_name_cmp(cert, pcert) != 0 ||
637     X509_issuer_name_cmp(cert, pcert) != 0)
638     continue;
639    
640     if (!X509_digest(cert, EVP_md5(), md, &mdlen) ||
641     *pmdlen != mdlen)
642     continue;
643    
644     if (memcmp(pmd, md, mdlen) != 0) {
645     r = SSL_CERT_MISMATCH;
646     break;
647     }
648     r = SSL_CERT_OK;
649     break;
650     }
651    
652     fclose(fd);
653     X509_free(cert);
654    
655     return r;
656     }
657    
658    
659     /*
660     * Print information about the SSL/TLS certificate.
661     */
662     void
663     imf_ssl_print_cert(X509 * cert, unsigned char *md, unsigned int *mdlen)
664     {
665     unsigned int i;
666     char *c;
667    
668     c = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
669     printf("Server certificate subject: %s\n", c);
670     xfree(c);
671    
672     c = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0);
673     printf("Server certificate issuer: %s\n", c);
674     xfree(c);
675    
676     printf("Server key fingerprint: ");
677     for (i = 0; i < *mdlen; i++)
678     printf(i != *mdlen - 1 ? "%02X:" : "%02X\n", md[i]);
679     }
680    
681    
682     /*
683     * Ask the user to reject/accept the SSL/TLS certificate.
684     */
685     int
686     imf_ssl_new_cert(X509 * cert)
687     {
688     FILE *fd;
689     char c, *cfn, buf[LINE_MAX];
690    
691     do {
692     printf("(R)eject, accept (t)emporarily or "
693     "accept (p)ermanently? ");
694     fgets(buf, LINE_MAX, stdin);
695     c = tolower(*buf);
696     } while (c != 'r' && c != 't' && c != 'p');
697    
698     if (c == 'r')
699     return SSL_CERT_ACTION_REJECT;
700     else if (c == 't')
701     return SSL_CERT_ACTION_ACCEPT;
702    
703     cfn = ".imapfilter/certificates";
704    
705     create_file(cfn, S_IRUSR | S_IWUSR);
706    
707     fd = fopen(cfn, "a");
708     if (fd == NULL)
709     return SSL_CERT_ACTION_REJECT;
710    
711     PEM_write_X509(fd, cert);
712    
713     fclose(fd);
714    
715     return SSL_CERT_ACTION_ACCEPT;
716     }
717    
718    
719     /*
720     * Ask user to proceed, while a fingerprint mismatch in the SSL/TLS
721     * certificate was found.
722     */
723     int
724     imf_ssl_cert_mismatch(void)
725     {
726     char c, buf[LINE_MAX];
727    
728     do {
729     printf("WARNING: SSL/TLS certificate fingerprint mismatch.\n"
730     "Proceed with the connection (y/n)? ");
731     fgets(buf, LINE_MAX, stdin);
732     c = tolower(*buf);
733     } while (c != 'y' && c != 'n');
734    
735     if (c == 'y')
736     return SSL_CERT_ACTION_CONTINUE;
737     else
738     return SSL_CERT_ACTION_ABORT;
739     }
740    
741     #endif /* SSL_TLS */

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26