2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2000
5 Copyright (C) Jeremy Allison 1994-1998
6 Copyright (C) Volker Lendecke 2007
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 "../librpc/gen_ndr/ndr_notify.h"
26 #include "librpc/gen_ndr/ndr_file_id.h"
28 struct notify_change_event
{
34 struct notify_change_buf
{
36 * If no requests are pending, changes are queued here. Simple array,
41 * num_changes == -1 means that we have got a catch-all change, when
42 * asked we just return NT_STATUS_OK without specific changes.
45 struct notify_change_event
*changes
;
48 * If no changes are around requests are queued here. Using a linked
49 * list, because we have to append at the end and delete from the top.
51 struct notify_change_request
*requests
;
54 struct notify_change_request
{
55 struct notify_change_request
*prev
, *next
;
56 struct files_struct
*fsp
; /* backpointer for cancel by mid */
57 struct smb_request
*req
;
60 void (*reply_fn
)(struct smb_request
*req
,
62 uint8_t *buf
, size_t len
);
63 struct notify_mid_map
*mid_map
;
67 static void notify_fsp(files_struct
*fsp
, struct timespec when
,
68 uint32_t action
, const char *name
);
70 bool change_notify_fsp_has_changes(struct files_struct
*fsp
)
76 if (fsp
->notify
== NULL
) {
80 if (fsp
->notify
->num_changes
== 0) {
88 * For NTCancel, we need to find the notify_change_request indexed by
89 * mid. Separate list here.
92 struct notify_mid_map
{
93 struct notify_mid_map
*prev
, *next
;
94 struct notify_change_request
*req
;
98 static bool notify_change_record_identical(struct notify_change_event
*c1
,
99 struct notify_change_event
*c2
)
101 /* Note this is deliberately case sensitive. */
102 if (c1
->action
== c2
->action
&&
103 strcmp(c1
->name
, c2
->name
) == 0) {
109 static int compare_notify_change_events(const void *p1
, const void *p2
)
111 const struct notify_change_event
*e1
= p1
;
112 const struct notify_change_event
*e2
= p2
;
114 return timespec_compare(&e1
->when
, &e2
->when
);
117 static bool notify_marshall_changes(int num_changes
,
119 struct notify_change_event
*changes
,
120 DATA_BLOB
*final_blob
)
124 if (num_changes
== -1) {
129 * Sort the notifies by timestamp when the event happened to avoid
130 * coalescing and thus dropping events.
133 qsort(changes
, num_changes
,
134 sizeof(*changes
), compare_notify_change_events
);
136 for (i
=0; i
<num_changes
; i
++) {
137 enum ndr_err_code ndr_err
;
138 struct notify_change_event
*c
;
139 struct FILE_NOTIFY_INFORMATION m
;
142 /* Coalesce any identical records. */
143 while (i
+1 < num_changes
&&
144 notify_change_record_identical(&changes
[i
],
151 m
.FileName1
= c
->name
;
152 m
.FileNameLength
= strlen_m(c
->name
)*2;
153 m
.Action
= c
->action
;
154 m
.NextEntryOffset
= (i
== num_changes
-1) ? 0 : ndr_size_FILE_NOTIFY_INFORMATION(&m
, 0);
157 * Offset to next entry, only if there is one
160 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &m
,
161 (ndr_push_flags_fn_t
)ndr_push_FILE_NOTIFY_INFORMATION
);
162 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
166 if (DEBUGLEVEL
>= 10) {
167 NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION
, &m
);
170 if (!data_blob_append(talloc_tos(), final_blob
,
171 blob
.data
, blob
.length
)) {
172 data_blob_free(&blob
);
176 data_blob_free(&blob
);
178 if (final_blob
->length
> max_offset
) {
179 /* Too much data for client. */
180 DEBUG(10, ("Client only wanted %d bytes, trying to "
181 "marshall %d bytes\n", (int)max_offset
,
182 (int)final_blob
->length
));
190 /****************************************************************************
191 Setup the common parts of the return packet and send it.
192 *****************************************************************************/
194 void change_notify_reply(struct smb_request
*req
,
197 struct notify_change_buf
*notify_buf
,
198 void (*reply_fn
)(struct smb_request
*req
,
200 uint8_t *buf
, size_t len
))
202 DATA_BLOB blob
= data_blob_null
;
204 if (!NT_STATUS_IS_OK(error_code
)) {
205 reply_fn(req
, error_code
, NULL
, 0);
209 if (max_param
== 0 || notify_buf
== NULL
) {
210 reply_fn(req
, NT_STATUS_OK
, NULL
, 0);
214 if (!notify_marshall_changes(notify_buf
->num_changes
, max_param
,
215 notify_buf
->changes
, &blob
)) {
217 * We exceed what the client is willing to accept. Send
220 data_blob_free(&blob
);
223 reply_fn(req
, NT_STATUS_OK
, blob
.data
, blob
.length
);
225 data_blob_free(&blob
);
227 TALLOC_FREE(notify_buf
->changes
);
228 notify_buf
->num_changes
= 0;
231 static void notify_callback(void *private_data
, struct timespec when
,
232 const struct notify_event
*e
)
234 files_struct
*fsp
= (files_struct
*)private_data
;
235 DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp
)));
236 notify_fsp(fsp
, when
, e
->action
, e
->path
);
239 static void sys_notify_callback(struct sys_notify_context
*ctx
,
241 struct notify_event
*e
)
243 files_struct
*fsp
= (files_struct
*)private_data
;
244 DEBUG(10, ("sys_notify_callback called for %s\n", fsp_str_dbg(fsp
)));
245 notify_fsp(fsp
, timespec_current(), e
->action
, e
->path
);
248 NTSTATUS
change_notify_create(struct files_struct
*fsp
, uint32_t filter
,
253 uint32_t subdir_filter
;
254 NTSTATUS status
= NT_STATUS_NOT_IMPLEMENTED
;
256 if (fsp
->notify
!= NULL
) {
257 DEBUG(1, ("change_notify_create: fsp->notify != NULL, "
258 "fname = %s\n", fsp
->fsp_name
->base_name
));
259 return NT_STATUS_INVALID_PARAMETER
;
262 if (!(fsp
->notify
= talloc_zero(NULL
, struct notify_change_buf
))) {
263 DEBUG(0, ("talloc failed\n"));
264 return NT_STATUS_NO_MEMORY
;
267 /* Do notify operations on the base_name. */
268 fullpath
= talloc_asprintf(
269 talloc_tos(), "%s/%s", fsp
->conn
->connectpath
,
270 fsp
->fsp_name
->base_name
);
271 if (fullpath
== NULL
) {
272 DEBUG(0, ("talloc_asprintf failed\n"));
273 TALLOC_FREE(fsp
->notify
);
274 return NT_STATUS_NO_MEMORY
;
278 * Avoid /. at the end of the path name. notify can't deal with it.
280 len
= strlen(fullpath
);
281 if (len
> 1 && fullpath
[len
-1] == '.' && fullpath
[len
-2] == '/') {
282 fullpath
[len
-2] = '\0';
285 subdir_filter
= recursive
? filter
: 0;
287 if (fsp
->conn
->sconn
->sys_notify_ctx
!= NULL
) {
288 void *sys_notify_handle
= NULL
;
290 status
= SMB_VFS_NOTIFY_WATCH(
291 fsp
->conn
, fsp
->conn
->sconn
->sys_notify_ctx
,
292 fullpath
, &filter
, &subdir_filter
,
293 sys_notify_callback
, fsp
, &sys_notify_handle
);
295 if (NT_STATUS_IS_OK(status
)) {
296 talloc_steal(fsp
->notify
, sys_notify_handle
);
300 if ((filter
!= 0) || (subdir_filter
!= 0)) {
301 status
= notify_add(fsp
->conn
->sconn
->notify_ctx
,
302 fullpath
, filter
, subdir_filter
,
303 notify_callback
, fsp
);
305 TALLOC_FREE(fullpath
);
309 NTSTATUS
change_notify_add_request(struct smb_request
*req
,
311 uint32_t filter
, bool recursive
,
312 struct files_struct
*fsp
,
313 void (*reply_fn
)(struct smb_request
*req
,
315 uint8_t *buf
, size_t len
))
317 struct notify_change_request
*request
= NULL
;
318 struct notify_mid_map
*map
= NULL
;
319 struct smbd_server_connection
*sconn
= req
->sconn
;
321 DEBUG(10, ("change_notify_add_request: Adding request for %s: "
322 "max_param = %d\n", fsp_str_dbg(fsp
), (int)max_param
));
324 if (!(request
= talloc(NULL
, struct notify_change_request
))
325 || !(map
= talloc(request
, struct notify_mid_map
))) {
326 TALLOC_FREE(request
);
327 return NT_STATUS_NO_MEMORY
;
330 request
->mid_map
= map
;
333 request
->req
= talloc_move(request
, &req
);
334 request
->max_param
= max_param
;
335 request
->filter
= filter
;
337 request
->reply_fn
= reply_fn
;
338 request
->backend_data
= NULL
;
340 DLIST_ADD_END(fsp
->notify
->requests
, request
,
341 struct notify_change_request
*);
343 map
->mid
= request
->req
->mid
;
344 DLIST_ADD(sconn
->smb1
.notify_mid_maps
, map
);
349 static void change_notify_remove_request(struct smbd_server_connection
*sconn
,
350 struct notify_change_request
*remove_req
)
353 struct notify_change_request
*req
;
356 * Paranoia checks, the fsp referenced must must have the request in
357 * its list of pending requests
360 fsp
= remove_req
->fsp
;
361 SMB_ASSERT(fsp
->notify
!= NULL
);
363 for (req
= fsp
->notify
->requests
; req
; req
= req
->next
) {
364 if (req
== remove_req
) {
370 smb_panic("notify_req not found in fsp's requests");
373 DLIST_REMOVE(fsp
->notify
->requests
, req
);
374 DLIST_REMOVE(sconn
->smb1
.notify_mid_maps
, req
->mid_map
);
378 static void smbd_notify_cancel_by_map(struct notify_mid_map
*map
)
380 struct smb_request
*smbreq
= map
->req
->req
;
381 struct smbd_server_connection
*sconn
= smbreq
->sconn
;
382 struct smbd_smb2_request
*smb2req
= smbreq
->smb2req
;
383 NTSTATUS notify_status
= NT_STATUS_CANCELLED
;
385 if (smb2req
!= NULL
) {
386 if (smb2req
->session
== NULL
) {
387 notify_status
= STATUS_NOTIFY_CLEANUP
;
388 } else if (!NT_STATUS_IS_OK(smb2req
->session
->status
)) {
389 notify_status
= STATUS_NOTIFY_CLEANUP
;
391 if (smb2req
->tcon
== NULL
) {
392 notify_status
= STATUS_NOTIFY_CLEANUP
;
393 } else if (!NT_STATUS_IS_OK(smb2req
->tcon
->status
)) {
394 notify_status
= STATUS_NOTIFY_CLEANUP
;
398 change_notify_reply(smbreq
, notify_status
,
399 0, NULL
, map
->req
->reply_fn
);
400 change_notify_remove_request(sconn
, map
->req
);
403 /****************************************************************************
404 Delete entries by mid from the change notify pending queue. Always send reply.
405 *****************************************************************************/
407 void remove_pending_change_notify_requests_by_mid(
408 struct smbd_server_connection
*sconn
, uint64_t mid
)
410 struct notify_mid_map
*map
;
412 for (map
= sconn
->smb1
.notify_mid_maps
; map
; map
= map
->next
) {
413 if (map
->mid
== mid
) {
422 smbd_notify_cancel_by_map(map
);
425 void smbd_notify_cancel_by_smbreq(const struct smb_request
*smbreq
)
427 struct smbd_server_connection
*sconn
= smbreq
->sconn
;
428 struct notify_mid_map
*map
;
430 for (map
= sconn
->smb1
.notify_mid_maps
; map
; map
= map
->next
) {
431 if (map
->req
->req
== smbreq
) {
440 smbd_notify_cancel_by_map(map
);
443 static struct files_struct
*smbd_notify_cancel_deleted_fn(
444 struct files_struct
*fsp
, void *private_data
)
446 struct file_id
*fid
= talloc_get_type_abort(
447 private_data
, struct file_id
);
449 if (file_id_equal(&fsp
->file_id
, fid
)) {
450 remove_pending_change_notify_requests_by_fid(
451 fsp
, NT_STATUS_DELETE_PENDING
);
456 void smbd_notify_cancel_deleted(struct messaging_context
*msg
,
457 void *private_data
, uint32_t msg_type
,
458 struct server_id server_id
, DATA_BLOB
*data
)
460 struct smbd_server_connection
*sconn
= talloc_get_type_abort(
461 private_data
, struct smbd_server_connection
);
463 enum ndr_err_code ndr_err
;
465 fid
= talloc(talloc_tos(), struct file_id
);
467 DEBUG(1, ("talloc failed\n"));
471 ndr_err
= ndr_pull_struct_blob_all(
472 data
, fid
, fid
, (ndr_pull_flags_fn_t
)ndr_pull_file_id
);
473 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
474 DEBUG(10, ("%s: ndr_pull_file_id failed: %s\n", __func__
,
475 ndr_errstr(ndr_err
)));
479 files_forall(sconn
, smbd_notify_cancel_deleted_fn
, fid
);
485 /****************************************************************************
486 Delete entries by fnum from the change notify pending queue.
487 *****************************************************************************/
489 void remove_pending_change_notify_requests_by_fid(files_struct
*fsp
,
492 if (fsp
->notify
== NULL
) {
496 while (fsp
->notify
->requests
!= NULL
) {
497 change_notify_reply(fsp
->notify
->requests
->req
,
499 fsp
->notify
->requests
->reply_fn
);
500 change_notify_remove_request(fsp
->conn
->sconn
,
501 fsp
->notify
->requests
);
505 void notify_fname(connection_struct
*conn
, uint32_t action
, uint32_t filter
,
508 struct notify_context
*notify_ctx
= conn
->sconn
->notify_ctx
;
510 if (path
[0] == '.' && path
[1] == '/') {
514 notify_trigger(notify_ctx
, action
, filter
, conn
->connectpath
, path
);
517 static void notify_fsp(files_struct
*fsp
, struct timespec when
,
518 uint32_t action
, const char *name
)
520 struct notify_change_event
*change
, *changes
;
523 if (fsp
->notify
== NULL
) {
525 * Nobody is waiting, don't queue
531 * Someone has triggered a notify previously, queue the change for
535 if ((fsp
->notify
->num_changes
> 1000) || (name
== NULL
)) {
537 * The real number depends on the client buf, just provide a
538 * guard against a DoS here. If name == NULL the CN backend is
539 * alerting us to a problem. Possibly dropped events. Clear
540 * queued changes and send the catch-all response to the client
541 * if a request is pending.
543 TALLOC_FREE(fsp
->notify
->changes
);
544 fsp
->notify
->num_changes
= -1;
545 if (fsp
->notify
->requests
!= NULL
) {
546 change_notify_reply(fsp
->notify
->requests
->req
,
548 fsp
->notify
->requests
->max_param
,
550 fsp
->notify
->requests
->reply_fn
);
551 change_notify_remove_request(fsp
->conn
->sconn
,
552 fsp
->notify
->requests
);
557 /* If we've exceeded the server side queue or received a NULL name
558 * from the underlying CN implementation, don't queue up any more
559 * requests until we can send a catch-all response to the client */
560 if (fsp
->notify
->num_changes
== -1) {
564 if (!(changes
= talloc_realloc(
565 fsp
->notify
, fsp
->notify
->changes
,
566 struct notify_change_event
,
567 fsp
->notify
->num_changes
+1))) {
568 DEBUG(0, ("talloc_realloc failed\n"));
572 fsp
->notify
->changes
= changes
;
574 change
= &(fsp
->notify
->changes
[fsp
->notify
->num_changes
]);
576 if (!(tmp
= talloc_strdup(changes
, name
))) {
577 DEBUG(0, ("talloc_strdup failed\n"));
581 string_replace(tmp
, '/', '\\');
585 change
->action
= action
;
586 fsp
->notify
->num_changes
+= 1;
588 if (fsp
->notify
->requests
== NULL
) {
590 * Nobody is waiting, so don't send anything. The ot
595 if (action
== NOTIFY_ACTION_OLD_NAME
) {
597 * We have to send the two rename events in one reply. So hold
598 * the first part back.
604 * Someone is waiting for the change, trigger the reply immediately.
606 * TODO: do we have to walk the lists of requests pending?
609 change_notify_reply(fsp
->notify
->requests
->req
,
611 fsp
->notify
->requests
->max_param
,
613 fsp
->notify
->requests
->reply_fn
);
615 change_notify_remove_request(fsp
->conn
->sconn
, fsp
->notify
->requests
);
618 char *notify_filter_string(TALLOC_CTX
*mem_ctx
, uint32_t filter
)
622 result
= talloc_strdup(mem_ctx
, "");
624 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
625 result
= talloc_asprintf_append(result
, "FILE_NAME|");
626 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
627 result
= talloc_asprintf_append(result
, "DIR_NAME|");
628 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
629 result
= talloc_asprintf_append(result
, "ATTRIBUTES|");
630 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
631 result
= talloc_asprintf_append(result
, "SIZE|");
632 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
633 result
= talloc_asprintf_append(result
, "LAST_WRITE|");
634 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
635 result
= talloc_asprintf_append(result
, "LAST_ACCESS|");
636 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
637 result
= talloc_asprintf_append(result
, "CREATION|");
638 if (filter
& FILE_NOTIFY_CHANGE_EA
)
639 result
= talloc_asprintf_append(result
, "EA|");
640 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
641 result
= talloc_asprintf_append(result
, "SECURITY|");
642 if (filter
& FILE_NOTIFY_CHANGE_STREAM_NAME
)
643 result
= talloc_asprintf_append(result
, "STREAM_NAME|");
644 if (filter
& FILE_NOTIFY_CHANGE_STREAM_SIZE
)
645 result
= talloc_asprintf_append(result
, "STREAM_SIZE|");
646 if (filter
& FILE_NOTIFY_CHANGE_STREAM_WRITE
)
647 result
= talloc_asprintf_append(result
, "STREAM_WRITE|");
649 if (result
== NULL
) return NULL
;
650 if (*result
== '\0') return result
;
652 result
[strlen(result
)-1] = '\0';
656 struct sys_notify_context
*sys_notify_context_create(TALLOC_CTX
*mem_ctx
,
657 struct tevent_context
*ev
)
659 struct sys_notify_context
*ctx
;
661 if (!(ctx
= talloc(mem_ctx
, struct sys_notify_context
))) {
662 DEBUG(0, ("talloc failed\n"));
667 ctx
->private_data
= NULL
;