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