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"
26 #include "lib/tevent_wait.h"
28 static struct tevent_req
*smbd_smb2_close_send(TALLOC_CTX
*mem_ctx
,
29 struct tevent_context
*ev
,
30 struct smbd_smb2_request
*smb2req
,
31 struct files_struct
*in_fsp
,
33 static NTSTATUS
smbd_smb2_close_recv(struct tevent_req
*req
,
35 NTTIME
*out_creation_time
,
36 NTTIME
*out_last_access_time
,
37 NTTIME
*out_last_write_time
,
38 NTTIME
*out_change_time
,
39 uint64_t *out_allocation_size
,
40 uint64_t *out_end_of_file
,
41 uint32_t *out_file_attributes
);
43 static void smbd_smb2_request_close_done(struct tevent_req
*subreq
);
45 NTSTATUS
smbd_smb2_request_process_close(struct smbd_smb2_request
*req
)
47 const uint8_t *inbody
;
48 int i
= req
->current_idx
;
50 uint64_t in_file_id_persistent
;
51 uint64_t in_file_id_volatile
;
52 struct files_struct
*in_fsp
;
54 struct tevent_req
*subreq
;
56 status
= smbd_smb2_request_verify_sizes(req
, 0x18);
57 if (!NT_STATUS_IS_OK(status
)) {
58 return smbd_smb2_request_error(req
, status
);
60 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
62 in_flags
= SVAL(inbody
, 0x02);
63 in_file_id_persistent
= BVAL(inbody
, 0x08);
64 in_file_id_volatile
= BVAL(inbody
, 0x10);
66 in_fsp
= file_fsp_smb2(req
, in_file_id_persistent
, in_file_id_volatile
);
68 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
71 subreq
= smbd_smb2_close_send(req
, req
->sconn
->ev_ctx
,
72 req
, in_fsp
, in_flags
);
74 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
76 tevent_req_set_callback(subreq
, smbd_smb2_request_close_done
, req
);
78 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
81 static void smbd_smb2_request_close_done(struct tevent_req
*subreq
)
83 struct smbd_smb2_request
*req
=
84 tevent_req_callback_data(subreq
,
85 struct smbd_smb2_request
);
88 NTTIME out_creation_time
;
89 NTTIME out_last_access_time
;
90 NTTIME out_last_write_time
;
91 NTTIME out_change_time
;
92 uint64_t out_allocation_size
;
93 uint64_t out_end_of_file
;
94 uint32_t out_file_attributes
;
98 status
= smbd_smb2_close_recv(subreq
,
101 &out_last_access_time
,
102 &out_last_write_time
,
104 &out_allocation_size
,
106 &out_file_attributes
);
108 if (!NT_STATUS_IS_OK(status
)) {
109 error
= smbd_smb2_request_error(req
, status
);
110 if (!NT_STATUS_IS_OK(error
)) {
111 smbd_server_connection_terminate(req
->sconn
,
118 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x3C);
119 if (outbody
.data
== NULL
) {
120 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
121 if (!NT_STATUS_IS_OK(error
)) {
122 smbd_server_connection_terminate(req
->sconn
,
129 SSVAL(outbody
.data
, 0x00, 0x3C); /* struct size */
130 SSVAL(outbody
.data
, 0x02, out_flags
);
131 SIVAL(outbody
.data
, 0x04, 0); /* reserved */
132 SBVAL(outbody
.data
, 0x08, out_creation_time
);
133 SBVAL(outbody
.data
, 0x10, out_last_access_time
);
134 SBVAL(outbody
.data
, 0x18, out_last_write_time
);
135 SBVAL(outbody
.data
, 0x20, out_change_time
);
136 SBVAL(outbody
.data
, 0x28, out_allocation_size
);
137 SBVAL(outbody
.data
, 0x30, out_end_of_file
);
138 SIVAL(outbody
.data
, 0x38, out_file_attributes
);
140 error
= smbd_smb2_request_done(req
, outbody
, NULL
);
141 if (!NT_STATUS_IS_OK(error
)) {
142 smbd_server_connection_terminate(req
->sconn
,
148 static NTSTATUS
smbd_smb2_close(struct smbd_smb2_request
*req
,
149 struct files_struct
*fsp
,
152 NTTIME
*out_creation_time
,
153 NTTIME
*out_last_access_time
,
154 NTTIME
*out_last_write_time
,
155 NTTIME
*out_change_time
,
156 uint64_t *out_allocation_size
,
157 uint64_t *out_end_of_file
,
158 uint32_t *out_file_attributes
)
161 struct smb_request
*smbreq
;
162 connection_struct
*conn
= req
->tcon
->compat
;
163 struct smb_filename
*smb_fname
= NULL
;
164 struct timespec mdate_ts
, adate_ts
, cdate_ts
, create_date_ts
;
165 uint64_t allocation_size
= 0;
166 uint64_t file_size
= 0;
167 uint32_t dos_attrs
= 0;
169 bool posix_open
= false;
171 ZERO_STRUCT(create_date_ts
);
172 ZERO_STRUCT(adate_ts
);
173 ZERO_STRUCT(mdate_ts
);
174 ZERO_STRUCT(cdate_ts
);
177 *out_creation_time
= 0;
178 *out_last_access_time
= 0;
179 *out_last_write_time
= 0;
180 *out_change_time
= 0;
181 *out_allocation_size
= 0;
182 *out_end_of_file
= 0;
183 *out_file_attributes
= 0;
185 DEBUG(10,("smbd_smb2_close: %s - %s\n",
186 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
188 smbreq
= smbd_smb2_fake_smb_request(req
);
189 if (smbreq
== NULL
) {
190 return NT_STATUS_NO_MEMORY
;
193 posix_open
= fsp
->posix_open
;
194 status
= copy_smb_filename(talloc_tos(),
197 if (!NT_STATUS_IS_OK(status
)) {
201 status
= close_file(smbreq
, fsp
, NORMAL_CLOSE
);
202 if (!NT_STATUS_IS_OK(status
)) {
203 DEBUG(5,("smbd_smb2_close: close_file[%s]: %s\n",
204 fsp_str_dbg(fsp
), nt_errstr(status
)));
208 if (in_flags
& SMB2_CLOSE_FLAGS_FULL_INFORMATION
) {
211 ret
= SMB_VFS_LSTAT(conn
, smb_fname
);
213 ret
= SMB_VFS_STAT(conn
, smb_fname
);
216 flags
= SMB2_CLOSE_FLAGS_FULL_INFORMATION
;
217 dos_attrs
= dos_mode(conn
, smb_fname
);
218 mdate_ts
= smb_fname
->st
.st_ex_mtime
;
219 adate_ts
= smb_fname
->st
.st_ex_atime
;
220 create_date_ts
= get_create_timespec(conn
, NULL
, smb_fname
);
221 cdate_ts
= get_change_timespec(conn
, NULL
, smb_fname
);
223 if (lp_dos_filetime_resolution(SNUM(conn
))) {
224 dos_filetime_timespec(&create_date_ts
);
225 dos_filetime_timespec(&mdate_ts
);
226 dos_filetime_timespec(&adate_ts
);
227 dos_filetime_timespec(&cdate_ts
);
229 if (!(dos_attrs
& FILE_ATTRIBUTE_DIRECTORY
)) {
230 file_size
= get_file_size_stat(&smb_fname
->st
);
233 allocation_size
= SMB_VFS_GET_ALLOC_SIZE(conn
, NULL
, &smb_fname
->st
);
239 round_timespec(conn
->ts_res
, &create_date_ts
);
240 unix_timespec_to_nt_time(out_creation_time
, create_date_ts
);
242 round_timespec(conn
->ts_res
, &adate_ts
);
243 unix_timespec_to_nt_time(out_last_access_time
, adate_ts
);
245 round_timespec(conn
->ts_res
, &mdate_ts
);
246 unix_timespec_to_nt_time(out_last_write_time
, mdate_ts
);
248 round_timespec(conn
->ts_res
, &cdate_ts
);
249 unix_timespec_to_nt_time(out_change_time
, cdate_ts
);
251 *out_allocation_size
= allocation_size
;
252 *out_end_of_file
= file_size
;
253 *out_file_attributes
= dos_attrs
;
258 struct smbd_smb2_close_state
{
259 struct smbd_smb2_request
*smb2req
;
260 struct files_struct
*in_fsp
;
263 NTTIME out_creation_time
;
264 NTTIME out_last_access_time
;
265 NTTIME out_last_write_time
;
266 NTTIME out_change_time
;
267 uint64_t out_allocation_size
;
268 uint64_t out_end_of_file
;
269 uint32_t out_file_attributes
;
272 static void smbd_smb2_close_do(struct tevent_req
*subreq
);
274 static struct tevent_req
*smbd_smb2_close_send(TALLOC_CTX
*mem_ctx
,
275 struct tevent_context
*ev
,
276 struct smbd_smb2_request
*smb2req
,
277 struct files_struct
*in_fsp
,
280 struct tevent_req
*req
;
281 struct smbd_smb2_close_state
*state
;
284 req
= tevent_req_create(mem_ctx
, &state
,
285 struct smbd_smb2_close_state
);
289 state
->smb2req
= smb2req
;
290 state
->in_fsp
= in_fsp
;
291 state
->in_flags
= in_flags
;
293 if (in_fsp
->num_aio_requests
!= 0) {
295 in_fsp
->deferred_close
= tevent_wait_send(in_fsp
, ev
);
296 if (tevent_req_nomem(in_fsp
->deferred_close
, req
)) {
297 return tevent_req_post(req
, ev
);
299 tevent_req_set_callback(in_fsp
->deferred_close
,
300 smbd_smb2_close_do
, req
);
304 status
= smbd_smb2_close(smb2req
,
308 &state
->out_creation_time
,
309 &state
->out_last_access_time
,
310 &state
->out_last_write_time
,
311 &state
->out_change_time
,
312 &state
->out_allocation_size
,
313 &state
->out_end_of_file
,
314 &state
->out_file_attributes
);
315 if (tevent_req_nterror(req
, status
)) {
316 return tevent_req_post(req
, ev
);
319 tevent_req_done(req
);
320 return tevent_req_post(req
, ev
);
323 static void smbd_smb2_close_do(struct tevent_req
*subreq
)
325 struct tevent_req
*req
= tevent_req_callback_data(
326 subreq
, struct tevent_req
);
327 struct smbd_smb2_close_state
*state
= tevent_req_data(
328 req
, struct smbd_smb2_close_state
);
332 ret
= tevent_wait_recv(subreq
);
335 DEBUG(10, ("tevent_wait_recv returned %s\n",
338 * Continue anyway, this should never happen
342 status
= smbd_smb2_close(state
->smb2req
,
346 &state
->out_creation_time
,
347 &state
->out_last_access_time
,
348 &state
->out_last_write_time
,
349 &state
->out_change_time
,
350 &state
->out_allocation_size
,
351 &state
->out_end_of_file
,
352 &state
->out_file_attributes
);
353 if (tevent_req_nterror(req
, status
)) {
356 tevent_req_done(req
);
359 static NTSTATUS
smbd_smb2_close_recv(struct tevent_req
*req
,
361 NTTIME
*out_creation_time
,
362 NTTIME
*out_last_access_time
,
363 NTTIME
*out_last_write_time
,
364 NTTIME
*out_change_time
,
365 uint64_t *out_allocation_size
,
366 uint64_t *out_end_of_file
,
367 uint32_t *out_file_attributes
)
369 struct smbd_smb2_close_state
*state
=
371 struct smbd_smb2_close_state
);
374 if (tevent_req_is_nterror(req
, &status
)) {
375 tevent_req_received(req
);
379 *out_flags
= state
->out_flags
;
380 *out_creation_time
= state
->out_creation_time
;
381 *out_last_access_time
= state
->out_last_access_time
;
382 *out_last_write_time
= state
->out_last_write_time
;
383 *out_change_time
= state
->out_change_time
;
384 *out_allocation_size
= state
->out_allocation_size
;
385 *out_end_of_file
= state
->out_end_of_file
;
386 *out_file_attributes
= state
->out_file_attributes
;
388 tevent_req_received(req
);