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

Annotation of /imapfilter/parse.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (hide annotations)
Sat Feb 7 23:54:16 2004 UTC (20 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.7: +10 -7 lines
File MIME type: text/plain
Removed CHECK_PERMISSIONS compilation option and added variable to control permissions checking.

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 lefcha 1.8
56 lefcha 1.1 fd = fopen(cfg, "r");
57     if (fd == NULL)
58     fatal(ERROR_FILE_OPEN, "opening config file %s; %s\n", cfg,
59     strerror(errno));
60    
61     if (c != NULL)
62     xfree(c);
63    
64     if ((r = parse_config(fd)))
65     fatal(ERROR_CONFIG_PARSE,
66     "parse error in config file at row %d\n", r);
67    
68     fclose(fd);
69    
70 lefcha 1.8 check_file_perms(cfg, S_IRUSR | S_IWUSR);
71    
72 lefcha 1.1 #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 lefcha 1.8 "HEADERS|NAMESPACE|PEEK|PERMISSIONS|PROTOCOL|SUBSCRIBE)"
146     "[[:blank:]]*=[[:blank:]]*(YES|NO)[[:blank:]]*\n$",
147 lefcha 1.1
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 lefcha 1.7 } else if (!strncasecmp(line + m[2].rm_so, "peek", 4)) {
274     if (!strncasecmp(line + m[3].rm_so, "yes", 3))
275     options |= OPTION_PEEK;
276     else
277     options &= ~(OPTION_PEEK);
278 lefcha 1.8 } else if (!strncasecmp(line + m[2].rm_so, "permissions", 8)) {
279     if (!strncasecmp(line + m[3].rm_so, "yes", 3))
280     options |= OPTION_PERMISSIONS;
281     else
282     options &= ~(OPTION_PERMISSIONS);
283 lefcha 1.1 } else if (!strncasecmp(line + m[2].rm_so, "subscribe", 9)) {
284     if (!strncasecmp(line + m[3].rm_so, "yes", 3))
285     options |= OPTION_SUBSCRIBE;
286     else
287     options &= ~(OPTION_SUBSCRIBE);
288     } else if (!strncasecmp(line + m[2].rm_so, "timeout", 7)) {
289     errno = 0;
290     timeout = strtol(line + m[3].rm_so, NULL, 10);
291     if (errno)
292     timeout = 0;
293     } else if (!strncasecmp(line + m[2].rm_so, "daemon", 6) &&
294     !(options & OPTION_DAEMON_MODE)) {
295     options |= OPTION_DAEMON_MODE;
296     errno = 0;
297     interval = strtoul(line + m[3].rm_so, NULL, 10);
298     if (errno)
299     interval = 0;
300     }
301     }
302    
303    
304     #ifdef ENCRYPTED_PASSWORDS
305     /*
306     * Open password file and call parse_passwords().
307     */
308     int
309     read_passwords(void)
310     {
311     FILE *fd;
312     char pwfile[PATH_MAX];
313    
314     if (!(flags & FLAG_BLANK_PASSWORD))
315     return ERROR_CONFIG_PARSE;
316    
317     if (!passphr) {
318     passphr = (char *)smalloc(PASSPHRASE_LEN);
319     *passphr = '\0';
320     }
321     snprintf(pwfile, PATH_MAX, "%s/%s", home, PATHNAME_PASSWORD_FILE);
322     #ifdef DEBUG
323     fprintf(stderr, "debug: passwords file: '%s'\n", pwfile);
324     #endif
325    
326     if (!exists_file(pwfile))
327     return ERROR_FILE_OPEN;
328    
329     check_file_perms(pwfile, S_IRUSR | S_IWUSR);
330    
331     fd = fopen(pwfile, "r");
332     if (fd == NULL)
333     fatal(ERROR_FILE_OPEN, "opening passwords file %s; %s\n",
334     pwfile, strerror(errno));
335    
336     parse_passwords(fd);
337    
338     fclose(fd);
339    
340     return 0;
341     }
342    
343    
344     /*
345     * Parse unencrypted password file.
346     */
347     int
348     parse_passwords(FILE * fd)
349     {
350     int t;
351     char *pe;
352     char user[USERNAME_LEN], serv[SERVER_LEN];
353     unsigned char *buf;
354     char *c, *cp, *line;
355     regex_t creg;
356     regmatch_t match[4];
357     const char *reg;
358    
359     t = 3;
360     pe = NULL;
361     reg = "([[:alnum:].-]+) ([[:graph:]]+) ([[:graph:]]+)";
362    
363     if (!(flags & FLAG_DAEMON_MODE)) {
364     do {
365     fseek(fd, 0L, SEEK_SET);
366     printf("Enter master passphrase: ");
367     get_password(passphr, PASSPHRASE_LEN);
368     } while (decrypt_passwords(&buf, fd) && --t != 0);
369    
370     if (!t)
371     return ERROR_PASSPHRASE;
372     } else if (decrypt_passwords(&buf, fd))
373     fatal(ERROR_DECRYPT,
374     "failed decrypting passwords in daemon mode\n");
375    
376     c = cp = sstrdup(buf);
377    
378     regcomp(&creg, reg, REG_EXTENDED | REG_ICASE);
379    
380     line = strtok_r(c, "\n", &c);
381     while (line != NULL && !regexec(&creg, line, 4, match, 0)) {
382     user[0] = serv[0] = '\0';
383    
384     strncat(serv, line + match[1].rm_so,
385     min(match[1].rm_eo - match[1].rm_so, SERVER_LEN - 1));
386     strncat(user, line + match[2].rm_so,
387     min(match[2].rm_eo - match[2].rm_so, USERNAME_LEN - 1));
388    
389     if ((pe = (char *)find_password(user, serv)))
390     strncat(pe, line + match[3].rm_so,
391     min(match[3].rm_eo - match[3].rm_so,
392     PASSWORD_LEN - 1));
393    
394     line = strtok_r(NULL, "\n", &c);
395     }
396    
397     regfree(&creg);
398     sfree(cp);
399     sfree(buf);
400    
401     return 0;
402     }
403     #endif /* ENCRYPTED_PASSWORDS */

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26