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/globals.h"
24 #include "../librpc/gen_ndr/ndr_notify.h"
26 struct notify_change_request
{
27 struct notify_change_request
*prev
, *next
;
28 struct files_struct
*fsp
; /* backpointer for cancel by mid */
29 struct smb_request
*req
;
32 void (*reply_fn
)(struct smb_request
*req
,
34 uint8_t *buf
, size_t len
);
35 struct notify_mid_map
*mid_map
;
39 static void notify_fsp(files_struct
*fsp
, uint32 action
, const char *name
);
42 * For NTCancel, we need to find the notify_change_request indexed by
43 * mid. Separate list here.
46 struct notify_mid_map
{
47 struct notify_mid_map
*prev
, *next
;
48 struct notify_change_request
*req
;
52 static bool notify_change_record_identical(struct notify_change
*c1
,
53 struct notify_change
*c2
)
55 /* Note this is deliberately case sensitive. */
56 if (c1
->action
== c2
->action
&&
57 strcmp(c1
->name
, c2
->name
) == 0) {
63 static bool notify_marshall_changes(int num_changes
,
65 struct notify_change
*changes
,
66 DATA_BLOB
*final_blob
)
70 if (num_changes
== -1) {
74 for (i
=0; i
<num_changes
; i
++) {
75 enum ndr_err_code ndr_err
;
76 struct notify_change
*c
;
77 struct FILE_NOTIFY_INFORMATION m
;
80 /* Coalesce any identical records. */
81 while (i
+1 < num_changes
&&
82 notify_change_record_identical(&changes
[i
],
89 m
.FileName1
= c
->name
;
90 m
.FileNameLength
= strlen_m(c
->name
)*2;
92 m
.NextEntryOffset
= (i
== num_changes
-1) ? 0 : ndr_size_FILE_NOTIFY_INFORMATION(&m
, 0);
95 * Offset to next entry, only if there is one
98 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &m
,
99 (ndr_push_flags_fn_t
)ndr_push_FILE_NOTIFY_INFORMATION
);
100 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
104 if (DEBUGLEVEL
>= 10) {
105 NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION
, &m
);
108 if (!data_blob_append(talloc_tos(), final_blob
,
109 blob
.data
, blob
.length
)) {
110 data_blob_free(&blob
);
114 data_blob_free(&blob
);
116 if (final_blob
->length
> max_offset
) {
117 /* Too much data for client. */
118 DEBUG(10, ("Client only wanted %d bytes, trying to "
119 "marshall %d bytes\n", (int)max_offset
,
120 (int)final_blob
->length
));
128 /****************************************************************************
129 Setup the common parts of the return packet and send it.
130 *****************************************************************************/
132 void change_notify_reply(struct smb_request
*req
,
135 struct notify_change_buf
*notify_buf
,
136 void (*reply_fn
)(struct smb_request
*req
,
138 uint8_t *buf
, size_t len
))
140 DATA_BLOB blob
= data_blob_null
;
142 if (!NT_STATUS_IS_OK(error_code
)) {
143 reply_fn(req
, error_code
, NULL
, 0);
147 if (max_param
== 0 || notify_buf
== NULL
) {
148 reply_fn(req
, NT_STATUS_OK
, NULL
, 0);
152 if (!notify_marshall_changes(notify_buf
->num_changes
, max_param
,
153 notify_buf
->changes
, &blob
)) {
155 * We exceed what the client is willing to accept. Send
158 data_blob_free(&blob
);
161 reply_fn(req
, NT_STATUS_OK
, blob
.data
, blob
.length
);
163 data_blob_free(&blob
);
165 TALLOC_FREE(notify_buf
->changes
);
166 notify_buf
->num_changes
= 0;
169 static void notify_callback(void *private_data
, const struct notify_event
*e
)
171 files_struct
*fsp
= (files_struct
*)private_data
;
172 DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp
)));
173 notify_fsp(fsp
, e
->action
, e
->path
);
176 NTSTATUS
change_notify_create(struct files_struct
*fsp
, uint32 filter
,
180 struct notify_entry e
;
183 SMB_ASSERT(fsp
->notify
== NULL
);
185 if (!(fsp
->notify
= TALLOC_ZERO_P(NULL
, struct notify_change_buf
))) {
186 DEBUG(0, ("talloc failed\n"));
187 return NT_STATUS_NO_MEMORY
;
190 /* Do notify operations on the base_name. */
191 if (asprintf(&fullpath
, "%s/%s", fsp
->conn
->connectpath
,
192 fsp
->fsp_name
->base_name
) == -1) {
193 DEBUG(0, ("asprintf failed\n"));
194 TALLOC_FREE(fsp
->notify
);
195 return NT_STATUS_NO_MEMORY
;
200 e
.dir_fd
= fsp
->fh
->fd
;
201 e
.dir_id
= fsp
->file_id
;
205 e
.subdir_filter
= filter
;
208 status
= notify_add(fsp
->conn
->notify_ctx
, &e
, notify_callback
, fsp
);
214 NTSTATUS
change_notify_add_request(struct smb_request
*req
,
216 uint32 filter
, bool recursive
,
217 struct files_struct
*fsp
,
218 void (*reply_fn
)(struct smb_request
*req
,
220 uint8_t *buf
, size_t len
))
222 struct notify_change_request
*request
= NULL
;
223 struct notify_mid_map
*map
= NULL
;
224 struct smbd_server_connection
*sconn
= req
->sconn
;
226 DEBUG(10, ("change_notify_add_request: Adding request for %s: "
227 "max_param = %d\n", fsp_str_dbg(fsp
), (int)max_param
));
229 if (!(request
= talloc(NULL
, struct notify_change_request
))
230 || !(map
= talloc(request
, struct notify_mid_map
))) {
231 TALLOC_FREE(request
);
232 return NT_STATUS_NO_MEMORY
;
235 request
->mid_map
= map
;
238 request
->req
= talloc_move(request
, &req
);
239 request
->max_param
= max_param
;
240 request
->filter
= filter
;
242 request
->reply_fn
= reply_fn
;
243 request
->backend_data
= NULL
;
245 DLIST_ADD_END(fsp
->notify
->requests
, request
,
246 struct notify_change_request
*);
248 map
->mid
= request
->req
->mid
;
249 DLIST_ADD(sconn
->smb1
.notify_mid_maps
, map
);
254 static void change_notify_remove_request(struct smbd_server_connection
*sconn
,
255 struct notify_change_request
*remove_req
)
258 struct notify_change_request
*req
;
261 * Paranoia checks, the fsp referenced must must have the request in
262 * its list of pending requests
265 fsp
= remove_req
->fsp
;
266 SMB_ASSERT(fsp
->notify
!= NULL
);
268 for (req
= fsp
->notify
->requests
; req
; req
= req
->next
) {
269 if (req
== remove_req
) {
275 smb_panic("notify_req not found in fsp's requests");
278 DLIST_REMOVE(fsp
->notify
->requests
, req
);
279 DLIST_REMOVE(sconn
->smb1
.notify_mid_maps
, req
->mid_map
);
283 /****************************************************************************
284 Delete entries by mid from the change notify pending queue. Always send reply.
285 *****************************************************************************/
287 void remove_pending_change_notify_requests_by_mid(
288 struct smbd_server_connection
*sconn
, uint64_t mid
)
290 struct notify_mid_map
*map
;
292 for (map
= sconn
->smb1
.notify_mid_maps
; map
; map
= map
->next
) {
293 if (map
->mid
== mid
) {
302 change_notify_reply(map
->req
->req
,
303 NT_STATUS_CANCELLED
, 0, NULL
, map
->req
->reply_fn
);
304 change_notify_remove_request(sconn
, map
->req
);
307 void smbd_notify_cancel_by_smbreq(const struct smb_request
*smbreq
)
309 struct smbd_server_connection
*sconn
= smbreq
->sconn
;
310 struct notify_mid_map
*map
;
312 for (map
= sconn
->smb1
.notify_mid_maps
; map
; map
= map
->next
) {
313 if (map
->req
->req
== smbreq
) {
322 change_notify_reply(map
->req
->req
,
323 NT_STATUS_CANCELLED
, 0, NULL
, map
->req
->reply_fn
);
324 change_notify_remove_request(sconn
, map
->req
);
327 /****************************************************************************
328 Delete entries by fnum from the change notify pending queue.
329 *****************************************************************************/
331 void remove_pending_change_notify_requests_by_fid(files_struct
*fsp
,
334 if (fsp
->notify
== NULL
) {
338 while (fsp
->notify
->requests
!= NULL
) {
339 change_notify_reply(fsp
->notify
->requests
->req
,
341 fsp
->notify
->requests
->reply_fn
);
342 change_notify_remove_request(fsp
->conn
->sconn
,
343 fsp
->notify
->requests
);
347 void notify_fname(connection_struct
*conn
, uint32 action
, uint32 filter
,
354 if (path
[0] == '.' && path
[1] == '/') {
357 if (parent_dirname(talloc_tos(), path
, &parent
, &name
)) {
358 struct smb_filename smb_fname_parent
;
360 ZERO_STRUCT(smb_fname_parent
);
361 smb_fname_parent
.base_name
= parent
;
363 if (SMB_VFS_STAT(conn
, &smb_fname_parent
) != -1) {
364 notify_onelevel(conn
->notify_ctx
, action
, filter
,
365 SMB_VFS_FILE_ID_CREATE(conn
, &smb_fname_parent
.st
),
370 fullpath
= talloc_asprintf(talloc_tos(), "%s/%s", conn
->connectpath
,
372 if (fullpath
== NULL
) {
373 DEBUG(0, ("asprintf failed\n"));
376 notify_trigger(conn
->notify_ctx
, action
, filter
, fullpath
);
377 TALLOC_FREE(fullpath
);
380 static void notify_fsp(files_struct
*fsp
, uint32 action
, const char *name
)
382 struct notify_change
*change
, *changes
;
385 if (fsp
->notify
== NULL
) {
387 * Nobody is waiting, don't queue
393 * Someone has triggered a notify previously, queue the change for
397 if ((fsp
->notify
->num_changes
> 1000) || (name
== NULL
)) {
399 * The real number depends on the client buf, just provide a
400 * guard against a DoS here. If name == NULL the CN backend is
401 * alerting us to a problem. Possibly dropped events. Clear
402 * queued changes and send the catch-all response to the client
403 * if a request is pending.
405 TALLOC_FREE(fsp
->notify
->changes
);
406 fsp
->notify
->num_changes
= -1;
407 if (fsp
->notify
->requests
!= NULL
) {
408 change_notify_reply(fsp
->notify
->requests
->req
,
410 fsp
->notify
->requests
->max_param
,
412 fsp
->notify
->requests
->reply_fn
);
413 change_notify_remove_request(fsp
->conn
->sconn
,
414 fsp
->notify
->requests
);
419 /* If we've exceeded the server side queue or received a NULL name
420 * from the underlying CN implementation, don't queue up any more
421 * requests until we can send a catch-all response to the client */
422 if (fsp
->notify
->num_changes
== -1) {
426 if (!(changes
= TALLOC_REALLOC_ARRAY(
427 fsp
->notify
, fsp
->notify
->changes
,
428 struct notify_change
, fsp
->notify
->num_changes
+1))) {
429 DEBUG(0, ("talloc_realloc failed\n"));
433 fsp
->notify
->changes
= changes
;
435 change
= &(fsp
->notify
->changes
[fsp
->notify
->num_changes
]);
437 if (!(tmp
= talloc_strdup(changes
, name
))) {
438 DEBUG(0, ("talloc_strdup failed\n"));
442 string_replace(tmp
, '/', '\\');
445 change
->action
= action
;
446 fsp
->notify
->num_changes
+= 1;
448 if (fsp
->notify
->requests
== NULL
) {
450 * Nobody is waiting, so don't send anything. The ot
455 if (action
== NOTIFY_ACTION_OLD_NAME
) {
457 * We have to send the two rename events in one reply. So hold
458 * the first part back.
464 * Someone is waiting for the change, trigger the reply immediately.
466 * TODO: do we have to walk the lists of requests pending?
469 change_notify_reply(fsp
->notify
->requests
->req
,
471 fsp
->notify
->requests
->max_param
,
473 fsp
->notify
->requests
->reply_fn
);
475 change_notify_remove_request(fsp
->conn
->sconn
, fsp
->notify
->requests
);
478 char *notify_filter_string(TALLOC_CTX
*mem_ctx
, uint32 filter
)
482 result
= talloc_strdup(mem_ctx
, "");
484 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
485 result
= talloc_asprintf_append(result
, "FILE_NAME|");
486 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
487 result
= talloc_asprintf_append(result
, "DIR_NAME|");
488 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
489 result
= talloc_asprintf_append(result
, "ATTRIBUTES|");
490 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
491 result
= talloc_asprintf_append(result
, "SIZE|");
492 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
493 result
= talloc_asprintf_append(result
, "LAST_WRITE|");
494 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
495 result
= talloc_asprintf_append(result
, "LAST_ACCESS|");
496 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
497 result
= talloc_asprintf_append(result
, "CREATION|");
498 if (filter
& FILE_NOTIFY_CHANGE_EA
)
499 result
= talloc_asprintf_append(result
, "EA|");
500 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
501 result
= talloc_asprintf_append(result
, "SECURITY|");
502 if (filter
& FILE_NOTIFY_CHANGE_STREAM_NAME
)
503 result
= talloc_asprintf_append(result
, "STREAM_NAME|");
504 if (filter
& FILE_NOTIFY_CHANGE_STREAM_SIZE
)
505 result
= talloc_asprintf_append(result
, "STREAM_SIZE|");
506 if (filter
& FILE_NOTIFY_CHANGE_STREAM_WRITE
)
507 result
= talloc_asprintf_append(result
, "STREAM_WRITE|");
509 if (result
== NULL
) return NULL
;
510 if (*result
== '\0') return result
;
512 result
[strlen(result
)-1] = '\0';
516 struct sys_notify_context
*sys_notify_context_create(connection_struct
*conn
,
518 struct event_context
*ev
)
520 struct sys_notify_context
*ctx
;
522 if (!(ctx
= TALLOC_P(mem_ctx
, struct sys_notify_context
))) {
523 DEBUG(0, ("talloc failed\n"));
529 ctx
->private_data
= NULL
;
533 NTSTATUS
sys_notify_watch(struct sys_notify_context
*ctx
,
534 struct notify_entry
*e
,
535 void (*callback
)(struct sys_notify_context
*ctx
,
537 struct notify_event
*ev
),
538 void *private_data
, void *handle
)
540 return SMB_VFS_NOTIFY_WATCH(ctx
->conn
, ctx
, e
, callback
, private_data
,