1 |
lefcha |
1.1 |
#include <stdio.h> |
2 |
|
|
#include <errno.h> |
3 |
|
|
#include <regex.h> |
4 |
|
|
#include <string.h> |
5 |
|
|
#include <stdlib.h> |
6 |
|
|
#include <limits.h> |
7 |
|
|
|
8 |
|
|
#include "config.h" |
9 |
|
|
#include "imapfilter.h" |
10 |
|
|
#include "file.h" |
11 |
|
|
#include "log.h" |
12 |
|
|
|
13 |
|
|
|
14 |
|
|
account_data account; |
15 |
lefcha |
1.2 |
filter_entry **dfilters; /* Filters of DENY type. */ |
16 |
|
|
filter_entry **afilters; /* Filters of ALLOW type. */ |
17 |
|
|
int dfcnt = 0; |
18 |
|
|
int afcnt = 0; /* DENY and ALLOW type filters found |
19 |
|
|
in the configuration file */ |
20 |
lefcha |
1.1 |
|
21 |
|
|
|
22 |
|
|
/* |
23 |
|
|
* Finds path to configuration file, opens it and calls parse_config(). |
24 |
|
|
*/ |
25 |
|
|
int read_config(char *cfg) |
26 |
|
|
{ |
27 |
|
|
int r; |
28 |
|
|
FILE *fp; |
29 |
|
|
char *home = NULL; |
30 |
|
|
|
31 |
|
|
if (!cfg) { |
32 |
|
|
cfg = (char *) malloc(PATH_MAX * sizeof(char)); |
33 |
|
|
|
34 |
|
|
home = getenv("HOME"); |
35 |
|
|
|
36 |
|
|
snprintf(cfg, PATH_MAX, "%s/%s", home, ".imapfilterrc"); |
37 |
|
|
} |
38 |
|
|
#ifdef DEBUG |
39 |
|
|
printf("debug: configuration file: %s\n", cfg); |
40 |
|
|
#endif |
41 |
|
|
|
42 |
|
|
fp = fopen(cfg, "r"); |
43 |
|
|
|
44 |
|
|
if (!fp) { |
45 |
|
|
fprintf(stderr, "imapfilter: Could not open config file %s; %s\n", |
46 |
|
|
cfg, strerror(errno)); |
47 |
|
|
return FAILURE; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
account.port = 143; /* Default IMAP protocol port to connect. */ |
51 |
|
|
|
52 |
|
|
r = parse_config(fp); |
53 |
|
|
|
54 |
|
|
fclose(fp); |
55 |
|
|
|
56 |
|
|
return r; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
/* |
60 |
|
|
* Parses configuration file. |
61 |
|
|
*/ |
62 |
|
|
int parse_config(FILE * fp) |
63 |
|
|
{ |
64 |
|
|
int i; |
65 |
|
|
unsigned int row = 0; |
66 |
|
|
char line[LINE_MAX]; |
67 |
|
|
regex_t comreg[4]; |
68 |
lefcha |
1.2 |
regmatch_t match[4]; |
69 |
lefcha |
1.1 |
const char *reg[] = { |
70 |
|
|
"^[[:blank:]]*(SERVER|PORT|USERNAME|PASSWORD|LOGFILE)[[:blank:]]*=[[:blank:]]*([[:graph:]]*)[[:blank:]]*", |
71 |
lefcha |
1.2 |
"^[[:blank:]]*(DENY|ALLOW)[[:blank:]]*=[[:blank:]]*(FROM|CC|BCC|SUBJECT|TO):? ([[:print:]]*)", |
72 |
|
|
"^[[:blank:]]*(DENY|ALLOW)[[:blank:]]*=[[:blank:]]*([[:graph:]]+):? ([[:print:]]*)", |
73 |
lefcha |
1.1 |
"^[[:blank:]]*#{0,1}.*" |
74 |
|
|
}; |
75 |
|
|
|
76 |
|
|
for (i = 0; i < 4; i++) |
77 |
|
|
regcomp(&comreg[i], reg[i], REG_EXTENDED); |
78 |
|
|
|
79 |
|
|
while (fgets(line, LINE_MAX - 1, fp)) { |
80 |
|
|
row++; |
81 |
|
|
if (!regexec(&comreg[0], line, 3, match, 0)) { |
82 |
|
|
set_account(line, match); |
83 |
|
|
continue; |
84 |
lefcha |
1.2 |
} else if (!regexec(&comreg[1], line, 4, match, 0)) { |
85 |
lefcha |
1.1 |
standard_filter(line, match); |
86 |
|
|
continue; |
87 |
lefcha |
1.2 |
} else if (!regexec(&comreg[2], line, 4, match, 0)) { |
88 |
|
|
custom_filter(line, match); |
89 |
lefcha |
1.1 |
continue; |
90 |
|
|
} else if (!regexec(&comreg[3], line, 1, match, 0)) |
91 |
|
|
continue; |
92 |
|
|
else { |
93 |
|
|
fprintf(stderr, |
94 |
|
|
"imapfilter: parse error in config file at row %d\n", |
95 |
|
|
row); |
96 |
|
|
return FAILURE; |
97 |
|
|
} |
98 |
|
|
} |
99 |
|
|
#ifdef DEBUG |
100 |
|
|
printf("debug: account setting SERVER: '%s'\n" |
101 |
|
|
"debug: account setting PORT: %d\n" |
102 |
|
|
"debug: account setting USERNAME: '%s'\n" |
103 |
|
|
"debug: account setting PASSWORD: '%s'\n" |
104 |
|
|
"debug: account setting LOGFILE: '%s'\n", |
105 |
|
|
account.server, account.port, account.username, |
106 |
|
|
account.password, account.logfile); |
107 |
|
|
#endif |
108 |
|
|
return SUCCESS; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
|
112 |
|
|
/* |
113 |
|
|
* An account setting was found from parse_config(), changes the |
114 |
|
|
* apropriate variable. |
115 |
|
|
*/ |
116 |
|
|
void set_account(char *line, regmatch_t * match) |
117 |
|
|
{ |
118 |
|
|
int s; |
119 |
|
|
char port[6]; |
120 |
|
|
|
121 |
|
|
if (!strncmp(line + match[1].rm_so, "SERVER", 6)) { |
122 |
|
|
s = min((match[2].rm_eo - match[2].rm_so), SERVER_MAX - 1); |
123 |
|
|
strncpy(account.server, line + match[2].rm_so, s); |
124 |
|
|
account.server[s] = 0; |
125 |
|
|
} else if (!strncmp(line + match[1].rm_so, "PORT", 4)) { |
126 |
|
|
strncpy(port, line + match[2].rm_so, |
127 |
|
|
min((match[2].rm_eo - match[2].rm_so), sizeof(port))); |
128 |
|
|
account.port = strtoul(port, NULL, 0); |
129 |
|
|
} else if (!strncmp(line + match[1].rm_so, "USERNAME", 8)) { |
130 |
|
|
s = min((match[2].rm_eo - match[2].rm_so), USERNAME_MAX - 1); |
131 |
|
|
strncpy(account.username, line + match[2].rm_so, s); |
132 |
|
|
account.username[s] = 0; |
133 |
|
|
} else if (!strncmp(line + match[1].rm_so, "PASSWORD", 8)) { |
134 |
|
|
s = min((match[2].rm_eo - match[2].rm_so), PASSWORD_MAX - 1); |
135 |
|
|
strncpy(account.password, line + match[2].rm_so, s); |
136 |
|
|
account.password[s] = 0; |
137 |
|
|
} else if (!strncmp(line + match[1].rm_so, "LOGFILE", 7)) { |
138 |
|
|
s = min((match[2].rm_eo - match[2].rm_so), PATH_MAX - 1); |
139 |
|
|
strncpy(account.logfile, line + match[2].rm_so, s); |
140 |
|
|
account.logfile[s] = 0; |
141 |
|
|
} |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
|
145 |
|
|
/* |
146 |
|
|
* A filter entry was found from parse_config() proccesses it and saves it. |
147 |
|
|
*/ |
148 |
|
|
void set_filters(char *line, regmatch_t * match, int expl) |
149 |
|
|
{ |
150 |
|
|
int s; |
151 |
lefcha |
1.2 |
int *fcnt; |
152 |
|
|
filter_entry ***filters; |
153 |
|
|
|
154 |
|
|
if (!(strncmp(line + match[1].rm_so, "DENY", 4))) { |
155 |
|
|
#ifdef DEBUG |
156 |
|
|
printf("debug: filter entry: DENY"); |
157 |
|
|
#endif |
158 |
|
|
fcnt = &dfcnt; |
159 |
|
|
filters = &dfilters; |
160 |
|
|
} else { |
161 |
|
|
#ifdef DEBUG |
162 |
|
|
printf("debug: filter entry: ALLOW"); |
163 |
|
|
#endif |
164 |
|
|
fcnt = &afcnt; |
165 |
|
|
filters = &afilters; |
166 |
|
|
} |
167 |
lefcha |
1.1 |
|
168 |
|
|
/* Every FILTER_ENTRIES_MAX, space for more filters is realloc() -ed. */ |
169 |
lefcha |
1.2 |
if (!*fcnt) |
170 |
|
|
*filters = |
171 |
lefcha |
1.1 |
(filter_entry **) malloc(FILTER_ENTRIES_MAX * |
172 |
lefcha |
1.2 |
sizeof(filter_entry *)); |
173 |
|
|
else if (!(*fcnt % FILTER_ENTRIES_MAX)) |
174 |
|
|
*filters = (filter_entry **) realloc(*filters, |
175 |
|
|
(((*fcnt / |
176 |
|
|
FILTER_ENTRIES_MAX) + |
177 |
|
|
1) * FILTER_ENTRIES_MAX) * |
178 |
|
|
sizeof(filter_entry *)); |
179 |
|
|
|
180 |
|
|
(*filters)[*fcnt] = (filter_entry *) malloc(sizeof(filter_entry)); |
181 |
|
|
|
182 |
|
|
(*filters)[*fcnt]->custom = expl; |
183 |
|
|
|
184 |
|
|
s = min((match[2].rm_eo - match[2].rm_so), FIELD_NAME_MAX - 1); |
185 |
|
|
strncpy((*filters)[*fcnt]->name, line + match[2].rm_so, s); |
186 |
|
|
(*filters)[*fcnt]->name[s] = 0; |
187 |
|
|
|
188 |
|
|
s = min((match[3].rm_eo - match[3].rm_so), FIELD_BODY_MAX - 1); |
189 |
|
|
strncpy((*filters)[*fcnt]->body, line + match[3].rm_so, s); |
190 |
|
|
(*filters)[*fcnt]->body[s] = 0; |
191 |
lefcha |
1.1 |
|
192 |
|
|
#ifdef DEBUG |
193 |
lefcha |
1.2 |
printf(" '%s': '%s'\n", (*filters)[*fcnt]->name, |
194 |
|
|
(*filters)[*fcnt]->body); |
195 |
lefcha |
1.1 |
#endif |
196 |
|
|
|
197 |
lefcha |
1.2 |
(*fcnt)++; |
198 |
lefcha |
1.1 |
} |