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

Annotation of /imapfilter/parse.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (hide annotations)
Thu Feb 19 11:30:50 2004 UTC (20 years, 1 month 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 lefcha 1.1 #include <stdio.h>
2 lefcha 1.3 #include <stdlib.h>
3 lefcha 1.1 #include <string.h>
4 lefcha 1.5 #include <strings.h>
5     #include <errno.h>
6 lefcha 1.1 #include <limits.h>
7     #include <sys/stat.h>
8 lefcha 1.14 #include <sys/types.h> /* For POSIX.1-2001 non-conformant systems. */
9 lefcha 1.5 #include <regex.h>
10 lefcha 1.1
11     #include "config.h"
12     #include "imapfilter.h"
13     #include "pathnames.h"
14    
15    
16 lefcha 1.12 extern options_t opts;
17 lefcha 1.1 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 lefcha 1.12 void set_options(char *line, regmatch_t * m);
28 lefcha 1.1
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 lefcha 1.12 snprintf(cfg, PATH_MAX, "%s/%s", home, PATHNAME_CONFIG);
47 lefcha 1.1 }
48 lefcha 1.9 debug("configuration file: '%s'\n", cfg);
49 lefcha 1.8
50 lefcha 1.10 check_file_perms(cfg, S_IRUSR | S_IWUSR);
51    
52 lefcha 1.1 fd = fopen(cfg, "r");
53     if (fd == NULL)
54 lefcha 1.12 fatal(ERROR_FILEOPEN, "opening config file %s; %s\n", cfg,
55 lefcha 1.1 strerror(errno));
56    
57     if (c != NULL)
58     xfree(c);
59    
60     if ((r = parse_config(fd)))
61 lefcha 1.12 fatal(ERROR_PARSER,
62 lefcha 1.1 "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 lefcha 1.16 "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(LOGFILE|CHARSET|"
131     "_PROTOCOL)[[:blank:]]*=[[:blank:]]*(\"[[:print:]]*\"|"
132     "[[:graph:]]+)[[:blank:]]*\n$",
133 lefcha 1.1
134     "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(ERRORS|EXPUNGE|"
135 lefcha 1.10 "HEADERS|NAMESPACE|PEEK|SUBSCRIBE)[[:blank:]]*=[[:blank:]]*"
136     "(YES|NO)[[:blank:]]*\n$",
137 lefcha 1.1
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 lefcha 1.15 !regexec(&creg[12], line, 4, match, 0))
184     continue;
185 lefcha 1.1 else
186     return row;
187    
188 lefcha 1.12 if (r == ERROR_PARSER)
189 lefcha 1.1 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 lefcha 1.4 * Signal SIGUSR1 received, destroy all data structures and reread the
203 lefcha 1.1 * configuration file.
204     */
205     void
206     reread_config(char *cfg)
207     {
208 lefcha 1.13
209 lefcha 1.1 destroy_all();
210     read_config(cfg);
211 lefcha 1.2 #ifdef ENCRYPTED_PASSWORDS
212 lefcha 1.1 read_passwords();
213 lefcha 1.2 #endif
214 lefcha 1.12 flags &= ~(FLAG_SIGUSR1);
215 lefcha 1.1 }
216    
217    
218     /*
219     * Set other options found in config file.
220     */
221     void
222     set_options(char *line, regmatch_t * m)
223     {
224 lefcha 1.13
225 lefcha 1.1 if (!strncasecmp(line + m[2].rm_so, "logfile", 7)) {
226 lefcha 1.11 if (*opts.logfile == '\0') {
227 lefcha 1.1 if (*(line + m[3].rm_so) == '"' &&
228     *(line + m[3].rm_eo - 1) == '"')
229 lefcha 1.11 strncat(opts.logfile, line + m[3].rm_so + 1,
230 lefcha 1.1 min((m[3].rm_eo - m[3].rm_so - 2),
231 lefcha 1.15 PATH_MAX - 1));
232 lefcha 1.1 else
233 lefcha 1.11 strncat(opts.logfile, line + m[3].rm_so,
234 lefcha 1.1 min((m[3].rm_eo - m[3].rm_so),
235 lefcha 1.15 PATH_MAX - 1));
236 lefcha 1.1 }
237 lefcha 1.17 debug("options: logfile = '%s'\n", opts.logfile);
238 lefcha 1.1 } 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 lefcha 1.11 strncat(opts.charset, line + m[3].rm_so + 1,
242 lefcha 1.1 min((m[3].rm_eo - m[3].rm_so - 2),
243 lefcha 1.15 CHARSET_LEN - 1));
244 lefcha 1.1 else
245 lefcha 1.11 strncat(opts.charset, line + m[3].rm_so,
246 lefcha 1.1 min((m[3].rm_eo - m[3].rm_so), CHARSET_LEN - 1));
247 lefcha 1.17 debug("options: charset = '%s'\n", opts.charset);
248 lefcha 1.16 } 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 lefcha 1.17 debug("options: force_protocol = %d\n", opts.force_protocol);
256 lefcha 1.1 } else if (!strncasecmp(line + m[2].rm_so, "errors", 6)) {
257     if (!strncasecmp(line + m[3].rm_so, "yes", 3))
258 lefcha 1.11 opts.errors = 1;
259 lefcha 1.1 else
260 lefcha 1.11 opts.errors = 0;
261 lefcha 1.17 debug("options: errors = %d\n", opts.errors);
262 lefcha 1.1 } else if (!strncasecmp(line + m[2].rm_so, "expunge", 7)) {
263     if (!strncasecmp(line + m[3].rm_so, "yes", 3))
264 lefcha 1.11 opts.expunge = 1;
265 lefcha 1.1 else
266 lefcha 1.11 opts.expunge = 0;
267 lefcha 1.17 debug("options: expunge = %d\n", opts.expunge);
268 lefcha 1.1 } else if (!strncasecmp(line + m[2].rm_so, "header", 6)) {
269     if (!strncasecmp(line + m[3].rm_so, "yes", 3))
270 lefcha 1.11 opts.headers = 1;
271 lefcha 1.1 else
272 lefcha 1.11 opts.headers = 0;
273 lefcha 1.17 debug("options: headers = %d\n", opts.headers);
274 lefcha 1.1 } else if (!strncasecmp(line + m[2].rm_so, "namespace", 9)) {
275     if (!strncasecmp(line + m[3].rm_so, "yes", 3))
276 lefcha 1.11 opts.namespace = 1;
277 lefcha 1.1 else
278 lefcha 1.11 opts.namespace = 0;
279 lefcha 1.17 debug("options: namespace = %d\n", opts.namespace);
280 lefcha 1.7 } else if (!strncasecmp(line + m[2].rm_so, "peek", 4)) {
281     if (!strncasecmp(line + m[3].rm_so, "yes", 3))
282 lefcha 1.11 opts.peek = 1;
283 lefcha 1.7 else
284 lefcha 1.11 opts.peek = 0;
285 lefcha 1.17 debug("options: peek = %d\n", opts.peek);
286 lefcha 1.1 } else if (!strncasecmp(line + m[2].rm_so, "subscribe", 9)) {
287     if (!strncasecmp(line + m[3].rm_so, "yes", 3))
288 lefcha 1.11 opts.subscribe = 1;
289 lefcha 1.1 else
290 lefcha 1.11 opts.subscribe = 0;
291 lefcha 1.17 debug("options: subscribe = %d\n", opts.subscribe);
292 lefcha 1.1 } else if (!strncasecmp(line + m[2].rm_so, "timeout", 7)) {
293     errno = 0;
294 lefcha 1.11 opts.timeout = strtol(line + m[3].rm_so, NULL, 10);
295 lefcha 1.1 if (errno)
296 lefcha 1.11 opts.timeout = 0;
297 lefcha 1.17 debug("options: timeout = %d\n", opts.timeout);
298 lefcha 1.1 } else if (!strncasecmp(line + m[2].rm_so, "daemon", 6) &&
299 lefcha 1.11 !opts.daemon) {
300 lefcha 1.1 errno = 0;
301 lefcha 1.11 opts.daemon = strtoul(line + m[3].rm_so, NULL, 10);
302 lefcha 1.1 if (errno)
303 lefcha 1.11 opts.daemon = 0;
304 lefcha 1.17 debug("options: daemon = %d\n", opts.daemon);
305 lefcha 1.1 }
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 lefcha 1.12 if (!(flags & FLAG_BLANKPASS))
320     return ERROR_PARSER;
321 lefcha 1.1
322     if (!passphr) {
323     passphr = (char *)smalloc(PASSPHRASE_LEN);
324     *passphr = '\0';
325     }
326 lefcha 1.12 snprintf(pwfile, PATH_MAX, "%s/%s", home, PATHNAME_PASSWORDS);
327 lefcha 1.9 debug("passwords file: '%s'\n", pwfile);
328 lefcha 1.1
329     if (!exists_file(pwfile))
330 lefcha 1.12 return ERROR_FILEOPEN;
331 lefcha 1.1
332     check_file_perms(pwfile, S_IRUSR | S_IWUSR);
333    
334     fd = fopen(pwfile, "r");
335     if (fd == NULL)
336 lefcha 1.12 fatal(ERROR_FILEOPEN, "opening passwords file %s; %s\n",
337 lefcha 1.1 pwfile, strerror(errno));
338    
339     parse_passwords(fd);
340    
341     fclose(fd);
342    
343     return 0;
344     }
345    
346    
347     /*
348 lefcha 1.15 * Parse unencrypted password file.
349 lefcha 1.1 */
350     int
351     parse_passwords(FILE * fd)
352     {
353     int t;
354     char *pe;
355 lefcha 1.12 char user[USER_LEN], serv[SERVER_LEN];
356 lefcha 1.1 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 lefcha 1.12 if (!(flags & FLAG_DAEMON)) {
367 lefcha 1.1 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 lefcha 1.12 min(match[2].rm_eo - match[2].rm_so, USER_LEN - 1));
391 lefcha 1.1
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 lefcha 1.15 PASS_LEN - 1));
396 lefcha 1.1
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