forked from pi-hole/FTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex.c
305 lines (256 loc) · 7.52 KB
/
regex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* Pi-hole: A black hole for Internet advertisements
* (c) 2017 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* FTL Engine
* Regular Expressions
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
#include "FTL.h"
#include <regex.h>
static int num_regex;
static regex_t *regex = NULL;
static bool *regexconfigured = NULL;
static char **regexbuffer = NULL;
static whitelistStruct whitelist = { NULL, 0 };
static void log_regex_error(const char *where, int errcode, int index)
{
// Regex failed for some reason (probably user syntax error)
// Get error string and log it
size_t length = regerror(errcode, ®ex[index], NULL, 0);
char *buffer = calloc(length,sizeof(char));
(void) regerror (errcode, ®ex[index], buffer, length);
logg("ERROR %s regex on line %i: %s (%i)", where, index+1, buffer, errcode);
free(buffer);
}
static bool init_regex(const char *regexin, int index)
{
// compile regular expressions into data structures that
// can be used with regexec to match against a string
int errcode = regcomp(®ex[index], regexin, REG_EXTENDED);
if(errcode != 0)
{
log_regex_error("compiling", errcode, index);
return false;
}
// Store compiled regex string in buffer if in regex debug mode
if(config.debug & DEBUG_REGEX)
{
regexbuffer[index] = strdup(regexin);
}
return true;
}
bool __attribute__((pure)) in_whitelist(char *domain)
{
bool found = false;
for(int i=0; i < whitelist.count; i++)
{
// strcasecmp() compares two strings ignoring case
if(strcasecmp(whitelist.domains[i], domain) == 0)
{
found = true;
break;
}
}
return found;
}
static void free_whitelist_domains(void)
{
for(int i=0; i < whitelist.count; i++)
free(whitelist.domains[i]);
whitelist.count = 0;
// Free whitelist domains array only allocated
if(whitelist.domains != NULL)
{
free(whitelist.domains);
whitelist.domains = NULL;
}
}
bool match_regex(char *input)
{
int index;
bool matched = false;
// Start matching timer
timer_start(REGEX_TIMER);
for(index = 0; index < num_regex; index++)
{
// Only check regex which have been successfully compiled
if(!regexconfigured[index])
continue;
// Try to match the compiled regular expression against input
int errcode = regexec(®ex[index], input, 0, NULL, 0);
if (errcode == 0)
{
// Match, return true
matched = true;
// Print match message when in regex debug mode
if(config.debug & DEBUG_REGEX)
logg("Regex in line %i \"%s\" matches \"%s\"", index+1, regexbuffer[index], input);
break;
}
else if (errcode != REG_NOMATCH)
{
// Error, return false afterwards
log_regex_error("matching", errcode, index);
break;
}
}
double elapsed = timer_elapsed_msec(REGEX_TIMER);
// Only log evaluation times if they are longer than normal
if(elapsed > 10.0)
logg("WARN: Regex evaluation took %.3f msec", elapsed);
// No match, no error, return false
return matched;
}
void free_regex(void)
{
// Return early if we don't use any regex
if(regex == NULL)
return;
// Disable blocking regex checking and free regex datastructure
for(int index = 0; index < num_regex; index++)
{
if(regexconfigured[index])
{
regfree(®ex[index]);
// Also free buffered regex strings if in regex debug mode
if(config.debug & DEBUG_REGEX)
{
free(regexbuffer[index]);
regexbuffer[index] = NULL;
}
}
}
// Free array with regex datastructure
free(regex);
regex = NULL;
free(regexconfigured);
regexconfigured = NULL;
// Reset counter for number of regex
num_regex = 0;
// Must reevaluate regex filters after having reread the regex filter
// We reset all regex status to unknown to have them being reevaluated
if(counters->domains > 0)
validate_access("domains", counters->domains-1, false, __LINE__, __FUNCTION__, __FILE__);
for(int i=0; i < counters->domains; i++)
{
domains[i].regexmatch = REGEX_UNKNOWN;
}
// Also free array of whitelisted domains
free_whitelist_domains();
}
static void read_whitelist_from_file(void)
{
FILE *fp;
char *buffer = NULL;
size_t size = 0;
// Get number of lines in the whitelist file
whitelist.count = countlines(files.whitelist);
if(whitelist.count < 0)
{
logg("INFO: No whitelist file found");
return;
}
if((fp = fopen(files.whitelist, "r")) == NULL) {
logg("WARN: Cannot access whitelist (%s)",files.whitelist);
return;
}
// Allocate memory for array of whitelisted domains
whitelist.domains = calloc(whitelist.count, sizeof(char*));
// Search through file
// getline reads a string from the specified file up to either a
// newline character or EOF
for(int i=0; getline(&buffer, &size, fp) != -1; i++)
{
// Test if file has changed since we counted the lines therein (unlikely
// but not impossible). If so, read only as far as we have reserved memory
if(i >= whitelist.count)
break;
// Strip potential newline character at the end of line we just read
if(buffer[strlen(buffer)-1] == '\n')
buffer[strlen(buffer)-1] = '\0';
// Copy this whitelisted domain into memory
whitelist.domains[i] = strdup(buffer);
}
// Free allocated memory
if(buffer != NULL)
{
free(buffer);
buffer = NULL;
}
// Close the file
fclose(fp);
}
void read_regex_from_file(void)
{
FILE *fp;
char *buffer = NULL;
size_t size = 0;
int errors = 0, skipped = 0;
// Start timer for regex compilation analysis
timer_start(REGEX_TIMER);
// Get number of lines in the regex file
num_regex = countlines(files.regexlist);
if(num_regex < 0)
{
logg("INFO: No Regex file found");
return;
}
if((fp = fopen(files.regexlist, "r")) == NULL) {
logg("WARN: Cannot access Regex file");
return;
}
// Allocate memory for regex
regex = calloc(num_regex, sizeof(regex_t));
regexconfigured = calloc(num_regex, sizeof(bool));
// Buffer strings if in regex debug mode
if(config.debug & DEBUG_REGEX)
regexbuffer = calloc(num_regex, sizeof(char*));
// Search through file
// getline reads a string from the specified file up to either a
// newline character or EOF
for(int i=0; getline(&buffer, &size, fp) != -1; i++)
{
// Test if file has changed since we counted the lines therein (unlikely
// but not impossible). If so, read only as far as we have reserved memory
if(i >= num_regex)
break;
// Strip potential newline character at the end of line we just read
if(buffer[strlen(buffer)-1] == '\n')
buffer[strlen(buffer)-1] = '\0';
// Skip this entry if empty: an empty regex filter would match
// anything anywhere and hence match (and block) all incoming domains.
// A user can still achieve this with a filter such as ".*", however
// empty lines in regex.list are probably not expected to have such an
// effect and would immediately lead to "blocking the entire Internet"
if(strlen(buffer) < 1)
{
regexconfigured[i] = false;
logg("Skipping empty regex filter on line %i", i+1);
skipped++;
continue;
}
// Skip this entry if it is commented out
if(buffer[0] == '#')
{
regexconfigured[i] = false;
logg("Skipping commented out regex filter on line %i", i+1);
skipped++;
continue;
}
// Compile this regex
regexconfigured[i] = init_regex(buffer, i);
}
// Free allocated memory
if(buffer != NULL)
{
free(buffer);
buffer = NULL;
}
// Close the file
fclose(fp);
// Read whitelisted domains from file
read_whitelist_from_file();
logg("Compiled %i Regex filters and %i whitelisted domains in %.1f msec (%i errors)", (num_regex-skipped), whitelist.count > 0 ? whitelist.count : 0, timer_elapsed_msec(REGEX_TIMER), errors);
}