Events: dns:fail Bug 3011
[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 long f_pos = 0;
122 int ret = FAIL, cnt, lcount = REGEX_LOOPCOUNT_STORE_RESET;
123
124 regex_vars_clear();
125
126 if (!mime_stream)                               /* We are in the DATA ACL */
127   {
128   if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
129     {                                           /* error while spooling */
130     log_write(0, LOG_MAIN|LOG_PANIC,
131            "regex acl condition: error while creating mbox spool file");
132     return DEFER;
133     }
134   }
135 else
136   {
137   if ((f_pos = ftell(mime_stream)) < 0)
138     {
139     log_write(0, LOG_MAIN|LOG_PANIC,
140            "regex acl condition: mime_stream: %s", strerror(errno));
141     return DEFER;
142     }
143   mbox_file = mime_stream;
144   }
145
146   /* precompile our regexes */
147   if ((re_list_head = compile(*listptr, cacheable, &cnt)))
148     {
149     rmark reset_point = store_mark();
150
151     /* match each line against all regexes */
152     while (fgets(CS big_buffer, big_buffer_size, mbox_file))
153       {
154       if (  mime_stream && mime_current_boundary                /* check boundary */
155          && Ustrncmp(big_buffer, "--", 2) == 0
156          && Ustrncmp((big_buffer+2), mime_current_boundary,
157                       Ustrlen(mime_current_boundary)) == 0)
158         break;                                          /* found boundary */
159
160       if ((ret = matcher(re_list_head, big_buffer, (int)Ustrlen(big_buffer))) == OK)
161         break;
162
163       if ((lcount -= cnt) <= 0)
164         {
165         store_reset(reset_point); reset_point = store_mark();
166         lcount = REGEX_LOOPCOUNT_STORE_RESET;
167         }
168       }
169
170     store_reset(reset_point);
171     }
172
173 if (!mime_stream)
174   (void)fclose(mbox_file);
175 else
176   {
177   clearerr(mime_stream);
178   if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
179     {
180     log_write(0, LOG_MAIN|LOG_PANIC,
181            "regex acl condition: mime_stream: %s", strerror(errno));
182     clearerr(mime_stream);
183     }
184   }
185
186 return ret;
187 }
188
189
190 int
191 mime_regex(const uschar **listptr, BOOL cacheable)
192 {
193 pcre_list * re_list_head = NULL;
194 FILE * f;
195 uschar * mime_subject = NULL;
196 int mime_subject_len = 0;
197 int ret = FAIL;
198 rmark reset_point;
199
200 regex_vars_clear();
201
202 /* check if the file is already decoded */
203 if (!mime_decoded_filename)
204   {                             /* no, decode it first */
205   const uschar *empty = US"";
206   mime_decode(&empty);
207   if (!mime_decoded_filename)
208     {                           /* decoding failed */
209     log_write(0, LOG_MAIN,
210        "mime_regex acl condition warning - could not decode MIME part to file");
211     return DEFER;
212     }
213   }
214
215 /* open file */
216 if (!(f = fopen(CS mime_decoded_filename, "rb")))
217   {
218   log_write(0, LOG_MAIN,
219        "mime_regex acl condition warning - can't open '%s' for reading",
220        mime_decoded_filename);
221   return DEFER;
222   }
223
224 reset_point = store_mark();
225   {
226   /* precompile our regexes */
227   if ((re_list_head = compile(*listptr, cacheable, NULL)))
228     {
229     /* get 32k memory, tainted */
230     mime_subject = store_get(32767, GET_TAINTED);
231
232     mime_subject_len = fread(mime_subject, 1, 32766, f);
233
234     ret = matcher(re_list_head, mime_subject, mime_subject_len);
235     }
236   }
237 store_reset(reset_point);
238 (void)fclose(f);
239 return ret;
240 }
241
242 #endif /* WITH_CONTENT_SCAN */