Weekend W.I.P. checkin
[users/jgh/exim.git] / src / src / spool_mbox.c
1 /* $Cambridge: exim/src/src/spool_mbox.c,v 1.1.2.2 2004/11/26 16:04:26 tom Exp $ */
2
3 /*************************************************
4 *     Exim - an Internet mail transport agent    *
5 *************************************************/
6
7 #ifdef WITH_CONTENT_SCAN
8
9 /* Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-???? */
10 /* License: GPL */
11
12 /* Code for setting up a MBOX style spool file inside a /scan/<msgid>
13 sub directory of exim's spool directory. */
14
15 #include "exim.h"
16
17 /* externals, we must reset them on unspooling */
18 extern int demime_ok;
19 extern struct file_extension *file_extensions;
20
21 extern int malware_ok;
22 extern int spam_ok;
23
24 int spool_mbox_ok = 0;
25 uschar spooled_message_id[17];
26
27 /* returns a pointer to the FILE, and puts the size in bytes into mbox_file_size */
28
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];
33   FILE *mbox_file;
34   FILE *data_file = NULL;
35   header_line *my_headerlist;
36   struct stat statbuf;
37   int i,j;
38   
39   if (!spool_mbox_ok) {
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);
43       return NULL;
44     };
45     
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);
50       return NULL;
51     };
52     
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");
56     
57     if (mbox_file == NULL) {
58       debug_printf("unable to open file for writing: %s\n", mbox_path);
59       return NULL;
60     };
61     
62     /* write all header lines to mbox file */
63     my_headerlist = header_list;
64     while (my_headerlist != NULL) {
65       
66       /* skip deleted headers */
67       if (my_headerlist->type == '*') {
68         my_headerlist = my_headerlist->next;
69         continue;
70       };
71   
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);
75         fclose(mbox_file);
76         return NULL;
77       };
78       
79       my_headerlist = my_headerlist->next;
80     };
81   
82     /* copy body file */
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)
89         break;
90     };
91
92     fread(data_buffer, 1, 18, data_file);
93     
94     do {
95       j = fread(data_buffer, 1, sizeof(data_buffer), data_file);
96       if (j > 0) {
97         i = fwrite(data_buffer, 1, j, mbox_file);
98         if (i != j) {
99           debug_printf("error/short write on writing in: %s", mbox_path);
100           fclose(mbox_file);
101           fclose(data_file);
102           return NULL;
103         };
104       };
105     } while (j > 0);
106     
107     fclose(data_file);
108     fclose(mbox_file);
109     Ustrcpy(spooled_message_id, message_id);
110     spool_mbox_ok = 1;
111   };
112
113   snprintf(CS mbox_path, 1024, "%s/scan/%s/%s.eml", spool_directory, message_id, message_id);
114
115   /* get the size of the mbox message */
116   stat(CS mbox_path, &statbuf);
117   *mbox_file_size = statbuf.st_size;
118
119   /* open [message_id].eml file for reading */
120   mbox_file = Ufopen(mbox_path,"r");
121   
122   return mbox_file;
123 }
124
125 /* remove mbox spool file, demimed files and temp directory */
126 void unspool_mbox(void) {
127
128   /* reset all exiscan state variables */
129   demime_ok = 0;
130   demime_errorlevel = 0;
131   demime_reason = NULL;
132   file_extensions = NULL;
133   spam_ok = 0;
134   malware_ok = 0;
135   
136   if (spool_mbox_ok) {
137
138     spool_mbox_ok = 0;
139     
140     if (!no_mbox_unspool) {
141       uschar mbox_path[1024];
142       uschar file_path[1024];
143       int n;
144       struct dirent *entry;
145       DIR *tempdir;
146       
147       snprintf(CS mbox_path, 1024, "%s/scan/%s", spool_directory, spooled_message_id);
148         
149         tempdir = opendir(CS mbox_path);
150         /* loop thru dir & delete entries */
151         n = 0;
152         do {
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);
159             }; 
160         } while (n > -1);
161         
162         closedir(tempdir);
163         
164         /* remove directory */
165         n = rmdir(CS mbox_path);
166     };
167   };
168 }
169
170 #endif