1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
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. */
11 #include "queuefile.h"
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
18 optionlist queuefile_transport_options
[] = {
19 { "directory", opt_stringptr
,
20 (void *)offsetof(queuefile_transport_options_block
, dirname
) },
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. */
26 int queuefile_transport_options_count
=
27 sizeof(queuefile_transport_options
) / sizeof(optionlist
);
29 /* Default private options block for the appendfile transport. */
31 queuefile_transport_options_block queuefile_transport_option_defaults
= {
35 /*************************************************
36 * Initialization entry point *
37 *************************************************/
39 void queuefile_transport_init(transport_instance
*tblock
)
41 queuefile_transport_options_block
*ob
=
42 (queuefile_transport_options_block
*)(tblock
->options_block
);
44 if (ob
->dirname
== NULL
)
45 log_write(0, LOG_PANIC_DIE
| LOG_CONFIG
,
46 "directory must be set for the %s transport", tblock
->name
);
49 /* This function will copy from a file to another
52 to_fd FILE to write to (the destination queue file)
53 from_fd FILE to read from (the spool queue file)
55 Returns: TRUE if all went well, FALSE otherwise
58 static BOOL
copy_spool_file (FILE *to_fd
, FILE *from_fd
)
67 j
= fread(buffer
, 1, sizeof(buffer
), from_fd
);
70 i
= fwrite(buffer
, j
, 1, to_fd
);
79 /* This function performs the actual copying of the header
80 and data files to the destination directory
83 tname uschar the transport name
84 addr address_item being processed
85 sdfd int Source directory fd
86 ddfd int Destination directory fd
87 suffix uschar file suffix
88 dirname uschar Destination directory
89 link_file BOOL use linkat instead of data copy
90 is_data_file BOOL the file is a data file not a header file
91 dst_file FILE to write to
92 src_file FILE to read from
94 Returns: TRUE if all went well, FALSE otherwise
97 static BOOL
copy_spool_files(uschar
*tname
, address_item
*addr
,
98 int sdfd
, int ddfd
, uschar
*suffix
, uschar
*dirname
, BOOL link_file
,
99 BOOL is_data_file
, FILE *dst_file
, FILE *src_file
)
103 uschar message_subdir[2];
104 message_subdir[1] = '\0';
105 message_subdir[0] = split_spool_directory? message_id[5] : 0;
107 uschar
*filename
= string_sprintf("%s-%s", message_id
, suffix
);
109 uschar *srcpath = string_sprintf("%s/%s/%s/%s-%s", spool_directory,
110 US"input", message_subdir, message_id, suffix);
112 uschar
*srcpath
= spool_fname(US
"input", message_subdir
, message_id
, suffix
);
113 uschar
*dstpath
= string_sprintf("%s/%s-%s", dirname
, message_id
, suffix
);
119 debug_printf("%s transport, linking %s => %s\n", tname
,
121 if (linkat(sdfd
, CCS filename
, ddfd
, CCS filename
, 0) < 0)
129 debug_printf("%s transport, copying %s => %s\n", tname
,
132 openat(ddfd
, CCS filename
, O_RDWR
|O_CREAT
|O_EXCL
, SPOOL_MODE
)) < 0)
134 addr
->transport_return
= DEFER
;
135 addr
->basic_errno
= errno
;
136 addr
->message
= string_sprintf("%s transport opening file: %s "
137 "failed with error: %s", tname
, dstpath
, strerror(errno
));
141 fchmod(dstfd
, SPOOL_MODE
);
143 if ((dst_file
= fdopen(dstfd
, "wb")) < 0)
145 addr
->transport_return
= DEFER
;
146 addr
->basic_errno
= errno
;
147 addr
->message
= string_sprintf("%s transport opening file fd: %s "
148 "failed with error: %s", tname
, dstpath
, strerror(errno
));
154 srcfd
= deliver_datafile
;
157 if ((srcfd
= openat(sdfd
, CCS filename
, O_RDONLY
)) < 0)
159 addr
->transport_return
= DEFER
;
160 addr
->basic_errno
= errno
;
161 addr
->message
= string_sprintf("%s transport opening file: %s "
162 "failed with error: %s", tname
, srcpath
, strerror(errno
));
167 if ((src_file
= fdopen(srcfd
, "rb")) < 0)
169 addr
->transport_return
= DEFER
;
170 addr
->basic_errno
= errno
;
171 addr
->message
= string_sprintf("%s transport opening file fd: "
172 "%s failed with error: %s", tname
, srcpath
, strerror(errno
));
173 if (!is_data_file
) (void)close(srcfd
);
177 if (!copy_spool_file(dst_file
, src_file
))
179 addr
->transport_return
= DEFER
;
180 addr
->message
= string_sprintf("%s transport creating file: "
181 "%s failed with error: %s", tname
, dstpath
, strerror(errno
));
187 (void)fclose(src_file
);
191 (void)fclose(dst_file
);
194 } /* end data copy */
199 /*************************************************
201 *************************************************/
203 /* This transport always returns FALSE, indicating that the status in
204 the first address is the status for all addresses in a batch. */
206 BOOL
queuefile_transport_entry(transport_instance
*tblock
,
212 struct stat dstatbuf
;
213 struct stat sstatbuf
;
214 FILE *dst_file
= NULL
;
215 FILE *src_file
= NULL
;
216 /* uschar message_subdir[2]; */
217 int ddfd
, sdfd
, dfd_oflags
;
218 queuefile_transport_options_block
*ob
=
219 (queuefile_transport_options_block
*)(tblock
->options_block
);
222 debug_printf("%s transport entered\n", tblock
->name
);
225 # define O_DIRECTORY 0
228 dfd_oflags
= O_RDONLY
|O_DIRECTORY
;
230 dfd_oflags
|= O_NOFOLLOW
;
233 if (ob
->dirname
[0] != '/')
235 addr
->transport_return
= PANIC
;
236 addr
->message
= string_sprintf("%s transport directory: "
237 "%s is not absolute", tblock
->name
, ob
->dirname
);
241 if ((ddfd
= Uopen(ob
->dirname
, dfd_oflags
, 0)) < 0)
243 addr
->transport_return
= PANIC
;
244 addr
->basic_errno
= errno
;
245 addr
->message
= string_sprintf("%s transport accessing directory: %s "
246 "failed with error: %s", tblock
->name
, ob
->dirname
, strerror(errno
));
251 if ((fstat(ddfd
, &dstatbuf
)) < 0)
253 addr
->transport_return
= PANIC
;
254 addr
->basic_errno
= errno
;
255 addr
->message
= string_sprintf("%s transport fstat on directory fd: "
256 "%s failed with error: %s", tblock
->name
, ob
->dirname
, strerror(errno
));
260 sourcedir
= spool_dname(US
"input", message_subdir
);
262 message_subdir[1] = '\0';
263 message_subdir[0] = split_spool_directory? message_id[5] : 0;
264 sourcedir = string_sprintf("%s/%s/%s", spool_directory,
265 US"input", message_subdir);
268 if ((sdfd
= Uopen(sourcedir
, dfd_oflags
, 0)) < 0)
270 addr
->transport_return
= PANIC
;
271 addr
->basic_errno
= errno
;
272 addr
->message
= string_sprintf("%s transport accessing directory: %s "
273 "failed with error: %s", tblock
->name
, sourcedir
, strerror(errno
));
277 if ((fstat(sdfd
, &sstatbuf
)) < 0)
279 addr
->transport_return
= PANIC
;
280 addr
->basic_errno
= errno
;
281 addr
->message
= string_sprintf("%s transport fstat on directory fd: "
282 "%s failed with error: %s", tblock
->name
, sourcedir
, strerror(errno
));
289 debug_printf("*** delivery by %s transport bypassed by -N option\n",
291 addr
->transport_return
= OK
;
295 /* process the header file */
297 debug_printf("%s transport, copying header file\n", tblock
->name
);
299 is_data_file
= FALSE
;
300 link_file
= (dstatbuf
.st_dev
== sstatbuf
.st_dev
);
302 if ((copy_spool_files(tblock
->name
, addr
, sdfd
, ddfd
, US
"H", ob
->dirname
,
303 link_file
, is_data_file
, dst_file
, src_file
)) == FALSE
)
306 /* process the data file */
308 debug_printf("%s transport, copying data file\n", tblock
->name
);
312 if ((copy_spool_files(tblock
->name
, addr
, sdfd
, ddfd
, US
"D", ob
->dirname
,
313 link_file
, is_data_file
, dst_file
, src_file
)) == FALSE
)
316 debug_printf("%s transport, copying data file failed, "
317 "unlinking the header file\n", tblock
->name
);
318 Uunlink(string_sprintf("%s/%s-H", ob
->dirname
, message_id
));
326 debug_printf("%s transport succeeded\n", tblock
->name
);
328 addr
->transport_return
= OK
;
331 if (dst_file
) (void)fclose(dst_file
);
332 if (src_file
&& !is_data_file
) (void)fclose(src_file
);
333 if (ddfd
) (void)close(ddfd
);
334 if (sdfd
) (void)close(sdfd
);
336 /* A return of FALSE means that if there was an error, a common error was
337 put in the first address of a batch. */