1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
6 * Copyright (c) The Exim Maintainers 2016 - 2023
7 * Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-2015
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 /* Code for matching regular expressions against headers and body.
16 #ifdef WITH_CONTENT_SCAN
20 /* Structure to hold a list of Regular expressions */
21 typedef struct pcre_list {
22 const pcre2_code * re;
24 struct pcre_list * next;
27 uschar regex_match_string_buffer[1024];
29 extern FILE *mime_stream;
30 extern uschar *mime_current_boundary;
34 compile(const uschar * list, BOOL cacheable, int * cntp)
37 uschar * regex_string;
38 pcre_list * re_list_head = NULL, * ri;
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)
44 /* compile our regular expression */
46 const pcre2_code * re = regex_compile(regex_string,
47 cacheable ? MCS_CACHEABLE : MCS_NOFLAGS, &errstr, pcre_gen_cmp_ctx);
51 log_write(0, LOG_MAIN, "regex acl condition warning - %s, skipped", errstr);
55 ri = store_get(sizeof(pcre_list), GET_UNTAINTED);
57 ri->pcre_text = regex_string;
58 ri->next = re_list_head;
62 if (cntp) *cntp = cnt;
67 /* Check list of REs against buffer, returning OK for (first) match,
68 else FAIL. On match return allocated result strings in regex_vars[].
70 We use the perm-pool for that, so that our caller can release
74 matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
76 pcre2_match_data * md = pcre2_match_data_create(REGEX_VARS + 1, pcre_gen_ctx);
78 for (pcre_list * ri = re_list_head; ri; ri = ri->next)
82 /* try matcher on the line */
83 if ((n = pcre2_match(ri->re, (PCRE2_SPTR)linebuffer, len, 0, 0, md, pcre_gen_mtc_ctx)) > 0)
85 int save_pool = store_pool;
86 store_pool = POOL_PERM;
88 Ustrncpy(regex_match_string_buffer, ri->pcre_text,
89 sizeof(regex_match_string_buffer)-1);
90 regex_match_string = regex_match_string_buffer;
92 for (int nn = 1; nn < n; nn++)
94 PCRE2_SIZE * ovec = pcre2_get_ovector_pointer(md);
96 int len = ovec[off + 1] - ovec[off];
97 regex_vars[nn-1] = string_copyn(linebuffer + ovec[off], len);
100 store_pool = save_pool;
104 /* pcre2_match_data_free(md); gen ctx needs no free */
109 /* reset expansion variables */
111 regex_vars_clear(void)
113 regex_match_string = NULL;
114 for (int i = 0; i < REGEX_VARS; i++) regex_vars[i] = NULL;
120 regex(const uschar ** listptr, BOOL cacheable)
122 unsigned long mbox_size;
124 pcre_list * re_list_head;
127 int ret = FAIL, cnt, lcount = REGEX_LOOPCOUNT_STORE_RESET;
132 if (!mime_stream) /* We are in the DATA ACL */
134 if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
135 { /* error while spooling */
136 log_write(0, LOG_MAIN|LOG_PANIC,
137 "regex acl condition: error while creating mbox spool file");
143 if ((f_pos = ftell(mime_stream)) < 0)
145 log_write(0, LOG_MAIN|LOG_PANIC,
146 "regex acl condition: mime_stream: %s", strerror(errno));
149 mbox_file = mime_stream;
152 reset_point = store_mark();
154 /* precompile our regexes */
155 if ((re_list_head = compile(*listptr, cacheable, &cnt)))
157 /* match each line against all regexes */
158 linebuffer = store_get(32767, GET_TAINTED);
159 while (fgets(CS linebuffer, 32767, mbox_file))
161 if ( mime_stream && mime_current_boundary /* check boundary */
162 && Ustrncmp(linebuffer, "--", 2) == 0
163 && Ustrncmp((linebuffer+2), mime_current_boundary,
164 Ustrlen(mime_current_boundary)) == 0)
165 break; /* found boundary */
167 if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
170 if ((lcount -= cnt) <= 0)
172 store_reset(reset_point); reset_point = store_mark();
173 lcount = REGEX_LOOPCOUNT_STORE_RESET;
178 store_reset(reset_point);
181 (void)fclose(mbox_file);
184 clearerr(mime_stream);
185 if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
187 log_write(0, LOG_MAIN|LOG_PANIC,
188 "regex acl condition: mime_stream: %s", strerror(errno));
189 clearerr(mime_stream);
198 mime_regex(const uschar **listptr, BOOL cacheable)
200 pcre_list * re_list_head = NULL;
202 uschar * mime_subject = NULL;
203 int mime_subject_len = 0;
209 /* check if the file is already decoded */
210 if (!mime_decoded_filename)
211 { /* no, decode it first */
212 const uschar *empty = US"";
214 if (!mime_decoded_filename)
215 { /* decoding failed */
216 log_write(0, LOG_MAIN,
217 "mime_regex acl condition warning - could not decode MIME part to file");
223 if (!(f = fopen(CS mime_decoded_filename, "rb")))
225 log_write(0, LOG_MAIN,
226 "mime_regex acl condition warning - can't open '%s' for reading",
227 mime_decoded_filename);
231 reset_point = store_mark();
233 /* precompile our regexes */
234 if ((re_list_head = compile(*listptr, cacheable, NULL)))
236 /* get 32k memory, tainted */
237 mime_subject = store_get(32767, GET_TAINTED);
239 mime_subject_len = fread(mime_subject, 1, 32766, f);
241 ret = matcher(re_list_head, mime_subject, mime_subject_len);
244 store_reset(reset_point);
249 #endif /* WITH_CONTENT_SCAN */