s3:smbd: implement SMB2 Flush
[ira/wip.git] / source3 / smbd / smb2_flush.c
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 "../source4/libcli/smb2/smb2_constants.h"
24
25 static NTSTATUS smbd_smb2_flush(struct smbd_smb2_request *req,
26                                 uint64_t in_file_id_volatile);
27
28 NTSTATUS smbd_smb2_request_process_flush(struct smbd_smb2_request *req)
29 {
30         const uint8_t *inhdr;
31         const uint8_t *inbody;
32         int i = req->current_idx;
33         DATA_BLOB outbody;
34         size_t expected_body_size = 0x18;
35         size_t body_size;
36         uint64_t in_file_id_persistent;
37         uint64_t in_file_id_volatile;
38         NTSTATUS status;
39
40         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
41         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
42                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
43         }
44
45         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
46
47         body_size = SVAL(inbody, 0x00);
48         if (body_size != expected_body_size) {
49                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
50         }
51
52         in_file_id_persistent   = BVAL(inbody, 0x08);
53         in_file_id_volatile     = BVAL(inbody, 0x10);
54
55         if (in_file_id_persistent != 0) {
56                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
57         }
58
59         status = smbd_smb2_flush(req,
60                                  in_file_id_volatile);
61         if (!NT_STATUS_IS_OK(status)) {
62                 return smbd_smb2_request_error(req, status);
63         }
64
65         outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
66         if (outbody.data == NULL) {
67                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
68         }
69
70         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
71         SSVAL(outbody.data, 0x02, 0);           /* reserved */
72
73         return smbd_smb2_request_done(req, outbody, NULL);
74 }
75
76 static NTSTATUS smbd_smb2_flush(struct smbd_smb2_request *req,
77                                 uint64_t in_file_id_volatile)
78 {
79         NTSTATUS status;
80         struct smb_request *smbreq;
81         connection_struct *conn = req->tcon->compat_conn;
82         files_struct *fsp;
83
84         DEBUG(10,("smbd_smb2_flush: file_id[0x%016llX]\n",
85                   (unsigned long long)in_file_id_volatile));
86
87         smbreq = smbd_smb2_fake_smb_request(req);
88         if (smbreq == NULL) {
89                 return NT_STATUS_NO_MEMORY;
90         }
91
92         /* If it's an IPC, pass off the pipe handler. */
93         if (IS_IPC(conn)) {
94                 return NT_STATUS_NOT_IMPLEMENTED;
95         }
96
97         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
98         if (fsp == NULL) {
99                 return NT_STATUS_FILE_CLOSED;
100         }
101         if (conn != fsp->conn) {
102                 return NT_STATUS_FILE_CLOSED;
103         }
104         if (req->session->vuid != fsp->vuid) {
105                 return NT_STATUS_FILE_CLOSED;
106         }
107
108         if (!CHECK_WRITE(fsp)) {
109                 return NT_STATUS_ACCESS_DENIED;
110         }
111
112         status = sync_file(conn, fsp, true);
113         if (!NT_STATUS_IS_OK(status)) {
114                 DEBUG(5,("smbd_smb2_flush: sync_file for %s returned %s\n",
115                         fsp->fsp_name, nt_errstr(status)));
116                 return status;
117         }
118
119         return NT_STATUS_OK;
120 }