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