s3:smb2_close: add missing TALLOC_FREE(subreq) in smbd_smb2_request_close_done()
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_close.c
blob3df408eadc57f0eb7895f604cab3b7133137e9e1
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"
27 static struct tevent_req *smbd_smb2_close_send(TALLOC_CTX *mem_ctx,
28 struct tevent_context *ev,
29 struct smbd_smb2_request *smb2req,
30 uint16_t in_flags,
31 uint64_t in_file_id_volatile);
32 static NTSTATUS smbd_smb2_close_recv(struct tevent_req *req,
33 uint16_t *out_flags,
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;
48 uint16_t in_flags;
49 uint64_t in_file_id_persistent;
50 uint64_t in_file_id_volatile;
51 NTSTATUS status;
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) {
65 /* skip check */
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,
71 req->sconn->ev_ctx,
72 req,
73 in_flags,
74 in_file_id_volatile);
75 if (subreq == NULL) {
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);
88 DATA_BLOB outbody;
89 uint16_t out_flags;
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;
97 NTSTATUS status;
98 NTSTATUS error;
100 status = smbd_smb2_close_recv(subreq,
101 &out_flags,
102 &out_creation_time,
103 &out_last_access_time,
104 &out_last_write_time,
105 &out_change_time,
106 &out_allocation_size,
107 &out_end_of_file,
108 &out_file_attributes);
109 TALLOC_FREE(subreq);
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,
114 nt_errstr(error));
115 return;
117 return;
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,
125 nt_errstr(error));
126 return;
128 return;
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,
145 nt_errstr(error));
146 return;
150 static NTSTATUS smbd_smb2_close(struct smbd_smb2_request *req,
151 uint16_t in_flags,
152 uint64_t in_file_id_volatile,
153 uint16_t *out_flags,
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)
162 NTSTATUS status;
163 struct smb_request *smbreq;
164 connection_struct *conn = req->tcon->compat_conn;
165 files_struct *fsp;
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;
171 uint16_t flags = 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);
179 *out_flags = 0;
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);
197 if (fsp == NULL) {
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(),
209 fsp->fsp_name,
210 &smb_fname);
211 if (!NT_STATUS_IS_OK(status)) {
212 return 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)));
219 return status;
222 if (in_flags & SMB2_CLOSE_FLAGS_FULL_INFORMATION) {
223 int ret;
224 if (posix_open) {
225 ret = SMB_VFS_LSTAT(conn, smb_fname);
226 } else {
227 ret = SMB_VFS_STAT(conn, smb_fname);
229 if (ret == 0) {
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);
251 *out_flags = flags;
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;
269 return NT_STATUS_OK;
272 struct smbd_smb2_close_state {
273 uint16_t in_flags;
274 uint64_t in_file_id_volatile;
275 uint16_t out_flags;
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,
288 uint16_t in_flags,
289 uint64_t in_file_id_volatile)
291 struct tevent_req *req;
292 struct smbd_smb2_close_state *state;
293 NTSTATUS status;
295 req = tevent_req_create(mem_ctx, &state,
296 struct smbd_smb2_close_state);
297 if (req == NULL) {
298 return NULL;
300 state->in_flags = in_flags;
301 state->in_file_id_volatile = in_file_id_volatile;
303 status = smbd_smb2_close(smb2req,
304 state->in_flags,
305 state->in_file_id_volatile,
306 &state->out_flags,
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,
323 uint16_t *out_flags,
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 =
333 tevent_req_data(req,
334 struct smbd_smb2_close_state);
335 NTSTATUS status;
337 if (tevent_req_is_nterror(req, &status)) {
338 tevent_req_received(req);
339 return status;
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);
352 return NT_STATUS_OK;