Support pkg-config for SSL libraries.
[exim.git] / src / src / regex.c
CommitLineData
8523533c
TK
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
5/* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
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
28int regex(uschar **listptr) {
29 int sep = 0;
30 uschar *list = *listptr;
31 uschar *regex_string;
32 uschar regex_string_buffer[1024];
f7b63901 33 unsigned long mbox_size;
8523533c
TK
34 FILE *mbox_file;
35 pcre *re;
36 pcre_list *re_list_head = NULL;
37 pcre_list *re_list_item;
38 const char *pcre_error;
39 int pcre_erroffset;
40 uschar *linebuffer;
41 long f_pos = 0;
8e669ac1 42
8523533c
TK
43 /* reset expansion variable */
44 regex_match_string = NULL;
8e669ac1 45
8523533c
TK
46 if (mime_stream == NULL) {
47 /* We are in the DATA ACL */
8544e77a 48 mbox_file = spool_mbox(&mbox_size, NULL);
8523533c
TK
49 if (mbox_file == NULL) {
50 /* error while spooling */
51 log_write(0, LOG_MAIN|LOG_PANIC,
52 "regex acl condition: error while creating mbox spool file");
53 return DEFER;
54 };
55 }
56 else {
57 f_pos = ftell(mime_stream);
58 mbox_file = mime_stream;
59 };
8e669ac1 60
8523533c
TK
61 /* precompile our regexes */
62 while ((regex_string = string_nextinlist(&list, &sep,
63 regex_string_buffer,
64 sizeof(regex_string_buffer))) != NULL) {
8e669ac1 65
8523533c 66 /* parse option */
8e669ac1 67 if ( (strcmpic(regex_string,US"false") == 0) ||
8523533c
TK
68 (Ustrcmp(regex_string,"0") == 0) ) {
69 /* explicitly no matching */
70 continue;
71 };
8e669ac1 72
8523533c
TK
73 /* compile our regular expression */
74 re = pcre_compile( CS regex_string,
75 0,
76 &pcre_error,
77 &pcre_erroffset,
78 NULL );
79
80 if (re == NULL) {
81 log_write(0, LOG_MAIN,
82 "regex acl condition warning - error in regex '%s': %s at offset %d, skipped.", regex_string, pcre_error, pcre_erroffset);
83 continue;
84 }
85 else {
86 re_list_item = store_get(sizeof(pcre_list));
87 re_list_item->re = re;
88 re_list_item->pcre_text = string_copy(regex_string);
89 re_list_item->next = re_list_head;
90 re_list_head = re_list_item;
91 };
92 };
8e669ac1 93
8523533c
TK
94 /* no regexes -> nothing to do */
95 if (re_list_head == NULL) {
96 return FAIL;
97 };
8e669ac1 98
8523533c
TK
99 /* match each line against all regexes */
100 linebuffer = store_get(32767);
8e669ac1 101 while (fgets(CS linebuffer, 32767, mbox_file) != NULL) {
8523533c
TK
102 if ( (mime_stream != NULL) && (mime_current_boundary != NULL) ) {
103 /* check boundary */
104 if (Ustrncmp(linebuffer,"--",2) == 0) {
105 if (Ustrncmp((linebuffer+2),mime_current_boundary,Ustrlen(mime_current_boundary)) == 0)
106 /* found boundary */
107 break;
108 };
109 };
110 re_list_item = re_list_head;
111 do {
112 /* try matcher on the line */
113 if (pcre_exec(re_list_item->re, NULL, CS linebuffer,
384152a6 114 (int)Ustrlen(linebuffer), 0, 0, NULL, 0) >= 0) {
8523533c
TK
115 Ustrncpy(regex_match_string_buffer, re_list_item->pcre_text, 1023);
116 regex_match_string = regex_match_string_buffer;
117 if (mime_stream == NULL)
f1e894f3 118 (void)fclose(mbox_file);
8523533c
TK
119 else {
120 clearerr(mime_stream);
121 fseek(mime_stream,f_pos,SEEK_SET);
122 };
123 return OK;
124 };
125 re_list_item = re_list_item->next;
126 } while (re_list_item != NULL);
127 };
8e669ac1 128
8523533c 129 if (mime_stream == NULL)
f1e894f3 130 (void)fclose(mbox_file);
8523533c
TK
131 else {
132 clearerr(mime_stream);
133 fseek(mime_stream,f_pos,SEEK_SET);
134 };
8e669ac1 135
8523533c
TK
136 /* no matches ... */
137 return FAIL;
138}
139
140
141int mime_regex(uschar **listptr) {
142 int sep = 0;
143 uschar *list = *listptr;
144 uschar *regex_string;
145 uschar regex_string_buffer[1024];
146 pcre *re;
147 pcre_list *re_list_head = NULL;
148 pcre_list *re_list_item;
149 const char *pcre_error;
150 int pcre_erroffset;
151 FILE *f;
152 uschar *mime_subject = NULL;
153 int mime_subject_len = 0;
154
155 /* reset expansion variable */
156 regex_match_string = NULL;
157
158 /* precompile our regexes */
159 while ((regex_string = string_nextinlist(&list, &sep,
160 regex_string_buffer,
161 sizeof(regex_string_buffer))) != NULL) {
8e669ac1 162
8523533c 163 /* parse option */
8e669ac1 164 if ( (strcmpic(regex_string,US"false") == 0) ||
8523533c
TK
165 (Ustrcmp(regex_string,"0") == 0) ) {
166 /* explicitly no matching */
167 continue;
168 };
8e669ac1 169
8523533c
TK
170 /* compile our regular expression */
171 re = pcre_compile( CS regex_string,
172 0,
173 &pcre_error,
174 &pcre_erroffset,
175 NULL );
176
177 if (re == NULL) {
178 log_write(0, LOG_MAIN,
179 "regex acl condition warning - error in regex '%s': %s at offset %d, skipped.", regex_string, pcre_error, pcre_erroffset);
180 continue;
181 }
182 else {
183 re_list_item = store_get(sizeof(pcre_list));
184 re_list_item->re = re;
185 re_list_item->pcre_text = string_copy(regex_string);
186 re_list_item->next = re_list_head;
187 re_list_head = re_list_item;
188 };
189 };
8e669ac1 190
8523533c
TK
191 /* no regexes -> nothing to do */
192 if (re_list_head == NULL) {
193 return FAIL;
194 };
8e669ac1 195
8523533c
TK
196 /* check if the file is already decoded */
197 if (mime_decoded_filename == NULL) {
198 uschar *empty = US"";
199 /* no, decode it first */
200 mime_decode(&empty);
201 if (mime_decoded_filename == NULL) {
202 /* decoding failed */
203 log_write(0, LOG_MAIN,
204 "mime_regex acl condition warning - could not decode MIME part to file.");
205 return DEFER;
206 };
207 };
208
209
210 /* open file */
c58b88df 211 f = fopen(CS mime_decoded_filename, "rb");
8523533c
TK
212 if (f == NULL) {
213 /* open failed */
214 log_write(0, LOG_MAIN,
215 "mime_regex acl condition warning - can't open '%s' for reading.", mime_decoded_filename);
216 return DEFER;
217 };
8e669ac1 218
8523533c
TK
219 /* get 32k memory */
220 mime_subject = (uschar *)store_get(32767);
8e669ac1 221
8523533c
TK
222 /* read max 32k chars from file */
223 mime_subject_len = fread(mime_subject, 1, 32766, f);
8e669ac1 224
8523533c
TK
225 re_list_item = re_list_head;
226 do {
227 /* try matcher on the mmapped file */
228 debug_printf("Matching '%s'\n", re_list_item->pcre_text);
229 if (pcre_exec(re_list_item->re, NULL, CS mime_subject,
230 mime_subject_len, 0, 0, NULL, 0) >= 0) {
231 Ustrncpy(regex_match_string_buffer, re_list_item->pcre_text, 1023);
232 regex_match_string = regex_match_string_buffer;
f1e894f3 233 (void)fclose(f);
8523533c
TK
234 return OK;
235 };
236 re_list_item = re_list_item->next;
237 } while (re_list_item != NULL);
238
f1e894f3 239 (void)fclose(f);
8e669ac1 240
8523533c
TK
241 /* no matches ... */
242 return FAIL;
243}
244
245#endif