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 status
= smbd_smb2_request_verify_creditcharge(req
,
81 in_output_buffer_length
);
83 if (!NT_STATUS_IS_OK(status
)) {
84 return smbd_smb2_request_error(req
, status
);
87 if (req
->compat_chain_fsp
) {
89 } else if (in_file_id_persistent
!= in_file_id_volatile
) {
90 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
93 subreq
= smbd_smb2_notify_send(req
,
97 in_output_buffer_length
,
99 in_completion_filter
);
100 if (subreq
== NULL
) {
101 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
103 tevent_req_set_callback(subreq
, smbd_smb2_request_notify_done
, req
);
105 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
108 static void smbd_smb2_request_notify_done(struct tevent_req
*subreq
)
110 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
111 struct smbd_smb2_request
);
112 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 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x08);
155 if (outbody
.data
== NULL
) {
156 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
157 if (!NT_STATUS_IS_OK(error
)) {
158 smbd_server_connection_terminate(req
->sconn
,
165 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
166 SSVAL(outbody
.data
, 0x02,
167 out_output_buffer_offset
); /* output buffer offset */
168 SIVAL(outbody
.data
, 0x04,
169 out_output_buffer
.length
); /* output buffer length */
171 outdyn
= out_output_buffer
;
173 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
174 if (!NT_STATUS_IS_OK(error
)) {
175 smbd_server_connection_terminate(req
->sconn
,
181 static void smbd_smb2_notify_reply(struct smb_request
*smbreq
,
183 uint8_t *buf
, size_t len
);
184 static void smbd_smb2_notify_reply_trigger(struct tevent_context
*ctx
,
185 struct tevent_immediate
*im
,
187 static bool smbd_smb2_notify_cancel(struct tevent_req
*req
);
189 static struct tevent_req
*smbd_smb2_notify_send(TALLOC_CTX
*mem_ctx
,
190 struct tevent_context
*ev
,
191 struct smbd_smb2_request
*smb2req
,
193 uint32_t in_output_buffer_length
,
194 uint64_t in_file_id_volatile
,
195 uint64_t in_completion_filter
)
197 struct tevent_req
*req
;
198 struct smbd_smb2_notify_state
*state
;
199 struct smb_request
*smbreq
;
200 connection_struct
*conn
= smb2req
->tcon
->compat_conn
;
202 bool recursive
= (in_flags
& SMB2_WATCH_TREE
) ? true : false;
205 req
= tevent_req_create(mem_ctx
, &state
,
206 struct smbd_smb2_notify_state
);
210 state
->smb2req
= smb2req
;
211 state
->status
= NT_STATUS_INTERNAL_ERROR
;
212 state
->out_output_buffer
= data_blob_null
;
215 DEBUG(10,("smbd_smb2_notify_send: file_id[0x%016llX]\n",
216 (unsigned long long)in_file_id_volatile
));
218 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
219 if (tevent_req_nomem(smbreq
, req
)) {
220 return tevent_req_post(req
, ev
);
223 state
->smbreq
= smbreq
;
224 smbreq
->async_priv
= (void *)req
;
226 fsp
= file_fsp(smbreq
, (uint16_t)in_file_id_volatile
);
228 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
229 return tevent_req_post(req
, ev
);
231 if (conn
!= fsp
->conn
) {
232 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
233 return tevent_req_post(req
, ev
);
235 if (smb2req
->session
->vuid
!= fsp
->vuid
) {
236 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
237 return tevent_req_post(req
, ev
);
243 filter_string
= notify_filter_string(NULL
, in_completion_filter
);
244 if (tevent_req_nomem(filter_string
, req
)) {
245 return tevent_req_post(req
, ev
);
248 DEBUG(3,("smbd_smb2_notify_send: notify change "
249 "called on %s, filter = %s, recursive = %d\n",
250 fsp_str_dbg(fsp
), filter_string
, recursive
));
252 TALLOC_FREE(filter_string
);
255 if ((!fsp
->is_directory
) || (conn
!= fsp
->conn
)) {
256 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
257 return tevent_req_post(req
, ev
);
260 if (fsp
->notify
== NULL
) {
262 status
= change_notify_create(fsp
,
263 in_completion_filter
,
265 if (!NT_STATUS_IS_OK(status
)) {
266 DEBUG(10, ("change_notify_create returned %s\n",
268 tevent_req_nterror(req
, status
);
269 return tevent_req_post(req
, ev
);
273 if (change_notify_fsp_has_changes(fsp
)) {
276 * We've got changes pending, respond immediately
280 * TODO: write a torture test to check the filtering behaviour
284 change_notify_reply(smbreq
,
286 in_output_buffer_length
,
288 smbd_smb2_notify_reply
);
291 * change_notify_reply() above has independently
292 * called tevent_req_done().
294 return tevent_req_post(req
, ev
);
297 state
->im
= tevent_create_immediate(state
);
298 if (tevent_req_nomem(state
->im
, req
)) {
299 return tevent_req_post(req
, ev
);
303 * No changes pending, queue the request
306 status
= change_notify_add_request(smbreq
,
307 in_output_buffer_length
,
308 in_completion_filter
,
310 smbd_smb2_notify_reply
);
311 if (!NT_STATUS_IS_OK(status
)) {
312 tevent_req_nterror(req
, status
);
313 return tevent_req_post(req
, ev
);
316 /* allow this request to be canceled */
317 tevent_req_set_cancel_fn(req
, smbd_smb2_notify_cancel
);
322 static void smbd_smb2_notify_reply(struct smb_request
*smbreq
,
324 uint8_t *buf
, size_t len
)
326 struct tevent_req
*req
= talloc_get_type_abort(smbreq
->async_priv
,
328 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
329 struct smbd_smb2_notify_state
);
331 state
->status
= error_code
;
332 if (!NT_STATUS_IS_OK(error_code
)) {
334 } else if (len
== 0) {
335 state
->status
= STATUS_NOTIFY_ENUM_DIR
;
337 state
->out_output_buffer
= data_blob_talloc(state
, buf
, len
);
338 if (state
->out_output_buffer
.data
== NULL
) {
339 state
->status
= NT_STATUS_NO_MEMORY
;
343 if (state
->im
== NULL
) {
344 smbd_smb2_notify_reply_trigger(NULL
, NULL
, req
);
349 * if this is called async, we need to go via an immediate event
350 * because the caller replies on the smb_request (a child of req
351 * being arround after calling this function
353 tevent_schedule_immediate(state
->im
,
354 state
->smb2req
->sconn
->ev_ctx
,
355 smbd_smb2_notify_reply_trigger
,
359 static void smbd_smb2_notify_reply_trigger(struct tevent_context
*ctx
,
360 struct tevent_immediate
*im
,
363 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
365 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
366 struct smbd_smb2_notify_state
);
368 if (!NT_STATUS_IS_OK(state
->status
)) {
369 tevent_req_nterror(req
, state
->status
);
373 tevent_req_done(req
);
376 static bool smbd_smb2_notify_cancel(struct tevent_req
*req
)
378 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
379 struct smbd_smb2_notify_state
);
381 smbd_notify_cancel_by_smbreq(state
->smbreq
);
383 state
->smb2req
->cancelled
= true;
384 tevent_req_done(req
);
388 static NTSTATUS
smbd_smb2_notify_recv(struct tevent_req
*req
,
390 DATA_BLOB
*out_output_buffer
)
393 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
394 struct smbd_smb2_notify_state
);
396 if (tevent_req_is_nterror(req
, &status
)) {
397 tevent_req_received(req
);
401 *out_output_buffer
= state
->out_output_buffer
;
402 talloc_steal(mem_ctx
, out_output_buffer
->data
);
404 tevent_req_received(req
);