1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Andrew Colin Kissa <andrew@topdog.za.net> 2016 */
6 /* Copyright (c) University of Cambridge 2016 */
7 /* Copyright (c) The Exim Maintainers 1995 - 2021 */
8 /* See the file NOTICE for conditions of use and distribution. */
9 /* SPDX-License-Identifier: GPL-2.0-or-later */
15 #ifdef EXPERIMENTAL_QUEUEFILE /* whole file */
16 #include "queuefile.h"
18 #ifndef EXIM_HAVE_OPENAT
19 # error queuefile transport reqires openat() support
22 /* Options specific to the appendfile transport. They must be in alphabetic
23 order (note that "_" comes before the lower case letters). Some of them are
24 stored in the publicly visible instance block - these are flagged with the
27 optionlist queuefile_transport_options[] = {
28 { "directory", opt_stringptr,
29 OPT_OFF(queuefile_transport_options_block, dirname) },
33 /* Size of the options list. An extern variable has to be used so that its
34 address can appear in the tables drtables.c. */
36 int queuefile_transport_options_count =
37 sizeof(queuefile_transport_options) / sizeof(optionlist);
43 queuefile_transport_options_block queuefile_transport_option_defaults = {0};
44 void queuefile_transport_init(transport_instance *tblock) {}
45 BOOL queuefile_transport_entry(transport_instance *tblock, address_item *addr) {return FALSE;}
47 #else /*!MACRO_PREDEF*/
51 /* Default private options block for the appendfile transport. */
53 queuefile_transport_options_block queuefile_transport_option_defaults = {
57 /*************************************************
58 * Initialization entry point *
59 *************************************************/
61 void queuefile_transport_init(transport_instance *tblock)
63 queuefile_transport_options_block *ob =
64 (queuefile_transport_options_block *) tblock->options_block;
67 log_write(0, LOG_PANIC_DIE | LOG_CONFIG,
68 "directory must be set for the %s transport", tblock->name);
71 /* This function will copy from a file to another
74 dst fd to write to (the destination queue file)
75 src fd to read from (the spool queue file)
77 Returns: TRUE if all went well, FALSE otherwise with errno set
81 copy_spool_file(int dst, int src)
86 if (lseek(src, 0, SEEK_SET) != 0)
90 if ((j = read(src, buffer, sizeof(buffer))) > 0)
91 for (uschar * s = buffer; (i = write(dst, s, j)) != j; s += i, j -= i)
100 /* This function performs the actual copying of the header
101 and data files to the destination directory
104 tb the transport block
105 addr address_item being processed
106 dstpath destination directory name
107 sdfd int Source directory fd
108 ddfd int Destination directory fd
109 link_file BOOL use linkat instead of data copy
110 srcfd fd for data file, or -1 for header file
112 Returns: TRUE if all went well, FALSE otherwise
116 copy_spool_files(transport_instance * tb, address_item * addr,
117 const uschar * dstpath, int sdfd, int ddfd, BOOL link_file, int srcfd)
119 BOOL is_hdr_file = srcfd < 0;
120 const uschar * suffix = srcfd < 0 ? US"H" : US"D";
122 const uschar * filename = string_sprintf("%s-%s", message_id, suffix);
123 const uschar * srcpath = spool_fname(US"input", message_subdir, message_id, suffix);
124 const uschar * s, * op;
126 dstpath = string_sprintf("%s/%s-%s", dstpath, message_id, suffix);
130 DEBUG(D_transport) debug_printf("%s transport, linking %s => %s\n",
131 tb->name, srcpath, dstpath);
133 if (linkat(sdfd, CCS filename, ddfd, CCS filename, 0) >= 0)
139 else /* use data copy */
141 DEBUG(D_transport) debug_printf("%s transport, copying %s => %s\n",
142 tb->name, srcpath, dstpath);
145 (dstfd = exim_openat4(ddfd, CCS filename, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE))
149 && (s = srcpath, (srcfd = exim_openat(sdfd, CCS filename, O_RDONLY)) < 0)
154 if (s = dstpath, fchmod(dstfd, SPOOL_MODE) != 0)
155 op = US"setting perms on";
157 if (!copy_spool_file(dstfd, srcfd))
163 addr->basic_errno = errno;
164 addr->message = string_sprintf("%s transport %s file: %s failed with error: %s",
165 tb->name, op, s, strerror(errno));
166 addr->transport_return = DEFER;
170 /*************************************************
172 *************************************************/
174 /* This transport always returns FALSE, indicating that the status in
175 the first address is the status for all addresses in a batch. */
178 queuefile_transport_entry(transport_instance * tblock, address_item * addr)
180 queuefile_transport_options_block * ob =
181 (queuefile_transport_options_block *) tblock->options_block;
183 uschar * sourcedir = spool_dname(US"input", message_subdir);
184 uschar * s, * dstdir;
185 struct stat dstatbuf, sstatbuf;
186 int ddfd = -1, sdfd = -1;
189 debug_printf("%s transport entered\n", tblock->name);
192 # define O_DIRECTORY 0
195 # define O_NOFOLLOW 0
198 if (!(dstdir = expand_string(ob->dirname)))
200 addr->message = string_sprintf("%s transport: failed to expand dirname option",
202 addr->transport_return = DEFER;
207 addr->transport_return = PANIC;
208 addr->message = string_sprintf("%s transport directory: "
209 "%s is not absolute", tblock->name, dstdir);
213 /* Open the source and destination directories and check if they are
214 on the same filesystem, so we can hard-link files rather than copying. */
217 (ddfd = Uopen(s, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
219 (sdfd = Uopen(sourcedir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
222 addr->transport_return = PANIC;
223 addr->basic_errno = errno;
224 addr->message = string_sprintf("%s transport accessing directory: %s "
225 "failed with error: %s", tblock->name, s, strerror(errno));
226 if (ddfd >= 0) (void) close(ddfd);
230 if ( (s = dstdir, fstat(ddfd, &dstatbuf) < 0)
231 || (s = sourcedir, fstat(sdfd, &sstatbuf) < 0)
234 addr->transport_return = PANIC;
235 addr->basic_errno = errno;
236 addr->message = string_sprintf("%s transport fstat on directory fd: "
237 "%s failed with error: %s", tblock->name, s, strerror(errno));
240 can_link = (dstatbuf.st_dev == sstatbuf.st_dev);
245 debug_printf("*** delivery by %s transport bypassed by -N option\n",
247 addr->transport_return = OK;
251 /* Link or copy the header and data spool files */
254 debug_printf("%s transport, copying header file\n", tblock->name);
256 if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link, -1))
260 debug_printf("%s transport, copying data file\n", tblock->name);
262 if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link,
266 debug_printf("%s transport, copying data file failed, "
267 "unlinking the header file\n", tblock->name);
268 Uunlink(string_sprintf("%s/%s-H", dstdir, message_id));
273 debug_printf("%s transport succeeded\n", tblock->name);
275 addr->transport_return = OK;
278 if (ddfd >= 0) (void) close(ddfd);
279 if (sdfd >= 0) (void) close(sdfd);
281 /* A return of FALSE means that if there was an error, a common error was
282 put in the first address of a batch. */
286 #endif /*!MACRO_PREDEF*/
287 #endif /*EXPERIMENTAL_QUEUEFILE*/