1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
6 * Copyright (c) The Exim Maintainers 2016 - 2024
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;
126 if (!mime_stream) /* We are in the DATA ACL */
128 if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
129 { /* error while spooling */
130 log_write(0, LOG_MAIN|LOG_PANIC,
131 "regex acl condition: error while creating mbox spool file");
137 if ((f_pos = ftell(mime_stream)) < 0)
139 log_write(0, LOG_MAIN|LOG_PANIC,
140 "regex acl condition: mime_stream: %s", strerror(errno));
143 mbox_file = mime_stream;
146 /* precompile our regexes */
147 if ((re_list_head = compile(*listptr, cacheable, &cnt)))
149 rmark reset_point = store_mark();
151 /* match each line against all regexes */
152 while (fgets(CS big_buffer, big_buffer_size, mbox_file))
154 if ( mime_stream && mime_current_boundary /* check boundary */
155 && Ustrncmp(big_buffer, "--", 2) == 0
156 && Ustrncmp((big_buffer+2), mime_current_boundary,
157 Ustrlen(mime_current_boundary)) == 0)
158 break; /* found boundary */
160 if ((ret = matcher(re_list_head, big_buffer, (int)Ustrlen(big_buffer))) == OK)
163 if ((lcount -= cnt) <= 0)
165 store_reset(reset_point); reset_point = store_mark();
166 lcount = REGEX_LOOPCOUNT_STORE_RESET;
170 store_reset(reset_point);
174 (void)fclose(mbox_file);
177 clearerr(mime_stream);
178 if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
180 log_write(0, LOG_MAIN|LOG_PANIC,
181 "regex acl condition: mime_stream: %s", strerror(errno));
182 clearerr(mime_stream);
191 mime_regex(const uschar **listptr, BOOL cacheable)
193 pcre_list * re_list_head = NULL;
195 uschar * mime_subject = NULL;
196 int mime_subject_len = 0;
202 /* check if the file is already decoded */
203 if (!mime_decoded_filename)
204 { /* no, decode it first */
205 const uschar *empty = US"";
207 if (!mime_decoded_filename)
208 { /* decoding failed */
209 log_write(0, LOG_MAIN,
210 "mime_regex acl condition warning - could not decode MIME part to file");
216 if (!(f = fopen(CS mime_decoded_filename, "rb")))
218 log_write(0, LOG_MAIN,
219 "mime_regex acl condition warning - can't open '%s' for reading",
220 mime_decoded_filename);
224 reset_point = store_mark();
226 /* precompile our regexes */
227 if ((re_list_head = compile(*listptr, cacheable, NULL)))
229 /* get 32k memory, tainted */
230 mime_subject = store_get(32767, GET_TAINTED);
232 mime_subject_len = fread(mime_subject, 1, 32766, f);
234 ret = matcher(re_list_head, mime_subject, mime_subject_len);
237 store_reset(reset_point);
242 #endif /* WITH_CONTENT_SCAN */