s3:smbd: rename conn => sconn for smbd_server_connection structs
[Samba/aatanasov.git] / source3 / smbd / smb2_break.c
blob29c55fccf6ac766ff356d00aa16a68dcc2f626ad
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "smbd/globals.h"
23 #include "../source4/libcli/smb2/smb2_constants.h"
25 static struct tevent_req *smbd_smb2_oplock_break_send(TALLOC_CTX *mem_ctx,
26 struct tevent_context *ev,
27 struct smbd_smb2_request *smb2req,
28 uint8_t in_oplock_level,
29 uint64_t in_file_id_volatile);
30 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
31 uint8_t *out_oplock_level);
33 static void smbd_smb2_request_oplock_break_done(struct tevent_req *subreq);
34 NTSTATUS smbd_smb2_request_process_break(struct smbd_smb2_request *req)
36 const uint8_t *inhdr;
37 const uint8_t *inbody;
38 int i = req->current_idx;
39 size_t expected_body_size = 0x18;
40 size_t body_size;
41 uint8_t in_oplock_level;
42 uint64_t in_file_id_persistent;
43 uint64_t in_file_id_volatile;
44 struct tevent_req *subreq;
46 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
47 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
48 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
51 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
53 body_size = SVAL(inbody, 0x00);
54 if (body_size != expected_body_size) {
55 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
58 in_oplock_level = CVAL(inbody, 0x02);
59 /* 0x03 1 bytes reserved */
60 /* 0x04 4 bytes reserved */
61 in_file_id_persistent = BVAL(inbody, 0x08);
62 in_file_id_volatile = BVAL(inbody, 0x10);
64 if (req->compat_chain_fsp) {
65 /* skip check */
66 } else if (in_file_id_persistent != 0) {
67 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
70 subreq = smbd_smb2_oplock_break_send(req,
71 req->sconn->smb2.event_ctx,
72 req,
73 in_oplock_level,
74 in_file_id_volatile);
75 if (subreq == NULL) {
76 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
78 tevent_req_set_callback(subreq, smbd_smb2_request_oplock_break_done, req);
80 if (tevent_req_is_in_progress(subreq)) {
81 return smbd_smb2_request_pending_queue(req);
84 return NT_STATUS_OK;
87 static void smbd_smb2_request_oplock_break_done(struct tevent_req *subreq)
89 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
90 struct smbd_smb2_request);
91 const uint8_t *inbody;
92 int i = req->current_idx;
93 uint64_t in_file_id_persistent;
94 uint64_t in_file_id_volatile;
95 uint8_t out_oplock_level = 0;
96 DATA_BLOB outbody;
97 NTSTATUS status;
98 NTSTATUS error; /* transport error */
100 status = smbd_smb2_oplock_break_recv(subreq, &out_oplock_level);
101 TALLOC_FREE(subreq);
102 if (!NT_STATUS_IS_OK(status)) {
103 error = smbd_smb2_request_error(req, status);
104 if (!NT_STATUS_IS_OK(error)) {
105 smbd_server_connection_terminate(req->sconn,
106 nt_errstr(error));
107 return;
109 return;
112 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
114 in_file_id_persistent = BVAL(inbody, 0x08);
115 in_file_id_volatile = BVAL(inbody, 0x10);
117 outbody = data_blob_talloc(req->out.vector, NULL, 0x18);
118 if (outbody.data == NULL) {
119 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
120 if (!NT_STATUS_IS_OK(error)) {
121 smbd_server_connection_terminate(req->sconn,
122 nt_errstr(error));
123 return;
125 return;
128 SSVAL(outbody.data, 0x00, 0x18); /* struct size */
129 SCVAL(outbody.data, 0x02,
130 out_oplock_level); /* oplock level */
131 SCVAL(outbody.data, 0x03, 0); /* reserved */
132 SIVAL(outbody.data, 0x04, 0); /* reserved */
133 SBVAL(outbody.data, 0x08,
134 in_file_id_persistent); /* file id (persistent) */
135 SBVAL(outbody.data, 0x10,
136 in_file_id_volatile); /* file id (volatile) */
138 error = smbd_smb2_request_done(req, outbody, NULL);
139 if (!NT_STATUS_IS_OK(error)) {
140 smbd_server_connection_terminate(req->sconn,
141 nt_errstr(error));
142 return;
146 struct smbd_smb2_oplock_break_state {
147 struct smbd_smb2_request *smb2req;
148 uint8_t out_oplock_level;
151 static struct tevent_req *smbd_smb2_oplock_break_send(TALLOC_CTX *mem_ctx,
152 struct tevent_context *ev,
153 struct smbd_smb2_request *smb2req,
154 uint8_t in_oplock_level,
155 uint64_t in_file_id_volatile)
157 struct tevent_req *req;
158 struct smbd_smb2_oplock_break_state *state;
159 struct smb_request *smbreq;
160 connection_struct *conn = smb2req->tcon->compat_conn;
161 files_struct *fsp;
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]\n",
172 (unsigned long long)in_file_id_volatile));
174 smbreq = smbd_smb2_fake_smb_request(smb2req);
175 if (tevent_req_nomem(smbreq, req)) {
176 return tevent_req_post(req, ev);
179 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
180 if (fsp == NULL) {
181 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
182 return tevent_req_post(req, ev);
184 if (conn != fsp->conn) {
185 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
186 return tevent_req_post(req, ev);
188 if (smb2req->session->vuid != fsp->vuid) {
189 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
190 return tevent_req_post(req, ev);
193 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
194 return tevent_req_post(req, ev);
197 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
198 uint8_t *out_oplock_level)
200 NTSTATUS status;
201 struct smbd_smb2_oplock_break_state *state =
202 tevent_req_data(req,
203 struct smbd_smb2_oplock_break_state);
205 if (tevent_req_is_nterror(req, &status)) {
206 tevent_req_received(req);
207 return status;
210 *out_oplock_level = state->out_oplock_level;
212 tevent_req_received(req);
213 return NT_STATUS_OK;