New: queuefile transport, under EXPERIMENTAL_QUEUEFILE
[exim.git] / src / src / transports / queuefile.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) Andrew Colin Kissa <andrew@topdog.za.net> 2016 */
6 /* Copyright (c) University of Cambridge 2016 */
7 /* See the file NOTICE for conditions of use and distribution. */
8
9
10 #include "../exim.h"
11 #include "queuefile.h"
12
13 /* Options specific to the appendfile transport. They must be in alphabetic
14 order (note that "_" comes before the lower case letters). Some of them are
15 stored in the publicly visible instance block - these are flagged with the
16 opt_public flag. */
17
18 optionlist queuefile_transport_options[] = {
19   { "directory", opt_stringptr,
20     (void *)offsetof(queuefile_transport_options_block, dirname) },
21 };
22
23 /* Size of the options list. An extern variable has to be used so that its
24 address can appear in the tables drtables.c. */
25
26 int queuefile_transport_options_count =
27   sizeof(queuefile_transport_options) / sizeof(optionlist);
28
29 /* Default private options block for the appendfile transport. */
30
31 queuefile_transport_options_block queuefile_transport_option_defaults = {
32   NULL,           /* dirname */
33 };
34
35 /*************************************************
36 *          Initialization entry point            *
37 *************************************************/
38
39 void queuefile_transport_init(transport_instance *tblock)
40 {
41 queuefile_transport_options_block *ob =
42   (queuefile_transport_options_block *)(tblock->options_block);
43
44 if (ob->dirname == NULL)
45   log_write(0, LOG_PANIC_DIE | LOG_CONFIG,
46     "directory must be set for the %s transport", tblock->name);
47 }
48
49 /* This function will copy from a file to another
50
51 Arguments:
52   to_fd        FILE to write to (the destination queue file)
53   from_fd      FILE to read from (the spool queue file)
54
55 Returns:       TRUE if all went well, FALSE otherwise
56 */
57
58 static BOOL copy_spool_file (FILE *to_fd, FILE *from_fd)
59 {
60 int i, j;
61 uschar buffer[16384];
62
63 rewind(from_fd);
64
65 do
66   {
67     j = fread(buffer, 1, sizeof(buffer), from_fd);
68     if (j > 0)
69       {
70       i = fwrite(buffer, j, 1, to_fd);
71       if (i != 1)
72         return FALSE;
73       }
74   }
75 while (j > 0);
76 return TRUE;
77 }
78
79 /* This function performs the actual copying of the header
80 and data files to the destination directory
81
82 Arguments:
83   tname         uschar the transport name
84   addr          address_item being processed
85   sdfd          int Source directory fd
86   ddfd          int Destination directory fd
87   suffix        uschar file suffix
88   dirname       uschar Destination directory
89   link_file     BOOL use linkat instead of data copy
90   is_data_file  BOOL the file is a data file not a header file
91   dst_file      FILE to write to
92   src_file      FILE to read from
93
94 Returns:       TRUE if all went well, FALSE otherwise
95 */
96
97 static BOOL copy_spool_files(uschar *tname, address_item *addr,
98   int sdfd, int ddfd, uschar *suffix, uschar *dirname, BOOL link_file,
99   BOOL is_data_file, FILE *dst_file, FILE *src_file)
100 {
101 int dstfd, srcfd;
102 /*
103 uschar message_subdir[2];
104 message_subdir[1] = '\0';
105 message_subdir[0] = split_spool_directory? message_id[5] : 0;
106 */
107 uschar *filename = string_sprintf("%s-%s", message_id, suffix);
108 /*
109 uschar *srcpath = string_sprintf("%s/%s/%s/%s-%s", spool_directory,
110     US"input", message_subdir, message_id, suffix);
111 */
112 uschar *srcpath = spool_fname(US"input", message_subdir, message_id, suffix);
113 uschar *dstpath = string_sprintf("%s/%s-%s", dirname, message_id, suffix);
114
115 if (link_file)
116   {
117   /* use linkat */
118   DEBUG(D_transport)
119     debug_printf("%s transport, linking %s => %s\n", tname,
120       srcpath, dstpath);
121   if (linkat(sdfd, CCS filename, ddfd, CCS filename, 0) < 0)
122     return FALSE;
123   return TRUE;
124   }
125 else
126   {
127   /* use data copy */
128   DEBUG(D_transport)
129     debug_printf("%s transport, copying %s => %s\n", tname,
130       srcpath, dstpath);
131   if ((dstfd =
132     openat(ddfd, CCS filename, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE)) < 0)
133     {
134     addr->transport_return = DEFER;
135     addr->basic_errno = errno;
136     addr->message = string_sprintf("%s transport opening file: %s "
137       "failed with error: %s", tname, dstpath, strerror(errno));
138     return FALSE;
139     }
140
141   fchmod(dstfd, SPOOL_MODE);
142
143   if ((dst_file = fdopen(dstfd, "wb")) < 0)
144     {
145     addr->transport_return = DEFER;
146     addr->basic_errno = errno;
147     addr->message = string_sprintf("%s transport opening file fd: %s "
148       "failed with error: %s", tname, dstpath, strerror(errno));
149     (void)close(dstfd);
150     return FALSE;
151     }
152
153   if (is_data_file)
154     srcfd = deliver_datafile;
155   else
156     {
157     if ((srcfd = openat(sdfd, CCS filename, O_RDONLY)) < 0)
158       {
159       addr->transport_return = DEFER;
160       addr->basic_errno = errno;
161       addr->message = string_sprintf("%s transport opening file: %s "
162         "failed with error: %s", tname, srcpath, strerror(errno));
163       return FALSE;
164       }
165     }
166
167   if ((src_file = fdopen(srcfd, "rb")) < 0)
168     {
169     addr->transport_return = DEFER;
170     addr->basic_errno = errno;
171     addr->message = string_sprintf("%s transport opening file fd: "
172       "%s failed with error: %s", tname, srcpath, strerror(errno));
173     if (!is_data_file) (void)close(srcfd);
174     return FALSE;
175     }
176
177   if (!copy_spool_file(dst_file, src_file))
178     {
179     addr->transport_return = DEFER;
180     addr->message = string_sprintf("%s transport creating file: "
181       "%s failed with error: %s", tname, dstpath, strerror(errno));
182     return FALSE;
183     }
184
185   if (!is_data_file)
186     {
187     (void)fclose(src_file);
188     src_file = NULL;
189     }
190
191   (void)fclose(dst_file);
192   dst_file = NULL;
193
194   } /* end data copy */
195
196 return TRUE;
197 }
198
199 /*************************************************
200 *              Main entry point                  *
201 *************************************************/
202
203 /* This transport always returns FALSE, indicating that the status in
204 the first address is the status for all addresses in a batch. */
205
206 BOOL queuefile_transport_entry(transport_instance *tblock,
207   address_item *addr)
208 {
209 BOOL link_file;
210 BOOL is_data_file;
211 uschar *sourcedir;
212 struct stat dstatbuf;
213 struct stat sstatbuf;
214 FILE *dst_file = NULL;
215 FILE *src_file = NULL;
216 /* uschar message_subdir[2]; */
217 int ddfd, sdfd, dfd_oflags;
218 queuefile_transport_options_block *ob =
219   (queuefile_transport_options_block *)(tblock->options_block);
220
221 DEBUG(D_transport)
222   debug_printf("%s transport entered\n", tblock->name);
223
224 # ifndef O_DIRECTORY
225 #  define O_DIRECTORY 0
226 # endif
227
228 dfd_oflags = O_RDONLY|O_DIRECTORY;
229 #ifdef O_NOFOLLOW
230 dfd_oflags |= O_NOFOLLOW;
231 #endif
232
233 if (ob->dirname[0] != '/')
234   {
235   addr->transport_return = PANIC;
236   addr->message = string_sprintf("%s transport directory: "
237     "%s is not absolute", tblock->name, ob->dirname);
238   return FALSE;
239   }
240
241 if ((ddfd = Uopen(ob->dirname, dfd_oflags, 0)) < 0)
242   {
243   addr->transport_return = PANIC;
244   addr->basic_errno = errno;
245   addr->message = string_sprintf("%s transport accessing directory: %s "
246     "failed with error: %s", tblock->name, ob->dirname, strerror(errno));
247   return FALSE;
248   }
249
250
251 if ((fstat(ddfd, &dstatbuf)) < 0)
252   {
253   addr->transport_return = PANIC;
254   addr->basic_errno = errno;
255   addr->message = string_sprintf("%s transport fstat on directory fd: "
256     "%s failed with error: %s", tblock->name, ob->dirname, strerror(errno));
257   goto RETURN;
258   }
259
260 sourcedir = spool_dname(US"input", message_subdir);
261 /*
262 message_subdir[1] = '\0';
263 message_subdir[0] = split_spool_directory? message_id[5] : 0;
264 sourcedir = string_sprintf("%s/%s/%s", spool_directory,
265   US"input", message_subdir);
266 */
267
268 if ((sdfd = Uopen(sourcedir, dfd_oflags, 0)) < 0)
269   {
270   addr->transport_return = PANIC;
271   addr->basic_errno = errno;
272   addr->message = string_sprintf("%s transport accessing directory: %s "
273     "failed with error: %s", tblock->name, sourcedir, strerror(errno));
274   goto RETURN;
275   }
276
277 if ((fstat(sdfd, &sstatbuf)) < 0)
278   {
279   addr->transport_return = PANIC;
280   addr->basic_errno = errno;
281   addr->message = string_sprintf("%s transport fstat on directory fd: "
282     "%s failed with error: %s", tblock->name, sourcedir, strerror(errno));
283   goto RETURN;
284   }
285
286 if (dont_deliver)
287   {
288   DEBUG(D_transport)
289     debug_printf("*** delivery by %s transport bypassed by -N option\n",
290       tblock->name);
291   addr->transport_return = OK;
292   goto RETURN;
293   }
294
295 /* process the header file */
296 DEBUG(D_transport)
297   debug_printf("%s transport, copying header file\n", tblock->name);
298
299 is_data_file = FALSE;
300 link_file = (dstatbuf.st_dev == sstatbuf.st_dev);
301
302 if ((copy_spool_files(tblock->name, addr, sdfd, ddfd, US"H", ob->dirname,
303   link_file, is_data_file, dst_file, src_file)) == FALSE)
304   goto RETURN;
305
306 /* process the data file */
307 DEBUG(D_transport)
308   debug_printf("%s transport, copying data file\n", tblock->name);
309
310 is_data_file = TRUE;
311
312 if ((copy_spool_files(tblock->name, addr, sdfd, ddfd, US"D", ob->dirname,
313   link_file, is_data_file, dst_file, src_file)) == FALSE)
314   {
315   DEBUG(D_transport)
316     debug_printf("%s transport, copying data file failed, "
317       "unlinking the header file\n", tblock->name);
318   Uunlink(string_sprintf("%s/%s-H", ob->dirname, message_id));
319   goto RETURN;
320   }
321
322 (void)close(ddfd);
323 (void)close(sdfd);
324
325 DEBUG(D_transport)
326   debug_printf("%s transport succeeded\n", tblock->name);
327
328 addr->transport_return = OK;
329
330 RETURN:
331 if (dst_file) (void)fclose(dst_file);
332 if (src_file && !is_data_file) (void)fclose(src_file);
333 if (ddfd) (void)close(ddfd);
334 if (sdfd) (void)close(sdfd);
335
336 /* A return of FALSE means that if there was an error, a common error was
337 put in the first address of a batch. */
338 return FALSE;
339 }