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