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

Annotation of /imapfilter/file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.26.2.1 - (hide annotations)
Wed Dec 5 09:57:38 2001 UTC (22 years, 3 months ago) by lefcha
Branch: release-0_7-patches
Changes since 1.26: +21 -7 lines
File MIME type: text/plain
Timeout and namespace changes.

1 lefcha 1.1 #include <stdio.h>
2     #include <errno.h>
3 lefcha 1.5 #include <sys/types.h>
4 lefcha 1.1 #include <regex.h>
5     #include <string.h>
6     #include <stdlib.h>
7     #include <limits.h>
8 lefcha 1.10 #include <sys/stat.h>
9 lefcha 1.1
10     #include "config.h"
11     #include "imapfilter.h"
12 lefcha 1.13 #include "data.h"
13 lefcha 1.1
14    
15 lefcha 1.6 extern char logfile[PATH_MAX];
16 lefcha 1.11 extern unsigned int options;
17 lefcha 1.8
18 lefcha 1.26.2.1 long timeout = -1; /* Server non-response timeout in seconds. */
19    
20 lefcha 1.1
21     /*
22 lefcha 1.13 * Find the path to configuration file, open it and call parse_config().
23 lefcha 1.1 */
24     int read_config(char *cfg)
25     {
26     int r;
27     FILE *fp;
28     char *home = NULL;
29 lefcha 1.23 char *c = NULL;
30 lefcha 1.24
31 lefcha 1.1 if (!cfg) {
32 lefcha 1.23 cfg = c = (char *) xmalloc(PATH_MAX * sizeof(char));
33 lefcha 1.8
34 lefcha 1.1 home = getenv("HOME");
35    
36     snprintf(cfg, PATH_MAX, "%s/%s", home, ".imapfilterrc");
37     }
38     #ifdef DEBUG
39 lefcha 1.11 printf("debug: configuration file: '%s'\n", cfg);
40 lefcha 1.1 #endif
41 lefcha 1.13 #ifdef CHECK_PERMISSIONS
42     check_permissions(cfg);
43 lefcha 1.12 #endif
44 lefcha 1.1 fp = fopen(cfg, "r");
45    
46 lefcha 1.17 if (!fp)
47     fatal(ERROR_FILE_OPEN, "imapfilter: opening config file %s; %s\n",
48 lefcha 1.11 cfg, strerror(errno));
49 lefcha 1.1
50 lefcha 1.23 if (c)
51     free(c);
52    
53 lefcha 1.17 if ((r = parse_config(fp)))
54     fatal(ERROR_CONFIG_PARSE,
55     "imapfilter: parse error in config file at row %d\n", r);
56 lefcha 1.1
57     fclose(fp);
58    
59 lefcha 1.11 #ifdef DEBUG
60     printf("debug: options: %0#10x\n", options);
61     #endif
62    
63 lefcha 1.17 return 0;
64 lefcha 1.1 }
65    
66 lefcha 1.6
67 lefcha 1.13 #ifdef CHECK_PERMISSIONS
68 lefcha 1.1 /*
69 lefcha 1.13 * Check the permissions of the configuration file.
70 lefcha 1.10 */
71 lefcha 1.13 int check_permissions(char *cfg)
72 lefcha 1.10 {
73     struct stat fs;
74    
75     if (stat(cfg, &fs)) {
76 lefcha 1.11 error("imapfilter: getting file %s status; %s\n", cfg,
77     strerror(errno));
78 lefcha 1.17 return ERROR_TRIVIAL;
79 lefcha 1.10 }
80     if (!S_ISREG(fs.st_mode)) {
81 lefcha 1.11 error("imapfilter: file %s not a regular file\n", cfg);
82 lefcha 1.17 return ERROR_TRIVIAL;
83 lefcha 1.10 }
84     if ((fs.st_mode & 00777) != (S_IRUSR | S_IWUSR)) {
85 lefcha 1.17 error("imapfilter: warning: improper config file %s permissions\n"
86     "imapfilter: warning: file's mode should be 600 not %o\n",
87     cfg, fs.st_mode & 00777);
88     return ERROR_TRIVIAL;
89 lefcha 1.10 }
90     return 0;
91     }
92 lefcha 1.12 #endif
93 lefcha 1.10
94     /*
95 lefcha 1.13 * Parse configuration file.
96 lefcha 1.1 */
97     int parse_config(FILE * fp)
98     {
99 lefcha 1.17 int i, r = 0;
100 lefcha 1.1 unsigned int row = 0;
101     char line[LINE_MAX];
102 lefcha 1.26.2.1 regex_t creg[13];
103 lefcha 1.13 regmatch_t match[7];
104 lefcha 1.26.2.1 const char *reg[13] = {
105 lefcha 1.15 "^([[:blank:]]*\n|#.*\n)$",
106 lefcha 1.25 #ifndef SSL_TLS
107 lefcha 1.20 "^[[:blank:]]*ACCOUNT[[:blank:]]+([[:graph:]]*):([[:graph:]]*)@([[:alnum:].-]+)(:[[:digit:]]+)?[[:blank:]]*\n$",
108 lefcha 1.25 #else
109     "^[[:blank:]]*ACCOUNT[[:blank:]]+([[:graph:]]*):([[:graph:]]*)@([[:alnum:].-]+)(:[[:digit:]]+)?[[:blank:]]*([[:blank:]]SSL|[[:blank:]]SSL2|[[:blank:]]SSL3|[[:blank:]]TLS1)?[[:blank:]]*\n$",
110     #endif
111 lefcha 1.13 "^[[:blank:]]*FOLDER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+([[:graph:]]+)[[:blank:]]*\n$",
112     "^[[:blank:]]*FILTER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]*([[:blank:]]OR|[[:blank:]]AND)?[[:blank:]]*\n$",
113 lefcha 1.17 "^[[:blank:]]*ACTION[[:blank:]]+(DELETE|COPY[[:blank:]]+([[:graph:]]+)|MOVE[[:blank:]]+([[:graph:]]+)|LIST)[[:blank:]]*([[:graph:]]*)[[:blank:]]*\n$",
114     "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(ANSWERED|DELETED|DRAFT|FLAGGED|NEW|OLD|RECENT|SEEN|UNANSWERED|UNDELETED|UNDRAFT|UNFLAGGED|UNSEEN)[[:blank:]]*\n$",
115     "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(BCC|BODY|CC|FROM|SUBJECT|TEXT|TO)[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)[[:blank:]]*\n$",
116     "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(HEADER)[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)[[:blank:]]*\n$",
117 lefcha 1.26 "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(LARGER|SMALLER|OLDER|NEWER)[[:blank:]]+([[:digit:]]+)[[:blank:]]*\n$",
118 lefcha 1.13 "^[[:blank:]]*JOB[[:blank:]]+([[:alnum:],_-]+)[[:blank:]]+([[:alnum:],_-]+)[[:blank:]]*\n$",
119 lefcha 1.26 "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(LOGFILE)[[:blank:]]*=[[:blank:]]*([[:print:]]+)\n$",
120 lefcha 1.26.2.1 "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(HEADERS|NAMESPACE)[[:blank:]]*=[[:blank:]]*(YES|NO)[[:blank:]]*\n$",
121     "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(TIMEOUT)[[:blank:]]*=[[:blank:]]*(-?[[:digit:]]+)\n$"
122 lefcha 1.1 };
123    
124 lefcha 1.26.2.1 for (i = 0; i < 13; i++)
125 lefcha 1.14 regcomp(&creg[i], reg[i], REG_EXTENDED | REG_ICASE);
126 lefcha 1.1
127     while (fgets(line, LINE_MAX - 1, fp)) {
128     row++;
129 lefcha 1.15 if (!regexec(&creg[0], line, 0, match, 0))
130     continue;
131 lefcha 1.26 #ifndef SSL_TLS
132 lefcha 1.25 else if (!regexec(&creg[1], line, 5, match, 0))
133     #else
134 lefcha 1.15 else if (!regexec(&creg[1], line, 6, match, 0))
135 lefcha 1.25 #endif
136 lefcha 1.13 set_account(line, match);
137 lefcha 1.15 else if (!regexec(&creg[2], line, 3, match, 0))
138 lefcha 1.17 r = set_mboxgrp(line, match);
139 lefcha 1.15 else if (!regexec(&creg[3], line, 3, match, 0))
140 lefcha 1.17 r = set_filter(line, match);
141     else if (!regexec(&creg[4], line, 5, match, 0))
142     r = set_action(line, match);
143 lefcha 1.15 else if (!regexec(&creg[5], line, 7, match, 0) ||
144 lefcha 1.18 !regexec(&creg[6], line, 7, match, 0) ||
145     !regexec(&creg[7], line, 7, match, 0) ||
146     !regexec(&creg[8], line, 7, match, 0))
147 lefcha 1.17 r = set_mask(line, match);
148 lefcha 1.15 else if (!regexec(&creg[9], line, 3, match, 0))
149 lefcha 1.17 r = set_job(line, match);
150 lefcha 1.26 else if (!regexec(&creg[10], line, 4, match, 0) ||
151 lefcha 1.26.2.1 !regexec(&creg[11], line, 4, match, 0) ||
152     !regexec(&creg[12], line, 4, match, 0))
153 lefcha 1.15 set_options(line, match);
154 lefcha 1.17 else
155 lefcha 1.18 return row;
156    
157 lefcha 1.17 if (r == ERROR_CONFIG_PARSE)
158     return row;
159 lefcha 1.1 }
160 lefcha 1.3
161 lefcha 1.26.2.1 for (i = 0; i < 13; i++)
162 lefcha 1.12 regfree(&creg[i]);
163 lefcha 1.18
164 lefcha 1.15 destroy_data();
165 lefcha 1.6
166 lefcha 1.8 return 0;
167 lefcha 1.1 }
168    
169    
170     /*
171 lefcha 1.13 * Set other options found in config file.
172 lefcha 1.11 */
173     void set_options(char *line, regmatch_t * match)
174     {
175 lefcha 1.26 if (!strncasecmp(line + match[2].rm_so, "logfile", 7)) {
176 lefcha 1.19 if (!*logfile)
177 lefcha 1.26 strncat(logfile, line + match[3].rm_so,
178     min((match[3].rm_eo - match[3].rm_so), PATH_MAX - 1));
179 lefcha 1.26.2.1 } else if (!strncasecmp(line + match[2].rm_so, "header", 6)) {
180 lefcha 1.26 if (!strncasecmp(line + match[3].rm_so, "yes", 3))
181 lefcha 1.19 options |= OPTION_HEADERS;
182 lefcha 1.25 else
183     options &= ~(OPTION_HEADERS);
184 lefcha 1.26.2.1 } else if (!strncasecmp(line + match[2].rm_so, "namespace", 9)) {
185     if (!strncasecmp(line + match[3].rm_so, "yes", 3))
186     options |= OPTION_NAMESPACE;
187     else
188     options &= ~(OPTION_NAMESPACE);
189     } else if (!strncasecmp(line + match[2].rm_so, "timeout", 7)) {
190     errno = 0;
191     timeout = strtol(line + match[3].rm_so, NULL, 10);
192     if (errno)
193     timeout = 0;
194 lefcha 1.19 }
195 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26