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 extern FILE *mime_stream;
28 extern uschar *mime_current_boundary;
32 compile(const uschar * list, BOOL cacheable, int * cntp)
35 uschar * regex_string;
36 pcre_list * re_list_head = NULL, * ri;
38 /* precompile our regexes */
39 while ((regex_string = string_nextinlist(&list, &sep, NULL, 0)))
40 if (strcmpic(regex_string, US"false") != 0 && Ustrcmp(regex_string, "0") != 0)
42 /* compile our regular expression */
44 const pcre2_code * re = regex_compile(regex_string,
45 cacheable ? MCS_CACHEABLE : MCS_NOFLAGS, &errstr, pcre_gen_cmp_ctx);
49 log_write(0, LOG_MAIN, "regex acl condition warning - %s, skipped", errstr);
53 ri = store_get(sizeof(pcre_list), GET_UNTAINTED);
55 ri->pcre_text = regex_string;
56 ri->next = re_list_head;
60 if (cntp) *cntp = cnt;
65 /* Check list of REs against buffer, returning OK for (first) match,
66 else FAIL. On match return allocated result strings in regex_vars[].
68 We use the perm-pool for that, so that our caller can release
72 matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
74 pcre2_match_data * md = pcre2_match_data_create(REGEX_VARS + 1, pcre_gen_ctx);
76 for (pcre_list * ri = re_list_head; ri; ri = ri->next)
80 /* try matcher on the line */
81 if ((n = pcre2_match(ri->re, (PCRE2_SPTR)linebuffer, len, 0, 0, md, pcre_gen_mtc_ctx)) > 0)
83 int save_pool = store_pool;
84 store_pool = POOL_PERM;
86 regex_match_string = string_copy(ri->pcre_text);
88 for (int nn = 1; nn < n; nn++)
90 PCRE2_SIZE * ovec = pcre2_get_ovector_pointer(md);
92 int len = ovec[off + 1] - ovec[off];
93 regex_vars[nn-1] = string_copyn(linebuffer + ovec[off], len);
96 store_pool = save_pool;
100 /* pcre2_match_data_free(md); gen ctx needs no free */
105 /* reset expansion variables */
107 regex_vars_clear(void)
109 regex_match_string = NULL;
110 for (int i = 0; i < REGEX_VARS; i++) regex_vars[i] = NULL;
116 regex(const uschar ** listptr, BOOL cacheable)
118 unsigned long mbox_size;
120 pcre_list * re_list_head;
122 int ret = FAIL, cnt, lcount = REGEX_LOOPCOUNT_STORE_RESET;
127 if (!mime_stream) /* We are in the DATA ACL */
129 if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
130 { /* error while spooling */
131 log_write(0, LOG_MAIN|LOG_PANIC,
132 "regex acl condition: error while creating mbox spool file");
138 if ((f_pos = ftell(mime_stream)) < 0)
140 log_write(0, LOG_MAIN|LOG_PANIC,
141 "regex acl condition: mime_stream: %s", strerror(errno));
144 mbox_file = mime_stream;
147 reset_point = store_mark();
149 /* precompile our regexes */
150 if ((re_list_head = compile(*listptr, cacheable, &cnt)))
152 /* match each line against all regexes */
153 while (fgets(CS big_buffer, big_buffer_size, mbox_file))
155 if ( mime_stream && mime_current_boundary /* check boundary */
156 && Ustrncmp(big_buffer, "--", 2) == 0
157 && Ustrncmp((big_buffer+2), mime_current_boundary,
158 Ustrlen(mime_current_boundary)) == 0)
159 break; /* found boundary */
161 if ((ret = matcher(re_list_head, big_buffer, (int)Ustrlen(big_buffer))) == OK)
164 if ((lcount -= cnt) <= 0)
166 store_reset(reset_point); reset_point = store_mark();
167 lcount = REGEX_LOOPCOUNT_STORE_RESET;
172 store_reset(reset_point);
175 (void)fclose(mbox_file);
178 clearerr(mime_stream);
179 if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
181 log_write(0, LOG_MAIN|LOG_PANIC,
182 "regex acl condition: mime_stream: %s", strerror(errno));
183 clearerr(mime_stream);
192 mime_regex(const uschar **listptr, BOOL cacheable)
194 pcre_list * re_list_head = NULL;
196 uschar * mime_subject = NULL;
197 int mime_subject_len = 0;
203 /* check if the file is already decoded */
204 if (!mime_decoded_filename)
205 { /* no, decode it first */
206 const uschar *empty = US"";
208 if (!mime_decoded_filename)
209 { /* decoding failed */
210 log_write(0, LOG_MAIN,
211 "mime_regex acl condition warning - could not decode MIME part to file");
217 if (!(f = fopen(CS mime_decoded_filename, "rb")))
219 log_write(0, LOG_MAIN,
220 "mime_regex acl condition warning - can't open '%s' for reading",
221 mime_decoded_filename);
225 reset_point = store_mark();
227 /* precompile our regexes */
228 if ((re_list_head = compile(*listptr, cacheable, NULL)))
230 /* get 32k memory, tainted */
231 mime_subject = store_get(32767, GET_TAINTED);
233 mime_subject_len = fread(mime_subject, 1, 32766, f);
235 ret = matcher(re_list_head, mime_subject, mime_subject_len);
238 store_reset(reset_point);
243 #endif /* WITH_CONTENT_SCAN */