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_event
{
33 struct notify_change_buf
{
35 * If no requests are pending, changes are queued here. Simple array,
40 * num_changes == -1 means that we have got a catch-all change, when
41 * asked we just return NT_STATUS_OK without specific changes.
44 struct notify_change_event
*changes
;
47 * If no changes are around requests are queued here. Using a linked
48 * list, because we have to append at the end and delete from the top.
50 struct notify_change_request
*requests
;
53 struct notify_change_request
{
54 struct notify_change_request
*prev
, *next
;
55 struct files_struct
*fsp
; /* backpointer for cancel by mid */
56 struct smb_request
*req
;
59 void (*reply_fn
)(struct smb_request
*req
,
61 uint8_t *buf
, size_t len
);
62 struct notify_mid_map
*mid_map
;
66 static void notify_fsp(files_struct
*fsp
, struct timespec when
,
67 uint32 action
, const char *name
);
69 bool change_notify_fsp_has_changes(struct files_struct
*fsp
)
75 if (fsp
->notify
== NULL
) {
79 if (fsp
->notify
->num_changes
== 0) {
87 * For NTCancel, we need to find the notify_change_request indexed by
88 * mid. Separate list here.
91 struct notify_mid_map
{
92 struct notify_mid_map
*prev
, *next
;
93 struct notify_change_request
*req
;
97 static bool notify_change_record_identical(struct notify_change_event
*c1
,
98 struct notify_change_event
*c2
)
100 /* Note this is deliberately case sensitive. */
101 if (c1
->action
== c2
->action
&&
102 strcmp(c1
->name
, c2
->name
) == 0) {
108 static bool notify_marshall_changes(int num_changes
,
110 struct notify_change_event
*changes
,
111 DATA_BLOB
*final_blob
)
115 if (num_changes
== -1) {
119 for (i
=0; i
<num_changes
; i
++) {
120 enum ndr_err_code ndr_err
;
121 struct notify_change_event
*c
;
122 struct FILE_NOTIFY_INFORMATION m
;
125 /* Coalesce any identical records. */
126 while (i
+1 < num_changes
&&
127 notify_change_record_identical(&changes
[i
],
134 m
.FileName1
= c
->name
;
135 m
.FileNameLength
= strlen_m(c
->name
)*2;
136 m
.Action
= c
->action
;
137 m
.NextEntryOffset
= (i
== num_changes
-1) ? 0 : ndr_size_FILE_NOTIFY_INFORMATION(&m
, 0);
140 * Offset to next entry, only if there is one
143 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &m
,
144 (ndr_push_flags_fn_t
)ndr_push_FILE_NOTIFY_INFORMATION
);
145 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
149 if (DEBUGLEVEL
>= 10) {
150 NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION
, &m
);
153 if (!data_blob_append(talloc_tos(), final_blob
,
154 blob
.data
, blob
.length
)) {
155 data_blob_free(&blob
);
159 data_blob_free(&blob
);
161 if (final_blob
->length
> max_offset
) {
162 /* Too much data for client. */
163 DEBUG(10, ("Client only wanted %d bytes, trying to "
164 "marshall %d bytes\n", (int)max_offset
,
165 (int)final_blob
->length
));
173 /****************************************************************************
174 Setup the common parts of the return packet and send it.
175 *****************************************************************************/
177 void change_notify_reply(struct smb_request
*req
,
180 struct notify_change_buf
*notify_buf
,
181 void (*reply_fn
)(struct smb_request
*req
,
183 uint8_t *buf
, size_t len
))
185 DATA_BLOB blob
= data_blob_null
;
187 if (!NT_STATUS_IS_OK(error_code
)) {
188 reply_fn(req
, error_code
, NULL
, 0);
192 if (max_param
== 0 || notify_buf
== NULL
) {
193 reply_fn(req
, NT_STATUS_OK
, NULL
, 0);
197 if (!notify_marshall_changes(notify_buf
->num_changes
, max_param
,
198 notify_buf
->changes
, &blob
)) {
200 * We exceed what the client is willing to accept. Send
203 data_blob_free(&blob
);
206 reply_fn(req
, NT_STATUS_OK
, blob
.data
, blob
.length
);
208 data_blob_free(&blob
);
210 TALLOC_FREE(notify_buf
->changes
);
211 notify_buf
->num_changes
= 0;
214 static void notify_callback(void *private_data
, struct timespec when
,
215 const struct notify_event
*e
)
217 files_struct
*fsp
= (files_struct
*)private_data
;
218 DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp
)));
219 notify_fsp(fsp
, when
, e
->action
, e
->path
);
222 static void sys_notify_callback(struct sys_notify_context
*ctx
,
224 struct notify_event
*e
)
226 files_struct
*fsp
= (files_struct
*)private_data
;
227 DEBUG(10, ("sys_notify_callback called for %s\n", fsp_str_dbg(fsp
)));
228 notify_fsp(fsp
, timespec_current(), e
->action
, e
->path
);
231 NTSTATUS
change_notify_create(struct files_struct
*fsp
, uint32 filter
,
236 uint32_t subdir_filter
;
237 NTSTATUS status
= NT_STATUS_NOT_IMPLEMENTED
;
239 if (fsp
->notify
!= NULL
) {
240 DEBUG(1, ("change_notify_create: fsp->notify != NULL, "
241 "fname = %s\n", fsp
->fsp_name
->base_name
));
242 return NT_STATUS_INVALID_PARAMETER
;
245 if (!(fsp
->notify
= talloc_zero(NULL
, struct notify_change_buf
))) {
246 DEBUG(0, ("talloc failed\n"));
247 return NT_STATUS_NO_MEMORY
;
250 /* Do notify operations on the base_name. */
251 fullpath
= talloc_asprintf(
252 talloc_tos(), "%s/%s", fsp
->conn
->connectpath
,
253 fsp
->fsp_name
->base_name
);
254 if (fullpath
== NULL
) {
255 DEBUG(0, ("talloc_asprintf failed\n"));
256 TALLOC_FREE(fsp
->notify
);
257 return NT_STATUS_NO_MEMORY
;
261 * Avoid /. at the end of the path name. notify can't deal with it.
263 len
= strlen(fullpath
);
264 if (len
> 1 && fullpath
[len
-1] == '.' && fullpath
[len
-2] == '/') {
265 fullpath
[len
-2] = '\0';
268 subdir_filter
= recursive
? filter
: 0;
270 if (fsp
->conn
->sconn
->sys_notify_ctx
!= NULL
) {
271 void *sys_notify_handle
= NULL
;
273 status
= SMB_VFS_NOTIFY_WATCH(
274 fsp
->conn
, fsp
->conn
->sconn
->sys_notify_ctx
,
275 fullpath
, &filter
, &subdir_filter
,
276 sys_notify_callback
, fsp
, &sys_notify_handle
);
278 if (NT_STATUS_IS_OK(status
)) {
279 talloc_steal(fsp
->notify
, sys_notify_handle
);
283 if ((filter
!= 0) || (subdir_filter
!= 0)) {
284 status
= notify_add(fsp
->conn
->sconn
->notify_ctx
,
285 fullpath
, filter
, subdir_filter
,
286 notify_callback
, fsp
);
288 TALLOC_FREE(fullpath
);
292 NTSTATUS
change_notify_add_request(struct smb_request
*req
,
294 uint32 filter
, bool recursive
,
295 struct files_struct
*fsp
,
296 void (*reply_fn
)(struct smb_request
*req
,
298 uint8_t *buf
, size_t len
))
300 struct notify_change_request
*request
= NULL
;
301 struct notify_mid_map
*map
= NULL
;
302 struct smbd_server_connection
*sconn
= req
->sconn
;
304 DEBUG(10, ("change_notify_add_request: Adding request for %s: "
305 "max_param = %d\n", fsp_str_dbg(fsp
), (int)max_param
));
307 if (!(request
= talloc(NULL
, struct notify_change_request
))
308 || !(map
= talloc(request
, struct notify_mid_map
))) {
309 TALLOC_FREE(request
);
310 return NT_STATUS_NO_MEMORY
;
313 request
->mid_map
= map
;
316 request
->req
= talloc_move(request
, &req
);
317 request
->max_param
= max_param
;
318 request
->filter
= filter
;
320 request
->reply_fn
= reply_fn
;
321 request
->backend_data
= NULL
;
323 DLIST_ADD_END(fsp
->notify
->requests
, request
,
324 struct notify_change_request
*);
326 map
->mid
= request
->req
->mid
;
327 DLIST_ADD(sconn
->smb1
.notify_mid_maps
, map
);
332 static void change_notify_remove_request(struct smbd_server_connection
*sconn
,
333 struct notify_change_request
*remove_req
)
336 struct notify_change_request
*req
;
339 * Paranoia checks, the fsp referenced must must have the request in
340 * its list of pending requests
343 fsp
= remove_req
->fsp
;
344 SMB_ASSERT(fsp
->notify
!= NULL
);
346 for (req
= fsp
->notify
->requests
; req
; req
= req
->next
) {
347 if (req
== remove_req
) {
353 smb_panic("notify_req not found in fsp's requests");
356 DLIST_REMOVE(fsp
->notify
->requests
, req
);
357 DLIST_REMOVE(sconn
->smb1
.notify_mid_maps
, req
->mid_map
);
361 /****************************************************************************
362 Delete entries by mid from the change notify pending queue. Always send reply.
363 *****************************************************************************/
365 void remove_pending_change_notify_requests_by_mid(
366 struct smbd_server_connection
*sconn
, uint64_t mid
)
368 struct notify_mid_map
*map
;
370 for (map
= sconn
->smb1
.notify_mid_maps
; map
; map
= map
->next
) {
371 if (map
->mid
== mid
) {
380 change_notify_reply(map
->req
->req
,
381 NT_STATUS_CANCELLED
, 0, NULL
, map
->req
->reply_fn
);
382 change_notify_remove_request(sconn
, map
->req
);
385 void smbd_notify_cancel_by_smbreq(const struct smb_request
*smbreq
)
387 struct smbd_server_connection
*sconn
= smbreq
->sconn
;
388 struct notify_mid_map
*map
;
390 for (map
= sconn
->smb1
.notify_mid_maps
; map
; map
= map
->next
) {
391 if (map
->req
->req
== smbreq
) {
400 change_notify_reply(map
->req
->req
,
401 NT_STATUS_CANCELLED
, 0, NULL
, map
->req
->reply_fn
);
402 change_notify_remove_request(sconn
, map
->req
);
405 /****************************************************************************
406 Delete entries by fnum from the change notify pending queue.
407 *****************************************************************************/
409 void remove_pending_change_notify_requests_by_fid(files_struct
*fsp
,
412 if (fsp
->notify
== NULL
) {
416 while (fsp
->notify
->requests
!= NULL
) {
417 change_notify_reply(fsp
->notify
->requests
->req
,
419 fsp
->notify
->requests
->reply_fn
);
420 change_notify_remove_request(fsp
->conn
->sconn
,
421 fsp
->notify
->requests
);
425 void notify_fname(connection_struct
*conn
, uint32 action
, uint32 filter
,
428 struct notify_context
*notify_ctx
= conn
->sconn
->notify_ctx
;
429 char *fullpath
, *to_free
;
430 char tmpbuf
[PATH_MAX
];
433 if (path
[0] == '.' && path
[1] == '/') {
436 len
= full_path_tos(conn
->connectpath
, path
, tmpbuf
, sizeof(tmpbuf
),
437 &fullpath
, &to_free
);
439 DEBUG(0, ("full_path_tos failed\n"));
442 notify_trigger(notify_ctx
, action
, filter
, fullpath
);
443 TALLOC_FREE(to_free
);
446 static void notify_fsp(files_struct
*fsp
, struct timespec when
,
447 uint32 action
, const char *name
)
449 struct notify_change_event
*change
, *changes
;
452 if (fsp
->notify
== NULL
) {
454 * Nobody is waiting, don't queue
460 * Someone has triggered a notify previously, queue the change for
464 if ((fsp
->notify
->num_changes
> 1000) || (name
== NULL
)) {
466 * The real number depends on the client buf, just provide a
467 * guard against a DoS here. If name == NULL the CN backend is
468 * alerting us to a problem. Possibly dropped events. Clear
469 * queued changes and send the catch-all response to the client
470 * if a request is pending.
472 TALLOC_FREE(fsp
->notify
->changes
);
473 fsp
->notify
->num_changes
= -1;
474 if (fsp
->notify
->requests
!= NULL
) {
475 change_notify_reply(fsp
->notify
->requests
->req
,
477 fsp
->notify
->requests
->max_param
,
479 fsp
->notify
->requests
->reply_fn
);
480 change_notify_remove_request(fsp
->conn
->sconn
,
481 fsp
->notify
->requests
);
486 /* If we've exceeded the server side queue or received a NULL name
487 * from the underlying CN implementation, don't queue up any more
488 * requests until we can send a catch-all response to the client */
489 if (fsp
->notify
->num_changes
== -1) {
493 if (!(changes
= talloc_realloc(
494 fsp
->notify
, fsp
->notify
->changes
,
495 struct notify_change_event
,
496 fsp
->notify
->num_changes
+1))) {
497 DEBUG(0, ("talloc_realloc failed\n"));
501 fsp
->notify
->changes
= changes
;
503 change
= &(fsp
->notify
->changes
[fsp
->notify
->num_changes
]);
505 if (!(tmp
= talloc_strdup(changes
, name
))) {
506 DEBUG(0, ("talloc_strdup failed\n"));
510 string_replace(tmp
, '/', '\\');
514 change
->action
= action
;
515 fsp
->notify
->num_changes
+= 1;
517 if (fsp
->notify
->requests
== NULL
) {
519 * Nobody is waiting, so don't send anything. The ot
524 if (action
== NOTIFY_ACTION_OLD_NAME
) {
526 * We have to send the two rename events in one reply. So hold
527 * the first part back.
533 * Someone is waiting for the change, trigger the reply immediately.
535 * TODO: do we have to walk the lists of requests pending?
538 change_notify_reply(fsp
->notify
->requests
->req
,
540 fsp
->notify
->requests
->max_param
,
542 fsp
->notify
->requests
->reply_fn
);
544 change_notify_remove_request(fsp
->conn
->sconn
, fsp
->notify
->requests
);
547 char *notify_filter_string(TALLOC_CTX
*mem_ctx
, uint32 filter
)
551 result
= talloc_strdup(mem_ctx
, "");
553 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
554 result
= talloc_asprintf_append(result
, "FILE_NAME|");
555 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
556 result
= talloc_asprintf_append(result
, "DIR_NAME|");
557 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
558 result
= talloc_asprintf_append(result
, "ATTRIBUTES|");
559 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
560 result
= talloc_asprintf_append(result
, "SIZE|");
561 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
562 result
= talloc_asprintf_append(result
, "LAST_WRITE|");
563 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
564 result
= talloc_asprintf_append(result
, "LAST_ACCESS|");
565 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
566 result
= talloc_asprintf_append(result
, "CREATION|");
567 if (filter
& FILE_NOTIFY_CHANGE_EA
)
568 result
= talloc_asprintf_append(result
, "EA|");
569 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
570 result
= talloc_asprintf_append(result
, "SECURITY|");
571 if (filter
& FILE_NOTIFY_CHANGE_STREAM_NAME
)
572 result
= talloc_asprintf_append(result
, "STREAM_NAME|");
573 if (filter
& FILE_NOTIFY_CHANGE_STREAM_SIZE
)
574 result
= talloc_asprintf_append(result
, "STREAM_SIZE|");
575 if (filter
& FILE_NOTIFY_CHANGE_STREAM_WRITE
)
576 result
= talloc_asprintf_append(result
, "STREAM_WRITE|");
578 if (result
== NULL
) return NULL
;
579 if (*result
== '\0') return result
;
581 result
[strlen(result
)-1] = '\0';
585 struct sys_notify_context
*sys_notify_context_create(TALLOC_CTX
*mem_ctx
,
586 struct tevent_context
*ev
)
588 struct sys_notify_context
*ctx
;
590 if (!(ctx
= talloc(mem_ctx
, struct sys_notify_context
))) {
591 DEBUG(0, ("talloc failed\n"));
596 ctx
->private_data
= NULL
;