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

Annotation of /imapfilter/parse.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16 - (hide annotations)
Sat Feb 14 23:08:31 2004 UTC (20 years, 1 month ago) by lefcha
Branch: MAIN
Changes since 1.15: +10 -3 lines
File MIME type: text/plain
Undocumented variable to force protocol version.

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 lefcha 1.11 debug("options: %0#10x '%s'\n", opts, opts.charset);
67 lefcha 1.1
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 lefcha 1.16 "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(LOGFILE|CHARSET|"
133     "_PROTOCOL)[[:blank:]]*=[[:blank:]]*(\"[[:print:]]*\"|"
134     "[[:graph:]]+)[[:blank:]]*\n$",
135 lefcha 1.1
136     "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(ERRORS|EXPUNGE|"
137 lefcha 1.10 "HEADERS|NAMESPACE|PEEK|SUBSCRIBE)[[:blank:]]*=[[:blank:]]*"
138     "(YES|NO)[[:blank:]]*\n$",
139 lefcha 1.1
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 lefcha 1.15 !regexec(&creg[12], line, 4, match, 0))
186     continue;
187 lefcha 1.1 else
188     return row;
189    
190 lefcha 1.12 if (r == ERROR_PARSER)
191 lefcha 1.1 return row;
192     }
193    
194     for (i = 0; i < 13; i++)
195     regfree(&creg[i]);
196    
197     destroy_unneeded();
198    
199     return 0;
200     }
201    
202    
203     /*
204 lefcha 1.4 * Signal SIGUSR1 received, destroy all data structures and reread the
205 lefcha 1.1 * configuration file.
206     */
207     void
208     reread_config(char *cfg)
209     {
210 lefcha 1.13
211 lefcha 1.1 destroy_all();
212     read_config(cfg);
213 lefcha 1.2 #ifdef ENCRYPTED_PASSWORDS
214 lefcha 1.1 read_passwords();
215 lefcha 1.2 #endif
216 lefcha 1.12 flags &= ~(FLAG_SIGUSR1);
217 lefcha 1.1 }
218    
219    
220     /*
221     * Set other options found in config file.
222     */
223     void
224     set_options(char *line, regmatch_t * m)
225     {
226 lefcha 1.13
227 lefcha 1.1 if (!strncasecmp(line + m[2].rm_so, "logfile", 7)) {
228 lefcha 1.11 if (*opts.logfile == '\0') {
229 lefcha 1.1 if (*(line + m[3].rm_so) == '"' &&
230     *(line + m[3].rm_eo - 1) == '"')
231 lefcha 1.11 strncat(opts.logfile, line + m[3].rm_so + 1,
232 lefcha 1.1 min((m[3].rm_eo - m[3].rm_so - 2),
233 lefcha 1.15 PATH_MAX - 1));
234 lefcha 1.1 else
235 lefcha 1.11 strncat(opts.logfile, line + m[3].rm_so,
236 lefcha 1.1 min((m[3].rm_eo - m[3].rm_so),
237 lefcha 1.15 PATH_MAX - 1));
238 lefcha 1.1 }
239     } else if (!strncasecmp(line + m[2].rm_so, "charset", 7)) {
240     if (*(line + m[3].rm_so) == '"' &&
241     *(line + m[3].rm_eo - 1) == '"')
242 lefcha 1.11 strncat(opts.charset, line + m[3].rm_so + 1,
243 lefcha 1.1 min((m[3].rm_eo - m[3].rm_so - 2),
244 lefcha 1.15 CHARSET_LEN - 1));
245 lefcha 1.1 else
246 lefcha 1.11 strncat(opts.charset, line + m[3].rm_so,
247 lefcha 1.1 min((m[3].rm_eo - m[3].rm_so), CHARSET_LEN - 1));
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.1 } else if (!strncasecmp(line + m[2].rm_so, "errors", 6)) {
256     if (!strncasecmp(line + m[3].rm_so, "yes", 3))
257 lefcha 1.11 opts.errors = 1;
258 lefcha 1.1 else
259 lefcha 1.11 opts.errors = 0;
260 lefcha 1.1 } else if (!strncasecmp(line + m[2].rm_so, "expunge", 7)) {
261     if (!strncasecmp(line + m[3].rm_so, "yes", 3))
262 lefcha 1.11 opts.expunge = 1;
263 lefcha 1.1 else
264 lefcha 1.11 opts.expunge = 0;
265 lefcha 1.1 } else if (!strncasecmp(line + m[2].rm_so, "header", 6)) {
266     if (!strncasecmp(line + m[3].rm_so, "yes", 3))
267 lefcha 1.11 opts.headers = 1;
268 lefcha 1.1 else
269 lefcha 1.11 opts.headers = 0;
270 lefcha 1.1 } else if (!strncasecmp(line + m[2].rm_so, "namespace", 9)) {
271     if (!strncasecmp(line + m[3].rm_so, "yes", 3))
272 lefcha 1.11 opts.namespace = 1;
273 lefcha 1.1 else
274 lefcha 1.11 opts.namespace = 0;
275 lefcha 1.7 } else if (!strncasecmp(line + m[2].rm_so, "peek", 4)) {
276     if (!strncasecmp(line + m[3].rm_so, "yes", 3))
277 lefcha 1.11 opts.peek = 1;
278 lefcha 1.7 else
279 lefcha 1.11 opts.peek = 0;
280 lefcha 1.1 } else if (!strncasecmp(line + m[2].rm_so, "subscribe", 9)) {
281     if (!strncasecmp(line + m[3].rm_so, "yes", 3))
282 lefcha 1.11 opts.subscribe = 1;
283 lefcha 1.1 else
284 lefcha 1.11 opts.subscribe = 0;
285 lefcha 1.1 } else if (!strncasecmp(line + m[2].rm_so, "timeout", 7)) {
286     errno = 0;
287 lefcha 1.11 opts.timeout = strtol(line + m[3].rm_so, NULL, 10);
288 lefcha 1.1 if (errno)
289 lefcha 1.11 opts.timeout = 0;
290 lefcha 1.1 } else if (!strncasecmp(line + m[2].rm_so, "daemon", 6) &&
291 lefcha 1.11 !opts.daemon) {
292 lefcha 1.1 errno = 0;
293 lefcha 1.11 opts.daemon = strtoul(line + m[3].rm_so, NULL, 10);
294 lefcha 1.1 if (errno)
295 lefcha 1.11 opts.daemon = 0;
296 lefcha 1.1 }
297     }
298    
299    
300     #ifdef ENCRYPTED_PASSWORDS
301     /*
302     * Open password file and call parse_passwords().
303     */
304     int
305     read_passwords(void)
306     {
307     FILE *fd;
308     char pwfile[PATH_MAX];
309    
310 lefcha 1.12 if (!(flags & FLAG_BLANKPASS))
311     return ERROR_PARSER;
312 lefcha 1.1
313     if (!passphr) {
314     passphr = (char *)smalloc(PASSPHRASE_LEN);
315     *passphr = '\0';
316     }
317 lefcha 1.12 snprintf(pwfile, PATH_MAX, "%s/%s", home, PATHNAME_PASSWORDS);
318 lefcha 1.9 debug("passwords file: '%s'\n", pwfile);
319 lefcha 1.1
320     if (!exists_file(pwfile))
321 lefcha 1.12 return ERROR_FILEOPEN;
322 lefcha 1.1
323     check_file_perms(pwfile, S_IRUSR | S_IWUSR);
324    
325     fd = fopen(pwfile, "r");
326     if (fd == NULL)
327 lefcha 1.12 fatal(ERROR_FILEOPEN, "opening passwords file %s; %s\n",
328 lefcha 1.1 pwfile, strerror(errno));
329    
330     parse_passwords(fd);
331    
332     fclose(fd);
333    
334     return 0;
335     }
336    
337    
338     /*
339 lefcha 1.15 * Parse unencrypted password file.
340 lefcha 1.1 */
341     int
342     parse_passwords(FILE * fd)
343     {
344     int t;
345     char *pe;
346 lefcha 1.12 char user[USER_LEN], serv[SERVER_LEN];
347 lefcha 1.1 unsigned char *buf;
348     char *c, *cp, *line;
349     regex_t creg;
350     regmatch_t match[4];
351     const char *reg;
352    
353     t = 3;
354     pe = NULL;
355     reg = "([[:alnum:].-]+) ([[:graph:]]+) ([[:graph:]]+)";
356    
357 lefcha 1.12 if (!(flags & FLAG_DAEMON)) {
358 lefcha 1.1 do {
359     fseek(fd, 0L, SEEK_SET);
360     printf("Enter master passphrase: ");
361     get_password(passphr, PASSPHRASE_LEN);
362     } while (decrypt_passwords(&buf, fd) && --t != 0);
363    
364     if (!t)
365     return ERROR_PASSPHRASE;
366     } else if (decrypt_passwords(&buf, fd))
367     fatal(ERROR_DECRYPT,
368     "failed decrypting passwords in daemon mode\n");
369    
370     c = cp = sstrdup(buf);
371    
372     regcomp(&creg, reg, REG_EXTENDED | REG_ICASE);
373    
374     line = strtok_r(c, "\n", &c);
375     while (line != NULL && !regexec(&creg, line, 4, match, 0)) {
376     user[0] = serv[0] = '\0';
377    
378     strncat(serv, line + match[1].rm_so,
379     min(match[1].rm_eo - match[1].rm_so, SERVER_LEN - 1));
380     strncat(user, line + match[2].rm_so,
381 lefcha 1.12 min(match[2].rm_eo - match[2].rm_so, USER_LEN - 1));
382 lefcha 1.1
383     if ((pe = (char *)find_password(user, serv)))
384     strncat(pe, line + match[3].rm_so,
385     min(match[3].rm_eo - match[3].rm_so,
386 lefcha 1.15 PASS_LEN - 1));
387 lefcha 1.1
388     line = strtok_r(NULL, "\n", &c);
389     }
390    
391     regfree(&creg);
392     sfree(cp);
393     sfree(buf);
394    
395     return 0;
396     }
397     #endif /* ENCRYPTED_PASSWORDS */

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26