2 Unix SMB/CIFS implementation.
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/>.
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../lib/util/tevent_ntstatus.h"
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
,
35 static NTSTATUS
smbd_smb2_close_recv(struct tevent_req
*req
,
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
;
51 uint64_t in_file_id_persistent
;
52 uint64_t in_file_id_volatile
;
53 struct files_struct
*in_fsp
;
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
);
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
);
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
);
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;
100 status
= smbd_smb2_close_recv(subreq
,
106 &out_allocation_size
,
108 &out_file_attributes
);
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
,
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
,
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
,
154 static void setup_close_full_information(connection_struct
*conn
,
155 struct smb_filename
*smb_fname
,
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
,
162 uint64_t *out_allocation_size
,
163 uint64_t *out_end_of_file
,
164 uint32_t *out_file_attributes
)
168 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
170 ret
= SMB_VFS_STAT(conn
, smb_fname
);
176 *out_flags
= SMB2_CLOSE_FLAGS_FULL_INFORMATION
;
177 *out_file_attributes
= dos_mode(conn
, smb_fname
);
178 *out_last_write_ts
= smb_fname
->st
.st_ex_mtime
;
179 *out_last_access_ts
= smb_fname
->st
.st_ex_atime
;
180 *out_creation_ts
= get_create_timespec(conn
, NULL
, smb_fname
);
181 *out_change_ts
= get_change_timespec(conn
, NULL
, smb_fname
);
183 if (lp_dos_filetime_resolution(SNUM(conn
))) {
184 dos_filetime_timespec(out_creation_ts
);
185 dos_filetime_timespec(out_last_write_ts
);
186 dos_filetime_timespec(out_last_access_ts
);
187 dos_filetime_timespec(out_change_ts
);
189 if (!(*out_file_attributes
& FILE_ATTRIBUTE_DIRECTORY
)) {
190 *out_end_of_file
= get_file_size_stat(&smb_fname
->st
);
193 *out_allocation_size
= SMB_VFS_GET_ALLOC_SIZE(conn
, NULL
, &smb_fname
->st
);
196 static NTSTATUS
smbd_smb2_close(struct smbd_smb2_request
*req
,
197 struct files_struct
*fsp
,
200 struct timespec
*out_creation_ts
,
201 struct timespec
*out_last_access_ts
,
202 struct timespec
*out_last_write_ts
,
203 struct timespec
*out_change_ts
,
204 uint64_t *out_allocation_size
,
205 uint64_t *out_end_of_file
,
206 uint32_t *out_file_attributes
)
209 struct smb_request
*smbreq
;
210 connection_struct
*conn
= req
->tcon
->compat
;
211 struct smb_filename
*smb_fname
= NULL
;
212 uint64_t allocation_size
= 0;
213 uint64_t file_size
= 0;
214 uint32_t dos_attrs
= 0;
216 bool posix_open
= false;
218 *out_creation_ts
= (struct timespec
){0, SAMBA_UTIME_OMIT
};
219 *out_last_access_ts
= (struct timespec
){0, SAMBA_UTIME_OMIT
};
220 *out_last_write_ts
= (struct timespec
){0, SAMBA_UTIME_OMIT
};
221 *out_change_ts
= (struct timespec
){0, SAMBA_UTIME_OMIT
};
224 *out_allocation_size
= 0;
225 *out_end_of_file
= 0;
226 *out_file_attributes
= 0;
228 DEBUG(10,("smbd_smb2_close: %s - %s\n",
229 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
231 smbreq
= smbd_smb2_fake_smb_request(req
);
232 if (smbreq
== NULL
) {
233 return NT_STATUS_NO_MEMORY
;
236 posix_open
= (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
);
237 smb_fname
= cp_smb_filename(talloc_tos(), fsp
->fsp_name
);
238 if (smb_fname
== NULL
) {
239 return NT_STATUS_NO_MEMORY
;
242 if ((in_flags
& SMB2_CLOSE_FLAGS_FULL_INFORMATION
) &&
243 (fsp
->fsp_flags
.initial_delete_on_close
||
244 fsp
->fsp_flags
.delete_on_close
))
247 * We might be deleting the file. Ensure we
248 * return valid data from before the file got
251 setup_close_full_information(conn
,
264 status
= close_file(smbreq
, fsp
, NORMAL_CLOSE
);
265 if (!NT_STATUS_IS_OK(status
)) {
266 DEBUG(5,("smbd_smb2_close: close_file[%s]: %s\n",
267 smb_fname_str_dbg(smb_fname
), nt_errstr(status
)));
271 if (in_flags
& SMB2_CLOSE_FLAGS_FULL_INFORMATION
) {
272 setup_close_full_information(conn
,
286 *out_allocation_size
= allocation_size
;
287 *out_end_of_file
= file_size
;
288 *out_file_attributes
= dos_attrs
;
293 struct smbd_smb2_close_state
{
294 struct smbd_smb2_request
*smb2req
;
295 struct files_struct
*in_fsp
;
298 struct timespec out_creation_ts
;
299 struct timespec out_last_access_ts
;
300 struct timespec out_last_write_ts
;
301 struct timespec out_change_ts
;
302 uint64_t out_allocation_size
;
303 uint64_t out_end_of_file
;
304 uint32_t out_file_attributes
;
305 struct tevent_queue
*wait_queue
;
308 static void smbd_smb2_close_wait_done(struct tevent_req
*subreq
);
310 static struct tevent_req
*smbd_smb2_close_send(TALLOC_CTX
*mem_ctx
,
311 struct tevent_context
*ev
,
312 struct smbd_smb2_request
*smb2req
,
313 struct files_struct
*in_fsp
,
316 struct tevent_req
*req
;
317 struct smbd_smb2_close_state
*state
;
321 req
= tevent_req_create(mem_ctx
, &state
,
322 struct smbd_smb2_close_state
);
326 state
->smb2req
= smb2req
;
327 state
->in_fsp
= in_fsp
;
328 state
->in_flags
= in_flags
;
330 in_fsp
->fsp_flags
.closing
= true;
333 while (i
< in_fsp
->num_aio_requests
) {
334 bool ok
= tevent_req_cancel(in_fsp
->aio_requests
[i
]);
341 if (in_fsp
->num_aio_requests
!= 0) {
342 struct tevent_req
*subreq
;
344 state
->wait_queue
= tevent_queue_create(state
,
345 "smbd_smb2_close_send_wait_queue");
346 if (tevent_req_nomem(state
->wait_queue
, req
)) {
347 return tevent_req_post(req
, ev
);
350 * Now wait until all aio requests on this fsp are
353 * We don't set a callback, as we just want to block the
354 * wait queue and the talloc_free() of fsp->aio_request
355 * will remove the item from the wait queue.
357 subreq
= tevent_queue_wait_send(in_fsp
->aio_requests
,
358 smb2req
->sconn
->ev_ctx
,
360 if (tevent_req_nomem(subreq
, req
)) {
361 return tevent_req_post(req
, ev
);
365 * Now we add our own waiter to the end of the queue,
366 * this way we get notified when all pending requests are
369 subreq
= tevent_queue_wait_send(state
,
370 smb2req
->sconn
->ev_ctx
,
372 if (tevent_req_nomem(subreq
, req
)) {
373 return tevent_req_post(req
, ev
);
376 tevent_req_set_callback(subreq
, smbd_smb2_close_wait_done
, req
);
380 status
= smbd_smb2_close(smb2req
,
384 &state
->out_creation_ts
,
385 &state
->out_last_access_ts
,
386 &state
->out_last_write_ts
,
387 &state
->out_change_ts
,
388 &state
->out_allocation_size
,
389 &state
->out_end_of_file
,
390 &state
->out_file_attributes
);
391 if (tevent_req_nterror(req
, status
)) {
392 return tevent_req_post(req
, ev
);
395 tevent_req_done(req
);
396 return tevent_req_post(req
, ev
);
399 static void smbd_smb2_close_wait_done(struct tevent_req
*subreq
)
401 struct tevent_req
*req
= tevent_req_callback_data(
402 subreq
, struct tevent_req
);
403 struct smbd_smb2_close_state
*state
= tevent_req_data(
404 req
, struct smbd_smb2_close_state
);
407 tevent_queue_wait_recv(subreq
);
410 status
= smbd_smb2_close(state
->smb2req
,
414 &state
->out_creation_ts
,
415 &state
->out_last_access_ts
,
416 &state
->out_last_write_ts
,
417 &state
->out_change_ts
,
418 &state
->out_allocation_size
,
419 &state
->out_end_of_file
,
420 &state
->out_file_attributes
);
421 if (tevent_req_nterror(req
, status
)) {
424 tevent_req_done(req
);
427 static NTSTATUS
smbd_smb2_close_recv(struct tevent_req
*req
,
429 struct timespec
*out_creation_ts
,
430 struct timespec
*out_last_access_ts
,
431 struct timespec
*out_last_write_ts
,
432 struct timespec
*out_change_ts
,
433 uint64_t *out_allocation_size
,
434 uint64_t *out_end_of_file
,
435 uint32_t *out_file_attributes
)
437 struct smbd_smb2_close_state
*state
=
439 struct smbd_smb2_close_state
);
442 if (tevent_req_is_nterror(req
, &status
)) {
443 tevent_req_received(req
);
447 *out_flags
= state
->out_flags
;
448 *out_creation_ts
= state
->out_creation_ts
;
449 *out_last_access_ts
= state
->out_last_access_ts
;
450 *out_last_write_ts
= state
->out_last_write_ts
;
451 *out_change_ts
= state
->out_change_ts
;
452 *out_allocation_size
= state
->out_allocation_size
;
453 *out_end_of_file
= state
->out_end_of_file
;
454 *out_file_attributes
= state
->out_file_attributes
;
456 tevent_req_received(req
);