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 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
)
68 for(pcre_list
* ri
= re_list_head
; ri
; ri
= ri
->next
)
70 int ovec
[3*(REGEX_VARS
+1)];
73 /* try matcher on the line */
74 if ((n
= pcre_exec(ri
->re
, NULL
, CS linebuffer
, len
, 0, 0, ovec
, nelem(ovec
))) > 0)
76 Ustrncpy(regex_match_string_buffer
, ri
->pcre_text
,
77 sizeof(regex_match_string_buffer
)-1);
78 regex_match_string
= regex_match_string_buffer
;
80 for (int nn
= 1; nn
< n
; nn
++)
82 string_copyn(linebuffer
+ ovec
[nn
*2], ovec
[nn
*2+1] - ovec
[nn
*2]);
91 regex(const uschar
**listptr
)
93 unsigned long mbox_size
;
95 pcre_list
*re_list_head
;
100 /* reset expansion variable */
101 regex_match_string
= NULL
;
103 if (!mime_stream
) /* We are in the DATA ACL */
105 if (!(mbox_file
= spool_mbox(&mbox_size
, NULL
, NULL
)))
106 { /* error while spooling */
107 log_write(0, LOG_MAIN
|LOG_PANIC
,
108 "regex acl condition: error while creating mbox spool file");
114 if ((f_pos
= ftell(mime_stream
)) < 0)
116 log_write(0, LOG_MAIN
|LOG_PANIC
,
117 "regex acl condition: mime_stream: %s", strerror(errno
));
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
))
131 if ( mime_stream
&& mime_current_boundary
/* check boundary */
132 && Ustrncmp(linebuffer
, "--", 2) == 0
133 && Ustrncmp((linebuffer
+2), mime_current_boundary
,
134 Ustrlen(mime_current_boundary
)) == 0)
135 break; /* found boundary */
137 if ((ret
= matcher(re_list_head
, linebuffer
, (int)Ustrlen(linebuffer
))) == OK
)
144 (void)fclose(mbox_file
);
147 clearerr(mime_stream
);
148 if (fseek(mime_stream
, f_pos
, SEEK_SET
) == -1)
150 log_write(0, LOG_MAIN
|LOG_PANIC
,
151 "regex acl condition: mime_stream: %s", strerror(errno
));
152 clearerr(mime_stream
);
161 mime_regex(const uschar
**listptr
)
163 pcre_list
*re_list_head
= NULL
;
165 uschar
*mime_subject
= NULL
;
166 int mime_subject_len
= 0;
169 /* reset expansion variable */
170 regex_match_string
= NULL
;
172 /* precompile our regexes */
173 if (!(re_list_head
= compile(*listptr
)))
174 return FAIL
; /* no regexes -> nothing to do */
176 /* check if the file is already decoded */
177 if (!mime_decoded_filename
)
178 { /* no, decode it first */
179 const uschar
*empty
= US
"";
181 if (!mime_decoded_filename
)
182 { /* decoding failed */
183 log_write(0, LOG_MAIN
,
184 "mime_regex acl condition warning - could not decode MIME part to file");
190 if (!(f
= fopen(CS mime_decoded_filename
, "rb")))
192 log_write(0, LOG_MAIN
,
193 "mime_regex acl condition warning - can't open '%s' for reading",
194 mime_decoded_filename
);
199 mime_subject
= store_get(32767);
201 mime_subject_len
= fread(mime_subject
, 1, 32766, f
);
203 ret
= matcher(re_list_head
, mime_subject
, mime_subject_len
);
208 #endif /* WITH_CONTENT_SCAN */