1 /* $Cambridge: exim/src/src/spool_mbox.c,v 1.7 2005/06/27 14:29: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 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);
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);
59 mbox_file = Ufopen(mbox_path,"w");
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,"r");
133 if (data_file != NULL)
137 (void)fread(data_buffer, 1, 18, data_file);
140 j = fread(data_buffer, 1, sizeof(data_buffer), data_file);
142 i = fwrite(data_buffer, 1, j, mbox_file);
144 debug_printf("error/short write on writing in: %s", mbox_path);
145 (void)fclose(mbox_file);
146 (void)fclose(data_file);
152 (void)fclose(data_file);
153 (void)fclose(mbox_file);
154 Ustrcpy(spooled_message_id, message_id);
158 snprintf(CS mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
160 /* get the size of the mbox message */
161 stat(CS mbox_path, &statbuf);
162 *mbox_file_size = statbuf.st_size;
164 /* open [message_id].eml file for reading */
165 mbox_file = Ufopen(mbox_path,"r");
170 /* remove mbox spool file, demimed files and temp directory */
171 void unspool_mbox(void) {
173 /* reset all exiscan state variables */
174 #ifdef WITH_OLD_DEMIME
176 demime_errorlevel = 0;
177 demime_reason = NULL;
178 file_extensions = NULL;
188 if (!no_mbox_unspool) {
189 uschar mbox_path[1024];
190 uschar file_path[1024];
192 struct dirent *entry;
195 snprintf(CS mbox_path, 1024, "%s/scan/%s", spool_directory, spooled_message_id);
197 tempdir = opendir(CS mbox_path);
198 /* loop thru dir & delete entries */
201 entry = readdir(tempdir);
202 if (entry == NULL) break;
203 snprintf(CS file_path, 1024,"%s/scan/%s/%s", spool_directory, spooled_message_id, entry->d_name);
204 if ( (Ustrcmp(entry->d_name,"..") != 0) && (Ustrcmp(entry->d_name,".") != 0) ) {
205 debug_printf("unspool_mbox(): unlinking '%s'\n", file_path);
206 n = unlink(CS file_path);
212 /* remove directory */
213 n = rmdir(CS mbox_path);