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