1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
6 * Copyright (c) The Exim Maintainers 2016 - 2022
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)
37 uschar * regex_string;
38 pcre_list * re_list_head = NULL;
41 /* precompile our regexes */
42 while ((regex_string = string_nextinlist(&list, &sep, NULL, 0)))
43 if (strcmpic(regex_string, US"false") != 0 && Ustrcmp(regex_string, "0") != 0)
45 /* compile our regular expression */
47 const pcre2_code * re = regex_compile(regex_string,
48 cacheable ? MCS_CACHEABLE : MCS_NOFLAGS, &errstr, pcre_gen_cmp_ctx);
52 log_write(0, LOG_MAIN, "regex acl condition warning - %s, skipped", errstr);
56 ri = store_get(sizeof(pcre_list), GET_UNTAINTED);
58 ri->pcre_text = regex_string;
59 ri->next = re_list_head;
66 matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
68 pcre2_match_data * md = pcre2_match_data_create(REGEX_VARS + 1, pcre_gen_ctx);
70 for (pcre_list * ri = re_list_head; ri; ri = ri->next)
74 /* try matcher on the line */
75 if ((n = pcre2_match(ri->re, (PCRE2_SPTR)linebuffer, len, 0, 0, md, pcre_gen_mtc_ctx)) > 0)
77 Ustrncpy(regex_match_string_buffer, ri->pcre_text,
78 sizeof(regex_match_string_buffer)-1);
79 regex_match_string = regex_match_string_buffer;
81 for (int nn = 1; nn < n; nn++)
83 PCRE2_SIZE * ovec = pcre2_get_ovector_pointer(md);
85 int len = ovec[off + 1] - ovec[off];
86 regex_vars[nn-1] = string_copyn(linebuffer + ovec[off], len);
92 /* pcre2_match_data_free(md); gen ctx needs no free */
97 /* reset expansion variables */
99 regex_vars_clear(void)
101 regex_match_string = NULL;
102 for (int i = 0; i < REGEX_VARS; i++) regex_vars[i] = NULL;
108 regex(const uschar ** listptr, BOOL cacheable)
110 unsigned long mbox_size;
112 pcre_list * re_list_head;
119 if (!mime_stream) /* We are in the DATA ACL */
121 if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
122 { /* error while spooling */
123 log_write(0, LOG_MAIN|LOG_PANIC,
124 "regex acl condition: error while creating mbox spool file");
130 if ((f_pos = ftell(mime_stream)) < 0)
132 log_write(0, LOG_MAIN|LOG_PANIC,
133 "regex acl condition: mime_stream: %s", strerror(errno));
136 mbox_file = mime_stream;
139 /* precompile our regexes */
140 if (!(re_list_head = compile(*listptr, cacheable)))
141 return FAIL; /* no regexes -> nothing to do */
143 /* match each line against all regexes */
144 linebuffer = store_get(32767, GET_TAINTED);
145 while (fgets(CS linebuffer, 32767, mbox_file))
147 if ( mime_stream && mime_current_boundary /* check boundary */
148 && Ustrncmp(linebuffer, "--", 2) == 0
149 && Ustrncmp((linebuffer+2), mime_current_boundary,
150 Ustrlen(mime_current_boundary)) == 0)
151 break; /* found boundary */
153 if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
160 (void)fclose(mbox_file);
163 clearerr(mime_stream);
164 if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
166 log_write(0, LOG_MAIN|LOG_PANIC,
167 "regex acl condition: mime_stream: %s", strerror(errno));
168 clearerr(mime_stream);
177 mime_regex(const uschar **listptr, BOOL cacheable)
179 pcre_list * re_list_head = NULL;
181 uschar * mime_subject = NULL;
182 int mime_subject_len = 0;
187 /* precompile our regexes */
188 if (!(re_list_head = compile(*listptr, cacheable)))
189 return FAIL; /* no regexes -> nothing to do */
191 /* check if the file is already decoded */
192 if (!mime_decoded_filename)
193 { /* no, decode it first */
194 const uschar *empty = US"";
196 if (!mime_decoded_filename)
197 { /* decoding failed */
198 log_write(0, LOG_MAIN,
199 "mime_regex acl condition warning - could not decode MIME part to file");
205 if (!(f = fopen(CS mime_decoded_filename, "rb")))
207 log_write(0, LOG_MAIN,
208 "mime_regex acl condition warning - can't open '%s' for reading",
209 mime_decoded_filename);
213 /* get 32k memory, tainted */
214 mime_subject = store_get(32767, GET_TAINTED);
216 mime_subject_len = fread(mime_subject, 1, 32766, f);
218 ret = matcher(re_list_head, mime_subject, mime_subject_len);
223 #endif /* WITH_CONTENT_SCAN */