1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
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
;
28 int regex(uschar
**listptr
) {
30 uschar
*list
= *listptr
;
32 uschar regex_string_buffer
[1024];
33 unsigned long mbox_size
;
36 pcre_list
*re_list_head
= NULL
;
37 pcre_list
*re_list_item
;
38 const char *pcre_error
;
43 /* reset expansion variable */
44 regex_match_string
= NULL
;
46 if (mime_stream
== NULL
) {
47 /* We are in the DATA ACL */
48 mbox_file
= spool_mbox(&mbox_size
, NULL
);
49 if (mbox_file
== NULL
) {
50 /* error while spooling */
51 log_write(0, LOG_MAIN
|LOG_PANIC
,
52 "regex acl condition: error while creating mbox spool file");
57 f_pos
= ftell(mime_stream
);
58 mbox_file
= mime_stream
;
61 /* precompile our regexes */
62 while ((regex_string
= string_nextinlist(&list
, &sep
,
64 sizeof(regex_string_buffer
))) != NULL
) {
67 if ( (strcmpic(regex_string
,US
"false") == 0) ||
68 (Ustrcmp(regex_string
,"0") == 0) ) {
69 /* explicitly no matching */
73 /* compile our regular expression */
74 re
= pcre_compile( CS regex_string
,
81 log_write(0, LOG_MAIN
,
82 "regex acl condition warning - error in regex '%s': %s at offset %d, skipped.", regex_string
, pcre_error
, pcre_erroffset
);
86 re_list_item
= store_get(sizeof(pcre_list
));
87 re_list_item
->re
= re
;
88 re_list_item
->pcre_text
= string_copy(regex_string
);
89 re_list_item
->next
= re_list_head
;
90 re_list_head
= re_list_item
;
94 /* no regexes -> nothing to do */
95 if (re_list_head
== NULL
) {
99 /* match each line against all regexes */
100 linebuffer
= store_get(32767);
101 while (fgets(CS linebuffer
, 32767, mbox_file
) != NULL
) {
102 if ( (mime_stream
!= NULL
) && (mime_current_boundary
!= NULL
) ) {
104 if (Ustrncmp(linebuffer
,"--",2) == 0) {
105 if (Ustrncmp((linebuffer
+2),mime_current_boundary
,Ustrlen(mime_current_boundary
)) == 0)
110 re_list_item
= re_list_head
;
112 /* try matcher on the line */
113 if (pcre_exec(re_list_item
->re
, NULL
, CS linebuffer
,
114 (int)Ustrlen(linebuffer
), 0, 0, NULL
, 0) >= 0) {
115 Ustrncpy(regex_match_string_buffer
, re_list_item
->pcre_text
, 1023);
116 regex_match_string
= regex_match_string_buffer
;
117 if (mime_stream
== NULL
)
118 (void)fclose(mbox_file
);
120 clearerr(mime_stream
);
121 fseek(mime_stream
,f_pos
,SEEK_SET
);
125 re_list_item
= re_list_item
->next
;
126 } while (re_list_item
!= NULL
);
129 if (mime_stream
== NULL
)
130 (void)fclose(mbox_file
);
132 clearerr(mime_stream
);
133 fseek(mime_stream
,f_pos
,SEEK_SET
);
141 int mime_regex(uschar
**listptr
) {
143 uschar
*list
= *listptr
;
144 uschar
*regex_string
;
145 uschar regex_string_buffer
[1024];
147 pcre_list
*re_list_head
= NULL
;
148 pcre_list
*re_list_item
;
149 const char *pcre_error
;
152 uschar
*mime_subject
= NULL
;
153 int mime_subject_len
= 0;
155 /* reset expansion variable */
156 regex_match_string
= NULL
;
158 /* precompile our regexes */
159 while ((regex_string
= string_nextinlist(&list
, &sep
,
161 sizeof(regex_string_buffer
))) != NULL
) {
164 if ( (strcmpic(regex_string
,US
"false") == 0) ||
165 (Ustrcmp(regex_string
,"0") == 0) ) {
166 /* explicitly no matching */
170 /* compile our regular expression */
171 re
= pcre_compile( CS regex_string
,
178 log_write(0, LOG_MAIN
,
179 "regex acl condition warning - error in regex '%s': %s at offset %d, skipped.", regex_string
, pcre_error
, pcre_erroffset
);
183 re_list_item
= store_get(sizeof(pcre_list
));
184 re_list_item
->re
= re
;
185 re_list_item
->pcre_text
= string_copy(regex_string
);
186 re_list_item
->next
= re_list_head
;
187 re_list_head
= re_list_item
;
191 /* no regexes -> nothing to do */
192 if (re_list_head
== NULL
) {
196 /* check if the file is already decoded */
197 if (mime_decoded_filename
== NULL
) {
198 uschar
*empty
= US
"";
199 /* no, decode it first */
201 if (mime_decoded_filename
== NULL
) {
202 /* decoding failed */
203 log_write(0, LOG_MAIN
,
204 "mime_regex acl condition warning - could not decode MIME part to file.");
211 f
= fopen(CS mime_decoded_filename
, "rb");
214 log_write(0, LOG_MAIN
,
215 "mime_regex acl condition warning - can't open '%s' for reading.", mime_decoded_filename
);
220 mime_subject
= (uschar
*)store_get(32767);
222 /* read max 32k chars from file */
223 mime_subject_len
= fread(mime_subject
, 1, 32766, f
);
225 re_list_item
= re_list_head
;
227 /* try matcher on the mmapped file */
228 debug_printf("Matching '%s'\n", re_list_item
->pcre_text
);
229 if (pcre_exec(re_list_item
->re
, NULL
, CS mime_subject
,
230 mime_subject_len
, 0, 0, NULL
, 0) >= 0) {
231 Ustrncpy(regex_match_string_buffer
, re_list_item
->pcre_text
, 1023);
232 regex_match_string
= regex_match_string_buffer
;
236 re_list_item
= re_list_item
->next
;
237 } while (re_list_item
!= NULL
);