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"
26 #include "librpc/gen_ndr/ndr_file_id.h"
28 struct notify_change_event {
34 struct notify_change_buf {
36 * If no requests are pending, changes are queued here. Simple array,
41 * num_changes == -1 means that we have got a catch-all change, when
42 * asked we just return NT_STATUS_OK without specific changes.
45 struct notify_change_event *changes;
48 * If no changes are around requests are queued here. Using a linked
49 * list, because we have to append at the end and delete from the top.
51 struct notify_change_request *requests;
54 struct notify_change_request {
55 struct notify_change_request *prev, *next;
56 struct files_struct *fsp; /* backpointer for cancel by mid */
57 struct smb_request *req;
60 void (*reply_fn)(struct smb_request *req,
62 uint8_t *buf, size_t len);
63 struct notify_mid_map *mid_map;
67 static void notify_fsp(files_struct *fsp, struct timespec when,
68 uint32_t action, const char *name);
70 bool change_notify_fsp_has_changes(struct files_struct *fsp)
76 if (fsp->notify == NULL) {
80 if (fsp->notify->num_changes == 0) {
88 * For NTCancel, we need to find the notify_change_request indexed by
89 * mid. Separate list here.
92 struct notify_mid_map {
93 struct notify_mid_map *prev, *next;
94 struct notify_change_request *req;
98 static bool notify_change_record_identical(struct notify_change_event *c1,
99 struct notify_change_event *c2)
101 /* Note this is deliberately case sensitive. */
102 if (c1->action == c2->action &&
103 strcmp(c1->name, c2->name) == 0) {
109 static int compare_notify_change_events(const void *p1, const void *p2)
111 const struct notify_change_event *e1 = p1;
112 const struct notify_change_event *e2 = p2;
114 return timespec_compare(&e1->when, &e2->when);
117 static bool notify_marshall_changes(int num_changes,
119 struct notify_change_event *changes,
120 DATA_BLOB *final_blob)
124 if (num_changes == -1) {
129 * Sort the notifies by timestamp when the event happened to avoid
130 * coalescing and thus dropping events.
133 qsort(changes, num_changes,
134 sizeof(*changes), compare_notify_change_events);
136 for (i=0; i<num_changes; i++) {
137 enum ndr_err_code ndr_err;
138 struct notify_change_event *c;
139 struct FILE_NOTIFY_INFORMATION m;
143 /* Coalesce any identical records. */
144 while (i+1 < num_changes &&
145 notify_change_record_identical(&changes[i],
152 m.FileName1 = c->name;
153 m.FileNameLength = strlen_m(c->name)*2;
154 m.Action = c->action;
156 m._pad = data_blob_null;
159 * Offset to next entry, only if there is one
162 if (i == (num_changes-1)) {
163 m.NextEntryOffset = 0;
165 if ((m.FileNameLength % 4) == 2) {
166 m._pad = data_blob_const(&pad, 2);
169 ndr_size_FILE_NOTIFY_INFORMATION(&m, 0);
172 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &m,
173 (ndr_push_flags_fn_t)ndr_push_FILE_NOTIFY_INFORMATION);
174 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
178 if (DEBUGLEVEL >= 10) {
179 NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION, &m);
182 if (!data_blob_append(talloc_tos(), final_blob,
183 blob.data, blob.length)) {
184 data_blob_free(&blob);
188 data_blob_free(&blob);
190 if (final_blob->length > max_offset) {
191 /* Too much data for client. */
192 DEBUG(10, ("Client only wanted %d bytes, trying to "
193 "marshall %d bytes\n", (int)max_offset,
194 (int)final_blob->length));
202 /****************************************************************************
203 Setup the common parts of the return packet and send it.
204 *****************************************************************************/
206 void change_notify_reply(struct smb_request *req,
209 struct notify_change_buf *notify_buf,
210 void (*reply_fn)(struct smb_request *req,
212 uint8_t *buf, size_t len))
214 DATA_BLOB blob = data_blob_null;
216 if (!NT_STATUS_IS_OK(error_code)) {
217 reply_fn(req, error_code, NULL, 0);
221 if (max_param == 0 || notify_buf == NULL) {
222 reply_fn(req, NT_STATUS_OK, NULL, 0);
226 if (!notify_marshall_changes(notify_buf->num_changes, max_param,
227 notify_buf->changes, &blob)) {
229 * We exceed what the client is willing to accept. Send
232 data_blob_free(&blob);
235 reply_fn(req, NT_STATUS_OK, blob.data, blob.length);
237 data_blob_free(&blob);
239 TALLOC_FREE(notify_buf->changes);
240 notify_buf->num_changes = 0;
243 static void notify_callback(void *private_data, struct timespec when,
244 const struct notify_event *e)
246 files_struct *fsp = (files_struct *)private_data;
247 DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp)));
248 notify_fsp(fsp, when, e->action, e->path);
251 NTSTATUS change_notify_create(struct files_struct *fsp, uint32_t filter,
254 size_t len = fsp_fullbasepath(fsp, NULL, 0);
255 char fullpath[len+1];
256 uint32_t subdir_filter;
257 NTSTATUS status = NT_STATUS_NOT_IMPLEMENTED;
259 if (fsp->notify != NULL) {
260 DEBUG(1, ("change_notify_create: fsp->notify != NULL, "
261 "fname = %s\n", fsp->fsp_name->base_name));
262 return NT_STATUS_INVALID_PARAMETER;
265 if (!(fsp->notify = talloc_zero(NULL, struct notify_change_buf))) {
266 DEBUG(0, ("talloc failed\n"));
267 return NT_STATUS_NO_MEMORY;
270 fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
273 * Avoid /. at the end of the path name. notify can't deal with it.
275 if (len > 1 && fullpath[len-1] == '.' && fullpath[len-2] == '/') {
276 fullpath[len-2] = '\0';
279 subdir_filter = recursive ? filter : 0;
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);
290 NTSTATUS change_notify_add_request(struct smb_request *req,
292 uint32_t 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);
323 map->mid = request->req->mid;
324 DLIST_ADD(sconn->smb1.notify_mid_maps, map);
329 static void change_notify_remove_request(struct smbd_server_connection *sconn,
330 struct notify_change_request *remove_req)
333 struct notify_change_request *req;
336 * Paranoia checks, the fsp referenced must must have the request in
337 * its list of pending requests
340 fsp = remove_req->fsp;
341 SMB_ASSERT(fsp->notify != NULL);
343 for (req = fsp->notify->requests; req; req = req->next) {
344 if (req == remove_req) {
350 smb_panic("notify_req not found in fsp's requests");
353 DLIST_REMOVE(fsp->notify->requests, req);
354 DLIST_REMOVE(sconn->smb1.notify_mid_maps, req->mid_map);
358 static void smbd_notify_cancel_by_map(struct notify_mid_map *map)
360 struct smb_request *smbreq = map->req->req;
361 struct smbd_server_connection *sconn = smbreq->sconn;
362 struct smbd_smb2_request *smb2req = smbreq->smb2req;
363 NTSTATUS notify_status = NT_STATUS_CANCELLED;
365 if (smb2req != NULL) {
366 if (smb2req->session == NULL) {
367 notify_status = STATUS_NOTIFY_CLEANUP;
368 } else if (!NT_STATUS_IS_OK(smb2req->session->status)) {
369 notify_status = STATUS_NOTIFY_CLEANUP;
371 if (smb2req->tcon == NULL) {
372 notify_status = STATUS_NOTIFY_CLEANUP;
373 } else if (!NT_STATUS_IS_OK(smb2req->tcon->status)) {
374 notify_status = STATUS_NOTIFY_CLEANUP;
378 change_notify_reply(smbreq, notify_status,
379 0, NULL, map->req->reply_fn);
380 change_notify_remove_request(sconn, map->req);
383 /****************************************************************************
384 Delete entries by mid from the change notify pending queue. Always send reply.
385 *****************************************************************************/
387 void remove_pending_change_notify_requests_by_mid(
388 struct smbd_server_connection *sconn, uint64_t mid)
390 struct notify_mid_map *map;
392 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
393 if (map->mid == mid) {
402 smbd_notify_cancel_by_map(map);
405 void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
407 struct smbd_server_connection *sconn = smbreq->sconn;
408 struct notify_mid_map *map;
410 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
411 if (map->req->req == smbreq) {
420 smbd_notify_cancel_by_map(map);
423 static struct files_struct *smbd_notify_cancel_deleted_fn(
424 struct files_struct *fsp, void *private_data)
426 struct file_id *fid = talloc_get_type_abort(
427 private_data, struct file_id);
429 if (file_id_equal(&fsp->file_id, fid)) {
430 remove_pending_change_notify_requests_by_fid(
431 fsp, NT_STATUS_DELETE_PENDING);
436 void smbd_notify_cancel_deleted(struct messaging_context *msg,
437 void *private_data, uint32_t msg_type,
438 struct server_id server_id, DATA_BLOB *data)
440 struct smbd_server_connection *sconn = talloc_get_type_abort(
441 private_data, struct smbd_server_connection);
443 enum ndr_err_code ndr_err;
445 fid = talloc(talloc_tos(), struct file_id);
447 DEBUG(1, ("talloc failed\n"));
451 ndr_err = ndr_pull_struct_blob_all(
452 data, fid, fid, (ndr_pull_flags_fn_t)ndr_pull_file_id);
453 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
454 DEBUG(10, ("%s: ndr_pull_file_id failed: %s\n", __func__,
455 ndr_errstr(ndr_err)));
459 files_forall(sconn, smbd_notify_cancel_deleted_fn, fid);
465 /****************************************************************************
466 Delete entries by fnum from the change notify pending queue.
467 *****************************************************************************/
469 void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
472 if (fsp->notify == NULL) {
476 while (fsp->notify->requests != NULL) {
477 change_notify_reply(fsp->notify->requests->req,
479 fsp->notify->requests->reply_fn);
480 change_notify_remove_request(fsp->conn->sconn,
481 fsp->notify->requests);
485 void notify_fname(connection_struct *conn, uint32_t action, uint32_t filter,
488 struct notify_context *notify_ctx = conn->sconn->notify_ctx;
490 if (path[0] == '.' && path[1] == '/') {
494 notify_trigger(notify_ctx, action, filter, conn->connectpath, path);
497 static void notify_fsp(files_struct *fsp, struct timespec when,
498 uint32_t action, const char *name)
500 struct notify_change_event *change, *changes;
503 if (fsp->notify == NULL) {
505 * Nobody is waiting, don't queue
511 * Someone has triggered a notify previously, queue the change for
515 if ((fsp->notify->num_changes > 1000) || (name == NULL)) {
517 * The real number depends on the client buf, just provide a
518 * guard against a DoS here. If name == NULL the CN backend is
519 * alerting us to a problem. Possibly dropped events. Clear
520 * queued changes and send the catch-all response to the client
521 * if a request is pending.
523 TALLOC_FREE(fsp->notify->changes);
524 fsp->notify->num_changes = -1;
525 if (fsp->notify->requests != NULL) {
526 change_notify_reply(fsp->notify->requests->req,
528 fsp->notify->requests->max_param,
530 fsp->notify->requests->reply_fn);
531 change_notify_remove_request(fsp->conn->sconn,
532 fsp->notify->requests);
537 /* If we've exceeded the server side queue or received a NULL name
538 * from the underlying CN implementation, don't queue up any more
539 * requests until we can send a catch-all response to the client */
540 if (fsp->notify->num_changes == -1) {
544 if (!(changes = talloc_realloc(
545 fsp->notify, fsp->notify->changes,
546 struct notify_change_event,
547 fsp->notify->num_changes+1))) {
548 DEBUG(0, ("talloc_realloc failed\n"));
552 fsp->notify->changes = changes;
554 change = &(fsp->notify->changes[fsp->notify->num_changes]);
556 if (!(tmp = talloc_strdup(changes, name))) {
557 DEBUG(0, ("talloc_strdup failed\n"));
561 string_replace(tmp, '/', '\\');
565 change->action = action;
566 fsp->notify->num_changes += 1;
568 if (fsp->notify->requests == NULL) {
570 * Nobody is waiting, so don't send anything. The ot
575 if (action == NOTIFY_ACTION_OLD_NAME) {
577 * We have to send the two rename events in one reply. So hold
578 * the first part back.
584 * Someone is waiting for the change, trigger the reply immediately.
586 * TODO: do we have to walk the lists of requests pending?
589 change_notify_reply(fsp->notify->requests->req,
591 fsp->notify->requests->max_param,
593 fsp->notify->requests->reply_fn);
595 change_notify_remove_request(fsp->conn->sconn, fsp->notify->requests);
598 char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32_t filter)
602 result = talloc_strdup(mem_ctx, "");
604 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
605 result = talloc_asprintf_append(result, "FILE_NAME|");
606 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
607 result = talloc_asprintf_append(result, "DIR_NAME|");
608 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
609 result = talloc_asprintf_append(result, "ATTRIBUTES|");
610 if (filter & FILE_NOTIFY_CHANGE_SIZE)
611 result = talloc_asprintf_append(result, "SIZE|");
612 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
613 result = talloc_asprintf_append(result, "LAST_WRITE|");
614 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
615 result = talloc_asprintf_append(result, "LAST_ACCESS|");
616 if (filter & FILE_NOTIFY_CHANGE_CREATION)
617 result = talloc_asprintf_append(result, "CREATION|");
618 if (filter & FILE_NOTIFY_CHANGE_EA)
619 result = talloc_asprintf_append(result, "EA|");
620 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
621 result = talloc_asprintf_append(result, "SECURITY|");
622 if (filter & FILE_NOTIFY_CHANGE_STREAM_NAME)
623 result = talloc_asprintf_append(result, "STREAM_NAME|");
624 if (filter & FILE_NOTIFY_CHANGE_STREAM_SIZE)
625 result = talloc_asprintf_append(result, "STREAM_SIZE|");
626 if (filter & FILE_NOTIFY_CHANGE_STREAM_WRITE)
627 result = talloc_asprintf_append(result, "STREAM_WRITE|");
629 if (result == NULL) return NULL;
630 if (*result == '\0') return result;
632 result[strlen(result)-1] = '\0';
636 struct sys_notify_context *sys_notify_context_create(TALLOC_CTX *mem_ctx,
637 struct tevent_context *ev)
639 struct sys_notify_context *ctx;
641 if (!(ctx = talloc(mem_ctx, struct sys_notify_context))) {
642 DEBUG(0, ("talloc failed\n"));
647 ctx->private_data = NULL;