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
,
39 struct files_struct
*in_fsp
,
41 uint32_t in_output_buffer_length
,
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
;
53 uint32_t in_output_buffer_length
;
54 uint64_t in_file_id_persistent
;
55 uint64_t in_file_id_volatile
;
56 struct files_struct
*in_fsp
;
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
= SMBD_SMB2_IN_BODY_PTR(req
);
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 in_fsp
= file_fsp_smb2(req
, 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
, req
->sconn
->ev_ctx
,
95 in_output_buffer_length
,
96 in_completion_filter
);
98 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
100 tevent_req_set_callback(subreq
, smbd_smb2_request_notify_done
, req
);
102 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
105 static void smbd_smb2_request_notify_done(struct tevent_req
*subreq
)
107 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
108 struct smbd_smb2_request
);
111 uint16_t out_output_buffer_offset
;
112 DATA_BLOB out_output_buffer
= data_blob_null
;
114 NTSTATUS error
; /* transport error */
116 if (req
->cancelled
) {
117 struct smbd_smb2_notify_state
*state
= tevent_req_data(subreq
,
118 struct smbd_smb2_notify_state
);
119 const uint8_t *inhdr
= SMBD_SMB2_IN_HDR_PTR(req
);
120 uint64_t mid
= BVAL(inhdr
, SMB2_HDR_MESSAGE_ID
);
122 DEBUG(10,("smbd_smb2_request_notify_done: cancelled mid %llu\n",
123 (unsigned long long)mid
));
124 error
= smbd_smb2_request_error(req
, NT_STATUS_CANCELLED
);
125 if (!NT_STATUS_IS_OK(error
)) {
126 smbd_server_connection_terminate(req
->sconn
,
130 TALLOC_FREE(state
->im
);
134 status
= smbd_smb2_notify_recv(subreq
,
138 if (!NT_STATUS_IS_OK(status
)) {
139 error
= smbd_smb2_request_error(req
, status
);
140 if (!NT_STATUS_IS_OK(error
)) {
141 smbd_server_connection_terminate(req
->sconn
,
148 out_output_buffer_offset
= SMB2_HDR_BODY
+ 0x08;
150 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x08);
151 if (outbody
.data
== NULL
) {
152 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
153 if (!NT_STATUS_IS_OK(error
)) {
154 smbd_server_connection_terminate(req
->sconn
,
161 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
162 SSVAL(outbody
.data
, 0x02,
163 out_output_buffer_offset
); /* output buffer offset */
164 SIVAL(outbody
.data
, 0x04,
165 out_output_buffer
.length
); /* output buffer length */
167 outdyn
= out_output_buffer
;
169 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
170 if (!NT_STATUS_IS_OK(error
)) {
171 smbd_server_connection_terminate(req
->sconn
,
177 static void smbd_smb2_notify_reply(struct smb_request
*smbreq
,
179 uint8_t *buf
, size_t len
);
180 static void smbd_smb2_notify_reply_trigger(struct tevent_context
*ctx
,
181 struct tevent_immediate
*im
,
183 static bool smbd_smb2_notify_cancel(struct tevent_req
*req
);
185 static struct tevent_req
*smbd_smb2_notify_send(TALLOC_CTX
*mem_ctx
,
186 struct tevent_context
*ev
,
187 struct smbd_smb2_request
*smb2req
,
188 struct files_struct
*fsp
,
190 uint32_t in_output_buffer_length
,
191 uint64_t in_completion_filter
)
193 struct tevent_req
*req
;
194 struct smbd_smb2_notify_state
*state
;
195 struct smb_request
*smbreq
;
196 connection_struct
*conn
= smb2req
->tcon
->compat
;
197 bool recursive
= (in_flags
& SMB2_WATCH_TREE
) ? true : false;
200 req
= tevent_req_create(mem_ctx
, &state
,
201 struct smbd_smb2_notify_state
);
205 state
->smb2req
= smb2req
;
206 state
->status
= NT_STATUS_INTERNAL_ERROR
;
207 state
->out_output_buffer
= data_blob_null
;
210 DEBUG(10,("smbd_smb2_notify_send: %s - %s\n",
211 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
213 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
214 if (tevent_req_nomem(smbreq
, req
)) {
215 return tevent_req_post(req
, ev
);
218 state
->smbreq
= smbreq
;
219 smbreq
->async_priv
= (void *)req
;
224 filter_string
= notify_filter_string(NULL
, in_completion_filter
);
225 if (tevent_req_nomem(filter_string
, req
)) {
226 return tevent_req_post(req
, ev
);
229 DEBUG(3,("smbd_smb2_notify_send: notify change "
230 "called on %s, filter = %s, recursive = %d\n",
231 fsp_str_dbg(fsp
), filter_string
, recursive
));
233 TALLOC_FREE(filter_string
);
236 if ((!fsp
->is_directory
) || (conn
!= fsp
->conn
)) {
237 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
238 return tevent_req_post(req
, ev
);
241 if (fsp
->notify
== NULL
) {
243 status
= change_notify_create(fsp
,
244 in_completion_filter
,
246 if (!NT_STATUS_IS_OK(status
)) {
247 DEBUG(10, ("change_notify_create returned %s\n",
249 tevent_req_nterror(req
, status
);
250 return tevent_req_post(req
, ev
);
254 if (change_notify_fsp_has_changes(fsp
)) {
257 * We've got changes pending, respond immediately
261 * TODO: write a torture test to check the filtering behaviour
265 change_notify_reply(smbreq
,
267 in_output_buffer_length
,
269 smbd_smb2_notify_reply
);
272 * change_notify_reply() above has independently
273 * called tevent_req_done().
275 return tevent_req_post(req
, ev
);
278 state
->im
= tevent_create_immediate(state
);
279 if (tevent_req_nomem(state
->im
, req
)) {
280 return tevent_req_post(req
, ev
);
284 * No changes pending, queue the request
287 status
= change_notify_add_request(smbreq
,
288 in_output_buffer_length
,
289 in_completion_filter
,
291 smbd_smb2_notify_reply
);
292 if (!NT_STATUS_IS_OK(status
)) {
293 tevent_req_nterror(req
, status
);
294 return tevent_req_post(req
, ev
);
297 /* allow this request to be canceled */
298 tevent_req_set_cancel_fn(req
, smbd_smb2_notify_cancel
);
303 static void smbd_smb2_notify_reply(struct smb_request
*smbreq
,
305 uint8_t *buf
, size_t len
)
307 struct tevent_req
*req
= talloc_get_type_abort(smbreq
->async_priv
,
309 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
310 struct smbd_smb2_notify_state
);
312 state
->status
= error_code
;
313 if (!NT_STATUS_IS_OK(error_code
)) {
315 } else if (len
== 0) {
316 state
->status
= STATUS_NOTIFY_ENUM_DIR
;
318 state
->out_output_buffer
= data_blob_talloc(state
, buf
, len
);
319 if (state
->out_output_buffer
.data
== NULL
) {
320 state
->status
= NT_STATUS_NO_MEMORY
;
324 if (state
->im
== NULL
) {
325 smbd_smb2_notify_reply_trigger(NULL
, NULL
, req
);
330 * if this is called async, we need to go via an immediate event
331 * because the caller replies on the smb_request (a child of req
332 * being arround after calling this function
334 tevent_schedule_immediate(state
->im
,
335 state
->smb2req
->sconn
->ev_ctx
,
336 smbd_smb2_notify_reply_trigger
,
340 static void smbd_smb2_notify_reply_trigger(struct tevent_context
*ctx
,
341 struct tevent_immediate
*im
,
344 struct tevent_req
*req
= talloc_get_type_abort(private_data
,
346 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
347 struct smbd_smb2_notify_state
);
349 if (!NT_STATUS_IS_OK(state
->status
)) {
350 tevent_req_nterror(req
, state
->status
);
354 tevent_req_done(req
);
357 static bool smbd_smb2_notify_cancel(struct tevent_req
*req
)
359 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
360 struct smbd_smb2_notify_state
);
362 smbd_notify_cancel_by_smbreq(state
->smbreq
);
364 state
->smb2req
->cancelled
= true;
365 tevent_req_done(req
);
369 static NTSTATUS
smbd_smb2_notify_recv(struct tevent_req
*req
,
371 DATA_BLOB
*out_output_buffer
)
374 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
375 struct smbd_smb2_notify_state
);
377 if (tevent_req_is_nterror(req
, &status
)) {
378 tevent_req_received(req
);
382 *out_output_buffer
= state
->out_output_buffer
;
383 talloc_steal(mem_ctx
, out_output_buffer
->data
);
385 tevent_req_received(req
);