Fix non-WITH_CONTENT_SCAN build.
[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 int
97 regex(const uschar ** listptr, BOOL cacheable)
98 {
99 unsigned long mbox_size;
100 FILE * mbox_file;
101 pcre_list * re_list_head;
102 uschar * linebuffer;
103 long f_pos = 0;
104 int ret = FAIL;
105
106 regex_vars_clear();
107
108 if (!mime_stream)                               /* We are in the DATA ACL */
109   {
110   if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
111     {                                           /* error while spooling */
112     log_write(0, LOG_MAIN|LOG_PANIC,
113            "regex acl condition: error while creating mbox spool file");
114     return DEFER;
115     }
116   }
117 else
118   {
119   if ((f_pos = ftell(mime_stream)) < 0)
120     {
121     log_write(0, LOG_MAIN|LOG_PANIC,
122            "regex acl condition: mime_stream: %s", strerror(errno));
123     return DEFER;
124     }
125   mbox_file = mime_stream;
126   }
127
128 /* precompile our regexes */
129 if (!(re_list_head = compile(*listptr, cacheable)))
130   return FAIL;                  /* no regexes -> nothing to do */
131
132 /* match each line against all regexes */
133 linebuffer = store_get(32767, GET_TAINTED);
134 while (fgets(CS linebuffer, 32767, mbox_file))
135   {
136   if (  mime_stream && mime_current_boundary            /* check boundary */
137      && Ustrncmp(linebuffer, "--", 2) == 0
138      && Ustrncmp((linebuffer+2), mime_current_boundary,
139                   Ustrlen(mime_current_boundary)) == 0)
140       break;                                            /* found boundary */
141
142   if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
143     goto done;
144   }
145 /* no matches ... */
146
147 done:
148 if (!mime_stream)
149   (void)fclose(mbox_file);
150 else
151   {
152   clearerr(mime_stream);
153   if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
154     {
155     log_write(0, LOG_MAIN|LOG_PANIC,
156            "regex acl condition: mime_stream: %s", strerror(errno));
157     clearerr(mime_stream);
158     }
159   }
160
161 return ret;
162 }
163
164
165 int
166 mime_regex(const uschar **listptr, BOOL cacheable)
167 {
168 pcre_list * re_list_head = NULL;
169 FILE * f;
170 uschar * mime_subject = NULL;
171 int mime_subject_len = 0;
172 int ret;
173
174 regex_vars_clear();
175
176 /* precompile our regexes */
177 if (!(re_list_head = compile(*listptr, cacheable)))
178   return FAIL;                  /* no regexes -> nothing to do */
179
180 /* check if the file is already decoded */
181 if (!mime_decoded_filename)
182   {                             /* no, decode it first */
183   const uschar *empty = US"";
184   mime_decode(&empty);
185   if (!mime_decoded_filename)
186     {                           /* decoding failed */
187     log_write(0, LOG_MAIN,
188        "mime_regex acl condition warning - could not decode MIME part to file");
189     return DEFER;
190     }
191   }
192
193 /* open file */
194 if (!(f = fopen(CS mime_decoded_filename, "rb")))
195   {
196   log_write(0, LOG_MAIN,
197        "mime_regex acl condition warning - can't open '%s' for reading",
198        mime_decoded_filename);
199   return DEFER;
200   }
201
202 /* get 32k memory, tainted */
203 mime_subject = store_get(32767, GET_TAINTED);
204
205 mime_subject_len = fread(mime_subject, 1, 32766, f);
206
207 ret = matcher(re_list_head, mime_subject, mime_subject_len);
208 (void)fclose(f);
209 return ret;
210 }
211
212 #endif /* WITH_CONTENT_SCAN */