Commit | Line | Data |
---|---|---|
8523533c TK |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
80fea873 JH |
5 | /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-2015 |
6 | * License: GPL | |
7 | * Copyright (c) The Exim Maintainers 2016 | |
8 | */ | |
8523533c TK |
9 | |
10 | /* Code for matching regular expressions against headers and body. | |
11 | Called from acl.c. */ | |
12 | ||
13 | #include "exim.h" | |
14 | #ifdef WITH_CONTENT_SCAN | |
15 | #include <unistd.h> | |
16 | #include <sys/mman.h> | |
17 | ||
18 | /* Structure to hold a list of Regular expressions */ | |
19 | typedef struct pcre_list { | |
20 | pcre *re; | |
21 | uschar *pcre_text; | |
22 | struct pcre_list *next; | |
23 | } pcre_list; | |
24 | ||
25 | uschar regex_match_string_buffer[1024]; | |
26 | ||
27 | extern FILE *mime_stream; | |
28 | extern uschar *mime_current_boundary; | |
29 | ||
f38917cc JH |
30 | static pcre_list * |
31 | compile(const uschar * list) | |
55414b25 | 32 | { |
10a831a3 JH |
33 | int sep = 0; |
34 | uschar *regex_string; | |
35 | const char *pcre_error; | |
36 | int pcre_erroffset; | |
37 | pcre_list *re_list_head = NULL; | |
38 | pcre_list *ri; | |
39 | ||
40 | /* precompile our regexes */ | |
41 | while ((regex_string = string_nextinlist(&list, &sep, NULL, 0))) | |
42 | if (strcmpic(regex_string, US"false") != 0 && Ustrcmp(regex_string, "0") != 0) | |
43 | { | |
f38917cc JH |
44 | pcre *re; |
45 | ||
f38917cc JH |
46 | /* compile our regular expression */ |
47 | if (!(re = pcre_compile( CS regex_string, | |
10a831a3 JH |
48 | 0, &pcre_error, &pcre_erroffset, NULL ))) |
49 | { | |
f38917cc | 50 | log_write(0, LOG_MAIN, |
10a831a3 | 51 | "regex acl condition warning - error in regex '%s': %s at offset %d, skipped.", |
f38917cc JH |
52 | regex_string, pcre_error, pcre_erroffset); |
53 | continue; | |
10a831a3 | 54 | } |
f38917cc JH |
55 | |
56 | ri = store_get(sizeof(pcre_list)); | |
57 | ri->re = re; | |
10a831a3 | 58 | ri->pcre_text = regex_string; |
f38917cc JH |
59 | ri->next = re_list_head; |
60 | re_list_head = ri; | |
10a831a3 JH |
61 | } |
62 | return re_list_head; | |
f38917cc JH |
63 | } |
64 | ||
65 | static int | |
66 | matcher(pcre_list * re_list_head, uschar * linebuffer, int len) | |
67 | { | |
10a831a3 JH |
68 | pcre_list * ri; |
69 | ||
70 | for(ri = re_list_head; ri; ri = ri->next) | |
71 | { | |
72 | int ovec[3*(REGEX_VARS+1)]; | |
73 | int n, nn; | |
f38917cc | 74 | |
10a831a3 JH |
75 | /* try matcher on the line */ |
76 | n = pcre_exec(ri->re, NULL, CS linebuffer, len, 0, 0, ovec, nelem(ovec)); | |
77 | if (n > 0) | |
f38917cc | 78 | { |
10a831a3 JH |
79 | Ustrncpy(regex_match_string_buffer, ri->pcre_text, |
80 | sizeof(regex_match_string_buffer)-1); | |
81 | regex_match_string = regex_match_string_buffer; | |
f38917cc | 82 | |
10a831a3 JH |
83 | for (nn = 1; nn < n; nn++) |
84 | regex_vars[nn-1] = | |
85 | string_copyn(linebuffer + ovec[nn*2], ovec[nn*2+1] - ovec[nn*2]); | |
f38917cc | 86 | |
10a831a3 | 87 | return OK; |
f38917cc | 88 | } |
10a831a3 JH |
89 | } |
90 | return FAIL; | |
f38917cc JH |
91 | } |
92 | ||
93 | int | |
94 | regex(const uschar **listptr) | |
95 | { | |
10a831a3 JH |
96 | unsigned long mbox_size; |
97 | FILE *mbox_file; | |
98 | pcre_list *re_list_head; | |
99 | uschar *linebuffer; | |
100 | long f_pos = 0; | |
101 | int ret = FAIL; | |
102 | ||
103 | /* reset expansion variable */ | |
104 | regex_match_string = NULL; | |
105 | ||
106 | if (!mime_stream) /* We are in the DATA ACL */ | |
107 | { | |
108 | if (!(mbox_file = spool_mbox(&mbox_size, NULL))) | |
109 | { /* error while spooling */ | |
110 | log_write(0, LOG_MAIN|LOG_PANIC, | |
111 | "regex acl condition: error while creating mbox spool file"); | |
112 | return DEFER; | |
f38917cc | 113 | } |
8523533c | 114 | } |
10a831a3 JH |
115 | else |
116 | { | |
cb570b5e JH |
117 | if ((f_pos = ftell(mime_stream)) < 0) |
118 | { | |
119 | log_write(0, LOG_MAIN|LOG_PANIC, | |
120 | "regex acl condition: mime_stream: %s", strerror(errno)); | |
121 | return DEFER; | |
122 | } | |
10a831a3 | 123 | mbox_file = mime_stream; |
f38917cc | 124 | } |
8e669ac1 | 125 | |
10a831a3 JH |
126 | /* precompile our regexes */ |
127 | if (!(re_list_head = compile(*listptr))) | |
128 | return FAIL; /* no regexes -> nothing to do */ | |
129 | ||
130 | /* match each line against all regexes */ | |
131 | linebuffer = store_get(32767); | |
132 | while (fgets(CS linebuffer, 32767, mbox_file)) | |
133 | { | |
134 | if ( mime_stream && mime_current_boundary /* check boundary */ | |
135 | && Ustrncmp(linebuffer, "--", 2) == 0 | |
136 | && Ustrncmp((linebuffer+2), mime_current_boundary, | |
137 | Ustrlen(mime_current_boundary)) == 0) | |
138 | break; /* found boundary */ | |
139 | ||
140 | if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK) | |
141 | goto done; | |
f38917cc | 142 | } |
10a831a3 | 143 | /* no matches ... */ |
f38917cc JH |
144 | |
145 | done: | |
10a831a3 JH |
146 | if (!mime_stream) |
147 | (void)fclose(mbox_file); | |
148 | else | |
149 | { | |
150 | clearerr(mime_stream); | |
4dc2379a JH |
151 | if (fseek(mime_stream, f_pos, SEEK_SET) == -1) |
152 | { | |
153 | log_write(0, LOG_MAIN|LOG_PANIC, | |
154 | "regex acl condition: mime_stream: %s", strerror(errno)); | |
155 | clearerr(mime_stream); | |
156 | } | |
10a831a3 JH |
157 | } |
158 | ||
159 | return ret; | |
8523533c TK |
160 | } |
161 | ||
162 | ||
55414b25 JH |
163 | int |
164 | mime_regex(const uschar **listptr) | |
165 | { | |
10a831a3 JH |
166 | pcre_list *re_list_head = NULL; |
167 | FILE *f; | |
168 | uschar *mime_subject = NULL; | |
169 | int mime_subject_len = 0; | |
170 | int ret; | |
171 | ||
172 | /* reset expansion variable */ | |
173 | regex_match_string = NULL; | |
174 | ||
175 | /* precompile our regexes */ | |
176 | if (!(re_list_head = compile(*listptr))) | |
177 | return FAIL; /* no regexes -> nothing to do */ | |
178 | ||
179 | /* check if the file is already decoded */ | |
180 | if (!mime_decoded_filename) | |
181 | { /* no, decode it first */ | |
182 | const uschar *empty = US""; | |
183 | mime_decode(&empty); | |
184 | if (!mime_decoded_filename) | |
185 | { /* decoding failed */ | |
186 | log_write(0, LOG_MAIN, | |
187 | "mime_regex acl condition warning - could not decode MIME part to file"); | |
188 | return DEFER; | |
f38917cc JH |
189 | } |
190 | } | |
8523533c | 191 | |
10a831a3 JH |
192 | /* open file */ |
193 | if (!(f = fopen(CS mime_decoded_filename, "rb"))) | |
194 | { | |
195 | log_write(0, LOG_MAIN, | |
196 | "mime_regex acl condition warning - can't open '%s' for reading", | |
197 | mime_decoded_filename); | |
198 | return DEFER; | |
f38917cc | 199 | } |
8e669ac1 | 200 | |
10a831a3 JH |
201 | /* get 32k memory */ |
202 | mime_subject = store_get(32767); | |
8e669ac1 | 203 | |
10a831a3 | 204 | mime_subject_len = fread(mime_subject, 1, 32766, f); |
8e669ac1 | 205 | |
10a831a3 JH |
206 | ret = matcher(re_list_head, mime_subject, mime_subject_len); |
207 | (void)fclose(f); | |
208 | return ret; | |
8523533c TK |
209 | } |
210 | ||
476be7e2 | 211 | #endif /* WITH_CONTENT_SCAN */ |