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

Annotation of /imapfilter/file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.35 - (hide annotations)
Fri Feb 8 22:15:43 2002 UTC (22 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.34: +0 -8 lines
File MIME type: text/plain
Allow ssl appear in config file, but fail with error message.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26