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