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 matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
69 pcre2_match_data * md = pcre2_match_data_create(REGEX_VARS + 1, pcre_gen_ctx);
71 for (pcre_list * ri = re_list_head; ri; ri = ri->next)
75 /* try matcher on the line */
76 if ((n = pcre2_match(ri->re, (PCRE2_SPTR)linebuffer, len, 0, 0, md, pcre_gen_mtc_ctx)) > 0)
78 Ustrncpy(regex_match_string_buffer, ri->pcre_text,
79 sizeof(regex_match_string_buffer)-1);
80 regex_match_string = regex_match_string_buffer;
82 for (int nn = 1; nn < n; nn++)
84 PCRE2_SIZE * ovec = pcre2_get_ovector_pointer(md);
86 int len = ovec[off + 1] - ovec[off];
87 regex_vars[nn-1] = string_copyn(linebuffer + ovec[off], len);
93 /* pcre2_match_data_free(md); gen ctx needs no free */
98 /* reset expansion variables */
100 regex_vars_clear(void)
102 regex_match_string = NULL;
103 for (int i = 0; i < REGEX_VARS; i++) regex_vars[i] = NULL;
109 regex(const uschar ** listptr, BOOL cacheable)
111 unsigned long mbox_size;
113 pcre_list * re_list_head;
116 int ret = FAIL, cnt, lcount = REGEX_LOOPCOUNT_STORE_RESET;
121 if (!mime_stream) /* We are in the DATA ACL */
123 if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
124 { /* error while spooling */
125 log_write(0, LOG_MAIN|LOG_PANIC,
126 "regex acl condition: error while creating mbox spool file");
132 if ((f_pos = ftell(mime_stream)) < 0)
134 log_write(0, LOG_MAIN|LOG_PANIC,
135 "regex acl condition: mime_stream: %s", strerror(errno));
138 mbox_file = mime_stream;
141 reset_point = store_mark();
143 /* precompile our regexes */
144 if ((re_list_head = compile(*listptr, cacheable, &cnt)))
146 /* match each line against all regexes */
147 linebuffer = store_get(32767, GET_TAINTED);
148 while (fgets(CS linebuffer, 32767, mbox_file))
150 if ( mime_stream && mime_current_boundary /* check boundary */
151 && Ustrncmp(linebuffer, "--", 2) == 0
152 && Ustrncmp((linebuffer+2), mime_current_boundary,
153 Ustrlen(mime_current_boundary)) == 0)
154 break; /* found boundary */
156 if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
159 if ((lcount -= cnt) <= 0)
161 store_reset(reset_point); reset_point = store_mark();
162 lcount = REGEX_LOOPCOUNT_STORE_RESET;
167 store_reset(reset_point);
170 (void)fclose(mbox_file);
173 clearerr(mime_stream);
174 if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
176 log_write(0, LOG_MAIN|LOG_PANIC,
177 "regex acl condition: mime_stream: %s", strerror(errno));
178 clearerr(mime_stream);
187 mime_regex(const uschar **listptr, BOOL cacheable)
189 pcre_list * re_list_head = NULL;
191 uschar * mime_subject = NULL;
192 int mime_subject_len = 0;
198 /* check if the file is already decoded */
199 if (!mime_decoded_filename)
200 { /* no, decode it first */
201 const uschar *empty = US"";
203 if (!mime_decoded_filename)
204 { /* decoding failed */
205 log_write(0, LOG_MAIN,
206 "mime_regex acl condition warning - could not decode MIME part to file");
212 if (!(f = fopen(CS mime_decoded_filename, "rb")))
214 log_write(0, LOG_MAIN,
215 "mime_regex acl condition warning - can't open '%s' for reading",
216 mime_decoded_filename);
220 reset_point = store_mark();
222 /* precompile our regexes */
223 if ((re_list_head = compile(*listptr, cacheable, NULL)))
225 /* get 32k memory, tainted */
226 mime_subject = store_get(32767, GET_TAINTED);
228 mime_subject_len = fread(mime_subject, 1, 32766, f);
230 ret = matcher(re_list_head, mime_subject, mime_subject_len);
233 store_reset(reset_point);
238 #endif /* WITH_CONTENT_SCAN */