First part of fix for bug #7331 - Compound async SMB 2 requests don't work right.
[Samba/ekacnet.git] / source3 / smbd / smb2_notify.c
blob3d60ffb39c5537b61dfc817f13613d77981123ae
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 "../libcli/smb/smb_common.h"
25 static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
26 struct tevent_context *ev,
27 struct smbd_smb2_request *smb2req,
28 uint16_t in_flags,
29 uint32_t in_output_buffer_length,
30 uint64_t in_file_id_volatile,
31 uint64_t in_completion_filter);
32 static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
33 TALLOC_CTX *mem_ctx,
34 DATA_BLOB *out_output_buffer);
36 static void smbd_smb2_request_notify_done(struct tevent_req *subreq);
37 NTSTATUS smbd_smb2_request_process_notify(struct smbd_smb2_request *req)
39 const uint8_t *inhdr;
40 const uint8_t *inbody;
41 int i = req->current_idx;
42 size_t expected_body_size = 0x20;
43 size_t body_size;
44 uint16_t in_flags;
45 uint32_t in_output_buffer_length;
46 uint64_t in_file_id_persistent;
47 uint64_t in_file_id_volatile;
48 uint64_t in_completion_filter;
49 struct tevent_req *subreq;
51 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
52 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
53 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
56 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
58 body_size = SVAL(inbody, 0x00);
59 if (body_size != expected_body_size) {
60 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
63 in_flags = SVAL(inbody, 0x02);
64 in_output_buffer_length = IVAL(inbody, 0x04);
65 in_file_id_persistent = BVAL(inbody, 0x08);
66 in_file_id_volatile = BVAL(inbody, 0x10);
67 in_completion_filter = IVAL(inbody, 0x18);
70 * 0x00010000 is what Windows 7 uses,
71 * Windows 2008 uses 0x00080000
73 if (in_output_buffer_length > lp_smb2_max_trans()) {
74 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
77 if (req->compat_chain_fsp) {
78 /* skip check */
79 } else if (in_file_id_persistent != 0) {
80 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
83 subreq = smbd_smb2_notify_send(req,
84 req->sconn->smb2.event_ctx,
85 req,
86 in_flags,
87 in_output_buffer_length,
88 in_file_id_volatile,
89 in_completion_filter);
90 if (subreq == NULL) {
91 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
93 tevent_req_set_callback(subreq, smbd_smb2_request_notify_done, req);
95 return smbd_smb2_request_pending_queue(req, subreq);
98 static void smbd_smb2_request_notify_done(struct tevent_req *subreq)
100 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
101 struct smbd_smb2_request);
102 int i = req->current_idx;
103 uint8_t *outhdr;
104 DATA_BLOB outbody;
105 DATA_BLOB outdyn;
106 uint16_t out_output_buffer_offset;
107 DATA_BLOB out_output_buffer = data_blob_null;
108 NTSTATUS status;
109 NTSTATUS error; /* transport error */
111 if (req->cancelled) {
112 const uint8_t *inhdr = (const uint8_t *)req->in.vector[i].iov_base;
113 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
114 DEBUG(10,("smbd_smb2_request_notify_done: cancelled mid %llu\n",
115 (unsigned long long)mid ));
116 return;
119 status = smbd_smb2_notify_recv(subreq,
120 req,
121 &out_output_buffer);
122 TALLOC_FREE(subreq);
123 if (!NT_STATUS_IS_OK(status)) {
124 error = smbd_smb2_request_error(req, status);
125 if (!NT_STATUS_IS_OK(error)) {
126 smbd_server_connection_terminate(req->sconn,
127 nt_errstr(error));
128 return;
130 return;
133 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
135 outhdr = (uint8_t *)req->out.vector[i].iov_base;
137 outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
138 if (outbody.data == NULL) {
139 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
140 if (!NT_STATUS_IS_OK(error)) {
141 smbd_server_connection_terminate(req->sconn,
142 nt_errstr(error));
143 return;
145 return;
148 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
149 SSVAL(outbody.data, 0x02,
150 out_output_buffer_offset); /* output buffer offset */
151 SIVAL(outbody.data, 0x04,
152 out_output_buffer.length); /* output buffer length */
154 outdyn = out_output_buffer;
156 error = smbd_smb2_request_done(req, outbody, &outdyn);
157 if (!NT_STATUS_IS_OK(error)) {
158 smbd_server_connection_terminate(req->sconn,
159 nt_errstr(error));
160 return;
164 struct smbd_smb2_notify_state {
165 struct smbd_smb2_request *smb2req;
166 struct smb_request *smbreq;
167 struct tevent_immediate *im;
168 NTSTATUS status;
169 DATA_BLOB out_output_buffer;
172 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
173 NTSTATUS error_code,
174 uint8_t *buf, size_t len);
175 static void smbd_smb2_notify_reply_trigger(struct tevent_context *ctx,
176 struct tevent_immediate *im,
177 void *private_data);
178 static bool smbd_smb2_notify_cancel(struct tevent_req *req);
180 static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
181 struct tevent_context *ev,
182 struct smbd_smb2_request *smb2req,
183 uint16_t in_flags,
184 uint32_t in_output_buffer_length,
185 uint64_t in_file_id_volatile,
186 uint64_t in_completion_filter)
188 struct tevent_req *req;
189 struct smbd_smb2_notify_state *state;
190 struct smb_request *smbreq;
191 connection_struct *conn = smb2req->tcon->compat_conn;
192 files_struct *fsp;
193 bool recursive = (in_flags & 0x0001) ? true : false;
194 NTSTATUS status;
196 req = tevent_req_create(mem_ctx, &state,
197 struct smbd_smb2_notify_state);
198 if (req == NULL) {
199 return NULL;
201 state->smb2req = smb2req;
202 state->status = NT_STATUS_INTERNAL_ERROR;
203 state->out_output_buffer = data_blob_null;
204 state->im = NULL;
206 DEBUG(10,("smbd_smb2_notify_send: file_id[0x%016llX]\n",
207 (unsigned long long)in_file_id_volatile));
209 smbreq = smbd_smb2_fake_smb_request(smb2req);
210 if (tevent_req_nomem(smbreq, req)) {
211 return tevent_req_post(req, ev);
214 state->smbreq = smbreq;
215 smbreq->async_priv = (void *)req;
217 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
218 if (fsp == NULL) {
219 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
220 return tevent_req_post(req, ev);
222 if (conn != fsp->conn) {
223 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
224 return tevent_req_post(req, ev);
226 if (smb2req->session->vuid != fsp->vuid) {
227 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
228 return tevent_req_post(req, ev);
232 char *filter_string;
234 filter_string = notify_filter_string(NULL, in_completion_filter);
235 if (tevent_req_nomem(filter_string, req)) {
236 return tevent_req_post(req, ev);
239 DEBUG(3,("smbd_smb2_notify_send: notify change "
240 "called on %s, filter = %s, recursive = %d\n",
241 fsp_str_dbg(fsp), filter_string, recursive));
243 TALLOC_FREE(filter_string);
246 if ((!fsp->is_directory) || (conn != fsp->conn)) {
247 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
248 return tevent_req_post(req, ev);
251 if (fsp->notify == NULL) {
253 status = change_notify_create(fsp,
254 in_completion_filter,
255 recursive);
256 if (!NT_STATUS_IS_OK(status)) {
257 DEBUG(10, ("change_notify_create returned %s\n",
258 nt_errstr(status)));
259 tevent_req_nterror(req, status);
260 return tevent_req_post(req, ev);
264 if (fsp->notify->num_changes != 0) {
267 * We've got changes pending, respond immediately
271 * TODO: write a torture test to check the filtering behaviour
272 * here.
275 change_notify_reply(fsp->conn, smbreq,
276 NT_STATUS_OK,
277 in_output_buffer_length,
278 fsp->notify,
279 smbd_smb2_notify_reply);
282 * change_notify_reply() above has independently
283 * called tevent_req_done().
285 return tevent_req_post(req, ev);
288 state->im = tevent_create_immediate(state);
289 if (tevent_req_nomem(state->im, req)) {
290 return tevent_req_post(req, ev);
294 * No changes pending, queue the request
297 status = change_notify_add_request(smbreq,
298 in_output_buffer_length,
299 in_completion_filter,
300 recursive, fsp,
301 smbd_smb2_notify_reply);
302 if (!NT_STATUS_IS_OK(status)) {
303 tevent_req_nterror(req, status);
304 return tevent_req_post(req, ev);
307 /* allow this request to be canceled */
308 tevent_req_set_cancel_fn(req, smbd_smb2_notify_cancel);
310 return req;
313 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
314 NTSTATUS error_code,
315 uint8_t *buf, size_t len)
317 struct tevent_req *req = talloc_get_type_abort(smbreq->async_priv,
318 struct tevent_req);
319 struct smbd_smb2_notify_state *state = tevent_req_data(req,
320 struct smbd_smb2_notify_state);
322 state->status = error_code;
323 if (!NT_STATUS_IS_OK(error_code)) {
324 /* nothing */
325 } else if (len == 0) {
326 state->status = STATUS_NOTIFY_ENUM_DIR;
327 } else {
328 state->out_output_buffer = data_blob_talloc(state, buf, len);
329 if (state->out_output_buffer.data == NULL) {
330 state->status = NT_STATUS_NO_MEMORY;
334 if (state->im == NULL) {
335 smbd_smb2_notify_reply_trigger(NULL, NULL, req);
336 return;
340 * if this is called async, we need to go via an immediate event
341 * because the caller replies on the smb_request (a child of req
342 * being arround after calling this function
344 tevent_schedule_immediate(state->im,
345 state->smb2req->sconn->smb2.event_ctx,
346 smbd_smb2_notify_reply_trigger,
347 req);
350 static void smbd_smb2_notify_reply_trigger(struct tevent_context *ctx,
351 struct tevent_immediate *im,
352 void *private_data)
354 struct tevent_req *req = talloc_get_type_abort(private_data,
355 struct tevent_req);
356 struct smbd_smb2_notify_state *state = tevent_req_data(req,
357 struct smbd_smb2_notify_state);
359 if (!NT_STATUS_IS_OK(state->status)) {
360 tevent_req_nterror(req, state->status);
361 return;
364 tevent_req_done(req);
367 static bool smbd_smb2_notify_cancel(struct tevent_req *req)
369 struct smbd_smb2_notify_state *state = tevent_req_data(req,
370 struct smbd_smb2_notify_state);
372 smbd_notify_cancel_by_smbreq(state->smb2req->sconn,
373 state->smbreq);
375 state->smb2req->cancelled = true;
376 tevent_req_done(req);
377 return true;
380 static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
381 TALLOC_CTX *mem_ctx,
382 DATA_BLOB *out_output_buffer)
384 NTSTATUS status;
385 struct smbd_smb2_notify_state *state = tevent_req_data(req,
386 struct smbd_smb2_notify_state);
388 if (tevent_req_is_nterror(req, &status)) {
389 tevent_req_received(req);
390 return status;
393 *out_output_buffer = state->out_output_buffer;
394 talloc_steal(mem_ctx, out_output_buffer->data);
396 tevent_req_received(req);
397 return NT_STATUS_OK;