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