Add two flags to allow for handling of Extended Signatures (Session Key Protection...
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_close.c
blob6d93278e520d97272fe66cc16af3feda4aa9bfc9
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"
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,
32 uint16_t in_flags);
33 static NTSTATUS smbd_smb2_close_recv(struct tevent_req *req,
34 uint16_t *out_flags,
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;
49 uint16_t in_flags;
50 uint64_t in_file_id_persistent;
51 uint64_t in_file_id_volatile;
52 struct files_struct *in_fsp;
53 NTSTATUS status;
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);
67 if (in_fsp == NULL) {
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);
73 if (subreq == NULL) {
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);
86 DATA_BLOB outbody;
87 uint16_t out_flags;
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;
95 NTSTATUS status;
96 NTSTATUS error;
98 status = smbd_smb2_close_recv(subreq,
99 &out_flags,
100 &out_creation_time,
101 &out_last_access_time,
102 &out_last_write_time,
103 &out_change_time,
104 &out_allocation_size,
105 &out_end_of_file,
106 &out_file_attributes);
107 TALLOC_FREE(subreq);
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,
112 nt_errstr(error));
113 return;
115 return;
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,
123 nt_errstr(error));
124 return;
126 return;
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,
143 nt_errstr(error));
144 return;
148 static NTSTATUS smbd_smb2_close(struct smbd_smb2_request *req,
149 struct files_struct *fsp,
150 uint16_t in_flags,
151 uint16_t *out_flags,
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)
160 NTSTATUS status;
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;
168 uint16_t flags = 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);
176 *out_flags = 0;
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(),
195 fsp->fsp_name,
196 &smb_fname);
197 if (!NT_STATUS_IS_OK(status)) {
198 return 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)));
205 return status;
208 if (in_flags & SMB2_CLOSE_FLAGS_FULL_INFORMATION) {
209 int ret;
210 if (posix_open) {
211 ret = SMB_VFS_LSTAT(conn, smb_fname);
212 } else {
213 ret = SMB_VFS_STAT(conn, smb_fname);
215 if (ret == 0) {
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);
237 *out_flags = flags;
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;
255 return NT_STATUS_OK;
258 struct smbd_smb2_close_state {
259 struct smbd_smb2_request *smb2req;
260 struct files_struct *in_fsp;
261 uint16_t in_flags;
262 uint16_t out_flags;
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,
278 uint16_t in_flags)
280 struct tevent_req *req;
281 struct smbd_smb2_close_state *state;
282 NTSTATUS status;
284 req = tevent_req_create(mem_ctx, &state,
285 struct smbd_smb2_close_state);
286 if (req == NULL) {
287 return NULL;
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);
301 return req;
304 status = smbd_smb2_close(smb2req,
305 state->in_fsp,
306 state->in_flags,
307 &state->out_flags,
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);
329 NTSTATUS status;
330 int ret;
332 ret = tevent_wait_recv(subreq);
333 TALLOC_FREE(subreq);
334 if (ret != 0) {
335 DEBUG(10, ("tevent_wait_recv returned %s\n",
336 strerror(ret)));
338 * Continue anyway, this should never happen
342 status = smbd_smb2_close(state->smb2req,
343 state->in_fsp,
344 state->in_flags,
345 &state->out_flags,
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)) {
354 return;
356 tevent_req_done(req);
359 static NTSTATUS smbd_smb2_close_recv(struct tevent_req *req,
360 uint16_t *out_flags,
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 =
370 tevent_req_data(req,
371 struct smbd_smb2_close_state);
372 NTSTATUS status;
374 if (tevent_req_is_nterror(req, &status)) {
375 tevent_req_received(req);
376 return status;
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);
389 return NT_STATUS_OK;