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 /* Copyright (c) The Exim Maintainers 1995 - 2018 */
8 /* See the file NOTICE for conditions of use and distribution. */
12 #include "queuefile.h"
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
19 optionlist queuefile_transport_options
[] = {
20 { "directory", opt_stringptr
,
21 (void *)offsetof(queuefile_transport_options_block
, dirname
) },
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. */
28 int queuefile_transport_options_count
=
29 sizeof(queuefile_transport_options
) / sizeof(optionlist
);
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
;}
39 #else /*!MACRO_PREDEF*/
43 /* Default private options block for the appendfile transport. */
45 queuefile_transport_options_block queuefile_transport_option_defaults
= {
49 /*************************************************
50 * Initialization entry point *
51 *************************************************/
53 void queuefile_transport_init(transport_instance
*tblock
)
55 queuefile_transport_options_block
*ob
=
56 (queuefile_transport_options_block
*) tblock
->options_block
;
59 log_write(0, LOG_PANIC_DIE
| LOG_CONFIG
,
60 "directory must be set for the %s transport", tblock
->name
);
63 /* This function will copy from a file to another
66 dst fd to write to (the destination queue file)
67 src fd to read from (the spool queue file)
69 Returns: TRUE if all went well, FALSE otherwise with errno set
73 copy_spool_file(int dst
, int src
)
79 if (lseek(src
, 0, SEEK_SET
) != 0)
83 if ((j
= read(src
, buffer
, sizeof(buffer
))) > 0)
84 for (s
= buffer
; (i
= write(dst
, s
, j
)) != j
; s
+= i
, j
-= i
)
93 /* This function performs the actual copying of the header
94 and data files to the destination directory
97 tb the transport block
98 addr address_item being processed
99 dstpath destination directory name
100 sdfd int Source directory fd
101 ddfd int Destination directory fd
102 link_file BOOL use linkat instead of data copy
103 srcfd fd for data file, or -1 for header file
105 Returns: TRUE if all went well, FALSE otherwise
109 copy_spool_files(transport_instance
* tb
, address_item
* addr
,
110 const uschar
* dstpath
, int sdfd
, int ddfd
, BOOL link_file
, int srcfd
)
112 BOOL is_hdr_file
= srcfd
< 0;
113 const uschar
* suffix
= srcfd
< 0 ? US
"H" : US
"D";
115 const uschar
* filename
= string_sprintf("%s-%s", message_id
, suffix
);
116 const uschar
* srcpath
= spool_fname(US
"input", message_subdir
, message_id
, suffix
);
117 const uschar
* s
, * op
;
119 dstpath
= string_sprintf("%s/%s-%s", dstpath
, message_id
, suffix
);
123 DEBUG(D_transport
) debug_printf("%s transport, linking %s => %s\n",
124 tb
->name
, srcpath
, dstpath
);
126 if (linkat(sdfd
, CCS filename
, ddfd
, CCS filename
, 0) >= 0)
132 else /* use data copy */
134 DEBUG(D_transport
) debug_printf("%s transport, copying %s => %s\n",
135 tb
->name
, srcpath
, dstpath
);
138 (dstfd
= openat(ddfd
, CCS filename
, O_RDWR
|O_CREAT
|O_EXCL
, SPOOL_MODE
))
142 && (s
= srcpath
, (srcfd
= openat(sdfd
, CCS filename
, O_RDONLY
)) < 0)
147 if (s
= dstpath
, fchmod(dstfd
, SPOOL_MODE
) != 0)
148 op
= US
"setting perms on";
150 if (!copy_spool_file(dstfd
, srcfd
))
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
;
163 /*************************************************
165 *************************************************/
167 /* This transport always returns FALSE, indicating that the status in
168 the first address is the status for all addresses in a batch. */
171 queuefile_transport_entry(transport_instance
* tblock
, address_item
* addr
)
173 queuefile_transport_options_block
* ob
=
174 (queuefile_transport_options_block
*) tblock
->options_block
;
176 uschar
* sourcedir
= spool_dname(US
"input", message_subdir
);
177 uschar
* s
, * dstdir
;
178 struct stat dstatbuf
, sstatbuf
;
179 int ddfd
= -1, sdfd
= -1;
182 debug_printf("%s transport entered\n", tblock
->name
);
185 # define O_DIRECTORY 0
188 # define O_NOFOLLOW 0
191 if (!(dstdir
= expand_string(ob
->dirname
)))
193 addr
->message
= string_sprintf("%s transport: failed to expand dirname option",
195 addr
->transport_return
= DEFER
;
200 addr
->transport_return
= PANIC
;
201 addr
->message
= string_sprintf("%s transport directory: "
202 "%s is not absolute", tblock
->name
, dstdir
);
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. */
210 (ddfd
= Uopen(s
, O_RDONLY
| O_DIRECTORY
| O_NOFOLLOW
, 0)) < 0)
212 (sdfd
= Uopen(sourcedir
, O_RDONLY
| O_DIRECTORY
| O_NOFOLLOW
, 0)) < 0)
215 addr
->transport_return
= PANIC
;
216 addr
->basic_errno
= errno
;
217 addr
->message
= string_sprintf("%s transport accessing directory: %s "
218 "failed with error: %s", tblock
->name
, s
, strerror(errno
));
219 if (ddfd
>= 0) (void) close(ddfd
);
223 if ( (s
= dstdir
, fstat(ddfd
, &dstatbuf
) < 0)
224 || (s
= sourcedir
, fstat(sdfd
, &sstatbuf
) < 0)
227 addr
->transport_return
= PANIC
;
228 addr
->basic_errno
= errno
;
229 addr
->message
= string_sprintf("%s transport fstat on directory fd: "
230 "%s failed with error: %s", tblock
->name
, s
, strerror(errno
));
233 can_link
= (dstatbuf
.st_dev
== sstatbuf
.st_dev
);
238 debug_printf("*** delivery by %s transport bypassed by -N option\n",
240 addr
->transport_return
= OK
;
244 /* Link or copy the header and data spool files */
247 debug_printf("%s transport, copying header file\n", tblock
->name
);
249 if (!copy_spool_files(tblock
, addr
, dstdir
, sdfd
, ddfd
, can_link
, -1))
253 debug_printf("%s transport, copying data file\n", tblock
->name
);
255 if (!copy_spool_files(tblock
, addr
, dstdir
, sdfd
, ddfd
, can_link
,
259 debug_printf("%s transport, copying data file failed, "
260 "unlinking the header file\n", tblock
->name
);
261 Uunlink(string_sprintf("%s/%s-H", dstdir
, message_id
));
266 debug_printf("%s transport succeeded\n", tblock
->name
);
268 addr
->transport_return
= OK
;
271 if (ddfd
>= 0) (void) close(ddfd
);
272 if (sdfd
>= 0) (void) close(sdfd
);
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. */
279 #endif /*!MACRO_PREDEF*/