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 GET_OPTION("directory");
199 if (!(dstdir = expand_string(ob->dirname)))
201 addr->message = string_sprintf("%s transport: failed to expand dirname option",
203 addr->transport_return = DEFER;
208 addr->transport_return = PANIC;
209 addr->message = string_sprintf("%s transport directory: "
210 "%s is not absolute", tblock->name, dstdir);
214 /* Open the source and destination directories and check if they are
215 on the same filesystem, so we can hard-link files rather than copying. */
218 (ddfd = Uopen(s, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
220 (sdfd = Uopen(sourcedir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
223 addr->transport_return = PANIC;
224 addr->basic_errno = errno;
225 addr->message = string_sprintf("%s transport accessing directory: %s "
226 "failed with error: %s", tblock->name, s, strerror(errno));
227 if (ddfd >= 0) (void) close(ddfd);
231 if ( (s = dstdir, fstat(ddfd, &dstatbuf) < 0)
232 || (s = sourcedir, fstat(sdfd, &sstatbuf) < 0)
235 addr->transport_return = PANIC;
236 addr->basic_errno = errno;
237 addr->message = string_sprintf("%s transport fstat on directory fd: "
238 "%s failed with error: %s", tblock->name, s, strerror(errno));
241 can_link = (dstatbuf.st_dev == sstatbuf.st_dev);
246 debug_printf("*** delivery by %s transport bypassed by -N option\n",
248 addr->transport_return = OK;
252 /* Link or copy the header and data spool files */
255 debug_printf("%s transport, copying header file\n", tblock->name);
257 if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link, -1))
261 debug_printf("%s transport, copying data file\n", tblock->name);
263 if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link,
267 debug_printf("%s transport, copying data file failed, "
268 "unlinking the header file\n", tblock->name);
269 Uunlink(string_sprintf("%s/%s-H", dstdir, message_id));
274 debug_printf("%s transport succeeded\n", tblock->name);
276 addr->transport_return = OK;
279 if (ddfd >= 0) (void) close(ddfd);
280 if (sdfd >= 0) (void) close(sdfd);
282 /* A return of FALSE means that if there was an error, a common error was
283 put in the first address of a batch. */
287 #endif /*!MACRO_PREDEF*/
288 #endif /*EXPERIMENTAL_QUEUEFILE*/