Merge branch 'debian'
[hcoop/debian/exim4.git] / src / regex.c
1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
4
5 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-2015
6 * License: GPL
7 * Copyright (c) The Exim Maintainers 2016 - 2018
8 */
9
10 /* Code for matching regular expressions against headers and body.
11 Called from acl.c. */
12
13 #include "exim.h"
14 #ifdef WITH_CONTENT_SCAN
15 #include <unistd.h>
16 #include <sys/mman.h>
17
18 /* Structure to hold a list of Regular expressions */
19 typedef struct pcre_list {
20 pcre *re;
21 uschar *pcre_text;
22 struct pcre_list *next;
23 } pcre_list;
24
25 uschar regex_match_string_buffer[1024];
26
27 extern FILE *mime_stream;
28 extern uschar *mime_current_boundary;
29
30 static pcre_list *
31 compile(const uschar * list)
32 {
33 int sep = 0;
34 uschar *regex_string;
35 const char *pcre_error;
36 int pcre_erroffset;
37 pcre_list *re_list_head = NULL;
38 pcre_list *ri;
39
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)
43 {
44 pcre *re;
45
46 /* compile our regular expression */
47 if (!(re = pcre_compile( CS regex_string,
48 0, &pcre_error, &pcre_erroffset, NULL )))
49 {
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);
53 continue;
54 }
55
56 ri = store_get(sizeof(pcre_list));
57 ri->re = re;
58 ri->pcre_text = regex_string;
59 ri->next = re_list_head;
60 re_list_head = ri;
61 }
62 return re_list_head;
63 }
64
65 static int
66 matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
67 {
68 pcre_list * ri;
69
70 for(ri = re_list_head; ri; ri = ri->next)
71 {
72 int ovec[3*(REGEX_VARS+1)];
73 int n, nn;
74
75 /* try matcher on the line */
76 n = pcre_exec(ri->re, NULL, CS linebuffer, len, 0, 0, ovec, nelem(ovec));
77 if (n > 0)
78 {
79 Ustrncpy(regex_match_string_buffer, ri->pcre_text,
80 sizeof(regex_match_string_buffer)-1);
81 regex_match_string = regex_match_string_buffer;
82
83 for (nn = 1; nn < n; nn++)
84 regex_vars[nn-1] =
85 string_copyn(linebuffer + ovec[nn*2], ovec[nn*2+1] - ovec[nn*2]);
86
87 return OK;
88 }
89 }
90 return FAIL;
91 }
92
93 int
94 regex(const uschar **listptr)
95 {
96 unsigned long mbox_size;
97 FILE *mbox_file;
98 pcre_list *re_list_head;
99 uschar *linebuffer;
100 long f_pos = 0;
101 int ret = FAIL;
102
103 /* reset expansion variable */
104 regex_match_string = NULL;
105
106 if (!mime_stream) /* We are in the DATA ACL */
107 {
108 if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
109 { /* error while spooling */
110 log_write(0, LOG_MAIN|LOG_PANIC,
111 "regex acl condition: error while creating mbox spool file");
112 return DEFER;
113 }
114 }
115 else
116 {
117 if ((f_pos = ftell(mime_stream)) < 0)
118 {
119 log_write(0, LOG_MAIN|LOG_PANIC,
120 "regex acl condition: mime_stream: %s", strerror(errno));
121 return DEFER;
122 }
123 mbox_file = mime_stream;
124 }
125
126 /* precompile our regexes */
127 if (!(re_list_head = compile(*listptr)))
128 return FAIL; /* no regexes -> nothing to do */
129
130 /* match each line against all regexes */
131 linebuffer = store_get(32767);
132 while (fgets(CS linebuffer, 32767, mbox_file))
133 {
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 */
139
140 if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
141 goto done;
142 }
143 /* no matches ... */
144
145 done:
146 if (!mime_stream)
147 (void)fclose(mbox_file);
148 else
149 {
150 clearerr(mime_stream);
151 if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
152 {
153 log_write(0, LOG_MAIN|LOG_PANIC,
154 "regex acl condition: mime_stream: %s", strerror(errno));
155 clearerr(mime_stream);
156 }
157 }
158
159 return ret;
160 }
161
162
163 int
164 mime_regex(const uschar **listptr)
165 {
166 pcre_list *re_list_head = NULL;
167 FILE *f;
168 uschar *mime_subject = NULL;
169 int mime_subject_len = 0;
170 int ret;
171
172 /* reset expansion variable */
173 regex_match_string = NULL;
174
175 /* precompile our regexes */
176 if (!(re_list_head = compile(*listptr)))
177 return FAIL; /* no regexes -> nothing to do */
178
179 /* check if the file is already decoded */
180 if (!mime_decoded_filename)
181 { /* no, decode it first */
182 const uschar *empty = US"";
183 mime_decode(&empty);
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");
188 return DEFER;
189 }
190 }
191
192 /* open file */
193 if (!(f = fopen(CS mime_decoded_filename, "rb")))
194 {
195 log_write(0, LOG_MAIN,
196 "mime_regex acl condition warning - can't open '%s' for reading",
197 mime_decoded_filename);
198 return DEFER;
199 }
200
201 /* get 32k memory */
202 mime_subject = store_get(32767);
203
204 mime_subject_len = fread(mime_subject, 1, 32766, f);
205
206 ret = matcher(re_list_head, mime_subject, mime_subject_len);
207 (void)fclose(f);
208 return ret;
209 }
210
211 #endif /* WITH_CONTENT_SCAN */