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"
27 struct notify_change_buf
{
29 * If no requests are pending, changes are queued here. Simple array,
34 * num_changes == -1 means that we have got a catch-all change, when
35 * asked we just return NT_STATUS_OK without specific changes.
38 struct notify_change
*changes
;
41 * If no changes are around requests are queued here. Using a linked
42 * list, because we have to append at the end and delete from the top.
44 struct notify_change_request
*requests
;
47 struct notify_change_request
{
48 struct notify_change_request
*prev
, *next
;
49 struct files_struct
*fsp
; /* backpointer for cancel by mid */
50 struct smb_request
*req
;
53 void (*reply_fn
)(struct smb_request
*req
,
55 uint8_t *buf
, size_t len
);
56 struct notify_mid_map
*mid_map
;
60 static void notify_fsp(files_struct
*fsp
, uint32 action
, const char *name
);
62 bool change_notify_fsp_has_changes(struct files_struct
*fsp
)
68 if (fsp
->notify
== NULL
) {
72 if (fsp
->notify
->num_changes
== 0) {
80 * For NTCancel, we need to find the notify_change_request indexed by
81 * mid. Separate list here.
84 struct notify_mid_map
{
85 struct notify_mid_map
*prev
, *next
;
86 struct notify_change_request
*req
;
90 static bool notify_change_record_identical(struct notify_change
*c1
,
91 struct notify_change
*c2
)
93 /* Note this is deliberately case sensitive. */
94 if (c1
->action
== c2
->action
&&
95 strcmp(c1
->name
, c2
->name
) == 0) {
101 static bool notify_marshall_changes(int num_changes
,
103 struct notify_change
*changes
,
104 DATA_BLOB
*final_blob
)
108 if (num_changes
== -1) {
112 for (i
=0; i
<num_changes
; i
++) {
113 enum ndr_err_code ndr_err
;
114 struct notify_change
*c
;
115 struct FILE_NOTIFY_INFORMATION m
;
118 /* Coalesce any identical records. */
119 while (i
+1 < num_changes
&&
120 notify_change_record_identical(&changes
[i
],
127 m
.FileName1
= c
->name
;
128 m
.FileNameLength
= strlen_m(c
->name
)*2;
129 m
.Action
= c
->action
;
130 m
.NextEntryOffset
= (i
== num_changes
-1) ? 0 : ndr_size_FILE_NOTIFY_INFORMATION(&m
, 0);
133 * Offset to next entry, only if there is one
136 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &m
,
137 (ndr_push_flags_fn_t
)ndr_push_FILE_NOTIFY_INFORMATION
);
138 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
142 if (DEBUGLEVEL
>= 10) {
143 NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION
, &m
);
146 if (!data_blob_append(talloc_tos(), final_blob
,
147 blob
.data
, blob
.length
)) {
148 data_blob_free(&blob
);
152 data_blob_free(&blob
);
154 if (final_blob
->length
> max_offset
) {
155 /* Too much data for client. */
156 DEBUG(10, ("Client only wanted %d bytes, trying to "
157 "marshall %d bytes\n", (int)max_offset
,
158 (int)final_blob
->length
));
166 /****************************************************************************
167 Setup the common parts of the return packet and send it.
168 *****************************************************************************/
170 void change_notify_reply(struct smb_request
*req
,
173 struct notify_change_buf
*notify_buf
,
174 void (*reply_fn
)(struct smb_request
*req
,
176 uint8_t *buf
, size_t len
))
178 DATA_BLOB blob
= data_blob_null
;
180 if (!NT_STATUS_IS_OK(error_code
)) {
181 reply_fn(req
, error_code
, NULL
, 0);
185 if (max_param
== 0 || notify_buf
== NULL
) {
186 reply_fn(req
, NT_STATUS_OK
, NULL
, 0);
190 if (!notify_marshall_changes(notify_buf
->num_changes
, max_param
,
191 notify_buf
->changes
, &blob
)) {
193 * We exceed what the client is willing to accept. Send
196 data_blob_free(&blob
);
199 reply_fn(req
, NT_STATUS_OK
, blob
.data
, blob
.length
);
201 data_blob_free(&blob
);
203 TALLOC_FREE(notify_buf
->changes
);
204 notify_buf
->num_changes
= 0;
207 static void notify_callback(void *private_data
, const struct notify_event
*e
)
209 files_struct
*fsp
= (files_struct
*)private_data
;
210 DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp
)));
211 notify_fsp(fsp
, e
->action
, e
->path
);
214 static void sys_notify_callback(struct sys_notify_context
*ctx
,
216 struct notify_event
*e
)
218 files_struct
*fsp
= (files_struct
*)private_data
;
219 DEBUG(10, ("sys_notify_callback called for %s\n", fsp_str_dbg(fsp
)));
220 notify_fsp(fsp
, e
->action
, e
->path
);
223 NTSTATUS
change_notify_create(struct files_struct
*fsp
, uint32 filter
,
228 uint32_t subdir_filter
;
229 NTSTATUS status
= NT_STATUS_NOT_IMPLEMENTED
;
231 if (fsp
->notify
!= NULL
) {
232 DEBUG(1, ("change_notify_create: fsp->notify != NULL, "
233 "fname = %s\n", fsp
->fsp_name
->base_name
));
234 return NT_STATUS_INVALID_PARAMETER
;
237 if (!(fsp
->notify
= talloc_zero(NULL
, struct notify_change_buf
))) {
238 DEBUG(0, ("talloc failed\n"));
239 return NT_STATUS_NO_MEMORY
;
242 /* Do notify operations on the base_name. */
243 fullpath
= talloc_asprintf(
244 talloc_tos(), "%s/%s", fsp
->conn
->connectpath
,
245 fsp
->fsp_name
->base_name
);
246 if (fullpath
== NULL
) {
247 DEBUG(0, ("talloc_asprintf failed\n"));
248 TALLOC_FREE(fsp
->notify
);
249 return NT_STATUS_NO_MEMORY
;
253 * Avoid /. at the end of the path name. notify can't deal with it.
255 len
= strlen(fullpath
);
256 if (len
> 1 && fullpath
[len
-1] == '.' && fullpath
[len
-2] == '/') {
257 fullpath
[len
-2] = '\0';
260 subdir_filter
= recursive
? filter
: 0;
262 if (fsp
->conn
->sconn
->sys_notify_ctx
!= NULL
) {
263 void *sys_notify_handle
= NULL
;
265 status
= SMB_VFS_NOTIFY_WATCH(
266 fsp
->conn
, fsp
->conn
->sconn
->sys_notify_ctx
,
267 fullpath
, &filter
, &subdir_filter
,
268 sys_notify_callback
, fsp
, &sys_notify_handle
);
270 if (NT_STATUS_IS_OK(status
)) {
271 talloc_steal(fsp
->notify
, sys_notify_handle
);
275 if ((filter
!= 0) || (subdir_filter
!= 0)) {
276 status
= notify_add(fsp
->conn
->sconn
->notify_ctx
,
277 fullpath
, filter
, subdir_filter
,
278 notify_callback
, fsp
);
280 TALLOC_FREE(fullpath
);
284 NTSTATUS
change_notify_add_request(struct smb_request
*req
,
286 uint32 filter
, bool recursive
,
287 struct files_struct
*fsp
,
288 void (*reply_fn
)(struct smb_request
*req
,
290 uint8_t *buf
, size_t len
))
292 struct notify_change_request
*request
= NULL
;
293 struct notify_mid_map
*map
= NULL
;
294 struct smbd_server_connection
*sconn
= req
->sconn
;
296 DEBUG(10, ("change_notify_add_request: Adding request for %s: "
297 "max_param = %d\n", fsp_str_dbg(fsp
), (int)max_param
));
299 if (!(request
= talloc(NULL
, struct notify_change_request
))
300 || !(map
= talloc(request
, struct notify_mid_map
))) {
301 TALLOC_FREE(request
);
302 return NT_STATUS_NO_MEMORY
;
305 request
->mid_map
= map
;
308 request
->req
= talloc_move(request
, &req
);
309 request
->max_param
= max_param
;
310 request
->filter
= filter
;
312 request
->reply_fn
= reply_fn
;
313 request
->backend_data
= NULL
;
315 DLIST_ADD_END(fsp
->notify
->requests
, request
,
316 struct notify_change_request
*);
318 map
->mid
= request
->req
->mid
;
319 DLIST_ADD(sconn
->smb1
.notify_mid_maps
, map
);
324 static void change_notify_remove_request(struct smbd_server_connection
*sconn
,
325 struct notify_change_request
*remove_req
)
328 struct notify_change_request
*req
;
331 * Paranoia checks, the fsp referenced must must have the request in
332 * its list of pending requests
335 fsp
= remove_req
->fsp
;
336 SMB_ASSERT(fsp
->notify
!= NULL
);
338 for (req
= fsp
->notify
->requests
; req
; req
= req
->next
) {
339 if (req
== remove_req
) {
345 smb_panic("notify_req not found in fsp's requests");
348 DLIST_REMOVE(fsp
->notify
->requests
, req
);
349 DLIST_REMOVE(sconn
->smb1
.notify_mid_maps
, req
->mid_map
);
353 /****************************************************************************
354 Delete entries by mid from the change notify pending queue. Always send reply.
355 *****************************************************************************/
357 void remove_pending_change_notify_requests_by_mid(
358 struct smbd_server_connection
*sconn
, uint64_t mid
)
360 struct notify_mid_map
*map
;
362 for (map
= sconn
->smb1
.notify_mid_maps
; map
; map
= map
->next
) {
363 if (map
->mid
== mid
) {
372 change_notify_reply(map
->req
->req
,
373 NT_STATUS_CANCELLED
, 0, NULL
, map
->req
->reply_fn
);
374 change_notify_remove_request(sconn
, map
->req
);
377 void smbd_notify_cancel_by_smbreq(const struct smb_request
*smbreq
)
379 struct smbd_server_connection
*sconn
= smbreq
->sconn
;
380 struct notify_mid_map
*map
;
382 for (map
= sconn
->smb1
.notify_mid_maps
; map
; map
= map
->next
) {
383 if (map
->req
->req
== smbreq
) {
392 change_notify_reply(map
->req
->req
,
393 NT_STATUS_CANCELLED
, 0, NULL
, map
->req
->reply_fn
);
394 change_notify_remove_request(sconn
, map
->req
);
397 /****************************************************************************
398 Delete entries by fnum from the change notify pending queue.
399 *****************************************************************************/
401 void remove_pending_change_notify_requests_by_fid(files_struct
*fsp
,
404 if (fsp
->notify
== NULL
) {
408 while (fsp
->notify
->requests
!= NULL
) {
409 change_notify_reply(fsp
->notify
->requests
->req
,
411 fsp
->notify
->requests
->reply_fn
);
412 change_notify_remove_request(fsp
->conn
->sconn
,
413 fsp
->notify
->requests
);
417 void notify_fname(connection_struct
*conn
, uint32 action
, uint32 filter
,
420 struct notify_context
*notify_ctx
= conn
->sconn
->notify_ctx
;
421 char *fullpath
, *to_free
;
422 char tmpbuf
[PATH_MAX
];
425 if (path
[0] == '.' && path
[1] == '/') {
428 len
= full_path_tos(conn
->connectpath
, path
, tmpbuf
, sizeof(tmpbuf
),
429 &fullpath
, &to_free
);
431 DEBUG(0, ("full_path_tos failed\n"));
434 notify_trigger(notify_ctx
, action
, filter
, fullpath
);
435 TALLOC_FREE(to_free
);
438 static void notify_fsp(files_struct
*fsp
, uint32 action
, const char *name
)
440 struct notify_change
*change
, *changes
;
443 if (fsp
->notify
== NULL
) {
445 * Nobody is waiting, don't queue
451 * Someone has triggered a notify previously, queue the change for
455 if ((fsp
->notify
->num_changes
> 1000) || (name
== NULL
)) {
457 * The real number depends on the client buf, just provide a
458 * guard against a DoS here. If name == NULL the CN backend is
459 * alerting us to a problem. Possibly dropped events. Clear
460 * queued changes and send the catch-all response to the client
461 * if a request is pending.
463 TALLOC_FREE(fsp
->notify
->changes
);
464 fsp
->notify
->num_changes
= -1;
465 if (fsp
->notify
->requests
!= NULL
) {
466 change_notify_reply(fsp
->notify
->requests
->req
,
468 fsp
->notify
->requests
->max_param
,
470 fsp
->notify
->requests
->reply_fn
);
471 change_notify_remove_request(fsp
->conn
->sconn
,
472 fsp
->notify
->requests
);
477 /* If we've exceeded the server side queue or received a NULL name
478 * from the underlying CN implementation, don't queue up any more
479 * requests until we can send a catch-all response to the client */
480 if (fsp
->notify
->num_changes
== -1) {
484 if (!(changes
= talloc_realloc(
485 fsp
->notify
, fsp
->notify
->changes
,
486 struct notify_change
, fsp
->notify
->num_changes
+1))) {
487 DEBUG(0, ("talloc_realloc failed\n"));
491 fsp
->notify
->changes
= changes
;
493 change
= &(fsp
->notify
->changes
[fsp
->notify
->num_changes
]);
495 if (!(tmp
= talloc_strdup(changes
, name
))) {
496 DEBUG(0, ("talloc_strdup failed\n"));
500 string_replace(tmp
, '/', '\\');
503 change
->action
= action
;
504 fsp
->notify
->num_changes
+= 1;
506 if (fsp
->notify
->requests
== NULL
) {
508 * Nobody is waiting, so don't send anything. The ot
513 if (action
== NOTIFY_ACTION_OLD_NAME
) {
515 * We have to send the two rename events in one reply. So hold
516 * the first part back.
522 * Someone is waiting for the change, trigger the reply immediately.
524 * TODO: do we have to walk the lists of requests pending?
527 change_notify_reply(fsp
->notify
->requests
->req
,
529 fsp
->notify
->requests
->max_param
,
531 fsp
->notify
->requests
->reply_fn
);
533 change_notify_remove_request(fsp
->conn
->sconn
, fsp
->notify
->requests
);
536 char *notify_filter_string(TALLOC_CTX
*mem_ctx
, uint32 filter
)
540 result
= talloc_strdup(mem_ctx
, "");
542 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
543 result
= talloc_asprintf_append(result
, "FILE_NAME|");
544 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
545 result
= talloc_asprintf_append(result
, "DIR_NAME|");
546 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
547 result
= talloc_asprintf_append(result
, "ATTRIBUTES|");
548 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
549 result
= talloc_asprintf_append(result
, "SIZE|");
550 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
551 result
= talloc_asprintf_append(result
, "LAST_WRITE|");
552 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
553 result
= talloc_asprintf_append(result
, "LAST_ACCESS|");
554 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
555 result
= talloc_asprintf_append(result
, "CREATION|");
556 if (filter
& FILE_NOTIFY_CHANGE_EA
)
557 result
= talloc_asprintf_append(result
, "EA|");
558 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
559 result
= talloc_asprintf_append(result
, "SECURITY|");
560 if (filter
& FILE_NOTIFY_CHANGE_STREAM_NAME
)
561 result
= talloc_asprintf_append(result
, "STREAM_NAME|");
562 if (filter
& FILE_NOTIFY_CHANGE_STREAM_SIZE
)
563 result
= talloc_asprintf_append(result
, "STREAM_SIZE|");
564 if (filter
& FILE_NOTIFY_CHANGE_STREAM_WRITE
)
565 result
= talloc_asprintf_append(result
, "STREAM_WRITE|");
567 if (result
== NULL
) return NULL
;
568 if (*result
== '\0') return result
;
570 result
[strlen(result
)-1] = '\0';
574 struct sys_notify_context
*sys_notify_context_create(TALLOC_CTX
*mem_ctx
,
575 struct tevent_context
*ev
)
577 struct sys_notify_context
*ctx
;
579 if (!(ctx
= talloc(mem_ctx
, struct sys_notify_context
))) {
580 DEBUG(0, ("talloc failed\n"));
585 ctx
->private_data
= NULL
;