1/*
2   Unix SMB/CIFS implementation.
3   Core SMB2 server
4
5   Copyright (C) Stefan Metzmacher 2009
6
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.
11
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.
16
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/>.
19*/
20
21#include "includes.h"
22#include "smbd/globals.h"
23#include "../libcli/smb/smb_common.h"
24
25static NTSTATUS smbd_smb2_close(struct smbd_smb2_request *req,
26				uint16_t in_flags,
27				uint64_t in_file_id_volatile);
28
29NTSTATUS smbd_smb2_request_process_close(struct smbd_smb2_request *req)
30{
31	const uint8_t *inhdr;
32	const uint8_t *inbody;
33	int i = req->current_idx;
34	uint8_t *outhdr;
35	DATA_BLOB outbody;
36	size_t expected_body_size = 0x18;
37	size_t body_size;
38	uint16_t in_flags;
39	uint64_t in_file_id_persistent;
40	uint64_t in_file_id_volatile;
41	NTSTATUS status;
42
43	inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
44	if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
45		return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
46	}
47
48	inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
49
50	body_size = SVAL(inbody, 0x00);
51	if (body_size != expected_body_size) {
52		return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
53	}
54
55	in_flags		= SVAL(inbody, 0x02);
56	in_file_id_persistent	= BVAL(inbody, 0x08);
57	in_file_id_volatile	= BVAL(inbody, 0x10);
58
59	if (req->compat_chain_fsp) {
60		/* skip check */
61	} else if (in_file_id_persistent != 0) {
62		return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
63	}
64
65	status = smbd_smb2_close(req,
66				in_flags,
67				in_file_id_volatile);
68	if (!NT_STATUS_IS_OK(status)) {
69		return smbd_smb2_request_error(req, status);
70	}
71
72	outhdr = (uint8_t *)req->out.vector[i].iov_base;
73
74	outbody = data_blob_talloc(req->out.vector, NULL, 0x3C);
75	if (outbody.data == NULL) {
76		return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
77	}
78
79	SSVAL(outbody.data, 0x00, 0x3C);	/* struct size */
80	SSVAL(outbody.data, 0x02, 0);		/* flags */
81	SIVAL(outbody.data, 0x04, 0);		/* reserved */
82	SBVAL(outbody.data, 0x08, 0);		/* creation time */
83	SBVAL(outbody.data, 0x10, 0);		/* last access time */
84	SBVAL(outbody.data, 0x18, 0);		/* last write time */
85	SBVAL(outbody.data, 0x20, 0);		/* change time */
86	SBVAL(outbody.data, 0x28, 0);		/* allocation size */
87	SBVAL(outbody.data, 0x30, 0);		/* end of size */
88	SIVAL(outbody.data, 0x38, 0);		/* file attributes */
89
90	return smbd_smb2_request_done(req, outbody, NULL);
91}
92
93static NTSTATUS smbd_smb2_close(struct smbd_smb2_request *req,
94				uint16_t in_flags,
95				uint64_t in_file_id_volatile)
96{
97	NTSTATUS status;
98	struct smb_request *smbreq;
99	connection_struct *conn = req->tcon->compat_conn;
100	files_struct *fsp;
101
102	DEBUG(10,("smbd_smb2_close: file_id[0x%016llX]\n",
103		  (unsigned long long)in_file_id_volatile));
104
105	smbreq = smbd_smb2_fake_smb_request(req);
106	if (smbreq == NULL) {
107		return NT_STATUS_NO_MEMORY;
108	}
109
110	fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
111	if (fsp == NULL) {
112		return NT_STATUS_FILE_CLOSED;
113	}
114	if (conn != fsp->conn) {
115		return NT_STATUS_FILE_CLOSED;
116	}
117	if (req->session->vuid != fsp->vuid) {
118		return NT_STATUS_FILE_CLOSED;
119	}
120
121	status = close_file(smbreq, fsp, NORMAL_CLOSE);
122	if (!NT_STATUS_IS_OK(status)) {
123		DEBUG(5,("smbd_smb2_close: close_file[%s]: %s\n",
124			 fsp_str_dbg(fsp), nt_errstr(status)));
125		return status;
126	}
127
128	return NT_STATUS_OK;
129}
130