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

Annotation of /imapfilter/file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.40 - (hide annotations)
Sat Jul 13 22:33:01 2002 UTC (21 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.39: +18 -13 lines
File MIME type: text/plain
Added variable that controls subsribing after mailbox creation.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26