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 */ | |
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 | |
14 | order (note that "_" comes before the lower case letters). Some of them are | |
15 | stored in the publicly visible instance block - these are flagged with the | |
16 | opt_public flag. */ | |
17 | ||
18 | optionlist 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 |
25 | address can appear in the tables drtables.c. */ | |
26 | ||
27 | int queuefile_transport_options_count = | |
28 | sizeof(queuefile_transport_options) / sizeof(optionlist); | |
29 | ||
d185889f JH |
30 | |
31 | #ifdef MACRO_PREDEF | |
32 | ||
33 | /* Dummy values */ | |
34 | queuefile_transport_options_block queuefile_transport_option_defaults = {0}; | |
35 | void queuefile_transport_init(transport_instance *tblock) {} | |
36 | BOOL 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 | ||
44 | queuefile_transport_options_block queuefile_transport_option_defaults = { | |
45 | NULL, /* dirname */ | |
46 | }; | |
47 | ||
48 | /************************************************* | |
49 | * Initialization entry point * | |
50 | *************************************************/ | |
51 | ||
52 | void queuefile_transport_init(transport_instance *tblock) | |
53 | { | |
54 | queuefile_transport_options_block *ob = | |
f09fe41f | 55 | (queuefile_transport_options_block *) tblock->options_block; |
3369a853 | 56 | |
f09fe41f | 57 | if (!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 | ||
64 | Arguments: | |
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 | 68 | Returns: TRUE if all went well, FALSE otherwise with errno set |
3369a853 ACK |
69 | */ |
70 | ||
f09fe41f | 71 | static BOOL |
b4758425 | 72 | copy_spool_file(int dst, int src) |
3369a853 ACK |
73 | { |
74 | int i, j; | |
75 | uschar buffer[16384]; | |
b4758425 | 76 | uschar * s; |
3369a853 | 77 | |
b4758425 JH |
78 | if (lseek(src, 0, SEEK_SET) != 0) |
79 | return FALSE; | |
3369a853 ACK |
80 | |
81 | do | |
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 |
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 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 | |
103 | Returns: TRUE if all went well, FALSE otherwise | |
104 | */ | |
105 | ||
f09fe41f JH |
106 | static BOOL |
107 | copy_spool_files(transport_instance * tb, address_item * addr, | |
108 | int sdfd, int ddfd, BOOL link_file, int srcfd) | |
3369a853 | 109 | { |
f09fe41f | 110 | BOOL is_hdr_file = srcfd < 0; |
b4758425 | 111 | const uschar * suffix = srcfd < 0 ? US"H" : US"D"; |
f09fe41f | 112 | int dstfd; |
b4758425 JH |
113 | const uschar * filename = string_sprintf("%s-%s", message_id, suffix); |
114 | const uschar * srcpath = spool_fname(US"input", message_subdir, message_id, suffix); | |
115 | const uschar * dstpath = string_sprintf("%s/%s-%s", | |
f09fe41f JH |
116 | ((queuefile_transport_options_block *) tb->options_block)->dirname, |
117 | message_id, suffix); | |
b4758425 JH |
118 | const uschar * s; |
119 | const uschar * op; | |
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); | |
177 | uschar * s; | |
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 | ||
191 | if (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 |
200 | on the same filesystem, so we can hard-link files rather than copying. */ | |
3369a853 | 201 | |
f09fe41f JH |
202 | if ( (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 |
216 | if ( (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 | 226 | can_link = (dstatbuf.st_dev == sstatbuf.st_dev); |
3369a853 ACK |
227 | |
228 | if (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 |
239 | DEBUG(D_transport) |
240 | debug_printf("%s transport, copying header file\n", tblock->name); | |
241 | ||
f09fe41f | 242 | if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, -1)) |
3369a853 ACK |
243 | goto RETURN; |
244 | ||
3369a853 ACK |
245 | DEBUG(D_transport) |
246 | debug_printf("%s transport, copying data file\n", tblock->name); | |
247 | ||
f09fe41f | 248 | if (!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 |
257 | DEBUG(D_transport) |
258 | debug_printf("%s transport succeeded\n", tblock->name); | |
259 | ||
260 | addr->transport_return = OK; | |
261 | ||
262 | RETURN: | |
f09fe41f JH |
263 | if (ddfd >= 0) (void) close(ddfd); |
264 | if (sdfd >= 0) (void) close(sdfd); | |
3369a853 ACK |
265 | |
266 | /* A return of FALSE means that if there was an error, a common error was | |
267 | put in the first address of a batch. */ | |
268 | return FALSE; | |
269 | } | |
d185889f JH |
270 | |
271 | #endif /*!MACRO_PREDEF*/ |