s3-wafbuild: Fix inotify detection (bug 8580)
[Samba/bjacke.git] / source3 / smbd / smb2_close.c
blobffe08cc8ac31a1a52ae8d23c2ac59cfeec66b9e7
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"
26 static NTSTATUS smbd_smb2_close(struct smbd_smb2_request *req,
27 uint16_t in_flags,
28 uint64_t in_file_id_volatile,
29 DATA_BLOB *outbody);
31 NTSTATUS smbd_smb2_request_process_close(struct smbd_smb2_request *req)
33 const uint8_t *inbody;
34 int i = req->current_idx;
35 uint8_t *outhdr;
36 DATA_BLOB outbody;
37 uint16_t in_flags;
38 uint64_t in_file_id_persistent;
39 uint64_t in_file_id_volatile;
40 NTSTATUS status;
42 status = smbd_smb2_request_verify_sizes(req, 0x18);
43 if (!NT_STATUS_IS_OK(status)) {
44 return smbd_smb2_request_error(req, status);
46 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
48 outbody = data_blob_talloc(req->out.vector, NULL, 0x3C);
49 if (outbody.data == NULL) {
50 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
53 in_flags = SVAL(inbody, 0x02);
54 in_file_id_persistent = BVAL(inbody, 0x08);
55 in_file_id_volatile = BVAL(inbody, 0x10);
57 if (req->compat_chain_fsp) {
58 /* skip check */
59 } else if (in_file_id_persistent != in_file_id_volatile) {
60 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
63 status = smbd_smb2_close(req,
64 in_flags,
65 in_file_id_volatile,
66 &outbody);
67 if (!NT_STATUS_IS_OK(status)) {
68 return smbd_smb2_request_error(req, status);
71 outhdr = (uint8_t *)req->out.vector[i].iov_base;
72 return smbd_smb2_request_done(req, outbody, NULL);
75 static NTSTATUS smbd_smb2_close(struct smbd_smb2_request *req,
76 uint16_t in_flags,
77 uint64_t in_file_id_volatile,
78 DATA_BLOB *outbody)
80 NTSTATUS status;
81 struct smb_request *smbreq;
82 connection_struct *conn = req->tcon->compat_conn;
83 files_struct *fsp;
84 struct smb_filename *smb_fname = NULL;
85 struct timespec mdate_ts, adate_ts, cdate_ts, create_date_ts;
86 uint64_t allocation_size = 0;
87 uint64_t file_size = 0;
88 uint32_t dos_attrs = 0;
89 uint16_t out_flags = 0;
90 bool posix_open = false;
92 ZERO_STRUCT(create_date_ts);
93 ZERO_STRUCT(adate_ts);
94 ZERO_STRUCT(mdate_ts);
95 ZERO_STRUCT(cdate_ts);
97 DEBUG(10,("smbd_smb2_close: file_id[0x%016llX]\n",
98 (unsigned long long)in_file_id_volatile));
100 smbreq = smbd_smb2_fake_smb_request(req);
101 if (smbreq == NULL) {
102 return NT_STATUS_NO_MEMORY;
105 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
106 if (fsp == NULL) {
107 return NT_STATUS_FILE_CLOSED;
109 if (conn != fsp->conn) {
110 return NT_STATUS_FILE_CLOSED;
112 if (req->session->vuid != fsp->vuid) {
113 return NT_STATUS_FILE_CLOSED;
116 posix_open = fsp->posix_open;
117 status = copy_smb_filename(talloc_tos(),
118 fsp->fsp_name,
119 &smb_fname);
120 if (!NT_STATUS_IS_OK(status)) {
121 return status;
124 status = close_file(smbreq, fsp, NORMAL_CLOSE);
125 if (!NT_STATUS_IS_OK(status)) {
126 DEBUG(5,("smbd_smb2_close: close_file[%s]: %s\n",
127 fsp_str_dbg(fsp), nt_errstr(status)));
128 return status;
131 if (in_flags & SMB2_CLOSE_FLAGS_FULL_INFORMATION) {
132 int ret;
133 if (posix_open) {
134 ret = SMB_VFS_LSTAT(conn, smb_fname);
135 } else {
136 ret = SMB_VFS_STAT(conn, smb_fname);
138 if (ret == 0) {
139 out_flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION;
140 dos_attrs = dos_mode(conn, smb_fname);
141 mdate_ts = smb_fname->st.st_ex_mtime;
142 adate_ts = smb_fname->st.st_ex_atime;
143 create_date_ts = get_create_timespec(conn, NULL, smb_fname);
144 cdate_ts = get_change_timespec(conn, NULL, smb_fname);
146 if (lp_dos_filetime_resolution(SNUM(conn))) {
147 dos_filetime_timespec(&create_date_ts);
148 dos_filetime_timespec(&mdate_ts);
149 dos_filetime_timespec(&adate_ts);
150 dos_filetime_timespec(&cdate_ts);
152 if (!(dos_attrs & FILE_ATTRIBUTE_DIRECTORY)) {
153 file_size = get_file_size_stat(&smb_fname->st);
156 allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn, NULL, &smb_fname->st);
160 SSVAL(outbody->data, 0x00, 0x3C); /* struct size */
161 SSVAL(outbody->data, 0x02, out_flags); /* flags */
162 SIVAL(outbody->data, 0x04, 0); /* reserved */
163 put_long_date_timespec(conn->ts_res,
164 (char *)&outbody->data[0x8],create_date_ts); /* creation time */
165 put_long_date_timespec(conn->ts_res,
166 (char *)&outbody->data[0x10],adate_ts); /* last access time */
167 put_long_date_timespec(conn->ts_res,
168 (char *)&outbody->data[0x18],mdate_ts); /* last write time */
169 put_long_date_timespec(conn->ts_res,
170 (char *)&outbody->data[0x20],cdate_ts); /* change time */
171 SBVAL(outbody->data, 0x28, allocation_size);/* allocation size */
172 SBVAL(outbody->data, 0x30, file_size); /* end of file */
173 SIVAL(outbody->data, 0x38, dos_attrs); /* file attributes */
175 return NT_STATUS_OK;