1 /* $Cambridge: exim/src/src/spool_mbox.c,v 1.1.2.2 2004/11/26 16:04:26 tom Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 #ifdef WITH_CONTENT_SCAN
9 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
12 /* Code for setting up a MBOX style spool file inside a /scan/<msgid>
13 sub directory of exim's spool directory. */
17 /* externals, we must reset them on unspooling */
19 extern struct file_extension *file_extensions;
21 extern int malware_ok;
24 int spool_mbox_ok = 0;
25 uschar spooled_message_id[17];
27 /* returns a pointer to the FILE, and puts the size in bytes into mbox_file_size */
29 FILE *spool_mbox(unsigned long long *mbox_file_size) {
30 uschar mbox_path[1024];
31 uschar message_subdir[2];
32 uschar data_buffer[65535];
34 FILE *data_file = NULL;
35 header_line *my_headerlist;
40 /* create scan directory, if not present */
41 if (!directory_make(spool_directory, US "scan", 0750, FALSE)) {
42 debug_printf("unable to create directory: %s/scan\n", spool_directory);
46 /* create temp directory inside scan dir */
47 snprintf(CS mbox_path, 1024, "%s/scan/%s", spool_directory, message_id);
48 if (!directory_make(NULL, mbox_path, 0750, FALSE)) {
49 debug_printf("unable to create directory: %s/scan/%s\n", spool_directory, message_id);
53 /* open [message_id].eml file for writing */
54 snprintf(CS mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
55 mbox_file = Ufopen(mbox_path,"w");
57 if (mbox_file == NULL) {
58 debug_printf("unable to open file for writing: %s\n", mbox_path);
62 /* write all header lines to mbox file */
63 my_headerlist = header_list;
64 while (my_headerlist != NULL) {
66 /* skip deleted headers */
67 if (my_headerlist->type == '*') {
68 my_headerlist = my_headerlist->next;
72 i = fwrite(my_headerlist->text, 1, my_headerlist->slen, mbox_file);
73 if (i != my_headerlist->slen) {
74 debug_printf("error/short write on writing in: %s", mbox_path);
79 my_headerlist = my_headerlist->next;
83 message_subdir[1] = '\0';
84 for (i = 0; i < 2; i++) {
85 message_subdir[0] = (split_spool_directory == (i == 0))? message_id[5] : 0;
86 sprintf(CS mbox_path, "%s/input/%s/%s-D", spool_directory, message_subdir, message_id);
87 data_file = Ufopen(mbox_path,"r");
88 if (data_file != NULL)
92 fread(data_buffer, 1, 18, data_file);
95 j = fread(data_buffer, 1, sizeof(data_buffer), data_file);
97 i = fwrite(data_buffer, 1, j, mbox_file);
99 debug_printf("error/short write on writing in: %s", mbox_path);
109 Ustrcpy(spooled_message_id, message_id);
113 snprintf(CS mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
115 /* get the size of the mbox message */
116 stat(CS mbox_path, &statbuf);
117 *mbox_file_size = statbuf.st_size;
119 /* open [message_id].eml file for reading */
120 mbox_file = Ufopen(mbox_path,"r");
125 /* remove mbox spool file, demimed files and temp directory */
126 void unspool_mbox(void) {
128 /* reset all exiscan state variables */
130 demime_errorlevel = 0;
131 demime_reason = NULL;
132 file_extensions = NULL;
140 if (!no_mbox_unspool) {
141 uschar mbox_path[1024];
142 uschar file_path[1024];
144 struct dirent *entry;
147 snprintf(CS mbox_path, 1024, "%s/scan/%s", spool_directory, spooled_message_id);
149 tempdir = opendir(CS mbox_path);
150 /* loop thru dir & delete entries */
153 entry = readdir(tempdir);
154 if (entry == NULL) break;
155 snprintf(CS file_path, 1024,"%s/scan/%s/%s", spool_directory, spooled_message_id, entry->d_name);
156 if ( (Ustrcmp(entry->d_name,"..") != 0) && (Ustrcmp(entry->d_name,".") != 0) ) {
157 debug_printf("unspool_mbox(): unlinking '%s'\n", file_path);
158 n = unlink(CS file_path);
164 /* remove directory */
165 n = rmdir(CS mbox_path);