Tidying: coverity issues
[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 /* Size of the options list. An extern variable has to be used so that its
24 address can appear in the tables drtables.c. */
25
26 int queuefile_transport_options_count =
27 sizeof(queuefile_transport_options) / sizeof(optionlist);
28
29 /* Default private options block for the appendfile transport. */
30
31 queuefile_transport_options_block queuefile_transport_option_defaults = {
32 NULL, /* dirname */
33 };
34
35 /*************************************************
36 * Initialization entry point *
37 *************************************************/
38
39 void queuefile_transport_init(transport_instance *tblock)
40 {
41 queuefile_transport_options_block *ob =
42 (queuefile_transport_options_block *) tblock->options_block;
43
44 if (!ob->dirname)
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
51 Arguments:
52 dst fd to write to (the destination queue file)
53 src fd to read from (the spool queue file)
54
55 Returns: TRUE if all went well, FALSE otherwise with errno set
56 */
57
58 static BOOL
59 copy_spool_file(int dst, int src)
60 {
61 int i, j;
62 uschar buffer[16384];
63 uschar * s;
64
65 if (lseek(src, 0, SEEK_SET) != 0)
66 return FALSE;
67
68 do
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;
75 while (j > 0);
76 return TRUE;
77 }
78
79 /* This function performs the actual copying of the header
80 and data files to the destination directory
81
82 Arguments:
83 tb the transport block
84 addr address_item being processed
85 sdfd int Source directory fd
86 ddfd int Destination directory fd
87 link_file BOOL use linkat instead of data copy
88 srcfd fd for data file, or -1 for header file
89
90 Returns: TRUE if all went well, FALSE otherwise
91 */
92
93 static BOOL
94 copy_spool_files(transport_instance * tb, address_item * addr,
95 int sdfd, int ddfd, BOOL link_file, int srcfd)
96 {
97 BOOL is_hdr_file = srcfd < 0;
98 const uschar * suffix = srcfd < 0 ? US"H" : US"D";
99 int dstfd;
100 const uschar * filename = string_sprintf("%s-%s", message_id, suffix);
101 const uschar * srcpath = spool_fname(US"input", message_subdir, message_id, suffix);
102 const uschar * dstpath = string_sprintf("%s/%s-%s",
103 ((queuefile_transport_options_block *) tb->options_block)->dirname,
104 message_id, suffix);
105 const uschar * s;
106 const uschar * op;
107
108 if (link_file)
109 {
110 DEBUG(D_transport) debug_printf("%s transport, linking %s => %s\n",
111 tb->name, srcpath, dstpath);
112
113 if (linkat(sdfd, CCS filename, ddfd, CCS filename, 0) >= 0)
114 return TRUE;
115
116 op = US"linking";
117 s = dstpath;
118 }
119 else /* use data copy */
120 {
121 DEBUG(D_transport) debug_printf("%s transport, copying %s => %s\n",
122 tb->name, srcpath, dstpath);
123
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";
132
133 else
134 if (s = dstpath, fchmod(dstfd, SPOOL_MODE) != 0)
135 op = US"setting perms on";
136 else
137 if (!copy_spool_file(dstfd, srcfd))
138 op = US"creating";
139 else
140 return TRUE;
141 }
142
143 addr->basic_errno = errno;
144 addr->message = string_sprintf("%s transport %s file: %s failed with error: %s",
145 tb->name, op, s, strerror(errno));
146 addr->transport_return = DEFER;
147 return FALSE;
148 }
149
150 /*************************************************
151 * Main entry point *
152 *************************************************/
153
154 /* This transport always returns FALSE, indicating that the status in
155 the first address is the status for all addresses in a batch. */
156
157 BOOL
158 queuefile_transport_entry(transport_instance * tblock, address_item * addr)
159 {
160 queuefile_transport_options_block * ob =
161 (queuefile_transport_options_block *) tblock->options_block;
162 BOOL can_link;
163 uschar * sourcedir = spool_dname(US"input", message_subdir);
164 uschar * s;
165 struct stat dstatbuf, sstatbuf;
166 int ddfd = -1, sdfd = -1;
167
168 DEBUG(D_transport)
169 debug_printf("%s transport entered\n", tblock->name);
170
171 #ifndef O_DIRECTORY
172 # define O_DIRECTORY 0
173 #endif
174 #ifndef O_NOFOLLOW
175 # define O_NOFOLLOW 0
176 #endif
177
178 if (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
186 /* Open the source and destination directories and check if they are
187 on the same filesystem, so we can hard-link files rather than copying. */
188
189 if ( (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 )
194 {
195 addr->transport_return = PANIC;
196 addr->basic_errno = errno;
197 addr->message = string_sprintf("%s transport accessing directory: %s "
198 "failed with error: %s", tblock->name, s, strerror(errno));
199 if (ddfd >= 0) (void) close(ddfd);
200 return FALSE;
201 }
202
203 if ( (s = ob->dirname, fstat(ddfd, &dstatbuf) < 0)
204 || (s = sourcedir, fstat(sdfd, &sstatbuf) < 0)
205 )
206 {
207 addr->transport_return = PANIC;
208 addr->basic_errno = errno;
209 addr->message = string_sprintf("%s transport fstat on directory fd: "
210 "%s failed with error: %s", tblock->name, s, strerror(errno));
211 goto RETURN;
212 }
213 can_link = (dstatbuf.st_dev == sstatbuf.st_dev);
214
215 if (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
224 /* Link or copy the header and data spool files */
225
226 DEBUG(D_transport)
227 debug_printf("%s transport, copying header file\n", tblock->name);
228
229 if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, -1))
230 goto RETURN;
231
232 DEBUG(D_transport)
233 debug_printf("%s transport, copying data file\n", tblock->name);
234
235 if (!copy_spool_files(tblock, addr, sdfd, ddfd, can_link, deliver_datafile))
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
244 DEBUG(D_transport)
245 debug_printf("%s transport succeeded\n", tblock->name);
246
247 addr->transport_return = OK;
248
249 RETURN:
250 if (ddfd >= 0) (void) close(ddfd);
251 if (sdfd >= 0) (void) close(sdfd);
252
253 /* A return of FALSE means that if there was an error, a common error was
254 put in the first address of a batch. */
255 return FALSE;
256 }