| 1 | /* $Cambridge: exim/src/src/spool_mbox.c,v 1.11 2006/02/22 14:46:44 ph10 Exp $ */ |
| 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 | |
| 30 | FILE *spool_mbox(unsigned long *mbox_file_size) { |
| 31 | uschar mbox_path[1024]; |
| 32 | uschar message_subdir[2]; |
| 33 | uschar data_buffer[65535]; |
| 34 | FILE *mbox_file; |
| 35 | FILE *data_file = NULL; |
| 36 | header_line *my_headerlist; |
| 37 | struct stat statbuf; |
| 38 | int i,j; |
| 39 | uschar *mbox_delimiter; |
| 40 | uschar *envelope_from; |
| 41 | uschar *envelope_to; |
| 42 | |
| 43 | if (!spool_mbox_ok) { |
| 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); |
| 47 | return NULL; |
| 48 | }; |
| 49 | |
| 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); |
| 54 | return NULL; |
| 55 | }; |
| 56 | |
| 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); |
| 60 | |
| 61 | if (mbox_file == NULL) { |
| 62 | debug_printf("unable to open file for writing: %s\n", mbox_path); |
| 63 | return NULL; |
| 64 | }; |
| 65 | |
| 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); |
| 74 | return NULL; |
| 75 | }; |
| 76 | }; |
| 77 | }; |
| 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); |
| 88 | return NULL; |
| 89 | }; |
| 90 | }; |
| 91 | }; |
| 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); |
| 102 | return NULL; |
| 103 | }; |
| 104 | }; |
| 105 | }; |
| 106 | |
| 107 | /* write all header lines to mbox file */ |
| 108 | my_headerlist = header_list; |
| 109 | while (my_headerlist != NULL) { |
| 110 | |
| 111 | /* skip deleted headers */ |
| 112 | if (my_headerlist->type == '*') { |
| 113 | my_headerlist = my_headerlist->next; |
| 114 | continue; |
| 115 | }; |
| 116 | |
| 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); |
| 121 | return NULL; |
| 122 | }; |
| 123 | |
| 124 | my_headerlist = my_headerlist->next; |
| 125 | }; |
| 126 | |
| 127 | /* copy body file */ |
| 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) |
| 134 | break; |
| 135 | }; |
| 136 | |
| 137 | /* The code used to use this line, but it doesn't work in Cygwin. |
| 138 | * |
| 139 | * (void)fread(data_buffer, 1, 18, data_file); |
| 140 | * |
| 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. |
| 145 | */ |
| 146 | |
| 147 | (void)fwrite("\n", 1, 1, mbox_file); |
| 148 | (void)fseek(data_file, SPOOL_DATA_START_OFFSET, SEEK_SET); |
| 149 | |
| 150 | do { |
| 151 | j = fread(data_buffer, 1, sizeof(data_buffer), data_file); |
| 152 | |
| 153 | if (j > 0) { |
| 154 | i = fwrite(data_buffer, 1, j, mbox_file); |
| 155 | if (i != j) { |
| 156 | debug_printf("error/short write on writing in: %s", mbox_path); |
| 157 | (void)fclose(mbox_file); |
| 158 | (void)fclose(data_file); |
| 159 | return NULL; |
| 160 | }; |
| 161 | }; |
| 162 | } while (j > 0); |
| 163 | |
| 164 | (void)fclose(data_file); |
| 165 | (void)fclose(mbox_file); |
| 166 | Ustrcpy(spooled_message_id, message_id); |
| 167 | spool_mbox_ok = 1; |
| 168 | }; |
| 169 | |
| 170 | (void)string_format(mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id); |
| 171 | |
| 172 | /* get the size of the mbox message */ |
| 173 | stat(CS mbox_path, &statbuf); |
| 174 | *mbox_file_size = statbuf.st_size; |
| 175 | |
| 176 | /* open [message_id].eml file for reading */ |
| 177 | mbox_file = Ufopen(mbox_path,"rb"); |
| 178 | |
| 179 | return mbox_file; |
| 180 | } |
| 181 | |
| 182 | /* remove mbox spool file, demimed files and temp directory */ |
| 183 | void unspool_mbox(void) { |
| 184 | |
| 185 | /* reset all exiscan state variables */ |
| 186 | #ifdef WITH_OLD_DEMIME |
| 187 | demime_ok = 0; |
| 188 | demime_errorlevel = 0; |
| 189 | demime_reason = NULL; |
| 190 | file_extensions = NULL; |
| 191 | #endif |
| 192 | |
| 193 | spam_ok = 0; |
| 194 | malware_ok = 0; |
| 195 | |
| 196 | if (spool_mbox_ok) { |
| 197 | |
| 198 | spool_mbox_ok = 0; |
| 199 | |
| 200 | if (!no_mbox_unspool) { |
| 201 | uschar mbox_path[1024]; |
| 202 | uschar file_path[1024]; |
| 203 | int n; |
| 204 | struct dirent *entry; |
| 205 | DIR *tempdir; |
| 206 | |
| 207 | (void)string_format(mbox_path, 1024, "%s/scan/%s", spool_directory, spooled_message_id); |
| 208 | |
| 209 | tempdir = opendir(CS mbox_path); |
| 210 | /* loop thru dir & delete entries */ |
| 211 | n = 0; |
| 212 | do { |
| 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); |
| 219 | }; |
| 220 | } while (n > -1); |
| 221 | |
| 222 | closedir(tempdir); |
| 223 | |
| 224 | /* remove directory */ |
| 225 | n = rmdir(CS mbox_path); |
| 226 | }; |
| 227 | }; |
| 228 | } |
| 229 | |
| 230 | #endif |