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
;
54 uint32_t in_output_buffer_length
;
55 uint64_t in_file_id_persistent
;
56 uint64_t in_file_id_volatile
;
57 uint64_t in_completion_filter
;
58 struct tevent_req
*subreq
;
60 status
= smbd_smb2_request_verify_sizes(req
, 0x20);
61 if (!NT_STATUS_IS_OK(status
)) {
62 return smbd_smb2_request_error(req
, status
);
64 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
66 in_flags
= SVAL(inbody
, 0x02);
67 in_output_buffer_length
= IVAL(inbody
, 0x04);
68 in_file_id_persistent
= BVAL(inbody
, 0x08);
69 in_file_id_volatile
= BVAL(inbody
, 0x10);
70 in_completion_filter
= IVAL(inbody
, 0x18);
73 * 0x00010000 is what Windows 7 uses,
74 * Windows 2008 uses 0x00080000
76 if (in_output_buffer_length
> req
->sconn
->smb2
.max_trans
) {
77 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
80 if (req
->compat_chain_fsp
) {
82 } else if (in_file_id_persistent
!= in_file_id_volatile
) {
83 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
86 subreq
= smbd_smb2_notify_send(req
,
90 in_output_buffer_length
,
92 in_completion_filter
);
94 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
96 tevent_req_set_callback(subreq
, smbd_smb2_request_notify_done
, req
);
98 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
101 static void smbd_smb2_request_notify_done(struct tevent_req
*subreq
)
103 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
104 struct smbd_smb2_request
);
105 int i
= req
->current_idx
;
108 uint16_t out_output_buffer_offset
;
109 DATA_BLOB out_output_buffer
= data_blob_null
;
111 NTSTATUS error
; /* transport error */
113 if (req
->cancelled
) {
114 struct smbd_smb2_notify_state
*state
= tevent_req_data(subreq
,
115 struct smbd_smb2_notify_state
);
116 const uint8_t *inhdr
= (const uint8_t *)req
->in
.vector
[i
].iov_base
;
117 uint64_t mid
= BVAL(inhdr
, SMB2_HDR_MESSAGE_ID
);
119 DEBUG(10,("smbd_smb2_request_notify_done: cancelled mid %llu\n",
120 (unsigned long long)mid
));
121 error
= smbd_smb2_request_error(req
, NT_STATUS_CANCELLED
);
122 if (!NT_STATUS_IS_OK(error
)) {
123 smbd_server_connection_terminate(req
->sconn
,
127 TALLOC_FREE(state
->im
);
131 status
= smbd_smb2_notify_recv(subreq
,
135 if (!NT_STATUS_IS_OK(status
)) {
136 error
= smbd_smb2_request_error(req
, status
);
137 if (!NT_STATUS_IS_OK(error
)) {
138 smbd_server_connection_terminate(req
->sconn
,
145 out_output_buffer_offset
= SMB2_HDR_BODY
+ 0x08;
147 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x08);
148 if (outbody
.data
== NULL
) {
149 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
150 if (!NT_STATUS_IS_OK(error
)) {
151 smbd_server_connection_terminate(req
->sconn
,
158 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
159 SSVAL(outbody
.data
, 0x02,
160 out_output_buffer_offset
); /* output buffer offset */
161 SIVAL(outbody
.data
, 0x04,
162 out_output_buffer
.length
); /* output buffer length */
164 outdyn
= out_output_buffer
;
166 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
167 if (!NT_STATUS_IS_OK(error
)) {
168 smbd_server_connection_terminate(req
->sconn
,
174 static void smbd_smb2_notify_reply(struct smb_request
*smbreq
,
176 uint8_t *buf
, size_t len
);
177 static void smbd_smb2_notify_reply_trigger(struct tevent_context
*ctx
,
178 struct tevent_immediate
*im
,
180 static bool smbd_smb2_notify_cancel(struct tevent_req
*req
);
182 static struct tevent_req
*smbd_smb2_notify_send(TALLOC_CTX
*mem_ctx
,
183 struct tevent_context
*ev
,
184 struct smbd_smb2_request
*smb2req
,
186 uint32_t in_output_buffer_length
,
187 uint64_t in_file_id_volatile
,
188 uint64_t in_completion_filter
)
190 struct tevent_req
*req
;
191 struct smbd_smb2_notify_state
*state
;
192 struct smb_request
*smbreq
;
193 connection_struct
*conn
= smb2req
->tcon
->compat_conn
;
195 bool recursive
= (in_flags
& SMB2_WATCH_TREE
) ? true : false;
198 req
= tevent_req_create(mem_ctx
, &state
,
199 struct smbd_smb2_notify_state
);
203 state
->smb2req
= smb2req
;
204 state
->status
= NT_STATUS_INTERNAL_ERROR
;
205 state
->out_output_buffer
= data_blob_null
;
208 DEBUG(10,("smbd_smb2_notify_send: file_id[0x%016llX]\n",
209 (unsigned long long)in_file_id_volatile
));
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
;
219 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
221 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
222 return tevent_req_post(req
, ev
);
224 if (conn
!= fsp
->conn
) {
225 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
226 return tevent_req_post(req
, ev
);
228 if (smb2req
->session
->vuid
!= fsp
->vuid
) {
229 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
230 return tevent_req_post(req
, ev
);
236 filter_string
= notify_filter_string(NULL
, in_completion_filter
);
237 if (tevent_req_nomem(filter_string
, req
)) {
238 return tevent_req_post(req
, ev
);
241 DEBUG(3,("smbd_smb2_notify_send: notify change "
242 "called on %s, filter = %s, recursive = %d\n",
243 fsp_str_dbg(fsp
), filter_string
, recursive
));
245 TALLOC_FREE(filter_string
);
248 if ((!fsp
->is_directory
) || (conn
!= fsp
->conn
)) {
249 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
250 return tevent_req_post(req
, ev
);
253 if (fsp
->notify
== NULL
) {
255 status
= change_notify_create(fsp
,
256 in_completion_filter
,
258 if (!NT_STATUS_IS_OK(status
)) {
259 DEBUG(10, ("change_notify_create returned %s\n",
261 tevent_req_nterror(req
, status
);
262 return tevent_req_post(req
, ev
);
266 if (fsp
->notify
->num_changes
!= 0) {
269 * We've got changes pending, respond immediately
273 * TODO: write a torture test to check the filtering behaviour
277 change_notify_reply(smbreq
,
279 in_output_buffer_length
,
281 smbd_smb2_notify_reply
);
284 * change_notify_reply() above has independently
285 * called tevent_req_done().
287 return tevent_req_post(req
, ev
);
290 state
->im
= tevent_create_immediate(state
);
291 if (tevent_req_nomem(state
->im
, req
)) {
292 return tevent_req_post(req
, ev
);
296 * No changes pending, queue the request
299 status
= change_notify_add_request(smbreq
,
300 in_output_buffer_length
,
301 in_completion_filter
,
303 smbd_smb2_notify_reply
);
304 if (!NT_STATUS_IS_OK(status
)) {
305 tevent_req_nterror(req
, status
);
306 return tevent_req_post(req
, ev
);
309 /* allow this request to be canceled */
310 tevent_req_set_cancel_fn(req
, smbd_smb2_notify_cancel
);
315 static void smbd_smb2_notify_reply(struct smb_request
*smbreq
,
317 uint8_t *buf
, size_t len
)
319 struct tevent_req
*req
= talloc_get_type_abort(smbreq
->async_priv
,
321 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
322 struct smbd_smb2_notify_state
);
324 state
->status
= error_code
;
325 if (!NT_STATUS_IS_OK(error_code
)) {
327 } else if (len
== 0) {
328 state
->status
= STATUS_NOTIFY_ENUM_DIR
;
330 state
->out_output_buffer
= data_blob_talloc(state
, buf
, len
);
331 if (state
->out_output_buffer
.data
== NULL
) {
332 state
->status
= NT_STATUS_NO_MEMORY
;
336 if (state
->im
== NULL
) {
337 smbd_smb2_notify_reply_trigger(NULL
, NULL
, req
);
342 * if this is called async, we need to go via an immediate event
343 * because the caller replies on the smb_request (a child of req
344 * being arround after calling this function
346 tevent_schedule_immediate(state
->im
,
347 state
->smb2req
->sconn
->ev_ctx
,
348 smbd_smb2_notify_reply_trigger
,
352 static void smbd_smb2_notify_reply_trigger(struct tevent_context
*ctx
,
353 struct tevent_immediate
*im
,
356 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
358 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
359 struct smbd_smb2_notify_state
);
361 if (!NT_STATUS_IS_OK(state
->status
)) {
362 tevent_req_nterror(req
, state
->status
);
366 tevent_req_done(req
);
369 static bool smbd_smb2_notify_cancel(struct tevent_req
*req
)
371 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
372 struct smbd_smb2_notify_state
);
374 smbd_notify_cancel_by_smbreq(state
->smbreq
);
376 state
->smb2req
->cancelled
= true;
377 tevent_req_done(req
);
381 static NTSTATUS
smbd_smb2_notify_recv(struct tevent_req
*req
,
383 DATA_BLOB
*out_output_buffer
)
386 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
387 struct smbd_smb2_notify_state
);
389 if (tevent_req_is_nterror(req
, &status
)) {
390 tevent_req_received(req
);
394 *out_output_buffer
= state
->out_output_buffer
;
395 talloc_steal(mem_ctx
, out_output_buffer
->data
);
397 tevent_req_received(req
);