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
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 const char *pcre_error;
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)
46 /* compile our regular expression */
47 if (!(re = pcre_compile( CS regex_string,
48 0, &pcre_error, &pcre_erroffset, NULL )))
50 log_write(0, LOG_MAIN,
51 "regex acl condition warning - error in regex '%s': %s at offset %d, skipped.",
52 regex_string, pcre_error, pcre_erroffset);
56 ri = store_get(sizeof(pcre_list));
58 ri->pcre_text = regex_string;
59 ri->next = re_list_head;
66 matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
70 for(ri = re_list_head; ri; ri = ri->next)
72 int ovec[3*(REGEX_VARS+1)];
75 /* try matcher on the line */
76 n = pcre_exec(ri->re, NULL, CS linebuffer, len, 0, 0, ovec, nelem(ovec));
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 (nn = 1; nn < n; nn++)
85 string_copyn(linebuffer + ovec[nn*2], ovec[nn*2+1] - ovec[nn*2]);
94 regex(const uschar **listptr)
96 unsigned long mbox_size;
98 pcre_list *re_list_head;
103 /* reset expansion variable */
104 regex_match_string = NULL;
106 if (!mime_stream) /* We are in the DATA ACL */
108 if (!(mbox_file = spool_mbox(&mbox_size, NULL)))
109 { /* error while spooling */
110 log_write(0, LOG_MAIN|LOG_PANIC,
111 "regex acl condition: error while creating mbox spool file");
117 if ((f_pos = ftell(mime_stream)) < 0)
119 log_write(0, LOG_MAIN|LOG_PANIC,
120 "regex acl condition: mime_stream: %s", strerror(errno));
123 mbox_file = mime_stream;
126 /* precompile our regexes */
127 if (!(re_list_head = compile(*listptr)))
128 return FAIL; /* no regexes -> nothing to do */
130 /* match each line against all regexes */
131 linebuffer = store_get(32767);
132 while (fgets(CS linebuffer, 32767, mbox_file))
134 if ( mime_stream && mime_current_boundary /* check boundary */
135 && Ustrncmp(linebuffer, "--", 2) == 0
136 && Ustrncmp((linebuffer+2), mime_current_boundary,
137 Ustrlen(mime_current_boundary)) == 0)
138 break; /* found boundary */
140 if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
147 (void)fclose(mbox_file);
150 clearerr(mime_stream);
151 if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
153 log_write(0, LOG_MAIN|LOG_PANIC,
154 "regex acl condition: mime_stream: %s", strerror(errno));
155 clearerr(mime_stream);
164 mime_regex(const uschar **listptr)
166 pcre_list *re_list_head = NULL;
168 uschar *mime_subject = NULL;
169 int mime_subject_len = 0;
172 /* reset expansion variable */
173 regex_match_string = NULL;
175 /* precompile our regexes */
176 if (!(re_list_head = compile(*listptr)))
177 return FAIL; /* no regexes -> nothing to do */
179 /* check if the file is already decoded */
180 if (!mime_decoded_filename)
181 { /* no, decode it first */
182 const uschar *empty = US"";
184 if (!mime_decoded_filename)
185 { /* decoding failed */
186 log_write(0, LOG_MAIN,
187 "mime_regex acl condition warning - could not decode MIME part to file");
193 if (!(f = fopen(CS mime_decoded_filename, "rb")))
195 log_write(0, LOG_MAIN,
196 "mime_regex acl condition warning - can't open '%s' for reading",
197 mime_decoded_filename);
202 mime_subject = store_get(32767);
204 mime_subject_len = fread(mime_subject, 1, 32766, f);
206 ret = matcher(re_list_head, mime_subject, mime_subject_len);
211 #endif /* WITH_CONTENT_SCAN */