s3:smbd: split smbd_smb2_flush() into a tevent_req based _send()/_recv() pair
[samba.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 struct tevent_req *smbd_smb2_flush_send(TALLOC_CTX *mem_ctx,
26                                                struct tevent_context *ev,
27                                                struct smbd_smb2_request *smb2req,
28                                                uint64_t in_file_id_volatile);
29 static NTSTATUS smbd_smb2_flush_recv(struct tevent_req *req);
30
31 static void smbd_smb2_request_flush_done(struct tevent_req *subreq);
32 NTSTATUS smbd_smb2_request_process_flush(struct smbd_smb2_request *req)
33 {
34         const uint8_t *inhdr;
35         const uint8_t *inbody;
36         int i = req->current_idx;
37         size_t expected_body_size = 0x18;
38         size_t body_size;
39         uint64_t in_file_id_persistent;
40         uint64_t in_file_id_volatile;
41         struct tevent_req *subreq;
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_file_id_persistent   = BVAL(inbody, 0x08);
56         in_file_id_volatile     = BVAL(inbody, 0x10);
57
58         if (in_file_id_persistent != 0) {
59                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
60         }
61
62         subreq = smbd_smb2_flush_send(req,
63                                       req->conn->smb2.event_ctx,
64                                       req,
65                                       in_file_id_volatile);
66         if (subreq == NULL) {
67                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
68         }
69         tevent_req_set_callback(subreq, smbd_smb2_request_flush_done, req);
70         return NT_STATUS_OK;
71 }
72
73 static void smbd_smb2_request_flush_done(struct tevent_req *subreq)
74 {
75         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
76                                         struct smbd_smb2_request);
77         DATA_BLOB outbody;
78         NTSTATUS status;
79         NTSTATUS error; /* transport error */
80
81         status = smbd_smb2_flush_recv(subreq);
82         TALLOC_FREE(subreq);
83         if (!NT_STATUS_IS_OK(status)) {
84                 error = smbd_smb2_request_error(req, status);
85                 if (!NT_STATUS_IS_OK(error)) {
86                         smbd_server_connection_terminate(req->conn,
87                                                          nt_errstr(error));
88                         return;
89                 }
90                 return;
91         }
92
93         outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
94         if (outbody.data == NULL) {
95                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
96                 if (!NT_STATUS_IS_OK(error)) {
97                         smbd_server_connection_terminate(req->conn,
98                                                          nt_errstr(error));
99                         return;
100                 }
101                 return;
102         }
103
104         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
105         SSVAL(outbody.data, 0x02, 0);           /* reserved */
106
107         error = smbd_smb2_request_done(req, outbody, NULL);
108         if (!NT_STATUS_IS_OK(error)) {
109                 smbd_server_connection_terminate(req->conn,
110                                                  nt_errstr(error));
111                 return;
112         }
113 }
114
115 struct smbd_smb2_flush_state {
116         struct smbd_smb2_request *smb2req;
117 };
118
119 static struct tevent_req *smbd_smb2_flush_send(TALLOC_CTX *mem_ctx,
120                                                struct tevent_context *ev,
121                                                struct smbd_smb2_request *smb2req,
122                                                uint64_t in_file_id_volatile)
123 {
124         struct tevent_req *req;
125         struct smbd_smb2_flush_state *state;
126         NTSTATUS status;
127         struct smb_request *smbreq;
128         files_struct *fsp;
129
130         req = tevent_req_create(mem_ctx, &state,
131                                 struct smbd_smb2_flush_state);
132         if (req == NULL) {
133                 return NULL;
134         }
135         state->smb2req = smb2req;
136
137         DEBUG(10,("smbd_smb2_flush: file_id[0x%016llX]\n",
138                   (unsigned long long)in_file_id_volatile));
139
140         smbreq = smbd_smb2_fake_smb_request(smb2req);
141         if (tevent_req_nomem(smbreq, req)) {
142                 return tevent_req_post(req, ev);
143         }
144
145         if (IS_IPC(smbreq->conn)) {
146                 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
147                 return tevent_req_post(req, ev);
148         }
149
150         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
151         if (fsp == NULL) {
152                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
153                 return tevent_req_post(req, ev);
154         }
155         if (smbreq->conn != fsp->conn) {
156                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
157                 return tevent_req_post(req, ev);
158         }
159         if (smb2req->session->vuid != fsp->vuid) {
160                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
161                 return tevent_req_post(req, ev);
162         }
163
164         if (!CHECK_WRITE(fsp)) {
165                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
166                 return tevent_req_post(req, ev);
167         }
168
169         status = sync_file(smbreq->conn, fsp, true);
170         if (!NT_STATUS_IS_OK(status)) {
171                 DEBUG(5,("smbd_smb2_flush: sync_file for %s returned %s\n",
172                         fsp->fsp_name, nt_errstr(status)));
173                 tevent_req_nterror(req, status);
174                 return tevent_req_post(req, ev);
175         }
176
177         tevent_req_done(req);
178         return tevent_req_post(req, ev);
179 }
180
181 static NTSTATUS smbd_smb2_flush_recv(struct tevent_req *req)
182 {
183         NTSTATUS status;
184
185         if (tevent_req_is_nterror(req, &status)) {
186                 tevent_req_received(req);
187                 return status;
188         }
189
190         tevent_req_received(req);
191         return NT_STATUS_OK;
192 }