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