ClamAV INSTREAM scanning by default, unless built with WITH_OLD_CLAMAV_STREAM.
[exim.git] / src / src / spool_mbox.c
1 /* $Cambridge: exim/src/src/spool_mbox.c,v 1.15 2010/06/05 11:13:30 pdp Exp $ */
2
3 /*************************************************
4 *     Exim - an Internet mail transport agent    *
5 *************************************************/
6
7 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
8 /* License: GPL */
9
10 /* Code for setting up a MBOX style spool file inside a /scan/<msgid>
11 sub directory of exim's spool directory. */
12
13 #include "exim.h"
14 #ifdef WITH_CONTENT_SCAN
15
16 /* externals, we must reset them on unspooling */
17 #ifdef WITH_OLD_DEMIME
18 extern int demime_ok;
19 extern struct file_extension *file_extensions;
20 #endif
21
22 extern int malware_ok;
23 extern int spam_ok;
24
25 int spool_mbox_ok = 0;
26 uschar spooled_message_id[17];
27
28 /* returns a pointer to the FILE, and puts the size in bytes into mbox_file_size
29  * normally, source_file_override is NULL */
30
31 FILE *spool_mbox(unsigned long *mbox_file_size, uschar *source_file_override) {
32   uschar message_subdir[2];
33   uschar buffer[16384];
34   uschar *temp_string;
35   uschar *mbox_path;
36   FILE *mbox_file = NULL;
37   FILE *data_file = NULL;
38   FILE *yield = NULL;
39   header_line *my_headerlist;
40   struct stat statbuf;
41   int i, j;
42   void *reset_point = store_get(0);
43
44   mbox_path = string_sprintf("%s/scan/%s/%s.eml", spool_directory, message_id,
45     message_id);
46
47   /* Skip creation if already spooled out as mbox file */
48   if (!spool_mbox_ok) {
49     /* create temp directory inside scan dir, directory_make works recursively */
50     temp_string = string_sprintf("scan/%s", message_id);
51     if (!directory_make(spool_directory, temp_string, 0750, FALSE)) {
52       log_write(0, LOG_MAIN|LOG_PANIC, "%s", string_open_failed(errno,
53         "scan directory %s/scan/%s", spool_directory, temp_string));
54       goto OUT;
55     };
56
57     /* open [message_id].eml file for writing */
58     mbox_file = modefopen(mbox_path, "wb", SPOOL_MODE);
59     if (mbox_file == NULL) {
60       log_write(0, LOG_MAIN|LOG_PANIC, "%s", string_open_failed(errno,
61         "scan file %s", mbox_file));
62       goto OUT;
63     };
64
65     /* Generate mailbox headers. The $received_for variable is (up to at least
66     Exim 4.64) never set here, because it is only set when expanding the
67     contents of the Received: header line. However, the code below will use it
68     if it should become available in future. */
69
70     temp_string = expand_string(
71       US"From ${if def:return_path{$return_path}{MAILER-DAEMON}} ${tod_bsdinbox}\n"
72       "${if def:sender_address{X-Envelope-From: <${sender_address}>\n}}"
73       "${if def:recipients{X-Envelope-To: ${recipients}\n}}");
74
75     if (temp_string != NULL) {
76       i = fwrite(temp_string, Ustrlen(temp_string), 1, mbox_file);
77       if (i != 1) {
78         log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \
79             mailbox headers to %s", mbox_path);
80         goto OUT;
81       };
82     };
83
84     /* write all header lines to mbox file */
85     my_headerlist = header_list;
86     for (my_headerlist = header_list; my_headerlist != NULL;
87       my_headerlist = my_headerlist->next)
88     {
89       /* skip deleted headers */
90       if (my_headerlist->type == '*') continue;
91
92       i = fwrite(my_headerlist->text, my_headerlist->slen, 1, mbox_file);
93       if (i != 1) {
94         log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \
95             message headers to %s", mbox_path);
96         goto OUT;
97       };
98     };
99
100     /* End headers */
101     (void)fwrite("\n", 1, 1, mbox_file);
102
103     /* copy body file */
104     if (source_file_override == NULL) {
105       message_subdir[1] = '\0';
106       for (i = 0; i < 2; i++) {
107         message_subdir[0] = (split_spool_directory == (i == 0))? message_id[5] : 0;
108         temp_string = string_sprintf("%s/input/%s/%s-D", spool_directory,
109           message_subdir, message_id);
110         data_file = Ufopen(temp_string, "rb");
111         if (data_file != NULL) break;
112       };
113     } else {
114       data_file = Ufopen(source_file_override, "rb");
115     };
116
117     if (data_file == NULL) {
118       log_write(0, LOG_MAIN|LOG_PANIC, "Could not open datafile for message %s",
119         message_id);
120       goto OUT;
121     };
122
123     /* The code used to use this line, but it doesn't work in Cygwin.
124      *
125      *  (void)fread(data_buffer, 1, 18, data_file);
126      *
127      * What's happening is that spool_mbox used to use an fread to jump over the
128      * file header. That fails under Cygwin because the header is locked, but
129      * doing an fseek succeeds. We have to output the leading newline
130      * explicitly, because the one in the file is parted of the locked area.
131      */
132
133     if (!source_file_override)
134       (void)fseek(data_file, SPOOL_DATA_START_OFFSET, SEEK_SET);
135
136     do {
137       j = fread(buffer, 1, sizeof(buffer), data_file);
138
139       if (j > 0) {
140         i = fwrite(buffer, j, 1, mbox_file);
141         if (i != 1) {
142           log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \
143               message body to %s", mbox_path);
144           goto OUT;
145         };
146       };
147     } while (j > 0);
148
149     (void)fclose(mbox_file);
150     mbox_file = NULL;
151
152     Ustrcpy(spooled_message_id, message_id);
153     spool_mbox_ok = 1;
154   };
155
156   /* get the size of the mbox message and open [message_id].eml file for reading*/
157   if (Ustat(mbox_path, &statbuf) != 0 ||
158       (yield = Ufopen(mbox_path,"rb")) == NULL) {
159     log_write(0, LOG_MAIN|LOG_PANIC, "%s", string_open_failed(errno,
160       "scan file %s", mbox_file));
161     goto OUT;
162   };
163
164   *mbox_file_size = statbuf.st_size;
165
166   OUT:
167   if (data_file) (void)fclose(data_file);
168   if (mbox_file) (void)fclose(mbox_file);
169   store_reset(reset_point);
170   return yield;
171 }
172
173 /* remove mbox spool file, demimed files and temp directory */
174 void unspool_mbox(void) {
175
176   /* reset all exiscan state variables */
177   #ifdef WITH_OLD_DEMIME
178   demime_ok = 0;
179   demime_errorlevel = 0;
180   demime_reason = NULL;
181   file_extensions = NULL;
182   #endif
183
184   spam_ok = 0;
185   malware_ok = 0;
186
187   if (spool_mbox_ok && !no_mbox_unspool) {
188     uschar *mbox_path;
189     uschar *file_path;
190     int n;
191     struct dirent *entry;
192     DIR *tempdir;
193
194     mbox_path = string_sprintf("%s/scan/%s", spool_directory, spooled_message_id);
195
196     tempdir = opendir(CS mbox_path);
197     if (!tempdir) {
198       debug_printf("Unable to opendir(%s): %s\n", mbox_path, strerror(errno));
199       /* Just in case we still can: */
200       rmdir(CS mbox_path);
201       return;
202     }
203     /* loop thru dir & delete entries */
204     while((entry = readdir(tempdir)) != NULL) {
205       uschar *name = US entry->d_name;
206       if (Ustrcmp(name, US".") == 0 || Ustrcmp(name, US"..") == 0) continue;
207
208       file_path = string_sprintf("%s/%s", mbox_path, name);
209       debug_printf("unspool_mbox(): unlinking '%s'\n", file_path);
210       n = unlink(CS file_path);
211     };
212
213     closedir(tempdir);
214
215     /* remove directory */
216     rmdir(CS mbox_path);
217     store_reset(mbox_path);
218   };
219   spool_mbox_ok = 0;
220 }
221
222 #endif