ACL: in "regex" condition, release store every thousand lines. Bug 3047
[exim.git] / src / src / regex.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /*
6  * Copyright (c) The Exim Maintainers 2016 - 2023
7  * Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-2015
8  * License: GPL
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11
12 /* Code for matching regular expressions against headers and body.
13  Called from acl.c. */
14
15 #include "exim.h"
16 #ifdef WITH_CONTENT_SCAN
17 #include <unistd.h>
18 #include <sys/mman.h>
19
20 /* Structure to hold a list of Regular expressions */
21 typedef struct pcre_list {
22   const pcre2_code *    re;
23   uschar *              pcre_text;
24   struct pcre_list *    next;
25 } pcre_list;
26
27 uschar regex_match_string_buffer[1024];
28
29 extern FILE *mime_stream;
30 extern uschar *mime_current_boundary;
31
32
33 static pcre_list *
34 compile(const uschar * list, BOOL cacheable, int * cntp)
35 {
36 int sep = 0, cnt = 0;
37 uschar * regex_string;
38 pcre_list * re_list_head = NULL, * 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     /* compile our regular expression */
45     uschar * errstr;
46     const pcre2_code * re = regex_compile(regex_string,
47       cacheable ? MCS_CACHEABLE : MCS_NOFLAGS, &errstr, pcre_gen_cmp_ctx);
48
49     if (!re)
50       {
51       log_write(0, LOG_MAIN, "regex acl condition warning - %s, skipped", errstr);
52       continue;
53       }
54
55     ri = store_get(sizeof(pcre_list), GET_UNTAINTED);
56     ri->re = re;
57     ri->pcre_text = regex_string;
58     ri->next = re_list_head;
59     re_list_head = ri;
60     cnt++;
61     }
62 if (cntp) *cntp = cnt;
63 return re_list_head;
64 }
65
66 static int
67 matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
68 {
69 pcre2_match_data * md = pcre2_match_data_create(REGEX_VARS + 1, pcre_gen_ctx);
70
71 for (pcre_list * ri = re_list_head; ri; ri = ri->next)
72   {
73   int n;
74
75   /* try matcher on the line */
76   if ((n = pcre2_match(ri->re, (PCRE2_SPTR)linebuffer, len, 0, 0, md, pcre_gen_mtc_ctx)) > 0)
77     {
78     Ustrncpy(regex_match_string_buffer, ri->pcre_text,
79               sizeof(regex_match_string_buffer)-1);
80     regex_match_string = regex_match_string_buffer;
81
82     for (int nn = 1; nn < n; nn++)
83       {
84       PCRE2_SIZE * ovec = pcre2_get_ovector_pointer(md);
85       int off = nn * 2;
86       int len = ovec[off + 1] - ovec[off];
87       regex_vars[nn-1] = string_copyn(linebuffer + ovec[off], len);
88       }
89
90     return OK;
91     }
92   }
93 /* pcre2_match_data_free(md);   gen ctx needs no free */
94 return FAIL;
95 }
96
97
98 /* reset expansion variables */
99 void
100 regex_vars_clear(void)
101 {
102 regex_match_string = NULL;
103 for (int i = 0; i < REGEX_VARS; i++) regex_vars[i] = NULL;
104 }
105
106
107
108 int
109 regex(const uschar ** listptr, BOOL cacheable)
110 {
111 unsigned long mbox_size;
112 FILE * mbox_file;
113 pcre_list * re_list_head;
114 uschar * linebuffer;
115 long f_pos = 0;
116 int ret = FAIL, cnt, lcount = REGEX_LOOPCOUNT_STORE_RESET;
117 rmark reset_point;
118
119 regex_vars_clear();
120
121 if (!mime_stream)                               /* We are in the DATA ACL */
122   {
123   if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
124     {                                           /* error while spooling */
125     log_write(0, LOG_MAIN|LOG_PANIC,
126            "regex acl condition: error while creating mbox spool file");
127     return DEFER;
128     }
129   }
130 else
131   {
132   if ((f_pos = ftell(mime_stream)) < 0)
133     {
134     log_write(0, LOG_MAIN|LOG_PANIC,
135            "regex acl condition: mime_stream: %s", strerror(errno));
136     return DEFER;
137     }
138   mbox_file = mime_stream;
139   }
140
141 reset_point = store_mark();
142   {
143   /* precompile our regexes */
144   if ((re_list_head = compile(*listptr, cacheable, &cnt)))
145     {
146     /* match each line against all regexes */
147     linebuffer = store_get(32767, GET_TAINTED);
148     while (fgets(CS linebuffer, 32767, mbox_file))
149       {
150       if (  mime_stream && mime_current_boundary                /* check boundary */
151          && Ustrncmp(linebuffer, "--", 2) == 0
152          && Ustrncmp((linebuffer+2), mime_current_boundary,
153                       Ustrlen(mime_current_boundary)) == 0)
154         break;                                          /* found boundary */
155
156       if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
157         break;
158
159       if ((lcount -= cnt) <= 0)
160         {
161         store_reset(reset_point); reset_point = store_mark();
162         lcount = REGEX_LOOPCOUNT_STORE_RESET;
163         }
164       }
165     }
166   }
167 store_reset(reset_point);
168
169 if (!mime_stream)
170   (void)fclose(mbox_file);
171 else
172   {
173   clearerr(mime_stream);
174   if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
175     {
176     log_write(0, LOG_MAIN|LOG_PANIC,
177            "regex acl condition: mime_stream: %s", strerror(errno));
178     clearerr(mime_stream);
179     }
180   }
181
182 return ret;
183 }
184
185
186 int
187 mime_regex(const uschar **listptr, BOOL cacheable)
188 {
189 pcre_list * re_list_head = NULL;
190 FILE * f;
191 uschar * mime_subject = NULL;
192 int mime_subject_len = 0;
193 int ret = FAIL;
194 rmark reset_point;
195
196 regex_vars_clear();
197
198 /* check if the file is already decoded */
199 if (!mime_decoded_filename)
200   {                             /* no, decode it first */
201   const uschar *empty = US"";
202   mime_decode(&empty);
203   if (!mime_decoded_filename)
204     {                           /* decoding failed */
205     log_write(0, LOG_MAIN,
206        "mime_regex acl condition warning - could not decode MIME part to file");
207     return DEFER;
208     }
209   }
210
211 /* open file */
212 if (!(f = fopen(CS mime_decoded_filename, "rb")))
213   {
214   log_write(0, LOG_MAIN,
215        "mime_regex acl condition warning - can't open '%s' for reading",
216        mime_decoded_filename);
217   return DEFER;
218   }
219
220 reset_point = store_mark();
221   {
222   /* precompile our regexes */
223   if ((re_list_head = compile(*listptr, cacheable, NULL)))
224     {
225     /* get 32k memory, tainted */
226     mime_subject = store_get(32767, GET_TAINTED);
227
228     mime_subject_len = fread(mime_subject, 1, 32766, f);
229
230     ret = matcher(re_list_head, mime_subject, mime_subject_len);
231     }
232   }
233 store_reset(reset_point);
234 (void)fclose(f);
235 return ret;
236 }
237
238 #endif /* WITH_CONTENT_SCAN */