1 /* $Cambridge: exim/src/src/spool_mbox.c,v 1.11 2006/02/22 14:46:44 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 mbox_path[1024];
32 uschar message_subdir[2];
33 uschar data_buffer[65535];
35 FILE *data_file = NULL;
36 header_line *my_headerlist;
39 uschar *mbox_delimiter;
40 uschar *envelope_from;
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);
50 /* create temp directory inside scan dir */
51 (void)string_format(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);
57 /* open [message_id].eml file for writing */
58 (void)string_format(mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
59 mbox_file = modefopen(mbox_path,"wb",SPOOL_MODE);
61 if (mbox_file == NULL) {
62 debug_printf("unable to open file for writing: %s\n", mbox_path);
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);
73 (void)fclose(mbox_file);
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);
87 (void)fclose(mbox_file);
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);
101 (void)fclose(mbox_file);
107 /* write all header lines to mbox file */
108 my_headerlist = header_list;
109 while (my_headerlist != NULL) {
111 /* skip deleted headers */
112 if (my_headerlist->type == '*') {
113 my_headerlist = my_headerlist->next;
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);
120 (void)fclose(mbox_file);
124 my_headerlist = my_headerlist->next;
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);
132 data_file = Ufopen(mbox_path,"rb");
133 if (data_file != NULL)
137 /* The code used to use this line, but it doesn't work in Cygwin.
139 * (void)fread(data_buffer, 1, 18, data_file);
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.
147 (void)fwrite("\n", 1, 1, mbox_file);
148 (void)fseek(data_file, SPOOL_DATA_START_OFFSET, SEEK_SET);
151 j = fread(data_buffer, 1, sizeof(data_buffer), data_file);
154 i = fwrite(data_buffer, 1, j, mbox_file);
156 debug_printf("error/short write on writing in: %s", mbox_path);
157 (void)fclose(mbox_file);
158 (void)fclose(data_file);
164 (void)fclose(data_file);
165 (void)fclose(mbox_file);
166 Ustrcpy(spooled_message_id, message_id);
170 (void)string_format(mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
172 /* get the size of the mbox message */
173 stat(CS mbox_path, &statbuf);
174 *mbox_file_size = statbuf.st_size;
176 /* open [message_id].eml file for reading */
177 mbox_file = Ufopen(mbox_path,"rb");
182 /* remove mbox spool file, demimed files and temp directory */
183 void unspool_mbox(void) {
185 /* reset all exiscan state variables */
186 #ifdef WITH_OLD_DEMIME
188 demime_errorlevel = 0;
189 demime_reason = NULL;
190 file_extensions = NULL;
200 if (!no_mbox_unspool) {
201 uschar mbox_path[1024];
202 uschar file_path[1024];
204 struct dirent *entry;
207 (void)string_format(mbox_path, 1024, "%s/scan/%s", spool_directory, spooled_message_id);
209 tempdir = opendir(CS mbox_path);
210 /* loop thru dir & delete entries */
213 entry = readdir(tempdir);
214 if (entry == NULL) break;
215 (void)string_format(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);
218 n = unlink(CS file_path);
224 /* remove directory */
225 n = rmdir(CS mbox_path);