21 |
extern char *home; |
extern char *home; |
22 |
|
|
23 |
#ifdef ENCRYPTED_PASSWORDS |
#ifdef ENCRYPTED_PASSWORDS |
24 |
char passphr[PASSPHRASE_LEN]; |
char *passphr; /* Master password to access the |
25 |
#endif |
passwords file. */ |
|
|
|
|
|
|
|
/* |
|
|
* Create $HOME/.imapfilter directory. |
|
|
*/ |
|
|
int create_homedir(void) |
|
|
{ |
|
|
char *hdname = ".imapfilter"; |
|
|
|
|
|
if (home) |
|
|
if (chdir(home)) |
|
|
error("imapfilter: could not change directory; %s\n", |
|
|
strerror(errno)); |
|
|
|
|
|
create_dir(hdname, S_IRUSR | S_IWUSR | S_IXUSR); |
|
|
|
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
/* |
|
|
* Check if a file exists. |
|
|
*/ |
|
|
int exists_file(char *fname) |
|
|
{ |
|
|
struct stat fs; |
|
|
|
|
|
if (access(fname, F_OK)) |
|
|
return 0; |
|
|
|
|
|
stat(fname, &fs); |
|
|
if (!S_ISREG(fs.st_mode)) { |
|
|
error("imapfilter: file %s not a regular file\n", fname); |
|
|
return ERROR_FILE_OPEN; |
|
|
} |
|
|
|
|
|
return 1; |
|
|
} |
|
|
|
|
|
|
|
|
/* |
|
|
* Check if a directory exists. |
|
|
*/ |
|
|
int exists_dir(char *dname) |
|
|
{ |
|
|
struct stat ds; |
|
|
|
|
|
if (access(dname, F_OK)) |
|
|
return 0; |
|
|
|
|
|
stat(dname, &ds); |
|
|
if (!S_ISDIR(ds.st_mode)) { |
|
|
error("imapfilter: file %s not a directory\n", dname); |
|
|
return ERROR_FILE_OPEN; |
|
|
} |
|
|
|
|
|
return 1; |
|
|
} |
|
|
|
|
|
|
|
|
/* |
|
|
* Create a file. |
|
|
*/ |
|
|
int create_file(char *fname, mode_t mode) |
|
|
{ |
|
|
int fd = 0; |
|
|
|
|
|
if (!exists_file(fname)) |
|
|
fd = creat(fname, mode); |
|
|
if (fd == -1) { |
|
|
error("imapfilter: could not create file %s; %s\n", fname, |
|
|
strerror(errno)); |
|
|
return ERROR_FILE_OPEN; |
|
|
} |
|
|
close(fd); |
|
|
|
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
/* |
|
|
* Create a directory. |
|
|
*/ |
|
|
int create_dir(char *dname, mode_t mode) |
|
|
{ |
|
|
if (!exists_dir(dname)) |
|
|
if (mkdir(dname, mode)) |
|
|
error("imapfilter: could not create directory %s; %s\n", dname, |
|
|
strerror(errno)); |
|
|
|
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
#ifdef CHECK_PERMISSIONS |
|
|
/* |
|
|
* Check the permissions of a file. |
|
|
*/ |
|
|
int check_file_perms(char *fname, mode_t mode) |
|
|
{ |
|
|
struct stat fs; |
|
|
|
|
|
if (stat(fname, &fs)) { |
|
|
error("imapfilter: getting file %s status; %s\n", fname, |
|
|
strerror(errno)); |
|
|
return ERROR_TRIVIAL; |
|
|
} |
|
|
if (!S_ISREG(fs.st_mode)) { |
|
|
error("imapfilter: file %s not a regular file\n", fname); |
|
|
return ERROR_TRIVIAL; |
|
|
} |
|
|
if ((fs.st_mode & 00777) != mode) { |
|
|
error("imapfilter: warning: improper file %s permissions\n" |
|
|
"imapfilter: warning: file's mode should be %o not %o\n", |
|
|
fname, mode, fs.st_mode & 00777); |
|
|
return ERROR_TRIVIAL; |
|
|
} |
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
/* |
|
|
* Check the permissions of a directory. |
|
|
*/ |
|
|
int check_dir_perms(char *dname, mode_t mode) |
|
|
{ |
|
|
struct stat ds; |
|
|
|
|
|
if (stat(dname, &ds)) { |
|
|
error("imapfilter: getting file %s status; %s\n", dname, |
|
|
strerror(errno)); |
|
|
return ERROR_TRIVIAL; |
|
|
} |
|
|
if (!S_ISDIR(ds.st_mode)) { |
|
|
error("imapfilter: file %s not a directory\n", dname); |
|
|
return ERROR_TRIVIAL; |
|
|
} |
|
|
if ((ds.st_mode & 00777) != mode) { |
|
|
error("imapfilter: warning: improper dir %s permissions\n" |
|
|
"imapfilter: warning: file's mode should be %o not %o\n", |
|
|
dname, mode, ds.st_mode & 00777); |
|
|
return ERROR_TRIVIAL; |
|
|
} |
|
|
return 0; |
|
|
} |
|
26 |
#endif |
#endif |
27 |
|
|
28 |
|
|
86 |
#else |
#else |
87 |
"^[[:blank:]]*ACCOUNT[[:blank:]]+(([[:graph:]]+):([[:graph:]]*)|([[:graph:]]+))@([[:alnum:].-]+)(:[[:digit:]]+)?[[:blank:]]*([[:blank:]]SSL|[[:blank:]]SSL2|[[:blank:]]SSL3|[[:blank:]]TLS1)?[[:blank:]]*\n$", |
"^[[:blank:]]*ACCOUNT[[:blank:]]+(([[:graph:]]+):([[:graph:]]*)|([[:graph:]]+))@([[:alnum:].-]+)(:[[:digit:]]+)?[[:blank:]]*([[:blank:]]SSL|[[:blank:]]SSL2|[[:blank:]]SSL3|[[:blank:]]TLS1)?[[:blank:]]*\n$", |
88 |
#endif |
#endif |
89 |
"^[[:blank:]]*FOLDER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+([[:graph:]]+)[[:blank:]]*\n$", |
"^[[:blank:]]*FOLDER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+([[:print:]]+)[[:blank:]]*\n$", |
90 |
"^[[:blank:]]*FILTER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]*([[:blank:]]OR|[[:blank:]]AND)?[[:blank:]]*\n$", |
"^[[:blank:]]*FILTER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]*([[:blank:]]OR|[[:blank:]]AND)?[[:blank:]]*\n$", |
91 |
"^[[:blank:]]*ACTION[[:blank:]]+(DELETE|COPY[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)|MOVE[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)|LIST)[[:blank:]]*([[:graph:]]*)[[:blank:]]*\n$", |
"^[[:blank:]]*ACTION[[:blank:]]+(DELETE|COPY[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)|MOVE[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)|LIST)[[:blank:]]*([[:graph:]]*)[[:blank:]]*\n$", |
92 |
"^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(ANSWERED|DELETED|DRAFT|FLAGGED|NEW|OLD|RECENT|SEEN|UNANSWERED|UNDELETED|UNDRAFT|UNFLAGGED|UNSEEN)[[:blank:]]*\n$", |
"^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(ANSWERED|DELETED|DRAFT|FLAGGED|NEW|OLD|RECENT|SEEN|UNANSWERED|UNDELETED|UNDRAFT|UNFLAGGED|UNSEEN)[[:blank:]]*\n$", |
181 |
{ |
{ |
182 |
FILE *fd; |
FILE *fd; |
183 |
char pwfile[PATH_MAX]; |
char pwfile[PATH_MAX]; |
184 |
|
|
185 |
|
passphr = (char *) smalloc(PASSPHRASE_LEN); |
186 |
|
|
187 |
snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords"); |
snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords"); |
188 |
#ifdef DEBUG |
#ifdef DEBUG |
224 |
regmatch_t match[4]; |
regmatch_t match[4]; |
225 |
const char *reg = "([[:alnum:].-]+) ([[:graph:]]+) ([[:graph:]]+)"; |
const char *reg = "([[:alnum:].-]+) ([[:graph:]]+) ([[:graph:]]+)"; |
226 |
|
|
|
buf = (unsigned char *) xmalloc(DECRYPTION_BUF * sizeof(char)); |
|
|
|
|
227 |
do { |
do { |
228 |
printf("Enter master passphrase: "); |
printf("Enter master passphrase: "); |
229 |
get_password(passphr, PASSPHRASE_LEN); |
get_password(passphr, PASSPHRASE_LEN); |
230 |
} while (decrypt_passwords(buf, fd) && --t); |
} while (decrypt_passwords(&buf, fd) && --t); |
231 |
|
|
232 |
if (!t) |
if (!t) |
233 |
return ERROR_PASSPHRASE; |
return ERROR_PASSPHRASE; |
234 |
|
|
235 |
c = cp = xstrdup(buf); |
c = cp = sstrdup(buf); |
236 |
|
|
237 |
regcomp(&creg, reg, REG_EXTENDED | REG_ICASE); |
regcomp(&creg, reg, REG_EXTENDED | REG_ICASE); |
238 |
|
|
250 |
min(match[3].rm_eo - match[3].rm_so, PASSWORD_LEN - 1)); |
min(match[3].rm_eo - match[3].rm_so, PASSWORD_LEN - 1)); |
251 |
} |
} |
252 |
|
|
|
xfree(cp); |
|
253 |
regfree(&creg); |
regfree(&creg); |
254 |
|
sfree(cp); |
255 |
|
sfree(buf); |
256 |
|
|
257 |
return 0; |
return 0; |
258 |
} |
} |
281 |
return 0; |
return 0; |
282 |
} |
} |
283 |
#endif |
#endif |
284 |
|
|
285 |
|
|
286 |
|
/* |
287 |
|
* Create $HOME/.imapfilter directory. |
288 |
|
*/ |
289 |
|
int create_homedir(void) |
290 |
|
{ |
291 |
|
char *hdname = ".imapfilter"; |
292 |
|
|
293 |
|
if (home) |
294 |
|
if (chdir(home)) |
295 |
|
error("imapfilter: could not change directory; %s\n", |
296 |
|
strerror(errno)); |
297 |
|
|
298 |
|
create_dir(hdname, S_IRUSR | S_IWUSR | S_IXUSR); |
299 |
|
|
300 |
|
return 0; |
301 |
|
} |
302 |
|
|
303 |
|
|
304 |
|
/* |
305 |
|
* Check if a file exists. |
306 |
|
*/ |
307 |
|
int exists_file(char *fname) |
308 |
|
{ |
309 |
|
struct stat fs; |
310 |
|
|
311 |
|
if (access(fname, F_OK)) |
312 |
|
return 0; |
313 |
|
|
314 |
|
stat(fname, &fs); |
315 |
|
if (!S_ISREG(fs.st_mode)) { |
316 |
|
error("imapfilter: file %s not a regular file\n", fname); |
317 |
|
return ERROR_FILE_OPEN; |
318 |
|
} |
319 |
|
|
320 |
|
return 1; |
321 |
|
} |
322 |
|
|
323 |
|
|
324 |
|
/* |
325 |
|
* Check if a directory exists. |
326 |
|
*/ |
327 |
|
int exists_dir(char *dname) |
328 |
|
{ |
329 |
|
struct stat ds; |
330 |
|
|
331 |
|
if (access(dname, F_OK)) |
332 |
|
return 0; |
333 |
|
|
334 |
|
stat(dname, &ds); |
335 |
|
if (!S_ISDIR(ds.st_mode)) { |
336 |
|
error("imapfilter: file %s not a directory\n", dname); |
337 |
|
return ERROR_FILE_OPEN; |
338 |
|
} |
339 |
|
|
340 |
|
return 1; |
341 |
|
} |
342 |
|
|
343 |
|
|
344 |
|
/* |
345 |
|
* Create a file. |
346 |
|
*/ |
347 |
|
int create_file(char *fname, mode_t mode) |
348 |
|
{ |
349 |
|
int fd = 0; |
350 |
|
|
351 |
|
if (!exists_file(fname)) |
352 |
|
fd = creat(fname, mode); |
353 |
|
if (fd == -1) { |
354 |
|
error("imapfilter: could not create file %s; %s\n", fname, |
355 |
|
strerror(errno)); |
356 |
|
return ERROR_FILE_OPEN; |
357 |
|
} |
358 |
|
close(fd); |
359 |
|
|
360 |
|
return 0; |
361 |
|
} |
362 |
|
|
363 |
|
|
364 |
|
/* |
365 |
|
* Create a directory. |
366 |
|
*/ |
367 |
|
int create_dir(char *dname, mode_t mode) |
368 |
|
{ |
369 |
|
if (!exists_dir(dname)) |
370 |
|
if (mkdir(dname, mode)) |
371 |
|
error("imapfilter: could not create directory %s; %s\n", dname, |
372 |
|
strerror(errno)); |
373 |
|
|
374 |
|
return 0; |
375 |
|
} |
376 |
|
|
377 |
|
|
378 |
|
#ifdef CHECK_PERMISSIONS |
379 |
|
/* |
380 |
|
* Check the permissions of a file. |
381 |
|
*/ |
382 |
|
int check_file_perms(char *fname, mode_t mode) |
383 |
|
{ |
384 |
|
struct stat fs; |
385 |
|
|
386 |
|
if (stat(fname, &fs)) { |
387 |
|
error("imapfilter: getting file %s status; %s\n", fname, |
388 |
|
strerror(errno)); |
389 |
|
return ERROR_TRIVIAL; |
390 |
|
} |
391 |
|
if (!S_ISREG(fs.st_mode)) { |
392 |
|
error("imapfilter: file %s not a regular file\n", fname); |
393 |
|
return ERROR_TRIVIAL; |
394 |
|
} |
395 |
|
if ((fs.st_mode & 00777) != mode) { |
396 |
|
error("imapfilter: warning: improper file %s permissions\n" |
397 |
|
"imapfilter: warning: file's mode should be %o not %o\n", |
398 |
|
fname, mode, fs.st_mode & 00777); |
399 |
|
return ERROR_TRIVIAL; |
400 |
|
} |
401 |
|
return 0; |
402 |
|
} |
403 |
|
|
404 |
|
|
405 |
|
/* |
406 |
|
* Check the permissions of a directory. |
407 |
|
*/ |
408 |
|
int check_dir_perms(char *dname, mode_t mode) |
409 |
|
{ |
410 |
|
struct stat ds; |
411 |
|
|
412 |
|
if (stat(dname, &ds)) { |
413 |
|
error("imapfilter: getting file %s status; %s\n", dname, |
414 |
|
strerror(errno)); |
415 |
|
return ERROR_TRIVIAL; |
416 |
|
} |
417 |
|
if (!S_ISDIR(ds.st_mode)) { |
418 |
|
error("imapfilter: file %s not a directory\n", dname); |
419 |
|
return ERROR_TRIVIAL; |
420 |
|
} |
421 |
|
if ((ds.st_mode & 00777) != mode) { |
422 |
|
error("imapfilter: warning: improper dir %s permissions\n" |
423 |
|
"imapfilter: warning: file's mode should be %o not %o\n", |
424 |
|
dname, mode, ds.st_mode & 00777); |
425 |
|
return ERROR_TRIVIAL; |
426 |
|
} |
427 |
|
return 0; |
428 |
|
} |
429 |
|
#endif |
430 |
|
|