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. */
12 #include "queuefile.h"
14 #ifndef EXIM_HAVE_OPENAT
15 # error queuefile transport reqires openat() support
18 /* Options specific to the appendfile transport. They must be in alphabetic
19 order (note that "_" comes before the lower case letters). Some of them are
20 stored in the publicly visible instance block - these are flagged with the
23 optionlist queuefile_transport_options[] = {
24 { "directory", opt_stringptr,
25 OPT_OFF(queuefile_transport_options_block, dirname) },
29 /* Size of the options list. An extern variable has to be used so that its
30 address can appear in the tables drtables.c. */
32 int queuefile_transport_options_count =
33 sizeof(queuefile_transport_options) / sizeof(optionlist);
39 queuefile_transport_options_block queuefile_transport_option_defaults = {0};
40 void queuefile_transport_init(transport_instance *tblock) {}
41 BOOL queuefile_transport_entry(transport_instance *tblock, address_item *addr) {return FALSE;}
43 #else /*!MACRO_PREDEF*/
47 /* Default private options block for the appendfile transport. */
49 queuefile_transport_options_block queuefile_transport_option_defaults = {
53 /*************************************************
54 * Initialization entry point *
55 *************************************************/
57 void queuefile_transport_init(transport_instance *tblock)
59 queuefile_transport_options_block *ob =
60 (queuefile_transport_options_block *) tblock->options_block;
63 log_write(0, LOG_PANIC_DIE | LOG_CONFIG,
64 "directory must be set for the %s transport", tblock->name);
67 /* This function will copy from a file to another
70 dst fd to write to (the destination queue file)
71 src fd to read from (the spool queue file)
73 Returns: TRUE if all went well, FALSE otherwise with errno set
77 copy_spool_file(int dst, int src)
82 if (lseek(src, 0, SEEK_SET) != 0)
86 if ((j = read(src, buffer, sizeof(buffer))) > 0)
87 for (uschar * s = buffer; (i = write(dst, s, j)) != j; s += i, j -= i)
96 /* This function performs the actual copying of the header
97 and data files to the destination directory
100 tb the transport block
101 addr address_item being processed
102 dstpath destination directory name
103 sdfd int Source directory fd
104 ddfd int Destination directory fd
105 link_file BOOL use linkat instead of data copy
106 srcfd fd for data file, or -1 for header file
108 Returns: TRUE if all went well, FALSE otherwise
112 copy_spool_files(transport_instance * tb, address_item * addr,
113 const uschar * dstpath, int sdfd, int ddfd, BOOL link_file, int srcfd)
115 BOOL is_hdr_file = srcfd < 0;
116 const uschar * suffix = srcfd < 0 ? US"H" : US"D";
118 const uschar * filename = string_sprintf("%s-%s", message_id, suffix);
119 const uschar * srcpath = spool_fname(US"input", message_subdir, message_id, suffix);
120 const uschar * s, * op;
122 dstpath = string_sprintf("%s/%s-%s", dstpath, message_id, suffix);
126 DEBUG(D_transport) debug_printf("%s transport, linking %s => %s\n",
127 tb->name, srcpath, dstpath);
129 if (linkat(sdfd, CCS filename, ddfd, CCS filename, 0) >= 0)
135 else /* use data copy */
137 DEBUG(D_transport) debug_printf("%s transport, copying %s => %s\n",
138 tb->name, srcpath, dstpath);
141 (dstfd = exim_openat4(ddfd, CCS filename, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE))
145 && (s = srcpath, (srcfd = exim_openat(sdfd, CCS filename, O_RDONLY)) < 0)
150 if (s = dstpath, fchmod(dstfd, SPOOL_MODE) != 0)
151 op = US"setting perms on";
153 if (!copy_spool_file(dstfd, srcfd))
159 addr->basic_errno = errno;
160 addr->message = string_sprintf("%s transport %s file: %s failed with error: %s",
161 tb->name, op, s, strerror(errno));
162 addr->transport_return = DEFER;
166 /*************************************************
168 *************************************************/
170 /* This transport always returns FALSE, indicating that the status in
171 the first address is the status for all addresses in a batch. */
174 queuefile_transport_entry(transport_instance * tblock, address_item * addr)
176 queuefile_transport_options_block * ob =
177 (queuefile_transport_options_block *) tblock->options_block;
179 uschar * sourcedir = spool_dname(US"input", message_subdir);
180 uschar * s, * dstdir;
181 struct stat dstatbuf, sstatbuf;
182 int ddfd = -1, sdfd = -1;
185 debug_printf("%s transport entered\n", tblock->name);
188 # define O_DIRECTORY 0
191 # define O_NOFOLLOW 0
194 if (!(dstdir = expand_string(ob->dirname)))
196 addr->message = string_sprintf("%s transport: failed to expand dirname option",
198 addr->transport_return = DEFER;
203 addr->transport_return = PANIC;
204 addr->message = string_sprintf("%s transport directory: "
205 "%s is not absolute", tblock->name, dstdir);
209 /* Open the source and destination directories and check if they are
210 on the same filesystem, so we can hard-link files rather than copying. */
213 (ddfd = Uopen(s, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
215 (sdfd = Uopen(sourcedir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
218 addr->transport_return = PANIC;
219 addr->basic_errno = errno;
220 addr->message = string_sprintf("%s transport accessing directory: %s "
221 "failed with error: %s", tblock->name, s, strerror(errno));
222 if (ddfd >= 0) (void) close(ddfd);
226 if ( (s = dstdir, fstat(ddfd, &dstatbuf) < 0)
227 || (s = sourcedir, fstat(sdfd, &sstatbuf) < 0)
230 addr->transport_return = PANIC;
231 addr->basic_errno = errno;
232 addr->message = string_sprintf("%s transport fstat on directory fd: "
233 "%s failed with error: %s", tblock->name, s, strerror(errno));
236 can_link = (dstatbuf.st_dev == sstatbuf.st_dev);
241 debug_printf("*** delivery by %s transport bypassed by -N option\n",
243 addr->transport_return = OK;
247 /* Link or copy the header and data spool files */
250 debug_printf("%s transport, copying header file\n", tblock->name);
252 if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link, -1))
256 debug_printf("%s transport, copying data file\n", tblock->name);
258 if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link,
262 debug_printf("%s transport, copying data file failed, "
263 "unlinking the header file\n", tblock->name);
264 Uunlink(string_sprintf("%s/%s-H", dstdir, message_id));
269 debug_printf("%s transport succeeded\n", tblock->name);
271 addr->transport_return = OK;
274 if (ddfd >= 0) (void) close(ddfd);
275 if (sdfd >= 0) (void) close(sdfd);
277 /* A return of FALSE means that if there was an error, a common error was
278 put in the first address of a batch. */
282 #endif /*!MACRO_PREDEF*/