1 /* $Cambridge: exim/src/src/spool_mbox.c,v 1.12 2006/09/12 10:35:56 ph10 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
10 /* Code for setting up a MBOX style spool file inside a /scan/<msgid>
11 sub directory of exim's spool directory. */
14 #ifdef WITH_CONTENT_SCAN
16 /* externals, we must reset them on unspooling */
17 #ifdef WITH_OLD_DEMIME
19 extern struct file_extension
*file_extensions
;
22 extern int malware_ok
;
25 int spool_mbox_ok
= 0;
26 uschar spooled_message_id
[17];
28 /* returns a pointer to the FILE, and puts the size in bytes into mbox_file_size */
30 FILE *spool_mbox(unsigned long *mbox_file_size
) {
31 uschar message_subdir
[2];
35 FILE *mbox_file
= NULL
;
36 FILE *data_file
= NULL
;
38 header_line
*my_headerlist
;
41 void *reset_point
= store_get(0);
43 mbox_path
= string_sprintf("%s/scan/%s/%s.eml", spool_directory
, message_id
,
46 /* Skip creation if already spooled out as mbox file */
48 /* create temp directory inside scan dir, directory_make works recursively */
49 temp_string
= string_sprintf("scan/%s", message_id
);
50 if (!directory_make(spool_directory
, temp_string
, 0750, FALSE
)) {
51 log_write(0, LOG_MAIN
|LOG_PANIC
, "%s", string_open_failed(errno
,
52 "scan directory %s/scan/%s", spool_directory
, temp_string
));
56 /* open [message_id].eml file for writing */
57 mbox_file
= modefopen(mbox_path
, "wb", SPOOL_MODE
);
58 if (mbox_file
== NULL
) {
59 log_write(0, LOG_MAIN
|LOG_PANIC
, "%s", string_open_failed(errno
,
60 "scan file %s", mbox_file
));
64 /* Generate mailbox headers. The $received_for variable is (up to at least
65 Exim 4.64) never set here, because it is only set when expanding the
66 contents of the Received: header line. However, the code below will use it
67 if it should become available in future. */
69 temp_string
= expand_string(
70 US
"From ${if def:return_path{$return_path}{MAILER-DAEMON}} ${tod_bsdinbox}\n"
71 "${if def:sender_address{X-Envelope-From: <${sender_address}>\n}}"
72 "${if def:received_for{X-Envelope-To: <${received_for}>\n}}");
74 if (temp_string
!= NULL
) {
75 i
= fwrite(temp_string
, Ustrlen(temp_string
), 1, mbox_file
);
77 log_write(0, LOG_MAIN
|LOG_PANIC
, "Error/short write while writing \
78 mailbox headers to %s", mbox_path
);
83 /* write all header lines to mbox file */
84 my_headerlist
= header_list
;
85 for (my_headerlist
= header_list
; my_headerlist
!= NULL
;
86 my_headerlist
= my_headerlist
->next
)
88 /* skip deleted headers */
89 if (my_headerlist
->type
== '*') continue;
91 i
= fwrite(my_headerlist
->text
, my_headerlist
->slen
, 1, mbox_file
);
93 log_write(0, LOG_MAIN
|LOG_PANIC
, "Error/short write while writing \
94 message headers to %s", mbox_path
);
100 (void)fwrite("\n", 1, 1, mbox_file
);
103 message_subdir
[1] = '\0';
104 for (i
= 0; i
< 2; i
++) {
105 message_subdir
[0] = (split_spool_directory
== (i
== 0))? message_id
[5] : 0;
106 temp_string
= string_sprintf("%s/input/%s/%s-D", spool_directory
,
107 message_subdir
, message_id
);
108 data_file
= Ufopen(temp_string
, "rb");
109 if (data_file
!= NULL
) break;
112 if (data_file
== NULL
) {
113 log_write(0, LOG_MAIN
|LOG_PANIC
, "Could not open datafile for message %s",
118 /* The code used to use this line, but it doesn't work in Cygwin.
120 * (void)fread(data_buffer, 1, 18, data_file);
122 * What's happening is that spool_mbox used to use an fread to jump over the
123 * file header. That fails under Cygwin because the header is locked, but
124 * doing an fseek succeeds. We have to output the leading newline
125 * explicitly, because the one in the file is parted of the locked area.
128 (void)fseek(data_file
, SPOOL_DATA_START_OFFSET
, SEEK_SET
);
131 j
= fread(buffer
, 1, sizeof(buffer
), data_file
);
134 i
= fwrite(buffer
, j
, 1, mbox_file
);
136 log_write(0, LOG_MAIN
|LOG_PANIC
, "Error/short write while writing \
137 message body to %s", mbox_path
);
143 Ustrcpy(spooled_message_id
, message_id
);
147 /* get the size of the mbox message and open [message_id].eml file for reading*/
148 if (Ustat(mbox_path
, &statbuf
) != 0 ||
149 (yield
= Ufopen(mbox_path
,"rb")) == NULL
) {
150 log_write(0, LOG_MAIN
|LOG_PANIC
, "%s", string_open_failed(errno
,
151 "scan file %s", mbox_file
));
155 *mbox_file_size
= statbuf
.st_size
;
158 if (data_file
) (void)fclose(data_file
);
159 if (mbox_file
) (void)fclose(mbox_file
);
160 store_reset(reset_point
);
164 /* remove mbox spool file, demimed files and temp directory */
165 void unspool_mbox(void) {
167 /* reset all exiscan state variables */
168 #ifdef WITH_OLD_DEMIME
170 demime_errorlevel
= 0;
171 demime_reason
= NULL
;
172 file_extensions
= NULL
;
178 if (spool_mbox_ok
&& !no_mbox_unspool
) {
182 struct dirent
*entry
;
185 mbox_path
= string_sprintf("%s/scan/%s", spool_directory
, spooled_message_id
);
187 tempdir
= opendir(CS mbox_path
);
188 /* loop thru dir & delete entries */
189 while((entry
= readdir(tempdir
)) != NULL
) {
190 uschar
*name
= US entry
->d_name
;
191 if (Ustrcmp(name
, US
".") == 0 || Ustrcmp(name
, US
"..") == 0) continue;
193 file_path
= string_sprintf("%s/%s", mbox_path
, name
);
194 debug_printf("unspool_mbox(): unlinking '%s'\n", file_path
);
195 n
= unlink(CS file_path
);
200 /* remove directory */
202 store_reset(mbox_path
);