DANE: move to mainline
[exim.git] / src / src / transports / queuefile.c
CommitLineData
3369a853
ACK
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
5/* Copyright (c) Andrew Colin Kissa <andrew@topdog.za.net> 2016 */
6/* Copyright (c) University of Cambridge 2016 */
f9ba5e22 7/* Copyright (c) The Exim Maintainers 1995 - 2018 */
3369a853
ACK
8/* See the file NOTICE for conditions of use and distribution. */
9
10
11#include "../exim.h"
12#include "queuefile.h"
13
14/* Options specific to the appendfile transport. They must be in alphabetic
15order (note that "_" comes before the lower case letters). Some of them are
16stored in the publicly visible instance block - these are flagged with the
17opt_public flag. */
18
19optionlist queuefile_transport_options[] = {
20 { "directory", opt_stringptr,
21 (void *)offsetof(queuefile_transport_options_block, dirname) },
22};
23
d185889f 24
3369a853
ACK
25/* Size of the options list. An extern variable has to be used so that its
26address can appear in the tables drtables.c. */
27
28int queuefile_transport_options_count =
29 sizeof(queuefile_transport_options) / sizeof(optionlist);
30
d185889f
JH
31
32#ifdef MACRO_PREDEF
33
34/* Dummy values */
35queuefile_transport_options_block queuefile_transport_option_defaults = {0};
36void queuefile_transport_init(transport_instance *tblock) {}
37BOOL queuefile_transport_entry(transport_instance *tblock, address_item *addr) {return FALSE;}
38
39#else /*!MACRO_PREDEF*/
40
41
42
3369a853
ACK
43/* Default private options block for the appendfile transport. */
44
45queuefile_transport_options_block queuefile_transport_option_defaults = {
46 NULL, /* dirname */
47};
48
49/*************************************************
50* Initialization entry point *
51*************************************************/
52
53void queuefile_transport_init(transport_instance *tblock)
54{
55queuefile_transport_options_block *ob =
f09fe41f 56 (queuefile_transport_options_block *) tblock->options_block;
3369a853 57
f09fe41f 58if (!ob->dirname)
3369a853
ACK
59 log_write(0, LOG_PANIC_DIE | LOG_CONFIG,
60 "directory must be set for the %s transport", tblock->name);
61}
62
63/* This function will copy from a file to another
64
65Arguments:
b4758425
JH
66 dst fd to write to (the destination queue file)
67 src fd to read from (the spool queue file)
3369a853 68
b4758425 69Returns: TRUE if all went well, FALSE otherwise with errno set
3369a853
ACK
70*/
71
f09fe41f 72static BOOL
b4758425 73copy_spool_file(int dst, int src)
3369a853
ACK
74{
75int i, j;
76uschar buffer[16384];
b4758425 77uschar * s;
3369a853 78
b4758425
JH
79if (lseek(src, 0, SEEK_SET) != 0)
80 return FALSE;
3369a853
ACK
81
82do
b4758425
JH
83 if ((j = read(src, buffer, sizeof(buffer))) > 0)
84 for (s = buffer; (i = write(dst, s, j)) != j; s += i, j -= i)
85 if (i < 0)
86 return FALSE;
87 else if (j < 0)
88 return FALSE;
3369a853
ACK
89while (j > 0);
90return TRUE;
91}
92
93/* This function performs the actual copying of the header
94and data files to the destination directory
95
96Arguments:
f09fe41f 97 tb the transport block
3369a853
ACK
98 addr address_item being processed
99 sdfd int Source directory fd
100 ddfd int Destination directory fd
3369a853 101 link_file BOOL use linkat instead of data copy
f09fe41f 102 srcfd fd for data file, or -1 for header file
3369a853
ACK
103
104Returns: TRUE if all went well, FALSE otherwise
105*/
106
f09fe41f
JH
107static BOOL
108copy_spool_files(transport_instance * tb, address_item * addr,
109 int sdfd, int ddfd, BOOL link_file, int srcfd)
3369a853 110{
f09fe41f 111BOOL is_hdr_file = srcfd < 0;
b4758425 112const uschar * suffix = srcfd < 0 ? US"H" : US"D";
f09fe41f 113int dstfd;
b4758425
JH
114const uschar * filename = string_sprintf("%s-%s", message_id, suffix);
115const uschar * srcpath = spool_fname(US"input", message_subdir, message_id, suffix);
116const uschar * dstpath = string_sprintf("%s/%s-%s",
f09fe41f
JH
117 ((queuefile_transport_options_block *) tb->options_block)->dirname,
118 message_id, suffix);
b4758425
JH
119const uschar * s;
120const uschar * op;
3369a853
ACK
121
122if (link_file)
123 {
f09fe41f
JH
124 DEBUG(D_transport) debug_printf("%s transport, linking %s => %s\n",
125 tb->name, srcpath, dstpath);
126
b4758425
JH
127 if (linkat(sdfd, CCS filename, ddfd, CCS filename, 0) >= 0)
128 return TRUE;
129
130 op = US"linking";
131 s = dstpath;
3369a853 132 }
d88f0784
JH
133else /* use data copy */
134 {
135 DEBUG(D_transport) debug_printf("%s transport, copying %s => %s\n",
136 tb->name, srcpath, dstpath);
3369a853 137
d88f0784
JH
138 if ( (s = dstpath,
139 (dstfd = openat(ddfd, CCS filename, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE))
140 < 0
141 )
142 || is_hdr_file
143 && (s = srcpath, (srcfd = openat(sdfd, CCS filename, O_RDONLY)) < 0)
144 )
145 op = US"opening";
b4758425 146
b4758425 147 else
d88f0784
JH
148 if (s = dstpath, fchmod(dstfd, SPOOL_MODE) != 0)
149 op = US"setting perms on";
b4758425 150 else
d88f0784
JH
151 if (!copy_spool_file(dstfd, srcfd))
152 op = US"creating";
153 else
154 return TRUE;
155 }
b4758425
JH
156
157addr->basic_errno = errno;
158addr->message = string_sprintf("%s transport %s file: %s failed with error: %s",
159 tb->name, op, s, strerror(errno));
160addr->transport_return = DEFER;
161return FALSE;
3369a853
ACK
162}
163
164/*************************************************
165* Main entry point *
166*************************************************/
167
168/* This transport always returns FALSE, indicating that the status in
169the first address is the status for all addresses in a batch. */
170
f09fe41f
JH
171BOOL
172queuefile_transport_entry(transport_instance * tblock, address_item * addr)
3369a853 173{
f09fe41f
JH
174queuefile_transport_options_block * ob =
175 (queuefile_transport_options_block *) tblock->options_block;
176BOOL can_link;
177uschar * sourcedir = spool_dname(US"input", message_subdir);
178uschar * s;
179struct stat dstatbuf, sstatbuf;
180int ddfd = -1, sdfd = -1;
3369a853
ACK
181
182DEBUG(D_transport)
183 debug_printf("%s transport entered\n", tblock->name);
184
f09fe41f
JH
185#ifndef O_DIRECTORY
186# define O_DIRECTORY 0
187#endif
188#ifndef O_NOFOLLOW
189# define O_NOFOLLOW 0
3369a853
ACK
190#endif
191
192if (ob->dirname[0] != '/')
193 {
194 addr->transport_return = PANIC;
195 addr->message = string_sprintf("%s transport directory: "
196 "%s is not absolute", tblock->name, ob->dirname);
197 return FALSE;
198 }
199
f09fe41f
JH
200/* Open the source and destination directories and check if they are
201on the same filesystem, so we can hard-link files rather than copying. */
3369a853 202
f09fe41f
JH
203if ( (s = ob->dirname,
204 (ddfd = Uopen(s, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
205 || (s = sourcedir,
206 (sdfd = Uopen(sourcedir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
207 )
3369a853
ACK
208 {
209 addr->transport_return = PANIC;
210 addr->basic_errno = errno;
211 addr->message = string_sprintf("%s transport accessing directory: %s "
f09fe41f
JH
212 "failed with error: %s", tblock->name, s, strerror(errno));
213 if (ddfd >= 0) (void) close(ddfd);
214 return FALSE;
3369a853
ACK
215 }
216
f09fe41f
JH
217if ( (s = ob->dirname, fstat(ddfd, &dstatbuf) < 0)
218 || (s = sourcedir, fstat(sdfd, &sstatbuf) < 0)
219 )
3369a853
ACK
220 {
221 addr->transport_return = PANIC;
222 addr->basic_errno = errno;
223 addr->message = string_sprintf("%s transport fstat on directory fd: "
f09fe41f 224 "%s failed with error: %s", tblock->name, s, strerror(errno));
3369a853
ACK
225 goto RETURN;
226 }
f09fe41f 227can_link = (dstatbuf.st_dev == sstatbuf.st_dev);
3369a853
ACK
228
229if (dont_deliver)
230 {
231 DEBUG(D_transport)
232 debug_printf("*** delivery by %s transport bypassed by -N option\n",
233 tblock->name);
234 addr->transport_return = OK;
235 goto RETURN;
236 }
237
f09fe41f
JH
238/* Link or copy the header and data spool files */
239
3369a853
ACK
240DEBUG(D_transport)
241 debug_printf("%s transport, copying header file\n", tblock->name);
242
f09fe41f 243if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, -1))
3369a853
ACK
244 goto RETURN;
245
3369a853
ACK
246DEBUG(D_transport)
247 debug_printf("%s transport, copying data file\n", tblock->name);
248
f09fe41f 249if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, deliver_datafile))
3369a853
ACK
250 {
251 DEBUG(D_transport)
252 debug_printf("%s transport, copying data file failed, "
253 "unlinking the header file\n", tblock->name);
254 Uunlink(string_sprintf("%s/%s-H", ob->dirname, message_id));
255 goto RETURN;
256 }
257
3369a853
ACK
258DEBUG(D_transport)
259 debug_printf("%s transport succeeded\n", tblock->name);
260
261addr->transport_return = OK;
262
263RETURN:
f09fe41f
JH
264if (ddfd >= 0) (void) close(ddfd);
265if (sdfd >= 0) (void) close(sdfd);
3369a853
ACK
266
267/* A return of FALSE means that if there was an error, a common error was
268put in the first address of a batch. */
269return FALSE;
270}
d185889f
JH
271
272#endif /*!MACRO_PREDEF*/