8 |
#include <limits.h> |
#include <limits.h> |
9 |
#include <sys/stat.h> |
#include <sys/stat.h> |
10 |
#include <fcntl.h> |
#include <fcntl.h> |
11 |
|
#include <time.h> |
12 |
|
|
13 |
#include "config.h" |
#include "config.h" |
14 |
#include "imapfilter.h" |
#include "imapfilter.h" |
18 |
extern char logfile[PATH_MAX]; |
extern char logfile[PATH_MAX]; |
19 |
extern unsigned int options; |
extern unsigned int options; |
20 |
extern long timeout; |
extern long timeout; |
21 |
|
extern char *home; |
22 |
|
|
23 |
|
#ifdef ENCRYPTED_PASSWORDS |
24 |
|
char passphr[PASSPHRASE_LEN]; |
25 |
|
#endif |
26 |
|
|
27 |
|
|
28 |
/* |
/* |
31 |
int create_homedir(void) |
int create_homedir(void) |
32 |
{ |
{ |
33 |
char *hdname = ".imapfilter"; |
char *hdname = ".imapfilter"; |
34 |
char *home = NULL; |
|
|
|
|
|
home = getenv("HOME"); |
|
|
|
|
35 |
if (home) |
if (home) |
36 |
if (chdir(home)) |
if (chdir(home)) |
37 |
error("imapfilter: could not change directory; %s\n", |
error("imapfilter: could not change directory; %s\n", |
38 |
strerror(errno)); |
strerror(errno)); |
39 |
|
|
40 |
create_dir(hdname, S_IRUSR | S_IWUSR | S_IXUSR); |
create_dir(hdname, S_IRUSR | S_IWUSR | S_IXUSR); |
41 |
|
|
42 |
return 0; |
return 0; |
44 |
|
|
45 |
|
|
46 |
/* |
/* |
47 |
* Create a file. |
* Check if a file exists. |
48 |
*/ |
*/ |
49 |
int create_file(char *fname, mode_t mode) |
int exists_file(char *fname) |
50 |
{ |
{ |
|
int fd; |
|
51 |
struct stat fs; |
struct stat fs; |
52 |
|
|
53 |
|
if (access(fname, F_OK)) |
54 |
|
return 0; |
55 |
|
|
56 |
|
stat(fname, &fs); |
57 |
|
if (!S_ISREG(fs.st_mode)) { |
58 |
|
error("imapfilter: file %s not a regular file\n", fname); |
59 |
|
return ERROR_FILE_OPEN; |
60 |
|
} |
61 |
|
|
62 |
|
return 1; |
63 |
|
} |
64 |
|
|
65 |
|
|
66 |
|
/* |
67 |
|
* Check if a directory exists. |
68 |
|
*/ |
69 |
|
int exists_dir(char *dname) |
70 |
|
{ |
71 |
|
struct stat ds; |
72 |
|
|
73 |
|
if (access(dname, F_OK)) |
74 |
|
return 0; |
75 |
|
|
76 |
|
stat(dname, &ds); |
77 |
|
if (!S_ISDIR(ds.st_mode)) { |
78 |
|
error("imapfilter: file %s not a directory\n", dname); |
79 |
|
return ERROR_FILE_OPEN; |
80 |
|
} |
81 |
|
|
82 |
|
return 1; |
83 |
|
} |
84 |
|
|
85 |
if (access(fname, F_OK)) { |
|
86 |
|
/* |
87 |
|
* Create a file. |
88 |
|
*/ |
89 |
|
int create_file(char *fname, mode_t mode) |
90 |
|
{ |
91 |
|
int fd = 0; |
92 |
|
|
93 |
|
if (!exists_file(fname)) |
94 |
fd = creat(fname, mode); |
fd = creat(fname, mode); |
95 |
if (fd == -1) { |
if (fd == -1) { |
96 |
error("imapfilter: could not create file %s; %s\n", fname, |
error("imapfilter: could not create file %s; %s\n", fname, |
98 |
return ERROR_FILE_OPEN; |
return ERROR_FILE_OPEN; |
99 |
} |
} |
100 |
close(fd); |
close(fd); |
|
} else { |
|
|
stat(fname, &fs); |
|
|
if (!S_ISREG(fs.st_mode)) { |
|
|
error("imapfilter: file %s not a regular file\n", fname); |
|
|
return ERROR_FILE_OPEN; |
|
|
} |
|
|
} |
|
101 |
|
|
102 |
return 0; |
return 0; |
103 |
} |
} |
104 |
|
|
105 |
|
|
106 |
/* |
/* |
107 |
* Create a directory. |
* Create a directory. |
108 |
*/ |
*/ |
109 |
int create_dir(char *dname, mode_t mode) |
int create_dir(char *dname, mode_t mode) |
110 |
{ |
{ |
111 |
struct stat ds; |
if (!exists_dir(dname)) |
|
|
|
|
if (access(dname, F_OK)) { |
|
112 |
if (mkdir(dname, mode)) |
if (mkdir(dname, mode)) |
113 |
error("imapfilter: could not create directory %s; %s\n", dname, |
error("imapfilter: could not create directory %s; %s\n", dname, |
114 |
strerror(errno)); |
strerror(errno)); |
115 |
} else { |
|
|
stat(dname, &ds); |
|
|
if (!S_ISDIR(ds.st_mode)) |
|
|
error("imapfilter: file %s not a directory\n", dname); |
|
|
} |
|
|
|
|
116 |
return 0; |
return 0; |
117 |
} |
} |
118 |
|
|
140 |
fname, mode, fs.st_mode & 00777); |
fname, mode, fs.st_mode & 00777); |
141 |
return ERROR_TRIVIAL; |
return ERROR_TRIVIAL; |
142 |
} |
} |
|
|
|
143 |
return 0; |
return 0; |
144 |
} |
} |
145 |
|
|
150 |
int check_dir_perms(char *dname, mode_t mode) |
int check_dir_perms(char *dname, mode_t mode) |
151 |
{ |
{ |
152 |
struct stat ds; |
struct stat ds; |
153 |
|
|
154 |
if(stat(dname, &ds)) { |
if (stat(dname, &ds)) { |
155 |
error("imapfilter: getting file %s status; %s\n", dname, |
error("imapfilter: getting file %s status; %s\n", dname, |
156 |
strerror(errno)); |
strerror(errno)); |
157 |
return ERROR_TRIVIAL; |
return ERROR_TRIVIAL; |
166 |
dname, mode, ds.st_mode & 00777); |
dname, mode, ds.st_mode & 00777); |
167 |
return ERROR_TRIVIAL; |
return ERROR_TRIVIAL; |
168 |
} |
} |
|
|
|
169 |
return 0; |
return 0; |
170 |
} |
} |
171 |
#endif |
#endif |
177 |
int read_config(char *cfg) |
int read_config(char *cfg) |
178 |
{ |
{ |
179 |
int r; |
int r; |
180 |
FILE *fp; |
FILE *fd; |
|
char *home = NULL; |
|
181 |
char *c = NULL; |
char *c = NULL; |
182 |
|
|
183 |
if (!cfg) { |
if (!cfg) { |
184 |
cfg = c = (char *) xmalloc(PATH_MAX * sizeof(char)); |
cfg = c = (char *) xmalloc(PATH_MAX * sizeof(char)); |
185 |
|
|
|
home = getenv("HOME"); |
|
|
|
|
186 |
snprintf(cfg, PATH_MAX, "%s/%s", home, ".imapfilterrc"); |
snprintf(cfg, PATH_MAX, "%s/%s", home, ".imapfilterrc"); |
187 |
} |
} |
188 |
#ifdef DEBUG |
#ifdef DEBUG |
191 |
#ifdef CHECK_PERMISSIONS |
#ifdef CHECK_PERMISSIONS |
192 |
check_file_perms(cfg, S_IRUSR | S_IWUSR); |
check_file_perms(cfg, S_IRUSR | S_IWUSR); |
193 |
#endif |
#endif |
194 |
fp = fopen(cfg, "r"); |
fd = fopen(cfg, "r"); |
195 |
|
|
196 |
if (!fp) |
if (!fd) |
197 |
fatal(ERROR_FILE_OPEN, "imapfilter: opening config file %s; %s\n", |
fatal(ERROR_FILE_OPEN, "imapfilter: opening config file %s; %s\n", |
198 |
cfg, strerror(errno)); |
cfg, strerror(errno)); |
199 |
|
|
200 |
if (c) |
if (c) |
201 |
free(c); |
xfree(c); |
202 |
|
|
203 |
if ((r = parse_config(fp))) |
if ((r = parse_config(fd))) |
204 |
fatal(ERROR_CONFIG_PARSE, |
fatal(ERROR_CONFIG_PARSE, |
205 |
"imapfilter: parse error in config file at row %d\n", r); |
"imapfilter: parse error in config file at row %d\n", r); |
206 |
|
|
207 |
fclose(fp); |
fclose(fd); |
208 |
|
|
209 |
#ifdef DEBUG |
#ifdef DEBUG |
210 |
printf("debug: options: %0#10x\n", options); |
printf("debug: options: %0#10x\n", options); |
217 |
/* |
/* |
218 |
* Parse configuration file. |
* Parse configuration file. |
219 |
*/ |
*/ |
220 |
int parse_config(FILE * fp) |
int parse_config(FILE * fd) |
221 |
{ |
{ |
222 |
int i, r = 0; |
int i, r = 0; |
223 |
unsigned int row = 0; |
unsigned int row = 0; |
247 |
for (i = 0; i < 13; i++) |
for (i = 0; i < 13; i++) |
248 |
regcomp(&creg[i], reg[i], REG_EXTENDED | REG_ICASE); |
regcomp(&creg[i], reg[i], REG_EXTENDED | REG_ICASE); |
249 |
|
|
250 |
while (fgets(line, LINE_MAX - 1, fp)) { |
while (fgets(line, LINE_MAX - 1, fd)) { |
251 |
row++; |
row++; |
252 |
if (!regexec(&creg[0], line, 0, match, 0)) |
if (!regexec(&creg[0], line, 0, match, 0)) |
253 |
continue; |
continue; |
316 |
timeout = 0; |
timeout = 0; |
317 |
} |
} |
318 |
} |
} |
319 |
|
|
320 |
|
|
321 |
|
#ifdef ENCRYPTED_PASSWORDS |
322 |
|
/* |
323 |
|
* Open password file and call parse_passwords(). |
324 |
|
*/ |
325 |
|
int read_passwords(void) |
326 |
|
{ |
327 |
|
FILE *fd; |
328 |
|
char pwfile[PATH_MAX]; |
329 |
|
|
330 |
|
snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords"); |
331 |
|
#ifdef DEBUG |
332 |
|
printf("debug: passwords file: '%s'\n", pwfile); |
333 |
|
#endif |
334 |
|
|
335 |
|
if (!exists_file(pwfile)) |
336 |
|
return 1; |
337 |
|
|
338 |
|
#ifdef CHECK_PERMISSIONS |
339 |
|
check_file_perms(pwfile, S_IRUSR | S_IWUSR); |
340 |
|
#endif |
341 |
|
|
342 |
|
fd = fopen(pwfile, "r"); |
343 |
|
|
344 |
|
if (!fd) |
345 |
|
fatal(ERROR_FILE_OPEN, "imapfilter: opening passwords file %s; %s\n", |
346 |
|
pwfile, strerror(errno)); |
347 |
|
|
348 |
|
parse_passwords(fd); |
349 |
|
|
350 |
|
fclose(fd); |
351 |
|
|
352 |
|
return 0; |
353 |
|
} |
354 |
|
|
355 |
|
|
356 |
|
/* |
357 |
|
* Parse unencrypted password file. |
358 |
|
*/ |
359 |
|
int parse_passwords(FILE * fd) |
360 |
|
{ |
361 |
|
int t = 3; |
362 |
|
char *pe = NULL; |
363 |
|
char user[USERNAME_LEN], serv[SERVER_LEN]; |
364 |
|
unsigned char *buf; |
365 |
|
char *c, *cp, *line; |
366 |
|
regex_t creg; |
367 |
|
regmatch_t match[4]; |
368 |
|
const char *reg = "([[:alnum:].-]+) ([[:graph:]]+) ([[:graph:]]+)"; |
369 |
|
|
370 |
|
buf = (unsigned char *) xmalloc(DECRYPTION_BUF * sizeof(char)); |
371 |
|
|
372 |
|
do { |
373 |
|
printf("Enter master passphrase: "); |
374 |
|
get_password(passphr, PASSPHRASE_LEN); |
375 |
|
} while (decrypt_passwords(buf, fd) && --t); |
376 |
|
|
377 |
|
if (!t) |
378 |
|
return ERROR_PASSPHRASE; |
379 |
|
|
380 |
|
c = cp = xstrdup(buf); |
381 |
|
|
382 |
|
regcomp(&creg, reg, REG_EXTENDED | REG_ICASE); |
383 |
|
|
384 |
|
while ((line = strsep(&c, "\n")) && |
385 |
|
!regexec(&creg, line, 4, match, 0)) { |
386 |
|
user[0] = serv[0] = 0; |
387 |
|
|
388 |
|
strncat(serv, line + match[1].rm_so, |
389 |
|
min(match[1].rm_eo - match[1].rm_so, SERVER_LEN - 1)); |
390 |
|
strncat(user, line + match[2].rm_so, |
391 |
|
min(match[2].rm_eo - match[2].rm_so, USERNAME_LEN - 1)); |
392 |
|
|
393 |
|
if ((pe = (char *) find_password(user, serv))) |
394 |
|
strncat(pe, line + match[3].rm_so, |
395 |
|
min(match[3].rm_eo - match[3].rm_so, PASSWORD_LEN - 1)); |
396 |
|
} |
397 |
|
|
398 |
|
xfree(cp); |
399 |
|
regfree(&creg); |
400 |
|
|
401 |
|
return 0; |
402 |
|
} |
403 |
|
|
404 |
|
|
405 |
|
/* |
406 |
|
* Store encrypted passwords to file. |
407 |
|
*/ |
408 |
|
int store_passwords(account_t * accts[]) |
409 |
|
{ |
410 |
|
char pwfile[PATH_MAX]; |
411 |
|
FILE *fd; |
412 |
|
|
413 |
|
snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords"); |
414 |
|
|
415 |
|
fd = fopen(pwfile, "w"); |
416 |
|
|
417 |
|
if (!fd) |
418 |
|
fatal(ERROR_FILE_OPEN, "imapfilter: opening passwords file %s; %s\n", |
419 |
|
pwfile, strerror(errno)); |
420 |
|
|
421 |
|
encrypt_passwords(fd, accts); |
422 |
|
|
423 |
|
fclose(fd); |
424 |
|
|
425 |
|
return 0; |
426 |
|
} |
427 |
|
#endif |