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