lib: Remove unused parmlist code
[Samba.git] / source3 / smbd / notify.c
blobe776749a318778ec5528135c6a927270fde645ac
1 /*
2 Unix SMB/CIFS implementation.
3 change notify handling
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/>.
22 #include "includes.h"
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 {
29 struct timespec when;
30 uint32_t action;
31 const char *name;
34 struct notify_change_buf {
36 * If no requests are pending, changes are queued here. Simple array,
37 * we only append.
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.
44 int num_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;
58 uint32_t filter;
59 uint32_t max_param;
60 void (*reply_fn)(struct smb_request *req,
61 NTSTATUS error_code,
62 uint8_t *buf, size_t len);
63 struct notify_mid_map *mid_map;
64 void *backend_data;
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)
72 if (fsp == NULL) {
73 return false;
76 if (fsp->notify == NULL) {
77 return false;
80 if (fsp->notify->num_changes == 0) {
81 return false;
84 return true;
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;
95 uint64_t mid;
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) {
104 return True;
106 return False;
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,
118 uint32_t max_offset,
119 struct notify_change_event *changes,
120 DATA_BLOB *final_blob)
122 int i;
124 if (num_changes == -1) {
125 return false;
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;
140 DATA_BLOB blob;
142 /* Coalesce any identical records. */
143 while (i+1 < num_changes &&
144 notify_change_record_identical(&changes[i],
145 &changes[i+1])) {
146 i++;
149 c = &changes[i];
151 m.FileName1 = c->name;
152 m.FileNameLength = strlen_m(c->name)*2;
153 m.Action = c->action;
154 m.NextEntryOffset = (i == num_changes-1) ? 0 : ndr_size_FILE_NOTIFY_INFORMATION(&m, 0);
157 * Offset to next entry, only if there is one
160 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &m,
161 (ndr_push_flags_fn_t)ndr_push_FILE_NOTIFY_INFORMATION);
162 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
163 return false;
166 if (DEBUGLEVEL >= 10) {
167 NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION, &m);
170 if (!data_blob_append(talloc_tos(), final_blob,
171 blob.data, blob.length)) {
172 data_blob_free(&blob);
173 return false;
176 data_blob_free(&blob);
178 if (final_blob->length > max_offset) {
179 /* Too much data for client. */
180 DEBUG(10, ("Client only wanted %d bytes, trying to "
181 "marshall %d bytes\n", (int)max_offset,
182 (int)final_blob->length));
183 return False;
187 return True;
190 /****************************************************************************
191 Setup the common parts of the return packet and send it.
192 *****************************************************************************/
194 void change_notify_reply(struct smb_request *req,
195 NTSTATUS error_code,
196 uint32_t max_param,
197 struct notify_change_buf *notify_buf,
198 void (*reply_fn)(struct smb_request *req,
199 NTSTATUS error_code,
200 uint8_t *buf, size_t len))
202 DATA_BLOB blob = data_blob_null;
204 if (!NT_STATUS_IS_OK(error_code)) {
205 reply_fn(req, error_code, NULL, 0);
206 return;
209 if (max_param == 0 || notify_buf == NULL) {
210 reply_fn(req, NT_STATUS_OK, NULL, 0);
211 return;
214 if (!notify_marshall_changes(notify_buf->num_changes, max_param,
215 notify_buf->changes, &blob)) {
217 * We exceed what the client is willing to accept. Send
218 * nothing.
220 data_blob_free(&blob);
223 reply_fn(req, NT_STATUS_OK, blob.data, blob.length);
225 data_blob_free(&blob);
227 TALLOC_FREE(notify_buf->changes);
228 notify_buf->num_changes = 0;
231 static void notify_callback(void *private_data, struct timespec when,
232 const struct notify_event *e)
234 files_struct *fsp = (files_struct *)private_data;
235 DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp)));
236 notify_fsp(fsp, when, e->action, e->path);
239 NTSTATUS change_notify_create(struct files_struct *fsp, uint32_t filter,
240 bool recursive)
242 char *fullpath;
243 size_t len;
244 uint32_t subdir_filter;
245 NTSTATUS status = NT_STATUS_NOT_IMPLEMENTED;
247 if (fsp->notify != NULL) {
248 DEBUG(1, ("change_notify_create: fsp->notify != NULL, "
249 "fname = %s\n", fsp->fsp_name->base_name));
250 return NT_STATUS_INVALID_PARAMETER;
253 if (!(fsp->notify = talloc_zero(NULL, struct notify_change_buf))) {
254 DEBUG(0, ("talloc failed\n"));
255 return NT_STATUS_NO_MEMORY;
258 /* Do notify operations on the base_name. */
259 fullpath = talloc_asprintf(
260 talloc_tos(), "%s/%s", fsp->conn->connectpath,
261 fsp->fsp_name->base_name);
262 if (fullpath == NULL) {
263 DEBUG(0, ("talloc_asprintf failed\n"));
264 TALLOC_FREE(fsp->notify);
265 return NT_STATUS_NO_MEMORY;
269 * Avoid /. at the end of the path name. notify can't deal with it.
271 len = strlen(fullpath);
272 if (len > 1 && fullpath[len-1] == '.' && fullpath[len-2] == '/') {
273 fullpath[len-2] = '\0';
276 subdir_filter = recursive ? filter : 0;
278 if ((filter != 0) || (subdir_filter != 0)) {
279 status = notify_add(fsp->conn->sconn->notify_ctx,
280 fullpath, filter, subdir_filter,
281 notify_callback, fsp);
283 TALLOC_FREE(fullpath);
284 return status;
287 NTSTATUS change_notify_add_request(struct smb_request *req,
288 uint32_t max_param,
289 uint32_t filter, bool recursive,
290 struct files_struct *fsp,
291 void (*reply_fn)(struct smb_request *req,
292 NTSTATUS error_code,
293 uint8_t *buf, size_t len))
295 struct notify_change_request *request = NULL;
296 struct notify_mid_map *map = NULL;
297 struct smbd_server_connection *sconn = req->sconn;
299 DEBUG(10, ("change_notify_add_request: Adding request for %s: "
300 "max_param = %d\n", fsp_str_dbg(fsp), (int)max_param));
302 if (!(request = talloc(NULL, struct notify_change_request))
303 || !(map = talloc(request, struct notify_mid_map))) {
304 TALLOC_FREE(request);
305 return NT_STATUS_NO_MEMORY;
308 request->mid_map = map;
309 map->req = request;
311 request->req = talloc_move(request, &req);
312 request->max_param = max_param;
313 request->filter = filter;
314 request->fsp = fsp;
315 request->reply_fn = reply_fn;
316 request->backend_data = NULL;
318 DLIST_ADD_END(fsp->notify->requests, request,
319 struct notify_change_request *);
321 map->mid = request->req->mid;
322 DLIST_ADD(sconn->smb1.notify_mid_maps, map);
324 return NT_STATUS_OK;
327 static void change_notify_remove_request(struct smbd_server_connection *sconn,
328 struct notify_change_request *remove_req)
330 files_struct *fsp;
331 struct notify_change_request *req;
334 * Paranoia checks, the fsp referenced must must have the request in
335 * its list of pending requests
338 fsp = remove_req->fsp;
339 SMB_ASSERT(fsp->notify != NULL);
341 for (req = fsp->notify->requests; req; req = req->next) {
342 if (req == remove_req) {
343 break;
347 if (req == NULL) {
348 smb_panic("notify_req not found in fsp's requests");
351 DLIST_REMOVE(fsp->notify->requests, req);
352 DLIST_REMOVE(sconn->smb1.notify_mid_maps, req->mid_map);
353 TALLOC_FREE(req);
356 static void smbd_notify_cancel_by_map(struct notify_mid_map *map)
358 struct smb_request *smbreq = map->req->req;
359 struct smbd_server_connection *sconn = smbreq->sconn;
360 struct smbd_smb2_request *smb2req = smbreq->smb2req;
361 NTSTATUS notify_status = NT_STATUS_CANCELLED;
363 if (smb2req != NULL) {
364 if (smb2req->session == NULL) {
365 notify_status = STATUS_NOTIFY_CLEANUP;
366 } else if (!NT_STATUS_IS_OK(smb2req->session->status)) {
367 notify_status = STATUS_NOTIFY_CLEANUP;
369 if (smb2req->tcon == NULL) {
370 notify_status = STATUS_NOTIFY_CLEANUP;
371 } else if (!NT_STATUS_IS_OK(smb2req->tcon->status)) {
372 notify_status = STATUS_NOTIFY_CLEANUP;
376 change_notify_reply(smbreq, notify_status,
377 0, NULL, map->req->reply_fn);
378 change_notify_remove_request(sconn, map->req);
381 /****************************************************************************
382 Delete entries by mid from the change notify pending queue. Always send reply.
383 *****************************************************************************/
385 void remove_pending_change_notify_requests_by_mid(
386 struct smbd_server_connection *sconn, uint64_t mid)
388 struct notify_mid_map *map;
390 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
391 if (map->mid == mid) {
392 break;
396 if (map == NULL) {
397 return;
400 smbd_notify_cancel_by_map(map);
403 void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
405 struct smbd_server_connection *sconn = smbreq->sconn;
406 struct notify_mid_map *map;
408 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
409 if (map->req->req == smbreq) {
410 break;
414 if (map == NULL) {
415 return;
418 smbd_notify_cancel_by_map(map);
421 static struct files_struct *smbd_notify_cancel_deleted_fn(
422 struct files_struct *fsp, void *private_data)
424 struct file_id *fid = talloc_get_type_abort(
425 private_data, struct file_id);
427 if (file_id_equal(&fsp->file_id, fid)) {
428 remove_pending_change_notify_requests_by_fid(
429 fsp, NT_STATUS_DELETE_PENDING);
431 return NULL;
434 void smbd_notify_cancel_deleted(struct messaging_context *msg,
435 void *private_data, uint32_t msg_type,
436 struct server_id server_id, DATA_BLOB *data)
438 struct smbd_server_connection *sconn = talloc_get_type_abort(
439 private_data, struct smbd_server_connection);
440 struct file_id *fid;
441 enum ndr_err_code ndr_err;
443 fid = talloc(talloc_tos(), struct file_id);
444 if (fid == NULL) {
445 DEBUG(1, ("talloc failed\n"));
446 return;
449 ndr_err = ndr_pull_struct_blob_all(
450 data, fid, fid, (ndr_pull_flags_fn_t)ndr_pull_file_id);
451 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
452 DEBUG(10, ("%s: ndr_pull_file_id failed: %s\n", __func__,
453 ndr_errstr(ndr_err)));
454 goto done;
457 files_forall(sconn, smbd_notify_cancel_deleted_fn, fid);
459 done:
460 TALLOC_FREE(fid);
463 /****************************************************************************
464 Delete entries by fnum from the change notify pending queue.
465 *****************************************************************************/
467 void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
468 NTSTATUS status)
470 if (fsp->notify == NULL) {
471 return;
474 while (fsp->notify->requests != NULL) {
475 change_notify_reply(fsp->notify->requests->req,
476 status, 0, NULL,
477 fsp->notify->requests->reply_fn);
478 change_notify_remove_request(fsp->conn->sconn,
479 fsp->notify->requests);
483 void notify_fname(connection_struct *conn, uint32_t action, uint32_t filter,
484 const char *path)
486 struct notify_context *notify_ctx = conn->sconn->notify_ctx;
488 if (path[0] == '.' && path[1] == '/') {
489 path += 2;
492 notify_trigger(notify_ctx, action, filter, conn->connectpath, path);
495 static void notify_fsp(files_struct *fsp, struct timespec when,
496 uint32_t action, const char *name)
498 struct notify_change_event *change, *changes;
499 char *tmp;
501 if (fsp->notify == NULL) {
503 * Nobody is waiting, don't queue
505 return;
509 * Someone has triggered a notify previously, queue the change for
510 * later.
513 if ((fsp->notify->num_changes > 1000) || (name == NULL)) {
515 * The real number depends on the client buf, just provide a
516 * guard against a DoS here. If name == NULL the CN backend is
517 * alerting us to a problem. Possibly dropped events. Clear
518 * queued changes and send the catch-all response to the client
519 * if a request is pending.
521 TALLOC_FREE(fsp->notify->changes);
522 fsp->notify->num_changes = -1;
523 if (fsp->notify->requests != NULL) {
524 change_notify_reply(fsp->notify->requests->req,
525 NT_STATUS_OK,
526 fsp->notify->requests->max_param,
527 fsp->notify,
528 fsp->notify->requests->reply_fn);
529 change_notify_remove_request(fsp->conn->sconn,
530 fsp->notify->requests);
532 return;
535 /* If we've exceeded the server side queue or received a NULL name
536 * from the underlying CN implementation, don't queue up any more
537 * requests until we can send a catch-all response to the client */
538 if (fsp->notify->num_changes == -1) {
539 return;
542 if (!(changes = talloc_realloc(
543 fsp->notify, fsp->notify->changes,
544 struct notify_change_event,
545 fsp->notify->num_changes+1))) {
546 DEBUG(0, ("talloc_realloc failed\n"));
547 return;
550 fsp->notify->changes = changes;
552 change = &(fsp->notify->changes[fsp->notify->num_changes]);
554 if (!(tmp = talloc_strdup(changes, name))) {
555 DEBUG(0, ("talloc_strdup failed\n"));
556 return;
559 string_replace(tmp, '/', '\\');
560 change->name = tmp;
562 change->when = when;
563 change->action = action;
564 fsp->notify->num_changes += 1;
566 if (fsp->notify->requests == NULL) {
568 * Nobody is waiting, so don't send anything. The ot
570 return;
573 if (action == NOTIFY_ACTION_OLD_NAME) {
575 * We have to send the two rename events in one reply. So hold
576 * the first part back.
578 return;
582 * Someone is waiting for the change, trigger the reply immediately.
584 * TODO: do we have to walk the lists of requests pending?
587 change_notify_reply(fsp->notify->requests->req,
588 NT_STATUS_OK,
589 fsp->notify->requests->max_param,
590 fsp->notify,
591 fsp->notify->requests->reply_fn);
593 change_notify_remove_request(fsp->conn->sconn, fsp->notify->requests);
596 char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32_t filter)
598 char *result = NULL;
600 result = talloc_strdup(mem_ctx, "");
602 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
603 result = talloc_asprintf_append(result, "FILE_NAME|");
604 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
605 result = talloc_asprintf_append(result, "DIR_NAME|");
606 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
607 result = talloc_asprintf_append(result, "ATTRIBUTES|");
608 if (filter & FILE_NOTIFY_CHANGE_SIZE)
609 result = talloc_asprintf_append(result, "SIZE|");
610 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
611 result = talloc_asprintf_append(result, "LAST_WRITE|");
612 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
613 result = talloc_asprintf_append(result, "LAST_ACCESS|");
614 if (filter & FILE_NOTIFY_CHANGE_CREATION)
615 result = talloc_asprintf_append(result, "CREATION|");
616 if (filter & FILE_NOTIFY_CHANGE_EA)
617 result = talloc_asprintf_append(result, "EA|");
618 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
619 result = talloc_asprintf_append(result, "SECURITY|");
620 if (filter & FILE_NOTIFY_CHANGE_STREAM_NAME)
621 result = talloc_asprintf_append(result, "STREAM_NAME|");
622 if (filter & FILE_NOTIFY_CHANGE_STREAM_SIZE)
623 result = talloc_asprintf_append(result, "STREAM_SIZE|");
624 if (filter & FILE_NOTIFY_CHANGE_STREAM_WRITE)
625 result = talloc_asprintf_append(result, "STREAM_WRITE|");
627 if (result == NULL) return NULL;
628 if (*result == '\0') return result;
630 result[strlen(result)-1] = '\0';
631 return result;
634 struct sys_notify_context *sys_notify_context_create(TALLOC_CTX *mem_ctx,
635 struct tevent_context *ev)
637 struct sys_notify_context *ctx;
639 if (!(ctx = talloc(mem_ctx, struct sys_notify_context))) {
640 DEBUG(0, ("talloc failed\n"));
641 return NULL;
644 ctx->ev = ev;
645 ctx->private_data = NULL;
646 return ctx;