s3-libnet: avoid using lp_dns_hostname() in join code
[samba4-gss.git] / source3 / smbd / smb2_close.c
blobd68c0bd9e6c2a8a509dea1cf92883b3b352ce735
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 file_free(smbreq, fsp);
229 *_fsp = fsp = NULL;
230 return status;
233 if (in_flags & SMB2_CLOSE_FLAGS_FULL_INFORMATION) {
234 setup_close_full_information(conn,
235 fsp->fsp_name,
236 out_creation_ts,
237 out_last_access_ts,
238 out_last_write_ts,
239 out_change_ts,
240 out_flags,
241 out_allocation_size,
242 out_end_of_file);
245 file_free(smbreq, fsp);
246 *_fsp = fsp = NULL;
247 return NT_STATUS_OK;
250 struct smbd_smb2_close_state {
251 struct smbd_smb2_request *smb2req;
252 struct files_struct *in_fsp;
253 uint16_t in_flags;
254 uint16_t out_flags;
255 struct timespec out_creation_ts;
256 struct timespec out_last_access_ts;
257 struct timespec out_last_write_ts;
258 struct timespec out_change_ts;
259 uint64_t out_allocation_size;
260 uint64_t out_end_of_file;
261 uint32_t out_file_attributes;
262 struct tevent_queue *wait_queue;
265 static void smbd_smb2_close_wait_done(struct tevent_req *subreq);
266 static void smbd_smb2_close_delay_lease_break_done(struct tevent_req *subreq);
268 static struct tevent_req *smbd_smb2_close_send(TALLOC_CTX *mem_ctx,
269 struct tevent_context *ev,
270 struct smbd_smb2_request *smb2req,
271 struct files_struct *in_fsp,
272 uint16_t in_flags)
274 struct tevent_req *req;
275 struct smbd_smb2_close_state *state;
276 const char *fsp_name_str = NULL;
277 const char *fsp_fnum_str = NULL;
278 unsigned i;
279 NTSTATUS status;
281 if (CHECK_DEBUGLVL(DBGLVL_INFO)) {
282 fsp_name_str = fsp_str_dbg(in_fsp);
283 fsp_fnum_str = fsp_fnum_dbg(in_fsp);
286 DBG_DEBUG("%s - %s\n", fsp_name_str, fsp_fnum_str);
288 req = tevent_req_create(mem_ctx, &state,
289 struct smbd_smb2_close_state);
290 if (req == NULL) {
291 return NULL;
293 state->smb2req = smb2req;
294 state->in_fsp = in_fsp;
295 state->in_flags = in_flags;
297 in_fsp->fsp_flags.closing = true;
299 i = 0;
300 while (i < in_fsp->num_aio_requests) {
301 bool ok = tevent_req_cancel(in_fsp->aio_requests[i]);
302 if (ok) {
303 continue;
305 i += 1;
308 if (in_fsp->num_aio_requests != 0) {
309 struct tevent_req *subreq;
311 state->wait_queue = tevent_queue_create(state,
312 "smbd_smb2_close_send_wait_queue");
313 if (tevent_req_nomem(state->wait_queue, req)) {
314 return tevent_req_post(req, ev);
317 * Now wait until all aio requests on this fsp are
318 * finished.
320 * We don't set a callback, as we just want to block the
321 * wait queue and the talloc_free() of fsp->aio_request
322 * will remove the item from the wait queue.
324 subreq = tevent_queue_wait_send(in_fsp->aio_requests,
325 smb2req->sconn->ev_ctx,
326 state->wait_queue);
327 if (tevent_req_nomem(subreq, req)) {
328 return tevent_req_post(req, ev);
332 * Now we add our own waiter to the end of the queue,
333 * this way we get notified when all pending requests are
334 * finished.
336 subreq = tevent_queue_wait_send(state,
337 smb2req->sconn->ev_ctx,
338 state->wait_queue);
339 if (tevent_req_nomem(subreq, req)) {
340 return tevent_req_post(req, ev);
343 tevent_req_set_callback(subreq, smbd_smb2_close_wait_done, req);
344 return req;
347 if (in_fsp->fsp_flags.initial_delete_on_close) {
348 struct tevent_req *subreq = NULL;
349 struct share_mode_lock *lck = NULL;
350 struct timeval timeout;
352 lck = get_existing_share_mode_lock(mem_ctx,
353 in_fsp->file_id);
354 if (lck == NULL) {
355 tevent_req_nterror(req,
356 NT_STATUS_UNSUCCESSFUL);
357 return tevent_req_post(req, ev);
360 if (is_delete_on_close_set(lck, in_fsp->name_hash)) {
361 TALLOC_FREE(lck);
362 goto do_close;
365 timeout = tevent_timeval_set(OPLOCK_BREAK_TIMEOUT, 0);
366 timeout = timeval_sum(&smb2req->request_time, &timeout);
368 subreq = delay_for_handle_lease_break_send(req,
370 timeout,
371 in_fsp,
372 false,
373 &lck);
374 if (tevent_req_nomem(subreq, req)) {
375 TALLOC_FREE(lck);
376 return tevent_req_post(req, ev);
378 if (tevent_req_is_in_progress(subreq)) {
379 tevent_req_set_callback(
380 subreq,
381 smbd_smb2_close_delay_lease_break_done,
382 req);
383 return req;
386 status = delay_for_handle_lease_break_recv(subreq,
387 mem_ctx,
388 &lck);
389 TALLOC_FREE(subreq);
390 TALLOC_FREE(lck);
391 if (tevent_req_nterror(req, status)) {
392 return tevent_req_post(req, ev);
394 /* Fall through */
397 do_close:
398 status = smbd_smb2_close(smb2req,
399 &state->in_fsp,
400 state->in_flags,
401 &state->out_flags,
402 &state->out_creation_ts,
403 &state->out_last_access_ts,
404 &state->out_last_write_ts,
405 &state->out_change_ts,
406 &state->out_allocation_size,
407 &state->out_end_of_file,
408 &state->out_file_attributes);
409 if (tevent_req_nterror(req, status)) {
410 DBG_INFO("%s - %s: close file failed: %s\n",
411 fsp_name_str, fsp_fnum_str,
412 nt_errstr(status));
413 return tevent_req_post(req, ev);
416 tevent_req_done(req);
417 return tevent_req_post(req, ev);
420 static void smbd_smb2_close_wait_done(struct tevent_req *subreq)
422 struct tevent_req *req = tevent_req_callback_data(
423 subreq, struct tevent_req);
424 struct smbd_smb2_close_state *state = tevent_req_data(
425 req, struct smbd_smb2_close_state);
426 NTSTATUS status;
428 tevent_queue_wait_recv(subreq);
429 TALLOC_FREE(subreq);
431 status = smbd_smb2_close(state->smb2req,
432 &state->in_fsp,
433 state->in_flags,
434 &state->out_flags,
435 &state->out_creation_ts,
436 &state->out_last_access_ts,
437 &state->out_last_write_ts,
438 &state->out_change_ts,
439 &state->out_allocation_size,
440 &state->out_end_of_file,
441 &state->out_file_attributes);
442 if (tevent_req_nterror(req, status)) {
443 return;
445 tevent_req_done(req);
448 static void smbd_smb2_close_delay_lease_break_done(struct tevent_req *subreq)
450 struct tevent_req *req = tevent_req_callback_data(subreq, struct tevent_req);
451 struct smbd_smb2_close_state *state = tevent_req_data(
452 req, struct smbd_smb2_close_state);
453 struct share_mode_lock *lck = NULL;
454 NTSTATUS status;
455 bool ok;
457 status = delay_for_handle_lease_break_recv(subreq, state, &lck);
458 TALLOC_FREE(subreq);
459 TALLOC_FREE(lck);
460 if (tevent_req_nterror(req, status)) {
461 return;
465 * Make sure we run as the user again
467 ok = change_to_user_and_service(
468 state->smb2req->tcon->compat,
469 state->smb2req->session->global->session_wire_id);
470 if (!ok) {
471 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
472 return;
475 status = smbd_smb2_close(state->smb2req,
476 &state->in_fsp,
477 state->in_flags,
478 &state->out_flags,
479 &state->out_creation_ts,
480 &state->out_last_access_ts,
481 &state->out_last_write_ts,
482 &state->out_change_ts,
483 &state->out_allocation_size,
484 &state->out_end_of_file,
485 &state->out_file_attributes);
486 if (tevent_req_nterror(req, status)) {
487 return;
489 tevent_req_done(req);
492 static NTSTATUS smbd_smb2_close_recv(struct tevent_req *req,
493 uint16_t *out_flags,
494 struct timespec *out_creation_ts,
495 struct timespec *out_last_access_ts,
496 struct timespec *out_last_write_ts,
497 struct timespec *out_change_ts,
498 uint64_t *out_allocation_size,
499 uint64_t *out_end_of_file,
500 uint32_t *out_file_attributes)
502 struct smbd_smb2_close_state *state =
503 tevent_req_data(req,
504 struct smbd_smb2_close_state);
505 NTSTATUS status;
507 if (tevent_req_is_nterror(req, &status)) {
508 tevent_req_received(req);
509 return status;
512 *out_flags = state->out_flags;
513 *out_creation_ts = state->out_creation_ts;
514 *out_last_access_ts = state->out_last_access_ts;
515 *out_last_write_ts = state->out_last_write_ts;
516 *out_change_ts = state->out_change_ts;
517 *out_allocation_size = state->out_allocation_size;
518 *out_end_of_file = state->out_end_of_file;
519 *out_file_attributes = state->out_file_attributes;
521 tevent_req_received(req);
522 return NT_STATUS_OK;