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