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

Contents of /imapfilter/parse.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations)
Fri Feb 13 12:17:16 2004 UTC (20 years, 1 month ago) by lefcha
Branch: MAIN
Changes since 1.11: +17 -17 lines
File MIME type: text/plain
Stylistic changes.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26