14 |
|
|
15 |
|
|
16 |
extern char logfile[PATH_MAX]; |
extern char logfile[PATH_MAX]; |
17 |
|
extern unsigned int options; |
18 |
|
|
19 |
account_t *accounts = NULL; /* All accounts. */ |
account_t *accounts = NULL; /* All accounts. */ |
20 |
|
|
21 |
filter_t *dfilters; /* Filters of DENY type. */ |
filter_t *dfilters = NULL; /* Filters of DENY type. */ |
22 |
filter_t *afilters; /* Filters of ALLOW type. */ |
filter_t *afilters = NULL; /* Filters of ALLOW type. */ |
23 |
|
|
24 |
unsigned int dlimit = 0; |
unsigned int dlimit = 0; |
25 |
unsigned int alimit = 0; /* DENY and ALLOW message size limits. */ |
unsigned int alimit = 0; /* DENY and ALLOW message size limits. */ |
43 |
snprintf(cfg, PATH_MAX, "%s/%s", home, ".imapfilterrc"); |
snprintf(cfg, PATH_MAX, "%s/%s", home, ".imapfilterrc"); |
44 |
} |
} |
45 |
#ifdef DEBUG |
#ifdef DEBUG |
46 |
printf("debug: configuration file: %s\n", cfg); |
printf("debug: configuration file: '%s'\n", cfg); |
47 |
#endif |
#endif |
48 |
|
|
49 |
check_permissions(cfg); |
check_permissions(cfg); |
51 |
fp = fopen(cfg, "r"); |
fp = fopen(cfg, "r"); |
52 |
|
|
53 |
if (!fp) { |
if (!fp) { |
54 |
fprintf(stderr, "imapfilter: opening config file %s; %s\n", |
error("imapfilter: opening config file %s; %s\n", |
55 |
cfg, strerror(errno)); |
cfg, strerror(errno)); |
56 |
return 1; |
return 1; |
57 |
} |
} |
58 |
|
|
60 |
|
|
61 |
fclose(fp); |
fclose(fp); |
62 |
|
|
63 |
|
#ifdef DEBUG |
64 |
|
printf("debug: options: %0#10x\n", options); |
65 |
|
#endif |
66 |
|
|
67 |
return r; |
return r; |
68 |
} |
} |
69 |
|
|
76 |
struct stat fs; |
struct stat fs; |
77 |
|
|
78 |
if (stat(cfg, &fs)) { |
if (stat(cfg, &fs)) { |
79 |
fprintf(stderr, "imapfilter: getting file %s status; %s\n", cfg, |
error("imapfilter: getting file %s status; %s\n", cfg, |
80 |
strerror(errno)); |
strerror(errno)); |
81 |
return 1; |
return 1; |
82 |
} |
} |
83 |
|
|
84 |
if (!S_ISREG(fs.st_mode)) { |
if (!S_ISREG(fs.st_mode)) { |
85 |
fprintf(stderr, "imapfilter: file %s not a regular file\n", cfg); |
error("imapfilter: file %s not a regular file\n", cfg); |
86 |
return 1; |
return 1; |
87 |
} |
} |
88 |
|
|
106 |
int i; |
int i; |
107 |
unsigned int row = 0; |
unsigned int row = 0; |
108 |
char line[LINE_MAX]; |
char line[LINE_MAX]; |
109 |
regex_t creg[5]; |
regex_t creg[7]; |
110 |
regmatch_t match[4]; |
regmatch_t match[4]; |
111 |
const char *reg[5] = { |
const char *reg[7] = { |
112 |
"^[[:blank:]]*(SERVER|PORT|USERNAME|PASSWORD|LOGFILE)[[:blank:]]*=[[:blank:]]*([[:graph:]]*)[[:blank:]]*", |
"^[[:blank:]]*(SERVER|PORT|USERNAME|PASSWORD)[[:blank:]]*=[[:blank:]]*([[:graph:]]*)[[:blank:]]*\n$", |
113 |
"^[[:blank:]]*(DENY|ALLOW)[[:blank:]]*=[[:blank:]]*(FROM|CC|BCC|SUBJECT|TO):? ([[:print:]]*)", |
"^[[:blank:]]*(DENY|ALLOW)[[:blank:]]*=[[:blank:]]*(FROM|CC|BCC|SUBJECT|TO):? ([[:print:]]*)\n$", |
114 |
"^[[:blank:]]*(DENY|ALLOW)[[:blank:]]*=[[:blank:]]*([[:graph:]]+):? ([[:print:]]*)", |
"^[[:blank:]]*(DENY|ALLOW)[[:blank:]]*=[[:blank:]]*([[:graph:]]+):? ([[:print:]]*)\n$", |
115 |
"^[[:blank:]]*(DENY_LIMIT|ALLOW_LIMIT)[[:blank:]]*=[[:blank:]]*([[:digit:]]*)[[:blank:]]*", |
"^[[:blank:]]*(DENY_LIMIT|ALLOW_LIMIT)[[:blank:]]*=[[:blank:]]*([[:digit:]]*)[[:blank:]]*\n$", |
116 |
"^[[:blank:]]*#{0,1}.*" |
"^[[:blank:]]*(LOGFILE)[[:blank:]]*=[[:blank:]]*([[:graph:]]*)[[:blank:]]*\n$", |
117 |
|
"^[[:blank:]]*(TEST_MODE|SHOW_HEADERS|UNSEEN_ONLY)[[:blank:]]*=[[:blank:]]*(yes|no)[[:blank:]]*\n$", |
118 |
|
"^([[:blank:]]*\n|#.*\n)$" |
119 |
}; |
}; |
120 |
|
|
121 |
for (i = 0; i < 5; i++) |
for (i = 0; i < 7; i++) |
122 |
regcomp(&creg[i], reg[i], REG_EXTENDED); |
regcomp(&creg[i], reg[i], REG_EXTENDED); |
123 |
|
|
124 |
while (fgets(line, LINE_MAX - 1, fp)) { |
while (fgets(line, LINE_MAX - 1, fp)) { |
138 |
} else if (!regexec(&creg[3], line, 3, match, 0)) { |
} else if (!regexec(&creg[3], line, 3, match, 0)) { |
139 |
set_limits(line, match); |
set_limits(line, match); |
140 |
continue; |
continue; |
141 |
} else if (!regexec(&creg[4], line, 1, match, 0)) |
} else if (!regexec(&creg[4], line, 3, match, 0) || |
142 |
|
!regexec(&creg[5], line, 3, match, 0)) { |
143 |
|
set_options(line, match); |
144 |
continue; |
continue; |
145 |
else { |
} else if (!regexec(&creg[6], line, 0, match, 0)) { |
146 |
fprintf(stderr, |
continue; |
147 |
"imapfilter: parse error in config file at row %d\n", |
} else { |
148 |
row); |
error("imapfilter: parse error in config file at row %d\n", |
149 |
|
row); |
150 |
return 1; |
return 1; |
151 |
} |
} |
152 |
} |
} |
160 |
|
|
161 |
/* Fail if no filters were defined. */ |
/* Fail if no filters were defined. */ |
162 |
if (!dfilters && !afilters) { |
if (!dfilters && !afilters) { |
163 |
fprintf(stderr, "imapfilter: no filters defined in config file\n"); |
error("imapfilter: no filters defined in config file\n"); |
164 |
return 1; |
return 1; |
165 |
} |
} |
166 |
|
|
229 |
p[s] = 0; |
p[s] = 0; |
230 |
ca->port = strtoul(p, NULL, 0); |
ca->port = strtoul(p, NULL, 0); |
231 |
#ifdef DEBUG |
#ifdef DEBUG |
232 |
printf("debug: account setting PORT: '%d'\n", ca->port); |
printf("debug: account setting PORT: %d\n", ca->port); |
233 |
#endif |
#endif |
234 |
} else if (!strncmp(line + match[1].rm_so, "USERNAME", 8)) { |
} else if (!strncmp(line + match[1].rm_so, "USERNAME", 8)) { |
235 |
s = min((match[2].rm_eo - match[2].rm_so), USERID_MAX - 1); |
s = min((match[2].rm_eo - match[2].rm_so), USERID_MAX - 1); |
245 |
#ifdef DEBUG |
#ifdef DEBUG |
246 |
printf("debug: account setting PASSWORD: '%s'\n", ca->passwd); |
printf("debug: account setting PASSWORD: '%s'\n", ca->passwd); |
247 |
#endif |
#endif |
|
} else if (!logfile[0] |
|
|
&& !strncmp(line + match[1].rm_so, "LOGFILE", 7)) { |
|
|
s = min((match[2].rm_eo - match[2].rm_so), PATH_MAX - 1); |
|
|
strncpy(logfile, line + match[2].rm_so, s); |
|
|
logfile[s] = 0; |
|
248 |
} |
} |
249 |
} |
} |
250 |
|
|
264 |
|
|
265 |
if (!(strncmp(line + match[1].rm_so, "DENY", 4))) { |
if (!(strncmp(line + match[1].rm_so, "DENY", 4))) { |
266 |
#ifdef DEBUG |
#ifdef DEBUG |
267 |
printf("debug: filter entry: DENY"); |
printf("debug: filter entry DENY:"); |
268 |
#endif |
#endif |
269 |
cf = &cdf; |
cf = &cdf; |
270 |
fl = &dfilters; |
fl = &dfilters; |
271 |
} else { |
} else { |
272 |
#ifdef DEBUG |
#ifdef DEBUG |
273 |
printf("debug: filter entry: ALLOW"); |
printf("debug: filter entry ALLOW:"); |
274 |
#endif |
#endif |
275 |
cf = &caf; |
cf = &caf; |
276 |
fl = &afilters; |
fl = &afilters; |
309 |
(*cf)->body[s] = 0; |
(*cf)->body[s] = 0; |
310 |
|
|
311 |
#ifdef DEBUG |
#ifdef DEBUG |
312 |
printf(" '%s': '%s'\n", (*cf)->name, (*cf)->body); |
printf(" '%s' '%s'\n", (*cf)->name, (*cf)->body); |
313 |
#endif |
#endif |
314 |
return 0; |
return 0; |
315 |
} |
} |
329 |
|
|
330 |
if (!strncmp(line + match[1].rm_so, "DENY_LIMIT", 10)) { |
if (!strncmp(line + match[1].rm_so, "DENY_LIMIT", 10)) { |
331 |
dlimit = strtoul(lim, NULL, 0); |
dlimit = strtoul(lim, NULL, 0); |
332 |
|
#ifdef DEBUG |
333 |
|
printf("debug: size filter DENY_LIMIT: %d\n", dlimit); |
334 |
|
#endif |
335 |
} else { |
} else { |
336 |
alimit = strtoul(lim, NULL, 0); |
alimit = strtoul(lim, NULL, 0); |
337 |
|
#ifdef DEBUG |
338 |
|
printf("debug: size filter ALLOW_LIMIT: %d\n", alimit); |
339 |
|
#endif |
340 |
|
} |
341 |
|
} |
342 |
|
|
343 |
|
|
344 |
|
/* |
345 |
|
* Sets other options found in config file. |
346 |
|
*/ |
347 |
|
void set_options(char *line, regmatch_t * match) |
348 |
|
{ |
349 |
|
int s; |
350 |
|
|
351 |
|
if (!logfile[0] && !strncmp(line + match[1].rm_so, "LOGFILE", 7)) { |
352 |
|
s = min((match[2].rm_eo - match[2].rm_so), PATH_MAX - 1); |
353 |
|
strncpy(logfile, line + match[2].rm_so, s); |
354 |
|
logfile[s] = 0; |
355 |
|
} else if (!strncmp(line + match[1].rm_so, "TEST_MODE", 9) && |
356 |
|
!(options & OPT_TEST_MODE_STICKY)) { |
357 |
|
if (!strncmp(line + match[2].rm_so, "yes", 3)) |
358 |
|
options |= OPT_TEST_MODE; |
359 |
|
else if (options & OPT_TEST_MODE) |
360 |
|
options ^= OPT_TEST_MODE; |
361 |
|
} else if (!strncmp(line + match[1].rm_so, "SHOW_HEADERS", 12)) { |
362 |
|
if (!strncmp(line + match[2].rm_so, "yes", 3)) |
363 |
|
options |= OPT_SHOW_HEADERS; |
364 |
|
else if (options & OPT_UNSEEN_ONLY) |
365 |
|
options ^= OPT_SHOW_HEADERS; |
366 |
|
} else if (!strncmp(line + match[1].rm_so, "UNSEEN_ONLY", 11)) { |
367 |
|
if (!strncmp(line + match[2].rm_so, "yes", 3)) |
368 |
|
options |= OPT_UNSEEN_ONLY; |
369 |
|
else if (options & OPT_UNSEEN_ONLY) |
370 |
|
options ^= OPT_UNSEEN_ONLY; |
371 |
} |
} |
372 |
} |
} |