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, | |
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 |
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]; | |
b4758425 | 77 | uschar * s; |
3369a853 | 78 | |
b4758425 JH |
79 | if (lseek(src, 0, SEEK_SET) != 0) |
80 | return FALSE; | |
3369a853 ACK |
81 | |
82 | do | |
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 |
89 | while (j > 0); |
90 | return TRUE; | |
91 | } | |
92 | ||
93 | /* This function performs the actual copying of the header | |
94 | and data files to the destination directory | |
95 | ||
96 | Arguments: | |
f09fe41f | 97 | tb the transport block |
3369a853 | 98 | addr address_item being processed |
9604413b | 99 | dstpath destination directory name |
3369a853 ACK |
100 | sdfd int Source directory fd |
101 | ddfd int Destination directory fd | |
3369a853 | 102 | link_file BOOL use linkat instead of data copy |
f09fe41f | 103 | srcfd fd for data file, or -1 for header file |
3369a853 ACK |
104 | |
105 | Returns: TRUE if all went well, FALSE otherwise | |
106 | */ | |
107 | ||
f09fe41f JH |
108 | static BOOL |
109 | copy_spool_files(transport_instance * tb, address_item * addr, | |
9604413b | 110 | const uschar * dstpath, int sdfd, int ddfd, BOOL link_file, int srcfd) |
3369a853 | 111 | { |
f09fe41f | 112 | BOOL is_hdr_file = srcfd < 0; |
b4758425 | 113 | const uschar * suffix = srcfd < 0 ? US"H" : US"D"; |
f09fe41f | 114 | int dstfd; |
b4758425 JH |
115 | const uschar * filename = string_sprintf("%s-%s", message_id, suffix); |
116 | const uschar * srcpath = spool_fname(US"input", message_subdir, message_id, suffix); | |
9604413b JH |
117 | const uschar * s, * op; |
118 | ||
119 | dstpath = string_sprintf("%s/%s-%s", dstpath, message_id, suffix); | |
3369a853 ACK |
120 | |
121 | if (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 |
132 | else /* 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 | |
156 | addr->basic_errno = errno; | |
157 | addr->message = string_sprintf("%s transport %s file: %s failed with error: %s", | |
158 | tb->name, op, s, strerror(errno)); | |
159 | addr->transport_return = DEFER; | |
160 | return FALSE; | |
3369a853 ACK |
161 | } |
162 | ||
163 | /************************************************* | |
164 | * Main entry point * | |
165 | *************************************************/ | |
166 | ||
167 | /* This transport always returns FALSE, indicating that the status in | |
168 | the first address is the status for all addresses in a batch. */ | |
169 | ||
f09fe41f JH |
170 | BOOL |
171 | queuefile_transport_entry(transport_instance * tblock, address_item * addr) | |
3369a853 | 172 | { |
f09fe41f JH |
173 | queuefile_transport_options_block * ob = |
174 | (queuefile_transport_options_block *) tblock->options_block; | |
175 | BOOL can_link; | |
176 | uschar * sourcedir = spool_dname(US"input", message_subdir); | |
9604413b | 177 | uschar * s, * dstdir; |
f09fe41f JH |
178 | struct stat dstatbuf, sstatbuf; |
179 | int ddfd = -1, sdfd = -1; | |
3369a853 ACK |
180 | |
181 | DEBUG(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 | ||
9604413b JH |
191 | if (!(dstdir = expand_string(ob->dirname))) |
192 | { | |
193 | addr->message = string_sprintf("%s transport: failed to expand dirname option", | |
194 | tblock->name); | |
195 | addr->transport_return = DEFER; | |
196 | return FALSE; | |
197 | } | |
198 | if (*dstdir != '/') | |
3369a853 ACK |
199 | { |
200 | addr->transport_return = PANIC; | |
201 | addr->message = string_sprintf("%s transport directory: " | |
9604413b | 202 | "%s is not absolute", tblock->name, dstdir); |
3369a853 ACK |
203 | return FALSE; |
204 | } | |
205 | ||
f09fe41f JH |
206 | /* Open the source and destination directories and check if they are |
207 | on the same filesystem, so we can hard-link files rather than copying. */ | |
3369a853 | 208 | |
9604413b | 209 | if ( (s = dstdir, |
f09fe41f JH |
210 | (ddfd = Uopen(s, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0) |
211 | || (s = sourcedir, | |
212 | (sdfd = Uopen(sourcedir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0) | |
213 | ) | |
3369a853 ACK |
214 | { |
215 | addr->transport_return = PANIC; | |
216 | addr->basic_errno = errno; | |
217 | addr->message = string_sprintf("%s transport accessing directory: %s " | |
f09fe41f JH |
218 | "failed with error: %s", tblock->name, s, strerror(errno)); |
219 | if (ddfd >= 0) (void) close(ddfd); | |
220 | return FALSE; | |
3369a853 ACK |
221 | } |
222 | ||
9604413b JH |
223 | if ( (s = dstdir, fstat(ddfd, &dstatbuf) < 0) |
224 | || (s = sourcedir, fstat(sdfd, &sstatbuf) < 0) | |
f09fe41f | 225 | ) |
3369a853 ACK |
226 | { |
227 | addr->transport_return = PANIC; | |
228 | addr->basic_errno = errno; | |
229 | addr->message = string_sprintf("%s transport fstat on directory fd: " | |
f09fe41f | 230 | "%s failed with error: %s", tblock->name, s, strerror(errno)); |
3369a853 ACK |
231 | goto RETURN; |
232 | } | |
f09fe41f | 233 | can_link = (dstatbuf.st_dev == sstatbuf.st_dev); |
3369a853 ACK |
234 | |
235 | if (dont_deliver) | |
236 | { | |
237 | DEBUG(D_transport) | |
238 | debug_printf("*** delivery by %s transport bypassed by -N option\n", | |
239 | tblock->name); | |
240 | addr->transport_return = OK; | |
241 | goto RETURN; | |
242 | } | |
243 | ||
f09fe41f JH |
244 | /* Link or copy the header and data spool files */ |
245 | ||
3369a853 ACK |
246 | DEBUG(D_transport) |
247 | debug_printf("%s transport, copying header file\n", tblock->name); | |
248 | ||
9604413b | 249 | if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link, -1)) |
3369a853 ACK |
250 | goto RETURN; |
251 | ||
3369a853 ACK |
252 | DEBUG(D_transport) |
253 | debug_printf("%s transport, copying data file\n", tblock->name); | |
254 | ||
9604413b JH |
255 | if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link, |
256 | deliver_datafile)) | |
3369a853 ACK |
257 | { |
258 | DEBUG(D_transport) | |
259 | debug_printf("%s transport, copying data file failed, " | |
260 | "unlinking the header file\n", tblock->name); | |
9604413b | 261 | Uunlink(string_sprintf("%s/%s-H", dstdir, message_id)); |
3369a853 ACK |
262 | goto RETURN; |
263 | } | |
264 | ||
3369a853 ACK |
265 | DEBUG(D_transport) |
266 | debug_printf("%s transport succeeded\n", tblock->name); | |
267 | ||
268 | addr->transport_return = OK; | |
269 | ||
270 | RETURN: | |
f09fe41f JH |
271 | if (ddfd >= 0) (void) close(ddfd); |
272 | if (sdfd >= 0) (void) close(sdfd); | |
3369a853 ACK |
273 | |
274 | /* A return of FALSE means that if there was an error, a common error was | |
275 | put in the first address of a batch. */ | |
276 | return FALSE; | |
277 | } | |
d185889f JH |
278 | |
279 | #endif /*!MACRO_PREDEF*/ |