Split macro name storage out from macro definition struct
[exim.git] / src / src / transports / queuefile.c
CommitLineData
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
14order (note that "_" comes before the lower case letters). Some of them are
15stored in the publicly visible instance block - these are flagged with the
16opt_public flag. */
17
18optionlist 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
24address can appear in the tables drtables.c. */
25
26int queuefile_transport_options_count =
27 sizeof(queuefile_transport_options) / sizeof(optionlist);
28
29/* Default private options block for the appendfile transport. */
30
31queuefile_transport_options_block queuefile_transport_option_defaults = {
32 NULL, /* dirname */
33};
34
35/*************************************************
36* Initialization entry point *
37*************************************************/
38
39void queuefile_transport_init(transport_instance *tblock)
40{
41queuefile_transport_options_block *ob =
f09fe41f 42 (queuefile_transport_options_block *) tblock->options_block;
3369a853 43
f09fe41f 44if (!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
51Arguments:
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 55Returns: TRUE if all went well, FALSE otherwise with errno set
3369a853
ACK
56*/
57
f09fe41f 58static BOOL
b4758425 59copy_spool_file(int dst, int src)
3369a853
ACK
60{
61int i, j;
62uschar buffer[16384];
b4758425 63uschar * s;
3369a853 64
b4758425
JH
65if (lseek(src, 0, SEEK_SET) != 0)
66 return FALSE;
3369a853
ACK
67
68do
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
75while (j > 0);
76return TRUE;
77}
78
79/* This function performs the actual copying of the header
80and data files to the destination directory
81
82Arguments:
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
90Returns: TRUE if all went well, FALSE otherwise
91*/
92
f09fe41f
JH
93static BOOL
94copy_spool_files(transport_instance * tb, address_item * addr,
95 int sdfd, int ddfd, BOOL link_file, int srcfd)
3369a853 96{
f09fe41f 97BOOL is_hdr_file = srcfd < 0;
b4758425 98const uschar * suffix = srcfd < 0 ? US"H" : US"D";
f09fe41f 99int dstfd;
b4758425
JH
100const uschar * filename = string_sprintf("%s-%s", message_id, suffix);
101const uschar * srcpath = spool_fname(US"input", message_subdir, message_id, suffix);
102const uschar * dstpath = string_sprintf("%s/%s-%s",
f09fe41f
JH
103 ((queuefile_transport_options_block *) tb->options_block)->dirname,
104 message_id, suffix);
b4758425
JH
105const uschar * s;
106const uschar * op;
3369a853
ACK
107
108if (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 }
d88f0784
JH
119else /* use data copy */
120 {
121 DEBUG(D_transport) debug_printf("%s transport, copying %s => %s\n",
122 tb->name, srcpath, dstpath);
3369a853 123
d88f0784
JH
124 if ( (s = dstpath,
125 (dstfd = openat(ddfd, CCS filename, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE))
126 < 0
127 )
128 || is_hdr_file
129 && (s = srcpath, (srcfd = openat(sdfd, CCS filename, O_RDONLY)) < 0)
130 )
131 op = US"opening";
b4758425 132
b4758425 133 else
d88f0784
JH
134 if (s = dstpath, fchmod(dstfd, SPOOL_MODE) != 0)
135 op = US"setting perms on";
b4758425 136 else
d88f0784
JH
137 if (!copy_spool_file(dstfd, srcfd))
138 op = US"creating";
139 else
140 return TRUE;
141 }
b4758425
JH
142
143addr->basic_errno = errno;
144addr->message = string_sprintf("%s transport %s file: %s failed with error: %s",
145 tb->name, op, s, strerror(errno));
146addr->transport_return = DEFER;
147return FALSE;
3369a853
ACK
148}
149
150/*************************************************
151* Main entry point *
152*************************************************/
153
154/* This transport always returns FALSE, indicating that the status in
155the first address is the status for all addresses in a batch. */
156
f09fe41f
JH
157BOOL
158queuefile_transport_entry(transport_instance * tblock, address_item * addr)
3369a853 159{
f09fe41f
JH
160queuefile_transport_options_block * ob =
161 (queuefile_transport_options_block *) tblock->options_block;
162BOOL can_link;
163uschar * sourcedir = spool_dname(US"input", message_subdir);
164uschar * s;
165struct stat dstatbuf, sstatbuf;
166int ddfd = -1, sdfd = -1;
3369a853
ACK
167
168DEBUG(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
178if (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
187on the same filesystem, so we can hard-link files rather than copying. */
3369a853 188
f09fe41f
JH
189if ( (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
203if ( (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 213can_link = (dstatbuf.st_dev == sstatbuf.st_dev);
3369a853
ACK
214
215if (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
226DEBUG(D_transport)
227 debug_printf("%s transport, copying header file\n", tblock->name);
228
f09fe41f 229if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, -1))
3369a853
ACK
230 goto RETURN;
231
3369a853
ACK
232DEBUG(D_transport)
233 debug_printf("%s transport, copying data file\n", tblock->name);
234
f09fe41f 235if (!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
244DEBUG(D_transport)
245 debug_printf("%s transport succeeded\n", tblock->name);
246
247addr->transport_return = OK;
248
249RETURN:
f09fe41f
JH
250if (ddfd >= 0) (void) close(ddfd);
251if (sdfd >= 0) (void) close(sdfd);
3369a853
ACK
252
253/* A return of FALSE means that if there was an error, a common error was
254put in the first address of a batch. */
255return FALSE;
256}