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 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
)
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
);
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
);
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;
96 NTSTATUS error
; /* transport error */
98 status
= smbd_smb2_oplock_break_recv(subreq
, &out_oplock_level
);
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
,
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
,
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
,
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
);
162 req
= tevent_req_create(mem_ctx
, &state
,
163 struct smbd_smb2_oplock_break_state
);
167 state
->smb2req
= smb2req
;
168 state
->out_oplock_level
= SMB2_OPLOCK_LEVEL_NONE
;
170 DEBUG(10,("smbd_smb2_oplock_break_send: %s - %s, "
172 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
),
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 "
182 (unsigned int)in_oplock_level
,
186 if ((fsp
->sent_oplock_break
== BREAK_TO_NONE_SENT
) ||
188 result
= remove_oplock(fsp
);
189 state
->out_oplock_level
= SMB2_OPLOCK_LEVEL_NONE
;
191 result
= downgrade_oplock(fsp
);
192 state
->out_oplock_level
= SMB2_OPLOCK_LEVEL_II
;
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
)
210 struct smbd_smb2_oplock_break_state
*state
=
212 struct smbd_smb2_oplock_break_state
);
214 if (tevent_req_is_nterror(req
, &status
)) {
215 tevent_req_received(req
);
219 *out_oplock_level
= state
->out_oplock_level
;
221 tevent_req_received(req
);
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
;
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
,
252 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_SESSION_DELETED
) ||
256 DEBUG(10,("send_break_message_smb2: skip oplock break "
257 "for file %s, %s, smb2 level %u session %llu not found\n",
260 (unsigned int)smb2_oplock_level
,
261 (unsigned long long)fsp
->vuid
));
265 DEBUG(10,("send_break_message_smb2: sending oplock break "
266 "for file %s, %s, smb2 level %u\n",
269 (unsigned int)smb2_oplock_level
));
271 status
= smbd_smb2_send_oplock_break(xconn
,
276 if (!NT_STATUS_IS_OK(status
)) {
277 smbd_server_connection_terminate(xconn
,