/[imapfilter]/imapfilter/parse.c
ViewVC logotype

Contents of /imapfilter/parse.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (show annotations)
Thu Feb 19 11:30:50 2004 UTC (16 years, 8 months ago) by lefcha
Branch: MAIN
CVS Tags: HEAD
Changes since 1.16: +11 -2 lines
File MIME type: text/plain
Removed deprecated options debug message, added new based on options_t debug messages.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26