s3:smb2_break: First test for NT_STATUS_INVALID_OPLOCK_PROTOCOL, then for in_oplock_l...
[Samba.git] / source3 / smbd / smb2_break.c
blob449245645d9a0cc0fc25a4181cb25f749af94ecb
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 struct files_struct *in_fsp,
32 uint8_t in_oplock_level);
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 NTSTATUS status;
40 const uint8_t *inbody;
41 uint8_t in_oplock_level;
42 uint64_t in_file_id_persistent;
43 uint64_t in_file_id_volatile;
44 struct files_struct *in_fsp;
45 struct tevent_req *subreq;
47 status = smbd_smb2_request_verify_sizes(req, 0x18);
48 if (!NT_STATUS_IS_OK(status)) {
49 return smbd_smb2_request_error(req, status);
51 inbody = SMBD_SMB2_IN_BODY_PTR(req);
53 in_oplock_level = CVAL(inbody, 0x02);
55 /* 0x03 1 bytes reserved */
56 /* 0x04 4 bytes reserved */
57 in_file_id_persistent = BVAL(inbody, 0x08);
58 in_file_id_volatile = BVAL(inbody, 0x10);
60 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
61 if (in_fsp == NULL) {
62 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
65 /* Are we awaiting a break message ? */
66 if (in_fsp->oplock_timeout == NULL) {
67 return smbd_smb2_request_error(
68 req, NT_STATUS_INVALID_OPLOCK_PROTOCOL);
71 if (in_oplock_level != SMB2_OPLOCK_LEVEL_NONE &&
72 in_oplock_level != SMB2_OPLOCK_LEVEL_II) {
73 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
76 subreq = smbd_smb2_oplock_break_send(req, req->sconn->ev_ctx,
77 req, in_fsp, in_oplock_level);
78 if (subreq == NULL) {
79 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
81 tevent_req_set_callback(subreq, smbd_smb2_request_oplock_break_done, req);
83 return smbd_smb2_request_pending_queue(req, subreq, 500);
86 static void smbd_smb2_request_oplock_break_done(struct tevent_req *subreq)
88 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
89 struct smbd_smb2_request);
90 const uint8_t *inbody;
91 uint64_t in_file_id_persistent;
92 uint64_t in_file_id_volatile;
93 uint8_t out_oplock_level = 0;
94 DATA_BLOB outbody;
95 NTSTATUS status;
96 NTSTATUS error; /* transport error */
98 status = smbd_smb2_oplock_break_recv(subreq, &out_oplock_level);
99 TALLOC_FREE(subreq);
100 if (!NT_STATUS_IS_OK(status)) {
101 error = smbd_smb2_request_error(req, status);
102 if (!NT_STATUS_IS_OK(error)) {
103 smbd_server_connection_terminate(req->xconn,
104 nt_errstr(error));
105 return;
107 return;
110 inbody = SMBD_SMB2_IN_BODY_PTR(req);
112 in_file_id_persistent = BVAL(inbody, 0x08);
113 in_file_id_volatile = BVAL(inbody, 0x10);
115 outbody = smbd_smb2_generate_outbody(req, 0x18);
116 if (outbody.data == NULL) {
117 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
118 if (!NT_STATUS_IS_OK(error)) {
119 smbd_server_connection_terminate(req->xconn,
120 nt_errstr(error));
121 return;
123 return;
126 SSVAL(outbody.data, 0x00, 0x18); /* struct size */
127 SCVAL(outbody.data, 0x02,
128 out_oplock_level); /* SMB2 oplock level */
129 SCVAL(outbody.data, 0x03, 0); /* reserved */
130 SIVAL(outbody.data, 0x04, 0); /* reserved */
131 SBVAL(outbody.data, 0x08,
132 in_file_id_persistent); /* file id (persistent) */
133 SBVAL(outbody.data, 0x10,
134 in_file_id_volatile); /* file id (volatile) */
136 error = smbd_smb2_request_done(req, outbody, NULL);
137 if (!NT_STATUS_IS_OK(error)) {
138 smbd_server_connection_terminate(req->xconn,
139 nt_errstr(error));
140 return;
144 struct smbd_smb2_oplock_break_state {
145 struct smbd_smb2_request *smb2req;
146 uint8_t out_oplock_level; /* SMB2 oplock level. */
149 static struct tevent_req *smbd_smb2_oplock_break_send(TALLOC_CTX *mem_ctx,
150 struct tevent_context *ev,
151 struct smbd_smb2_request *smb2req,
152 struct files_struct *fsp,
153 uint8_t in_oplock_level)
155 struct tevent_req *req;
156 struct smbd_smb2_oplock_break_state *state;
157 struct smb_request *smbreq;
158 int oplocklevel = map_smb2_oplock_levels_to_samba(in_oplock_level);
159 bool break_to_none = (oplocklevel == NO_OPLOCK);
160 bool result;
162 req = tevent_req_create(mem_ctx, &state,
163 struct smbd_smb2_oplock_break_state);
164 if (req == NULL) {
165 return NULL;
167 state->smb2req = smb2req;
168 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
170 DEBUG(10,("smbd_smb2_oplock_break_send: %s - %s, "
171 "samba level %d\n",
172 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
173 oplocklevel));
175 smbreq = smbd_smb2_fake_smb_request(smb2req);
176 if (tevent_req_nomem(smbreq, req)) {
177 return tevent_req_post(req, ev);
180 DEBUG(5,("smbd_smb2_oplock_break_send: got SMB2 oplock break (%u) from client "
181 "for file %s, %s\n",
182 (unsigned int)in_oplock_level,
183 fsp_str_dbg(fsp),
184 fsp_fnum_dbg(fsp)));
186 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
187 (break_to_none)) {
188 result = remove_oplock(fsp);
189 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
190 } else {
191 result = downgrade_oplock(fsp);
192 state->out_oplock_level = SMB2_OPLOCK_LEVEL_II;
195 if (!result) {
196 DEBUG(0, ("smbd_smb2_oplock_break_send: error in removing "
197 "oplock on file %s\n", fsp_str_dbg(fsp)));
198 /* Hmmm. Is this panic justified? */
199 smb_panic("internal tdb error");
202 tevent_req_done(req);
203 return tevent_req_post(req, ev);
206 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
207 uint8_t *out_oplock_level)
209 NTSTATUS status;
210 struct smbd_smb2_oplock_break_state *state =
211 tevent_req_data(req,
212 struct smbd_smb2_oplock_break_state);
214 if (tevent_req_is_nterror(req, &status)) {
215 tevent_req_received(req);
216 return status;
219 *out_oplock_level = state->out_oplock_level;
221 tevent_req_received(req);
222 return NT_STATUS_OK;
225 /*********************************************************
226 Create and send an asynchronous
227 SMB2 OPLOCK_BREAK_NOTIFICATION.
228 *********************************************************/
230 void send_break_message_smb2(files_struct *fsp, int level)
232 uint8_t smb2_oplock_level = (level == OPLOCKLEVEL_II) ?
233 SMB2_OPLOCK_LEVEL_II :
234 SMB2_OPLOCK_LEVEL_NONE;
235 NTSTATUS status;
236 struct smbXsrv_connection *xconn = NULL;
237 struct smbXsrv_session *session = NULL;
238 struct timeval tv = timeval_current();
239 NTTIME now = timeval_to_nttime(&tv);
242 * TODO: in future we should have a better algorithm
243 * to find the correct connection for a break message.
244 * Then we also need some retries if a channel gets disconnected.
246 xconn = fsp->conn->sconn->client->connections;
248 status = smb2srv_session_lookup(xconn,
249 fsp->vuid,
250 now,
251 &session);
252 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED) ||
253 (session == NULL))
256 DEBUG(10,("send_break_message_smb2: skip oplock break "
257 "for file %s, %s, smb2 level %u session %llu not found\n",
258 fsp_str_dbg(fsp),
259 fsp_fnum_dbg(fsp),
260 (unsigned int)smb2_oplock_level,
261 (unsigned long long)fsp->vuid));
262 return;
265 DEBUG(10,("send_break_message_smb2: sending oplock break "
266 "for file %s, %s, smb2 level %u\n",
267 fsp_str_dbg(fsp),
268 fsp_fnum_dbg(fsp),
269 (unsigned int)smb2_oplock_level ));
271 status = smbd_smb2_send_oplock_break(xconn,
272 session,
273 fsp->conn->tcon,
274 fsp->op,
275 smb2_oplock_level);
276 if (!NT_STATUS_IS_OK(status)) {
277 smbd_server_connection_terminate(xconn,
278 nt_errstr(status));
279 return;