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
, uint32 action
, const char *name
);
68 bool change_notify_fsp_has_changes(struct files_struct
*fsp
)
74 if (fsp
->notify
== NULL
) {
78 if (fsp
->notify
->num_changes
== 0) {
86 * For NTCancel, we need to find the notify_change_request indexed by
87 * mid. Separate list here.
90 struct notify_mid_map
{
91 struct notify_mid_map
*prev
, *next
;
92 struct notify_change_request
*req
;
96 static bool notify_change_record_identical(struct notify_change_event
*c1
,
97 struct notify_change_event
*c2
)
99 /* Note this is deliberately case sensitive. */
100 if (c1
->action
== c2
->action
&&
101 strcmp(c1
->name
, c2
->name
) == 0) {
107 static bool notify_marshall_changes(int num_changes
,
109 struct notify_change_event
*changes
,
110 DATA_BLOB
*final_blob
)
114 if (num_changes
== -1) {
118 for (i
=0; i
<num_changes
; i
++) {
119 enum ndr_err_code ndr_err
;
120 struct notify_change_event
*c
;
121 struct FILE_NOTIFY_INFORMATION m
;
124 /* Coalesce any identical records. */
125 while (i
+1 < num_changes
&&
126 notify_change_record_identical(&changes
[i
],
133 m
.FileName1
= c
->name
;
134 m
.FileNameLength
= strlen_m(c
->name
)*2;
135 m
.Action
= c
->action
;
136 m
.NextEntryOffset
= (i
== num_changes
-1) ? 0 : ndr_size_FILE_NOTIFY_INFORMATION(&m
, 0);
139 * Offset to next entry, only if there is one
142 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &m
,
143 (ndr_push_flags_fn_t
)ndr_push_FILE_NOTIFY_INFORMATION
);
144 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
148 if (DEBUGLEVEL
>= 10) {
149 NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION
, &m
);
152 if (!data_blob_append(talloc_tos(), final_blob
,
153 blob
.data
, blob
.length
)) {
154 data_blob_free(&blob
);
158 data_blob_free(&blob
);
160 if (final_blob
->length
> max_offset
) {
161 /* Too much data for client. */
162 DEBUG(10, ("Client only wanted %d bytes, trying to "
163 "marshall %d bytes\n", (int)max_offset
,
164 (int)final_blob
->length
));
172 /****************************************************************************
173 Setup the common parts of the return packet and send it.
174 *****************************************************************************/
176 void change_notify_reply(struct smb_request
*req
,
179 struct notify_change_buf
*notify_buf
,
180 void (*reply_fn
)(struct smb_request
*req
,
182 uint8_t *buf
, size_t len
))
184 DATA_BLOB blob
= data_blob_null
;
186 if (!NT_STATUS_IS_OK(error_code
)) {
187 reply_fn(req
, error_code
, NULL
, 0);
191 if (max_param
== 0 || notify_buf
== NULL
) {
192 reply_fn(req
, NT_STATUS_OK
, NULL
, 0);
196 if (!notify_marshall_changes(notify_buf
->num_changes
, max_param
,
197 notify_buf
->changes
, &blob
)) {
199 * We exceed what the client is willing to accept. Send
202 data_blob_free(&blob
);
205 reply_fn(req
, NT_STATUS_OK
, blob
.data
, blob
.length
);
207 data_blob_free(&blob
);
209 TALLOC_FREE(notify_buf
->changes
);
210 notify_buf
->num_changes
= 0;
213 static void notify_callback(void *private_data
, const struct notify_event
*e
)
215 files_struct
*fsp
= (files_struct
*)private_data
;
216 DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp
)));
217 notify_fsp(fsp
, e
->action
, e
->path
);
220 static void sys_notify_callback(struct sys_notify_context
*ctx
,
222 struct notify_event
*e
)
224 files_struct
*fsp
= (files_struct
*)private_data
;
225 DEBUG(10, ("sys_notify_callback called for %s\n", fsp_str_dbg(fsp
)));
226 notify_fsp(fsp
, e
->action
, e
->path
);
229 NTSTATUS
change_notify_create(struct files_struct
*fsp
, uint32 filter
,
234 uint32_t subdir_filter
;
235 NTSTATUS status
= NT_STATUS_NOT_IMPLEMENTED
;
237 if (fsp
->notify
!= NULL
) {
238 DEBUG(1, ("change_notify_create: fsp->notify != NULL, "
239 "fname = %s\n", fsp
->fsp_name
->base_name
));
240 return NT_STATUS_INVALID_PARAMETER
;
243 if (!(fsp
->notify
= talloc_zero(NULL
, struct notify_change_buf
))) {
244 DEBUG(0, ("talloc failed\n"));
245 return NT_STATUS_NO_MEMORY
;
248 /* Do notify operations on the base_name. */
249 fullpath
= talloc_asprintf(
250 talloc_tos(), "%s/%s", fsp
->conn
->connectpath
,
251 fsp
->fsp_name
->base_name
);
252 if (fullpath
== NULL
) {
253 DEBUG(0, ("talloc_asprintf failed\n"));
254 TALLOC_FREE(fsp
->notify
);
255 return NT_STATUS_NO_MEMORY
;
259 * Avoid /. at the end of the path name. notify can't deal with it.
261 len
= strlen(fullpath
);
262 if (len
> 1 && fullpath
[len
-1] == '.' && fullpath
[len
-2] == '/') {
263 fullpath
[len
-2] = '\0';
266 subdir_filter
= recursive
? filter
: 0;
268 if (fsp
->conn
->sconn
->sys_notify_ctx
!= NULL
) {
269 void *sys_notify_handle
= NULL
;
271 status
= SMB_VFS_NOTIFY_WATCH(
272 fsp
->conn
, fsp
->conn
->sconn
->sys_notify_ctx
,
273 fullpath
, &filter
, &subdir_filter
,
274 sys_notify_callback
, fsp
, &sys_notify_handle
);
276 if (NT_STATUS_IS_OK(status
)) {
277 talloc_steal(fsp
->notify
, sys_notify_handle
);
281 if ((filter
!= 0) || (subdir_filter
!= 0)) {
282 status
= notify_add(fsp
->conn
->sconn
->notify_ctx
,
283 fullpath
, filter
, subdir_filter
,
284 notify_callback
, fsp
);
286 TALLOC_FREE(fullpath
);
290 NTSTATUS
change_notify_add_request(struct smb_request
*req
,
292 uint32 filter
, bool recursive
,
293 struct files_struct
*fsp
,
294 void (*reply_fn
)(struct smb_request
*req
,
296 uint8_t *buf
, size_t len
))
298 struct notify_change_request
*request
= NULL
;
299 struct notify_mid_map
*map
= NULL
;
300 struct smbd_server_connection
*sconn
= req
->sconn
;
302 DEBUG(10, ("change_notify_add_request: Adding request for %s: "
303 "max_param = %d\n", fsp_str_dbg(fsp
), (int)max_param
));
305 if (!(request
= talloc(NULL
, struct notify_change_request
))
306 || !(map
= talloc(request
, struct notify_mid_map
))) {
307 TALLOC_FREE(request
);
308 return NT_STATUS_NO_MEMORY
;
311 request
->mid_map
= map
;
314 request
->req
= talloc_move(request
, &req
);
315 request
->max_param
= max_param
;
316 request
->filter
= filter
;
318 request
->reply_fn
= reply_fn
;
319 request
->backend_data
= NULL
;
321 DLIST_ADD_END(fsp
->notify
->requests
, request
,
322 struct notify_change_request
*);
324 map
->mid
= request
->req
->mid
;
325 DLIST_ADD(sconn
->smb1
.notify_mid_maps
, map
);
330 static void change_notify_remove_request(struct smbd_server_connection
*sconn
,
331 struct notify_change_request
*remove_req
)
334 struct notify_change_request
*req
;
337 * Paranoia checks, the fsp referenced must must have the request in
338 * its list of pending requests
341 fsp
= remove_req
->fsp
;
342 SMB_ASSERT(fsp
->notify
!= NULL
);
344 for (req
= fsp
->notify
->requests
; req
; req
= req
->next
) {
345 if (req
== remove_req
) {
351 smb_panic("notify_req not found in fsp's requests");
354 DLIST_REMOVE(fsp
->notify
->requests
, req
);
355 DLIST_REMOVE(sconn
->smb1
.notify_mid_maps
, req
->mid_map
);
359 /****************************************************************************
360 Delete entries by mid from the change notify pending queue. Always send reply.
361 *****************************************************************************/
363 void remove_pending_change_notify_requests_by_mid(
364 struct smbd_server_connection
*sconn
, uint64_t mid
)
366 struct notify_mid_map
*map
;
368 for (map
= sconn
->smb1
.notify_mid_maps
; map
; map
= map
->next
) {
369 if (map
->mid
== mid
) {
378 change_notify_reply(map
->req
->req
,
379 NT_STATUS_CANCELLED
, 0, NULL
, map
->req
->reply_fn
);
380 change_notify_remove_request(sconn
, map
->req
);
383 void smbd_notify_cancel_by_smbreq(const struct smb_request
*smbreq
)
385 struct smbd_server_connection
*sconn
= smbreq
->sconn
;
386 struct notify_mid_map
*map
;
388 for (map
= sconn
->smb1
.notify_mid_maps
; map
; map
= map
->next
) {
389 if (map
->req
->req
== smbreq
) {
398 change_notify_reply(map
->req
->req
,
399 NT_STATUS_CANCELLED
, 0, NULL
, map
->req
->reply_fn
);
400 change_notify_remove_request(sconn
, map
->req
);
403 /****************************************************************************
404 Delete entries by fnum from the change notify pending queue.
405 *****************************************************************************/
407 void remove_pending_change_notify_requests_by_fid(files_struct
*fsp
,
410 if (fsp
->notify
== NULL
) {
414 while (fsp
->notify
->requests
!= NULL
) {
415 change_notify_reply(fsp
->notify
->requests
->req
,
417 fsp
->notify
->requests
->reply_fn
);
418 change_notify_remove_request(fsp
->conn
->sconn
,
419 fsp
->notify
->requests
);
423 void notify_fname(connection_struct
*conn
, uint32 action
, uint32 filter
,
426 struct notify_context
*notify_ctx
= conn
->sconn
->notify_ctx
;
427 char *fullpath
, *to_free
;
428 char tmpbuf
[PATH_MAX
];
431 if (path
[0] == '.' && path
[1] == '/') {
434 len
= full_path_tos(conn
->connectpath
, path
, tmpbuf
, sizeof(tmpbuf
),
435 &fullpath
, &to_free
);
437 DEBUG(0, ("full_path_tos failed\n"));
440 notify_trigger(notify_ctx
, action
, filter
, fullpath
);
441 TALLOC_FREE(to_free
);
444 static void notify_fsp(files_struct
*fsp
, uint32 action
, const char *name
)
446 struct notify_change_event
*change
, *changes
;
449 if (fsp
->notify
== NULL
) {
451 * Nobody is waiting, don't queue
457 * Someone has triggered a notify previously, queue the change for
461 if ((fsp
->notify
->num_changes
> 1000) || (name
== NULL
)) {
463 * The real number depends on the client buf, just provide a
464 * guard against a DoS here. If name == NULL the CN backend is
465 * alerting us to a problem. Possibly dropped events. Clear
466 * queued changes and send the catch-all response to the client
467 * if a request is pending.
469 TALLOC_FREE(fsp
->notify
->changes
);
470 fsp
->notify
->num_changes
= -1;
471 if (fsp
->notify
->requests
!= NULL
) {
472 change_notify_reply(fsp
->notify
->requests
->req
,
474 fsp
->notify
->requests
->max_param
,
476 fsp
->notify
->requests
->reply_fn
);
477 change_notify_remove_request(fsp
->conn
->sconn
,
478 fsp
->notify
->requests
);
483 /* If we've exceeded the server side queue or received a NULL name
484 * from the underlying CN implementation, don't queue up any more
485 * requests until we can send a catch-all response to the client */
486 if (fsp
->notify
->num_changes
== -1) {
490 if (!(changes
= talloc_realloc(
491 fsp
->notify
, fsp
->notify
->changes
,
492 struct notify_change_event
,
493 fsp
->notify
->num_changes
+1))) {
494 DEBUG(0, ("talloc_realloc failed\n"));
498 fsp
->notify
->changes
= changes
;
500 change
= &(fsp
->notify
->changes
[fsp
->notify
->num_changes
]);
502 if (!(tmp
= talloc_strdup(changes
, name
))) {
503 DEBUG(0, ("talloc_strdup failed\n"));
507 string_replace(tmp
, '/', '\\');
510 change
->action
= action
;
511 fsp
->notify
->num_changes
+= 1;
513 if (fsp
->notify
->requests
== NULL
) {
515 * Nobody is waiting, so don't send anything. The ot
520 if (action
== NOTIFY_ACTION_OLD_NAME
) {
522 * We have to send the two rename events in one reply. So hold
523 * the first part back.
529 * Someone is waiting for the change, trigger the reply immediately.
531 * TODO: do we have to walk the lists of requests pending?
534 change_notify_reply(fsp
->notify
->requests
->req
,
536 fsp
->notify
->requests
->max_param
,
538 fsp
->notify
->requests
->reply_fn
);
540 change_notify_remove_request(fsp
->conn
->sconn
, fsp
->notify
->requests
);
543 char *notify_filter_string(TALLOC_CTX
*mem_ctx
, uint32 filter
)
547 result
= talloc_strdup(mem_ctx
, "");
549 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
550 result
= talloc_asprintf_append(result
, "FILE_NAME|");
551 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
552 result
= talloc_asprintf_append(result
, "DIR_NAME|");
553 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
554 result
= talloc_asprintf_append(result
, "ATTRIBUTES|");
555 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
556 result
= talloc_asprintf_append(result
, "SIZE|");
557 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
558 result
= talloc_asprintf_append(result
, "LAST_WRITE|");
559 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
560 result
= talloc_asprintf_append(result
, "LAST_ACCESS|");
561 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
562 result
= talloc_asprintf_append(result
, "CREATION|");
563 if (filter
& FILE_NOTIFY_CHANGE_EA
)
564 result
= talloc_asprintf_append(result
, "EA|");
565 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
566 result
= talloc_asprintf_append(result
, "SECURITY|");
567 if (filter
& FILE_NOTIFY_CHANGE_STREAM_NAME
)
568 result
= talloc_asprintf_append(result
, "STREAM_NAME|");
569 if (filter
& FILE_NOTIFY_CHANGE_STREAM_SIZE
)
570 result
= talloc_asprintf_append(result
, "STREAM_SIZE|");
571 if (filter
& FILE_NOTIFY_CHANGE_STREAM_WRITE
)
572 result
= talloc_asprintf_append(result
, "STREAM_WRITE|");
574 if (result
== NULL
) return NULL
;
575 if (*result
== '\0') return result
;
577 result
[strlen(result
)-1] = '\0';
581 struct sys_notify_context
*sys_notify_context_create(TALLOC_CTX
*mem_ctx
,
582 struct tevent_context
*ev
)
584 struct sys_notify_context
*ctx
;
586 if (!(ctx
= talloc(mem_ctx
, struct sys_notify_context
))) {
587 DEBUG(0, ("talloc failed\n"));
592 ctx
->private_data
= NULL
;