SMB2 always have level2 oplock capability. Correct mapping from break messages to...
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_break.c
blobbd0fc566f03f594b469f63079c596b746981c011
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/globals.h"
24 #include "../libcli/smb/smb_common.h"
26 static struct tevent_req *smbd_smb2_oplock_break_send(TALLOC_CTX *mem_ctx,
27 struct tevent_context *ev,
28 struct smbd_smb2_request *smb2req,
29 uint8_t in_oplock_level,
30 uint64_t in_file_id_volatile);
31 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
32 uint8_t *out_oplock_level);
34 static void smbd_smb2_request_oplock_break_done(struct tevent_req *subreq);
35 NTSTATUS smbd_smb2_request_process_break(struct smbd_smb2_request *req)
37 const uint8_t *inhdr;
38 const uint8_t *inbody;
39 int i = req->current_idx;
40 size_t expected_body_size = 0x18;
41 size_t body_size;
42 uint8_t in_oplock_level;
43 uint64_t in_file_id_persistent;
44 uint64_t in_file_id_volatile;
45 struct tevent_req *subreq;
47 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
48 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
49 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
52 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
54 body_size = SVAL(inbody, 0x00);
55 if (body_size != expected_body_size) {
56 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
59 in_oplock_level = CVAL(inbody, 0x02);
61 if (in_oplock_level != SMB2_OPLOCK_LEVEL_NONE &&
62 in_oplock_level != SMB2_OPLOCK_LEVEL_II) {
63 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
66 /* 0x03 1 bytes reserved */
67 /* 0x04 4 bytes reserved */
68 in_file_id_persistent = BVAL(inbody, 0x08);
69 in_file_id_volatile = BVAL(inbody, 0x10);
71 if (req->compat_chain_fsp) {
72 /* skip check */
73 } else if (in_file_id_persistent != 0) {
74 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
77 subreq = smbd_smb2_oplock_break_send(req,
78 req->sconn->smb2.event_ctx,
79 req,
80 in_oplock_level,
81 in_file_id_volatile);
82 if (subreq == NULL) {
83 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
85 tevent_req_set_callback(subreq, smbd_smb2_request_oplock_break_done, req);
87 return smbd_smb2_request_pending_queue(req, subreq);
90 static void smbd_smb2_request_oplock_break_done(struct tevent_req *subreq)
92 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
93 struct smbd_smb2_request);
94 const uint8_t *inbody;
95 int i = req->current_idx;
96 uint64_t in_file_id_persistent;
97 uint64_t in_file_id_volatile;
98 uint8_t out_oplock_level = 0;
99 DATA_BLOB outbody;
100 NTSTATUS status;
101 NTSTATUS error; /* transport error */
103 status = smbd_smb2_oplock_break_recv(subreq, &out_oplock_level);
104 TALLOC_FREE(subreq);
105 if (!NT_STATUS_IS_OK(status)) {
106 error = smbd_smb2_request_error(req, status);
107 if (!NT_STATUS_IS_OK(error)) {
108 smbd_server_connection_terminate(req->sconn,
109 nt_errstr(error));
110 return;
112 return;
115 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
117 in_file_id_persistent = BVAL(inbody, 0x08);
118 in_file_id_volatile = BVAL(inbody, 0x10);
120 outbody = data_blob_talloc(req->out.vector, NULL, 0x18);
121 if (outbody.data == NULL) {
122 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
123 if (!NT_STATUS_IS_OK(error)) {
124 smbd_server_connection_terminate(req->sconn,
125 nt_errstr(error));
126 return;
128 return;
131 SSVAL(outbody.data, 0x00, 0x18); /* struct size */
132 SCVAL(outbody.data, 0x02,
133 out_oplock_level); /* SMB2 oplock level */
134 SCVAL(outbody.data, 0x03, 0); /* reserved */
135 SIVAL(outbody.data, 0x04, 0); /* reserved */
136 SBVAL(outbody.data, 0x08,
137 in_file_id_persistent); /* file id (persistent) */
138 SBVAL(outbody.data, 0x10,
139 in_file_id_volatile); /* file id (volatile) */
141 error = smbd_smb2_request_done(req, outbody, NULL);
142 if (!NT_STATUS_IS_OK(error)) {
143 smbd_server_connection_terminate(req->sconn,
144 nt_errstr(error));
145 return;
149 struct smbd_smb2_oplock_break_state {
150 struct smbd_smb2_request *smb2req;
151 uint8_t out_oplock_level; /* SMB2 oplock level. */
154 static struct tevent_req *smbd_smb2_oplock_break_send(TALLOC_CTX *mem_ctx,
155 struct tevent_context *ev,
156 struct smbd_smb2_request *smb2req,
157 uint8_t in_oplock_level,
158 uint64_t in_file_id_volatile)
160 struct tevent_req *req;
161 struct smbd_smb2_oplock_break_state *state;
162 struct smb_request *smbreq;
163 connection_struct *conn = smb2req->tcon->compat_conn;
164 files_struct *fsp = NULL;
165 int oplocklevel = map_smb2_oplock_levels_to_samba(in_oplock_level);
166 bool break_to_none = (oplocklevel == NO_OPLOCK);
167 bool result;
169 req = tevent_req_create(mem_ctx, &state,
170 struct smbd_smb2_oplock_break_state);
171 if (req == NULL) {
172 return NULL;
174 state->smb2req = smb2req;
175 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
177 DEBUG(10,("smbd_smb2_oplock_break_send: file_id[0x%016llX] "
178 "samba level %d\n",
179 (unsigned long long)in_file_id_volatile,
180 oplocklevel));
182 smbreq = smbd_smb2_fake_smb_request(smb2req);
183 if (tevent_req_nomem(smbreq, req)) {
184 return tevent_req_post(req, ev);
187 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
188 if (fsp == NULL) {
189 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
190 return tevent_req_post(req, ev);
192 if (conn != fsp->conn) {
193 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
194 return tevent_req_post(req, ev);
196 if (smb2req->session->vuid != fsp->vuid) {
197 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
198 return tevent_req_post(req, ev);
201 DEBUG(5,("smbd_smb2_oplock_break_send: got SMB2 oplock break (%u) from client "
202 "for file %s fnum = %d\n",
203 (unsigned int)in_oplock_level,
204 fsp_str_dbg(fsp),
205 fsp->fnum ));
207 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
208 (break_to_none)) {
209 result = remove_oplock(fsp);
210 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
211 } else {
212 result = downgrade_oplock(fsp);
213 state->out_oplock_level = SMB2_OPLOCK_LEVEL_II;
216 if (!result) {
217 DEBUG(0, ("smbd_smb2_oplock_break_send: error in removing "
218 "oplock on file %s\n", fsp_str_dbg(fsp)));
219 /* Hmmm. Is this panic justified? */
220 smb_panic("internal tdb error");
223 reply_to_oplock_break_requests(fsp);
225 tevent_req_done(req);
226 return tevent_req_post(req, ev);
229 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
230 uint8_t *out_oplock_level)
232 NTSTATUS status;
233 struct smbd_smb2_oplock_break_state *state =
234 tevent_req_data(req,
235 struct smbd_smb2_oplock_break_state);
237 if (tevent_req_is_nterror(req, &status)) {
238 tevent_req_received(req);
239 return status;
242 *out_oplock_level = state->out_oplock_level;
244 tevent_req_received(req);
245 return NT_STATUS_OK;
248 /*********************************************************
249 Create and send an asynchronous
250 SMB2 OPLOCK_BREAK_NOTIFICATION.
251 *********************************************************/
253 void send_break_message_smb2(files_struct *fsp, int level)
255 uint8_t smb2_oplock_level = (level == OPLOCKLEVEL_II) ?
256 SMB2_OPLOCK_LEVEL_II :
257 SMB2_OPLOCK_LEVEL_NONE;
258 NTSTATUS status;
260 DEBUG(10,("send_break_message_smb2: sending oplock break "
261 "for file %s, fnum = %d, smb2 level %u\n",
262 fsp_str_dbg(fsp),
263 fsp->fnum,
264 (unsigned int)smb2_oplock_level ));
266 status = smbd_smb2_send_oplock_break(fsp->conn->sconn,
267 (uint64_t)fsp->fnum,
268 smb2_oplock_level);
269 if (!NT_STATUS_IS_OK(status)) {
270 smbd_server_connection_terminate(fsp->conn->sconn,
271 nt_errstr(status));