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 /* See the file NOTICE for conditions of use and distribution. */
11 #include "queuefile.h"
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
18 optionlist queuefile_transport_options[] = {
19 { "directory", opt_stringptr,
20 (void *)offsetof(queuefile_transport_options_block, dirname) },
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. */
26 int queuefile_transport_options_count =
27 sizeof(queuefile_transport_options) / sizeof(optionlist);
29 /* Default private options block for the appendfile transport. */
31 queuefile_transport_options_block queuefile_transport_option_defaults = {
35 /*************************************************
36 * Initialization entry point *
37 *************************************************/
39 void queuefile_transport_init(transport_instance *tblock)
41 queuefile_transport_options_block *ob =
42 (queuefile_transport_options_block *) tblock->options_block;
45 log_write(0, LOG_PANIC_DIE | LOG_CONFIG,
46 "directory must be set for the %s transport", tblock->name);
49 /* This function will copy from a file to another
52 to_fd FILE to write to (the destination queue file)
53 from_fd FILE to read from (the spool queue file)
55 Returns: TRUE if all went well, FALSE otherwise
59 copy_spool_file (FILE *to_fd, FILE *from_fd)
67 if ((j = fread(buffer, 1, sizeof(buffer), from_fd)) > 0)
68 if ((i = fwrite(buffer, j, 1, to_fd)) != 1)
74 /* This function performs the actual copying of the header
75 and data files to the destination directory
78 tb the transport block
79 addr address_item being processed
80 sdfd int Source directory fd
81 ddfd int Destination directory fd
82 link_file BOOL use linkat instead of data copy
83 srcfd fd for data file, or -1 for header file
85 Returns: TRUE if all went well, FALSE otherwise
89 copy_spool_files(transport_instance * tb, address_item * addr,
90 int sdfd, int ddfd, BOOL link_file, int srcfd)
92 BOOL is_hdr_file = srcfd < 0;
93 uschar * suffix = srcfd < 0 ? US"H" : US"D";
95 FILE * dst_file, * src_file;
96 uschar * filename = string_sprintf("%s-%s", message_id, suffix);
97 uschar * srcpath = spool_fname(US"input", message_subdir, message_id, suffix);
98 uschar * dstpath = string_sprintf("%s/%s-%s",
99 ((queuefile_transport_options_block *) tb->options_block)->dirname,
104 DEBUG(D_transport) debug_printf("%s transport, linking %s => %s\n",
105 tb->name, srcpath, dstpath);
107 return linkat(sdfd, CCS filename, ddfd, CCS filename, 0) >= 0;
112 DEBUG(D_transport) debug_printf("%s transport, copying %s => %s\n",
113 tb->name, srcpath, dstpath);
116 openat(ddfd, CCS filename, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE)) < 0)
118 addr->transport_return = DEFER;
119 addr->basic_errno = errno;
120 addr->message = string_sprintf("%s transport opening file: %s "
121 "failed with error: %s", tb->name, dstpath, strerror(errno));
125 fchmod(dstfd, SPOOL_MODE);
127 if (!(dst_file = fdopen(dstfd, "wb")))
129 addr->transport_return = DEFER;
130 addr->basic_errno = errno;
131 addr->message = string_sprintf("%s transport opening file fd: %s "
132 "failed with error: %s", tb->name, dstpath, strerror(errno));
138 if ((srcfd = openat(sdfd, CCS filename, O_RDONLY)) < 0)
140 addr->basic_errno = errno;
141 addr->message = string_sprintf("%s transport opening file: %s "
142 "failed with error: %s", tb->name, srcpath, strerror(errno));
146 if (!(src_file = fdopen(srcfd, "rb")))
148 addr->basic_errno = errno;
149 addr->message = string_sprintf("%s transport opening file fd: "
150 "%s failed with error: %s", tb->name, srcpath, strerror(errno));
151 if (is_hdr_file) (void) close(srcfd);
155 if (!copy_spool_file(dst_file, src_file))
157 addr->message = string_sprintf("%s transport creating file: "
158 "%s failed with error: %s", tb->name, dstpath, strerror(errno));
159 if (is_hdr_file) (void) fclose(src_file);
163 if (is_hdr_file) (void) fclose(src_file);
164 (void) fclose(dst_file);
169 addr->transport_return = DEFER;
170 (void) fclose(dst_file);
174 /*************************************************
176 *************************************************/
178 /* This transport always returns FALSE, indicating that the status in
179 the first address is the status for all addresses in a batch. */
182 queuefile_transport_entry(transport_instance * tblock, address_item * addr)
184 queuefile_transport_options_block * ob =
185 (queuefile_transport_options_block *) tblock->options_block;
187 uschar * sourcedir = spool_dname(US"input", message_subdir);
189 struct stat dstatbuf, sstatbuf;
190 int ddfd = -1, sdfd = -1;
193 debug_printf("%s transport entered\n", tblock->name);
196 # define O_DIRECTORY 0
199 # define O_NOFOLLOW 0
202 if (ob->dirname[0] != '/')
204 addr->transport_return = PANIC;
205 addr->message = string_sprintf("%s transport directory: "
206 "%s is not absolute", tblock->name, ob->dirname);
210 /* Open the source and destination directories and check if they are
211 on the same filesystem, so we can hard-link files rather than copying. */
213 if ( (s = ob->dirname,
214 (ddfd = Uopen(s, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
216 (sdfd = Uopen(sourcedir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
219 addr->transport_return = PANIC;
220 addr->basic_errno = errno;
221 addr->message = string_sprintf("%s transport accessing directory: %s "
222 "failed with error: %s", tblock->name, s, strerror(errno));
223 if (ddfd >= 0) (void) close(ddfd);
227 if ( (s = ob->dirname, fstat(ddfd, &dstatbuf) < 0)
228 || (s = sourcedir, fstat(sdfd, &sstatbuf) < 0)
231 addr->transport_return = PANIC;
232 addr->basic_errno = errno;
233 addr->message = string_sprintf("%s transport fstat on directory fd: "
234 "%s failed with error: %s", tblock->name, s, strerror(errno));
237 can_link = (dstatbuf.st_dev == sstatbuf.st_dev);
242 debug_printf("*** delivery by %s transport bypassed by -N option\n",
244 addr->transport_return = OK;
248 /* Link or copy the header and data spool files */
251 debug_printf("%s transport, copying header file\n", tblock->name);
253 if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, -1))
257 debug_printf("%s transport, copying data file\n", tblock->name);
259 if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, deliver_datafile))
262 debug_printf("%s transport, copying data file failed, "
263 "unlinking the header file\n", tblock->name);
264 Uunlink(string_sprintf("%s/%s-H", ob->dirname, 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. */