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