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 const char *pcre_error;
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 /* compile our regular expression */
45 if (!(re = pcre_compile( CS regex_string,
46 0, &pcre_error, &pcre_erroffset, NULL )))
48 log_write(0, LOG_MAIN,
49 "regex acl condition warning - error in regex '%s': %s at offset %d, skipped.",
50 regex_string, pcre_error, pcre_erroffset);
54 ri = store_get(sizeof(pcre_list));
56 ri->pcre_text = regex_string;
57 ri->next = re_list_head;
64 matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
68 for(ri = re_list_head; ri; ri = ri->next)
70 int ovec[3*(REGEX_VARS+1)];
73 /* try matcher on the line */
74 n = pcre_exec(ri->re, NULL, CS linebuffer, len, 0, 0, ovec, nelem(ovec));
77 Ustrncpy(regex_match_string_buffer, ri->pcre_text,
78 sizeof(regex_match_string_buffer)-1);
79 regex_match_string = regex_match_string_buffer;
81 for (nn = 1; nn < n; nn++)
83 string_copyn(linebuffer + ovec[nn*2], ovec[nn*2+1] - ovec[nn*2]);
92 regex(const uschar **listptr)
94 unsigned long mbox_size;
96 pcre_list *re_list_head;
101 /* reset expansion variable */
102 regex_match_string = NULL;
104 if (!mime_stream) /* We are in the DATA ACL */
106 if (!(mbox_file = spool_mbox(&mbox_size, NULL)))
107 { /* error while spooling */
108 log_write(0, LOG_MAIN|LOG_PANIC,
109 "regex acl condition: error while creating mbox spool file");
115 f_pos = ftell(mime_stream);
116 mbox_file = mime_stream;
119 /* precompile our regexes */
120 if (!(re_list_head = compile(*listptr)))
121 return FAIL; /* no regexes -> nothing to do */
123 /* match each line against all regexes */
124 linebuffer = store_get(32767);
125 while (fgets(CS linebuffer, 32767, mbox_file))
127 if ( mime_stream && mime_current_boundary /* check boundary */
128 && Ustrncmp(linebuffer, "--", 2) == 0
129 && Ustrncmp((linebuffer+2), mime_current_boundary,
130 Ustrlen(mime_current_boundary)) == 0)
131 break; /* found boundary */
133 if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
140 (void)fclose(mbox_file);
143 clearerr(mime_stream);
144 fseek(mime_stream, f_pos, SEEK_SET);
152 mime_regex(const uschar **listptr)
154 pcre_list *re_list_head = NULL;
156 uschar *mime_subject = NULL;
157 int mime_subject_len = 0;
160 /* reset expansion variable */
161 regex_match_string = NULL;
163 /* precompile our regexes */
164 if (!(re_list_head = compile(*listptr)))
165 return FAIL; /* no regexes -> nothing to do */
167 /* check if the file is already decoded */
168 if (!mime_decoded_filename)
169 { /* no, decode it first */
170 const uschar *empty = US"";
172 if (!mime_decoded_filename)
173 { /* decoding failed */
174 log_write(0, LOG_MAIN,
175 "mime_regex acl condition warning - could not decode MIME part to file");
181 if (!(f = fopen(CS mime_decoded_filename, "rb")))
183 log_write(0, LOG_MAIN,
184 "mime_regex acl condition warning - can't open '%s' for reading",
185 mime_decoded_filename);
190 mime_subject = store_get(32767);
192 mime_subject_len = fread(mime_subject, 1, 32766, f);
194 ret = matcher(re_list_head, mime_subject, mime_subject_len);
199 #endif /* WITH_CONTENT_SCAN */