s3: smbd: Correctly set smb2req->smb1req->posix_pathnames from the calling fsp on...
[Samba.git] / source3 / smbd / smb2_close.c
blob236d5b79f91e98c732ce924bbb91e89d457f21c1
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../lib/util/tevent_ntstatus.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_SMB2
30 static struct tevent_req *smbd_smb2_close_send(TALLOC_CTX *mem_ctx,
31 struct tevent_context *ev,
32 struct smbd_smb2_request *smb2req,
33 struct files_struct *in_fsp,
34 uint16_t in_flags);
35 static NTSTATUS smbd_smb2_close_recv(struct tevent_req *req,
36 uint16_t *out_flags,
37 struct timespec *out_creation_ts,
38 struct timespec *out_last_access_ts,
39 struct timespec *out_last_write_ts,
40 struct timespec *out_change_ts,
41 uint64_t *out_allocation_size,
42 uint64_t *out_end_of_file,
43 uint32_t *out_file_attributes);
45 static void smbd_smb2_request_close_done(struct tevent_req *subreq);
47 NTSTATUS smbd_smb2_request_process_close(struct smbd_smb2_request *req)
49 const uint8_t *inbody;
50 uint16_t in_flags;
51 uint64_t in_file_id_persistent;
52 uint64_t in_file_id_volatile;
53 struct files_struct *in_fsp;
54 NTSTATUS status;
55 struct tevent_req *subreq;
57 status = smbd_smb2_request_verify_sizes(req, 0x18);
58 if (!NT_STATUS_IS_OK(status)) {
59 return smbd_smb2_request_error(req, status);
61 inbody = SMBD_SMB2_IN_BODY_PTR(req);
63 in_flags = SVAL(inbody, 0x02);
64 in_file_id_persistent = BVAL(inbody, 0x08);
65 in_file_id_volatile = BVAL(inbody, 0x10);
67 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
68 if (in_fsp == NULL) {
69 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
72 subreq = smbd_smb2_close_send(req, req->sconn->ev_ctx,
73 req, in_fsp, in_flags);
74 if (subreq == NULL) {
75 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
77 tevent_req_set_callback(subreq, smbd_smb2_request_close_done, req);
79 return smbd_smb2_request_pending_queue(req, subreq, 500);
82 static void smbd_smb2_request_close_done(struct tevent_req *subreq)
84 struct smbd_smb2_request *req =
85 tevent_req_callback_data(subreq,
86 struct smbd_smb2_request);
87 DATA_BLOB outbody;
88 uint16_t out_flags = 0;
89 connection_struct *conn = req->tcon->compat;
90 struct timespec out_creation_ts = { 0, };
91 struct timespec out_last_access_ts = { 0, };
92 struct timespec out_last_write_ts = { 0, };
93 struct timespec out_change_ts = { 0, };
94 uint64_t out_allocation_size = 0;
95 uint64_t out_end_of_file = 0;
96 uint32_t out_file_attributes = 0;
97 NTSTATUS status;
98 NTSTATUS error;
100 status = smbd_smb2_close_recv(subreq,
101 &out_flags,
102 &out_creation_ts,
103 &out_last_access_ts,
104 &out_last_write_ts,
105 &out_change_ts,
106 &out_allocation_size,
107 &out_end_of_file,
108 &out_file_attributes);
109 TALLOC_FREE(subreq);
110 if (!NT_STATUS_IS_OK(status)) {
111 error = smbd_smb2_request_error(req, status);
112 if (!NT_STATUS_IS_OK(error)) {
113 smbd_server_connection_terminate(req->xconn,
114 nt_errstr(error));
115 return;
117 return;
120 outbody = smbd_smb2_generate_outbody(req, 0x3C);
121 if (outbody.data == NULL) {
122 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
123 if (!NT_STATUS_IS_OK(error)) {
124 smbd_server_connection_terminate(req->xconn,
125 nt_errstr(error));
126 return;
128 return;
131 SSVAL(outbody.data, 0x00, 0x3C); /* struct size */
132 SSVAL(outbody.data, 0x02, out_flags);
133 SIVAL(outbody.data, 0x04, 0); /* reserved */
134 put_long_date_full_timespec(conn->ts_res,
135 (char *)outbody.data + 0x08, &out_creation_ts);
136 put_long_date_full_timespec(conn->ts_res,
137 (char *)outbody.data + 0x10, &out_last_access_ts);
138 put_long_date_full_timespec(conn->ts_res,
139 (char *)outbody.data + 0x18, &out_last_write_ts);
140 put_long_date_full_timespec(conn->ts_res,
141 (char *)outbody.data + 0x20, &out_change_ts);
142 SBVAL(outbody.data, 0x28, out_allocation_size);
143 SBVAL(outbody.data, 0x30, out_end_of_file);
144 SIVAL(outbody.data, 0x38, out_file_attributes);
146 error = smbd_smb2_request_done(req, outbody, NULL);
147 if (!NT_STATUS_IS_OK(error)) {
148 smbd_server_connection_terminate(req->xconn,
149 nt_errstr(error));
150 return;
154 static void setup_close_full_information(connection_struct *conn,
155 struct smb_filename *smb_fname,
156 struct timespec *out_creation_ts,
157 struct timespec *out_last_access_ts,
158 struct timespec *out_last_write_ts,
159 struct timespec *out_change_ts,
160 uint16_t *out_flags,
161 uint64_t *out_allocation_size,
162 uint64_t *out_end_of_file)
164 *out_flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION;
165 *out_last_write_ts = smb_fname->st.st_ex_mtime;
166 *out_last_access_ts = smb_fname->st.st_ex_atime;
167 *out_creation_ts = get_create_timespec(conn, NULL, smb_fname);
168 *out_change_ts = get_change_timespec(conn, NULL, smb_fname);
170 if (lp_dos_filetime_resolution(SNUM(conn))) {
171 dos_filetime_timespec(out_creation_ts);
172 dos_filetime_timespec(out_last_write_ts);
173 dos_filetime_timespec(out_last_access_ts);
174 dos_filetime_timespec(out_change_ts);
176 if (!S_ISDIR(smb_fname->st.st_ex_mode)) {
177 *out_end_of_file = get_file_size_stat(&smb_fname->st);
180 *out_allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, NULL, &smb_fname->st);
183 static NTSTATUS smbd_smb2_close(struct smbd_smb2_request *req,
184 struct files_struct **_fsp,
185 uint16_t in_flags,
186 uint16_t *out_flags,
187 struct timespec *out_creation_ts,
188 struct timespec *out_last_access_ts,
189 struct timespec *out_last_write_ts,
190 struct timespec *out_change_ts,
191 uint64_t *out_allocation_size,
192 uint64_t *out_end_of_file,
193 uint32_t *out_file_attributes)
195 NTSTATUS status;
196 struct smb_request *smbreq;
197 connection_struct *conn = req->tcon->compat;
198 struct files_struct *fsp = *_fsp;
199 struct smb_filename *smb_fname = NULL;
201 *out_creation_ts = (struct timespec){0, SAMBA_UTIME_OMIT};
202 *out_last_access_ts = (struct timespec){0, SAMBA_UTIME_OMIT};
203 *out_last_write_ts = (struct timespec){0, SAMBA_UTIME_OMIT};
204 *out_change_ts = (struct timespec){0, SAMBA_UTIME_OMIT};
206 *out_flags = 0;
207 *out_allocation_size = 0;
208 *out_end_of_file = 0;
209 *out_file_attributes = 0;
211 DEBUG(10,("smbd_smb2_close: %s - %s\n",
212 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
214 smbreq = smbd_smb2_fake_smb_request(req, fsp);
215 if (smbreq == NULL) {
216 return NT_STATUS_NO_MEMORY;
219 if (in_flags & SMB2_CLOSE_FLAGS_FULL_INFORMATION) {
220 *out_file_attributes = fdos_mode(fsp);
221 fsp->fsp_flags.fstat_before_close = true;
224 status = close_file_smb(smbreq, fsp, NORMAL_CLOSE);
225 if (!NT_STATUS_IS_OK(status)) {
226 DEBUG(5,("smbd_smb2_close: close_file[%s]: %s\n",
227 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
228 return status;
231 if (in_flags & SMB2_CLOSE_FLAGS_FULL_INFORMATION) {
232 setup_close_full_information(conn,
233 fsp->fsp_name,
234 out_creation_ts,
235 out_last_access_ts,
236 out_last_write_ts,
237 out_change_ts,
238 out_flags,
239 out_allocation_size,
240 out_end_of_file);
243 file_free(smbreq, fsp);
244 *_fsp = fsp = NULL;
245 return NT_STATUS_OK;
248 struct smbd_smb2_close_state {
249 struct smbd_smb2_request *smb2req;
250 struct files_struct *in_fsp;
251 uint16_t in_flags;
252 uint16_t out_flags;
253 struct timespec out_creation_ts;
254 struct timespec out_last_access_ts;
255 struct timespec out_last_write_ts;
256 struct timespec out_change_ts;
257 uint64_t out_allocation_size;
258 uint64_t out_end_of_file;
259 uint32_t out_file_attributes;
260 struct tevent_queue *wait_queue;
263 static void smbd_smb2_close_wait_done(struct tevent_req *subreq);
265 static struct tevent_req *smbd_smb2_close_send(TALLOC_CTX *mem_ctx,
266 struct tevent_context *ev,
267 struct smbd_smb2_request *smb2req,
268 struct files_struct *in_fsp,
269 uint16_t in_flags)
271 struct tevent_req *req;
272 struct smbd_smb2_close_state *state;
273 const char *fsp_name_str = NULL;
274 const char *fsp_fnum_str = NULL;
275 unsigned i;
276 NTSTATUS status;
278 if (CHECK_DEBUGLVL(DBGLVL_INFO)) {
279 fsp_name_str = fsp_str_dbg(in_fsp);
280 fsp_fnum_str = fsp_fnum_dbg(in_fsp);
283 DBG_DEBUG("%s - %s\n", fsp_name_str, fsp_fnum_str);
285 req = tevent_req_create(mem_ctx, &state,
286 struct smbd_smb2_close_state);
287 if (req == NULL) {
288 return NULL;
290 state->smb2req = smb2req;
291 state->in_fsp = in_fsp;
292 state->in_flags = in_flags;
294 in_fsp->fsp_flags.closing = true;
296 i = 0;
297 while (i < in_fsp->num_aio_requests) {
298 bool ok = tevent_req_cancel(in_fsp->aio_requests[i]);
299 if (ok) {
300 continue;
302 i += 1;
305 if (in_fsp->num_aio_requests != 0) {
306 struct tevent_req *subreq;
308 state->wait_queue = tevent_queue_create(state,
309 "smbd_smb2_close_send_wait_queue");
310 if (tevent_req_nomem(state->wait_queue, req)) {
311 return tevent_req_post(req, ev);
314 * Now wait until all aio requests on this fsp are
315 * finished.
317 * We don't set a callback, as we just want to block the
318 * wait queue and the talloc_free() of fsp->aio_request
319 * will remove the item from the wait queue.
321 subreq = tevent_queue_wait_send(in_fsp->aio_requests,
322 smb2req->sconn->ev_ctx,
323 state->wait_queue);
324 if (tevent_req_nomem(subreq, req)) {
325 return tevent_req_post(req, ev);
329 * Now we add our own waiter to the end of the queue,
330 * this way we get notified when all pending requests are
331 * finished.
333 subreq = tevent_queue_wait_send(state,
334 smb2req->sconn->ev_ctx,
335 state->wait_queue);
336 if (tevent_req_nomem(subreq, req)) {
337 return tevent_req_post(req, ev);
340 tevent_req_set_callback(subreq, smbd_smb2_close_wait_done, req);
341 return req;
344 status = smbd_smb2_close(smb2req,
345 &state->in_fsp,
346 state->in_flags,
347 &state->out_flags,
348 &state->out_creation_ts,
349 &state->out_last_access_ts,
350 &state->out_last_write_ts,
351 &state->out_change_ts,
352 &state->out_allocation_size,
353 &state->out_end_of_file,
354 &state->out_file_attributes);
355 if (tevent_req_nterror(req, status)) {
356 DBG_INFO("%s - %s: close file failed: %s\n",
357 fsp_name_str, fsp_fnum_str,
358 nt_errstr(status));
359 return tevent_req_post(req, ev);
362 tevent_req_done(req);
363 return tevent_req_post(req, ev);
366 static void smbd_smb2_close_wait_done(struct tevent_req *subreq)
368 struct tevent_req *req = tevent_req_callback_data(
369 subreq, struct tevent_req);
370 struct smbd_smb2_close_state *state = tevent_req_data(
371 req, struct smbd_smb2_close_state);
372 NTSTATUS status;
374 tevent_queue_wait_recv(subreq);
375 TALLOC_FREE(subreq);
377 status = smbd_smb2_close(state->smb2req,
378 &state->in_fsp,
379 state->in_flags,
380 &state->out_flags,
381 &state->out_creation_ts,
382 &state->out_last_access_ts,
383 &state->out_last_write_ts,
384 &state->out_change_ts,
385 &state->out_allocation_size,
386 &state->out_end_of_file,
387 &state->out_file_attributes);
388 if (tevent_req_nterror(req, status)) {
389 return;
391 tevent_req_done(req);
394 static NTSTATUS smbd_smb2_close_recv(struct tevent_req *req,
395 uint16_t *out_flags,
396 struct timespec *out_creation_ts,
397 struct timespec *out_last_access_ts,
398 struct timespec *out_last_write_ts,
399 struct timespec *out_change_ts,
400 uint64_t *out_allocation_size,
401 uint64_t *out_end_of_file,
402 uint32_t *out_file_attributes)
404 struct smbd_smb2_close_state *state =
405 tevent_req_data(req,
406 struct smbd_smb2_close_state);
407 NTSTATUS status;
409 if (tevent_req_is_nterror(req, &status)) {
410 tevent_req_received(req);
411 return status;
414 *out_flags = state->out_flags;
415 *out_creation_ts = state->out_creation_ts;
416 *out_last_access_ts = state->out_last_access_ts;
417 *out_last_write_ts = state->out_last_write_ts;
418 *out_change_ts = state->out_change_ts;
419 *out_allocation_size = state->out_allocation_size;
420 *out_end_of_file = state->out_end_of_file;
421 *out_file_attributes = state->out_file_attributes;
423 tevent_req_received(req);
424 return NT_STATUS_OK;