1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <string.h> |
4 |
#include <strings.h> |
5 |
#include <errno.h> |
6 |
#include <limits.h> |
7 |
#include <sys/stat.h> |
8 |
#include <sys/types.h> /* IEEE Std 1003.1-2001 non-conformance. */ |
9 |
#include <regex.h> |
10 |
|
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 |
debug("configuration file: '%s'\n", cfg); |
53 |
|
54 |
check_file_perms(cfg, S_IRUSR | S_IWUSR); |
55 |
|
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 |
debug("options: %0#10x '%s'\n", options, charset); |
71 |
|
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 |
"HEADERS|NAMESPACE|PEEK|SUBSCRIBE)[[:blank:]]*=[[:blank:]]*" |
142 |
"(YES|NO)[[:blank:]]*\n$", |
143 |
|
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 |
* Signal SIGUSR1 received, destroy all data structures and reread the |
208 |
* configuration file. |
209 |
*/ |
210 |
void |
211 |
reread_config(char *cfg) |
212 |
{ |
213 |
destroy_all(); |
214 |
read_config(cfg); |
215 |
#ifdef ENCRYPTED_PASSWORDS |
216 |
read_passwords(); |
217 |
#endif |
218 |
flags &= ~(FLAG_SIGUSR1_RECEIVED); |
219 |
} |
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 |
} 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 |
} else if (!strncasecmp(line + m[2].rm_so, "subscribe", 9)) { |
275 |
if (!strncasecmp(line + m[3].rm_so, "yes", 3)) |
276 |
options |= OPTION_SUBSCRIBE; |
277 |
else |
278 |
options &= ~(OPTION_SUBSCRIBE); |
279 |
} else if (!strncasecmp(line + m[2].rm_so, "timeout", 7)) { |
280 |
errno = 0; |
281 |
timeout = strtol(line + m[3].rm_so, NULL, 10); |
282 |
if (errno) |
283 |
timeout = 0; |
284 |
} else if (!strncasecmp(line + m[2].rm_so, "daemon", 6) && |
285 |
!(options & OPTION_DAEMON_MODE)) { |
286 |
options |= OPTION_DAEMON_MODE; |
287 |
errno = 0; |
288 |
interval = strtoul(line + m[3].rm_so, NULL, 10); |
289 |
if (errno) |
290 |
interval = 0; |
291 |
} |
292 |
} |
293 |
|
294 |
|
295 |
#ifdef ENCRYPTED_PASSWORDS |
296 |
/* |
297 |
* Open password file and call parse_passwords(). |
298 |
*/ |
299 |
int |
300 |
read_passwords(void) |
301 |
{ |
302 |
FILE *fd; |
303 |
char pwfile[PATH_MAX]; |
304 |
|
305 |
if (!(flags & FLAG_BLANK_PASSWORD)) |
306 |
return ERROR_CONFIG_PARSE; |
307 |
|
308 |
if (!passphr) { |
309 |
passphr = (char *)smalloc(PASSPHRASE_LEN); |
310 |
*passphr = '\0'; |
311 |
} |
312 |
snprintf(pwfile, PATH_MAX, "%s/%s", home, PATHNAME_PASSWORD_FILE); |
313 |
debug("passwords file: '%s'\n", pwfile); |
314 |
|
315 |
if (!exists_file(pwfile)) |
316 |
return ERROR_FILE_OPEN; |
317 |
|
318 |
check_file_perms(pwfile, S_IRUSR | S_IWUSR); |
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 |
#endif /* ENCRYPTED_PASSWORDS */ |