use dynamic mem for regex_match_string
[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 extern FILE *mime_stream;
28 extern uschar *mime_current_boundary;
29
30
31 static pcre_list *
32 compile(const uschar * list, BOOL cacheable, int * cntp)
33 {
34 int sep = 0, cnt = 0;
35 uschar * regex_string;
36 pcre_list * re_list_head = NULL, * ri;
37
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)
41     {
42     /* compile our regular expression */
43     uschar * errstr;
44     const pcre2_code * re = regex_compile(regex_string,
45       cacheable ? MCS_CACHEABLE : MCS_NOFLAGS, &errstr, pcre_gen_cmp_ctx);
46
47     if (!re)
48       {
49       log_write(0, LOG_MAIN, "regex acl condition warning - %s, skipped", errstr);
50       continue;
51       }
52
53     ri = store_get(sizeof(pcre_list), GET_UNTAINTED);
54     ri->re = re;
55     ri->pcre_text = regex_string;
56     ri->next = re_list_head;
57     re_list_head = ri;
58     cnt++;
59     }
60 if (cntp) *cntp = cnt;
61 return re_list_head;
62 }
63
64
65 /* Check list of REs against buffer, returning OK for (first) match,
66 else FAIL.  On match return allocated result strings in regex_vars[]. 
67
68 We use the perm-pool for that, so that our caller can release
69 other allocations.
70 */
71 static int
72 matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
73 {
74 pcre2_match_data * md = pcre2_match_data_create(REGEX_VARS + 1, pcre_gen_ctx);
75
76 for (pcre_list * ri = re_list_head; ri; ri = ri->next)
77   {
78   int n;
79
80   /* try matcher on the line */
81   if ((n = pcre2_match(ri->re, (PCRE2_SPTR)linebuffer, len, 0, 0, md, pcre_gen_mtc_ctx)) > 0)
82     {
83     int save_pool = store_pool;
84     store_pool = POOL_PERM;
85
86     regex_match_string = string_copy(ri->pcre_text);
87
88     for (int nn = 1; nn < n; nn++)
89       {
90       PCRE2_SIZE * ovec = pcre2_get_ovector_pointer(md);
91       int off = nn * 2;
92       int len = ovec[off + 1] - ovec[off];
93       regex_vars[nn-1] = string_copyn(linebuffer + ovec[off], len);
94       }
95
96     store_pool = save_pool;
97     return OK;
98     }
99   }
100 /* pcre2_match_data_free(md);   gen ctx needs no free */
101 return FAIL;
102 }
103
104
105 /* reset expansion variables */
106 void
107 regex_vars_clear(void)
108 {
109 regex_match_string = NULL;
110 for (int i = 0; i < REGEX_VARS; i++) regex_vars[i] = NULL;
111 }
112
113
114
115 int
116 regex(const uschar ** listptr, BOOL cacheable)
117 {
118 unsigned long mbox_size;
119 FILE * mbox_file;
120 pcre_list * re_list_head;
121 uschar * linebuffer;
122 long f_pos = 0;
123 int ret = FAIL, cnt, lcount = REGEX_LOOPCOUNT_STORE_RESET;
124 rmark reset_point;
125
126 regex_vars_clear();
127
128 if (!mime_stream)                               /* We are in the DATA ACL */
129   {
130   if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
131     {                                           /* error while spooling */
132     log_write(0, LOG_MAIN|LOG_PANIC,
133            "regex acl condition: error while creating mbox spool file");
134     return DEFER;
135     }
136   }
137 else
138   {
139   if ((f_pos = ftell(mime_stream)) < 0)
140     {
141     log_write(0, LOG_MAIN|LOG_PANIC,
142            "regex acl condition: mime_stream: %s", strerror(errno));
143     return DEFER;
144     }
145   mbox_file = mime_stream;
146   }
147
148 reset_point = store_mark();
149   {
150   /* precompile our regexes */
151   if ((re_list_head = compile(*listptr, cacheable, &cnt)))
152     {
153     /* match each line against all regexes */
154     linebuffer = store_get(32767, GET_TAINTED);
155     while (fgets(CS linebuffer, 32767, mbox_file))
156       {
157       if (  mime_stream && mime_current_boundary                /* check boundary */
158          && Ustrncmp(linebuffer, "--", 2) == 0
159          && Ustrncmp((linebuffer+2), mime_current_boundary,
160                       Ustrlen(mime_current_boundary)) == 0)
161         break;                                          /* found boundary */
162
163       if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
164         break;
165
166       if ((lcount -= cnt) <= 0)
167         {
168         store_reset(reset_point); reset_point = store_mark();
169         lcount = REGEX_LOOPCOUNT_STORE_RESET;
170         }
171       }
172     }
173   }
174 store_reset(reset_point);
175
176 if (!mime_stream)
177   (void)fclose(mbox_file);
178 else
179   {
180   clearerr(mime_stream);
181   if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
182     {
183     log_write(0, LOG_MAIN|LOG_PANIC,
184            "regex acl condition: mime_stream: %s", strerror(errno));
185     clearerr(mime_stream);
186     }
187   }
188
189 return ret;
190 }
191
192
193 int
194 mime_regex(const uschar **listptr, BOOL cacheable)
195 {
196 pcre_list * re_list_head = NULL;
197 FILE * f;
198 uschar * mime_subject = NULL;
199 int mime_subject_len = 0;
200 int ret = FAIL;
201 rmark reset_point;
202
203 regex_vars_clear();
204
205 /* check if the file is already decoded */
206 if (!mime_decoded_filename)
207   {                             /* no, decode it first */
208   const uschar *empty = US"";
209   mime_decode(&empty);
210   if (!mime_decoded_filename)
211     {                           /* decoding failed */
212     log_write(0, LOG_MAIN,
213        "mime_regex acl condition warning - could not decode MIME part to file");
214     return DEFER;
215     }
216   }
217
218 /* open file */
219 if (!(f = fopen(CS mime_decoded_filename, "rb")))
220   {
221   log_write(0, LOG_MAIN,
222        "mime_regex acl condition warning - can't open '%s' for reading",
223        mime_decoded_filename);
224   return DEFER;
225   }
226
227 reset_point = store_mark();
228   {
229   /* precompile our regexes */
230   if ((re_list_head = compile(*listptr, cacheable, NULL)))
231     {
232     /* get 32k memory, tainted */
233     mime_subject = store_get(32767, GET_TAINTED);
234
235     mime_subject_len = fread(mime_subject, 1, 32766, f);
236
237     ret = matcher(re_list_head, mime_subject, mime_subject_len);
238     }
239   }
240 store_reset(reset_point);
241 (void)fclose(f);
242 return ret;
243 }
244
245 #endif /* WITH_CONTENT_SCAN */