1 |
lefcha |
1.1 |
#include <stdio.h> |
2 |
lefcha |
1.3 |
#include <stdlib.h> |
3 |
lefcha |
1.1 |
#include <string.h> |
4 |
lefcha |
1.5 |
#include <strings.h> |
5 |
|
|
#include <errno.h> |
6 |
lefcha |
1.1 |
#include <limits.h> |
7 |
|
|
#include <sys/stat.h> |
8 |
lefcha |
1.6 |
#include <sys/types.h> /* IEEE Std 1003.1-2001 non-conformance. */ |
9 |
lefcha |
1.5 |
#include <regex.h> |
10 |
lefcha |
1.1 |
|
11 |
|
|
#include "config.h" |
12 |
|
|
#include "imapfilter.h" |
13 |
|
|
#include "pathnames.h" |
14 |
|
|
|
15 |
|
|
|
16 |
|
|
extern char logfile[PATH_MAX]; |
17 |
|
|
extern unsigned int options; |
18 |
|
|
extern char charset[CHARSET_LEN]; |
19 |
|
|
extern unsigned int flags; |
20 |
|
|
extern unsigned int interval; |
21 |
|
|
extern long timeout; |
22 |
|
|
extern char *home; |
23 |
|
|
|
24 |
|
|
#ifdef ENCRYPTED_PASSWORDS |
25 |
|
|
char *passphr = NULL; /* Master password to access the passwords |
26 |
|
|
* file. */ |
27 |
|
|
#endif |
28 |
|
|
|
29 |
|
|
|
30 |
|
|
int parse_config(FILE * fd); |
31 |
|
|
void set_options(char *line, regmatch_t * match); |
32 |
|
|
|
33 |
|
|
int parse_passwords(FILE * fd); |
34 |
|
|
|
35 |
|
|
|
36 |
|
|
/* |
37 |
|
|
* Find the path to configuration file, open it and call parse_config(). |
38 |
|
|
*/ |
39 |
|
|
int |
40 |
|
|
read_config(char *cfg) |
41 |
|
|
{ |
42 |
|
|
int r; |
43 |
|
|
FILE *fd; |
44 |
|
|
char *c; |
45 |
|
|
|
46 |
|
|
c = NULL; |
47 |
|
|
|
48 |
|
|
if (cfg == NULL) { |
49 |
|
|
cfg = c = (char *)xmalloc(PATH_MAX * sizeof(char)); |
50 |
|
|
snprintf(cfg, PATH_MAX, "%s/%s", home, PATHNAME_CONFIG_FILE); |
51 |
|
|
} |
52 |
lefcha |
1.9 |
debug("configuration file: '%s'\n", cfg); |
53 |
lefcha |
1.8 |
|
54 |
lefcha |
1.1 |
fd = fopen(cfg, "r"); |
55 |
|
|
if (fd == NULL) |
56 |
|
|
fatal(ERROR_FILE_OPEN, "opening config file %s; %s\n", cfg, |
57 |
|
|
strerror(errno)); |
58 |
|
|
|
59 |
|
|
if (c != NULL) |
60 |
|
|
xfree(c); |
61 |
|
|
|
62 |
|
|
if ((r = parse_config(fd))) |
63 |
|
|
fatal(ERROR_CONFIG_PARSE, |
64 |
|
|
"parse error in config file at row %d\n", r); |
65 |
|
|
|
66 |
|
|
fclose(fd); |
67 |
|
|
|
68 |
lefcha |
1.8 |
check_file_perms(cfg, S_IRUSR | S_IWUSR); |
69 |
|
|
|
70 |
lefcha |
1.9 |
debug("options: %0#10x '%s'\n", options, charset); |
71 |
lefcha |
1.1 |
|
72 |
|
|
return 0; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
|
76 |
|
|
/* |
77 |
|
|
* Parse configuration file. |
78 |
|
|
*/ |
79 |
|
|
int |
80 |
|
|
parse_config(FILE * fd) |
81 |
|
|
{ |
82 |
|
|
int i, r; |
83 |
|
|
unsigned int row; |
84 |
|
|
char line[LINE_MAX]; |
85 |
|
|
regex_t creg[13]; |
86 |
|
|
regmatch_t match[11]; |
87 |
|
|
const char *reg[13] = { |
88 |
|
|
"^([[:blank:]]*\n|#.*\n)$", |
89 |
|
|
|
90 |
|
|
"^[[:blank:]]*ACCOUNT[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+" |
91 |
|
|
"(([[:graph:]]+):([[:graph:]]*)|([[:graph:]]+))@" |
92 |
|
|
"([[:alnum:].-]+)(:[[:digit:]]+)?[[:blank:]]*" |
93 |
|
|
"([[:blank:]]SSL2|[[:blank:]]SSL3|[[:blank:]]TLS1)?" |
94 |
|
|
"[[:blank:]]*\n$", |
95 |
|
|
|
96 |
|
|
"^[[:blank:]]*FOLDER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+" |
97 |
|
|
"([[:print:]]+)[[:blank:]]*\n$", |
98 |
|
|
|
99 |
|
|
"^[[:blank:]]*FILTER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]*" |
100 |
|
|
"([[:blank:]]OR|[[:blank:]]AND)?[[:blank:]]*\n$", |
101 |
|
|
|
102 |
|
|
"^[[:blank:]]*ACTION[[:blank:]]+(DELETE|" |
103 |
|
|
"COPY[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)|" |
104 |
|
|
"MOVE[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)|" |
105 |
|
|
"RCOPY[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+" |
106 |
|
|
"(\"[[:print:]]*\"|[[:graph:]]+)|" |
107 |
|
|
"RMOVE[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+" |
108 |
|
|
"(\"[[:print:]]*\"|[[:graph:]]+)|" |
109 |
|
|
"FLAG[[:blank:]]+(REPLACE|ADD|REMOVE)[[:blank:]]+" |
110 |
|
|
"([[:alpha:],]+)|" |
111 |
|
|
"LIST)[[:blank:]]*([[:graph:]]*)[[:blank:]]*\n$", |
112 |
|
|
|
113 |
|
|
"^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|" |
114 |
|
|
"AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*" |
115 |
|
|
"(ANSWERED|DELETED|DRAFT|FLAGGED|NEW|OLD|RECENT|SEEN|" |
116 |
|
|
"UNANSWERED|UNDELETED|UNDRAFT|UNFLAGGED|UNSEEN)[[:blank:]]*\n$", |
117 |
|
|
|
118 |
|
|
"^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|" |
119 |
|
|
"AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*" |
120 |
|
|
"(BCC|BODY|CC|FROM|SUBJECT|TEXT|TO)[[:blank:]]+" |
121 |
|
|
"(\"[[:print:]]*\"|[[:graph:]]+)[[:blank:]]*\n$", |
122 |
|
|
|
123 |
|
|
"^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|" |
124 |
|
|
"AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*" |
125 |
|
|
"(HEADER)[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)" |
126 |
|
|
"[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)[[:blank:]]*\n$", |
127 |
|
|
|
128 |
|
|
"^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|" |
129 |
|
|
"AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*" |
130 |
|
|
"(LARGER|SMALLER|OLDER|NEWER)[[:blank:]]+([[:digit:]]+)" |
131 |
|
|
"[[:blank:]]*\n$", |
132 |
|
|
|
133 |
|
|
"^[[:blank:]]*JOB[[:blank:]]+([[:alnum:],_-]+)[[:blank:]]+" |
134 |
|
|
"([[:alnum:],_-]+)[[:blank:]]*\n$", |
135 |
|
|
|
136 |
|
|
"^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(LOGFILE|CHARSET)" |
137 |
|
|
"[[:blank:]]*=[[:blank:]]*(\"[[:print:]]*\"|[[:graph:]]+)" |
138 |
|
|
"[[:blank:]]*\n$", |
139 |
|
|
|
140 |
|
|
"^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(ERRORS|EXPUNGE|" |
141 |
lefcha |
1.8 |
"HEADERS|NAMESPACE|PEEK|PERMISSIONS|PROTOCOL|SUBSCRIBE)" |
142 |
|
|
"[[:blank:]]*=[[:blank:]]*(YES|NO)[[:blank:]]*\n$", |
143 |
lefcha |
1.1 |
|
144 |
|
|
"^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(DAEMON|TIMEOUT)" |
145 |
|
|
"[[:blank:]]*=[[:blank:]]*([[:digit:]]+)\n$" |
146 |
|
|
}; |
147 |
|
|
|
148 |
|
|
r = row = 0; |
149 |
|
|
|
150 |
|
|
for (i = 0; i < 13; i++) |
151 |
|
|
regcomp(&creg[i], reg[i], REG_EXTENDED | REG_ICASE); |
152 |
|
|
|
153 |
|
|
/* First process all the variables. This is done because some |
154 |
|
|
* variables' arguments are used before the other commands are |
155 |
|
|
* processed. */ |
156 |
|
|
while (fgets(line, LINE_MAX - 1, fd)) |
157 |
|
|
if (!regexec(&creg[10], line, 4, match, 0) || |
158 |
|
|
!regexec(&creg[11], line, 4, match, 0) || |
159 |
|
|
!regexec(&creg[12], line, 4, match, 0)) |
160 |
|
|
set_options(line, match); |
161 |
|
|
|
162 |
|
|
/* Then rewind and process everything else. */ |
163 |
|
|
fseek(fd, 0L, SEEK_SET); |
164 |
|
|
while (fgets(line, LINE_MAX - 1, fd)) { |
165 |
|
|
row++; |
166 |
|
|
if (!regexec(&creg[0], line, 0, match, 0)) |
167 |
|
|
continue; |
168 |
|
|
else if (!regexec(&creg[1], line, 9, match, 0)) |
169 |
|
|
set_account(line, match); |
170 |
|
|
else if (!regexec(&creg[2], line, 3, match, 0)) |
171 |
|
|
r = set_mboxgrp(line, match); |
172 |
|
|
else if (!regexec(&creg[3], line, 3, match, 0)) |
173 |
|
|
r = set_filter(line, match); |
174 |
|
|
else if (!regexec(&creg[4], line, 11, match, 0)) |
175 |
|
|
r = set_action(line, match); |
176 |
|
|
else if (!regexec(&creg[5], line, 5, match, 0)) |
177 |
|
|
r = set_mask(line, match, MASK_MATCH_1); |
178 |
|
|
else if (!regexec(&creg[6], line, 6, match, 0)) |
179 |
|
|
r = set_mask(line, match, MASK_MATCH_2); |
180 |
|
|
else if (!regexec(&creg[7], line, 7, match, 0)) |
181 |
|
|
r = set_mask(line, match, MASK_MATCH_3); |
182 |
|
|
else if (!regexec(&creg[8], line, 6, match, 0)) |
183 |
|
|
r = set_mask(line, match, MASK_MATCH_4); |
184 |
|
|
else if (!regexec(&creg[9], line, 3, match, 0)) |
185 |
|
|
r = set_job(line, match); |
186 |
|
|
/* Skip variable processing. */ |
187 |
|
|
else if (!regexec(&creg[10], line, 4, match, 0) || |
188 |
|
|
!regexec(&creg[11], line, 4, match, 0) || |
189 |
|
|
!regexec(&creg[12], line, 4, match, 0)); |
190 |
|
|
else |
191 |
|
|
return row; |
192 |
|
|
|
193 |
|
|
if (r == ERROR_CONFIG_PARSE) |
194 |
|
|
return row; |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
for (i = 0; i < 13; i++) |
198 |
|
|
regfree(&creg[i]); |
199 |
|
|
|
200 |
|
|
destroy_unneeded(); |
201 |
|
|
|
202 |
|
|
return 0; |
203 |
|
|
} |
204 |
|
|
|
205 |
|
|
|
206 |
|
|
/* |
207 |
lefcha |
1.4 |
* Signal SIGUSR1 received, destroy all data structures and reread the |
208 |
lefcha |
1.1 |
* configuration file. |
209 |
|
|
*/ |
210 |
|
|
void |
211 |
|
|
reread_config(char *cfg) |
212 |
|
|
{ |
213 |
|
|
destroy_all(); |
214 |
|
|
read_config(cfg); |
215 |
lefcha |
1.2 |
#ifdef ENCRYPTED_PASSWORDS |
216 |
lefcha |
1.1 |
read_passwords(); |
217 |
lefcha |
1.2 |
#endif |
218 |
lefcha |
1.4 |
flags &= ~(FLAG_SIGUSR1_RECEIVED); |
219 |
lefcha |
1.1 |
} |
220 |
|
|
|
221 |
|
|
|
222 |
|
|
/* |
223 |
|
|
* Set other options found in config file. |
224 |
|
|
*/ |
225 |
|
|
void |
226 |
|
|
set_options(char *line, regmatch_t * m) |
227 |
|
|
{ |
228 |
|
|
if (!strncasecmp(line + m[2].rm_so, "logfile", 7)) { |
229 |
|
|
if (*logfile == '\0') { |
230 |
|
|
if (*(line + m[3].rm_so) == '"' && |
231 |
|
|
*(line + m[3].rm_eo - 1) == '"') |
232 |
|
|
strncat(logfile, line + m[3].rm_so + 1, |
233 |
|
|
min((m[3].rm_eo - m[3].rm_so - 2), |
234 |
|
|
PATH_MAX - 1)); |
235 |
|
|
else |
236 |
|
|
strncat(logfile, line + m[3].rm_so, |
237 |
|
|
min((m[3].rm_eo - m[3].rm_so), |
238 |
|
|
PATH_MAX - 1)); |
239 |
|
|
} |
240 |
|
|
} else if (!strncasecmp(line + m[2].rm_so, "charset", 7)) { |
241 |
|
|
if (*(line + m[3].rm_so) == '"' && |
242 |
|
|
*(line + m[3].rm_eo - 1) == '"') |
243 |
|
|
strncat(charset, line + m[3].rm_so + 1, |
244 |
|
|
min((m[3].rm_eo - m[3].rm_so - 2), |
245 |
|
|
CHARSET_LEN - 1)); |
246 |
|
|
else |
247 |
|
|
strncat(charset, line + m[3].rm_so, |
248 |
|
|
min((m[3].rm_eo - m[3].rm_so), CHARSET_LEN - 1)); |
249 |
|
|
} else if (!strncasecmp(line + m[2].rm_so, "errors", 6)) { |
250 |
|
|
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
251 |
|
|
options |= OPTION_ERRORS; |
252 |
|
|
else |
253 |
|
|
options &= ~(OPTION_ERRORS); |
254 |
|
|
} else if (!strncasecmp(line + m[2].rm_so, "expunge", 7)) { |
255 |
|
|
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
256 |
|
|
options |= OPTION_EXPUNGE; |
257 |
|
|
else |
258 |
|
|
options &= ~(OPTION_EXPUNGE); |
259 |
|
|
} else if (!strncasecmp(line + m[2].rm_so, "header", 6)) { |
260 |
|
|
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
261 |
|
|
options |= OPTION_HEADERS; |
262 |
|
|
else |
263 |
|
|
options &= ~(OPTION_HEADERS); |
264 |
|
|
} else if (!strncasecmp(line + m[2].rm_so, "namespace", 9)) { |
265 |
|
|
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
266 |
|
|
options |= OPTION_NAMESPACE; |
267 |
|
|
else |
268 |
|
|
options &= ~(OPTION_NAMESPACE); |
269 |
lefcha |
1.7 |
} else if (!strncasecmp(line + m[2].rm_so, "peek", 4)) { |
270 |
|
|
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
271 |
|
|
options |= OPTION_PEEK; |
272 |
|
|
else |
273 |
|
|
options &= ~(OPTION_PEEK); |
274 |
lefcha |
1.8 |
} else if (!strncasecmp(line + m[2].rm_so, "permissions", 8)) { |
275 |
|
|
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
276 |
|
|
options |= OPTION_PERMISSIONS; |
277 |
|
|
else |
278 |
|
|
options &= ~(OPTION_PERMISSIONS); |
279 |
lefcha |
1.1 |
} else if (!strncasecmp(line + m[2].rm_so, "subscribe", 9)) { |
280 |
|
|
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
281 |
|
|
options |= OPTION_SUBSCRIBE; |
282 |
|
|
else |
283 |
|
|
options &= ~(OPTION_SUBSCRIBE); |
284 |
|
|
} else if (!strncasecmp(line + m[2].rm_so, "timeout", 7)) { |
285 |
|
|
errno = 0; |
286 |
|
|
timeout = strtol(line + m[3].rm_so, NULL, 10); |
287 |
|
|
if (errno) |
288 |
|
|
timeout = 0; |
289 |
|
|
} else if (!strncasecmp(line + m[2].rm_so, "daemon", 6) && |
290 |
|
|
!(options & OPTION_DAEMON_MODE)) { |
291 |
|
|
options |= OPTION_DAEMON_MODE; |
292 |
|
|
errno = 0; |
293 |
|
|
interval = strtoul(line + m[3].rm_so, NULL, 10); |
294 |
|
|
if (errno) |
295 |
|
|
interval = 0; |
296 |
|
|
} |
297 |
|
|
} |
298 |
|
|
|
299 |
|
|
|
300 |
|
|
#ifdef ENCRYPTED_PASSWORDS |
301 |
|
|
/* |
302 |
|
|
* Open password file and call parse_passwords(). |
303 |
|
|
*/ |
304 |
|
|
int |
305 |
|
|
read_passwords(void) |
306 |
|
|
{ |
307 |
|
|
FILE *fd; |
308 |
|
|
char pwfile[PATH_MAX]; |
309 |
|
|
|
310 |
|
|
if (!(flags & FLAG_BLANK_PASSWORD)) |
311 |
|
|
return ERROR_CONFIG_PARSE; |
312 |
|
|
|
313 |
|
|
if (!passphr) { |
314 |
|
|
passphr = (char *)smalloc(PASSPHRASE_LEN); |
315 |
|
|
*passphr = '\0'; |
316 |
|
|
} |
317 |
|
|
snprintf(pwfile, PATH_MAX, "%s/%s", home, PATHNAME_PASSWORD_FILE); |
318 |
lefcha |
1.9 |
debug("passwords file: '%s'\n", pwfile); |
319 |
lefcha |
1.1 |
|
320 |
|
|
if (!exists_file(pwfile)) |
321 |
|
|
return ERROR_FILE_OPEN; |
322 |
|
|
|
323 |
|
|
check_file_perms(pwfile, S_IRUSR | S_IWUSR); |
324 |
|
|
|
325 |
|
|
fd = fopen(pwfile, "r"); |
326 |
|
|
if (fd == NULL) |
327 |
|
|
fatal(ERROR_FILE_OPEN, "opening passwords file %s; %s\n", |
328 |
|
|
pwfile, strerror(errno)); |
329 |
|
|
|
330 |
|
|
parse_passwords(fd); |
331 |
|
|
|
332 |
|
|
fclose(fd); |
333 |
|
|
|
334 |
|
|
return 0; |
335 |
|
|
} |
336 |
|
|
|
337 |
|
|
|
338 |
|
|
/* |
339 |
|
|
* Parse unencrypted password file. |
340 |
|
|
*/ |
341 |
|
|
int |
342 |
|
|
parse_passwords(FILE * fd) |
343 |
|
|
{ |
344 |
|
|
int t; |
345 |
|
|
char *pe; |
346 |
|
|
char user[USERNAME_LEN], serv[SERVER_LEN]; |
347 |
|
|
unsigned char *buf; |
348 |
|
|
char *c, *cp, *line; |
349 |
|
|
regex_t creg; |
350 |
|
|
regmatch_t match[4]; |
351 |
|
|
const char *reg; |
352 |
|
|
|
353 |
|
|
t = 3; |
354 |
|
|
pe = NULL; |
355 |
|
|
reg = "([[:alnum:].-]+) ([[:graph:]]+) ([[:graph:]]+)"; |
356 |
|
|
|
357 |
|
|
if (!(flags & FLAG_DAEMON_MODE)) { |
358 |
|
|
do { |
359 |
|
|
fseek(fd, 0L, SEEK_SET); |
360 |
|
|
printf("Enter master passphrase: "); |
361 |
|
|
get_password(passphr, PASSPHRASE_LEN); |
362 |
|
|
} while (decrypt_passwords(&buf, fd) && --t != 0); |
363 |
|
|
|
364 |
|
|
if (!t) |
365 |
|
|
return ERROR_PASSPHRASE; |
366 |
|
|
} else if (decrypt_passwords(&buf, fd)) |
367 |
|
|
fatal(ERROR_DECRYPT, |
368 |
|
|
"failed decrypting passwords in daemon mode\n"); |
369 |
|
|
|
370 |
|
|
c = cp = sstrdup(buf); |
371 |
|
|
|
372 |
|
|
regcomp(&creg, reg, REG_EXTENDED | REG_ICASE); |
373 |
|
|
|
374 |
|
|
line = strtok_r(c, "\n", &c); |
375 |
|
|
while (line != NULL && !regexec(&creg, line, 4, match, 0)) { |
376 |
|
|
user[0] = serv[0] = '\0'; |
377 |
|
|
|
378 |
|
|
strncat(serv, line + match[1].rm_so, |
379 |
|
|
min(match[1].rm_eo - match[1].rm_so, SERVER_LEN - 1)); |
380 |
|
|
strncat(user, line + match[2].rm_so, |
381 |
|
|
min(match[2].rm_eo - match[2].rm_so, USERNAME_LEN - 1)); |
382 |
|
|
|
383 |
|
|
if ((pe = (char *)find_password(user, serv))) |
384 |
|
|
strncat(pe, line + match[3].rm_so, |
385 |
|
|
min(match[3].rm_eo - match[3].rm_so, |
386 |
|
|
PASSWORD_LEN - 1)); |
387 |
|
|
|
388 |
|
|
line = strtok_r(NULL, "\n", &c); |
389 |
|
|
} |
390 |
|
|
|
391 |
|
|
regfree(&creg); |
392 |
|
|
sfree(cp); |
393 |
|
|
sfree(buf); |
394 |
|
|
|
395 |
|
|
return 0; |
396 |
|
|
} |
397 |
|
|
#endif /* ENCRYPTED_PASSWORDS */ |