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

Contents of /imapfilter/file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.39 - (show annotations)
Sat Jul 13 21:19:52 2002 UTC (21 years, 9 months ago) by lefcha
Branch: MAIN
Changes since 1.38: +4 -2 lines
File MIME type: text/plain
Added action flag.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26