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
11 /* Code for matching regular expressions against headers and body.
15 #ifdef WITH_CONTENT_SCAN
19 /* Structure to hold a list of Regular expressions */
20 typedef struct pcre_list {
21 const pcre2_code * re;
23 struct pcre_list * next;
26 uschar regex_match_string_buffer[1024];
28 extern FILE *mime_stream;
29 extern uschar *mime_current_boundary;
33 compile(const uschar * list, BOOL cacheable)
36 uschar * regex_string;
37 pcre_list * re_list_head = NULL;
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;
65 matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
67 pcre2_match_data * md = pcre2_match_data_create(REGEX_VARS + 1, pcre_gen_ctx);
69 for (pcre_list * ri = re_list_head; ri; ri = ri->next)
73 /* try matcher on the line */
74 if ((n = pcre2_match(ri->re, (PCRE2_SPTR)linebuffer, len, 0, 0, md, pcre_gen_mtc_ctx)) > 0)
76 Ustrncpy(regex_match_string_buffer, ri->pcre_text,
77 sizeof(regex_match_string_buffer)-1);
78 regex_match_string = regex_match_string_buffer;
80 for (int nn = 1; nn < n; nn++)
84 pcre2_substring_get_bynumber(md, nn, &cstr, &cslen); /* uses same ctx as md */
85 regex_vars[nn-1] = CUS cstr;
91 /* pcre2_match_data_free(md); gen ctx needs no free */
97 regex(const uschar ** listptr, BOOL cacheable)
99 unsigned long mbox_size;
101 pcre_list * re_list_head;
108 if (!mime_stream) /* We are in the DATA ACL */
110 if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
111 { /* error while spooling */
112 log_write(0, LOG_MAIN|LOG_PANIC,
113 "regex acl condition: error while creating mbox spool file");
119 if ((f_pos = ftell(mime_stream)) < 0)
121 log_write(0, LOG_MAIN|LOG_PANIC,
122 "regex acl condition: mime_stream: %s", strerror(errno));
125 mbox_file = mime_stream;
128 /* precompile our regexes */
129 if (!(re_list_head = compile(*listptr, cacheable)))
130 return FAIL; /* no regexes -> nothing to do */
132 /* match each line against all regexes */
133 linebuffer = store_get(32767, GET_TAINTED);
134 while (fgets(CS linebuffer, 32767, mbox_file))
136 if ( mime_stream && mime_current_boundary /* check boundary */
137 && Ustrncmp(linebuffer, "--", 2) == 0
138 && Ustrncmp((linebuffer+2), mime_current_boundary,
139 Ustrlen(mime_current_boundary)) == 0)
140 break; /* found boundary */
142 if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
149 (void)fclose(mbox_file);
152 clearerr(mime_stream);
153 if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
155 log_write(0, LOG_MAIN|LOG_PANIC,
156 "regex acl condition: mime_stream: %s", strerror(errno));
157 clearerr(mime_stream);
166 mime_regex(const uschar **listptr, BOOL cacheable)
168 pcre_list * re_list_head = NULL;
170 uschar * mime_subject = NULL;
171 int mime_subject_len = 0;
176 /* precompile our regexes */
177 if (!(re_list_head = compile(*listptr, cacheable)))
178 return FAIL; /* no regexes -> nothing to do */
180 /* check if the file is already decoded */
181 if (!mime_decoded_filename)
182 { /* no, decode it first */
183 const uschar *empty = US"";
185 if (!mime_decoded_filename)
186 { /* decoding failed */
187 log_write(0, LOG_MAIN,
188 "mime_regex acl condition warning - could not decode MIME part to file");
194 if (!(f = fopen(CS mime_decoded_filename, "rb")))
196 log_write(0, LOG_MAIN,
197 "mime_regex acl condition warning - can't open '%s' for reading",
198 mime_decoded_filename);
202 /* get 32k memory, tainted */
203 mime_subject = store_get(32767, GET_TAINTED);
205 mime_subject_len = fread(mime_subject, 1, 32766, f);
207 ret = matcher(re_list_head, mime_subject, mime_subject_len);
212 #endif /* WITH_CONTENT_SCAN */