Prebuild the data structure for builtin macros
[exim.git] / src / src / transports / queuefile.c
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
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
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
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 =
55 (queuefile_transport_options_block *) tblock->options_block;
56
57 if (!ob->dirname)
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:
65 dst fd to write to (the destination queue file)
66 src fd to read from (the spool queue file)
67
68 Returns: TRUE if all went well, FALSE otherwise with errno set
69 */
70
71 static BOOL
72 copy_spool_file(int dst, int src)
73 {
74 int i, j;
75 uschar buffer[16384];
76 uschar * s;
77
78 if (lseek(src, 0, SEEK_SET) != 0)
79 return FALSE;
80
81 do
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;
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:
96 tb the transport block
97 addr address_item being processed
98 sdfd int Source directory fd
99 ddfd int Destination directory fd
100 link_file BOOL use linkat instead of data copy
101 srcfd fd for data file, or -1 for header file
102
103 Returns: TRUE if all went well, FALSE otherwise
104 */
105
106 static BOOL
107 copy_spool_files(transport_instance * tb, address_item * addr,
108 int sdfd, int ddfd, BOOL link_file, int srcfd)
109 {
110 BOOL is_hdr_file = srcfd < 0;
111 const uschar * suffix = srcfd < 0 ? US"H" : US"D";
112 int dstfd;
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",
116 ((queuefile_transport_options_block *) tb->options_block)->dirname,
117 message_id, suffix);
118 const uschar * s;
119 const uschar * op;
120
121 if (link_file)
122 {
123 DEBUG(D_transport) debug_printf("%s transport, linking %s => %s\n",
124 tb->name, srcpath, dstpath);
125
126 if (linkat(sdfd, CCS filename, ddfd, CCS filename, 0) >= 0)
127 return TRUE;
128
129 op = US"linking";
130 s = dstpath;
131 }
132 else /* use data copy */
133 {
134 DEBUG(D_transport) debug_printf("%s transport, copying %s => %s\n",
135 tb->name, srcpath, dstpath);
136
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";
145
146 else
147 if (s = dstpath, fchmod(dstfd, SPOOL_MODE) != 0)
148 op = US"setting perms on";
149 else
150 if (!copy_spool_file(dstfd, srcfd))
151 op = US"creating";
152 else
153 return TRUE;
154 }
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;
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
170 BOOL
171 queuefile_transport_entry(transport_instance * tblock, address_item * addr)
172 {
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;
180
181 DEBUG(D_transport)
182 debug_printf("%s transport entered\n", tblock->name);
183
184 #ifndef O_DIRECTORY
185 # define O_DIRECTORY 0
186 #endif
187 #ifndef O_NOFOLLOW
188 # define O_NOFOLLOW 0
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
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. */
201
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 )
207 {
208 addr->transport_return = PANIC;
209 addr->basic_errno = errno;
210 addr->message = string_sprintf("%s transport accessing directory: %s "
211 "failed with error: %s", tblock->name, s, strerror(errno));
212 if (ddfd >= 0) (void) close(ddfd);
213 return FALSE;
214 }
215
216 if ( (s = ob->dirname, fstat(ddfd, &dstatbuf) < 0)
217 || (s = sourcedir, fstat(sdfd, &sstatbuf) < 0)
218 )
219 {
220 addr->transport_return = PANIC;
221 addr->basic_errno = errno;
222 addr->message = string_sprintf("%s transport fstat on directory fd: "
223 "%s failed with error: %s", tblock->name, s, strerror(errno));
224 goto RETURN;
225 }
226 can_link = (dstatbuf.st_dev == sstatbuf.st_dev);
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
237 /* Link or copy the header and data spool files */
238
239 DEBUG(D_transport)
240 debug_printf("%s transport, copying header file\n", tblock->name);
241
242 if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, -1))
243 goto RETURN;
244
245 DEBUG(D_transport)
246 debug_printf("%s transport, copying data file\n", tblock->name);
247
248 if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, deliver_datafile))
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
257 DEBUG(D_transport)
258 debug_printf("%s transport succeeded\n", tblock->name);
259
260 addr->transport_return = OK;
261
262 RETURN:
263 if (ddfd >= 0) (void) close(ddfd);
264 if (sdfd >= 0) (void) close(sdfd);
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 }
270
271 #endif /*!MACRO_PREDEF*/