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