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 - 2020 */
8 /* See the file NOTICE for conditions of use and distribution. */
14 #ifdef EXPERIMENTAL_QUEUEFILE /* whole file */
15 #include "queuefile.h"
17 #ifndef EXIM_HAVE_OPENAT
18 # error queuefile transport reqires openat() support
21 /* Options specific to the appendfile transport. They must be in alphabetic
22 order (note that "_" comes before the lower case letters). Some of them are
23 stored in the publicly visible instance block - these are flagged with the
26 optionlist queuefile_transport_options[] = {
27 { "directory", opt_stringptr,
28 OPT_OFF(queuefile_transport_options_block, dirname) },
32 /* Size of the options list. An extern variable has to be used so that its
33 address can appear in the tables drtables.c. */
35 int queuefile_transport_options_count =
36 sizeof(queuefile_transport_options) / sizeof(optionlist);
42 queuefile_transport_options_block queuefile_transport_option_defaults = {0};
43 void queuefile_transport_init(transport_instance *tblock) {}
44 BOOL queuefile_transport_entry(transport_instance *tblock, address_item *addr) {return FALSE;}
46 #else /*!MACRO_PREDEF*/
50 /* Default private options block for the appendfile transport. */
52 queuefile_transport_options_block queuefile_transport_option_defaults = {
56 /*************************************************
57 * Initialization entry point *
58 *************************************************/
60 void queuefile_transport_init(transport_instance *tblock)
62 queuefile_transport_options_block *ob =
63 (queuefile_transport_options_block *) tblock->options_block;
66 log_write(0, LOG_PANIC_DIE | LOG_CONFIG,
67 "directory must be set for the %s transport", tblock->name);
70 /* This function will copy from a file to another
73 dst fd to write to (the destination queue file)
74 src fd to read from (the spool queue file)
76 Returns: TRUE if all went well, FALSE otherwise with errno set
80 copy_spool_file(int dst, int src)
85 if (lseek(src, 0, SEEK_SET) != 0)
89 if ((j = read(src, buffer, sizeof(buffer))) > 0)
90 for (uschar * s = buffer; (i = write(dst, s, j)) != j; s += i, j -= i)
99 /* This function performs the actual copying of the header
100 and data files to the destination directory
103 tb the transport block
104 addr address_item being processed
105 dstpath destination directory name
106 sdfd int Source directory fd
107 ddfd int Destination directory fd
108 link_file BOOL use linkat instead of data copy
109 srcfd fd for data file, or -1 for header file
111 Returns: TRUE if all went well, FALSE otherwise
115 copy_spool_files(transport_instance * tb, address_item * addr,
116 const uschar * dstpath, int sdfd, int ddfd, BOOL link_file, int srcfd)
118 BOOL is_hdr_file = srcfd < 0;
119 const uschar * suffix = srcfd < 0 ? US"H" : US"D";
121 const uschar * filename = string_sprintf("%s-%s", message_id, suffix);
122 const uschar * srcpath = spool_fname(US"input", message_subdir, message_id, suffix);
123 const uschar * s, * op;
125 dstpath = string_sprintf("%s/%s-%s", dstpath, message_id, suffix);
129 DEBUG(D_transport) debug_printf("%s transport, linking %s => %s\n",
130 tb->name, srcpath, dstpath);
132 if (linkat(sdfd, CCS filename, ddfd, CCS filename, 0) >= 0)
138 else /* use data copy */
140 DEBUG(D_transport) debug_printf("%s transport, copying %s => %s\n",
141 tb->name, srcpath, dstpath);
144 (dstfd = exim_openat4(ddfd, CCS filename, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE))
148 && (s = srcpath, (srcfd = exim_openat(sdfd, CCS filename, O_RDONLY)) < 0)
153 if (s = dstpath, fchmod(dstfd, SPOOL_MODE) != 0)
154 op = US"setting perms on";
156 if (!copy_spool_file(dstfd, srcfd))
162 addr->basic_errno = errno;
163 addr->message = string_sprintf("%s transport %s file: %s failed with error: %s",
164 tb->name, op, s, strerror(errno));
165 addr->transport_return = DEFER;
169 /*************************************************
171 *************************************************/
173 /* This transport always returns FALSE, indicating that the status in
174 the first address is the status for all addresses in a batch. */
177 queuefile_transport_entry(transport_instance * tblock, address_item * addr)
179 queuefile_transport_options_block * ob =
180 (queuefile_transport_options_block *) tblock->options_block;
182 uschar * sourcedir = spool_dname(US"input", message_subdir);
183 uschar * s, * dstdir;
184 struct stat dstatbuf, sstatbuf;
185 int ddfd = -1, sdfd = -1;
188 debug_printf("%s transport entered\n", tblock->name);
191 # define O_DIRECTORY 0
194 # define O_NOFOLLOW 0
197 if (!(dstdir = expand_string(ob->dirname)))
199 addr->message = string_sprintf("%s transport: failed to expand dirname option",
201 addr->transport_return = DEFER;
206 addr->transport_return = PANIC;
207 addr->message = string_sprintf("%s transport directory: "
208 "%s is not absolute", tblock->name, dstdir);
212 /* Open the source and destination directories and check if they are
213 on the same filesystem, so we can hard-link files rather than copying. */
216 (ddfd = Uopen(s, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
218 (sdfd = Uopen(sourcedir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
221 addr->transport_return = PANIC;
222 addr->basic_errno = errno;
223 addr->message = string_sprintf("%s transport accessing directory: %s "
224 "failed with error: %s", tblock->name, s, strerror(errno));
225 if (ddfd >= 0) (void) close(ddfd);
229 if ( (s = dstdir, fstat(ddfd, &dstatbuf) < 0)
230 || (s = sourcedir, fstat(sdfd, &sstatbuf) < 0)
233 addr->transport_return = PANIC;
234 addr->basic_errno = errno;
235 addr->message = string_sprintf("%s transport fstat on directory fd: "
236 "%s failed with error: %s", tblock->name, s, strerror(errno));
239 can_link = (dstatbuf.st_dev == sstatbuf.st_dev);
244 debug_printf("*** delivery by %s transport bypassed by -N option\n",
246 addr->transport_return = OK;
250 /* Link or copy the header and data spool files */
253 debug_printf("%s transport, copying header file\n", tblock->name);
255 if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link, -1))
259 debug_printf("%s transport, copying data file\n", tblock->name);
261 if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link,
265 debug_printf("%s transport, copying data file failed, "
266 "unlinking the header file\n", tblock->name);
267 Uunlink(string_sprintf("%s/%s-H", dstdir, message_id));
272 debug_printf("%s transport succeeded\n", tblock->name);
274 addr->transport_return = OK;
277 if (ddfd >= 0) (void) close(ddfd);
278 if (sdfd >= 0) (void) close(sdfd);
280 /* A return of FALSE means that if there was an error, a common error was
281 put in the first address of a batch. */
285 #endif /*!MACRO_PREDEF*/
286 #endif /*EXPERIMENTAL_QUEUEFILE*/