| 1 | /************************************************* |
| 2 | * Exim - an Internet mail transport agent * |
| 3 | *************************************************/ |
| 4 | |
| 5 | /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */ |
| 6 | /* License: GPL */ |
| 7 | |
| 8 | /* Code for setting up a MBOX style spool file inside a /scan/<msgid> |
| 9 | sub directory of exim's spool directory. */ |
| 10 | |
| 11 | #include "exim.h" |
| 12 | #ifdef WITH_CONTENT_SCAN |
| 13 | |
| 14 | /* externals, we must reset them on unspooling */ |
| 15 | #ifdef WITH_OLD_DEMIME |
| 16 | extern int demime_ok; |
| 17 | extern struct file_extension *file_extensions; |
| 18 | #endif |
| 19 | |
| 20 | extern int malware_ok; |
| 21 | extern int spam_ok; |
| 22 | |
| 23 | int spool_mbox_ok = 0; |
| 24 | uschar spooled_message_id[17]; |
| 25 | |
| 26 | /* returns a pointer to the FILE, and puts the size in bytes into mbox_file_size |
| 27 | * normally, source_file_override is NULL */ |
| 28 | |
| 29 | FILE *spool_mbox(unsigned long *mbox_file_size, uschar *source_file_override) { |
| 30 | uschar message_subdir[2]; |
| 31 | uschar buffer[16384]; |
| 32 | uschar *temp_string; |
| 33 | uschar *mbox_path; |
| 34 | FILE *mbox_file = NULL; |
| 35 | FILE *data_file = NULL; |
| 36 | FILE *yield = NULL; |
| 37 | header_line *my_headerlist; |
| 38 | struct stat statbuf; |
| 39 | int i, j; |
| 40 | void *reset_point = store_get(0); |
| 41 | |
| 42 | mbox_path = string_sprintf("%s/scan/%s/%s.eml", spool_directory, message_id, |
| 43 | message_id); |
| 44 | |
| 45 | /* Skip creation if already spooled out as mbox file */ |
| 46 | if (!spool_mbox_ok) { |
| 47 | /* create temp directory inside scan dir, directory_make works recursively */ |
| 48 | temp_string = string_sprintf("scan/%s", message_id); |
| 49 | if (!directory_make(spool_directory, temp_string, 0750, FALSE)) { |
| 50 | log_write(0, LOG_MAIN|LOG_PANIC, "%s", string_open_failed(errno, |
| 51 | "scan directory %s/scan/%s", spool_directory, temp_string)); |
| 52 | goto OUT; |
| 53 | }; |
| 54 | |
| 55 | /* open [message_id].eml file for writing */ |
| 56 | mbox_file = modefopen(mbox_path, "wb", SPOOL_MODE); |
| 57 | if (mbox_file == NULL) { |
| 58 | log_write(0, LOG_MAIN|LOG_PANIC, "%s", string_open_failed(errno, |
| 59 | "scan file %s", mbox_file)); |
| 60 | goto OUT; |
| 61 | }; |
| 62 | |
| 63 | /* Generate mailbox headers. The $received_for variable is (up to at least |
| 64 | Exim 4.64) never set here, because it is only set when expanding the |
| 65 | contents of the Received: header line. However, the code below will use it |
| 66 | if it should become available in future. */ |
| 67 | |
| 68 | temp_string = expand_string( |
| 69 | US"From ${if def:return_path{$return_path}{MAILER-DAEMON}} ${tod_bsdinbox}\n" |
| 70 | "${if def:sender_address{X-Envelope-From: <${sender_address}>\n}}" |
| 71 | "${if def:recipients{X-Envelope-To: ${recipients}\n}}"); |
| 72 | |
| 73 | if (temp_string != NULL) { |
| 74 | i = fwrite(temp_string, Ustrlen(temp_string), 1, mbox_file); |
| 75 | if (i != 1) { |
| 76 | log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \ |
| 77 | mailbox headers to %s", mbox_path); |
| 78 | goto OUT; |
| 79 | }; |
| 80 | }; |
| 81 | |
| 82 | /* write all header lines to mbox file */ |
| 83 | my_headerlist = header_list; |
| 84 | for (my_headerlist = header_list; my_headerlist != NULL; |
| 85 | my_headerlist = my_headerlist->next) |
| 86 | { |
| 87 | /* skip deleted headers */ |
| 88 | if (my_headerlist->type == '*') continue; |
| 89 | |
| 90 | i = fwrite(my_headerlist->text, my_headerlist->slen, 1, mbox_file); |
| 91 | if (i != 1) { |
| 92 | log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \ |
| 93 | message headers to %s", mbox_path); |
| 94 | goto OUT; |
| 95 | }; |
| 96 | }; |
| 97 | |
| 98 | /* End headers */ |
| 99 | (void)fwrite("\n", 1, 1, mbox_file); |
| 100 | |
| 101 | /* copy body file */ |
| 102 | if (source_file_override == NULL) { |
| 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; |
| 110 | }; |
| 111 | } else { |
| 112 | data_file = Ufopen(source_file_override, "rb"); |
| 113 | }; |
| 114 | |
| 115 | if (data_file == NULL) { |
| 116 | log_write(0, LOG_MAIN|LOG_PANIC, "Could not open datafile for message %s", |
| 117 | message_id); |
| 118 | goto OUT; |
| 119 | }; |
| 120 | |
| 121 | /* The code used to use this line, but it doesn't work in Cygwin. |
| 122 | * |
| 123 | * (void)fread(data_buffer, 1, 18, data_file); |
| 124 | * |
| 125 | * What's happening is that spool_mbox used to use an fread to jump over the |
| 126 | * file header. That fails under Cygwin because the header is locked, but |
| 127 | * doing an fseek succeeds. We have to output the leading newline |
| 128 | * explicitly, because the one in the file is parted of the locked area. |
| 129 | */ |
| 130 | |
| 131 | if (!source_file_override) |
| 132 | (void)fseek(data_file, SPOOL_DATA_START_OFFSET, SEEK_SET); |
| 133 | |
| 134 | do { |
| 135 | j = fread(buffer, 1, sizeof(buffer), data_file); |
| 136 | |
| 137 | if (j > 0) { |
| 138 | i = fwrite(buffer, j, 1, mbox_file); |
| 139 | if (i != 1) { |
| 140 | log_write(0, LOG_MAIN|LOG_PANIC, "Error/short write while writing \ |
| 141 | message body to %s", mbox_path); |
| 142 | goto OUT; |
| 143 | }; |
| 144 | }; |
| 145 | } while (j > 0); |
| 146 | |
| 147 | (void)fclose(mbox_file); |
| 148 | mbox_file = NULL; |
| 149 | |
| 150 | Ustrcpy(spooled_message_id, message_id); |
| 151 | spool_mbox_ok = 1; |
| 152 | }; |
| 153 | |
| 154 | /* get the size of the mbox message and open [message_id].eml file for reading*/ |
| 155 | if (Ustat(mbox_path, &statbuf) != 0 || |
| 156 | (yield = Ufopen(mbox_path,"rb")) == NULL) { |
| 157 | log_write(0, LOG_MAIN|LOG_PANIC, "%s", string_open_failed(errno, |
| 158 | "scan file %s", mbox_file)); |
| 159 | goto OUT; |
| 160 | }; |
| 161 | |
| 162 | *mbox_file_size = statbuf.st_size; |
| 163 | |
| 164 | OUT: |
| 165 | if (data_file) (void)fclose(data_file); |
| 166 | if (mbox_file) (void)fclose(mbox_file); |
| 167 | store_reset(reset_point); |
| 168 | return yield; |
| 169 | } |
| 170 | |
| 171 | /* remove mbox spool file, demimed files and temp directory */ |
| 172 | void unspool_mbox(void) { |
| 173 | |
| 174 | /* reset all exiscan state variables */ |
| 175 | #ifdef WITH_OLD_DEMIME |
| 176 | demime_ok = 0; |
| 177 | demime_errorlevel = 0; |
| 178 | demime_reason = NULL; |
| 179 | file_extensions = NULL; |
| 180 | #endif |
| 181 | |
| 182 | spam_ok = 0; |
| 183 | malware_ok = 0; |
| 184 | |
| 185 | if (spool_mbox_ok && !no_mbox_unspool) { |
| 186 | uschar *mbox_path; |
| 187 | uschar *file_path; |
| 188 | int n; |
| 189 | struct dirent *entry; |
| 190 | DIR *tempdir; |
| 191 | |
| 192 | mbox_path = string_sprintf("%s/scan/%s", spool_directory, spooled_message_id); |
| 193 | |
| 194 | tempdir = opendir(CS mbox_path); |
| 195 | if (!tempdir) { |
| 196 | debug_printf("Unable to opendir(%s): %s\n", mbox_path, strerror(errno)); |
| 197 | /* Just in case we still can: */ |
| 198 | rmdir(CS mbox_path); |
| 199 | return; |
| 200 | } |
| 201 | /* loop thru dir & delete entries */ |
| 202 | while((entry = readdir(tempdir)) != NULL) { |
| 203 | uschar *name = US entry->d_name; |
| 204 | if (Ustrcmp(name, US".") == 0 || Ustrcmp(name, US"..") == 0) continue; |
| 205 | |
| 206 | file_path = string_sprintf("%s/%s", mbox_path, name); |
| 207 | debug_printf("unspool_mbox(): unlinking '%s'\n", file_path); |
| 208 | n = unlink(CS file_path); |
| 209 | }; |
| 210 | |
| 211 | closedir(tempdir); |
| 212 | |
| 213 | /* remove directory */ |
| 214 | rmdir(CS mbox_path); |
| 215 | store_reset(mbox_path); |
| 216 | }; |
| 217 | spool_mbox_ok = 0; |
| 218 | } |
| 219 | |
| 220 | #endif |