2 Unix SMB/CIFS implementation.
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/>.
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
;
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
,
40 uint32_t in_output_buffer_length
,
41 uint64_t in_file_id_volatile
,
42 uint64_t in_completion_filter
);
43 static NTSTATUS
smbd_smb2_notify_recv(struct tevent_req
*req
,
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
)
51 const uint8_t *inbody
;
52 int i
= req
->current_idx
;
53 size_t expected_body_size
= 0x20;
56 uint32_t in_output_buffer_length
;
57 uint64_t in_file_id_persistent
;
58 uint64_t in_file_id_volatile
;
59 uint64_t in_completion_filter
;
60 struct tevent_req
*subreq
;
62 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
63 if (req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
64 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
67 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
69 body_size
= SVAL(inbody
, 0x00);
70 if (body_size
!= expected_body_size
) {
71 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
74 in_flags
= SVAL(inbody
, 0x02);
75 in_output_buffer_length
= IVAL(inbody
, 0x04);
76 in_file_id_persistent
= BVAL(inbody
, 0x08);
77 in_file_id_volatile
= BVAL(inbody
, 0x10);
78 in_completion_filter
= IVAL(inbody
, 0x18);
81 * 0x00010000 is what Windows 7 uses,
82 * Windows 2008 uses 0x00080000
84 if (in_output_buffer_length
> lp_smb2_max_trans()) {
85 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
88 if (req
->compat_chain_fsp
) {
90 } else if (in_file_id_persistent
!= in_file_id_volatile
) {
91 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
94 subreq
= smbd_smb2_notify_send(req
,
95 req
->sconn
->smb2
.event_ctx
,
98 in_output_buffer_length
,
100 in_completion_filter
);
101 if (subreq
== NULL
) {
102 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
104 tevent_req_set_callback(subreq
, smbd_smb2_request_notify_done
, req
);
106 return smbd_smb2_request_pending_queue(req
, subreq
);
109 static void smbd_smb2_request_notify_done(struct tevent_req
*subreq
)
111 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
112 struct smbd_smb2_request
);
113 int i
= req
->current_idx
;
117 uint16_t out_output_buffer_offset
;
118 DATA_BLOB out_output_buffer
= data_blob_null
;
120 NTSTATUS error
; /* transport error */
122 if (req
->cancelled
) {
123 struct smbd_smb2_notify_state
*state
= tevent_req_data(subreq
,
124 struct smbd_smb2_notify_state
);
125 const uint8_t *inhdr
= (const uint8_t *)req
->in
.vector
[i
].iov_base
;
126 uint64_t mid
= BVAL(inhdr
, SMB2_HDR_MESSAGE_ID
);
128 DEBUG(10,("smbd_smb2_request_notify_done: cancelled mid %llu\n",
129 (unsigned long long)mid
));
130 error
= smbd_smb2_request_error(req
, NT_STATUS_CANCELLED
);
131 if (!NT_STATUS_IS_OK(error
)) {
132 smbd_server_connection_terminate(req
->sconn
,
136 TALLOC_FREE(state
->im
);
140 status
= smbd_smb2_notify_recv(subreq
,
144 if (!NT_STATUS_IS_OK(status
)) {
145 error
= smbd_smb2_request_error(req
, status
);
146 if (!NT_STATUS_IS_OK(error
)) {
147 smbd_server_connection_terminate(req
->sconn
,
154 out_output_buffer_offset
= SMB2_HDR_BODY
+ 0x08;
156 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
158 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x08);
159 if (outbody
.data
== NULL
) {
160 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
161 if (!NT_STATUS_IS_OK(error
)) {
162 smbd_server_connection_terminate(req
->sconn
,
169 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
170 SSVAL(outbody
.data
, 0x02,
171 out_output_buffer_offset
); /* output buffer offset */
172 SIVAL(outbody
.data
, 0x04,
173 out_output_buffer
.length
); /* output buffer length */
175 outdyn
= out_output_buffer
;
177 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
178 if (!NT_STATUS_IS_OK(error
)) {
179 smbd_server_connection_terminate(req
->sconn
,
185 static void smbd_smb2_notify_reply(struct smb_request
*smbreq
,
187 uint8_t *buf
, size_t len
);
188 static void smbd_smb2_notify_reply_trigger(struct tevent_context
*ctx
,
189 struct tevent_immediate
*im
,
191 static bool smbd_smb2_notify_cancel(struct tevent_req
*req
);
193 static struct tevent_req
*smbd_smb2_notify_send(TALLOC_CTX
*mem_ctx
,
194 struct tevent_context
*ev
,
195 struct smbd_smb2_request
*smb2req
,
197 uint32_t in_output_buffer_length
,
198 uint64_t in_file_id_volatile
,
199 uint64_t in_completion_filter
)
201 struct tevent_req
*req
;
202 struct smbd_smb2_notify_state
*state
;
203 struct smb_request
*smbreq
;
204 connection_struct
*conn
= smb2req
->tcon
->compat_conn
;
206 bool recursive
= (in_flags
& 0x0001) ? true : false;
209 req
= tevent_req_create(mem_ctx
, &state
,
210 struct smbd_smb2_notify_state
);
214 state
->smb2req
= smb2req
;
215 state
->status
= NT_STATUS_INTERNAL_ERROR
;
216 state
->out_output_buffer
= data_blob_null
;
219 DEBUG(10,("smbd_smb2_notify_send: file_id[0x%016llX]\n",
220 (unsigned long long)in_file_id_volatile
));
222 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
223 if (tevent_req_nomem(smbreq
, req
)) {
224 return tevent_req_post(req
, ev
);
227 state
->smbreq
= smbreq
;
228 smbreq
->async_priv
= (void *)req
;
230 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
232 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
233 return tevent_req_post(req
, ev
);
235 if (conn
!= fsp
->conn
) {
236 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
237 return tevent_req_post(req
, ev
);
239 if (smb2req
->session
->vuid
!= fsp
->vuid
) {
240 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
241 return tevent_req_post(req
, ev
);
247 filter_string
= notify_filter_string(NULL
, in_completion_filter
);
248 if (tevent_req_nomem(filter_string
, req
)) {
249 return tevent_req_post(req
, ev
);
252 DEBUG(3,("smbd_smb2_notify_send: notify change "
253 "called on %s, filter = %s, recursive = %d\n",
254 fsp_str_dbg(fsp
), filter_string
, recursive
));
256 TALLOC_FREE(filter_string
);
259 if ((!fsp
->is_directory
) || (conn
!= fsp
->conn
)) {
260 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
261 return tevent_req_post(req
, ev
);
264 if (fsp
->notify
== NULL
) {
266 status
= change_notify_create(fsp
,
267 in_completion_filter
,
269 if (!NT_STATUS_IS_OK(status
)) {
270 DEBUG(10, ("change_notify_create returned %s\n",
272 tevent_req_nterror(req
, status
);
273 return tevent_req_post(req
, ev
);
277 if (fsp
->notify
->num_changes
!= 0) {
280 * We've got changes pending, respond immediately
284 * TODO: write a torture test to check the filtering behaviour
288 change_notify_reply(smbreq
,
290 in_output_buffer_length
,
292 smbd_smb2_notify_reply
);
295 * change_notify_reply() above has independently
296 * called tevent_req_done().
298 return tevent_req_post(req
, ev
);
301 state
->im
= tevent_create_immediate(state
);
302 if (tevent_req_nomem(state
->im
, req
)) {
303 return tevent_req_post(req
, ev
);
307 * No changes pending, queue the request
310 status
= change_notify_add_request(smbreq
,
311 in_output_buffer_length
,
312 in_completion_filter
,
314 smbd_smb2_notify_reply
);
315 if (!NT_STATUS_IS_OK(status
)) {
316 tevent_req_nterror(req
, status
);
317 return tevent_req_post(req
, ev
);
320 /* allow this request to be canceled */
321 tevent_req_set_cancel_fn(req
, smbd_smb2_notify_cancel
);
326 static void smbd_smb2_notify_reply(struct smb_request
*smbreq
,
328 uint8_t *buf
, size_t len
)
330 struct tevent_req
*req
= talloc_get_type_abort(smbreq
->async_priv
,
332 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
333 struct smbd_smb2_notify_state
);
335 state
->status
= error_code
;
336 if (!NT_STATUS_IS_OK(error_code
)) {
338 } else if (len
== 0) {
339 state
->status
= STATUS_NOTIFY_ENUM_DIR
;
341 state
->out_output_buffer
= data_blob_talloc(state
, buf
, len
);
342 if (state
->out_output_buffer
.data
== NULL
) {
343 state
->status
= NT_STATUS_NO_MEMORY
;
347 if (state
->im
== NULL
) {
348 smbd_smb2_notify_reply_trigger(NULL
, NULL
, req
);
353 * if this is called async, we need to go via an immediate event
354 * because the caller replies on the smb_request (a child of req
355 * being arround after calling this function
357 tevent_schedule_immediate(state
->im
,
358 state
->smb2req
->sconn
->smb2
.event_ctx
,
359 smbd_smb2_notify_reply_trigger
,
363 static void smbd_smb2_notify_reply_trigger(struct tevent_context
*ctx
,
364 struct tevent_immediate
*im
,
367 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
369 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
370 struct smbd_smb2_notify_state
);
372 if (!NT_STATUS_IS_OK(state
->status
)) {
373 tevent_req_nterror(req
, state
->status
);
377 tevent_req_done(req
);
380 static bool smbd_smb2_notify_cancel(struct tevent_req
*req
)
382 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
383 struct smbd_smb2_notify_state
);
385 smbd_notify_cancel_by_smbreq(state
->smbreq
);
387 state
->smb2req
->cancelled
= true;
388 tevent_req_done(req
);
392 static NTSTATUS
smbd_smb2_notify_recv(struct tevent_req
*req
,
394 DATA_BLOB
*out_output_buffer
)
397 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
398 struct smbd_smb2_notify_state
);
400 if (tevent_req_is_nterror(req
, &status
)) {
401 tevent_req_received(req
);
405 *out_output_buffer
= state
->out_output_buffer
;
406 talloc_steal(mem_ctx
, out_output_buffer
->data
);
408 tevent_req_received(req
);