SPDX: license tags (mostly by guesswork)
[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 /* Copyright (c) The Exim Maintainers 1995 - 2021 */
8 /* See the file NOTICE for conditions of use and distribution. */
9 /* SPDX-License-Identifier: GPL-2.0-only */
10
11
12
13 #include "../exim.h"
14
15 #ifdef EXPERIMENTAL_QUEUEFILE   /* whole file */
16 #include "queuefile.h"
17
18 #ifndef EXIM_HAVE_OPENAT
19 # error queuefile transport reqires openat() support
20 #endif
21
22 /* Options specific to the appendfile transport. They must be in alphabetic
23 order (note that "_" comes before the lower case letters). Some of them are
24 stored in the publicly visible instance block - these are flagged with the
25 opt_public flag. */
26
27 optionlist queuefile_transport_options[] = {
28   { "directory", opt_stringptr,
29     OPT_OFF(queuefile_transport_options_block, dirname) },
30 };
31
32
33 /* Size of the options list. An extern variable has to be used so that its
34 address can appear in the tables drtables.c. */
35
36 int queuefile_transport_options_count =
37   sizeof(queuefile_transport_options) / sizeof(optionlist);
38
39
40 #ifdef MACRO_PREDEF
41
42 /* Dummy values */
43 queuefile_transport_options_block queuefile_transport_option_defaults = {0};
44 void queuefile_transport_init(transport_instance *tblock) {}
45 BOOL queuefile_transport_entry(transport_instance *tblock, address_item *addr) {return FALSE;}
46
47 #else   /*!MACRO_PREDEF*/
48
49
50
51 /* Default private options block for the appendfile transport. */
52
53 queuefile_transport_options_block queuefile_transport_option_defaults = {
54   NULL,           /* dirname */
55 };
56
57 /*************************************************
58 *          Initialization entry point            *
59 *************************************************/
60
61 void queuefile_transport_init(transport_instance *tblock)
62 {
63 queuefile_transport_options_block *ob =
64   (queuefile_transport_options_block *) tblock->options_block;
65
66 if (!ob->dirname)
67   log_write(0, LOG_PANIC_DIE | LOG_CONFIG,
68     "directory must be set for the %s transport", tblock->name);
69 }
70
71 /* This function will copy from a file to another
72
73 Arguments:
74   dst        fd to write to (the destination queue file)
75   src        fd to read from (the spool queue file)
76
77 Returns:       TRUE if all went well, FALSE otherwise with errno set
78 */
79
80 static BOOL
81 copy_spool_file(int dst, int src)
82 {
83 int i, j;
84 uschar buffer[16384];
85
86 if (lseek(src, 0, SEEK_SET) != 0)
87   return FALSE;
88
89 do
90   if ((j = read(src, buffer, sizeof(buffer))) > 0)
91     for (uschar * s = buffer; (i = write(dst, s, j)) != j; s += i, j -= i)
92       if (i < 0)
93         return FALSE;
94   else if (j < 0)
95     return FALSE;
96 while (j > 0);
97 return TRUE;
98 }
99
100 /* This function performs the actual copying of the header
101 and data files to the destination directory
102
103 Arguments:
104   tb            the transport block
105   addr          address_item being processed
106   dstpath       destination directory name
107   sdfd          int Source directory fd
108   ddfd          int Destination directory fd
109   link_file     BOOL use linkat instead of data copy
110   srcfd         fd for data file, or -1 for header file
111
112 Returns:       TRUE if all went well, FALSE otherwise
113 */
114
115 static BOOL
116 copy_spool_files(transport_instance * tb, address_item * addr,
117   const uschar * dstpath, int sdfd, int ddfd, BOOL link_file, int srcfd)
118 {
119 BOOL is_hdr_file = srcfd < 0;
120 const uschar * suffix = srcfd < 0 ? US"H" : US"D";
121 int dstfd;
122 const uschar * filename = string_sprintf("%s-%s", message_id, suffix);
123 const uschar * srcpath = spool_fname(US"input", message_subdir, message_id, suffix);
124 const uschar * s, * op;
125
126 dstpath = string_sprintf("%s/%s-%s", dstpath, message_id, suffix);
127
128 if (link_file)
129   {
130   DEBUG(D_transport) debug_printf("%s transport, linking %s => %s\n",
131     tb->name, srcpath, dstpath);
132
133   if (linkat(sdfd, CCS filename, ddfd, CCS filename, 0) >= 0)
134     return TRUE;
135
136   op = US"linking";
137   s = dstpath;
138   }
139 else                                    /* use data copy */
140   {
141   DEBUG(D_transport) debug_printf("%s transport, copying %s => %s\n",
142     tb->name, srcpath, dstpath);
143
144   if (  (s = dstpath,
145          (dstfd = exim_openat4(ddfd, CCS filename, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE))
146          < 0
147         )
148      ||    is_hdr_file
149         && (s = srcpath, (srcfd = exim_openat(sdfd, CCS filename, O_RDONLY)) < 0)
150      )
151     op = US"opening";
152
153   else
154     if (s = dstpath, fchmod(dstfd, SPOOL_MODE) != 0)
155       op = US"setting perms on";
156     else
157       if (!copy_spool_file(dstfd, srcfd))
158         op = US"creating";
159       else
160         return TRUE;
161   }
162
163 addr->basic_errno = errno;
164 addr->message = string_sprintf("%s transport %s file: %s failed with error: %s",
165   tb->name, op, s, strerror(errno));
166 addr->transport_return = DEFER;
167 return FALSE;
168 }
169
170 /*************************************************
171 *              Main entry point                  *
172 *************************************************/
173
174 /* This transport always returns FALSE, indicating that the status in
175 the first address is the status for all addresses in a batch. */
176
177 BOOL
178 queuefile_transport_entry(transport_instance * tblock, address_item * addr)
179 {
180 queuefile_transport_options_block * ob =
181   (queuefile_transport_options_block *) tblock->options_block;
182 BOOL can_link;
183 uschar * sourcedir = spool_dname(US"input", message_subdir);
184 uschar * s, * dstdir;
185 struct stat dstatbuf, sstatbuf;
186 int ddfd = -1, sdfd = -1;
187
188 DEBUG(D_transport)
189   debug_printf("%s transport entered\n", tblock->name);
190
191 #ifndef O_DIRECTORY
192 # define O_DIRECTORY 0
193 #endif
194 #ifndef O_NOFOLLOW
195 # define O_NOFOLLOW 0
196 #endif
197
198 if (!(dstdir = expand_string(ob->dirname)))
199   {
200   addr->message = string_sprintf("%s transport: failed to expand dirname option",
201     tblock->name);
202   addr->transport_return = DEFER;
203   return FALSE;
204   }
205 if (*dstdir != '/')
206   {
207   addr->transport_return = PANIC;
208   addr->message = string_sprintf("%s transport directory: "
209     "%s is not absolute", tblock->name, dstdir);
210   return FALSE;
211   }
212
213 /* Open the source and destination directories and check if they are
214 on the same filesystem, so we can hard-link files rather than copying. */
215
216 if (  (s = dstdir,
217        (ddfd = Uopen(s, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
218    || (s = sourcedir,
219        (sdfd = Uopen(sourcedir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0)) < 0)
220    )
221   {
222   addr->transport_return = PANIC;
223   addr->basic_errno = errno;
224   addr->message = string_sprintf("%s transport accessing directory: %s "
225     "failed with error: %s", tblock->name, s, strerror(errno));
226   if (ddfd >= 0) (void) close(ddfd);
227   return FALSE;
228   }
229
230 if (  (s = dstdir,    fstat(ddfd, &dstatbuf) < 0)
231    || (s = sourcedir, fstat(sdfd, &sstatbuf) < 0)
232    )
233   {
234   addr->transport_return = PANIC;
235   addr->basic_errno = errno;
236   addr->message = string_sprintf("%s transport fstat on directory fd: "
237     "%s failed with error: %s", tblock->name, s, strerror(errno));
238   goto RETURN;
239   }
240 can_link = (dstatbuf.st_dev == sstatbuf.st_dev);
241
242 if (f.dont_deliver)
243   {
244   DEBUG(D_transport)
245     debug_printf("*** delivery by %s transport bypassed by -N option\n",
246       tblock->name);
247   addr->transport_return = OK;
248   goto RETURN;
249   }
250
251 /* Link or copy the header and data spool files */
252
253 DEBUG(D_transport)
254   debug_printf("%s transport, copying header file\n", tblock->name);
255
256 if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link, -1))
257   goto RETURN;
258
259 DEBUG(D_transport)
260   debug_printf("%s transport, copying data file\n", tblock->name);
261
262 if (!copy_spool_files(tblock, addr, dstdir, sdfd, ddfd, can_link,
263         deliver_datafile))
264   {
265   DEBUG(D_transport)
266     debug_printf("%s transport, copying data file failed, "
267       "unlinking the header file\n", tblock->name);
268   Uunlink(string_sprintf("%s/%s-H", dstdir, message_id));
269   goto RETURN;
270   }
271
272 DEBUG(D_transport)
273   debug_printf("%s transport succeeded\n", tblock->name);
274
275 addr->transport_return = OK;
276
277 RETURN:
278 if (ddfd >= 0) (void) close(ddfd);
279 if (sdfd >= 0) (void) close(sdfd);
280
281 /* A return of FALSE means that if there was an error, a common error was
282 put in the first address of a batch. */
283 return FALSE;
284 }
285
286 #endif /*!MACRO_PREDEF*/
287 #endif /*EXPERIMENTAL_QUEUEFILE*/