Use %ld not %l
[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  * Copyright (c) The Exim Maintainers 2016 - 2018
8  */
9
10 /* Code for matching regular expressions against headers and body.
11  Called from acl.c. */
12
13 #include "exim.h"
14 #ifdef WITH_CONTENT_SCAN
15 #include <unistd.h>
16 #include <sys/mman.h>
17
18 /* Structure to hold a list of Regular expressions */
19 typedef struct pcre_list {
20   pcre2_code *re;
21   uschar *pcre_text;
22   struct pcre_list *next;
23 } pcre_list;
24
25 uschar regex_match_string_buffer[1024];
26
27 extern FILE *mime_stream;
28 extern uschar *mime_current_boundary;
29
30 static pcre_list *
31 compile(const uschar * list)
32 {
33 int sep = 0;
34 uschar *regex_string;
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     {
42     pcre2_code * re;
43     int err;
44     PCRE2_SIZE pcre_erroffset;
45
46     /* compile our regular expression */
47     if (!(re = pcre2_compile( (PCRE2_SPTR) regex_string, PCRE2_ZERO_TERMINATED,
48                   0, &err, &pcre_erroffset, pcre_cmp_ctx)))
49       {
50       uschar errbuf[128];
51       pcre2_get_error_message(err, errbuf, sizeof(errbuf));
52       log_write(0, LOG_MAIN,
53            "regex acl condition warning - error in regex '%s': %s at offset %ld, skipped.",
54            regex_string, errbuf, (long)pcre_erroffset);
55       continue;
56       }
57
58     ri = store_get(sizeof(pcre_list), FALSE);
59     ri->re = re;
60     ri->pcre_text = regex_string;
61     ri->next = re_list_head;
62     re_list_head = ri;
63     }
64 return re_list_head;
65 }
66
67 static int
68 matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
69 {
70 pcre2_match_data * md = pcre2_match_data_create(REGEX_VARS + 1, pcre_gen_ctx);
71
72 for (pcre_list * ri = re_list_head; ri; ri = ri->next)
73   {
74   int n;
75
76   /* try matcher on the line */
77   if ((n = pcre2_match(ri->re, (PCRE2_SPTR)linebuffer, len, 0, 0, md, pcre_mtc_ctx)) > 0)
78     {
79     Ustrncpy(regex_match_string_buffer, ri->pcre_text,
80               sizeof(regex_match_string_buffer)-1);
81     regex_match_string = regex_match_string_buffer;
82
83     for (int nn = 1; nn < n; nn++)
84       {
85       PCRE2_UCHAR * cstr;
86       PCRE2_SIZE cslen;
87       pcre2_substring_get_bynumber(md, nn, &cstr, &cslen);
88       regex_vars[nn-1] = US cstr;
89       }
90
91     return OK;
92     }
93   }
94 pcre2_match_data_free(md);
95 return FAIL;
96 }
97
98 int
99 regex(const uschar **listptr)
100 {
101 unsigned long mbox_size;
102 FILE *mbox_file;
103 pcre_list *re_list_head;
104 uschar *linebuffer;
105 long f_pos = 0;
106 int ret = FAIL;
107
108 /* reset expansion variable */
109 regex_match_string = NULL;
110
111 if (!mime_stream)                               /* We are in the DATA ACL */
112   {
113   if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
114     {                                           /* error while spooling */
115     log_write(0, LOG_MAIN|LOG_PANIC,
116            "regex acl condition: error while creating mbox spool file");
117     return DEFER;
118     }
119   }
120 else
121   {
122   if ((f_pos = ftell(mime_stream)) < 0)
123     {
124     log_write(0, LOG_MAIN|LOG_PANIC,
125            "regex acl condition: mime_stream: %s", strerror(errno));
126     return DEFER;
127     }
128   mbox_file = mime_stream;
129   }
130
131 /* precompile our regexes */
132 if (!(re_list_head = compile(*listptr)))
133   return FAIL;                  /* no regexes -> nothing to do */
134
135 /* match each line against all regexes */
136 linebuffer = store_get(32767, TRUE);    /* tainted */
137 while (fgets(CS linebuffer, 32767, mbox_file))
138   {
139   if (  mime_stream && mime_current_boundary            /* check boundary */
140      && Ustrncmp(linebuffer, "--", 2) == 0
141      && Ustrncmp((linebuffer+2), mime_current_boundary,
142                   Ustrlen(mime_current_boundary)) == 0)
143       break;                                            /* found boundary */
144
145   if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
146     goto done;
147   }
148 /* no matches ... */
149
150 done:
151 if (!mime_stream)
152   (void)fclose(mbox_file);
153 else
154   {
155   clearerr(mime_stream);
156   if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
157     {
158     log_write(0, LOG_MAIN|LOG_PANIC,
159            "regex acl condition: mime_stream: %s", strerror(errno));
160     clearerr(mime_stream);
161     }
162   }
163
164 return ret;
165 }
166
167
168 int
169 mime_regex(const uschar **listptr)
170 {
171 pcre_list *re_list_head = NULL;
172 FILE *f;
173 uschar *mime_subject = NULL;
174 int mime_subject_len = 0;
175 int ret;
176
177 /* reset expansion variable */
178 regex_match_string = NULL;
179
180 /* precompile our regexes */
181 if (!(re_list_head = compile(*listptr)))
182   return FAIL;                  /* no regexes -> nothing to do */
183
184 /* check if the file is already decoded */
185 if (!mime_decoded_filename)
186   {                             /* no, decode it first */
187   const uschar *empty = US"";
188   mime_decode(&empty);
189   if (!mime_decoded_filename)
190     {                           /* decoding failed */
191     log_write(0, LOG_MAIN,
192        "mime_regex acl condition warning - could not decode MIME part to file");
193     return DEFER;
194     }
195   }
196
197 /* open file */
198 if (!(f = fopen(CS mime_decoded_filename, "rb")))
199   {
200   log_write(0, LOG_MAIN,
201        "mime_regex acl condition warning - can't open '%s' for reading",
202        mime_decoded_filename);
203   return DEFER;
204   }
205
206 /* get 32k memory, tainted */
207 mime_subject = store_get(32767, TRUE);
208
209 mime_subject_len = fread(mime_subject, 1, 32766, f);
210
211 ret = matcher(re_list_head, mime_subject, mime_subject_len);
212 (void)fclose(f);
213 return ret;
214 }
215
216 #endif /* WITH_CONTENT_SCAN */