1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-2015
7 * Copyright (c) The Exim Maintainers 2016 - 2018
10 /* Code for matching regular expressions against headers and body.
14 #ifdef WITH_CONTENT_SCAN
18 /* Structure to hold a list of Regular expressions */
19 typedef struct pcre_list {
22 struct pcre_list *next;
25 uschar regex_match_string_buffer[1024];
27 extern FILE *mime_stream;
28 extern uschar *mime_current_boundary;
31 compile(const uschar * list)
35 pcre_list *re_list_head = NULL;
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)
44 PCRE2_SIZE pcre_erroffset;
46 /* compile our regular expression */
47 if (!(re = pcre2_compile( (PCRE2_SPTR) regex_string, PCRE2_ZERO_TERMINATED,
48 0, &err, &pcre_erroffset, pcre_cmp_ctx)))
51 pcre2_get_error_message(err, errbuf, sizeof(errbuf));
52 log_write(0, LOG_MAIN,
53 "regex acl condition warning - error in regex '%s': %s at offset %ld, skipped.",
54 regex_string, errbuf, (long)pcre_erroffset);
58 ri = store_get(sizeof(pcre_list), FALSE);
60 ri->pcre_text = regex_string;
61 ri->next = re_list_head;
68 matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
70 pcre2_match_data * md = pcre2_match_data_create(REGEX_VARS + 1, pcre_gen_ctx);
72 for (pcre_list * ri = re_list_head; ri; ri = ri->next)
76 /* try matcher on the line */
77 if ((n = pcre2_match(ri->re, (PCRE2_SPTR)linebuffer, len, 0, 0, md, pcre_mtc_ctx)) > 0)
79 Ustrncpy(regex_match_string_buffer, ri->pcre_text,
80 sizeof(regex_match_string_buffer)-1);
81 regex_match_string = regex_match_string_buffer;
83 for (int nn = 1; nn < n; nn++)
87 pcre2_substring_get_bynumber(md, nn, &cstr, &cslen);
88 regex_vars[nn-1] = CUS cstr;
94 pcre2_match_data_free(md);
99 regex(const uschar **listptr)
101 unsigned long mbox_size;
103 pcre_list *re_list_head;
108 /* reset expansion variable */
109 regex_match_string = NULL;
111 if (!mime_stream) /* We are in the DATA ACL */
113 if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
114 { /* error while spooling */
115 log_write(0, LOG_MAIN|LOG_PANIC,
116 "regex acl condition: error while creating mbox spool file");
122 if ((f_pos = ftell(mime_stream)) < 0)
124 log_write(0, LOG_MAIN|LOG_PANIC,
125 "regex acl condition: mime_stream: %s", strerror(errno));
128 mbox_file = mime_stream;
131 /* precompile our regexes */
132 if (!(re_list_head = compile(*listptr)))
133 return FAIL; /* no regexes -> nothing to do */
135 /* match each line against all regexes */
136 linebuffer = store_get(32767, TRUE); /* tainted */
137 while (fgets(CS linebuffer, 32767, mbox_file))
139 if ( mime_stream && mime_current_boundary /* check boundary */
140 && Ustrncmp(linebuffer, "--", 2) == 0
141 && Ustrncmp((linebuffer+2), mime_current_boundary,
142 Ustrlen(mime_current_boundary)) == 0)
143 break; /* found boundary */
145 if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
152 (void)fclose(mbox_file);
155 clearerr(mime_stream);
156 if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
158 log_write(0, LOG_MAIN|LOG_PANIC,
159 "regex acl condition: mime_stream: %s", strerror(errno));
160 clearerr(mime_stream);
169 mime_regex(const uschar **listptr)
171 pcre_list *re_list_head = NULL;
173 uschar *mime_subject = NULL;
174 int mime_subject_len = 0;
177 /* reset expansion variable */
178 regex_match_string = NULL;
180 /* precompile our regexes */
181 if (!(re_list_head = compile(*listptr)))
182 return FAIL; /* no regexes -> nothing to do */
184 /* check if the file is already decoded */
185 if (!mime_decoded_filename)
186 { /* no, decode it first */
187 const uschar *empty = US"";
189 if (!mime_decoded_filename)
190 { /* decoding failed */
191 log_write(0, LOG_MAIN,
192 "mime_regex acl condition warning - could not decode MIME part to file");
198 if (!(f = fopen(CS mime_decoded_filename, "rb")))
200 log_write(0, LOG_MAIN,
201 "mime_regex acl condition warning - can't open '%s' for reading",
202 mime_decoded_filename);
206 /* get 32k memory, tainted */
207 mime_subject = store_get(32767, TRUE);
209 mime_subject_len = fread(mime_subject, 1, 32766, f);
211 ret = matcher(re_list_head, mime_subject, mime_subject_len);
216 #endif /* WITH_CONTENT_SCAN */