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"
27 static struct tevent_req
*smbd_smb2_close_send(TALLOC_CTX
*mem_ctx
,
28 struct tevent_context
*ev
,
29 struct smbd_smb2_request
*smb2req
,
31 uint64_t in_file_id_volatile
);
32 static NTSTATUS
smbd_smb2_close_recv(struct tevent_req
*req
,
34 NTTIME
*out_creation_time
,
35 NTTIME
*out_last_access_time
,
36 NTTIME
*out_last_write_time
,
37 NTTIME
*out_change_time
,
38 uint64_t *out_allocation_size
,
39 uint64_t *out_end_of_file
,
40 uint32_t *out_file_attributes
);
42 static void smbd_smb2_request_close_done(struct tevent_req
*subreq
);
44 NTSTATUS
smbd_smb2_request_process_close(struct smbd_smb2_request
*req
)
46 const uint8_t *inbody
;
47 int i
= req
->current_idx
;
49 uint64_t in_file_id_persistent
;
50 uint64_t in_file_id_volatile
;
52 struct tevent_req
*subreq
;
54 status
= smbd_smb2_request_verify_sizes(req
, 0x18);
55 if (!NT_STATUS_IS_OK(status
)) {
56 return smbd_smb2_request_error(req
, status
);
58 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
60 in_flags
= SVAL(inbody
, 0x02);
61 in_file_id_persistent
= BVAL(inbody
, 0x08);
62 in_file_id_volatile
= BVAL(inbody
, 0x10);
64 if (req
->compat_chain_fsp
) {
66 } else if (in_file_id_persistent
!= in_file_id_volatile
) {
67 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
70 subreq
= smbd_smb2_close_send(req
,
76 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
78 tevent_req_set_callback(subreq
, smbd_smb2_request_close_done
, req
);
80 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
83 static void smbd_smb2_request_close_done(struct tevent_req
*subreq
)
85 struct smbd_smb2_request
*req
=
86 tevent_req_callback_data(subreq
,
87 struct smbd_smb2_request
);
90 NTTIME out_creation_time
;
91 NTTIME out_last_access_time
;
92 NTTIME out_last_write_time
;
93 NTTIME out_change_time
;
94 uint64_t out_allocation_size
;
95 uint64_t out_end_of_file
;
96 uint32_t out_file_attributes
;
100 status
= smbd_smb2_close_recv(subreq
,
103 &out_last_access_time
,
104 &out_last_write_time
,
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
->sconn
,
120 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 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
->sconn
,
131 SSVAL(outbody
.data
, 0x00, 0x3C); /* struct size */
132 SSVAL(outbody
.data
, 0x02, out_flags
);
133 SIVAL(outbody
.data
, 0x04, 0); /* reserved */
134 SBVAL(outbody
.data
, 0x08, out_creation_time
);
135 SBVAL(outbody
.data
, 0x10, out_last_access_time
);
136 SBVAL(outbody
.data
, 0x18, out_last_write_time
);
137 SBVAL(outbody
.data
, 0x20, out_change_time
);
138 SBVAL(outbody
.data
, 0x28, out_allocation_size
);
139 SBVAL(outbody
.data
, 0x30, out_end_of_file
);
140 SIVAL(outbody
.data
, 0x38, out_file_attributes
);
142 error
= smbd_smb2_request_done(req
, outbody
, NULL
);
143 if (!NT_STATUS_IS_OK(error
)) {
144 smbd_server_connection_terminate(req
->sconn
,
150 static NTSTATUS
smbd_smb2_close(struct smbd_smb2_request
*req
,
152 uint64_t in_file_id_volatile
,
154 NTTIME
*out_creation_time
,
155 NTTIME
*out_last_access_time
,
156 NTTIME
*out_last_write_time
,
157 NTTIME
*out_change_time
,
158 uint64_t *out_allocation_size
,
159 uint64_t *out_end_of_file
,
160 uint32_t *out_file_attributes
)
163 struct smb_request
*smbreq
;
164 connection_struct
*conn
= req
->tcon
->compat_conn
;
166 struct smb_filename
*smb_fname
= NULL
;
167 struct timespec mdate_ts
, adate_ts
, cdate_ts
, create_date_ts
;
168 uint64_t allocation_size
= 0;
169 uint64_t file_size
= 0;
170 uint32_t dos_attrs
= 0;
172 bool posix_open
= false;
174 ZERO_STRUCT(create_date_ts
);
175 ZERO_STRUCT(adate_ts
);
176 ZERO_STRUCT(mdate_ts
);
177 ZERO_STRUCT(cdate_ts
);
180 *out_creation_time
= 0;
181 *out_last_access_time
= 0;
182 *out_last_write_time
= 0;
183 *out_change_time
= 0;
184 *out_allocation_size
= 0;
185 *out_end_of_file
= 0;
186 *out_file_attributes
= 0;
188 DEBUG(10,("smbd_smb2_close: file_id[0x%016llX]\n",
189 (unsigned long long)in_file_id_volatile
));
191 smbreq
= smbd_smb2_fake_smb_request(req
);
192 if (smbreq
== NULL
) {
193 return NT_STATUS_NO_MEMORY
;
196 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
198 return NT_STATUS_FILE_CLOSED
;
200 if (conn
!= fsp
->conn
) {
201 return NT_STATUS_FILE_CLOSED
;
203 if (req
->session
->vuid
!= fsp
->vuid
) {
204 return NT_STATUS_FILE_CLOSED
;
207 posix_open
= fsp
->posix_open
;
208 status
= copy_smb_filename(talloc_tos(),
211 if (!NT_STATUS_IS_OK(status
)) {
215 status
= close_file(smbreq
, fsp
, NORMAL_CLOSE
);
216 if (!NT_STATUS_IS_OK(status
)) {
217 DEBUG(5,("smbd_smb2_close: close_file[%s]: %s\n",
218 fsp_str_dbg(fsp
), nt_errstr(status
)));
222 if (in_flags
& SMB2_CLOSE_FLAGS_FULL_INFORMATION
) {
225 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
227 ret
= SMB_VFS_STAT(conn
, smb_fname
);
230 flags
= SMB2_CLOSE_FLAGS_FULL_INFORMATION
;
231 dos_attrs
= dos_mode(conn
, smb_fname
);
232 mdate_ts
= smb_fname
->st
.st_ex_mtime
;
233 adate_ts
= smb_fname
->st
.st_ex_atime
;
234 create_date_ts
= get_create_timespec(conn
, NULL
, smb_fname
);
235 cdate_ts
= get_change_timespec(conn
, NULL
, smb_fname
);
237 if (lp_dos_filetime_resolution(SNUM(conn
))) {
238 dos_filetime_timespec(&create_date_ts
);
239 dos_filetime_timespec(&mdate_ts
);
240 dos_filetime_timespec(&adate_ts
);
241 dos_filetime_timespec(&cdate_ts
);
243 if (!(dos_attrs
& FILE_ATTRIBUTE_DIRECTORY
)) {
244 file_size
= get_file_size_stat(&smb_fname
->st
);
247 allocation_size
= SMB_VFS_GET_ALLOC_SIZE(conn
, NULL
, &smb_fname
->st
);
253 round_timespec(conn
->ts_res
, &create_date_ts
);
254 unix_timespec_to_nt_time(out_creation_time
, create_date_ts
);
256 round_timespec(conn
->ts_res
, &adate_ts
);
257 unix_timespec_to_nt_time(out_last_access_time
, adate_ts
);
259 round_timespec(conn
->ts_res
, &mdate_ts
);
260 unix_timespec_to_nt_time(out_last_write_time
, mdate_ts
);
262 round_timespec(conn
->ts_res
, &cdate_ts
);
263 unix_timespec_to_nt_time(out_change_time
, cdate_ts
);
265 *out_allocation_size
= allocation_size
;
266 *out_end_of_file
= file_size
;
267 *out_file_attributes
= dos_attrs
;
272 struct smbd_smb2_close_state
{
274 uint64_t in_file_id_volatile
;
276 NTTIME out_creation_time
;
277 NTTIME out_last_access_time
;
278 NTTIME out_last_write_time
;
279 NTTIME out_change_time
;
280 uint64_t out_allocation_size
;
281 uint64_t out_end_of_file
;
282 uint32_t out_file_attributes
;
285 static struct tevent_req
*smbd_smb2_close_send(TALLOC_CTX
*mem_ctx
,
286 struct tevent_context
*ev
,
287 struct smbd_smb2_request
*smb2req
,
289 uint64_t in_file_id_volatile
)
291 struct tevent_req
*req
;
292 struct smbd_smb2_close_state
*state
;
295 req
= tevent_req_create(mem_ctx
, &state
,
296 struct smbd_smb2_close_state
);
300 state
->in_flags
= in_flags
;
301 state
->in_file_id_volatile
= in_file_id_volatile
;
303 status
= smbd_smb2_close(smb2req
,
305 state
->in_file_id_volatile
,
307 &state
->out_creation_time
,
308 &state
->out_last_access_time
,
309 &state
->out_last_write_time
,
310 &state
->out_change_time
,
311 &state
->out_allocation_size
,
312 &state
->out_end_of_file
,
313 &state
->out_file_attributes
);
314 if (tevent_req_nterror(req
, status
)) {
315 return tevent_req_post(req
, ev
);
318 tevent_req_done(req
);
319 return tevent_req_post(req
, ev
);
322 static NTSTATUS
smbd_smb2_close_recv(struct tevent_req
*req
,
324 NTTIME
*out_creation_time
,
325 NTTIME
*out_last_access_time
,
326 NTTIME
*out_last_write_time
,
327 NTTIME
*out_change_time
,
328 uint64_t *out_allocation_size
,
329 uint64_t *out_end_of_file
,
330 uint32_t *out_file_attributes
)
332 struct smbd_smb2_close_state
*state
=
334 struct smbd_smb2_close_state
);
337 if (tevent_req_is_nterror(req
, &status
)) {
338 tevent_req_received(req
);
342 *out_flags
= state
->out_flags
;
343 *out_creation_time
= state
->out_creation_time
;
344 *out_last_access_time
= state
->out_last_access_time
;
345 *out_last_write_time
= state
->out_last_write_time
;
346 *out_change_time
= state
->out_change_time
;
347 *out_allocation_size
= state
->out_allocation_size
;
348 *out_end_of_file
= state
->out_end_of_file
;
349 *out_file_attributes
= state
->out_file_attributes
;
351 tevent_req_received(req
);