nsswitch: explicitly mark nss_module_register() _PUBLIC_ on FreeBSD
[Samba.git] / source3 / smbd / smb2_close.c
blobda2a8fa59c8b83d22d85a12d2691c7d98dece6c2
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 bool posix_open,
157 struct timespec *out_creation_ts,
158 struct timespec *out_last_access_ts,
159 struct timespec *out_last_write_ts,
160 struct timespec *out_change_ts,
161 uint16_t *out_flags,
162 uint64_t *out_allocation_size,
163 uint64_t *out_end_of_file,
164 uint32_t *out_file_attributes)
166 NTSTATUS status;
167 int ret;
169 status = openat_pathref_fsp(conn->cwd_fsp, smb_fname);
170 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
171 (smb_fname->flags & SMB_FILENAME_POSIX_PATH) &&
172 S_ISLNK(smb_fname->st.st_ex_mode))
174 status = NT_STATUS_OK;
176 if (!NT_STATUS_IS_OK(status)) {
177 return;
180 if (posix_open) {
181 ret = SMB_VFS_LSTAT(conn, smb_fname);
182 } else {
183 ret = SMB_VFS_STAT(conn, smb_fname);
185 if (ret != 0) {
186 return;
189 *out_flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION;
190 *out_file_attributes = fdos_mode(smb_fname->fsp);
191 *out_last_write_ts = smb_fname->st.st_ex_mtime;
192 *out_last_access_ts = smb_fname->st.st_ex_atime;
193 *out_creation_ts = get_create_timespec(conn, NULL, smb_fname);
194 *out_change_ts = get_change_timespec(conn, NULL, smb_fname);
196 if (lp_dos_filetime_resolution(SNUM(conn))) {
197 dos_filetime_timespec(out_creation_ts);
198 dos_filetime_timespec(out_last_write_ts);
199 dos_filetime_timespec(out_last_access_ts);
200 dos_filetime_timespec(out_change_ts);
202 if (!(*out_file_attributes & FILE_ATTRIBUTE_DIRECTORY)) {
203 *out_end_of_file = get_file_size_stat(&smb_fname->st);
206 *out_allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, NULL, &smb_fname->st);
209 static NTSTATUS smbd_smb2_close(struct smbd_smb2_request *req,
210 struct files_struct *fsp,
211 uint16_t in_flags,
212 uint16_t *out_flags,
213 struct timespec *out_creation_ts,
214 struct timespec *out_last_access_ts,
215 struct timespec *out_last_write_ts,
216 struct timespec *out_change_ts,
217 uint64_t *out_allocation_size,
218 uint64_t *out_end_of_file,
219 uint32_t *out_file_attributes)
221 NTSTATUS status;
222 struct smb_request *smbreq;
223 connection_struct *conn = req->tcon->compat;
224 struct smb_filename *smb_fname = NULL;
225 uint64_t allocation_size = 0;
226 uint64_t file_size = 0;
227 uint32_t dos_attrs = 0;
228 uint16_t flags = 0;
229 bool posix_open = false;
231 *out_creation_ts = (struct timespec){0, SAMBA_UTIME_OMIT};
232 *out_last_access_ts = (struct timespec){0, SAMBA_UTIME_OMIT};
233 *out_last_write_ts = (struct timespec){0, SAMBA_UTIME_OMIT};
234 *out_change_ts = (struct timespec){0, SAMBA_UTIME_OMIT};
236 *out_flags = 0;
237 *out_allocation_size = 0;
238 *out_end_of_file = 0;
239 *out_file_attributes = 0;
241 DEBUG(10,("smbd_smb2_close: %s - %s\n",
242 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
244 smbreq = smbd_smb2_fake_smb_request(req);
245 if (smbreq == NULL) {
246 return NT_STATUS_NO_MEMORY;
249 posix_open = (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN);
250 smb_fname = cp_smb_filename(talloc_tos(), fsp->fsp_name);
251 if (smb_fname == NULL) {
252 return NT_STATUS_NO_MEMORY;
255 if ((in_flags & SMB2_CLOSE_FLAGS_FULL_INFORMATION) &&
256 (fsp->fsp_flags.initial_delete_on_close ||
257 fsp->fsp_flags.delete_on_close))
260 * We might be deleting the file. Ensure we
261 * return valid data from before the file got
262 * removed.
264 setup_close_full_information(conn,
265 smb_fname,
266 posix_open,
267 out_creation_ts,
268 out_last_access_ts,
269 out_last_write_ts,
270 out_change_ts,
271 &flags,
272 &allocation_size,
273 &file_size,
274 &dos_attrs);
277 status = close_file(smbreq, fsp, NORMAL_CLOSE);
278 if (!NT_STATUS_IS_OK(status)) {
279 DEBUG(5,("smbd_smb2_close: close_file[%s]: %s\n",
280 smb_fname_str_dbg(smb_fname), nt_errstr(status)));
281 return status;
284 if (in_flags & SMB2_CLOSE_FLAGS_FULL_INFORMATION) {
285 setup_close_full_information(conn,
286 smb_fname,
287 posix_open,
288 out_creation_ts,
289 out_last_access_ts,
290 out_last_write_ts,
291 out_change_ts,
292 &flags,
293 &allocation_size,
294 &file_size,
295 &dos_attrs);
298 *out_flags = flags;
299 *out_allocation_size = allocation_size;
300 *out_end_of_file = file_size;
301 *out_file_attributes = dos_attrs;
303 return NT_STATUS_OK;
306 struct smbd_smb2_close_state {
307 struct smbd_smb2_request *smb2req;
308 struct files_struct *in_fsp;
309 uint16_t in_flags;
310 uint16_t out_flags;
311 struct timespec out_creation_ts;
312 struct timespec out_last_access_ts;
313 struct timespec out_last_write_ts;
314 struct timespec out_change_ts;
315 uint64_t out_allocation_size;
316 uint64_t out_end_of_file;
317 uint32_t out_file_attributes;
318 struct tevent_queue *wait_queue;
321 static void smbd_smb2_close_wait_done(struct tevent_req *subreq);
323 static struct tevent_req *smbd_smb2_close_send(TALLOC_CTX *mem_ctx,
324 struct tevent_context *ev,
325 struct smbd_smb2_request *smb2req,
326 struct files_struct *in_fsp,
327 uint16_t in_flags)
329 struct tevent_req *req;
330 struct smbd_smb2_close_state *state;
331 unsigned i;
332 NTSTATUS status;
334 req = tevent_req_create(mem_ctx, &state,
335 struct smbd_smb2_close_state);
336 if (req == NULL) {
337 return NULL;
339 state->smb2req = smb2req;
340 state->in_fsp = in_fsp;
341 state->in_flags = in_flags;
343 in_fsp->fsp_flags.closing = true;
345 i = 0;
346 while (i < in_fsp->num_aio_requests) {
347 bool ok = tevent_req_cancel(in_fsp->aio_requests[i]);
348 if (ok) {
349 continue;
351 i += 1;
354 if (in_fsp->num_aio_requests != 0) {
355 struct tevent_req *subreq;
357 state->wait_queue = tevent_queue_create(state,
358 "smbd_smb2_close_send_wait_queue");
359 if (tevent_req_nomem(state->wait_queue, req)) {
360 return tevent_req_post(req, ev);
363 * Now wait until all aio requests on this fsp are
364 * finished.
366 * We don't set a callback, as we just want to block the
367 * wait queue and the talloc_free() of fsp->aio_request
368 * will remove the item from the wait queue.
370 subreq = tevent_queue_wait_send(in_fsp->aio_requests,
371 smb2req->sconn->ev_ctx,
372 state->wait_queue);
373 if (tevent_req_nomem(subreq, req)) {
374 return tevent_req_post(req, ev);
378 * Now we add our own waiter to the end of the queue,
379 * this way we get notified when all pending requests are
380 * finished.
382 subreq = tevent_queue_wait_send(state,
383 smb2req->sconn->ev_ctx,
384 state->wait_queue);
385 if (tevent_req_nomem(subreq, req)) {
386 return tevent_req_post(req, ev);
389 tevent_req_set_callback(subreq, smbd_smb2_close_wait_done, req);
390 return req;
393 status = smbd_smb2_close(smb2req,
394 state->in_fsp,
395 state->in_flags,
396 &state->out_flags,
397 &state->out_creation_ts,
398 &state->out_last_access_ts,
399 &state->out_last_write_ts,
400 &state->out_change_ts,
401 &state->out_allocation_size,
402 &state->out_end_of_file,
403 &state->out_file_attributes);
404 if (tevent_req_nterror(req, status)) {
405 return tevent_req_post(req, ev);
408 tevent_req_done(req);
409 return tevent_req_post(req, ev);
412 static void smbd_smb2_close_wait_done(struct tevent_req *subreq)
414 struct tevent_req *req = tevent_req_callback_data(
415 subreq, struct tevent_req);
416 struct smbd_smb2_close_state *state = tevent_req_data(
417 req, struct smbd_smb2_close_state);
418 NTSTATUS status;
420 tevent_queue_wait_recv(subreq);
421 TALLOC_FREE(subreq);
423 status = smbd_smb2_close(state->smb2req,
424 state->in_fsp,
425 state->in_flags,
426 &state->out_flags,
427 &state->out_creation_ts,
428 &state->out_last_access_ts,
429 &state->out_last_write_ts,
430 &state->out_change_ts,
431 &state->out_allocation_size,
432 &state->out_end_of_file,
433 &state->out_file_attributes);
434 if (tevent_req_nterror(req, status)) {
435 return;
437 tevent_req_done(req);
440 static NTSTATUS smbd_smb2_close_recv(struct tevent_req *req,
441 uint16_t *out_flags,
442 struct timespec *out_creation_ts,
443 struct timespec *out_last_access_ts,
444 struct timespec *out_last_write_ts,
445 struct timespec *out_change_ts,
446 uint64_t *out_allocation_size,
447 uint64_t *out_end_of_file,
448 uint32_t *out_file_attributes)
450 struct smbd_smb2_close_state *state =
451 tevent_req_data(req,
452 struct smbd_smb2_close_state);
453 NTSTATUS status;
455 if (tevent_req_is_nterror(req, &status)) {
456 tevent_req_received(req);
457 return status;
460 *out_flags = state->out_flags;
461 *out_creation_ts = state->out_creation_ts;
462 *out_last_access_ts = state->out_last_access_ts;
463 *out_last_write_ts = state->out_last_write_ts;
464 *out_change_ts = state->out_change_ts;
465 *out_allocation_size = state->out_allocation_size;
466 *out_end_of_file = state->out_end_of_file;
467 *out_file_attributes = state->out_file_attributes;
469 tevent_req_received(req);
470 return NT_STATUS_OK;