Use fseek() instead of fread() to skip the body file header line, so
[exim.git] / src / src / spool_mbox.c
CommitLineData
5b68f6e4 1/* $Cambridge: exim/src/src/spool_mbox.c,v 1.9 2005/08/01 13:51:05 ph10 Exp $ */
8523533c
TK
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>
11sub 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
18extern int demime_ok;
19extern struct file_extension *file_extensions;
20#endif
21
22extern int malware_ok;
23extern int spam_ok;
24
25int spool_mbox_ok = 0;
26uschar spooled_message_id[17];
27
28/* returns a pointer to the FILE, and puts the size in bytes into mbox_file_size */
29
f7b63901 30FILE *spool_mbox(unsigned long *mbox_file_size) {
8523533c
TK
31 uschar mbox_path[1024];
32 uschar message_subdir[2];
33 uschar data_buffer[65535];
34 FILE *mbox_file;
35 FILE *data_file = NULL;
36 header_line *my_headerlist;
37 struct stat statbuf;
38 int i,j;
7e274b4b
TK
39 uschar *mbox_delimiter;
40 uschar *envelope_from;
41 uschar *envelope_to;
8e669ac1 42
8523533c
TK
43 if (!spool_mbox_ok) {
44 /* create scan directory, if not present */
45 if (!directory_make(spool_directory, US "scan", 0750, FALSE)) {
46 debug_printf("unable to create directory: %s/scan\n", spool_directory);
47 return NULL;
48 };
8e669ac1 49
8523533c
TK
50 /* create temp directory inside scan dir */
51 snprintf(CS mbox_path, 1024, "%s/scan/%s", spool_directory, message_id);
52 if (!directory_make(NULL, mbox_path, 0750, FALSE)) {
53 debug_printf("unable to create directory: %s/scan/%s\n", spool_directory, message_id);
54 return NULL;
55 };
8e669ac1 56
8523533c
TK
57 /* open [message_id].eml file for writing */
58 snprintf(CS mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
c58b88df 59 mbox_file = Ufopen(mbox_path,"wb");
8e669ac1 60
8523533c
TK
61 if (mbox_file == NULL) {
62 debug_printf("unable to open file for writing: %s\n", mbox_path);
63 return NULL;
64 };
8e669ac1 65
7e274b4b
TK
66 /* Generate mailbox delimiter */
67 mbox_delimiter = expand_string(US"From ${sender_address} ${tod_bsdinbox}\n");
68 if (mbox_delimiter != NULL) {
69 if (mbox_delimiter[0] != 0) {
70 i = fwrite(mbox_delimiter, 1, Ustrlen(mbox_delimiter), mbox_file);
71 if (i != Ustrlen(mbox_delimiter)) {
72 debug_printf("error/short write on writing in: %s", mbox_path);
f1e894f3 73 (void)fclose(mbox_file);
7e274b4b
TK
74 return NULL;
75 };
76 };
77 };
78 /* Generate X-Envelope-From header */
79 envelope_from = expand_string(US"${sender_address}");
80 if (envelope_from != NULL) {
81 if (envelope_from[0] != 0) {
82 uschar *my_envelope_from;
83 my_envelope_from = string_sprintf("X-Envelope-From: <%s>\n", envelope_from);
84 i = fwrite(my_envelope_from, 1, Ustrlen(my_envelope_from), mbox_file);
85 if (i != Ustrlen(my_envelope_from)) {
86 debug_printf("error/short write on writing in: %s", mbox_path);
f1e894f3 87 (void)fclose(mbox_file);
7e274b4b
TK
88 return NULL;
89 };
90 };
91 };
92 /* Generate X-Envelope-To header */
93 envelope_to = expand_string(US"${if def:received_for{$received_for}}");
94 if (envelope_to != NULL) {
95 if (envelope_to[0] != 0) {
96 uschar *my_envelope_to;
97 my_envelope_to = string_sprintf("X-Envelope-To: <%s>\n", envelope_to);
98 i = fwrite(my_envelope_to, 1, Ustrlen(my_envelope_to), mbox_file);
99 if (i != Ustrlen(my_envelope_to)) {
100 debug_printf("error/short write on writing in: %s", mbox_path);
f1e894f3 101 (void)fclose(mbox_file);
7e274b4b
TK
102 return NULL;
103 };
104 };
105 };
106
8523533c
TK
107 /* write all header lines to mbox file */
108 my_headerlist = header_list;
109 while (my_headerlist != NULL) {
8e669ac1 110
8523533c
TK
111 /* skip deleted headers */
112 if (my_headerlist->type == '*') {
113 my_headerlist = my_headerlist->next;
114 continue;
115 };
8e669ac1 116
8523533c
TK
117 i = fwrite(my_headerlist->text, 1, my_headerlist->slen, mbox_file);
118 if (i != my_headerlist->slen) {
119 debug_printf("error/short write on writing in: %s", mbox_path);
f1e894f3 120 (void)fclose(mbox_file);
8523533c
TK
121 return NULL;
122 };
8e669ac1 123
8523533c
TK
124 my_headerlist = my_headerlist->next;
125 };
8e669ac1 126
8523533c
TK
127 /* copy body file */
128 message_subdir[1] = '\0';
129 for (i = 0; i < 2; i++) {
130 message_subdir[0] = (split_spool_directory == (i == 0))? message_id[5] : 0;
131 sprintf(CS mbox_path, "%s/input/%s/%s-D", spool_directory, message_subdir, message_id);
c58b88df 132 data_file = Ufopen(mbox_path,"rb");
8523533c
TK
133 if (data_file != NULL)
134 break;
135 };
136
5b68f6e4
PH
137 /* The code used to use this line, but it doesn't work in Cygwin.
138 *
139 * (void)fread(data_buffer, 1, 18, data_file);
140 *
141 * What's happening is that spool_mbox used to use an fread to jump over the
142 * file header. That fails under Cygwin because the header is locked, but
143 * doing an fseek succeeds. We have to output the leading newline
144 * explicitly, because the one in the file is parted of the locked area.
145 */
146
147 (void)fwrite("\n", 1, 1, mbox_file);
148 (void)fseek(data_file, SPOOL_DATA_START_OFFSET, SEEK_SET);
8e669ac1 149
8523533c
TK
150 do {
151 j = fread(data_buffer, 1, sizeof(data_buffer), data_file);
5b68f6e4 152
8523533c
TK
153 if (j > 0) {
154 i = fwrite(data_buffer, 1, j, mbox_file);
155 if (i != j) {
156 debug_printf("error/short write on writing in: %s", mbox_path);
f1e894f3
PH
157 (void)fclose(mbox_file);
158 (void)fclose(data_file);
8523533c
TK
159 return NULL;
160 };
161 };
162 } while (j > 0);
8e669ac1 163
f1e894f3
PH
164 (void)fclose(data_file);
165 (void)fclose(mbox_file);
8523533c
TK
166 Ustrcpy(spooled_message_id, message_id);
167 spool_mbox_ok = 1;
168 };
169
170 snprintf(CS mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
171
172 /* get the size of the mbox message */
173 stat(CS mbox_path, &statbuf);
174 *mbox_file_size = statbuf.st_size;
175
176 /* open [message_id].eml file for reading */
c58b88df 177 mbox_file = Ufopen(mbox_path,"rb");
8e669ac1 178
8523533c
TK
179 return mbox_file;
180}
181
182/* remove mbox spool file, demimed files and temp directory */
183void unspool_mbox(void) {
184
185 /* reset all exiscan state variables */
186 #ifdef WITH_OLD_DEMIME
187 demime_ok = 0;
188 demime_errorlevel = 0;
189 demime_reason = NULL;
190 file_extensions = NULL;
191 #endif
8e669ac1 192
8523533c
TK
193 spam_ok = 0;
194 malware_ok = 0;
8e669ac1 195
8523533c
TK
196 if (spool_mbox_ok) {
197
198 spool_mbox_ok = 0;
8e669ac1 199
8523533c
TK
200 if (!no_mbox_unspool) {
201 uschar mbox_path[1024];
202 uschar file_path[1024];
203 int n;
204 struct dirent *entry;
205 DIR *tempdir;
8e669ac1 206
8523533c 207 snprintf(CS mbox_path, 1024, "%s/scan/%s", spool_directory, spooled_message_id);
8e669ac1 208
384152a6
TK
209 tempdir = opendir(CS mbox_path);
210 /* loop thru dir & delete entries */
211 n = 0;
212 do {
213 entry = readdir(tempdir);
214 if (entry == NULL) break;
215 snprintf(CS file_path, 1024,"%s/scan/%s/%s", spool_directory, spooled_message_id, entry->d_name);
216 if ( (Ustrcmp(entry->d_name,"..") != 0) && (Ustrcmp(entry->d_name,".") != 0) ) {
217 debug_printf("unspool_mbox(): unlinking '%s'\n", file_path);
8523533c 218 n = unlink(CS file_path);
8e669ac1 219 };
384152a6 220 } while (n > -1);
8e669ac1 221
384152a6 222 closedir(tempdir);
8e669ac1 223
384152a6
TK
224 /* remove directory */
225 n = rmdir(CS mbox_path);
8523533c
TK
226 };
227 };
228}
229
230#endif