2 Unix SMB/CIFS implementation.
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/>.
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,
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,
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)
40 const uint8_t *inbody;
41 int i = req->current_idx;
42 size_t expected_body_size = 0x20;
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 > 0x00010000) {
74 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
77 if (req->compat_chain_fsp) {
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,
87 in_output_buffer_length,
89 in_completion_filter);
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;
106 uint16_t out_output_buffer_offset;
107 DATA_BLOB out_output_buffer = data_blob_null;
109 NTSTATUS error; /* transport error */
111 status = smbd_smb2_notify_recv(subreq,
115 if (!NT_STATUS_IS_OK(status)) {
116 error = smbd_smb2_request_error(req, status);
117 if (!NT_STATUS_IS_OK(error)) {
118 smbd_server_connection_terminate(req->sconn,
125 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
127 outhdr = (uint8_t *)req->out.vector[i].iov_base;
129 outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
130 if (outbody.data == NULL) {
131 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
132 if (!NT_STATUS_IS_OK(error)) {
133 smbd_server_connection_terminate(req->sconn,
140 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
141 SSVAL(outbody.data, 0x02,
142 out_output_buffer_offset); /* output buffer offset */
143 SIVAL(outbody.data, 0x04,
144 out_output_buffer.length); /* output buffer length */
146 outdyn = out_output_buffer;
148 error = smbd_smb2_request_done(req, outbody, &outdyn);
149 if (!NT_STATUS_IS_OK(error)) {
150 smbd_server_connection_terminate(req->sconn,
156 struct smbd_smb2_notify_state {
157 struct smbd_smb2_request *smb2req;
158 struct tevent_immediate *im;
160 DATA_BLOB out_output_buffer;
163 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
165 uint8_t *buf, size_t len);
166 static void smbd_smb2_notify_reply_trigger(struct tevent_context *ctx,
167 struct tevent_immediate *im,
170 static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
171 struct tevent_context *ev,
172 struct smbd_smb2_request *smb2req,
174 uint32_t in_output_buffer_length,
175 uint64_t in_file_id_volatile,
176 uint64_t in_completion_filter)
178 struct tevent_req *req;
179 struct smbd_smb2_notify_state *state;
180 struct smb_request *smbreq;
181 connection_struct *conn = smb2req->tcon->compat_conn;
183 bool recursive = (in_flags & 0x0001) ? true : false;
186 req = tevent_req_create(mem_ctx, &state,
187 struct smbd_smb2_notify_state);
191 state->smb2req = smb2req;
192 state->status = NT_STATUS_INTERNAL_ERROR;
193 state->out_output_buffer = data_blob_null;
196 DEBUG(10,("smbd_smb2_notify_send: file_id[0x%016llX]\n",
197 (unsigned long long)in_file_id_volatile));
199 smbreq = smbd_smb2_fake_smb_request(smb2req);
200 if (tevent_req_nomem(smbreq, req)) {
201 return tevent_req_post(req, ev);
204 smbreq->async_priv = (void *)req;
206 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
208 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
209 return tevent_req_post(req, ev);
211 if (conn != fsp->conn) {
212 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
213 return tevent_req_post(req, ev);
215 if (smb2req->session->vuid != fsp->vuid) {
216 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
217 return tevent_req_post(req, ev);
223 filter_string = notify_filter_string(NULL, in_completion_filter);
224 if (tevent_req_nomem(filter_string, req)) {
225 return tevent_req_post(req, ev);
228 DEBUG(3,("smbd_smb2_notify_send: notify change "
229 "called on %s, filter = %s, recursive = %d\n",
230 fsp_str_dbg(fsp), filter_string, recursive));
232 TALLOC_FREE(filter_string);
235 if ((!fsp->is_directory) || (conn != fsp->conn)) {
236 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
237 return tevent_req_post(req, ev);
240 if (fsp->notify == NULL) {
242 status = change_notify_create(fsp,
243 in_completion_filter,
245 if (!NT_STATUS_IS_OK(status)) {
246 DEBUG(10, ("change_notify_create returned %s\n",
248 tevent_req_nterror(req, status);
249 return tevent_req_post(req, ev);
253 if (fsp->notify->num_changes != 0) {
256 * We've got changes pending, respond immediately
260 * TODO: write a torture test to check the filtering behaviour
264 change_notify_reply(fsp->conn, smbreq,
266 in_output_buffer_length,
268 smbd_smb2_notify_reply);
271 * change_notify_reply() above has independently
272 * called tevent_req_done().
274 return tevent_req_post(req, ev);
277 state->im = tevent_create_immediate(state);
278 if (tevent_req_nomem(state->im, req)) {
279 return tevent_req_post(req, ev);
283 * No changes pending, queue the request
286 status = change_notify_add_request(smbreq,
287 in_output_buffer_length,
288 in_completion_filter,
290 smbd_smb2_notify_reply);
291 if (!NT_STATUS_IS_OK(status)) {
292 tevent_req_nterror(req, status);
293 return tevent_req_post(req, ev);
299 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
301 uint8_t *buf, size_t len)
303 struct tevent_req *req = talloc_get_type_abort(smbreq->async_priv,
305 struct smbd_smb2_notify_state *state = tevent_req_data(req,
306 struct smbd_smb2_notify_state);
308 state->status = error_code;
309 if (!NT_STATUS_IS_OK(error_code)) {
311 } else if (len == 0) {
312 state->status = STATUS_NOTIFY_ENUM_DIR;
314 state->out_output_buffer = data_blob_talloc(state, buf, len);
315 if (state->out_output_buffer.data == NULL) {
316 state->status = NT_STATUS_NO_MEMORY;
320 if (state->im == NULL) {
321 smbd_smb2_notify_reply_trigger(NULL, NULL, req);
326 * if this is called async, we need to go via an immediate event
327 * because the caller replies on the smb_request (a child of req
328 * being arround after calling this function
330 tevent_schedule_immediate(state->im,
331 state->smb2req->sconn->smb2.event_ctx,
332 smbd_smb2_notify_reply_trigger,
336 static void smbd_smb2_notify_reply_trigger(struct tevent_context *ctx,
337 struct tevent_immediate *im,
340 struct tevent_req *req = talloc_get_type_abort(private_data,
342 struct smbd_smb2_notify_state *state = tevent_req_data(req,
343 struct smbd_smb2_notify_state);
345 if (!NT_STATUS_IS_OK(state->status)) {
346 tevent_req_nterror(req, state->status);
350 tevent_req_done(req);
353 static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
355 DATA_BLOB *out_output_buffer)
358 struct smbd_smb2_notify_state *state = tevent_req_data(req,
359 struct smbd_smb2_notify_state);
361 if (tevent_req_is_nterror(req, &status)) {
362 tevent_req_received(req);
366 *out_output_buffer = state->out_output_buffer;
367 talloc_steal(mem_ctx, out_output_buffer->data);
369 tevent_req_received(req);