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
);
109 if (!NT_STATUS_IS_OK(status
)) {
110 error
= smbd_smb2_request_error(req
, status
);
111 if (!NT_STATUS_IS_OK(error
)) {
112 smbd_server_connection_terminate(req
->sconn
,
119 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x3C);
120 if (outbody
.data
== NULL
) {
121 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
122 if (!NT_STATUS_IS_OK(error
)) {
123 smbd_server_connection_terminate(req
->sconn
,
130 SSVAL(outbody
.data
, 0x00, 0x3C); /* struct size */
131 SSVAL(outbody
.data
, 0x02, out_flags
);
132 SIVAL(outbody
.data
, 0x04, 0); /* reserved */
133 SBVAL(outbody
.data
, 0x08, out_creation_time
);
134 SBVAL(outbody
.data
, 0x10, out_last_access_time
);
135 SBVAL(outbody
.data
, 0x18, out_last_write_time
);
136 SBVAL(outbody
.data
, 0x20, out_change_time
);
137 SBVAL(outbody
.data
, 0x28, out_allocation_size
);
138 SBVAL(outbody
.data
, 0x30, out_end_of_file
);
139 SIVAL(outbody
.data
, 0x38, out_file_attributes
);
141 error
= smbd_smb2_request_done(req
, outbody
, NULL
);
142 if (!NT_STATUS_IS_OK(error
)) {
143 smbd_server_connection_terminate(req
->sconn
,
149 static NTSTATUS
smbd_smb2_close(struct smbd_smb2_request
*req
,
151 uint64_t in_file_id_volatile
,
153 NTTIME
*out_creation_time
,
154 NTTIME
*out_last_access_time
,
155 NTTIME
*out_last_write_time
,
156 NTTIME
*out_change_time
,
157 uint64_t *out_allocation_size
,
158 uint64_t *out_end_of_file
,
159 uint32_t *out_file_attributes
)
162 struct smb_request
*smbreq
;
163 connection_struct
*conn
= req
->tcon
->compat_conn
;
165 struct smb_filename
*smb_fname
= NULL
;
166 struct timespec mdate_ts
, adate_ts
, cdate_ts
, create_date_ts
;
167 uint64_t allocation_size
= 0;
168 uint64_t file_size
= 0;
169 uint32_t dos_attrs
= 0;
171 bool posix_open
= false;
173 ZERO_STRUCT(create_date_ts
);
174 ZERO_STRUCT(adate_ts
);
175 ZERO_STRUCT(mdate_ts
);
176 ZERO_STRUCT(cdate_ts
);
179 *out_creation_time
= 0;
180 *out_last_access_time
= 0;
181 *out_last_write_time
= 0;
182 *out_change_time
= 0;
183 *out_allocation_size
= 0;
184 *out_end_of_file
= 0;
185 *out_file_attributes
= 0;
187 DEBUG(10,("smbd_smb2_close: file_id[0x%016llX]\n",
188 (unsigned long long)in_file_id_volatile
));
190 smbreq
= smbd_smb2_fake_smb_request(req
);
191 if (smbreq
== NULL
) {
192 return NT_STATUS_NO_MEMORY
;
195 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
197 return NT_STATUS_FILE_CLOSED
;
199 if (conn
!= fsp
->conn
) {
200 return NT_STATUS_FILE_CLOSED
;
202 if (req
->session
->vuid
!= fsp
->vuid
) {
203 return NT_STATUS_FILE_CLOSED
;
206 posix_open
= fsp
->posix_open
;
207 status
= copy_smb_filename(talloc_tos(),
210 if (!NT_STATUS_IS_OK(status
)) {
214 status
= close_file(smbreq
, fsp
, NORMAL_CLOSE
);
215 if (!NT_STATUS_IS_OK(status
)) {
216 DEBUG(5,("smbd_smb2_close: close_file[%s]: %s\n",
217 fsp_str_dbg(fsp
), nt_errstr(status
)));
221 if (in_flags
& SMB2_CLOSE_FLAGS_FULL_INFORMATION
) {
224 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
226 ret
= SMB_VFS_STAT(conn
, smb_fname
);
229 flags
= SMB2_CLOSE_FLAGS_FULL_INFORMATION
;
230 dos_attrs
= dos_mode(conn
, smb_fname
);
231 mdate_ts
= smb_fname
->st
.st_ex_mtime
;
232 adate_ts
= smb_fname
->st
.st_ex_atime
;
233 create_date_ts
= get_create_timespec(conn
, NULL
, smb_fname
);
234 cdate_ts
= get_change_timespec(conn
, NULL
, smb_fname
);
236 if (lp_dos_filetime_resolution(SNUM(conn
))) {
237 dos_filetime_timespec(&create_date_ts
);
238 dos_filetime_timespec(&mdate_ts
);
239 dos_filetime_timespec(&adate_ts
);
240 dos_filetime_timespec(&cdate_ts
);
242 if (!(dos_attrs
& FILE_ATTRIBUTE_DIRECTORY
)) {
243 file_size
= get_file_size_stat(&smb_fname
->st
);
246 allocation_size
= SMB_VFS_GET_ALLOC_SIZE(conn
, NULL
, &smb_fname
->st
);
252 round_timespec(conn
->ts_res
, &create_date_ts
);
253 unix_timespec_to_nt_time(out_creation_time
, create_date_ts
);
255 round_timespec(conn
->ts_res
, &adate_ts
);
256 unix_timespec_to_nt_time(out_last_access_time
, adate_ts
);
258 round_timespec(conn
->ts_res
, &mdate_ts
);
259 unix_timespec_to_nt_time(out_last_write_time
, mdate_ts
);
261 round_timespec(conn
->ts_res
, &cdate_ts
);
262 unix_timespec_to_nt_time(out_change_time
, cdate_ts
);
264 *out_allocation_size
= allocation_size
;
265 *out_end_of_file
= file_size
;
266 *out_file_attributes
= dos_attrs
;
271 struct smbd_smb2_close_state
{
273 uint64_t in_file_id_volatile
;
275 NTTIME out_creation_time
;
276 NTTIME out_last_access_time
;
277 NTTIME out_last_write_time
;
278 NTTIME out_change_time
;
279 uint64_t out_allocation_size
;
280 uint64_t out_end_of_file
;
281 uint32_t out_file_attributes
;
284 static struct tevent_req
*smbd_smb2_close_send(TALLOC_CTX
*mem_ctx
,
285 struct tevent_context
*ev
,
286 struct smbd_smb2_request
*smb2req
,
288 uint64_t in_file_id_volatile
)
290 struct tevent_req
*req
;
291 struct smbd_smb2_close_state
*state
;
294 req
= tevent_req_create(mem_ctx
, &state
,
295 struct smbd_smb2_close_state
);
299 state
->in_flags
= in_flags
;
300 state
->in_file_id_volatile
= in_file_id_volatile
;
302 status
= smbd_smb2_close(smb2req
,
304 state
->in_file_id_volatile
,
306 &state
->out_creation_time
,
307 &state
->out_last_access_time
,
308 &state
->out_last_write_time
,
309 &state
->out_change_time
,
310 &state
->out_allocation_size
,
311 &state
->out_end_of_file
,
312 &state
->out_file_attributes
);
313 if (tevent_req_nterror(req
, status
)) {
314 return tevent_req_post(req
, ev
);
317 tevent_req_done(req
);
318 return tevent_req_post(req
, ev
);
321 static NTSTATUS
smbd_smb2_close_recv(struct tevent_req
*req
,
323 NTTIME
*out_creation_time
,
324 NTTIME
*out_last_access_time
,
325 NTTIME
*out_last_write_time
,
326 NTTIME
*out_change_time
,
327 uint64_t *out_allocation_size
,
328 uint64_t *out_end_of_file
,
329 uint32_t *out_file_attributes
)
331 struct smbd_smb2_close_state
*state
=
333 struct smbd_smb2_close_state
);
336 if (tevent_req_is_nterror(req
, &status
)) {
337 tevent_req_received(req
);
341 *out_flags
= state
->out_flags
;
342 *out_creation_time
= state
->out_creation_time
;
343 *out_last_access_time
= state
->out_last_access_time
;
344 *out_last_write_time
= state
->out_last_write_time
;
345 *out_change_time
= state
->out_change_time
;
346 *out_allocation_size
= state
->out_allocation_size
;
347 *out_end_of_file
= state
->out_end_of_file
;
348 *out_file_attributes
= state
->out_file_attributes
;
350 tevent_req_received(req
);