s3: Pass "path" through vfs_notify_watch
[Samba/gebeck_regimport.git] / source3 / smbd / notify.c
blob5a78a726b717b6b72db5536acbd1e986fd675378
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"
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;
31 uint32 filter;
32 uint32 max_param;
33 void (*reply_fn)(struct smb_request *req,
34 NTSTATUS error_code,
35 uint8_t *buf, size_t len);
36 struct notify_mid_map *mid_map;
37 void *backend_data;
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;
50 uint64_t mid;
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) {
59 return True;
61 return False;
64 static bool notify_marshall_changes(int num_changes,
65 uint32 max_offset,
66 struct notify_change *changes,
67 DATA_BLOB *final_blob)
69 int i;
71 if (num_changes == -1) {
72 return false;
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;
79 DATA_BLOB blob;
81 /* Coalesce any identical records. */
82 while (i+1 < num_changes &&
83 notify_change_record_identical(&changes[i],
84 &changes[i+1])) {
85 i++;
88 c = &changes[i];
90 m.FileName1 = c->name;
91 m.FileNameLength = strlen_m(c->name)*2;
92 m.Action = c->action;
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)) {
102 return false;
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);
112 return false;
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));
122 return False;
126 return True;
129 /****************************************************************************
130 Setup the common parts of the return packet and send it.
131 *****************************************************************************/
133 void change_notify_reply(struct smb_request *req,
134 NTSTATUS error_code,
135 uint32_t max_param,
136 struct notify_change_buf *notify_buf,
137 void (*reply_fn)(struct smb_request *req,
138 NTSTATUS error_code,
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);
145 return;
148 if (max_param == 0 || notify_buf == NULL) {
149 reply_fn(req, NT_STATUS_OK, NULL, 0);
150 return;
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
157 * nothing.
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 NTSTATUS change_notify_create(struct files_struct *fsp, uint32 filter,
178 bool recursive)
180 char *fullpath;
181 size_t len;
182 struct notify_entry e;
183 NTSTATUS status;
185 if (fsp->notify != NULL) {
186 DEBUG(1, ("change_notify_create: fsp->notify != NULL, "
187 "fname = %s\n", fsp->fsp_name->base_name));
188 return NT_STATUS_INVALID_PARAMETER;
191 if (!(fsp->notify = talloc_zero(NULL, struct notify_change_buf))) {
192 DEBUG(0, ("talloc failed\n"));
193 return NT_STATUS_NO_MEMORY;
196 /* Do notify operations on the base_name. */
197 fullpath = talloc_asprintf(
198 talloc_tos(), "%s/%s", fsp->conn->connectpath,
199 fsp->fsp_name->base_name);
200 if (fullpath == NULL) {
201 DEBUG(0, ("talloc_asprintf failed\n"));
202 TALLOC_FREE(fsp->notify);
203 return NT_STATUS_NO_MEMORY;
207 * Avoid /. at the end of the path name. notify can't deal with it.
209 len = strlen(fullpath);
210 if (len > 1 && fullpath[len-1] == '.' && fullpath[len-2] == '/') {
211 fullpath[len-2] = '\0';
214 ZERO_STRUCT(e);
215 e.path = fullpath;
216 e.dir_fd = fsp->fh->fd;
217 e.dir_id = fsp->file_id;
218 e.filter = filter;
219 e.subdir_filter = 0;
220 if (recursive) {
221 e.subdir_filter = filter;
224 status = notify_add(fsp->conn->notify_ctx, &e, notify_callback, fsp);
225 TALLOC_FREE(fullpath);
227 return status;
230 NTSTATUS change_notify_add_request(struct smb_request *req,
231 uint32 max_param,
232 uint32 filter, bool recursive,
233 struct files_struct *fsp,
234 void (*reply_fn)(struct smb_request *req,
235 NTSTATUS error_code,
236 uint8_t *buf, size_t len))
238 struct notify_change_request *request = NULL;
239 struct notify_mid_map *map = NULL;
240 struct smbd_server_connection *sconn = req->sconn;
242 DEBUG(10, ("change_notify_add_request: Adding request for %s: "
243 "max_param = %d\n", fsp_str_dbg(fsp), (int)max_param));
245 if (!(request = talloc(NULL, struct notify_change_request))
246 || !(map = talloc(request, struct notify_mid_map))) {
247 TALLOC_FREE(request);
248 return NT_STATUS_NO_MEMORY;
251 request->mid_map = map;
252 map->req = request;
254 request->req = talloc_move(request, &req);
255 request->max_param = max_param;
256 request->filter = filter;
257 request->fsp = fsp;
258 request->reply_fn = reply_fn;
259 request->backend_data = NULL;
261 DLIST_ADD_END(fsp->notify->requests, request,
262 struct notify_change_request *);
264 map->mid = request->req->mid;
265 DLIST_ADD(sconn->smb1.notify_mid_maps, map);
267 return NT_STATUS_OK;
270 static void change_notify_remove_request(struct smbd_server_connection *sconn,
271 struct notify_change_request *remove_req)
273 files_struct *fsp;
274 struct notify_change_request *req;
277 * Paranoia checks, the fsp referenced must must have the request in
278 * its list of pending requests
281 fsp = remove_req->fsp;
282 SMB_ASSERT(fsp->notify != NULL);
284 for (req = fsp->notify->requests; req; req = req->next) {
285 if (req == remove_req) {
286 break;
290 if (req == NULL) {
291 smb_panic("notify_req not found in fsp's requests");
294 DLIST_REMOVE(fsp->notify->requests, req);
295 DLIST_REMOVE(sconn->smb1.notify_mid_maps, req->mid_map);
296 TALLOC_FREE(req);
299 /****************************************************************************
300 Delete entries by mid from the change notify pending queue. Always send reply.
301 *****************************************************************************/
303 void remove_pending_change_notify_requests_by_mid(
304 struct smbd_server_connection *sconn, uint64_t mid)
306 struct notify_mid_map *map;
308 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
309 if (map->mid == mid) {
310 break;
314 if (map == NULL) {
315 return;
318 change_notify_reply(map->req->req,
319 NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
320 change_notify_remove_request(sconn, map->req);
323 void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
325 struct smbd_server_connection *sconn = smbreq->sconn;
326 struct notify_mid_map *map;
328 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
329 if (map->req->req == smbreq) {
330 break;
334 if (map == NULL) {
335 return;
338 change_notify_reply(map->req->req,
339 NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
340 change_notify_remove_request(sconn, map->req);
343 /****************************************************************************
344 Delete entries by fnum from the change notify pending queue.
345 *****************************************************************************/
347 void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
348 NTSTATUS status)
350 if (fsp->notify == NULL) {
351 return;
354 while (fsp->notify->requests != NULL) {
355 change_notify_reply(fsp->notify->requests->req,
356 status, 0, NULL,
357 fsp->notify->requests->reply_fn);
358 change_notify_remove_request(fsp->conn->sconn,
359 fsp->notify->requests);
363 void notify_fname(connection_struct *conn, uint32 action, uint32 filter,
364 const char *path)
366 char *fullpath;
367 char *parent;
368 const char *name;
370 if (path[0] == '.' && path[1] == '/') {
371 path += 2;
373 if (parent_dirname(talloc_tos(), path, &parent, &name)) {
374 struct smb_filename smb_fname_parent;
376 ZERO_STRUCT(smb_fname_parent);
377 smb_fname_parent.base_name = parent;
379 if (SMB_VFS_STAT(conn, &smb_fname_parent) != -1) {
380 notify_onelevel(conn->notify_ctx, action, filter,
381 SMB_VFS_FILE_ID_CREATE(conn, &smb_fname_parent.st),
382 name);
386 fullpath = talloc_asprintf(talloc_tos(), "%s/%s", conn->connectpath,
387 path);
388 if (fullpath == NULL) {
389 DEBUG(0, ("asprintf failed\n"));
390 return;
392 notify_trigger(conn->notify_ctx, action, filter, fullpath);
393 TALLOC_FREE(fullpath);
396 static void notify_fsp(files_struct *fsp, uint32 action, const char *name)
398 struct notify_change *change, *changes;
399 char *tmp;
401 if (fsp->notify == NULL) {
403 * Nobody is waiting, don't queue
405 return;
409 * Someone has triggered a notify previously, queue the change for
410 * later.
413 if ((fsp->notify->num_changes > 1000) || (name == NULL)) {
415 * The real number depends on the client buf, just provide a
416 * guard against a DoS here. If name == NULL the CN backend is
417 * alerting us to a problem. Possibly dropped events. Clear
418 * queued changes and send the catch-all response to the client
419 * if a request is pending.
421 TALLOC_FREE(fsp->notify->changes);
422 fsp->notify->num_changes = -1;
423 if (fsp->notify->requests != NULL) {
424 change_notify_reply(fsp->notify->requests->req,
425 NT_STATUS_OK,
426 fsp->notify->requests->max_param,
427 fsp->notify,
428 fsp->notify->requests->reply_fn);
429 change_notify_remove_request(fsp->conn->sconn,
430 fsp->notify->requests);
432 return;
435 /* If we've exceeded the server side queue or received a NULL name
436 * from the underlying CN implementation, don't queue up any more
437 * requests until we can send a catch-all response to the client */
438 if (fsp->notify->num_changes == -1) {
439 return;
442 if (!(changes = talloc_realloc(
443 fsp->notify, fsp->notify->changes,
444 struct notify_change, fsp->notify->num_changes+1))) {
445 DEBUG(0, ("talloc_realloc failed\n"));
446 return;
449 fsp->notify->changes = changes;
451 change = &(fsp->notify->changes[fsp->notify->num_changes]);
453 if (!(tmp = talloc_strdup(changes, name))) {
454 DEBUG(0, ("talloc_strdup failed\n"));
455 return;
458 string_replace(tmp, '/', '\\');
459 change->name = tmp;
461 change->action = action;
462 fsp->notify->num_changes += 1;
464 if (fsp->notify->requests == NULL) {
466 * Nobody is waiting, so don't send anything. The ot
468 return;
471 if (action == NOTIFY_ACTION_OLD_NAME) {
473 * We have to send the two rename events in one reply. So hold
474 * the first part back.
476 return;
480 * Someone is waiting for the change, trigger the reply immediately.
482 * TODO: do we have to walk the lists of requests pending?
485 change_notify_reply(fsp->notify->requests->req,
486 NT_STATUS_OK,
487 fsp->notify->requests->max_param,
488 fsp->notify,
489 fsp->notify->requests->reply_fn);
491 change_notify_remove_request(fsp->conn->sconn, fsp->notify->requests);
494 char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32 filter)
496 char *result = NULL;
498 result = talloc_strdup(mem_ctx, "");
500 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
501 result = talloc_asprintf_append(result, "FILE_NAME|");
502 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
503 result = talloc_asprintf_append(result, "DIR_NAME|");
504 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
505 result = talloc_asprintf_append(result, "ATTRIBUTES|");
506 if (filter & FILE_NOTIFY_CHANGE_SIZE)
507 result = talloc_asprintf_append(result, "SIZE|");
508 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
509 result = talloc_asprintf_append(result, "LAST_WRITE|");
510 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
511 result = talloc_asprintf_append(result, "LAST_ACCESS|");
512 if (filter & FILE_NOTIFY_CHANGE_CREATION)
513 result = talloc_asprintf_append(result, "CREATION|");
514 if (filter & FILE_NOTIFY_CHANGE_EA)
515 result = talloc_asprintf_append(result, "EA|");
516 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
517 result = talloc_asprintf_append(result, "SECURITY|");
518 if (filter & FILE_NOTIFY_CHANGE_STREAM_NAME)
519 result = talloc_asprintf_append(result, "STREAM_NAME|");
520 if (filter & FILE_NOTIFY_CHANGE_STREAM_SIZE)
521 result = talloc_asprintf_append(result, "STREAM_SIZE|");
522 if (filter & FILE_NOTIFY_CHANGE_STREAM_WRITE)
523 result = talloc_asprintf_append(result, "STREAM_WRITE|");
525 if (result == NULL) return NULL;
526 if (*result == '\0') return result;
528 result[strlen(result)-1] = '\0';
529 return result;
532 struct sys_notify_context *sys_notify_context_create(connection_struct *conn,
533 TALLOC_CTX *mem_ctx,
534 struct event_context *ev)
536 struct sys_notify_context *ctx;
538 if (!(ctx = talloc(mem_ctx, struct sys_notify_context))) {
539 DEBUG(0, ("talloc failed\n"));
540 return NULL;
543 ctx->ev = ev;
544 ctx->conn = conn;
545 ctx->private_data = NULL;
546 return ctx;
549 NTSTATUS sys_notify_watch(struct sys_notify_context *ctx,
550 struct notify_entry *e,
551 const char *path,
552 void (*callback)(struct sys_notify_context *ctx,
553 void *private_data,
554 struct notify_event *ev),
555 void *private_data, void *handle)
557 return SMB_VFS_NOTIFY_WATCH(ctx->conn, ctx, e, path, callback,
558 private_data, handle);