2 Unix SMB/CIFS implementation.
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/>.
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
)
40 const uint8_t *inbody
;
41 int i
= req
->current_idx
;
42 size_t expected_body_size
= 0x18;
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
) {
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
,
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;
103 NTSTATUS error
; /* transport error */
105 status
= smbd_smb2_oplock_break_recv(subreq
, &out_oplock_level
);
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
,
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
,
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
,
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
);
171 req
= tevent_req_create(mem_ctx
, &state
,
172 struct smbd_smb2_oplock_break_state
);
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] "
181 (unsigned long long)in_file_id_volatile
,
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
);
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
,
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
) ||
217 result
= remove_oplock(fsp
);
218 state
->out_oplock_level
= SMB2_OPLOCK_LEVEL_NONE
;
220 result
= downgrade_oplock(fsp
);
221 state
->out_oplock_level
= SMB2_OPLOCK_LEVEL_II
;
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
)
241 struct smbd_smb2_oplock_break_state
*state
=
243 struct smbd_smb2_oplock_break_state
);
245 if (tevent_req_is_nterror(req
, &status
)) {
246 tevent_req_received(req
);
250 *out_oplock_level
= state
->out_oplock_level
;
252 tevent_req_received(req
);
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
;
268 DEBUG(10,("send_break_message_smb2: sending oplock break "
269 "for file %s, fnum = %d, smb2 level %u\n",
272 (unsigned int)smb2_oplock_level
));
274 status
= smbd_smb2_send_oplock_break(fsp
->conn
->sconn
,
278 if (!NT_STATUS_IS_OK(status
)) {
279 smbd_server_connection_terminate(fsp
->conn
->sconn
,