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