Commit | Line | Data |
---|---|---|
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> | |
9 | sub 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 | |
16 | extern int demime_ok; | |
17 | extern struct file_extension *file_extensions; | |
18 | #endif | |
19 | ||
20 | extern int malware_ok; | |
21 | extern int spam_ok; | |
22 | ||
23 | int spool_mbox_ok = 0; | |
24 | uschar 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 | |
0f0c8159 JH |
29 | FILE * |
30 | spool_mbox(unsigned long *mbox_file_size, const uschar *source_file_override) | |
31 | { | |
8523533c | 32 | uschar message_subdir[2]; |
f951fd57 PH |
33 | uschar buffer[16384]; |
34 | uschar *temp_string; | |
35 | uschar *mbox_path; | |
36 | FILE *mbox_file = NULL; | |
8523533c | 37 | FILE *data_file = NULL; |
f951fd57 | 38 | FILE *yield = NULL; |
8523533c TK |
39 | header_line *my_headerlist; |
40 | struct stat statbuf; | |
f951fd57 PH |
41 | int i, j; |
42 | void *reset_point = store_get(0); | |
8e669ac1 | 43 | |
f951fd57 PH |
44 | mbox_path = string_sprintf("%s/scan/%s/%s.eml", spool_directory, message_id, |
45 | message_id); | |
8e669ac1 | 46 | |
f951fd57 PH |
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; | |
8523533c | 55 | }; |
8e669ac1 | 56 | |
8523533c | 57 | /* open [message_id].eml file for writing */ |
f951fd57 | 58 | mbox_file = modefopen(mbox_path, "wb", SPOOL_MODE); |
8523533c | 59 | if (mbox_file == NULL) { |
f951fd57 | 60 | log_write(0, LOG_MAIN|LOG_PANIC, "%s", string_open_failed(errno, |
81f91683 | 61 | "scan file %s", mbox_path)); |
f951fd57 | 62 | goto OUT; |
8523533c | 63 | }; |
8e669ac1 | 64 | |
f951fd57 PH |
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}}" | |
c5537c6e | 73 | "${if def:recipients{X-Envelope-To: ${recipients}\n}}"); |
f951fd57 PH |
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; | |
7e274b4b TK |
81 | }; |
82 | }; | |
83 | ||
8523533c TK |
84 | /* write all header lines to mbox file */ |
85 | my_headerlist = header_list; | |
f951fd57 PH |
86 | for (my_headerlist = header_list; my_headerlist != NULL; |
87 | my_headerlist = my_headerlist->next) | |
88 | { | |
8523533c | 89 | /* skip deleted headers */ |
f951fd57 | 90 | if (my_headerlist->type == '*') continue; |
8e669ac1 | 91 | |
f951fd57 PH |
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; | |
8523533c | 97 | }; |
8523533c | 98 | }; |
8e669ac1 | 99 | |
f951fd57 | 100 | /* End headers */ |
1ac6b2e7 JH |
101 | if (fwrite("\n", 1, 1, mbox_file) != 1) { |
102 | log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \ | |
103 | message headers to %s", mbox_path); | |
104 | goto OUT; | |
105 | } | |
f951fd57 | 106 | |
8523533c | 107 | /* copy body file */ |
8544e77a PP |
108 | if (source_file_override == NULL) { |
109 | message_subdir[1] = '\0'; | |
110 | for (i = 0; i < 2; i++) { | |
111 | message_subdir[0] = (split_spool_directory == (i == 0))? message_id[5] : 0; | |
112 | temp_string = string_sprintf("%s/input/%s/%s-D", spool_directory, | |
113 | message_subdir, message_id); | |
114 | data_file = Ufopen(temp_string, "rb"); | |
115 | if (data_file != NULL) break; | |
116 | }; | |
117 | } else { | |
118 | data_file = Ufopen(source_file_override, "rb"); | |
f951fd57 PH |
119 | }; |
120 | ||
121 | if (data_file == NULL) { | |
122 | log_write(0, LOG_MAIN|LOG_PANIC, "Could not open datafile for message %s", | |
123 | message_id); | |
124 | goto OUT; | |
8523533c TK |
125 | }; |
126 | ||
5b68f6e4 PH |
127 | /* The code used to use this line, but it doesn't work in Cygwin. |
128 | * | |
129 | * (void)fread(data_buffer, 1, 18, data_file); | |
130 | * | |
131 | * What's happening is that spool_mbox used to use an fread to jump over the | |
132 | * file header. That fails under Cygwin because the header is locked, but | |
133 | * doing an fseek succeeds. We have to output the leading newline | |
134 | * explicitly, because the one in the file is parted of the locked area. | |
135 | */ | |
136 | ||
8544e77a PP |
137 | if (!source_file_override) |
138 | (void)fseek(data_file, SPOOL_DATA_START_OFFSET, SEEK_SET); | |
8e669ac1 | 139 | |
8523533c | 140 | do { |
f951fd57 | 141 | j = fread(buffer, 1, sizeof(buffer), data_file); |
5b68f6e4 | 142 | |
8523533c | 143 | if (j > 0) { |
f951fd57 PH |
144 | i = fwrite(buffer, j, 1, mbox_file); |
145 | if (i != 1) { | |
146 | log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \ | |
147 | message body to %s", mbox_path); | |
148 | goto OUT; | |
8523533c TK |
149 | }; |
150 | }; | |
151 | } while (j > 0); | |
8e669ac1 | 152 | |
21a04aa3 PH |
153 | (void)fclose(mbox_file); |
154 | mbox_file = NULL; | |
155 | ||
8523533c TK |
156 | Ustrcpy(spooled_message_id, message_id); |
157 | spool_mbox_ok = 1; | |
158 | }; | |
159 | ||
f951fd57 PH |
160 | /* get the size of the mbox message and open [message_id].eml file for reading*/ |
161 | if (Ustat(mbox_path, &statbuf) != 0 || | |
162 | (yield = Ufopen(mbox_path,"rb")) == NULL) { | |
163 | log_write(0, LOG_MAIN|LOG_PANIC, "%s", string_open_failed(errno, | |
81f91683 | 164 | "scan file %s", mbox_path)); |
f951fd57 PH |
165 | goto OUT; |
166 | }; | |
8523533c | 167 | |
8523533c TK |
168 | *mbox_file_size = statbuf.st_size; |
169 | ||
f951fd57 PH |
170 | OUT: |
171 | if (data_file) (void)fclose(data_file); | |
172 | if (mbox_file) (void)fclose(mbox_file); | |
173 | store_reset(reset_point); | |
174 | return yield; | |
8523533c TK |
175 | } |
176 | ||
177 | /* remove mbox spool file, demimed files and temp directory */ | |
178 | void unspool_mbox(void) { | |
179 | ||
180 | /* reset all exiscan state variables */ | |
181 | #ifdef WITH_OLD_DEMIME | |
182 | demime_ok = 0; | |
183 | demime_errorlevel = 0; | |
184 | demime_reason = NULL; | |
185 | file_extensions = NULL; | |
186 | #endif | |
8e669ac1 | 187 | |
8523533c TK |
188 | spam_ok = 0; |
189 | malware_ok = 0; | |
8e669ac1 | 190 | |
f951fd57 PH |
191 | if (spool_mbox_ok && !no_mbox_unspool) { |
192 | uschar *mbox_path; | |
193 | uschar *file_path; | |
194 | int n; | |
195 | struct dirent *entry; | |
196 | DIR *tempdir; | |
8523533c | 197 | |
f951fd57 | 198 | mbox_path = string_sprintf("%s/scan/%s", spool_directory, spooled_message_id); |
8e669ac1 | 199 | |
f951fd57 | 200 | tempdir = opendir(CS mbox_path); |
8544e77a PP |
201 | if (!tempdir) { |
202 | debug_printf("Unable to opendir(%s): %s\n", mbox_path, strerror(errno)); | |
203 | /* Just in case we still can: */ | |
204 | rmdir(CS mbox_path); | |
205 | return; | |
206 | } | |
f951fd57 PH |
207 | /* loop thru dir & delete entries */ |
208 | while((entry = readdir(tempdir)) != NULL) { | |
209 | uschar *name = US entry->d_name; | |
210 | if (Ustrcmp(name, US".") == 0 || Ustrcmp(name, US"..") == 0) continue; | |
8e669ac1 | 211 | |
f951fd57 | 212 | file_path = string_sprintf("%s/%s", mbox_path, name); |
384152a6 | 213 | debug_printf("unspool_mbox(): unlinking '%s'\n", file_path); |
f951fd57 PH |
214 | n = unlink(CS file_path); |
215 | }; | |
8e669ac1 | 216 | |
f951fd57 | 217 | closedir(tempdir); |
8e669ac1 | 218 | |
f951fd57 PH |
219 | /* remove directory */ |
220 | rmdir(CS mbox_path); | |
221 | store_reset(mbox_path); | |
8523533c | 222 | }; |
f951fd57 | 223 | spool_mbox_ok = 0; |
8523533c TK |
224 | } |
225 | ||
226 | #endif |