Commit | Line | Data |
---|---|---|
21a04aa3 | 1 | /* $Cambridge: exim/src/src/spool_mbox.c,v 1.13 2006/09/22 08:41:59 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> | |
11 | sub 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 | |
18 | extern int demime_ok; | |
19 | extern struct file_extension *file_extensions; | |
20 | #endif | |
21 | ||
22 | extern int malware_ok; | |
23 | extern int spam_ok; | |
24 | ||
25 | int spool_mbox_ok = 0; | |
26 | uschar spooled_message_id[17]; | |
27 | ||
28 | /* returns a pointer to the FILE, and puts the size in bytes into mbox_file_size */ | |
29 | ||
f7b63901 | 30 | FILE *spool_mbox(unsigned long *mbox_file_size) { |
8523533c | 31 | uschar message_subdir[2]; |
f951fd57 PH |
32 | uschar buffer[16384]; |
33 | uschar *temp_string; | |
34 | uschar *mbox_path; | |
35 | FILE *mbox_file = NULL; | |
8523533c | 36 | FILE *data_file = NULL; |
f951fd57 | 37 | FILE *yield = NULL; |
8523533c TK |
38 | header_line *my_headerlist; |
39 | struct stat statbuf; | |
f951fd57 PH |
40 | int i, j; |
41 | void *reset_point = store_get(0); | |
8e669ac1 | 42 | |
f951fd57 PH |
43 | mbox_path = string_sprintf("%s/scan/%s/%s.eml", spool_directory, message_id, |
44 | message_id); | |
8e669ac1 | 45 | |
f951fd57 PH |
46 | /* Skip creation if already spooled out as mbox file */ |
47 | if (!spool_mbox_ok) { | |
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)); | |
53 | goto OUT; | |
8523533c | 54 | }; |
8e669ac1 | 55 | |
8523533c | 56 | /* open [message_id].eml file for writing */ |
f951fd57 | 57 | mbox_file = modefopen(mbox_path, "wb", SPOOL_MODE); |
8523533c | 58 | if (mbox_file == NULL) { |
f951fd57 PH |
59 | log_write(0, LOG_MAIN|LOG_PANIC, "%s", string_open_failed(errno, |
60 | "scan file %s", mbox_file)); | |
61 | goto OUT; | |
8523533c | 62 | }; |
8e669ac1 | 63 | |
f951fd57 PH |
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. */ | |
68 | ||
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}}"); | |
73 | ||
74 | if (temp_string != NULL) { | |
75 | i = fwrite(temp_string, Ustrlen(temp_string), 1, mbox_file); | |
76 | if (i != 1) { | |
77 | log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \ | |
78 | mailbox headers to %s", mbox_path); | |
79 | goto OUT; | |
7e274b4b TK |
80 | }; |
81 | }; | |
82 | ||
8523533c TK |
83 | /* write all header lines to mbox file */ |
84 | my_headerlist = header_list; | |
f951fd57 PH |
85 | for (my_headerlist = header_list; my_headerlist != NULL; |
86 | my_headerlist = my_headerlist->next) | |
87 | { | |
8523533c | 88 | /* skip deleted headers */ |
f951fd57 | 89 | if (my_headerlist->type == '*') continue; |
8e669ac1 | 90 | |
f951fd57 PH |
91 | i = fwrite(my_headerlist->text, my_headerlist->slen, 1, mbox_file); |
92 | if (i != 1) { | |
93 | log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \ | |
94 | message headers to %s", mbox_path); | |
95 | goto OUT; | |
8523533c | 96 | }; |
8523533c | 97 | }; |
8e669ac1 | 98 | |
f951fd57 PH |
99 | /* End headers */ |
100 | (void)fwrite("\n", 1, 1, mbox_file); | |
101 | ||
8523533c TK |
102 | /* copy body 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; | |
f951fd57 PH |
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; | |
110 | }; | |
111 | ||
112 | if (data_file == NULL) { | |
113 | log_write(0, LOG_MAIN|LOG_PANIC, "Could not open datafile for message %s", | |
114 | message_id); | |
115 | goto OUT; | |
8523533c TK |
116 | }; |
117 | ||
5b68f6e4 PH |
118 | /* The code used to use this line, but it doesn't work in Cygwin. |
119 | * | |
120 | * (void)fread(data_buffer, 1, 18, data_file); | |
121 | * | |
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. | |
126 | */ | |
127 | ||
5b68f6e4 | 128 | (void)fseek(data_file, SPOOL_DATA_START_OFFSET, SEEK_SET); |
8e669ac1 | 129 | |
8523533c | 130 | do { |
f951fd57 | 131 | j = fread(buffer, 1, sizeof(buffer), data_file); |
5b68f6e4 | 132 | |
8523533c | 133 | if (j > 0) { |
f951fd57 PH |
134 | i = fwrite(buffer, j, 1, mbox_file); |
135 | if (i != 1) { | |
136 | log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \ | |
137 | message body to %s", mbox_path); | |
138 | goto OUT; | |
8523533c TK |
139 | }; |
140 | }; | |
141 | } while (j > 0); | |
8e669ac1 | 142 | |
21a04aa3 PH |
143 | (void)fclose(mbox_file); |
144 | mbox_file = NULL; | |
145 | ||
8523533c TK |
146 | Ustrcpy(spooled_message_id, message_id); |
147 | spool_mbox_ok = 1; | |
148 | }; | |
149 | ||
f951fd57 PH |
150 | /* get the size of the mbox message and open [message_id].eml file for reading*/ |
151 | if (Ustat(mbox_path, &statbuf) != 0 || | |
152 | (yield = Ufopen(mbox_path,"rb")) == NULL) { | |
153 | log_write(0, LOG_MAIN|LOG_PANIC, "%s", string_open_failed(errno, | |
154 | "scan file %s", mbox_file)); | |
155 | goto OUT; | |
156 | }; | |
8523533c | 157 | |
8523533c TK |
158 | *mbox_file_size = statbuf.st_size; |
159 | ||
f951fd57 PH |
160 | OUT: |
161 | if (data_file) (void)fclose(data_file); | |
162 | if (mbox_file) (void)fclose(mbox_file); | |
163 | store_reset(reset_point); | |
164 | return yield; | |
8523533c TK |
165 | } |
166 | ||
167 | /* remove mbox spool file, demimed files and temp directory */ | |
168 | void unspool_mbox(void) { | |
169 | ||
170 | /* reset all exiscan state variables */ | |
171 | #ifdef WITH_OLD_DEMIME | |
172 | demime_ok = 0; | |
173 | demime_errorlevel = 0; | |
174 | demime_reason = NULL; | |
175 | file_extensions = NULL; | |
176 | #endif | |
8e669ac1 | 177 | |
8523533c TK |
178 | spam_ok = 0; |
179 | malware_ok = 0; | |
8e669ac1 | 180 | |
f951fd57 PH |
181 | if (spool_mbox_ok && !no_mbox_unspool) { |
182 | uschar *mbox_path; | |
183 | uschar *file_path; | |
184 | int n; | |
185 | struct dirent *entry; | |
186 | DIR *tempdir; | |
8523533c | 187 | |
f951fd57 | 188 | mbox_path = string_sprintf("%s/scan/%s", spool_directory, spooled_message_id); |
8e669ac1 | 189 | |
f951fd57 PH |
190 | tempdir = opendir(CS mbox_path); |
191 | /* loop thru dir & delete entries */ | |
192 | while((entry = readdir(tempdir)) != NULL) { | |
193 | uschar *name = US entry->d_name; | |
194 | if (Ustrcmp(name, US".") == 0 || Ustrcmp(name, US"..") == 0) continue; | |
8e669ac1 | 195 | |
f951fd57 | 196 | file_path = string_sprintf("%s/%s", mbox_path, name); |
384152a6 | 197 | debug_printf("unspool_mbox(): unlinking '%s'\n", file_path); |
f951fd57 PH |
198 | n = unlink(CS file_path); |
199 | }; | |
8e669ac1 | 200 | |
f951fd57 | 201 | closedir(tempdir); |
8e669ac1 | 202 | |
f951fd57 PH |
203 | /* remove directory */ |
204 | rmdir(CS mbox_path); | |
205 | store_reset(mbox_path); | |
8523533c | 206 | }; |
f951fd57 | 207 | spool_mbox_ok = 0; |
8523533c TK |
208 | } |
209 | ||
210 | #endif |