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

Annotation of /imapfilter/file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.38 - (hide annotations)
Sat Jul 13 14:11:08 2002 UTC (21 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.37: +35 -35 lines
File MIME type: text/plain
Added variable to enable direct expunge of marked deleted mail.

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     #include <limits.h>
9 lefcha 1.10 #include <sys/stat.h>
10 lefcha 1.28 #include <fcntl.h>
11 lefcha 1.29 #include <time.h>
12 lefcha 1.1
13     #include "config.h"
14     #include "imapfilter.h"
15 lefcha 1.13 #include "data.h"
16 lefcha 1.1
17    
18 lefcha 1.6 extern char logfile[PATH_MAX];
19 lefcha 1.11 extern unsigned int options;
20 lefcha 1.34 extern unsigned int flags;
21     extern unsigned int interval;
22 lefcha 1.28 extern long timeout;
23 lefcha 1.29 extern char *home;
24    
25     #ifdef ENCRYPTED_PASSWORDS
26 lefcha 1.38 char *passphr; /* Master password to access the passwords
27     file. */
28 lefcha 1.28 #endif
29 lefcha 1.27
30 lefcha 1.1
31     /*
32 lefcha 1.13 * Find the path to configuration file, open it and call parse_config().
33 lefcha 1.1 */
34     int read_config(char *cfg)
35     {
36     int r;
37 lefcha 1.29 FILE *fd;
38 lefcha 1.23 char *c = NULL;
39 lefcha 1.24
40 lefcha 1.1 if (!cfg) {
41 lefcha 1.38 cfg = c = (char *)xmalloc(PATH_MAX * sizeof(char));
42 lefcha 1.1 snprintf(cfg, PATH_MAX, "%s/%s", home, ".imapfilterrc");
43     }
44     #ifdef DEBUG
45 lefcha 1.11 printf("debug: configuration file: '%s'\n", cfg);
46 lefcha 1.1 #endif
47 lefcha 1.13 #ifdef CHECK_PERMISSIONS
48 lefcha 1.28 check_file_perms(cfg, S_IRUSR | S_IWUSR);
49 lefcha 1.12 #endif
50 lefcha 1.29 fd = fopen(cfg, "r");
51 lefcha 1.1
52 lefcha 1.29 if (!fd)
53 lefcha 1.17 fatal(ERROR_FILE_OPEN, "imapfilter: opening config file %s; %s\n",
54 lefcha 1.11 cfg, strerror(errno));
55 lefcha 1.1
56 lefcha 1.23 if (c)
57 lefcha 1.29 xfree(c);
58 lefcha 1.23
59 lefcha 1.29 if ((r = parse_config(fd)))
60 lefcha 1.17 fatal(ERROR_CONFIG_PARSE,
61     "imapfilter: parse error in config file at row %d\n", r);
62 lefcha 1.1
63 lefcha 1.29 fclose(fd);
64 lefcha 1.1
65 lefcha 1.11 #ifdef DEBUG
66     printf("debug: options: %0#10x\n", options);
67     #endif
68    
69 lefcha 1.17 return 0;
70 lefcha 1.1 }
71    
72 lefcha 1.6
73 lefcha 1.10 /*
74 lefcha 1.13 * Parse configuration file.
75 lefcha 1.1 */
76 lefcha 1.29 int parse_config(FILE * fd)
77 lefcha 1.1 {
78 lefcha 1.17 int i, r = 0;
79 lefcha 1.1 unsigned int row = 0;
80     char line[LINE_MAX];
81 lefcha 1.27 regex_t creg[13];
82 lefcha 1.36 regmatch_t match[9];
83 lefcha 1.27 const char *reg[13] = {
84 lefcha 1.15 "^([[:blank:]]*\n|#.*\n)$",
85 lefcha 1.36
86     "^[[:blank:]]*ACCOUNT[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+"
87     "(([[:graph:]]+):([[:graph:]]*)|([[:graph:]]+))@([[:alnum:].-]+)"
88     "(:[[:digit:]]+)?[[:blank:]]*([[:blank:]]SSL|[[:blank:]]SSL2|"
89     "[[:blank:]]SSL3|[[:blank:]]TLS1)?[[:blank:]]*\n$",
90    
91     "^[[:blank:]]*FOLDER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+"
92     "([[:print:]]+)[[:blank:]]*\n$",
93    
94     "^[[:blank:]]*FILTER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]*"
95     "([[:blank:]]OR|[[:blank:]]AND)?[[:blank:]]*\n$",
96    
97     "^[[:blank:]]*ACTION[[:blank:]]+(DELETE|"
98     "COPY[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)|"
99     "MOVE[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)|"
100     "RCOPY[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+"
101     "(\"[[:print:]]*\"|[[:graph:]]+)|"
102     "RMOVE[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+"
103     "(\"[[:print:]]*\"|[[:graph:]]+)|"
104     "LIST)[[:blank:]]*([[:graph:]]*)[[:blank:]]*\n$",
105    
106     "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|"
107     "AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*"
108     "(ANSWERED|DELETED|DRAFT|FLAGGED|NEW|OLD|RECENT|SEEN|UNANSWERED|"
109     "UNDELETED|UNDRAFT|UNFLAGGED|UNSEEN)[[:blank:]]*\n$",
110    
111     "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|"
112     "AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*"
113     "(BCC|BODY|CC|FROM|SUBJECT|TEXT|TO)[[:blank:]]+(\"[[:print:]]*\"|"
114     "[[:graph:]]+)[[:blank:]]*\n$",
115    
116     "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|"
117     "AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*"
118     "(HEADER)[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)[[:blank:]]+"
119     "(\"[[:print:]]*\"|[[:graph:]]+)[[:blank:]]*\n$",
120    
121     "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|"
122     "AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*"
123     "(LARGER|SMALLER|OLDER|NEWER)[[:blank:]]+([[:digit:]]+)"
124     "[[:blank:]]*\n$",
125    
126     "^[[:blank:]]*JOB[[:blank:]]+([[:alnum:],_-]+)[[:blank:]]+"
127     "([[:alnum:],_-]+)[[:blank:]]*\n$",
128    
129     "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(LOGFILE)"
130     "[[:blank:]]*=[[:blank:]]*([[:print:]]+)\n$",
131    
132 lefcha 1.38 "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(HEADERS|NAMESPACE|EXPUNGE)"
133 lefcha 1.36 "[[:blank:]]*=[[:blank:]]*(YES|NO)[[:blank:]]*\n$",
134    
135     "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(DAEMON|TIMEOUT)"
136     "[[:blank:]]*=[[:blank:]]*([[:digit:]]+)\n$"
137 lefcha 1.1 };
138    
139 lefcha 1.27 for (i = 0; i < 13; i++)
140 lefcha 1.14 regcomp(&creg[i], reg[i], REG_EXTENDED | REG_ICASE);
141 lefcha 1.1
142 lefcha 1.29 while (fgets(line, LINE_MAX - 1, fd)) {
143 lefcha 1.1 row++;
144 lefcha 1.15 if (!regexec(&creg[0], line, 0, match, 0))
145     continue;
146 lefcha 1.36 else if (!regexec(&creg[1], line, 9, match, 0))
147 lefcha 1.13 set_account(line, match);
148 lefcha 1.15 else if (!regexec(&creg[2], line, 3, match, 0))
149 lefcha 1.17 r = set_mboxgrp(line, match);
150 lefcha 1.15 else if (!regexec(&creg[3], line, 3, match, 0))
151 lefcha 1.17 r = set_filter(line, match);
152 lefcha 1.36 else if (!regexec(&creg[4], line, 9, match, 0))
153 lefcha 1.17 r = set_action(line, match);
154 lefcha 1.15 else if (!regexec(&creg[5], line, 7, match, 0) ||
155 lefcha 1.18 !regexec(&creg[6], line, 7, match, 0) ||
156     !regexec(&creg[7], line, 7, match, 0) ||
157     !regexec(&creg[8], line, 7, match, 0))
158 lefcha 1.17 r = set_mask(line, match);
159 lefcha 1.15 else if (!regexec(&creg[9], line, 3, match, 0))
160 lefcha 1.17 r = set_job(line, match);
161 lefcha 1.26 else if (!regexec(&creg[10], line, 4, match, 0) ||
162 lefcha 1.27 !regexec(&creg[11], line, 4, match, 0) ||
163     !regexec(&creg[12], line, 4, match, 0))
164 lefcha 1.15 set_options(line, match);
165 lefcha 1.17 else
166 lefcha 1.18 return row;
167    
168 lefcha 1.17 if (r == ERROR_CONFIG_PARSE)
169     return row;
170 lefcha 1.1 }
171 lefcha 1.3
172 lefcha 1.27 for (i = 0; i < 13; i++)
173 lefcha 1.12 regfree(&creg[i]);
174 lefcha 1.18
175 lefcha 1.15 destroy_data();
176 lefcha 1.6
177 lefcha 1.8 return 0;
178 lefcha 1.1 }
179    
180    
181     /*
182 lefcha 1.13 * Set other options found in config file.
183 lefcha 1.11 */
184     void set_options(char *line, regmatch_t * match)
185     {
186 lefcha 1.26 if (!strncasecmp(line + match[2].rm_so, "logfile", 7)) {
187 lefcha 1.19 if (!*logfile)
188 lefcha 1.26 strncat(logfile, line + match[3].rm_so,
189     min((match[3].rm_eo - match[3].rm_so), PATH_MAX - 1));
190 lefcha 1.27 } else if (!strncasecmp(line + match[2].rm_so, "header", 6)) {
191 lefcha 1.26 if (!strncasecmp(line + match[3].rm_so, "yes", 3))
192 lefcha 1.19 options |= OPTION_HEADERS;
193 lefcha 1.25 else
194     options &= ~(OPTION_HEADERS);
195 lefcha 1.28 } else if (!strncasecmp(line + match[2].rm_so, "namespace", 9)) {
196     if (!strncasecmp(line + match[3].rm_so, "yes", 3))
197     options |= OPTION_NAMESPACE;
198     else
199     options &= ~(OPTION_NAMESPACE);
200 lefcha 1.38 } else if (!strncasecmp(line + match[2].rm_so, "expunge", 7)) {
201     if (!strncasecmp(line + match[3].rm_so, "yes", 3))
202     options |= OPTION_EXPUNGE;
203     else
204     options &= ~(OPTION_EXPUNGE);
205 lefcha 1.27 } else if (!strncasecmp(line + match[2].rm_so, "timeout", 7)) {
206     errno = 0;
207     timeout = strtol(line + match[3].rm_so, NULL, 10);
208     if (errno)
209     timeout = 0;
210 lefcha 1.38 } else if (!strncasecmp(line + match[2].rm_so, "daemon", 6) &&
211 lefcha 1.34 !(options & OPTION_DAEMON_MODE)) {
212     options |= OPTION_DAEMON_MODE;
213     errno = 0;
214     interval = strtoul(line + match[3].rm_so, NULL, 10);
215     if (errno)
216     interval = 0;
217 lefcha 1.19 }
218 lefcha 1.1 }
219 lefcha 1.29
220    
221     #ifdef ENCRYPTED_PASSWORDS
222     /*
223     * Open password file and call parse_passwords().
224     */
225     int read_passwords(void)
226     {
227     FILE *fd;
228     char pwfile[PATH_MAX];
229 lefcha 1.38
230 lefcha 1.34 if (!(flags & FLAG_BLANK_PASSWORD))
231 lefcha 1.36 return ERROR_CONFIG_PARSE;
232 lefcha 1.38
233     passphr = (char *)smalloc(PASSPHRASE_LEN);
234 lefcha 1.37 *passphr = 0;
235 lefcha 1.29
236     snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords");
237     #ifdef DEBUG
238     printf("debug: passwords file: '%s'\n", pwfile);
239     #endif
240 lefcha 1.38
241 lefcha 1.29 if (!exists_file(pwfile))
242 lefcha 1.36 return ERROR_FILE_OPEN;
243 lefcha 1.38
244 lefcha 1.29 #ifdef CHECK_PERMISSIONS
245     check_file_perms(pwfile, S_IRUSR | S_IWUSR);
246     #endif
247    
248     fd = fopen(pwfile, "r");
249    
250     if (!fd)
251     fatal(ERROR_FILE_OPEN, "imapfilter: opening passwords file %s; %s\n",
252     pwfile, strerror(errno));
253    
254     parse_passwords(fd);
255    
256     fclose(fd);
257    
258     return 0;
259     }
260    
261    
262     /*
263     * Parse unencrypted password file.
264     */
265     int parse_passwords(FILE * fd)
266     {
267     int t = 3;
268     char *pe = NULL;
269     char user[USERNAME_LEN], serv[SERVER_LEN];
270     unsigned char *buf;
271     char *c, *cp, *line;
272     regex_t creg;
273     regmatch_t match[4];
274     const char *reg = "([[:alnum:].-]+) ([[:graph:]]+) ([[:graph:]]+)";
275 lefcha 1.36 int r;
276 lefcha 1.29
277     do {
278 lefcha 1.36 fseek(fd, 0L, SEEK_SET);
279 lefcha 1.29 printf("Enter master passphrase: ");
280     get_password(passphr, PASSPHRASE_LEN);
281 lefcha 1.36 } while ((r = decrypt_passwords(&buf, fd)) && --t);
282 lefcha 1.38
283 lefcha 1.29 if (!t)
284     return ERROR_PASSPHRASE;
285    
286 lefcha 1.31 c = cp = sstrdup(buf);
287 lefcha 1.38
288 lefcha 1.29 regcomp(&creg, reg, REG_EXTENDED | REG_ICASE);
289    
290     while ((line = strsep(&c, "\n")) &&
291     !regexec(&creg, line, 4, match, 0)) {
292     user[0] = serv[0] = 0;
293 lefcha 1.38
294 lefcha 1.29 strncat(serv, line + match[1].rm_so,
295     min(match[1].rm_eo - match[1].rm_so, SERVER_LEN - 1));
296     strncat(user, line + match[2].rm_so,
297     min(match[2].rm_eo - match[2].rm_so, USERNAME_LEN - 1));
298 lefcha 1.38
299     if ((pe = (char *)find_password(user, serv)))
300 lefcha 1.29 strncat(pe, line + match[3].rm_so,
301     min(match[3].rm_eo - match[3].rm_so, PASSWORD_LEN - 1));
302     }
303 lefcha 1.38
304 lefcha 1.29 regfree(&creg);
305 lefcha 1.38 sfree(cp);
306 lefcha 1.31 sfree(buf);
307 lefcha 1.29
308     return 0;
309     }
310    
311    
312     /*
313     * Store encrypted passwords to file.
314     */
315     int store_passwords(account_t * accts[])
316     {
317     char pwfile[PATH_MAX];
318     FILE *fd;
319    
320     snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords");
321 lefcha 1.38
322 lefcha 1.32 create_file(pwfile, S_IRUSR | S_IWUSR);
323 lefcha 1.29
324     fd = fopen(pwfile, "w");
325    
326     if (!fd)
327     fatal(ERROR_FILE_OPEN, "imapfilter: opening passwords file %s; %s\n",
328     pwfile, strerror(errno));
329 lefcha 1.38
330 lefcha 1.29 encrypt_passwords(fd, accts);
331 lefcha 1.38
332 lefcha 1.29 fclose(fd);
333    
334     return 0;
335     }
336 lefcha 1.38
337 lefcha 1.29 #endif
338 lefcha 1.31
339    
340     /*
341     * Create $HOME/.imapfilter directory.
342     */
343     int create_homedir(void)
344     {
345 lefcha 1.33 char *hdn = ".imapfilter";
346 lefcha 1.38
347 lefcha 1.31 if (home)
348     if (chdir(home))
349     error("imapfilter: could not change directory; %s\n",
350     strerror(errno));
351    
352 lefcha 1.36 if (!exists_dir(hdn))
353     if (mkdir(hdn, S_IRUSR | S_IWUSR | S_IXUSR))
354     error("imapfilter: could not create directory %s; %s\n", hdn,
355     strerror(errno));
356    
357 lefcha 1.31 return 0;
358     }
359    
360    
361     /*
362     * Check if a file exists.
363     */
364     int exists_file(char *fname)
365     {
366     struct stat fs;
367 lefcha 1.38
368 lefcha 1.31 if (access(fname, F_OK))
369     return 0;
370 lefcha 1.38
371 lefcha 1.31 stat(fname, &fs);
372     if (!S_ISREG(fs.st_mode)) {
373     error("imapfilter: file %s not a regular file\n", fname);
374     return ERROR_FILE_OPEN;
375     }
376     return 1;
377     }
378    
379    
380 lefcha 1.38 /*
381 lefcha 1.31 * Check if a directory exists.
382     */
383     int exists_dir(char *dname)
384     {
385     struct stat ds;
386 lefcha 1.38
387 lefcha 1.31 if (access(dname, F_OK))
388     return 0;
389 lefcha 1.38
390 lefcha 1.31 stat(dname, &ds);
391     if (!S_ISDIR(ds.st_mode)) {
392     error("imapfilter: file %s not a directory\n", dname);
393     return ERROR_FILE_OPEN;
394     }
395     return 1;
396     }
397    
398    
399     /*
400 lefcha 1.36 * Create a file with the specified permissions.
401 lefcha 1.31 */
402     int create_file(char *fname, mode_t mode)
403     {
404     int fd = 0;
405 lefcha 1.38
406 lefcha 1.33 if (!exists_file(fname)) {
407     fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, mode);
408 lefcha 1.31 if (fd == -1) {
409     error("imapfilter: could not create file %s; %s\n", fname,
410     strerror(errno));
411     return ERROR_FILE_OPEN;
412     }
413     close(fd);
414 lefcha 1.33 }
415 lefcha 1.31 return 0;
416     }
417    
418    
419     #ifdef CHECK_PERMISSIONS
420     /*
421     * Check the permissions of a file.
422     */
423     int check_file_perms(char *fname, mode_t mode)
424     {
425     struct stat fs;
426 lefcha 1.38
427 lefcha 1.31 if (stat(fname, &fs)) {
428     error("imapfilter: getting file %s status; %s\n", fname,
429     strerror(errno));
430     return ERROR_TRIVIAL;
431     }
432     if (!S_ISREG(fs.st_mode)) {
433     error("imapfilter: file %s not a regular file\n", fname);
434     return ERROR_TRIVIAL;
435     }
436     if ((fs.st_mode & 00777) != mode) {
437     error("imapfilter: warning: improper file %s permissions\n"
438     "imapfilter: warning: file's mode should be %o not %o\n",
439     fname, mode, fs.st_mode & 00777);
440     return ERROR_TRIVIAL;
441     }
442     return 0;
443     }
444    
445    
446     /*
447     * Check the permissions of a directory.
448     */
449     int check_dir_perms(char *dname, mode_t mode)
450     {
451     struct stat ds;
452    
453     if (stat(dname, &ds)) {
454     error("imapfilter: getting file %s status; %s\n", dname,
455     strerror(errno));
456     return ERROR_TRIVIAL;
457     }
458     if (!S_ISDIR(ds.st_mode)) {
459     error("imapfilter: file %s not a directory\n", dname);
460     return ERROR_TRIVIAL;
461     }
462     if ((ds.st_mode & 00777) != mode) {
463     error("imapfilter: warning: improper dir %s permissions\n"
464     "imapfilter: warning: file's mode should be %o not %o\n",
465     dname, mode, ds.st_mode & 00777);
466     return ERROR_TRIVIAL;
467     }
468     return 0;
469     }
470 lefcha 1.38
471 lefcha 1.31 #endif

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26