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