s3: include smbd/smbd.h where needed.
[Samba.git] / source3 / smbd / smb2_break.c
blob925384c3bfead0b1f5d398a95b6cc345c7be11ca
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"
27 static struct tevent_req *smbd_smb2_oplock_break_send(TALLOC_CTX *mem_ctx,
28 struct tevent_context *ev,
29 struct smbd_smb2_request *smb2req,
30 uint8_t in_oplock_level,
31 uint64_t in_file_id_volatile);
32 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
33 uint8_t *out_oplock_level);
35 static void smbd_smb2_request_oplock_break_done(struct tevent_req *subreq);
36 NTSTATUS smbd_smb2_request_process_break(struct smbd_smb2_request *req)
38 const uint8_t *inhdr;
39 const uint8_t *inbody;
40 int i = req->current_idx;
41 size_t expected_body_size = 0x18;
42 size_t body_size;
43 uint8_t in_oplock_level;
44 uint64_t in_file_id_persistent;
45 uint64_t in_file_id_volatile;
46 struct tevent_req *subreq;
48 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
49 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
50 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
53 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
55 body_size = SVAL(inbody, 0x00);
56 if (body_size != expected_body_size) {
57 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
60 in_oplock_level = CVAL(inbody, 0x02);
62 if (in_oplock_level != SMB2_OPLOCK_LEVEL_NONE &&
63 in_oplock_level != SMB2_OPLOCK_LEVEL_II) {
64 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
67 /* 0x03 1 bytes reserved */
68 /* 0x04 4 bytes reserved */
69 in_file_id_persistent = BVAL(inbody, 0x08);
70 in_file_id_volatile = BVAL(inbody, 0x10);
72 if (req->compat_chain_fsp) {
73 /* skip check */
74 } else if (in_file_id_persistent != in_file_id_volatile) {
75 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
78 subreq = smbd_smb2_oplock_break_send(req,
79 req->sconn->smb2.event_ctx,
80 req,
81 in_oplock_level,
82 in_file_id_volatile);
83 if (subreq == NULL) {
84 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
86 tevent_req_set_callback(subreq, smbd_smb2_request_oplock_break_done, req);
88 return smbd_smb2_request_pending_queue(req, subreq);
91 static void smbd_smb2_request_oplock_break_done(struct tevent_req *subreq)
93 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
94 struct smbd_smb2_request);
95 const uint8_t *inbody;
96 int i = req->current_idx;
97 uint64_t in_file_id_persistent;
98 uint64_t in_file_id_volatile;
99 uint8_t out_oplock_level = 0;
100 DATA_BLOB outbody;
101 NTSTATUS status;
102 NTSTATUS error; /* transport error */
104 status = smbd_smb2_oplock_break_recv(subreq, &out_oplock_level);
105 TALLOC_FREE(subreq);
106 if (!NT_STATUS_IS_OK(status)) {
107 error = smbd_smb2_request_error(req, status);
108 if (!NT_STATUS_IS_OK(error)) {
109 smbd_server_connection_terminate(req->sconn,
110 nt_errstr(error));
111 return;
113 return;
116 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
118 in_file_id_persistent = BVAL(inbody, 0x08);
119 in_file_id_volatile = BVAL(inbody, 0x10);
121 outbody = data_blob_talloc(req->out.vector, NULL, 0x18);
122 if (outbody.data == NULL) {
123 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
124 if (!NT_STATUS_IS_OK(error)) {
125 smbd_server_connection_terminate(req->sconn,
126 nt_errstr(error));
127 return;
129 return;
132 SSVAL(outbody.data, 0x00, 0x18); /* struct size */
133 SCVAL(outbody.data, 0x02,
134 out_oplock_level); /* SMB2 oplock level */
135 SCVAL(outbody.data, 0x03, 0); /* reserved */
136 SIVAL(outbody.data, 0x04, 0); /* reserved */
137 SBVAL(outbody.data, 0x08,
138 in_file_id_persistent); /* file id (persistent) */
139 SBVAL(outbody.data, 0x10,
140 in_file_id_volatile); /* file id (volatile) */
142 error = smbd_smb2_request_done(req, outbody, NULL);
143 if (!NT_STATUS_IS_OK(error)) {
144 smbd_server_connection_terminate(req->sconn,
145 nt_errstr(error));
146 return;
150 struct smbd_smb2_oplock_break_state {
151 struct smbd_smb2_request *smb2req;
152 uint8_t out_oplock_level; /* SMB2 oplock level. */
155 static struct tevent_req *smbd_smb2_oplock_break_send(TALLOC_CTX *mem_ctx,
156 struct tevent_context *ev,
157 struct smbd_smb2_request *smb2req,
158 uint8_t in_oplock_level,
159 uint64_t in_file_id_volatile)
161 struct tevent_req *req;
162 struct smbd_smb2_oplock_break_state *state;
163 struct smb_request *smbreq;
164 connection_struct *conn = smb2req->tcon->compat_conn;
165 files_struct *fsp = NULL;
166 int oplocklevel = map_smb2_oplock_levels_to_samba(in_oplock_level);
167 bool break_to_none = (oplocklevel == NO_OPLOCK);
168 bool result;
170 req = tevent_req_create(mem_ctx, &state,
171 struct smbd_smb2_oplock_break_state);
172 if (req == NULL) {
173 return NULL;
175 state->smb2req = smb2req;
176 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
178 DEBUG(10,("smbd_smb2_oplock_break_send: file_id[0x%016llX] "
179 "samba level %d\n",
180 (unsigned long long)in_file_id_volatile,
181 oplocklevel));
183 smbreq = smbd_smb2_fake_smb_request(smb2req);
184 if (tevent_req_nomem(smbreq, req)) {
185 return tevent_req_post(req, ev);
188 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
189 if (fsp == NULL) {
190 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
191 return tevent_req_post(req, ev);
193 if (conn != fsp->conn) {
194 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
195 return tevent_req_post(req, ev);
197 if (smb2req->session->vuid != fsp->vuid) {
198 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
199 return tevent_req_post(req, ev);
202 DEBUG(5,("smbd_smb2_oplock_break_send: got SMB2 oplock break (%u) from client "
203 "for file %s fnum = %d\n",
204 (unsigned int)in_oplock_level,
205 fsp_str_dbg(fsp),
206 fsp->fnum ));
208 /* Are we awaiting a break message ? */
209 if (fsp->oplock_timeout == NULL) {
210 tevent_req_nterror(req, NT_STATUS_INVALID_OPLOCK_PROTOCOL);
211 return tevent_req_post(req, ev);
214 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
215 (break_to_none)) {
216 result = remove_oplock(fsp);
217 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
218 } else {
219 result = downgrade_oplock(fsp);
220 state->out_oplock_level = SMB2_OPLOCK_LEVEL_II;
223 if (!result) {
224 DEBUG(0, ("smbd_smb2_oplock_break_send: error in removing "
225 "oplock on file %s\n", fsp_str_dbg(fsp)));
226 /* Hmmm. Is this panic justified? */
227 smb_panic("internal tdb error");
230 reply_to_oplock_break_requests(fsp);
232 tevent_req_done(req);
233 return tevent_req_post(req, ev);
236 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
237 uint8_t *out_oplock_level)
239 NTSTATUS status;
240 struct smbd_smb2_oplock_break_state *state =
241 tevent_req_data(req,
242 struct smbd_smb2_oplock_break_state);
244 if (tevent_req_is_nterror(req, &status)) {
245 tevent_req_received(req);
246 return status;
249 *out_oplock_level = state->out_oplock_level;
251 tevent_req_received(req);
252 return NT_STATUS_OK;
255 /*********************************************************
256 Create and send an asynchronous
257 SMB2 OPLOCK_BREAK_NOTIFICATION.
258 *********************************************************/
260 void send_break_message_smb2(files_struct *fsp, int level)
262 uint8_t smb2_oplock_level = (level == OPLOCKLEVEL_II) ?
263 SMB2_OPLOCK_LEVEL_II :
264 SMB2_OPLOCK_LEVEL_NONE;
265 NTSTATUS status;
267 DEBUG(10,("send_break_message_smb2: sending oplock break "
268 "for file %s, fnum = %d, smb2 level %u\n",
269 fsp_str_dbg(fsp),
270 fsp->fnum,
271 (unsigned int)smb2_oplock_level ));
273 status = smbd_smb2_send_oplock_break(fsp->conn->sconn,
274 (uint64_t)fsp->fnum,
275 (uint64_t)fsp->fnum,
276 smb2_oplock_level);
277 if (!NT_STATUS_IS_OK(status)) {
278 smbd_server_connection_terminate(fsp->conn->sconn,
279 nt_errstr(status));