s3-smbd: Fix flooding the logs with records we don't find in pcap.
[Samba.git] / source3 / smbd / smb2_notify.c
blob72430eb955832e715f50ac3b00cd4e5e9026cf6e
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 struct smbd_smb2_notify_state {
29 struct smbd_smb2_request *smb2req;
30 struct smb_request *smbreq;
31 struct tevent_immediate *im;
32 NTSTATUS status;
33 DATA_BLOB out_output_buffer;
36 static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
37 struct tevent_context *ev,
38 struct smbd_smb2_request *smb2req,
39 struct files_struct *in_fsp,
40 uint16_t in_flags,
41 uint32_t in_output_buffer_length,
42 uint64_t in_completion_filter);
43 static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
44 TALLOC_CTX *mem_ctx,
45 DATA_BLOB *out_output_buffer);
47 static void smbd_smb2_request_notify_done(struct tevent_req *subreq);
48 NTSTATUS smbd_smb2_request_process_notify(struct smbd_smb2_request *req)
50 NTSTATUS status;
51 const uint8_t *inbody;
52 int i = req->current_idx;
53 uint16_t in_flags;
54 uint32_t in_output_buffer_length;
55 uint64_t in_file_id_persistent;
56 uint64_t in_file_id_volatile;
57 struct files_struct *in_fsp;
58 uint64_t in_completion_filter;
59 struct tevent_req *subreq;
61 status = smbd_smb2_request_verify_sizes(req, 0x20);
62 if (!NT_STATUS_IS_OK(status)) {
63 return smbd_smb2_request_error(req, status);
65 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
67 in_flags = SVAL(inbody, 0x02);
68 in_output_buffer_length = IVAL(inbody, 0x04);
69 in_file_id_persistent = BVAL(inbody, 0x08);
70 in_file_id_volatile = BVAL(inbody, 0x10);
71 in_completion_filter = IVAL(inbody, 0x18);
74 * 0x00010000 is what Windows 7 uses,
75 * Windows 2008 uses 0x00080000
77 if (in_output_buffer_length > req->sconn->smb2.max_trans) {
78 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
81 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
82 if (in_fsp == NULL) {
83 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
86 subreq = smbd_smb2_notify_send(req, req->sconn->smb2.event_ctx,
87 req, in_fsp,
88 in_flags,
89 in_output_buffer_length,
90 in_completion_filter);
91 if (subreq == NULL) {
92 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
94 tevent_req_set_callback(subreq, smbd_smb2_request_notify_done, req);
96 return smbd_smb2_request_pending_queue(req, subreq);
99 static void smbd_smb2_request_notify_done(struct tevent_req *subreq)
101 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
102 struct smbd_smb2_request);
103 int i = req->current_idx;
104 uint8_t *outhdr;
105 DATA_BLOB outbody;
106 DATA_BLOB outdyn;
107 uint16_t out_output_buffer_offset;
108 DATA_BLOB out_output_buffer = data_blob_null;
109 NTSTATUS status;
110 NTSTATUS error; /* transport error */
112 if (req->cancelled) {
113 struct smbd_smb2_notify_state *state = tevent_req_data(subreq,
114 struct smbd_smb2_notify_state);
115 const uint8_t *inhdr = (const uint8_t *)req->in.vector[i].iov_base;
116 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
118 DEBUG(10,("smbd_smb2_request_notify_done: cancelled mid %llu\n",
119 (unsigned long long)mid ));
120 error = smbd_smb2_request_error(req, NT_STATUS_CANCELLED);
121 if (!NT_STATUS_IS_OK(error)) {
122 smbd_server_connection_terminate(req->sconn,
123 nt_errstr(error));
124 return;
126 TALLOC_FREE(state->im);
127 return;
130 status = smbd_smb2_notify_recv(subreq,
131 req,
132 &out_output_buffer);
133 TALLOC_FREE(subreq);
134 if (!NT_STATUS_IS_OK(status)) {
135 error = smbd_smb2_request_error(req, status);
136 if (!NT_STATUS_IS_OK(error)) {
137 smbd_server_connection_terminate(req->sconn,
138 nt_errstr(error));
139 return;
141 return;
144 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
146 outhdr = (uint8_t *)req->out.vector[i].iov_base;
148 outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
149 if (outbody.data == NULL) {
150 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
151 if (!NT_STATUS_IS_OK(error)) {
152 smbd_server_connection_terminate(req->sconn,
153 nt_errstr(error));
154 return;
156 return;
159 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
160 SSVAL(outbody.data, 0x02,
161 out_output_buffer_offset); /* output buffer offset */
162 SIVAL(outbody.data, 0x04,
163 out_output_buffer.length); /* output buffer length */
165 outdyn = out_output_buffer;
167 error = smbd_smb2_request_done(req, outbody, &outdyn);
168 if (!NT_STATUS_IS_OK(error)) {
169 smbd_server_connection_terminate(req->sconn,
170 nt_errstr(error));
171 return;
175 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
176 NTSTATUS error_code,
177 uint8_t *buf, size_t len);
178 static void smbd_smb2_notify_reply_trigger(struct tevent_context *ctx,
179 struct tevent_immediate *im,
180 void *private_data);
181 static bool smbd_smb2_notify_cancel(struct tevent_req *req);
183 static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
184 struct tevent_context *ev,
185 struct smbd_smb2_request *smb2req,
186 struct files_struct *fsp,
187 uint16_t in_flags,
188 uint32_t in_output_buffer_length,
189 uint64_t in_completion_filter)
191 struct tevent_req *req;
192 struct smbd_smb2_notify_state *state;
193 struct smb_request *smbreq;
194 connection_struct *conn = smb2req->tcon->compat_conn;
195 bool recursive = (in_flags & 0x0001) ? true : false;
196 NTSTATUS status;
198 req = tevent_req_create(mem_ctx, &state,
199 struct smbd_smb2_notify_state);
200 if (req == NULL) {
201 return NULL;
203 state->smb2req = smb2req;
204 state->status = NT_STATUS_INTERNAL_ERROR;
205 state->out_output_buffer = data_blob_null;
206 state->im = NULL;
208 DEBUG(10,("smbd_smb2_notify_send: %s - fnum[%d]\n",
209 fsp_str_dbg(fsp), fsp->fnum));
211 smbreq = smbd_smb2_fake_smb_request(smb2req);
212 if (tevent_req_nomem(smbreq, req)) {
213 return tevent_req_post(req, ev);
216 state->smbreq = smbreq;
217 smbreq->async_priv = (void *)req;
220 char *filter_string;
222 filter_string = notify_filter_string(NULL, in_completion_filter);
223 if (tevent_req_nomem(filter_string, req)) {
224 return tevent_req_post(req, ev);
227 DEBUG(3,("smbd_smb2_notify_send: notify change "
228 "called on %s, filter = %s, recursive = %d\n",
229 fsp_str_dbg(fsp), filter_string, recursive));
231 TALLOC_FREE(filter_string);
234 if ((!fsp->is_directory) || (conn != fsp->conn)) {
235 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
236 return tevent_req_post(req, ev);
239 if (fsp->notify == NULL) {
241 status = change_notify_create(fsp,
242 in_completion_filter,
243 recursive);
244 if (!NT_STATUS_IS_OK(status)) {
245 DEBUG(10, ("change_notify_create returned %s\n",
246 nt_errstr(status)));
247 tevent_req_nterror(req, status);
248 return tevent_req_post(req, ev);
252 if (fsp->notify->num_changes != 0) {
255 * We've got changes pending, respond immediately
259 * TODO: write a torture test to check the filtering behaviour
260 * here.
263 change_notify_reply(smbreq,
264 NT_STATUS_OK,
265 in_output_buffer_length,
266 fsp->notify,
267 smbd_smb2_notify_reply);
270 * change_notify_reply() above has independently
271 * called tevent_req_done().
273 return tevent_req_post(req, ev);
276 state->im = tevent_create_immediate(state);
277 if (tevent_req_nomem(state->im, req)) {
278 return tevent_req_post(req, ev);
282 * No changes pending, queue the request
285 status = change_notify_add_request(smbreq,
286 in_output_buffer_length,
287 in_completion_filter,
288 recursive, fsp,
289 smbd_smb2_notify_reply);
290 if (!NT_STATUS_IS_OK(status)) {
291 tevent_req_nterror(req, status);
292 return tevent_req_post(req, ev);
295 /* allow this request to be canceled */
296 tevent_req_set_cancel_fn(req, smbd_smb2_notify_cancel);
298 return req;
301 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
302 NTSTATUS error_code,
303 uint8_t *buf, size_t len)
305 struct tevent_req *req = talloc_get_type_abort(smbreq->async_priv,
306 struct tevent_req);
307 struct smbd_smb2_notify_state *state = tevent_req_data(req,
308 struct smbd_smb2_notify_state);
310 state->status = error_code;
311 if (!NT_STATUS_IS_OK(error_code)) {
312 /* nothing */
313 } else if (len == 0) {
314 state->status = STATUS_NOTIFY_ENUM_DIR;
315 } else {
316 state->out_output_buffer = data_blob_talloc(state, buf, len);
317 if (state->out_output_buffer.data == NULL) {
318 state->status = NT_STATUS_NO_MEMORY;
322 if (state->im == NULL) {
323 smbd_smb2_notify_reply_trigger(NULL, NULL, req);
324 return;
328 * if this is called async, we need to go via an immediate event
329 * because the caller replies on the smb_request (a child of req
330 * being arround after calling this function
332 tevent_schedule_immediate(state->im,
333 state->smb2req->sconn->smb2.event_ctx,
334 smbd_smb2_notify_reply_trigger,
335 req);
338 static void smbd_smb2_notify_reply_trigger(struct tevent_context *ctx,
339 struct tevent_immediate *im,
340 void *private_data)
342 struct tevent_req *req = talloc_get_type_abort(private_data,
343 struct tevent_req);
344 struct smbd_smb2_notify_state *state = tevent_req_data(req,
345 struct smbd_smb2_notify_state);
347 if (!NT_STATUS_IS_OK(state->status)) {
348 tevent_req_nterror(req, state->status);
349 return;
352 tevent_req_done(req);
355 static bool smbd_smb2_notify_cancel(struct tevent_req *req)
357 struct smbd_smb2_notify_state *state = tevent_req_data(req,
358 struct smbd_smb2_notify_state);
360 smbd_notify_cancel_by_smbreq(state->smbreq);
362 state->smb2req->cancelled = true;
363 tevent_req_done(req);
364 return true;
367 static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
368 TALLOC_CTX *mem_ctx,
369 DATA_BLOB *out_output_buffer)
371 NTSTATUS status;
372 struct smbd_smb2_notify_state *state = tevent_req_data(req,
373 struct smbd_smb2_notify_state);
375 if (tevent_req_is_nterror(req, &status)) {
376 tevent_req_received(req);
377 return status;
380 *out_output_buffer = state->out_output_buffer;
381 talloc_steal(mem_ctx, out_output_buffer->data);
383 tevent_req_received(req);
384 return NT_STATUS_OK;