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_request
{
28 struct notify_change_request
*prev
, *next
;
29 struct files_struct
*fsp
; /* backpointer for cancel by mid */
30 struct smb_request
*req
;
33 void (*reply_fn
)(struct smb_request
*req
,
35 uint8_t *buf
, size_t len
);
36 struct notify_mid_map
*mid_map
;
40 static void notify_fsp(files_struct
*fsp
, uint32 action
, const char *name
);
43 * For NTCancel, we need to find the notify_change_request indexed by
44 * mid. Separate list here.
47 struct notify_mid_map
{
48 struct notify_mid_map
*prev
, *next
;
49 struct notify_change_request
*req
;
53 static bool notify_change_record_identical(struct notify_change
*c1
,
54 struct notify_change
*c2
)
56 /* Note this is deliberately case sensitive. */
57 if (c1
->action
== c2
->action
&&
58 strcmp(c1
->name
, c2
->name
) == 0) {
64 static bool notify_marshall_changes(int num_changes
,
66 struct notify_change
*changes
,
67 DATA_BLOB
*final_blob
)
71 if (num_changes
== -1) {
75 for (i
=0; i
<num_changes
; i
++) {
76 enum ndr_err_code ndr_err
;
77 struct notify_change
*c
;
78 struct FILE_NOTIFY_INFORMATION m
;
81 /* Coalesce any identical records. */
82 while (i
+1 < num_changes
&&
83 notify_change_record_identical(&changes
[i
],
90 m
.FileName1
= c
->name
;
91 m
.FileNameLength
= strlen_m(c
->name
)*2;
93 m
.NextEntryOffset
= (i
== num_changes
-1) ? 0 : ndr_size_FILE_NOTIFY_INFORMATION(&m
, 0);
96 * Offset to next entry, only if there is one
99 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &m
,
100 (ndr_push_flags_fn_t
)ndr_push_FILE_NOTIFY_INFORMATION
);
101 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
105 if (DEBUGLEVEL
>= 10) {
106 NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION
, &m
);
109 if (!data_blob_append(talloc_tos(), final_blob
,
110 blob
.data
, blob
.length
)) {
111 data_blob_free(&blob
);
115 data_blob_free(&blob
);
117 if (final_blob
->length
> max_offset
) {
118 /* Too much data for client. */
119 DEBUG(10, ("Client only wanted %d bytes, trying to "
120 "marshall %d bytes\n", (int)max_offset
,
121 (int)final_blob
->length
));
129 /****************************************************************************
130 Setup the common parts of the return packet and send it.
131 *****************************************************************************/
133 void change_notify_reply(struct smb_request
*req
,
136 struct notify_change_buf
*notify_buf
,
137 void (*reply_fn
)(struct smb_request
*req
,
139 uint8_t *buf
, size_t len
))
141 DATA_BLOB blob
= data_blob_null
;
143 if (!NT_STATUS_IS_OK(error_code
)) {
144 reply_fn(req
, error_code
, NULL
, 0);
148 if (max_param
== 0 || notify_buf
== NULL
) {
149 reply_fn(req
, NT_STATUS_OK
, NULL
, 0);
153 if (!notify_marshall_changes(notify_buf
->num_changes
, max_param
,
154 notify_buf
->changes
, &blob
)) {
156 * We exceed what the client is willing to accept. Send
159 data_blob_free(&blob
);
162 reply_fn(req
, NT_STATUS_OK
, blob
.data
, blob
.length
);
164 data_blob_free(&blob
);
166 TALLOC_FREE(notify_buf
->changes
);
167 notify_buf
->num_changes
= 0;
170 static void notify_callback(void *private_data
, const struct notify_event
*e
)
172 files_struct
*fsp
= (files_struct
*)private_data
;
173 DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp
)));
174 notify_fsp(fsp
, e
->action
, e
->path
);
177 static void sys_notify_callback(struct sys_notify_context
*ctx
,
179 struct notify_event
*e
)
181 files_struct
*fsp
= (files_struct
*)private_data
;
182 DEBUG(10, ("sys_notify_callback called for %s\n", fsp_str_dbg(fsp
)));
183 notify_fsp(fsp
, e
->action
, e
->path
);
186 NTSTATUS
change_notify_create(struct files_struct
*fsp
, uint32 filter
,
191 struct notify_entry e
;
192 NTSTATUS status
= NT_STATUS_NOT_IMPLEMENTED
;
194 if (fsp
->notify
!= NULL
) {
195 DEBUG(1, ("change_notify_create: fsp->notify != NULL, "
196 "fname = %s\n", fsp
->fsp_name
->base_name
));
197 return NT_STATUS_INVALID_PARAMETER
;
200 if (!(fsp
->notify
= talloc_zero(NULL
, struct notify_change_buf
))) {
201 DEBUG(0, ("talloc failed\n"));
202 return NT_STATUS_NO_MEMORY
;
205 /* Do notify operations on the base_name. */
206 fullpath
= talloc_asprintf(
207 talloc_tos(), "%s/%s", fsp
->conn
->connectpath
,
208 fsp
->fsp_name
->base_name
);
209 if (fullpath
== NULL
) {
210 DEBUG(0, ("talloc_asprintf failed\n"));
211 TALLOC_FREE(fsp
->notify
);
212 return NT_STATUS_NO_MEMORY
;
216 * Avoid /. at the end of the path name. notify can't deal with it.
218 len
= strlen(fullpath
);
219 if (len
> 1 && fullpath
[len
-1] == '.' && fullpath
[len
-2] == '/') {
220 fullpath
[len
-2] = '\0';
225 e
.dir_fd
= fsp
->fh
->fd
;
226 e
.dir_id
= fsp
->file_id
;
230 e
.subdir_filter
= filter
;
233 if (fsp
->conn
->sconn
->sys_notify_ctx
!= NULL
) {
234 void *sys_notify_handle
= NULL
;
236 status
= SMB_VFS_NOTIFY_WATCH(
237 fsp
->conn
, fsp
->conn
->sconn
->sys_notify_ctx
,
238 e
.path
, &e
.filter
, &e
.subdir_filter
,
239 sys_notify_callback
, fsp
, &sys_notify_handle
);
241 if (NT_STATUS_IS_OK(status
)) {
242 talloc_steal(fsp
->notify
, sys_notify_handle
);
246 if ((e
.filter
!= 0) || (e
.subdir_filter
!= 0)) {
247 status
= notify_add(fsp
->conn
->sconn
->notify_ctx
, fsp
->conn
,
248 &e
, notify_callback
, fsp
);
250 TALLOC_FREE(fullpath
);
254 NTSTATUS
change_notify_add_request(struct smb_request
*req
,
256 uint32 filter
, bool recursive
,
257 struct files_struct
*fsp
,
258 void (*reply_fn
)(struct smb_request
*req
,
260 uint8_t *buf
, size_t len
))
262 struct notify_change_request
*request
= NULL
;
263 struct notify_mid_map
*map
= NULL
;
264 struct smbd_server_connection
*sconn
= req
->sconn
;
266 DEBUG(10, ("change_notify_add_request: Adding request for %s: "
267 "max_param = %d\n", fsp_str_dbg(fsp
), (int)max_param
));
269 if (!(request
= talloc(NULL
, struct notify_change_request
))
270 || !(map
= talloc(request
, struct notify_mid_map
))) {
271 TALLOC_FREE(request
);
272 return NT_STATUS_NO_MEMORY
;
275 request
->mid_map
= map
;
278 request
->req
= talloc_move(request
, &req
);
279 request
->max_param
= max_param
;
280 request
->filter
= filter
;
282 request
->reply_fn
= reply_fn
;
283 request
->backend_data
= NULL
;
285 DLIST_ADD_END(fsp
->notify
->requests
, request
,
286 struct notify_change_request
*);
288 map
->mid
= request
->req
->mid
;
289 DLIST_ADD(sconn
->smb1
.notify_mid_maps
, map
);
294 static void change_notify_remove_request(struct smbd_server_connection
*sconn
,
295 struct notify_change_request
*remove_req
)
298 struct notify_change_request
*req
;
301 * Paranoia checks, the fsp referenced must must have the request in
302 * its list of pending requests
305 fsp
= remove_req
->fsp
;
306 SMB_ASSERT(fsp
->notify
!= NULL
);
308 for (req
= fsp
->notify
->requests
; req
; req
= req
->next
) {
309 if (req
== remove_req
) {
315 smb_panic("notify_req not found in fsp's requests");
318 DLIST_REMOVE(fsp
->notify
->requests
, req
);
319 DLIST_REMOVE(sconn
->smb1
.notify_mid_maps
, req
->mid_map
);
323 /****************************************************************************
324 Delete entries by mid from the change notify pending queue. Always send reply.
325 *****************************************************************************/
327 void remove_pending_change_notify_requests_by_mid(
328 struct smbd_server_connection
*sconn
, uint64_t mid
)
330 struct notify_mid_map
*map
;
332 for (map
= sconn
->smb1
.notify_mid_maps
; map
; map
= map
->next
) {
333 if (map
->mid
== mid
) {
342 change_notify_reply(map
->req
->req
,
343 NT_STATUS_CANCELLED
, 0, NULL
, map
->req
->reply_fn
);
344 change_notify_remove_request(sconn
, map
->req
);
347 void smbd_notify_cancel_by_smbreq(const struct smb_request
*smbreq
)
349 struct smbd_server_connection
*sconn
= smbreq
->sconn
;
350 struct notify_mid_map
*map
;
352 for (map
= sconn
->smb1
.notify_mid_maps
; map
; map
= map
->next
) {
353 if (map
->req
->req
== smbreq
) {
362 change_notify_reply(map
->req
->req
,
363 NT_STATUS_CANCELLED
, 0, NULL
, map
->req
->reply_fn
);
364 change_notify_remove_request(sconn
, map
->req
);
367 /****************************************************************************
368 Delete entries by fnum from the change notify pending queue.
369 *****************************************************************************/
371 void remove_pending_change_notify_requests_by_fid(files_struct
*fsp
,
374 if (fsp
->notify
== NULL
) {
378 while (fsp
->notify
->requests
!= NULL
) {
379 change_notify_reply(fsp
->notify
->requests
->req
,
381 fsp
->notify
->requests
->reply_fn
);
382 change_notify_remove_request(fsp
->conn
->sconn
,
383 fsp
->notify
->requests
);
387 void notify_fname(connection_struct
*conn
, uint32 action
, uint32 filter
,
390 struct notify_context
*notify_ctx
= conn
->sconn
->notify_ctx
;
395 if (path
[0] == '.' && path
[1] == '/') {
398 if (parent_dirname(talloc_tos(), path
, &parent
, &name
)) {
399 struct smb_filename smb_fname_parent
;
401 ZERO_STRUCT(smb_fname_parent
);
402 smb_fname_parent
.base_name
= parent
;
404 if (SMB_VFS_STAT(conn
, &smb_fname_parent
) != -1) {
405 notify_onelevel(notify_ctx
, action
, filter
,
406 SMB_VFS_FILE_ID_CREATE(conn
, &smb_fname_parent
.st
),
411 fullpath
= talloc_asprintf(talloc_tos(), "%s/%s", conn
->connectpath
,
413 if (fullpath
== NULL
) {
414 DEBUG(0, ("asprintf failed\n"));
417 notify_trigger(notify_ctx
, action
, filter
, fullpath
);
418 TALLOC_FREE(fullpath
);
421 static void notify_fsp(files_struct
*fsp
, uint32 action
, const char *name
)
423 struct notify_change
*change
, *changes
;
426 if (fsp
->notify
== NULL
) {
428 * Nobody is waiting, don't queue
434 * Someone has triggered a notify previously, queue the change for
438 if ((fsp
->notify
->num_changes
> 1000) || (name
== NULL
)) {
440 * The real number depends on the client buf, just provide a
441 * guard against a DoS here. If name == NULL the CN backend is
442 * alerting us to a problem. Possibly dropped events. Clear
443 * queued changes and send the catch-all response to the client
444 * if a request is pending.
446 TALLOC_FREE(fsp
->notify
->changes
);
447 fsp
->notify
->num_changes
= -1;
448 if (fsp
->notify
->requests
!= NULL
) {
449 change_notify_reply(fsp
->notify
->requests
->req
,
451 fsp
->notify
->requests
->max_param
,
453 fsp
->notify
->requests
->reply_fn
);
454 change_notify_remove_request(fsp
->conn
->sconn
,
455 fsp
->notify
->requests
);
460 /* If we've exceeded the server side queue or received a NULL name
461 * from the underlying CN implementation, don't queue up any more
462 * requests until we can send a catch-all response to the client */
463 if (fsp
->notify
->num_changes
== -1) {
467 if (!(changes
= talloc_realloc(
468 fsp
->notify
, fsp
->notify
->changes
,
469 struct notify_change
, fsp
->notify
->num_changes
+1))) {
470 DEBUG(0, ("talloc_realloc failed\n"));
474 fsp
->notify
->changes
= changes
;
476 change
= &(fsp
->notify
->changes
[fsp
->notify
->num_changes
]);
478 if (!(tmp
= talloc_strdup(changes
, name
))) {
479 DEBUG(0, ("talloc_strdup failed\n"));
483 string_replace(tmp
, '/', '\\');
486 change
->action
= action
;
487 fsp
->notify
->num_changes
+= 1;
489 if (fsp
->notify
->requests
== NULL
) {
491 * Nobody is waiting, so don't send anything. The ot
496 if (action
== NOTIFY_ACTION_OLD_NAME
) {
498 * We have to send the two rename events in one reply. So hold
499 * the first part back.
505 * Someone is waiting for the change, trigger the reply immediately.
507 * TODO: do we have to walk the lists of requests pending?
510 change_notify_reply(fsp
->notify
->requests
->req
,
512 fsp
->notify
->requests
->max_param
,
514 fsp
->notify
->requests
->reply_fn
);
516 change_notify_remove_request(fsp
->conn
->sconn
, fsp
->notify
->requests
);
519 char *notify_filter_string(TALLOC_CTX
*mem_ctx
, uint32 filter
)
523 result
= talloc_strdup(mem_ctx
, "");
525 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
526 result
= talloc_asprintf_append(result
, "FILE_NAME|");
527 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
528 result
= talloc_asprintf_append(result
, "DIR_NAME|");
529 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
530 result
= talloc_asprintf_append(result
, "ATTRIBUTES|");
531 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
532 result
= talloc_asprintf_append(result
, "SIZE|");
533 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
534 result
= talloc_asprintf_append(result
, "LAST_WRITE|");
535 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
536 result
= talloc_asprintf_append(result
, "LAST_ACCESS|");
537 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
538 result
= talloc_asprintf_append(result
, "CREATION|");
539 if (filter
& FILE_NOTIFY_CHANGE_EA
)
540 result
= talloc_asprintf_append(result
, "EA|");
541 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
542 result
= talloc_asprintf_append(result
, "SECURITY|");
543 if (filter
& FILE_NOTIFY_CHANGE_STREAM_NAME
)
544 result
= talloc_asprintf_append(result
, "STREAM_NAME|");
545 if (filter
& FILE_NOTIFY_CHANGE_STREAM_SIZE
)
546 result
= talloc_asprintf_append(result
, "STREAM_SIZE|");
547 if (filter
& FILE_NOTIFY_CHANGE_STREAM_WRITE
)
548 result
= talloc_asprintf_append(result
, "STREAM_WRITE|");
550 if (result
== NULL
) return NULL
;
551 if (*result
== '\0') return result
;
553 result
[strlen(result
)-1] = '\0';
557 struct sys_notify_context
*sys_notify_context_create(TALLOC_CTX
*mem_ctx
,
558 struct event_context
*ev
)
560 struct sys_notify_context
*ctx
;
562 if (!(ctx
= talloc(mem_ctx
, struct sys_notify_context
))) {
563 DEBUG(0, ("talloc failed\n"));
568 ctx
->private_data
= NULL
;