Capture substrings in ACL regex= . Bug 425.
[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-2015 */
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 static pcre_list *
29 compile(const uschar * list)
30 {
31   int sep = 0;
32   uschar *regex_string;
33   uschar regex_string_buffer[1024];
34   const char *pcre_error;
35   int pcre_erroffset;
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
68 static int
69 matcher(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
97 int
98 regex(const uschar **listptr)
99 {
100   unsigned long mbox_size;
101   FILE *mbox_file;
102   pcre_list *re_list_head;
103   uschar *linebuffer;
104   long f_pos = 0;
105   int ret = FAIL;
106
107   /* reset expansion variable */
108   regex_match_string = NULL;
109
110   if (mime_stream == NULL) {                    /* We are in the DATA ACL */
111     mbox_file = spool_mbox(&mbox_size, NULL);
112     if (mbox_file == NULL) {                    /* error while spooling */
113       log_write(0, LOG_MAIN|LOG_PANIC,
114              "regex acl condition: error while creating mbox spool file");
115       return DEFER;
116     }
117   }
118   else {
119     f_pos = ftell(mime_stream);
120     mbox_file = mime_stream;
121   }
122
123   /* precompile our regexes */
124   if (!(re_list_head = compile(*listptr)))
125     return FAIL;                        /* no regexes -> nothing to do */
126
127   /* match each line against all regexes */
128   linebuffer = store_get(32767);
129   while (fgets(CS linebuffer, 32767, mbox_file) != NULL) {
130
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
141 done:
142   if (mime_stream == NULL)
143     (void)fclose(mbox_file);
144   else {
145     clearerr(mime_stream);
146     fseek(mime_stream,f_pos,SEEK_SET);
147   };
148
149   return ret;
150 }
151
152
153 int
154 mime_regex(const uschar **listptr)
155 {
156   pcre_list *re_list_head = NULL;
157   FILE *f;
158   uschar *mime_subject = NULL;
159   int mime_subject_len = 0;
160   int ret;
161
162   /* reset expansion variable */
163   regex_match_string = NULL;
164
165   /* precompile our regexes */
166   if (!(re_list_head = compile(*listptr)))
167     return FAIL;                        /* no regexes -> nothing to do */
168
169   /* check if the file is already decoded */
170   if (mime_decoded_filename == NULL) {
171     const uschar *empty = US"";
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;
179     }
180   }
181
182   /* open file */
183   if (!(f = fopen(CS mime_decoded_filename, "rb"))) {
184     log_write(0, LOG_MAIN,
185          "mime_regex acl condition warning - can't open '%s' for reading.",
186          mime_decoded_filename);
187     return DEFER;
188   }
189
190   /* get 32k memory */
191   mime_subject = (uschar *)store_get(32767);
192
193   mime_subject_len = fread(mime_subject, 1, 32766, f);
194
195   ret = matcher(re_list_head, mime_subject, mime_subject_len);
196   (void)fclose(f);
197   return ret;
198 }
199
200 #endif /* WITH_CONTENT_SCAN */