1 /* $Cambridge: exim/src/src/spool_mbox.c,v 1.4 2005/02/17 11:58:26 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;
41 /* create scan directory, if not present */
42 if (!directory_make(spool_directory, US "scan", 0750, FALSE)) {
43 debug_printf("unable to create directory: %s/scan\n", spool_directory);
47 /* create temp directory inside scan dir */
48 snprintf(CS mbox_path, 1024, "%s/scan/%s", spool_directory, message_id);
49 if (!directory_make(NULL, mbox_path, 0750, FALSE)) {
50 debug_printf("unable to create directory: %s/scan/%s\n", spool_directory, message_id);
54 /* open [message_id].eml file for writing */
55 snprintf(CS mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
56 mbox_file = Ufopen(mbox_path,"w");
58 if (mbox_file == NULL) {
59 debug_printf("unable to open file for writing: %s\n", mbox_path);
63 /* write all header lines to mbox file */
64 my_headerlist = header_list;
65 while (my_headerlist != NULL) {
67 /* skip deleted headers */
68 if (my_headerlist->type == '*') {
69 my_headerlist = my_headerlist->next;
73 i = fwrite(my_headerlist->text, 1, my_headerlist->slen, mbox_file);
74 if (i != my_headerlist->slen) {
75 debug_printf("error/short write on writing in: %s", mbox_path);
80 my_headerlist = my_headerlist->next;
84 message_subdir[1] = '\0';
85 for (i = 0; i < 2; i++) {
86 message_subdir[0] = (split_spool_directory == (i == 0))? message_id[5] : 0;
87 sprintf(CS mbox_path, "%s/input/%s/%s-D", spool_directory, message_subdir, message_id);
88 data_file = Ufopen(mbox_path,"r");
89 if (data_file != NULL)
93 fread(data_buffer, 1, 18, data_file);
96 j = fread(data_buffer, 1, sizeof(data_buffer), data_file);
98 i = fwrite(data_buffer, 1, j, mbox_file);
100 debug_printf("error/short write on writing in: %s", mbox_path);
110 Ustrcpy(spooled_message_id, message_id);
114 snprintf(CS mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
116 /* get the size of the mbox message */
117 stat(CS mbox_path, &statbuf);
118 *mbox_file_size = statbuf.st_size;
120 /* open [message_id].eml file for reading */
121 mbox_file = Ufopen(mbox_path,"r");
126 /* remove mbox spool file, demimed files and temp directory */
127 void unspool_mbox(void) {
129 /* reset all exiscan state variables */
130 #ifdef WITH_OLD_DEMIME
132 demime_errorlevel = 0;
133 demime_reason = NULL;
134 file_extensions = NULL;
144 if (!no_mbox_unspool) {
145 uschar mbox_path[1024];
146 uschar file_path[1024];
148 struct dirent *entry;
151 snprintf(CS mbox_path, 1024, "%s/scan/%s", spool_directory, spooled_message_id);
153 tempdir = opendir(CS mbox_path);
154 /* loop thru dir & delete entries */
157 entry = readdir(tempdir);
158 if (entry == NULL) break;
159 snprintf(CS file_path, 1024,"%s/scan/%s/%s", spool_directory, spooled_message_id, entry->d_name);
160 if ( (Ustrcmp(entry->d_name,"..") != 0) && (Ustrcmp(entry->d_name,".") != 0) ) {
161 debug_printf("unspool_mbox(): unlinking '%s'\n", file_path);
162 n = unlink(CS file_path);
168 /* remove directory */
169 n = rmdir(CS mbox_path);