1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-2015 */
8 /* Code for matching regular expressions against headers and body.
12 #ifdef WITH_CONTENT_SCAN
16 /* Structure to hold a list of Regular expressions */
17 typedef struct pcre_list {
20 struct pcre_list *next;
23 uschar regex_match_string_buffer[1024];
25 extern FILE *mime_stream;
26 extern uschar *mime_current_boundary;
29 compile(const uschar * list)
33 uschar regex_string_buffer[1024];
34 const char *pcre_error;
36 pcre_list *re_list_head = NULL;
39 /* precompile our regexes */
40 while ((regex_string = string_nextinlist(&list, &sep,
42 sizeof(regex_string_buffer))) != NULL) {
46 if ( (strcmpic(regex_string,US"false") == 0) ||
47 (Ustrcmp(regex_string,"0") == 0) )
48 continue; /* explicitly no matching */
50 /* compile our regular expression */
51 if (!(re = pcre_compile( CS regex_string,
52 0, &pcre_error, &pcre_erroffset, NULL ))) {
53 log_write(0, LOG_MAIN,
54 "regex acl condition warning - error in regex '%s': %s at offset %d, skipped.",
55 regex_string, pcre_error, pcre_erroffset);
59 ri = store_get(sizeof(pcre_list));
61 ri->pcre_text = string_copy(regex_string);
62 ri->next = re_list_head;
69 matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
73 for(ri = re_list_head; ri; ri = ri->next)
75 int ovec[3*(REGEX_VARS+1)];
78 /* try matcher on the line */
79 n = pcre_exec(ri->re, NULL,
80 CS linebuffer, len, 0, 0,
84 Ustrncpy(regex_match_string_buffer, ri->pcre_text, 1023);
85 regex_match_string = regex_match_string_buffer;
87 for (nn = 1; nn < n; nn++)
89 string_copyn(linebuffer + ovec[nn*2], ovec[nn*2+1] - ovec[nn*2]);
98 regex(const uschar **listptr)
100 unsigned long mbox_size;
102 pcre_list *re_list_head;
107 /* reset expansion variable */
108 regex_match_string = NULL;
110 if (mime_stream == NULL) { /* We are in the DATA ACL */
111 mbox_file = spool_mbox(&mbox_size, NULL);
112 if (mbox_file == NULL) { /* error while spooling */
113 log_write(0, LOG_MAIN|LOG_PANIC,
114 "regex acl condition: error while creating mbox spool file");
119 f_pos = ftell(mime_stream);
120 mbox_file = mime_stream;
123 /* precompile our regexes */
124 if (!(re_list_head = compile(*listptr)))
125 return FAIL; /* no regexes -> nothing to do */
127 /* match each line against all regexes */
128 linebuffer = store_get(32767);
129 while (fgets(CS linebuffer, 32767, mbox_file) != NULL) {
131 if ( mime_stream && mime_current_boundary /* check boundary */
132 && Ustrncmp(linebuffer,"--",2) == 0
133 && Ustrncmp((linebuffer+2),mime_current_boundary,Ustrlen(mime_current_boundary)) == 0)
134 break; /* found boundary */
136 if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
142 if (mime_stream == NULL)
143 (void)fclose(mbox_file);
145 clearerr(mime_stream);
146 fseek(mime_stream,f_pos,SEEK_SET);
154 mime_regex(const uschar **listptr)
156 pcre_list *re_list_head = NULL;
158 uschar *mime_subject = NULL;
159 int mime_subject_len = 0;
162 /* reset expansion variable */
163 regex_match_string = NULL;
165 /* precompile our regexes */
166 if (!(re_list_head = compile(*listptr)))
167 return FAIL; /* no regexes -> nothing to do */
169 /* check if the file is already decoded */
170 if (mime_decoded_filename == NULL) {
171 const uschar *empty = US"";
172 /* no, decode it first */
174 if (mime_decoded_filename == NULL) {
175 /* decoding failed */
176 log_write(0, LOG_MAIN,
177 "mime_regex acl condition warning - could not decode MIME part to file.");
183 if (!(f = fopen(CS mime_decoded_filename, "rb"))) {
184 log_write(0, LOG_MAIN,
185 "mime_regex acl condition warning - can't open '%s' for reading.",
186 mime_decoded_filename);
191 mime_subject = (uschar *)store_get(32767);
193 mime_subject_len = fread(mime_subject, 1, 32766, f);
195 ret = matcher(re_list_head, mime_subject, mime_subject_len);
200 #endif /* WITH_CONTENT_SCAN */