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