s3-tevent: only include ../lib/util/tevent wrappers where needed.
[Samba.git] / source3 / smbd / smb2_break.c
blob5d5ab4139d90023073c10bad881a3c09d352af47
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
6 Copyright (C) Jeremy Allison 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "../lib/util/tevent_ntstatus.h"
28 static struct tevent_req *smbd_smb2_oplock_break_send(TALLOC_CTX *mem_ctx,
29 struct tevent_context *ev,
30 struct smbd_smb2_request *smb2req,
31 uint8_t in_oplock_level,
32 uint64_t in_file_id_volatile);
33 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
34 uint8_t *out_oplock_level);
36 static void smbd_smb2_request_oplock_break_done(struct tevent_req *subreq);
37 NTSTATUS smbd_smb2_request_process_break(struct smbd_smb2_request *req)
39 const uint8_t *inhdr;
40 const uint8_t *inbody;
41 int i = req->current_idx;
42 size_t expected_body_size = 0x18;
43 size_t body_size;
44 uint8_t in_oplock_level;
45 uint64_t in_file_id_persistent;
46 uint64_t in_file_id_volatile;
47 struct tevent_req *subreq;
49 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
50 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
51 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
54 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
56 body_size = SVAL(inbody, 0x00);
57 if (body_size != expected_body_size) {
58 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
61 in_oplock_level = CVAL(inbody, 0x02);
63 if (in_oplock_level != SMB2_OPLOCK_LEVEL_NONE &&
64 in_oplock_level != SMB2_OPLOCK_LEVEL_II) {
65 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
68 /* 0x03 1 bytes reserved */
69 /* 0x04 4 bytes reserved */
70 in_file_id_persistent = BVAL(inbody, 0x08);
71 in_file_id_volatile = BVAL(inbody, 0x10);
73 if (req->compat_chain_fsp) {
74 /* skip check */
75 } else if (in_file_id_persistent != in_file_id_volatile) {
76 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
79 subreq = smbd_smb2_oplock_break_send(req,
80 req->sconn->smb2.event_ctx,
81 req,
82 in_oplock_level,
83 in_file_id_volatile);
84 if (subreq == NULL) {
85 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
87 tevent_req_set_callback(subreq, smbd_smb2_request_oplock_break_done, req);
89 return smbd_smb2_request_pending_queue(req, subreq);
92 static void smbd_smb2_request_oplock_break_done(struct tevent_req *subreq)
94 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
95 struct smbd_smb2_request);
96 const uint8_t *inbody;
97 int i = req->current_idx;
98 uint64_t in_file_id_persistent;
99 uint64_t in_file_id_volatile;
100 uint8_t out_oplock_level = 0;
101 DATA_BLOB outbody;
102 NTSTATUS status;
103 NTSTATUS error; /* transport error */
105 status = smbd_smb2_oplock_break_recv(subreq, &out_oplock_level);
106 TALLOC_FREE(subreq);
107 if (!NT_STATUS_IS_OK(status)) {
108 error = smbd_smb2_request_error(req, status);
109 if (!NT_STATUS_IS_OK(error)) {
110 smbd_server_connection_terminate(req->sconn,
111 nt_errstr(error));
112 return;
114 return;
117 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
119 in_file_id_persistent = BVAL(inbody, 0x08);
120 in_file_id_volatile = BVAL(inbody, 0x10);
122 outbody = data_blob_talloc(req->out.vector, NULL, 0x18);
123 if (outbody.data == NULL) {
124 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
125 if (!NT_STATUS_IS_OK(error)) {
126 smbd_server_connection_terminate(req->sconn,
127 nt_errstr(error));
128 return;
130 return;
133 SSVAL(outbody.data, 0x00, 0x18); /* struct size */
134 SCVAL(outbody.data, 0x02,
135 out_oplock_level); /* SMB2 oplock level */
136 SCVAL(outbody.data, 0x03, 0); /* reserved */
137 SIVAL(outbody.data, 0x04, 0); /* reserved */
138 SBVAL(outbody.data, 0x08,
139 in_file_id_persistent); /* file id (persistent) */
140 SBVAL(outbody.data, 0x10,
141 in_file_id_volatile); /* file id (volatile) */
143 error = smbd_smb2_request_done(req, outbody, NULL);
144 if (!NT_STATUS_IS_OK(error)) {
145 smbd_server_connection_terminate(req->sconn,
146 nt_errstr(error));
147 return;
151 struct smbd_smb2_oplock_break_state {
152 struct smbd_smb2_request *smb2req;
153 uint8_t out_oplock_level; /* SMB2 oplock level. */
156 static struct tevent_req *smbd_smb2_oplock_break_send(TALLOC_CTX *mem_ctx,
157 struct tevent_context *ev,
158 struct smbd_smb2_request *smb2req,
159 uint8_t in_oplock_level,
160 uint64_t in_file_id_volatile)
162 struct tevent_req *req;
163 struct smbd_smb2_oplock_break_state *state;
164 struct smb_request *smbreq;
165 connection_struct *conn = smb2req->tcon->compat_conn;
166 files_struct *fsp = NULL;
167 int oplocklevel = map_smb2_oplock_levels_to_samba(in_oplock_level);
168 bool break_to_none = (oplocklevel == NO_OPLOCK);
169 bool result;
171 req = tevent_req_create(mem_ctx, &state,
172 struct smbd_smb2_oplock_break_state);
173 if (req == NULL) {
174 return NULL;
176 state->smb2req = smb2req;
177 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
179 DEBUG(10,("smbd_smb2_oplock_break_send: file_id[0x%016llX] "
180 "samba level %d\n",
181 (unsigned long long)in_file_id_volatile,
182 oplocklevel));
184 smbreq = smbd_smb2_fake_smb_request(smb2req);
185 if (tevent_req_nomem(smbreq, req)) {
186 return tevent_req_post(req, ev);
189 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
190 if (fsp == NULL) {
191 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
192 return tevent_req_post(req, ev);
194 if (conn != fsp->conn) {
195 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
196 return tevent_req_post(req, ev);
198 if (smb2req->session->vuid != fsp->vuid) {
199 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
200 return tevent_req_post(req, ev);
203 DEBUG(5,("smbd_smb2_oplock_break_send: got SMB2 oplock break (%u) from client "
204 "for file %s fnum = %d\n",
205 (unsigned int)in_oplock_level,
206 fsp_str_dbg(fsp),
207 fsp->fnum ));
209 /* Are we awaiting a break message ? */
210 if (fsp->oplock_timeout == NULL) {
211 tevent_req_nterror(req, NT_STATUS_INVALID_OPLOCK_PROTOCOL);
212 return tevent_req_post(req, ev);
215 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
216 (break_to_none)) {
217 result = remove_oplock(fsp);
218 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
219 } else {
220 result = downgrade_oplock(fsp);
221 state->out_oplock_level = SMB2_OPLOCK_LEVEL_II;
224 if (!result) {
225 DEBUG(0, ("smbd_smb2_oplock_break_send: error in removing "
226 "oplock on file %s\n", fsp_str_dbg(fsp)));
227 /* Hmmm. Is this panic justified? */
228 smb_panic("internal tdb error");
231 reply_to_oplock_break_requests(fsp);
233 tevent_req_done(req);
234 return tevent_req_post(req, ev);
237 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
238 uint8_t *out_oplock_level)
240 NTSTATUS status;
241 struct smbd_smb2_oplock_break_state *state =
242 tevent_req_data(req,
243 struct smbd_smb2_oplock_break_state);
245 if (tevent_req_is_nterror(req, &status)) {
246 tevent_req_received(req);
247 return status;
250 *out_oplock_level = state->out_oplock_level;
252 tevent_req_received(req);
253 return NT_STATUS_OK;
256 /*********************************************************
257 Create and send an asynchronous
258 SMB2 OPLOCK_BREAK_NOTIFICATION.
259 *********************************************************/
261 void send_break_message_smb2(files_struct *fsp, int level)
263 uint8_t smb2_oplock_level = (level == OPLOCKLEVEL_II) ?
264 SMB2_OPLOCK_LEVEL_II :
265 SMB2_OPLOCK_LEVEL_NONE;
266 NTSTATUS status;
268 DEBUG(10,("send_break_message_smb2: sending oplock break "
269 "for file %s, fnum = %d, smb2 level %u\n",
270 fsp_str_dbg(fsp),
271 fsp->fnum,
272 (unsigned int)smb2_oplock_level ));
274 status = smbd_smb2_send_oplock_break(fsp->conn->sconn,
275 (uint64_t)fsp->fnum,
276 (uint64_t)fsp->fnum,
277 smb2_oplock_level);
278 if (!NT_STATUS_IS_OK(status)) {
279 smbd_server_connection_terminate(fsp->conn->sconn,
280 nt_errstr(status));