s3:smb2_break: make use of file_fsp_smb2()
[samba.git] / source3 / smbd / smb2_break.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6    Copyright (C) Jeremy Allison 2010
7
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.
12
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.
17
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/>.
20 */
21
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"
27
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);
35
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)
38 {
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 files_struct *in_fsp;
46         struct tevent_req *subreq;
47
48         status = smbd_smb2_request_verify_sizes(req, 0x18);
49         if (!NT_STATUS_IS_OK(status)) {
50                 return smbd_smb2_request_error(req, status);
51         }
52         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
53
54         in_oplock_level         = CVAL(inbody, 0x02);
55
56         if (in_oplock_level != SMB2_OPLOCK_LEVEL_NONE &&
57                         in_oplock_level != SMB2_OPLOCK_LEVEL_II) {
58                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
59         }
60
61         /* 0x03 1 bytes reserved */
62         /* 0x04 4 bytes reserved */
63         in_file_id_persistent           = BVAL(inbody, 0x08);
64         in_file_id_volatile             = BVAL(inbody, 0x10);
65
66         in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
67         if (in_fsp == NULL) {
68                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
69         }
70
71         subreq = smbd_smb2_oplock_break_send(req, req->sconn->ev_ctx,
72                                              req, in_fsp, in_oplock_level);
73         if (subreq == NULL) {
74                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
75         }
76         tevent_req_set_callback(subreq, smbd_smb2_request_oplock_break_done, req);
77
78         return smbd_smb2_request_pending_queue(req, subreq, 500);
79 }
80
81 static void smbd_smb2_request_oplock_break_done(struct tevent_req *subreq)
82 {
83         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
84                                         struct smbd_smb2_request);
85         const uint8_t *inbody;
86         int i = req->current_idx;
87         uint64_t in_file_id_persistent;
88         uint64_t in_file_id_volatile;
89         uint8_t out_oplock_level = 0;
90         DATA_BLOB outbody;
91         NTSTATUS status;
92         NTSTATUS error; /* transport error */
93
94         status = smbd_smb2_oplock_break_recv(subreq, &out_oplock_level);
95         TALLOC_FREE(subreq);
96         if (!NT_STATUS_IS_OK(status)) {
97                 error = smbd_smb2_request_error(req, status);
98                 if (!NT_STATUS_IS_OK(error)) {
99                         smbd_server_connection_terminate(req->sconn,
100                                                          nt_errstr(error));
101                         return;
102                 }
103                 return;
104         }
105
106         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
107
108         in_file_id_persistent   = BVAL(inbody, 0x08);
109         in_file_id_volatile     = BVAL(inbody, 0x10);
110
111         outbody = data_blob_talloc(req->out.vector, NULL, 0x18);
112         if (outbody.data == NULL) {
113                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
114                 if (!NT_STATUS_IS_OK(error)) {
115                         smbd_server_connection_terminate(req->sconn,
116                                                          nt_errstr(error));
117                         return;
118                 }
119                 return;
120         }
121
122         SSVAL(outbody.data, 0x00, 0x18);        /* struct size */
123         SCVAL(outbody.data, 0x02,
124               out_oplock_level);                /* SMB2 oplock level */
125         SCVAL(outbody.data, 0x03, 0);           /* reserved */
126         SIVAL(outbody.data, 0x04, 0);           /* reserved */
127         SBVAL(outbody.data, 0x08,
128               in_file_id_persistent);           /* file id (persistent) */
129         SBVAL(outbody.data, 0x10,
130               in_file_id_volatile);             /* file id (volatile) */
131
132         error = smbd_smb2_request_done(req, outbody, NULL);
133         if (!NT_STATUS_IS_OK(error)) {
134                 smbd_server_connection_terminate(req->sconn,
135                                                  nt_errstr(error));
136                 return;
137         }
138 }
139
140 struct smbd_smb2_oplock_break_state {
141         struct smbd_smb2_request *smb2req;
142         uint8_t out_oplock_level; /* SMB2 oplock level. */
143 };
144
145 static struct tevent_req *smbd_smb2_oplock_break_send(TALLOC_CTX *mem_ctx,
146                                                       struct tevent_context *ev,
147                                                       struct smbd_smb2_request *smb2req,
148                                                       struct files_struct *fsp,
149                                                       uint8_t in_oplock_level)
150 {
151         struct tevent_req *req;
152         struct smbd_smb2_oplock_break_state *state;
153         struct smb_request *smbreq;
154         int oplocklevel = map_smb2_oplock_levels_to_samba(in_oplock_level);
155         bool break_to_none = (oplocklevel == NO_OPLOCK);
156         bool result;
157
158         req = tevent_req_create(mem_ctx, &state,
159                                 struct smbd_smb2_oplock_break_state);
160         if (req == NULL) {
161                 return NULL;
162         }
163         state->smb2req = smb2req;
164         state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
165
166         DEBUG(10,("smbd_smb2_oplock_break_send: %s - fnum[%d] "
167                   "samba level %d\n",
168                   fsp_str_dbg(fsp), fsp->fnum,
169                   oplocklevel));
170
171         smbreq = smbd_smb2_fake_smb_request(smb2req);
172         if (tevent_req_nomem(smbreq, req)) {
173                 return tevent_req_post(req, ev);
174         }
175
176         DEBUG(5,("smbd_smb2_oplock_break_send: got SMB2 oplock break (%u) from client "
177                 "for file %s fnum = %d\n",
178                 (unsigned int)in_oplock_level,
179                 fsp_str_dbg(fsp),
180                 fsp->fnum ));
181
182         /* Are we awaiting a break message ? */
183         if (fsp->oplock_timeout == NULL) {
184                 tevent_req_nterror(req, NT_STATUS_INVALID_OPLOCK_PROTOCOL);
185                 return tevent_req_post(req, ev);
186         }
187
188         if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
189                         (break_to_none)) {
190                 result = remove_oplock(fsp);
191                 state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
192         } else {
193                 result = downgrade_oplock(fsp);
194                 state->out_oplock_level = SMB2_OPLOCK_LEVEL_II;
195         }
196
197         if (!result) {
198                 DEBUG(0, ("smbd_smb2_oplock_break_send: error in removing "
199                         "oplock on file %s\n", fsp_str_dbg(fsp)));
200                 /* Hmmm. Is this panic justified? */
201                 smb_panic("internal tdb error");
202         }
203
204         reply_to_oplock_break_requests(fsp);
205
206         tevent_req_done(req);
207         return tevent_req_post(req, ev);
208 }
209
210 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
211                                             uint8_t *out_oplock_level)
212 {
213         NTSTATUS status;
214         struct smbd_smb2_oplock_break_state *state =
215                 tevent_req_data(req,
216                 struct smbd_smb2_oplock_break_state);
217
218         if (tevent_req_is_nterror(req, &status)) {
219                 tevent_req_received(req);
220                 return status;
221         }
222
223         *out_oplock_level = state->out_oplock_level;
224
225         tevent_req_received(req);
226         return NT_STATUS_OK;
227 }
228
229 /*********************************************************
230  Create and send an asynchronous
231  SMB2 OPLOCK_BREAK_NOTIFICATION.
232 *********************************************************/
233
234 void send_break_message_smb2(files_struct *fsp, int level)
235 {
236         uint8_t smb2_oplock_level = (level == OPLOCKLEVEL_II) ?
237                                 SMB2_OPLOCK_LEVEL_II :
238                                 SMB2_OPLOCK_LEVEL_NONE;
239         NTSTATUS status;
240
241         DEBUG(10,("send_break_message_smb2: sending oplock break "
242                 "for file %s, fnum = %d, smb2 level %u\n",
243                 fsp_str_dbg(fsp),
244                 fsp->fnum,
245                 (unsigned int)smb2_oplock_level ));
246
247         status = smbd_smb2_send_oplock_break(fsp->conn->sconn,
248                                         (uint64_t)fsp->fnum,
249                                         (uint64_t)fsp->fnum,
250                                         smb2_oplock_level);
251         if (!NT_STATUS_IS_OK(status)) {
252                 smbd_server_connection_terminate(fsp->conn->sconn,
253                                  nt_errstr(status));
254         }
255 }