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/globals.h"
24 #include "../libcli/smb/smb_common.h"
26 struct smbd_smb2_notify_state
{
27 struct smbd_smb2_request
*smb2req
;
28 struct smb_request
*smbreq
;
29 struct tevent_immediate
*im
;
31 DATA_BLOB out_output_buffer
;
34 static struct tevent_req
*smbd_smb2_notify_send(TALLOC_CTX
*mem_ctx
,
35 struct tevent_context
*ev
,
36 struct smbd_smb2_request
*smb2req
,
38 uint32_t in_output_buffer_length
,
39 uint64_t in_file_id_volatile
,
40 uint64_t in_completion_filter
);
41 static NTSTATUS
smbd_smb2_notify_recv(struct tevent_req
*req
,
43 DATA_BLOB
*out_output_buffer
);
45 static void smbd_smb2_request_notify_done(struct tevent_req
*subreq
);
46 NTSTATUS
smbd_smb2_request_process_notify(struct smbd_smb2_request
*req
)
49 const uint8_t *inbody
;
50 int i
= req
->current_idx
;
51 size_t expected_body_size
= 0x20;
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 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
61 if (req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
62 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
65 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
67 body_size
= SVAL(inbody
, 0x00);
68 if (body_size
!= expected_body_size
) {
69 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
72 in_flags
= SVAL(inbody
, 0x02);
73 in_output_buffer_length
= IVAL(inbody
, 0x04);
74 in_file_id_persistent
= BVAL(inbody
, 0x08);
75 in_file_id_volatile
= BVAL(inbody
, 0x10);
76 in_completion_filter
= IVAL(inbody
, 0x18);
79 * 0x00010000 is what Windows 7 uses,
80 * Windows 2008 uses 0x00080000
82 if (in_output_buffer_length
> lp_smb2_max_trans()) {
83 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
86 if (req
->compat_chain_fsp
) {
88 } else if (in_file_id_persistent
!= in_file_id_volatile
) {
89 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
92 subreq
= smbd_smb2_notify_send(req
,
93 req
->sconn
->smb2
.event_ctx
,
96 in_output_buffer_length
,
98 in_completion_filter
);
100 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
102 tevent_req_set_callback(subreq
, smbd_smb2_request_notify_done
, req
);
104 return smbd_smb2_request_pending_queue(req
, subreq
);
107 static void smbd_smb2_request_notify_done(struct tevent_req
*subreq
)
109 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
110 struct smbd_smb2_request
);
111 int i
= req
->current_idx
;
115 uint16_t out_output_buffer_offset
;
116 DATA_BLOB out_output_buffer
= data_blob_null
;
118 NTSTATUS error
; /* transport error */
120 if (req
->cancelled
) {
121 struct smbd_smb2_notify_state
*state
= tevent_req_data(subreq
,
122 struct smbd_smb2_notify_state
);
123 const uint8_t *inhdr
= (const uint8_t *)req
->in
.vector
[i
].iov_base
;
124 uint64_t mid
= BVAL(inhdr
, SMB2_HDR_MESSAGE_ID
);
126 DEBUG(10,("smbd_smb2_request_notify_done: cancelled mid %llu\n",
127 (unsigned long long)mid
));
128 error
= smbd_smb2_request_error(req
, NT_STATUS_CANCELLED
);
129 if (!NT_STATUS_IS_OK(error
)) {
130 smbd_server_connection_terminate(req
->sconn
,
134 TALLOC_FREE(state
->im
);
138 status
= smbd_smb2_notify_recv(subreq
,
142 if (!NT_STATUS_IS_OK(status
)) {
143 error
= smbd_smb2_request_error(req
, status
);
144 if (!NT_STATUS_IS_OK(error
)) {
145 smbd_server_connection_terminate(req
->sconn
,
152 out_output_buffer_offset
= SMB2_HDR_BODY
+ 0x08;
154 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
156 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x08);
157 if (outbody
.data
== NULL
) {
158 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
159 if (!NT_STATUS_IS_OK(error
)) {
160 smbd_server_connection_terminate(req
->sconn
,
167 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
168 SSVAL(outbody
.data
, 0x02,
169 out_output_buffer_offset
); /* output buffer offset */
170 SIVAL(outbody
.data
, 0x04,
171 out_output_buffer
.length
); /* output buffer length */
173 outdyn
= out_output_buffer
;
175 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
176 if (!NT_STATUS_IS_OK(error
)) {
177 smbd_server_connection_terminate(req
->sconn
,
183 static void smbd_smb2_notify_reply(struct smb_request
*smbreq
,
185 uint8_t *buf
, size_t len
);
186 static void smbd_smb2_notify_reply_trigger(struct tevent_context
*ctx
,
187 struct tevent_immediate
*im
,
189 static bool smbd_smb2_notify_cancel(struct tevent_req
*req
);
191 static struct tevent_req
*smbd_smb2_notify_send(TALLOC_CTX
*mem_ctx
,
192 struct tevent_context
*ev
,
193 struct smbd_smb2_request
*smb2req
,
195 uint32_t in_output_buffer_length
,
196 uint64_t in_file_id_volatile
,
197 uint64_t in_completion_filter
)
199 struct tevent_req
*req
;
200 struct smbd_smb2_notify_state
*state
;
201 struct smb_request
*smbreq
;
202 connection_struct
*conn
= smb2req
->tcon
->compat_conn
;
204 bool recursive
= (in_flags
& 0x0001) ? true : false;
207 req
= tevent_req_create(mem_ctx
, &state
,
208 struct smbd_smb2_notify_state
);
212 state
->smb2req
= smb2req
;
213 state
->status
= NT_STATUS_INTERNAL_ERROR
;
214 state
->out_output_buffer
= data_blob_null
;
217 DEBUG(10,("smbd_smb2_notify_send: file_id[0x%016llX]\n",
218 (unsigned long long)in_file_id_volatile
));
220 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
221 if (tevent_req_nomem(smbreq
, req
)) {
222 return tevent_req_post(req
, ev
);
225 state
->smbreq
= smbreq
;
226 smbreq
->async_priv
= (void *)req
;
228 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
230 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
231 return tevent_req_post(req
, ev
);
233 if (conn
!= fsp
->conn
) {
234 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
235 return tevent_req_post(req
, ev
);
237 if (smb2req
->session
->vuid
!= fsp
->vuid
) {
238 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
239 return tevent_req_post(req
, ev
);
245 filter_string
= notify_filter_string(NULL
, in_completion_filter
);
246 if (tevent_req_nomem(filter_string
, req
)) {
247 return tevent_req_post(req
, ev
);
250 DEBUG(3,("smbd_smb2_notify_send: notify change "
251 "called on %s, filter = %s, recursive = %d\n",
252 fsp_str_dbg(fsp
), filter_string
, recursive
));
254 TALLOC_FREE(filter_string
);
257 if ((!fsp
->is_directory
) || (conn
!= fsp
->conn
)) {
258 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
259 return tevent_req_post(req
, ev
);
262 if (fsp
->notify
== NULL
) {
264 status
= change_notify_create(fsp
,
265 in_completion_filter
,
267 if (!NT_STATUS_IS_OK(status
)) {
268 DEBUG(10, ("change_notify_create returned %s\n",
270 tevent_req_nterror(req
, status
);
271 return tevent_req_post(req
, ev
);
275 if (fsp
->notify
->num_changes
!= 0) {
278 * We've got changes pending, respond immediately
282 * TODO: write a torture test to check the filtering behaviour
286 change_notify_reply(smbreq
,
288 in_output_buffer_length
,
290 smbd_smb2_notify_reply
);
293 * change_notify_reply() above has independently
294 * called tevent_req_done().
296 return tevent_req_post(req
, ev
);
299 state
->im
= tevent_create_immediate(state
);
300 if (tevent_req_nomem(state
->im
, req
)) {
301 return tevent_req_post(req
, ev
);
305 * No changes pending, queue the request
308 status
= change_notify_add_request(smbreq
,
309 in_output_buffer_length
,
310 in_completion_filter
,
312 smbd_smb2_notify_reply
);
313 if (!NT_STATUS_IS_OK(status
)) {
314 tevent_req_nterror(req
, status
);
315 return tevent_req_post(req
, ev
);
318 /* allow this request to be canceled */
319 tevent_req_set_cancel_fn(req
, smbd_smb2_notify_cancel
);
324 static void smbd_smb2_notify_reply(struct smb_request
*smbreq
,
326 uint8_t *buf
, size_t len
)
328 struct tevent_req
*req
= talloc_get_type_abort(smbreq
->async_priv
,
330 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
331 struct smbd_smb2_notify_state
);
333 state
->status
= error_code
;
334 if (!NT_STATUS_IS_OK(error_code
)) {
336 } else if (len
== 0) {
337 state
->status
= STATUS_NOTIFY_ENUM_DIR
;
339 state
->out_output_buffer
= data_blob_talloc(state
, buf
, len
);
340 if (state
->out_output_buffer
.data
== NULL
) {
341 state
->status
= NT_STATUS_NO_MEMORY
;
345 if (state
->im
== NULL
) {
346 smbd_smb2_notify_reply_trigger(NULL
, NULL
, req
);
351 * if this is called async, we need to go via an immediate event
352 * because the caller replies on the smb_request (a child of req
353 * being arround after calling this function
355 tevent_schedule_immediate(state
->im
,
356 state
->smb2req
->sconn
->smb2
.event_ctx
,
357 smbd_smb2_notify_reply_trigger
,
361 static void smbd_smb2_notify_reply_trigger(struct tevent_context
*ctx
,
362 struct tevent_immediate
*im
,
365 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
367 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
368 struct smbd_smb2_notify_state
);
370 if (!NT_STATUS_IS_OK(state
->status
)) {
371 tevent_req_nterror(req
, state
->status
);
375 tevent_req_done(req
);
378 static bool smbd_smb2_notify_cancel(struct tevent_req
*req
)
380 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
381 struct smbd_smb2_notify_state
);
383 smbd_notify_cancel_by_smbreq(state
->smbreq
);
385 state
->smb2req
->cancelled
= true;
386 tevent_req_done(req
);
390 static NTSTATUS
smbd_smb2_notify_recv(struct tevent_req
*req
,
392 DATA_BLOB
*out_output_buffer
)
395 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
396 struct smbd_smb2_notify_state
);
398 if (tevent_req_is_nterror(req
, &status
)) {
399 tevent_req_received(req
);
403 *out_output_buffer
= state
->out_output_buffer
;
404 talloc_steal(mem_ctx
, out_output_buffer
->data
);
406 tevent_req_received(req
);