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