s4 dns: Only forward for zones we don't own
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_break.c
blob310b24bb393343e55639adb13c798c2c58df5236
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 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)
39 NTSTATUS status;
40 const uint8_t *inbody;
41 int i = req->current_idx;
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 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 = (const uint8_t *)req->in.vector[i+1].iov_base;
53 in_oplock_level = CVAL(inbody, 0x02);
55 if (in_oplock_level != SMB2_OPLOCK_LEVEL_NONE &&
56 in_oplock_level != SMB2_OPLOCK_LEVEL_II) {
57 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
60 /* 0x03 1 bytes reserved */
61 /* 0x04 4 bytes reserved */
62 in_file_id_persistent = BVAL(inbody, 0x08);
63 in_file_id_volatile = BVAL(inbody, 0x10);
65 if (req->compat_chain_fsp) {
66 /* skip check */
67 } else if (in_file_id_persistent != in_file_id_volatile) {
68 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
71 subreq = smbd_smb2_oplock_break_send(req,
72 req->sconn->ev_ctx,
73 req,
74 in_oplock_level,
75 in_file_id_volatile);
76 if (subreq == NULL) {
77 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
79 tevent_req_set_callback(subreq, smbd_smb2_request_oplock_break_done, req);
81 return smbd_smb2_request_pending_queue(req, subreq, 500);
84 static void smbd_smb2_request_oplock_break_done(struct tevent_req *subreq)
86 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
87 struct smbd_smb2_request);
88 const uint8_t *inbody;
89 int i = req->current_idx;
90 uint64_t in_file_id_persistent;
91 uint64_t in_file_id_volatile;
92 uint8_t out_oplock_level = 0;
93 DATA_BLOB outbody;
94 NTSTATUS status;
95 NTSTATUS error; /* transport error */
97 status = smbd_smb2_oplock_break_recv(subreq, &out_oplock_level);
98 TALLOC_FREE(subreq);
99 if (!NT_STATUS_IS_OK(status)) {
100 error = smbd_smb2_request_error(req, status);
101 if (!NT_STATUS_IS_OK(error)) {
102 smbd_server_connection_terminate(req->sconn,
103 nt_errstr(error));
104 return;
106 return;
109 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
111 in_file_id_persistent = BVAL(inbody, 0x08);
112 in_file_id_volatile = BVAL(inbody, 0x10);
114 outbody = data_blob_talloc(req->out.vector, NULL, 0x18);
115 if (outbody.data == NULL) {
116 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
117 if (!NT_STATUS_IS_OK(error)) {
118 smbd_server_connection_terminate(req->sconn,
119 nt_errstr(error));
120 return;
122 return;
125 SSVAL(outbody.data, 0x00, 0x18); /* struct size */
126 SCVAL(outbody.data, 0x02,
127 out_oplock_level); /* SMB2 oplock level */
128 SCVAL(outbody.data, 0x03, 0); /* reserved */
129 SIVAL(outbody.data, 0x04, 0); /* reserved */
130 SBVAL(outbody.data, 0x08,
131 in_file_id_persistent); /* file id (persistent) */
132 SBVAL(outbody.data, 0x10,
133 in_file_id_volatile); /* file id (volatile) */
135 error = smbd_smb2_request_done(req, outbody, NULL);
136 if (!NT_STATUS_IS_OK(error)) {
137 smbd_server_connection_terminate(req->sconn,
138 nt_errstr(error));
139 return;
143 struct smbd_smb2_oplock_break_state {
144 struct smbd_smb2_request *smb2req;
145 uint8_t out_oplock_level; /* SMB2 oplock level. */
148 static struct tevent_req *smbd_smb2_oplock_break_send(TALLOC_CTX *mem_ctx,
149 struct tevent_context *ev,
150 struct smbd_smb2_request *smb2req,
151 uint8_t in_oplock_level,
152 uint64_t in_file_id_volatile)
154 struct tevent_req *req;
155 struct smbd_smb2_oplock_break_state *state;
156 struct smb_request *smbreq;
157 connection_struct *conn = smb2req->tcon->compat_conn;
158 files_struct *fsp = NULL;
159 int oplocklevel = map_smb2_oplock_levels_to_samba(in_oplock_level);
160 bool break_to_none = (oplocklevel == NO_OPLOCK);
161 bool result;
163 req = tevent_req_create(mem_ctx, &state,
164 struct smbd_smb2_oplock_break_state);
165 if (req == NULL) {
166 return NULL;
168 state->smb2req = smb2req;
169 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
171 DEBUG(10,("smbd_smb2_oplock_break_send: file_id[0x%016llX] "
172 "samba level %d\n",
173 (unsigned long long)in_file_id_volatile,
174 oplocklevel));
176 smbreq = smbd_smb2_fake_smb_request(smb2req);
177 if (tevent_req_nomem(smbreq, req)) {
178 return tevent_req_post(req, ev);
181 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
182 if (fsp == NULL) {
183 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
184 return tevent_req_post(req, ev);
186 if (conn != fsp->conn) {
187 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
188 return tevent_req_post(req, ev);
190 if (smb2req->session->vuid != fsp->vuid) {
191 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
192 return tevent_req_post(req, ev);
195 DEBUG(5,("smbd_smb2_oplock_break_send: got SMB2 oplock break (%u) from client "
196 "for file %s fnum = %d\n",
197 (unsigned int)in_oplock_level,
198 fsp_str_dbg(fsp),
199 fsp->fnum ));
201 /* Are we awaiting a break message ? */
202 if (fsp->oplock_timeout == NULL) {
203 tevent_req_nterror(req, NT_STATUS_INVALID_OPLOCK_PROTOCOL);
204 return tevent_req_post(req, ev);
207 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
208 (break_to_none)) {
209 result = remove_oplock(fsp);
210 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
211 } else {
212 result = downgrade_oplock(fsp);
213 state->out_oplock_level = SMB2_OPLOCK_LEVEL_II;
216 if (!result) {
217 DEBUG(0, ("smbd_smb2_oplock_break_send: error in removing "
218 "oplock on file %s\n", fsp_str_dbg(fsp)));
219 /* Hmmm. Is this panic justified? */
220 smb_panic("internal tdb error");
223 reply_to_oplock_break_requests(fsp);
225 tevent_req_done(req);
226 return tevent_req_post(req, ev);
229 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
230 uint8_t *out_oplock_level)
232 NTSTATUS status;
233 struct smbd_smb2_oplock_break_state *state =
234 tevent_req_data(req,
235 struct smbd_smb2_oplock_break_state);
237 if (tevent_req_is_nterror(req, &status)) {
238 tevent_req_received(req);
239 return status;
242 *out_oplock_level = state->out_oplock_level;
244 tevent_req_received(req);
245 return NT_STATUS_OK;
248 /*********************************************************
249 Create and send an asynchronous
250 SMB2 OPLOCK_BREAK_NOTIFICATION.
251 *********************************************************/
253 void send_break_message_smb2(files_struct *fsp, int level)
255 uint8_t smb2_oplock_level = (level == OPLOCKLEVEL_II) ?
256 SMB2_OPLOCK_LEVEL_II :
257 SMB2_OPLOCK_LEVEL_NONE;
258 NTSTATUS status;
260 DEBUG(10,("send_break_message_smb2: sending oplock break "
261 "for file %s, fnum = %d, smb2 level %u\n",
262 fsp_str_dbg(fsp),
263 fsp->fnum,
264 (unsigned int)smb2_oplock_level ));
266 status = smbd_smb2_send_oplock_break(fsp->conn->sconn,
267 (uint64_t)fsp->fnum,
268 (uint64_t)fsp->fnum,
269 smb2_oplock_level);
270 if (!NT_STATUS_IS_OK(status)) {
271 smbd_server_connection_terminate(fsp->conn->sconn,
272 nt_errstr(status));