1 |
#include <stdio.h> |
2 |
#include <errno.h> |
3 |
#include <sys/types.h> |
4 |
#include <regex.h> |
5 |
#include <string.h> |
6 |
#include <stdlib.h> |
7 |
#include <limits.h> |
8 |
|
9 |
#include "config.h" |
10 |
#include "imapfilter.h" |
11 |
#include "file.h" |
12 |
#include "log.h" |
13 |
|
14 |
|
15 |
extern char logfile[PATH_MAX]; |
16 |
account_data **accounts; |
17 |
int acccnt = -1; /* Accounts declared. */ |
18 |
filter_entry **dfilters; /* Filters of DENY type. */ |
19 |
filter_entry **afilters; /* Filters of ALLOW type. */ |
20 |
int dfilcnt = 0; |
21 |
int afilcnt = 0; /* DENY and ALLOW type filters found |
22 |
in the configuration file. */ |
23 |
unsigned int dlimit = 0; |
24 |
unsigned int alimit = 0; /* DENY and ALLOW message size limits. */ |
25 |
|
26 |
|
27 |
/* |
28 |
* Finds path to configuration file, opens it and calls parse_config(). |
29 |
*/ |
30 |
int read_config(char *cfg) |
31 |
{ |
32 |
int r; |
33 |
FILE *fp; |
34 |
char *home = NULL; |
35 |
|
36 |
if (!cfg) { |
37 |
cfg = (char *) malloc(PATH_MAX * sizeof(char)); |
38 |
|
39 |
home = getenv("HOME"); |
40 |
|
41 |
snprintf(cfg, PATH_MAX, "%s/%s", home, ".imapfilterrc"); |
42 |
} |
43 |
#ifdef DEBUG |
44 |
printf("debug: configuration file: %s\n", cfg); |
45 |
#endif |
46 |
|
47 |
fp = fopen(cfg, "r"); |
48 |
|
49 |
if (!fp) { |
50 |
fprintf(stderr, "imapfilter: Could not open config file %s; %s\n", |
51 |
cfg, strerror(errno)); |
52 |
return FAILURE; |
53 |
} |
54 |
|
55 |
r = parse_config(fp); |
56 |
|
57 |
fclose(fp); |
58 |
|
59 |
return r; |
60 |
} |
61 |
|
62 |
|
63 |
/* |
64 |
* Parses configuration file. |
65 |
*/ |
66 |
int parse_config(FILE * fp) |
67 |
{ |
68 |
int i; |
69 |
unsigned int row = 0; |
70 |
char line[LINE_MAX]; |
71 |
regex_t comreg[5]; |
72 |
regmatch_t match[4]; |
73 |
const char *reg[5] = { |
74 |
"^[[:blank:]]*(SERVER|PORT|USERNAME|PASSWORD|LOGFILE)[[:blank:]]*=[[:blank:]]*([[:graph:]]*)[[:blank:]]*", |
75 |
"^[[:blank:]]*(DENY|ALLOW)[[:blank:]]*=[[:blank:]]*(FROM|CC|BCC|SUBJECT|TO):? ([[:print:]]*)", |
76 |
"^[[:blank:]]*(DENY|ALLOW)[[:blank:]]*=[[:blank:]]*([[:graph:]]+):? ([[:print:]]*)", |
77 |
"^[[:blank:]]*(DENY_LIMIT|ALLOW_LIMIT)[[:blank:]]*=[[:blank:]]*([[:digit:]]*)[[:blank:]]*", |
78 |
"^[[:blank:]]*#{0,1}.*" |
79 |
}; |
80 |
|
81 |
for (i = 0; i < 5; i++) |
82 |
regcomp(&comreg[i], reg[i], REG_EXTENDED); |
83 |
|
84 |
while (fgets(line, LINE_MAX - 1, fp)) { |
85 |
row++; |
86 |
if (!regexec(&comreg[0], line, 3, match, 0)) { |
87 |
set_account(line, match); |
88 |
continue; |
89 |
} else if (!regexec(&comreg[1], line, 4, match, 0)) { |
90 |
standard_filter(line, match); |
91 |
continue; |
92 |
} else if (!regexec(&comreg[2], line, 4, match, 0)) { |
93 |
custom_filter(line, match); |
94 |
continue; |
95 |
} else if (!regexec(&comreg[3], line, 3, match, 0)) { |
96 |
set_limits(line, match); |
97 |
continue; |
98 |
} else if (!regexec(&comreg[4], line, 1, match, 0)) |
99 |
continue; |
100 |
else { |
101 |
fprintf(stderr, |
102 |
"imapfilter: parse error in config file at row %d\n", |
103 |
row); |
104 |
return FAILURE; |
105 |
} |
106 |
} |
107 |
|
108 |
/* Fail if no accounts were defined. */ |
109 |
if (acccnt == -1) { |
110 |
fprintf(stderr, |
111 |
"imapfilter: no accounts defined in config file\n"); |
112 |
return FAILURE; |
113 |
} |
114 |
|
115 |
/* Fail if no fitlers were defined. */ |
116 |
if (!dfilcnt && !afilcnt) { |
117 |
fprintf(stderr, "imapfilter: no filters defined in config file\n"); |
118 |
return FAILURE; |
119 |
} |
120 |
|
121 |
return SUCCESS; |
122 |
} |
123 |
|
124 |
|
125 |
/* |
126 |
* Allocate memory for accounts and NULL terminate account settings. |
127 |
*/ |
128 |
void prepare_account(void) |
129 |
{ |
130 |
acccnt++; |
131 |
|
132 |
if (!acccnt) |
133 |
accounts = (account_data **) malloc(sizeof(account_data *)); |
134 |
else |
135 |
accounts = (account_data **) realloc(accounts, (acccnt + 1) * |
136 |
sizeof(account_data *)); |
137 |
|
138 |
accounts[acccnt] = (account_data *) malloc(sizeof(account_data)); |
139 |
|
140 |
accounts[acccnt]->server[0] = 0; |
141 |
accounts[acccnt]->port = 143; |
142 |
accounts[acccnt]->username[0] = 0; |
143 |
accounts[acccnt]->password[0] = 0; |
144 |
} |
145 |
|
146 |
|
147 |
/* |
148 |
* An account setting was found from parse_config(), changes the |
149 |
* apropriate variable. |
150 |
*/ |
151 |
void set_account(char *line, regmatch_t * match) |
152 |
{ |
153 |
int s; |
154 |
char port[6]; |
155 |
|
156 |
if (!strncmp(line + match[1].rm_so, "SERVER", 6)) { |
157 |
prepare_account(); |
158 |
s = min((match[2].rm_eo - match[2].rm_so), SERVER_MAX - 1); |
159 |
strncpy(accounts[acccnt]->server, line + match[2].rm_so, s); |
160 |
accounts[acccnt]->server[s] = 0; |
161 |
#ifdef DEBUG |
162 |
printf("debug: account setting SERVER: '%s'\n", accounts[acccnt]->server); |
163 |
#endif |
164 |
} else if (acccnt >= 0) { |
165 |
if (!strncmp(line + match[1].rm_so, "PORT", 4)) { |
166 |
s = min((match[2].rm_eo - match[2].rm_so), sizeof(port)); |
167 |
strncpy(port, line + match[2].rm_so, s); |
168 |
port[s] = 0; |
169 |
accounts[acccnt]->port = strtoul(port, NULL, 0); |
170 |
#ifdef DEBUG |
171 |
printf("debug: account setting PORT: '%d'\n", accounts[acccnt]->port); |
172 |
#endif |
173 |
} else if (!strncmp(line + match[1].rm_so, "USERNAME", 8)) { |
174 |
s = min((match[2].rm_eo - match[2].rm_so), USERNAME_MAX - 1); |
175 |
strncpy(accounts[acccnt]->username, line + match[2].rm_so, s); |
176 |
accounts[acccnt]->username[s] = 0; |
177 |
#ifdef DEBUG |
178 |
printf("debug: account setting USERNAME: '%s'\n", accounts[acccnt]->username); |
179 |
#endif |
180 |
} else if (!strncmp(line + match[1].rm_so, "PASSWORD", 8)) { |
181 |
s = min((match[2].rm_eo - match[2].rm_so), PASSWORD_MAX - 1); |
182 |
strncpy(accounts[acccnt]->password, line + match[2].rm_so, s); |
183 |
accounts[acccnt]->password[s] = 0; |
184 |
#ifdef DEBUG |
185 |
printf("debug: account setting PASSWORD: '%s'\n", accounts[acccnt]->password); |
186 |
#endif |
187 |
} else if (!strncmp(line + match[1].rm_so, "LOGFILE", 7)) { |
188 |
s = min((match[2].rm_eo - match[2].rm_so), PATH_MAX - 1); |
189 |
strncpy(logfile, line + match[2].rm_so, s); |
190 |
logfile[s] = 0; |
191 |
} |
192 |
} |
193 |
} |
194 |
|
195 |
|
196 |
/* |
197 |
* A filter entry was found from parse_config() processes it and saves it. |
198 |
*/ |
199 |
void set_filters(char *line, regmatch_t * match, int expl) |
200 |
{ |
201 |
int s; |
202 |
int *fcnt; |
203 |
filter_entry ***filters; |
204 |
|
205 |
if (!(strncmp(line + match[1].rm_so, "DENY", 4))) { |
206 |
#ifdef DEBUG |
207 |
printf("debug: filter entry: DENY"); |
208 |
#endif |
209 |
fcnt = &dfilcnt; |
210 |
filters = &dfilters; |
211 |
} else { |
212 |
#ifdef DEBUG |
213 |
printf("debug: filter entry: ALLOW"); |
214 |
#endif |
215 |
fcnt = &afilcnt; |
216 |
filters = &afilters; |
217 |
} |
218 |
|
219 |
/* Every FILTER_ENTRIES_MAX, space for more filters is realloc() -ed. */ |
220 |
if (!*fcnt) |
221 |
*filters = |
222 |
(filter_entry **) malloc(FILTER_ENTRIES_MAX * |
223 |
sizeof(filter_entry *)); |
224 |
else if (!(*fcnt % FILTER_ENTRIES_MAX)) |
225 |
*filters = (filter_entry **) realloc(*filters, |
226 |
(((*fcnt / |
227 |
FILTER_ENTRIES_MAX) + |
228 |
1) * FILTER_ENTRIES_MAX) * |
229 |
sizeof(filter_entry *)); |
230 |
|
231 |
(*filters)[*fcnt] = (filter_entry *) malloc(sizeof(filter_entry)); |
232 |
|
233 |
(*filters)[*fcnt]->custom = expl; |
234 |
|
235 |
s = min((match[2].rm_eo - match[2].rm_so), FIELD_NAME_MAX - 1); |
236 |
strncpy((*filters)[*fcnt]->name, line + match[2].rm_so, s); |
237 |
(*filters)[*fcnt]->name[s] = 0; |
238 |
|
239 |
s = min((match[3].rm_eo - match[3].rm_so), FIELD_BODY_MAX - 1); |
240 |
strncpy((*filters)[*fcnt]->body, line + match[3].rm_so, s); |
241 |
(*filters)[*fcnt]->body[s] = 0; |
242 |
|
243 |
#ifdef DEBUG |
244 |
printf(" '%s': '%s'\n", (*filters)[*fcnt]->name, |
245 |
(*filters)[*fcnt]->body); |
246 |
#endif |
247 |
|
248 |
(*fcnt)++; |
249 |
} |
250 |
|
251 |
|
252 |
/* |
253 |
* Sets the DENY and ALLOW limits from the values found in config file. |
254 |
*/ |
255 |
void set_limits(char *line, regmatch_t * match) |
256 |
{ |
257 |
int s; |
258 |
char limit[10]; |
259 |
|
260 |
s = min((match[2].rm_eo - match[2].rm_so), sizeof(limit)); |
261 |
strncpy(limit, line + match[2].rm_so, s); |
262 |
limit[s] = 0; |
263 |
|
264 |
if (!strncmp(line + match[1].rm_so, "DENY_LIMIT", 10)) { |
265 |
dlimit = strtoul(limit, NULL, 0); |
266 |
} else { |
267 |
alimit = strtoul(limit, NULL, 0); |
268 |
} |
269 |
} |