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
);
42 bool change_notify_fsp_has_changes(struct files_struct
*fsp
)
48 if (fsp
->notify
== NULL
) {
52 if (fsp
->notify
->num_changes
== 0) {
60 * For NTCancel, we need to find the notify_change_request indexed by
61 * mid. Separate list here.
64 struct notify_mid_map
{
65 struct notify_mid_map
*prev
, *next
;
66 struct notify_change_request
*req
;
70 static bool notify_change_record_identical(struct notify_change
*c1
,
71 struct notify_change
*c2
)
73 /* Note this is deliberately case sensitive. */
74 if (c1
->action
== c2
->action
&&
75 strcmp(c1
->name
, c2
->name
) == 0) {
81 static bool notify_marshall_changes(int num_changes
,
83 struct notify_change
*changes
,
84 DATA_BLOB
*final_blob
)
88 if (num_changes
== -1) {
92 for (i
=0; i
<num_changes
; i
++) {
93 enum ndr_err_code ndr_err
;
94 struct notify_change
*c
;
95 struct FILE_NOTIFY_INFORMATION m
;
98 /* Coalesce any identical records. */
99 while (i
+1 < num_changes
&&
100 notify_change_record_identical(&changes
[i
],
107 m
.FileName1
= c
->name
;
108 m
.FileNameLength
= strlen_m(c
->name
)*2;
109 m
.Action
= c
->action
;
110 m
.NextEntryOffset
= (i
== num_changes
-1) ? 0 : ndr_size_FILE_NOTIFY_INFORMATION(&m
, 0);
113 * Offset to next entry, only if there is one
116 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &m
,
117 (ndr_push_flags_fn_t
)ndr_push_FILE_NOTIFY_INFORMATION
);
118 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
122 if (DEBUGLEVEL
>= 10) {
123 NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION
, &m
);
126 if (!data_blob_append(talloc_tos(), final_blob
,
127 blob
.data
, blob
.length
)) {
128 data_blob_free(&blob
);
132 data_blob_free(&blob
);
134 if (final_blob
->length
> max_offset
) {
135 /* Too much data for client. */
136 DEBUG(10, ("Client only wanted %d bytes, trying to "
137 "marshall %d bytes\n", (int)max_offset
,
138 (int)final_blob
->length
));
146 /****************************************************************************
147 Setup the common parts of the return packet and send it.
148 *****************************************************************************/
150 void change_notify_reply(struct smb_request
*req
,
153 struct notify_change_buf
*notify_buf
,
154 void (*reply_fn
)(struct smb_request
*req
,
156 uint8_t *buf
, size_t len
))
158 DATA_BLOB blob
= data_blob_null
;
160 if (!NT_STATUS_IS_OK(error_code
)) {
161 reply_fn(req
, error_code
, NULL
, 0);
165 if (max_param
== 0 || notify_buf
== NULL
) {
166 reply_fn(req
, NT_STATUS_OK
, NULL
, 0);
170 if (!notify_marshall_changes(notify_buf
->num_changes
, max_param
,
171 notify_buf
->changes
, &blob
)) {
173 * We exceed what the client is willing to accept. Send
176 data_blob_free(&blob
);
179 reply_fn(req
, NT_STATUS_OK
, blob
.data
, blob
.length
);
181 data_blob_free(&blob
);
183 TALLOC_FREE(notify_buf
->changes
);
184 notify_buf
->num_changes
= 0;
187 static void notify_callback(void *private_data
, const struct notify_event
*e
)
189 files_struct
*fsp
= (files_struct
*)private_data
;
190 DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp
)));
191 notify_fsp(fsp
, e
->action
, e
->path
);
194 static void sys_notify_callback(struct sys_notify_context
*ctx
,
196 struct notify_event
*e
)
198 files_struct
*fsp
= (files_struct
*)private_data
;
199 DEBUG(10, ("sys_notify_callback called for %s\n", fsp_str_dbg(fsp
)));
200 notify_fsp(fsp
, e
->action
, e
->path
);
203 NTSTATUS
change_notify_create(struct files_struct
*fsp
, uint32 filter
,
208 uint32_t subdir_filter
;
209 NTSTATUS status
= NT_STATUS_NOT_IMPLEMENTED
;
211 if (fsp
->notify
!= NULL
) {
212 DEBUG(1, ("change_notify_create: fsp->notify != NULL, "
213 "fname = %s\n", fsp
->fsp_name
->base_name
));
214 return NT_STATUS_INVALID_PARAMETER
;
217 if (!(fsp
->notify
= talloc_zero(NULL
, struct notify_change_buf
))) {
218 DEBUG(0, ("talloc failed\n"));
219 return NT_STATUS_NO_MEMORY
;
222 /* Do notify operations on the base_name. */
223 fullpath
= talloc_asprintf(
224 talloc_tos(), "%s/%s", fsp
->conn
->connectpath
,
225 fsp
->fsp_name
->base_name
);
226 if (fullpath
== NULL
) {
227 DEBUG(0, ("talloc_asprintf failed\n"));
228 TALLOC_FREE(fsp
->notify
);
229 return NT_STATUS_NO_MEMORY
;
233 * Avoid /. at the end of the path name. notify can't deal with it.
235 len
= strlen(fullpath
);
236 if (len
> 1 && fullpath
[len
-1] == '.' && fullpath
[len
-2] == '/') {
237 fullpath
[len
-2] = '\0';
240 subdir_filter
= recursive
? filter
: 0;
242 if (fsp
->conn
->sconn
->sys_notify_ctx
!= NULL
) {
243 void *sys_notify_handle
= NULL
;
245 status
= SMB_VFS_NOTIFY_WATCH(
246 fsp
->conn
, fsp
->conn
->sconn
->sys_notify_ctx
,
247 fullpath
, &filter
, &subdir_filter
,
248 sys_notify_callback
, fsp
, &sys_notify_handle
);
250 if (NT_STATUS_IS_OK(status
)) {
251 talloc_steal(fsp
->notify
, sys_notify_handle
);
255 if ((filter
!= 0) || (subdir_filter
!= 0)) {
256 status
= notify_add(fsp
->conn
->sconn
->notify_ctx
,
257 fullpath
, filter
, subdir_filter
,
258 notify_callback
, fsp
);
260 TALLOC_FREE(fullpath
);
264 NTSTATUS
change_notify_add_request(struct smb_request
*req
,
266 uint32 filter
, bool recursive
,
267 struct files_struct
*fsp
,
268 void (*reply_fn
)(struct smb_request
*req
,
270 uint8_t *buf
, size_t len
))
272 struct notify_change_request
*request
= NULL
;
273 struct notify_mid_map
*map
= NULL
;
274 struct smbd_server_connection
*sconn
= req
->sconn
;
276 DEBUG(10, ("change_notify_add_request: Adding request for %s: "
277 "max_param = %d\n", fsp_str_dbg(fsp
), (int)max_param
));
279 if (!(request
= talloc(NULL
, struct notify_change_request
))
280 || !(map
= talloc(request
, struct notify_mid_map
))) {
281 TALLOC_FREE(request
);
282 return NT_STATUS_NO_MEMORY
;
285 request
->mid_map
= map
;
288 request
->req
= talloc_move(request
, &req
);
289 request
->max_param
= max_param
;
290 request
->filter
= filter
;
292 request
->reply_fn
= reply_fn
;
293 request
->backend_data
= NULL
;
295 DLIST_ADD_END(fsp
->notify
->requests
, request
,
296 struct notify_change_request
*);
298 map
->mid
= request
->req
->mid
;
299 DLIST_ADD(sconn
->smb1
.notify_mid_maps
, map
);
304 static void change_notify_remove_request(struct smbd_server_connection
*sconn
,
305 struct notify_change_request
*remove_req
)
308 struct notify_change_request
*req
;
311 * Paranoia checks, the fsp referenced must must have the request in
312 * its list of pending requests
315 fsp
= remove_req
->fsp
;
316 SMB_ASSERT(fsp
->notify
!= NULL
);
318 for (req
= fsp
->notify
->requests
; req
; req
= req
->next
) {
319 if (req
== remove_req
) {
325 smb_panic("notify_req not found in fsp's requests");
328 DLIST_REMOVE(fsp
->notify
->requests
, req
);
329 DLIST_REMOVE(sconn
->smb1
.notify_mid_maps
, req
->mid_map
);
333 /****************************************************************************
334 Delete entries by mid from the change notify pending queue. Always send reply.
335 *****************************************************************************/
337 void remove_pending_change_notify_requests_by_mid(
338 struct smbd_server_connection
*sconn
, uint64_t mid
)
340 struct notify_mid_map
*map
;
342 for (map
= sconn
->smb1
.notify_mid_maps
; map
; map
= map
->next
) {
343 if (map
->mid
== mid
) {
352 change_notify_reply(map
->req
->req
,
353 NT_STATUS_CANCELLED
, 0, NULL
, map
->req
->reply_fn
);
354 change_notify_remove_request(sconn
, map
->req
);
357 void smbd_notify_cancel_by_smbreq(const struct smb_request
*smbreq
)
359 struct smbd_server_connection
*sconn
= smbreq
->sconn
;
360 struct notify_mid_map
*map
;
362 for (map
= sconn
->smb1
.notify_mid_maps
; map
; map
= map
->next
) {
363 if (map
->req
->req
== smbreq
) {
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 /****************************************************************************
378 Delete entries by fnum from the change notify pending queue.
379 *****************************************************************************/
381 void remove_pending_change_notify_requests_by_fid(files_struct
*fsp
,
384 if (fsp
->notify
== NULL
) {
388 while (fsp
->notify
->requests
!= NULL
) {
389 change_notify_reply(fsp
->notify
->requests
->req
,
391 fsp
->notify
->requests
->reply_fn
);
392 change_notify_remove_request(fsp
->conn
->sconn
,
393 fsp
->notify
->requests
);
397 void notify_fname(connection_struct
*conn
, uint32 action
, uint32 filter
,
400 struct notify_context
*notify_ctx
= conn
->sconn
->notify_ctx
;
403 if (path
[0] == '.' && path
[1] == '/') {
406 fullpath
= talloc_asprintf(talloc_tos(), "%s/%s", conn
->connectpath
,
408 if (fullpath
== NULL
) {
409 DEBUG(0, ("asprintf failed\n"));
412 notify_trigger(notify_ctx
, action
, filter
, fullpath
);
413 TALLOC_FREE(fullpath
);
416 static void notify_fsp(files_struct
*fsp
, uint32 action
, const char *name
)
418 struct notify_change
*change
, *changes
;
421 if (fsp
->notify
== NULL
) {
423 * Nobody is waiting, don't queue
429 * Someone has triggered a notify previously, queue the change for
433 if ((fsp
->notify
->num_changes
> 1000) || (name
== NULL
)) {
435 * The real number depends on the client buf, just provide a
436 * guard against a DoS here. If name == NULL the CN backend is
437 * alerting us to a problem. Possibly dropped events. Clear
438 * queued changes and send the catch-all response to the client
439 * if a request is pending.
441 TALLOC_FREE(fsp
->notify
->changes
);
442 fsp
->notify
->num_changes
= -1;
443 if (fsp
->notify
->requests
!= NULL
) {
444 change_notify_reply(fsp
->notify
->requests
->req
,
446 fsp
->notify
->requests
->max_param
,
448 fsp
->notify
->requests
->reply_fn
);
449 change_notify_remove_request(fsp
->conn
->sconn
,
450 fsp
->notify
->requests
);
455 /* If we've exceeded the server side queue or received a NULL name
456 * from the underlying CN implementation, don't queue up any more
457 * requests until we can send a catch-all response to the client */
458 if (fsp
->notify
->num_changes
== -1) {
462 if (!(changes
= talloc_realloc(
463 fsp
->notify
, fsp
->notify
->changes
,
464 struct notify_change
, fsp
->notify
->num_changes
+1))) {
465 DEBUG(0, ("talloc_realloc failed\n"));
469 fsp
->notify
->changes
= changes
;
471 change
= &(fsp
->notify
->changes
[fsp
->notify
->num_changes
]);
473 if (!(tmp
= talloc_strdup(changes
, name
))) {
474 DEBUG(0, ("talloc_strdup failed\n"));
478 string_replace(tmp
, '/', '\\');
481 change
->action
= action
;
482 fsp
->notify
->num_changes
+= 1;
484 if (fsp
->notify
->requests
== NULL
) {
486 * Nobody is waiting, so don't send anything. The ot
491 if (action
== NOTIFY_ACTION_OLD_NAME
) {
493 * We have to send the two rename events in one reply. So hold
494 * the first part back.
500 * Someone is waiting for the change, trigger the reply immediately.
502 * TODO: do we have to walk the lists of requests pending?
505 change_notify_reply(fsp
->notify
->requests
->req
,
507 fsp
->notify
->requests
->max_param
,
509 fsp
->notify
->requests
->reply_fn
);
511 change_notify_remove_request(fsp
->conn
->sconn
, fsp
->notify
->requests
);
514 char *notify_filter_string(TALLOC_CTX
*mem_ctx
, uint32 filter
)
518 result
= talloc_strdup(mem_ctx
, "");
520 if (filter
& FILE_NOTIFY_CHANGE_FILE_NAME
)
521 result
= talloc_asprintf_append(result
, "FILE_NAME|");
522 if (filter
& FILE_NOTIFY_CHANGE_DIR_NAME
)
523 result
= talloc_asprintf_append(result
, "DIR_NAME|");
524 if (filter
& FILE_NOTIFY_CHANGE_ATTRIBUTES
)
525 result
= talloc_asprintf_append(result
, "ATTRIBUTES|");
526 if (filter
& FILE_NOTIFY_CHANGE_SIZE
)
527 result
= talloc_asprintf_append(result
, "SIZE|");
528 if (filter
& FILE_NOTIFY_CHANGE_LAST_WRITE
)
529 result
= talloc_asprintf_append(result
, "LAST_WRITE|");
530 if (filter
& FILE_NOTIFY_CHANGE_LAST_ACCESS
)
531 result
= talloc_asprintf_append(result
, "LAST_ACCESS|");
532 if (filter
& FILE_NOTIFY_CHANGE_CREATION
)
533 result
= talloc_asprintf_append(result
, "CREATION|");
534 if (filter
& FILE_NOTIFY_CHANGE_EA
)
535 result
= talloc_asprintf_append(result
, "EA|");
536 if (filter
& FILE_NOTIFY_CHANGE_SECURITY
)
537 result
= talloc_asprintf_append(result
, "SECURITY|");
538 if (filter
& FILE_NOTIFY_CHANGE_STREAM_NAME
)
539 result
= talloc_asprintf_append(result
, "STREAM_NAME|");
540 if (filter
& FILE_NOTIFY_CHANGE_STREAM_SIZE
)
541 result
= talloc_asprintf_append(result
, "STREAM_SIZE|");
542 if (filter
& FILE_NOTIFY_CHANGE_STREAM_WRITE
)
543 result
= talloc_asprintf_append(result
, "STREAM_WRITE|");
545 if (result
== NULL
) return NULL
;
546 if (*result
== '\0') return result
;
548 result
[strlen(result
)-1] = '\0';
552 struct sys_notify_context
*sys_notify_context_create(TALLOC_CTX
*mem_ctx
,
553 struct event_context
*ev
)
555 struct sys_notify_context
*ctx
;
557 if (!(ctx
= talloc(mem_ctx
, struct sys_notify_context
))) {
558 DEBUG(0, ("talloc failed\n"));
563 ctx
->private_data
= NULL
;