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

Annotation of /imapfilter/parse.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations)
Fri Aug 8 12:29:43 2003 UTC (20 years, 7 months ago) by lefcha
Branch: MAIN
Changes since 1.5: +1 -0 lines
File MIME type: text/plain
Added header includes to compile on systems that don't conform to IEEE Std 1003.1-2001 (POSIX.1).

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26