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

Contents of /imapfilter/file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.42 - (show annotations)
Thu Jul 25 21:24:53 2002 UTC (21 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.41: +8 -5 lines
File MIME type: text/plain
Run correctly when compiled with libc5.

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:]]*(EXPUNGE|HEADERS|NAMESPACE|"
135 "SUBSCRIBE)[[: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, 5, match, 0))
157 r = set_mask(line, match, MASK_MATCH_1);
158 else if (!regexec(&creg[6], line, 6, match, 0))
159 r = set_mask(line, match, MASK_MATCH_2);
160 else if (!regexec(&creg[7], line, 7, match, 0))
161 r = set_mask(line, match, MASK_MATCH_3);
162 else if (!regexec(&creg[8], line, 6, match, 0))
163 r = set_mask(line, match, MASK_MATCH_4);
164 else if (!regexec(&creg[9], line, 3, match, 0))
165 r = set_job(line, match);
166 else if (!regexec(&creg[10], line, 4, match, 0) ||
167 !regexec(&creg[11], line, 4, match, 0) ||
168 !regexec(&creg[12], line, 4, match, 0))
169 set_options(line, match);
170 else
171 return row;
172
173 if (r == ERROR_CONFIG_PARSE)
174 return row;
175 }
176
177 for (i = 0; i < 13; i++)
178 regfree(&creg[i]);
179
180 destroy_data();
181
182 return 0;
183 }
184
185
186 /*
187 * Set other options found in config file.
188 */
189 void set_options(char *line, regmatch_t * match)
190 {
191 if (!strncasecmp(line + match[2].rm_so, "logfile", 7)) {
192 if (!*logfile)
193 strncat(logfile, line + match[3].rm_so,
194 min((match[3].rm_eo - match[3].rm_so), PATH_MAX - 1));
195 } else if (!strncasecmp(line + match[2].rm_so, "expunge", 7)) {
196 if (!strncasecmp(line + match[3].rm_so, "yes", 3))
197 options |= OPTION_EXPUNGE;
198 else
199 options &= ~(OPTION_EXPUNGE);
200 } else if (!strncasecmp(line + match[2].rm_so, "header", 6)) {
201 if (!strncasecmp(line + match[3].rm_so, "yes", 3))
202 options |= OPTION_HEADERS;
203 else
204 options &= ~(OPTION_HEADERS);
205 } else if (!strncasecmp(line + match[2].rm_so, "namespace", 9)) {
206 if (!strncasecmp(line + match[3].rm_so, "yes", 3))
207 options |= OPTION_NAMESPACE;
208 else
209 options &= ~(OPTION_NAMESPACE);
210 } else if (!strncasecmp(line + match[2].rm_so, "subscribe", 9)) {
211 if (!strncasecmp(line + match[2].rm_so, "yes", 3))
212 options |= OPTION_SUBSCRIBE;
213 else
214 options &= ~(OPTION_SUBSCRIBE);
215 } else if (!strncasecmp(line + match[2].rm_so, "timeout", 7)) {
216 errno = 0;
217 timeout = strtol(line + match[3].rm_so, NULL, 10);
218 if (errno)
219 timeout = 0;
220 } else if (!strncasecmp(line + match[2].rm_so, "daemon", 6) &&
221 !(options & OPTION_DAEMON_MODE)) {
222 options |= OPTION_DAEMON_MODE;
223 errno = 0;
224 interval = strtoul(line + match[3].rm_so, NULL, 10);
225 if (errno)
226 interval = 0;
227 }
228 }
229
230
231 #ifdef ENCRYPTED_PASSWORDS
232 /*
233 * Open password file and call parse_passwords().
234 */
235 int read_passwords(void)
236 {
237 FILE *fd;
238 char pwfile[PATH_MAX];
239
240 if (!(flags & FLAG_BLANK_PASSWORD))
241 return ERROR_CONFIG_PARSE;
242
243 passphr = (char *)smalloc(PASSPHRASE_LEN);
244 *passphr = 0;
245
246 snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords");
247 #ifdef DEBUG
248 printf("debug: passwords file: '%s'\n", pwfile);
249 #endif
250
251 if (!exists_file(pwfile))
252 return ERROR_FILE_OPEN;
253
254 #ifdef CHECK_PERMISSIONS
255 check_file_perms(pwfile, S_IRUSR | S_IWUSR);
256 #endif
257
258 fd = fopen(pwfile, "r");
259
260 if (!fd)
261 fatal(ERROR_FILE_OPEN, "imapfilter: opening passwords file %s; %s\n",
262 pwfile, strerror(errno));
263
264 parse_passwords(fd);
265
266 fclose(fd);
267
268 return 0;
269 }
270
271
272 /*
273 * Parse unencrypted password file.
274 */
275 int parse_passwords(FILE * fd)
276 {
277 int t = 3;
278 char *pe = NULL;
279 char user[USERNAME_LEN], serv[SERVER_LEN];
280 unsigned char *buf;
281 char *c, *cp, *line;
282 regex_t creg;
283 regmatch_t match[4];
284 const char *reg = "([[:alnum:].-]+) ([[:graph:]]+) ([[:graph:]]+)";
285 int r;
286
287 do {
288 fseek(fd, 0L, SEEK_SET);
289 printf("Enter master passphrase: ");
290 get_password(passphr, PASSPHRASE_LEN);
291 } while ((r = decrypt_passwords(&buf, fd)) && --t);
292
293 if (!t)
294 return ERROR_PASSPHRASE;
295
296 c = cp = sstrdup(buf);
297
298 regcomp(&creg, reg, REG_EXTENDED | REG_ICASE);
299
300 while ((line = strsep(&c, "\n")) &&
301 !regexec(&creg, line, 4, match, 0)) {
302 user[0] = serv[0] = 0;
303
304 strncat(serv, line + match[1].rm_so,
305 min(match[1].rm_eo - match[1].rm_so, SERVER_LEN - 1));
306 strncat(user, line + match[2].rm_so,
307 min(match[2].rm_eo - match[2].rm_so, USERNAME_LEN - 1));
308
309 if ((pe = (char *)find_password(user, serv)))
310 strncat(pe, line + match[3].rm_so,
311 min(match[3].rm_eo - match[3].rm_so, PASSWORD_LEN - 1));
312 }
313
314 regfree(&creg);
315 sfree(cp);
316 sfree(buf);
317
318 return 0;
319 }
320
321
322 /*
323 * Store encrypted passwords to file.
324 */
325 int store_passwords(account_t * accts[])
326 {
327 char pwfile[PATH_MAX];
328 FILE *fd;
329
330 snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords");
331
332 create_file(pwfile, S_IRUSR | S_IWUSR);
333
334 fd = fopen(pwfile, "w");
335
336 if (!fd)
337 fatal(ERROR_FILE_OPEN, "imapfilter: opening passwords file %s; %s\n",
338 pwfile, strerror(errno));
339
340 encrypt_passwords(fd, accts);
341
342 fclose(fd);
343
344 return 0;
345 }
346
347 #endif
348
349
350 /*
351 * Create $HOME/.imapfilter directory.
352 */
353 int create_homedir(void)
354 {
355 char *hdn = ".imapfilter";
356
357 if (home)
358 if (chdir(home))
359 error("imapfilter: could not change directory; %s\n",
360 strerror(errno));
361
362 if (!exists_dir(hdn))
363 if (mkdir(hdn, S_IRUSR | S_IWUSR | S_IXUSR))
364 error("imapfilter: could not create directory %s; %s\n", hdn,
365 strerror(errno));
366
367 return 0;
368 }
369
370
371 /*
372 * Check if a file exists.
373 */
374 int exists_file(char *fname)
375 {
376 struct stat fs;
377
378 if (access(fname, F_OK))
379 return 0;
380
381 stat(fname, &fs);
382 if (!S_ISREG(fs.st_mode)) {
383 error("imapfilter: file %s not a regular file\n", fname);
384 return ERROR_FILE_OPEN;
385 }
386 return 1;
387 }
388
389
390 /*
391 * Check if a directory exists.
392 */
393 int exists_dir(char *dname)
394 {
395 struct stat ds;
396
397 if (access(dname, F_OK))
398 return 0;
399
400 stat(dname, &ds);
401 if (!S_ISDIR(ds.st_mode)) {
402 error("imapfilter: file %s not a directory\n", dname);
403 return ERROR_FILE_OPEN;
404 }
405 return 1;
406 }
407
408
409 /*
410 * Create a file with the specified permissions.
411 */
412 int create_file(char *fname, mode_t mode)
413 {
414 int fd = 0;
415
416 if (!exists_file(fname)) {
417 fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, mode);
418 if (fd == -1) {
419 error("imapfilter: could not create file %s; %s\n", fname,
420 strerror(errno));
421 return ERROR_FILE_OPEN;
422 }
423 close(fd);
424 }
425 return 0;
426 }
427
428
429 #ifdef CHECK_PERMISSIONS
430 /*
431 * Check the permissions of a file.
432 */
433 int check_file_perms(char *fname, mode_t mode)
434 {
435 struct stat fs;
436
437 if (stat(fname, &fs)) {
438 error("imapfilter: getting file %s status; %s\n", fname,
439 strerror(errno));
440 return ERROR_TRIVIAL;
441 }
442 if (!S_ISREG(fs.st_mode)) {
443 error("imapfilter: file %s not a regular file\n", fname);
444 return ERROR_TRIVIAL;
445 }
446 if ((fs.st_mode & 00777) != mode) {
447 error("imapfilter: warning: improper file %s permissions\n"
448 "imapfilter: warning: file's mode should be %o not %o\n",
449 fname, mode, fs.st_mode & 00777);
450 return ERROR_TRIVIAL;
451 }
452 return 0;
453 }
454
455
456 /*
457 * Check the permissions of a directory.
458 */
459 int check_dir_perms(char *dname, mode_t mode)
460 {
461 struct stat ds;
462
463 if (stat(dname, &ds)) {
464 error("imapfilter: getting file %s status; %s\n", dname,
465 strerror(errno));
466 return ERROR_TRIVIAL;
467 }
468 if (!S_ISDIR(ds.st_mode)) {
469 error("imapfilter: file %s not a directory\n", dname);
470 return ERROR_TRIVIAL;
471 }
472 if ((ds.st_mode & 00777) != mode) {
473 error("imapfilter: warning: improper dir %s permissions\n"
474 "imapfilter: warning: file's mode should be %o not %o\n",
475 dname, mode, ds.st_mode & 00777);
476 return ERROR_TRIVIAL;
477 }
478 return 0;
479 }
480
481 #endif

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26