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"
29 #define DBGC_CLASS DBGC_SMB2
31 struct smbd_smb2_notify_state
{
32 struct smbd_smb2_request
*smb2req
;
33 struct smb_request
*smbreq
;
37 DATA_BLOB out_output_buffer
;
40 static struct tevent_req
*smbd_smb2_notify_send(TALLOC_CTX
*mem_ctx
,
41 struct tevent_context
*ev
,
42 struct smbd_smb2_request
*smb2req
,
43 struct files_struct
*in_fsp
,
45 uint32_t in_output_buffer_length
,
46 uint64_t in_completion_filter
);
47 static NTSTATUS
smbd_smb2_notify_recv(struct tevent_req
*req
,
49 DATA_BLOB
*out_output_buffer
);
51 static void smbd_smb2_request_notify_done(struct tevent_req
*subreq
);
52 NTSTATUS
smbd_smb2_request_process_notify(struct smbd_smb2_request
*req
)
54 struct smbXsrv_connection
*xconn
= req
->xconn
;
56 const uint8_t *inbody
;
58 uint32_t in_output_buffer_length
;
59 uint64_t in_file_id_persistent
;
60 uint64_t in_file_id_volatile
;
61 struct files_struct
*in_fsp
;
62 uint64_t in_completion_filter
;
63 struct tevent_req
*subreq
;
65 status
= smbd_smb2_request_verify_sizes(req
, 0x20);
66 if (!NT_STATUS_IS_OK(status
)) {
67 return smbd_smb2_request_error(req
, status
);
69 inbody
= SMBD_SMB2_IN_BODY_PTR(req
);
71 in_flags
= SVAL(inbody
, 0x02);
72 in_output_buffer_length
= IVAL(inbody
, 0x04);
73 in_file_id_persistent
= BVAL(inbody
, 0x08);
74 in_file_id_volatile
= BVAL(inbody
, 0x10);
75 in_completion_filter
= IVAL(inbody
, 0x18);
78 * 0x00010000 is what Windows 7 uses,
79 * Windows 2008 uses 0x00080000
81 if (in_output_buffer_length
> xconn
->smb2
.server
.max_trans
) {
82 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
85 status
= smbd_smb2_request_verify_creditcharge(req
,
86 in_output_buffer_length
);
88 if (!NT_STATUS_IS_OK(status
)) {
89 return smbd_smb2_request_error(req
, status
);
92 in_fsp
= file_fsp_smb2(req
, in_file_id_persistent
, in_file_id_volatile
);
94 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
97 subreq
= smbd_smb2_notify_send(req
, req
->sconn
->ev_ctx
,
100 in_output_buffer_length
,
101 in_completion_filter
);
102 if (subreq
== NULL
) {
103 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
105 tevent_req_set_callback(subreq
, smbd_smb2_request_notify_done
, req
);
107 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
110 static void smbd_smb2_request_notify_done(struct tevent_req
*subreq
)
112 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
113 struct smbd_smb2_request
);
116 uint16_t out_output_buffer_offset
;
117 DATA_BLOB out_output_buffer
= data_blob_null
;
119 NTSTATUS error
; /* transport error */
121 status
= smbd_smb2_notify_recv(subreq
,
125 if (!NT_STATUS_IS_OK(status
)) {
126 error
= smbd_smb2_request_error(req
, status
);
127 if (!NT_STATUS_IS_OK(error
)) {
128 smbd_server_connection_terminate(req
->xconn
,
135 out_output_buffer_offset
= SMB2_HDR_BODY
+ 0x08;
137 outbody
= smbd_smb2_generate_outbody(req
, 0x08);
138 if (outbody
.data
== NULL
) {
139 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
140 if (!NT_STATUS_IS_OK(error
)) {
141 smbd_server_connection_terminate(req
->xconn
,
148 SSVAL(outbody
.data
, 0x00, 0x08 + 1); /* struct size */
149 SSVAL(outbody
.data
, 0x02,
150 out_output_buffer_offset
); /* output buffer offset */
151 SIVAL(outbody
.data
, 0x04,
152 out_output_buffer
.length
); /* output buffer length */
154 outdyn
= out_output_buffer
;
156 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
157 if (!NT_STATUS_IS_OK(error
)) {
158 smbd_server_connection_terminate(req
->xconn
,
164 static void smbd_smb2_notify_reply(struct smb_request
*smbreq
,
166 uint8_t *buf
, size_t len
);
167 static bool smbd_smb2_notify_cancel(struct tevent_req
*req
);
169 static int smbd_smb2_notify_state_destructor(struct smbd_smb2_notify_state
*state
)
171 if (!state
->has_request
) {
175 state
->skip_reply
= true;
176 smbd_notify_cancel_by_smbreq(state
->smbreq
);
180 static int smbd_smb2_notify_smbreq_destructor(struct smb_request
*smbreq
)
182 struct tevent_req
*req
= talloc_get_type_abort(smbreq
->async_priv
,
184 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
185 struct smbd_smb2_notify_state
);
188 * Our temporary parent from change_notify_add_request()
191 state
->has_request
= false;
194 * move it back to its original parent,
195 * which means we no longer need the destructor
198 talloc_steal(smbreq
->smb2req
, smbreq
);
199 talloc_set_destructor(smbreq
, NULL
);
202 * We want to keep smbreq!
207 static struct tevent_req
*smbd_smb2_notify_send(TALLOC_CTX
*mem_ctx
,
208 struct tevent_context
*ev
,
209 struct smbd_smb2_request
*smb2req
,
210 struct files_struct
*fsp
,
212 uint32_t in_output_buffer_length
,
213 uint64_t in_completion_filter
)
215 struct tevent_req
*req
;
216 struct smbd_smb2_notify_state
*state
;
217 struct smb_request
*smbreq
;
218 connection_struct
*conn
= smb2req
->tcon
->compat
;
219 bool recursive
= (in_flags
& SMB2_WATCH_TREE
) ? true : false;
222 req
= tevent_req_create(mem_ctx
, &state
,
223 struct smbd_smb2_notify_state
);
227 state
->smb2req
= smb2req
;
228 state
->status
= NT_STATUS_INTERNAL_ERROR
;
229 state
->out_output_buffer
= data_blob_null
;
230 talloc_set_destructor(state
, smbd_smb2_notify_state_destructor
);
232 DEBUG(10,("smbd_smb2_notify_send: %s - %s\n",
233 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
235 smbreq
= smbd_smb2_fake_smb_request(smb2req
, fsp
);
236 if (tevent_req_nomem(smbreq
, req
)) {
237 return tevent_req_post(req
, ev
);
240 state
->smbreq
= smbreq
;
241 smbreq
->async_priv
= (void *)req
;
243 if (DEBUGLEVEL
>= 3) {
246 filter_string
= notify_filter_string(NULL
, in_completion_filter
);
247 if (tevent_req_nomem(filter_string
, req
)) {
248 return tevent_req_post(req
, ev
);
251 DEBUG(3,("smbd_smb2_notify_send: notify change "
252 "called on %s, filter = %s, recursive = %d\n",
253 fsp_str_dbg(fsp
), filter_string
, recursive
));
255 TALLOC_FREE(filter_string
);
258 if ((!fsp
->fsp_flags
.is_directory
) || (conn
!= fsp
->conn
)) {
259 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
260 return tevent_req_post(req
, ev
);
263 if (fsp
->notify
== NULL
) {
265 status
= change_notify_create(fsp
,
266 in_output_buffer_length
,
267 in_completion_filter
,
269 if (tevent_req_nterror(req
, status
)) {
270 DEBUG(10, ("change_notify_create returned %s\n",
272 return tevent_req_post(req
, ev
);
276 if (change_notify_fsp_has_changes(fsp
)) {
279 * We've got changes pending, respond immediately
283 * TODO: write a torture test to check the filtering behaviour
287 change_notify_reply(smbreq
,
289 in_output_buffer_length
,
291 smbd_smb2_notify_reply
);
294 * change_notify_reply() above has independently
295 * called tevent_req_done().
297 return tevent_req_post(req
, ev
);
301 * No changes pending, queue the request
304 status
= change_notify_add_request(smbreq
,
305 in_output_buffer_length
,
306 in_completion_filter
,
308 smbd_smb2_notify_reply
);
309 if (tevent_req_nterror(req
, status
)) {
310 return tevent_req_post(req
, ev
);
316 * change_notify_add_request() talloc_moves()
317 * smbreq away from us, so we need a destructor
318 * which moves it back at the end.
320 state
->has_request
= true;
321 talloc_set_destructor(smbreq
, smbd_smb2_notify_smbreq_destructor
);
323 /* allow this request to be canceled */
324 tevent_req_set_cancel_fn(req
, smbd_smb2_notify_cancel
);
326 SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(state
->smb2req
->profile
);
330 static void smbd_smb2_notify_reply(struct smb_request
*smbreq
,
332 uint8_t *buf
, size_t len
)
334 struct tevent_req
*req
= talloc_get_type_abort(smbreq
->async_priv
,
336 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
337 struct smbd_smb2_notify_state
);
339 if (state
->skip_reply
) {
343 SMBPROFILE_IOBYTES_ASYNC_SET_BUSY(state
->smb2req
->profile
);
345 state
->status
= error_code
;
346 if (!NT_STATUS_IS_OK(error_code
)) {
348 } else if (len
== 0) {
349 state
->status
= NT_STATUS_NOTIFY_ENUM_DIR
;
351 state
->out_output_buffer
= data_blob_talloc(state
, buf
, len
);
352 if (state
->out_output_buffer
.data
== NULL
) {
353 state
->status
= NT_STATUS_NO_MEMORY
;
357 tevent_req_defer_callback(req
, state
->smb2req
->sconn
->ev_ctx
);
359 if (tevent_req_nterror(req
, state
->status
)) {
363 tevent_req_done(req
);
366 static bool smbd_smb2_notify_cancel(struct tevent_req
*req
)
368 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
369 struct smbd_smb2_notify_state
);
371 smbd_notify_cancel_by_smbreq(state
->smbreq
);
376 static NTSTATUS
smbd_smb2_notify_recv(struct tevent_req
*req
,
378 DATA_BLOB
*out_output_buffer
)
381 struct smbd_smb2_notify_state
*state
= tevent_req_data(req
,
382 struct smbd_smb2_notify_state
);
384 if (tevent_req_is_nterror(req
, &status
)) {
385 tevent_req_received(req
);
389 *out_output_buffer
= state
->out_output_buffer
;
390 talloc_steal(mem_ctx
, out_output_buffer
->data
);
392 tevent_req_received(req
);