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