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 */
98 regex(const uschar **listptr, BOOL cacheable)
100 unsigned long mbox_size;
102 pcre_list *re_list_head;
107 /* reset expansion variable */
108 regex_match_string = NULL;
110 if (!mime_stream) /* We are in the DATA ACL */
112 if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
113 { /* error while spooling */
114 log_write(0, LOG_MAIN|LOG_PANIC,
115 "regex acl condition: error while creating mbox spool file");
121 if ((f_pos = ftell(mime_stream)) < 0)
123 log_write(0, LOG_MAIN|LOG_PANIC,
124 "regex acl condition: mime_stream: %s", strerror(errno));
127 mbox_file = mime_stream;
130 /* precompile our regexes */
131 if (!(re_list_head = compile(*listptr, cacheable)))
132 return FAIL; /* no regexes -> nothing to do */
134 /* match each line against all regexes */
135 linebuffer = store_get(32767, GET_TAINTED);
136 while (fgets(CS linebuffer, 32767, mbox_file))
138 if ( mime_stream && mime_current_boundary /* check boundary */
139 && Ustrncmp(linebuffer, "--", 2) == 0
140 && Ustrncmp((linebuffer+2), mime_current_boundary,
141 Ustrlen(mime_current_boundary)) == 0)
142 break; /* found boundary */
144 if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
151 (void)fclose(mbox_file);
154 clearerr(mime_stream);
155 if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
157 log_write(0, LOG_MAIN|LOG_PANIC,
158 "regex acl condition: mime_stream: %s", strerror(errno));
159 clearerr(mime_stream);
168 mime_regex(const uschar **listptr, BOOL cacheable)
170 pcre_list *re_list_head = NULL;
172 uschar *mime_subject = NULL;
173 int mime_subject_len = 0;
176 /* reset expansion variable */
177 regex_match_string = NULL;
179 /* precompile our regexes */
180 if (!(re_list_head = compile(*listptr, cacheable)))
181 return FAIL; /* no regexes -> nothing to do */
183 /* check if the file is already decoded */
184 if (!mime_decoded_filename)
185 { /* no, decode it first */
186 const uschar *empty = US"";
188 if (!mime_decoded_filename)
189 { /* decoding failed */
190 log_write(0, LOG_MAIN,
191 "mime_regex acl condition warning - could not decode MIME part to file");
197 if (!(f = fopen(CS mime_decoded_filename, "rb")))
199 log_write(0, LOG_MAIN,
200 "mime_regex acl condition warning - can't open '%s' for reading",
201 mime_decoded_filename);
205 /* get 32k memory, tainted */
206 mime_subject = store_get(32767, GET_TAINTED);
208 mime_subject_len = fread(mime_subject, 1, 32766, f);
210 ret = matcher(re_list_head, mime_subject, mime_subject_len);
215 #endif /* WITH_CONTENT_SCAN */