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 |
read_passwords(); |
218 |
|
219 |
flags &= ~(FLAG_SIGHUP_RECEIVED); |
220 |
} |
221 |
|
222 |
|
223 |
/* |
224 |
* Set other options found in config file. |
225 |
*/ |
226 |
void |
227 |
set_options(char *line, regmatch_t * m) |
228 |
{ |
229 |
if (!strncasecmp(line + m[2].rm_so, "logfile", 7)) { |
230 |
if (*logfile == '\0') { |
231 |
if (*(line + m[3].rm_so) == '"' && |
232 |
*(line + m[3].rm_eo - 1) == '"') |
233 |
strncat(logfile, line + m[3].rm_so + 1, |
234 |
min((m[3].rm_eo - m[3].rm_so - 2), |
235 |
PATH_MAX - 1)); |
236 |
else |
237 |
strncat(logfile, line + m[3].rm_so, |
238 |
min((m[3].rm_eo - m[3].rm_so), |
239 |
PATH_MAX - 1)); |
240 |
} |
241 |
} else if (!strncasecmp(line + m[2].rm_so, "charset", 7)) { |
242 |
if (*(line + m[3].rm_so) == '"' && |
243 |
*(line + m[3].rm_eo - 1) == '"') |
244 |
strncat(charset, line + m[3].rm_so + 1, |
245 |
min((m[3].rm_eo - m[3].rm_so - 2), |
246 |
CHARSET_LEN - 1)); |
247 |
else |
248 |
strncat(charset, line + m[3].rm_so, |
249 |
min((m[3].rm_eo - m[3].rm_so), CHARSET_LEN - 1)); |
250 |
} else if (!strncasecmp(line + m[2].rm_so, "errors", 6)) { |
251 |
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
252 |
options |= OPTION_ERRORS; |
253 |
else |
254 |
options &= ~(OPTION_ERRORS); |
255 |
} else if (!strncasecmp(line + m[2].rm_so, "expunge", 7)) { |
256 |
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
257 |
options |= OPTION_EXPUNGE; |
258 |
else |
259 |
options &= ~(OPTION_EXPUNGE); |
260 |
} else if (!strncasecmp(line + m[2].rm_so, "header", 6)) { |
261 |
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
262 |
options |= OPTION_HEADERS; |
263 |
else |
264 |
options &= ~(OPTION_HEADERS); |
265 |
} else if (!strncasecmp(line + m[2].rm_so, "namespace", 9)) { |
266 |
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
267 |
options |= OPTION_NAMESPACE; |
268 |
else |
269 |
options &= ~(OPTION_NAMESPACE); |
270 |
} else if (!strncasecmp(line + m[2].rm_so, "subscribe", 9)) { |
271 |
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
272 |
options |= OPTION_SUBSCRIBE; |
273 |
else |
274 |
options &= ~(OPTION_SUBSCRIBE); |
275 |
} else if (!strncasecmp(line + m[2].rm_so, "timeout", 7)) { |
276 |
errno = 0; |
277 |
timeout = strtol(line + m[3].rm_so, NULL, 10); |
278 |
if (errno) |
279 |
timeout = 0; |
280 |
} else if (!strncasecmp(line + m[2].rm_so, "daemon", 6) && |
281 |
!(options & OPTION_DAEMON_MODE)) { |
282 |
options |= OPTION_DAEMON_MODE; |
283 |
errno = 0; |
284 |
interval = strtoul(line + m[3].rm_so, NULL, 10); |
285 |
if (errno) |
286 |
interval = 0; |
287 |
} |
288 |
} |
289 |
|
290 |
|
291 |
#ifdef ENCRYPTED_PASSWORDS |
292 |
/* |
293 |
* Open password file and call parse_passwords(). |
294 |
*/ |
295 |
int |
296 |
read_passwords(void) |
297 |
{ |
298 |
FILE *fd; |
299 |
char pwfile[PATH_MAX]; |
300 |
|
301 |
if (!(flags & FLAG_BLANK_PASSWORD)) |
302 |
return ERROR_CONFIG_PARSE; |
303 |
|
304 |
if (!passphr) { |
305 |
passphr = (char *)smalloc(PASSPHRASE_LEN); |
306 |
*passphr = '\0'; |
307 |
} |
308 |
snprintf(pwfile, PATH_MAX, "%s/%s", home, PATHNAME_PASSWORD_FILE); |
309 |
#ifdef DEBUG |
310 |
fprintf(stderr, "debug: passwords file: '%s'\n", pwfile); |
311 |
#endif |
312 |
|
313 |
if (!exists_file(pwfile)) |
314 |
return ERROR_FILE_OPEN; |
315 |
|
316 |
#ifdef CHECK_PERMISSIONS |
317 |
check_file_perms(pwfile, S_IRUSR | S_IWUSR); |
318 |
#endif |
319 |
|
320 |
fd = fopen(pwfile, "r"); |
321 |
if (fd == NULL) |
322 |
fatal(ERROR_FILE_OPEN, "opening passwords file %s; %s\n", |
323 |
pwfile, strerror(errno)); |
324 |
|
325 |
parse_passwords(fd); |
326 |
|
327 |
fclose(fd); |
328 |
|
329 |
return 0; |
330 |
} |
331 |
|
332 |
|
333 |
/* |
334 |
* Parse unencrypted password file. |
335 |
*/ |
336 |
int |
337 |
parse_passwords(FILE * fd) |
338 |
{ |
339 |
int t; |
340 |
char *pe; |
341 |
char user[USERNAME_LEN], serv[SERVER_LEN]; |
342 |
unsigned char *buf; |
343 |
char *c, *cp, *line; |
344 |
regex_t creg; |
345 |
regmatch_t match[4]; |
346 |
const char *reg; |
347 |
|
348 |
t = 3; |
349 |
pe = NULL; |
350 |
reg = "([[:alnum:].-]+) ([[:graph:]]+) ([[:graph:]]+)"; |
351 |
|
352 |
if (!(flags & FLAG_DAEMON_MODE)) { |
353 |
do { |
354 |
fseek(fd, 0L, SEEK_SET); |
355 |
printf("Enter master passphrase: "); |
356 |
get_password(passphr, PASSPHRASE_LEN); |
357 |
} while (decrypt_passwords(&buf, fd) && --t != 0); |
358 |
|
359 |
if (!t) |
360 |
return ERROR_PASSPHRASE; |
361 |
} else if (decrypt_passwords(&buf, fd)) |
362 |
fatal(ERROR_DECRYPT, |
363 |
"failed decrypting passwords in daemon mode\n"); |
364 |
|
365 |
c = cp = sstrdup(buf); |
366 |
|
367 |
regcomp(&creg, reg, REG_EXTENDED | REG_ICASE); |
368 |
|
369 |
line = strtok_r(c, "\n", &c); |
370 |
while (line != NULL && !regexec(&creg, line, 4, match, 0)) { |
371 |
user[0] = serv[0] = '\0'; |
372 |
|
373 |
strncat(serv, line + match[1].rm_so, |
374 |
min(match[1].rm_eo - match[1].rm_so, SERVER_LEN - 1)); |
375 |
strncat(user, line + match[2].rm_so, |
376 |
min(match[2].rm_eo - match[2].rm_so, USERNAME_LEN - 1)); |
377 |
|
378 |
if ((pe = (char *)find_password(user, serv))) |
379 |
strncat(pe, line + match[3].rm_so, |
380 |
min(match[3].rm_eo - match[3].rm_so, |
381 |
PASSWORD_LEN - 1)); |
382 |
|
383 |
line = strtok_r(NULL, "\n", &c); |
384 |
} |
385 |
|
386 |
regfree(&creg); |
387 |
sfree(cp); |
388 |
sfree(buf); |
389 |
|
390 |
return 0; |
391 |
} |
392 |
|
393 |
|
394 |
/* |
395 |
* Store encrypted passwords to file. |
396 |
*/ |
397 |
int |
398 |
store_passwords(account_t ** accts) |
399 |
{ |
400 |
char pwfile[PATH_MAX]; |
401 |
FILE *fd; |
402 |
|
403 |
snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords"); |
404 |
|
405 |
create_file(pwfile, S_IRUSR | S_IWUSR); |
406 |
|
407 |
fd = fopen(pwfile, "w"); |
408 |
|
409 |
if (fd == NULL) |
410 |
fatal(ERROR_FILE_OPEN, "opening passwords file %s; %s\n", |
411 |
pwfile, strerror(errno)); |
412 |
|
413 |
encrypt_passwords(fd, accts); |
414 |
|
415 |
fclose(fd); |
416 |
|
417 |
return 0; |
418 |
} |
419 |
#endif /* ENCRYPTED_PASSWORDS */ |