Do RE compilations at daemon startup. Bug 1568
[exim.git] / src / src / regex.c
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 */
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
28 int regex(uschar **listptr) {
29   int sep = 0;
30   uschar *list = *listptr;
31   uschar *regex_string;
32   uschar regex_string_buffer[1024];
33   unsigned long mbox_size;
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;
42
43   /* reset expansion variable */
44   regex_match_string = NULL;
45
46   if (mime_stream == NULL) {
47     /* We are in the DATA ACL */
48     mbox_file = spool_mbox(&mbox_size, NULL);
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   };
60
61   /* precompile our regexes */
62   while ((regex_string = string_nextinlist(&list, &sep,
63                                            regex_string_buffer,
64                                            sizeof(regex_string_buffer))) != NULL) {
65
66     /* parse option */
67     if ( (strcmpic(regex_string,US"false") == 0) ||
68          (Ustrcmp(regex_string,"0") == 0) ) {
69       /* explicitly no matching */
70       continue;
71     };
72
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   };
93
94   /* no regexes -> nothing to do */
95   if (re_list_head == NULL) {
96     return FAIL;
97   };
98
99   /* match each line against all regexes */
100   linebuffer = store_get(32767);
101   while (fgets(CS linebuffer, 32767, mbox_file) != NULL) {
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,
114       (int)Ustrlen(linebuffer), 0, 0, NULL, 0) >= 0) {
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)
118           (void)fclose(mbox_file);
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   };
128
129   if (mime_stream == NULL)
130     (void)fclose(mbox_file);
131   else {
132     clearerr(mime_stream);
133     fseek(mime_stream,f_pos,SEEK_SET);
134   };
135
136   /* no matches ... */
137   return FAIL;
138 }
139
140
141 int 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) {
162
163     /* parse option */
164     if ( (strcmpic(regex_string,US"false") == 0) ||
165          (Ustrcmp(regex_string,"0") == 0) ) {
166       /* explicitly no matching */
167       continue;
168     };
169
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   };
190
191   /* no regexes -> nothing to do */
192   if (re_list_head == NULL) {
193     return FAIL;
194   };
195
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 */
211   f = fopen(CS mime_decoded_filename, "rb");
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   };
218
219   /* get 32k memory */
220   mime_subject = (uschar *)store_get(32767);
221
222   /* read max 32k chars from file */
223   mime_subject_len = fread(mime_subject, 1, 32766, f);
224
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;
233       (void)fclose(f);
234       return OK;
235     };
236     re_list_item = re_list_item->next;
237   } while (re_list_item != NULL);
238
239   (void)fclose(f);
240
241   /* no matches ... */
242   return FAIL;
243 }
244
245 #endif /* WITH_CONTENT_SCAN */