When checking for a message's continued existence, exim_tidydb was not
[exim.git] / src / src / spool_mbox.c
1 /* $Cambridge: exim/src/src/spool_mbox.c,v 1.3 2004/12/17 14:52: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
40 if (!spool_mbox_ok) {
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);
44 return NULL;
45 };
46
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);
51 return NULL;
52 };
53
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");
57
58 if (mbox_file == NULL) {
59 debug_printf("unable to open file for writing: %s\n", mbox_path);
60 return NULL;
61 };
62
63 /* write all header lines to mbox file */
64 my_headerlist = header_list;
65 while (my_headerlist != NULL) {
66
67 /* skip deleted headers */
68 if (my_headerlist->type == '*') {
69 my_headerlist = my_headerlist->next;
70 continue;
71 };
72
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);
76 fclose(mbox_file);
77 return NULL;
78 };
79
80 my_headerlist = my_headerlist->next;
81 };
82
83 /* copy body file */
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)
90 break;
91 };
92
93 fread(data_buffer, 1, 18, data_file);
94
95 do {
96 j = fread(data_buffer, 1, sizeof(data_buffer), data_file);
97 if (j > 0) {
98 i = fwrite(data_buffer, 1, j, mbox_file);
99 if (i != j) {
100 debug_printf("error/short write on writing in: %s", mbox_path);
101 fclose(mbox_file);
102 fclose(data_file);
103 return NULL;
104 };
105 };
106 } while (j > 0);
107
108 fclose(data_file);
109 fclose(mbox_file);
110 Ustrcpy(spooled_message_id, message_id);
111 spool_mbox_ok = 1;
112 };
113
114 snprintf(CS mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
115
116 /* get the size of the mbox message */
117 stat(CS mbox_path, &statbuf);
118 *mbox_file_size = statbuf.st_size;
119
120 /* open [message_id].eml file for reading */
121 mbox_file = Ufopen(mbox_path,"r");
122
123 return mbox_file;
124 }
125
126 /* remove mbox spool file, demimed files and temp directory */
127 void unspool_mbox(void) {
128
129 /* reset all exiscan state variables */
130 #ifdef WITH_OLD_DEMIME
131 demime_ok = 0;
132 demime_errorlevel = 0;
133 demime_reason = NULL;
134 file_extensions = NULL;
135 #endif
136
137 spam_ok = 0;
138 malware_ok = 0;
139
140 if (spool_mbox_ok) {
141
142 spool_mbox_ok = 0;
143
144 if (!no_mbox_unspool) {
145 uschar mbox_path[1024];
146 uschar file_path[1024];
147 int n;
148 struct dirent *entry;
149 DIR *tempdir;
150
151 snprintf(CS mbox_path, 1024, "%s/scan/%s", spool_directory, spooled_message_id);
152
153 tempdir = opendir(CS mbox_path);
154 /* loop thru dir & delete entries */
155 n = 0;
156 do {
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);
163 };
164 } while (n > -1);
165
166 closedir(tempdir);
167
168 /* remove directory */
169 n = rmdir(CS mbox_path);
170 };
171 };
172 }
173
174 #endif