s3-rpc_server: Use the new endpoint register functions.
[Samba.git] / source3 / smbd / smb2_break.c
blob79b06b18764a40601b5dba002aae814fe877d203
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 != in_file_id_volatile) {
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 /* Are we awaiting a break message ? */
208 if (fsp->oplock_timeout == NULL) {
209 tevent_req_nterror(req, NT_STATUS_INVALID_OPLOCK_PROTOCOL);
210 return tevent_req_post(req, ev);
213 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
214 (break_to_none)) {
215 result = remove_oplock(fsp);
216 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
217 } else {
218 result = downgrade_oplock(fsp);
219 state->out_oplock_level = SMB2_OPLOCK_LEVEL_II;
222 if (!result) {
223 DEBUG(0, ("smbd_smb2_oplock_break_send: error in removing "
224 "oplock on file %s\n", fsp_str_dbg(fsp)));
225 /* Hmmm. Is this panic justified? */
226 smb_panic("internal tdb error");
229 reply_to_oplock_break_requests(fsp);
231 tevent_req_done(req);
232 return tevent_req_post(req, ev);
235 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
236 uint8_t *out_oplock_level)
238 NTSTATUS status;
239 struct smbd_smb2_oplock_break_state *state =
240 tevent_req_data(req,
241 struct smbd_smb2_oplock_break_state);
243 if (tevent_req_is_nterror(req, &status)) {
244 tevent_req_received(req);
245 return status;
248 *out_oplock_level = state->out_oplock_level;
250 tevent_req_received(req);
251 return NT_STATUS_OK;
254 /*********************************************************
255 Create and send an asynchronous
256 SMB2 OPLOCK_BREAK_NOTIFICATION.
257 *********************************************************/
259 void send_break_message_smb2(files_struct *fsp, int level)
261 uint8_t smb2_oplock_level = (level == OPLOCKLEVEL_II) ?
262 SMB2_OPLOCK_LEVEL_II :
263 SMB2_OPLOCK_LEVEL_NONE;
264 NTSTATUS status;
266 DEBUG(10,("send_break_message_smb2: sending oplock break "
267 "for file %s, fnum = %d, smb2 level %u\n",
268 fsp_str_dbg(fsp),
269 fsp->fnum,
270 (unsigned int)smb2_oplock_level ));
272 status = smbd_smb2_send_oplock_break(fsp->conn->sconn,
273 (uint64_t)fsp->fnum,
274 (uint64_t)fsp->fnum,
275 smb2_oplock_level);
276 if (!NT_STATUS_IS_OK(status)) {
277 smbd_server_connection_terminate(fsp->conn->sconn,
278 nt_errstr(status));