libcli: move some common SMB and SMB2 stuff into libcli/smb/
[kai/samba-autobuild/.git] / source3 / smbd / smb2_break.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/globals.h"
23 #include "../libcli/smb/smb_common.h"
24
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);
32
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)
35 {
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;
45
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);
49         }
50
51         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
52
53         body_size = SVAL(inbody, 0x00);
54         if (body_size != expected_body_size) {
55                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
56         }
57
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);
63
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);
68         }
69
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);
77         }
78         tevent_req_set_callback(subreq, smbd_smb2_request_oplock_break_done, req);
79
80         if (tevent_req_is_in_progress(subreq)) {
81                 return smbd_smb2_request_pending_queue(req);
82         }
83
84         return NT_STATUS_OK;
85 }
86
87 static void smbd_smb2_request_oplock_break_done(struct tevent_req *subreq)
88 {
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 */
99
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;
108                 }
109                 return;
110         }
111
112         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
113
114         in_file_id_persistent   = BVAL(inbody, 0x08);
115         in_file_id_volatile     = BVAL(inbody, 0x10);
116
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;
124                 }
125                 return;
126         }
127
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) */
137
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;
143         }
144 }
145
146 struct smbd_smb2_oplock_break_state {
147         struct smbd_smb2_request *smb2req;
148         uint8_t out_oplock_level;
149 };
150
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)
156 {
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;
162
163         req = tevent_req_create(mem_ctx, &state,
164                                 struct smbd_smb2_oplock_break_state);
165         if (req == NULL) {
166                 return NULL;
167         }
168         state->smb2req = smb2req;
169         state->out_oplock_level = SMB2_OPLOCK_LEVEL_NONE;
170
171         DEBUG(10,("smbd_smb2_oplock_break_send: file_id[0x%016llX]\n",
172                   (unsigned long long)in_file_id_volatile));
173
174         smbreq = smbd_smb2_fake_smb_request(smb2req);
175         if (tevent_req_nomem(smbreq, req)) {
176                 return tevent_req_post(req, ev);
177         }
178
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);
183         }
184         if (conn != fsp->conn) {
185                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
186                 return tevent_req_post(req, ev);
187         }
188         if (smb2req->session->vuid != fsp->vuid) {
189                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
190                 return tevent_req_post(req, ev);
191         }
192
193         tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
194         return tevent_req_post(req, ev);
195 }
196
197 static NTSTATUS smbd_smb2_oplock_break_recv(struct tevent_req *req,
198                                             uint8_t *out_oplock_level)
199 {
200         NTSTATUS status;
201         struct smbd_smb2_oplock_break_state *state =
202                 tevent_req_data(req,
203                 struct smbd_smb2_oplock_break_state);
204
205         if (tevent_req_is_nterror(req, &status)) {
206                 tevent_req_received(req);
207                 return status;
208         }
209
210         *out_oplock_level = state->out_oplock_level;
211
212         tevent_req_received(req);
213         return NT_STATUS_OK;
214 }