Remove the use of dirfd inside the vfs_dirsort.c.
[Samba/gebeck_regimport.git] / source3 / smbd / notify.c
blob4842d6f2fcb33b4a1ba34e404fa6a6666cb87c76
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_buf {
29 * If no requests are pending, changes are queued here. Simple array,
30 * we only append.
34 * num_changes == -1 means that we have got a catch-all change, when
35 * asked we just return NT_STATUS_OK without specific changes.
37 int num_changes;
38 struct notify_change *changes;
41 * If no changes are around requests are queued here. Using a linked
42 * list, because we have to append at the end and delete from the top.
44 struct notify_change_request *requests;
47 struct notify_change_request {
48 struct notify_change_request *prev, *next;
49 struct files_struct *fsp; /* backpointer for cancel by mid */
50 struct smb_request *req;
51 uint32 filter;
52 uint32 max_param;
53 void (*reply_fn)(struct smb_request *req,
54 NTSTATUS error_code,
55 uint8_t *buf, size_t len);
56 struct notify_mid_map *mid_map;
57 void *backend_data;
60 static void notify_fsp(files_struct *fsp, uint32 action, const char *name);
62 bool change_notify_fsp_has_changes(struct files_struct *fsp)
64 if (fsp == NULL) {
65 return false;
68 if (fsp->notify == NULL) {
69 return false;
72 if (fsp->notify->num_changes == 0) {
73 return false;
76 return true;
80 * For NTCancel, we need to find the notify_change_request indexed by
81 * mid. Separate list here.
84 struct notify_mid_map {
85 struct notify_mid_map *prev, *next;
86 struct notify_change_request *req;
87 uint64_t mid;
90 static bool notify_change_record_identical(struct notify_change *c1,
91 struct notify_change *c2)
93 /* Note this is deliberately case sensitive. */
94 if (c1->action == c2->action &&
95 strcmp(c1->name, c2->name) == 0) {
96 return True;
98 return False;
101 static bool notify_marshall_changes(int num_changes,
102 uint32 max_offset,
103 struct notify_change *changes,
104 DATA_BLOB *final_blob)
106 int i;
108 if (num_changes == -1) {
109 return false;
112 for (i=0; i<num_changes; i++) {
113 enum ndr_err_code ndr_err;
114 struct notify_change *c;
115 struct FILE_NOTIFY_INFORMATION m;
116 DATA_BLOB blob;
118 /* Coalesce any identical records. */
119 while (i+1 < num_changes &&
120 notify_change_record_identical(&changes[i],
121 &changes[i+1])) {
122 i++;
125 c = &changes[i];
127 m.FileName1 = c->name;
128 m.FileNameLength = strlen_m(c->name)*2;
129 m.Action = c->action;
130 m.NextEntryOffset = (i == num_changes-1) ? 0 : ndr_size_FILE_NOTIFY_INFORMATION(&m, 0);
133 * Offset to next entry, only if there is one
136 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &m,
137 (ndr_push_flags_fn_t)ndr_push_FILE_NOTIFY_INFORMATION);
138 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
139 return false;
142 if (DEBUGLEVEL >= 10) {
143 NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION, &m);
146 if (!data_blob_append(talloc_tos(), final_blob,
147 blob.data, blob.length)) {
148 data_blob_free(&blob);
149 return false;
152 data_blob_free(&blob);
154 if (final_blob->length > max_offset) {
155 /* Too much data for client. */
156 DEBUG(10, ("Client only wanted %d bytes, trying to "
157 "marshall %d bytes\n", (int)max_offset,
158 (int)final_blob->length));
159 return False;
163 return True;
166 /****************************************************************************
167 Setup the common parts of the return packet and send it.
168 *****************************************************************************/
170 void change_notify_reply(struct smb_request *req,
171 NTSTATUS error_code,
172 uint32_t max_param,
173 struct notify_change_buf *notify_buf,
174 void (*reply_fn)(struct smb_request *req,
175 NTSTATUS error_code,
176 uint8_t *buf, size_t len))
178 DATA_BLOB blob = data_blob_null;
180 if (!NT_STATUS_IS_OK(error_code)) {
181 reply_fn(req, error_code, NULL, 0);
182 return;
185 if (max_param == 0 || notify_buf == NULL) {
186 reply_fn(req, NT_STATUS_OK, NULL, 0);
187 return;
190 if (!notify_marshall_changes(notify_buf->num_changes, max_param,
191 notify_buf->changes, &blob)) {
193 * We exceed what the client is willing to accept. Send
194 * nothing.
196 data_blob_free(&blob);
199 reply_fn(req, NT_STATUS_OK, blob.data, blob.length);
201 data_blob_free(&blob);
203 TALLOC_FREE(notify_buf->changes);
204 notify_buf->num_changes = 0;
207 static void notify_callback(void *private_data, const struct notify_event *e)
209 files_struct *fsp = (files_struct *)private_data;
210 DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp)));
211 notify_fsp(fsp, e->action, e->path);
214 static void sys_notify_callback(struct sys_notify_context *ctx,
215 void *private_data,
216 struct notify_event *e)
218 files_struct *fsp = (files_struct *)private_data;
219 DEBUG(10, ("sys_notify_callback called for %s\n", fsp_str_dbg(fsp)));
220 notify_fsp(fsp, e->action, e->path);
223 NTSTATUS change_notify_create(struct files_struct *fsp, uint32 filter,
224 bool recursive)
226 char *fullpath;
227 size_t len;
228 uint32_t subdir_filter;
229 NTSTATUS status = NT_STATUS_NOT_IMPLEMENTED;
231 if (fsp->notify != NULL) {
232 DEBUG(1, ("change_notify_create: fsp->notify != NULL, "
233 "fname = %s\n", fsp->fsp_name->base_name));
234 return NT_STATUS_INVALID_PARAMETER;
237 if (!(fsp->notify = talloc_zero(NULL, struct notify_change_buf))) {
238 DEBUG(0, ("talloc failed\n"));
239 return NT_STATUS_NO_MEMORY;
242 /* Do notify operations on the base_name. */
243 fullpath = talloc_asprintf(
244 talloc_tos(), "%s/%s", fsp->conn->connectpath,
245 fsp->fsp_name->base_name);
246 if (fullpath == NULL) {
247 DEBUG(0, ("talloc_asprintf failed\n"));
248 TALLOC_FREE(fsp->notify);
249 return NT_STATUS_NO_MEMORY;
253 * Avoid /. at the end of the path name. notify can't deal with it.
255 len = strlen(fullpath);
256 if (len > 1 && fullpath[len-1] == '.' && fullpath[len-2] == '/') {
257 fullpath[len-2] = '\0';
260 subdir_filter = recursive ? filter : 0;
262 if (fsp->conn->sconn->sys_notify_ctx != NULL) {
263 void *sys_notify_handle = NULL;
265 status = SMB_VFS_NOTIFY_WATCH(
266 fsp->conn, fsp->conn->sconn->sys_notify_ctx,
267 fullpath, &filter, &subdir_filter,
268 sys_notify_callback, fsp, &sys_notify_handle);
270 if (NT_STATUS_IS_OK(status)) {
271 talloc_steal(fsp->notify, sys_notify_handle);
275 if ((filter != 0) || (subdir_filter != 0)) {
276 status = notify_add(fsp->conn->sconn->notify_ctx,
277 fullpath, filter, subdir_filter,
278 notify_callback, fsp);
280 TALLOC_FREE(fullpath);
281 return status;
284 NTSTATUS change_notify_add_request(struct smb_request *req,
285 uint32 max_param,
286 uint32 filter, bool recursive,
287 struct files_struct *fsp,
288 void (*reply_fn)(struct smb_request *req,
289 NTSTATUS error_code,
290 uint8_t *buf, size_t len))
292 struct notify_change_request *request = NULL;
293 struct notify_mid_map *map = NULL;
294 struct smbd_server_connection *sconn = req->sconn;
296 DEBUG(10, ("change_notify_add_request: Adding request for %s: "
297 "max_param = %d\n", fsp_str_dbg(fsp), (int)max_param));
299 if (!(request = talloc(NULL, struct notify_change_request))
300 || !(map = talloc(request, struct notify_mid_map))) {
301 TALLOC_FREE(request);
302 return NT_STATUS_NO_MEMORY;
305 request->mid_map = map;
306 map->req = request;
308 request->req = talloc_move(request, &req);
309 request->max_param = max_param;
310 request->filter = filter;
311 request->fsp = fsp;
312 request->reply_fn = reply_fn;
313 request->backend_data = NULL;
315 DLIST_ADD_END(fsp->notify->requests, request,
316 struct notify_change_request *);
318 map->mid = request->req->mid;
319 DLIST_ADD(sconn->smb1.notify_mid_maps, map);
321 return NT_STATUS_OK;
324 static void change_notify_remove_request(struct smbd_server_connection *sconn,
325 struct notify_change_request *remove_req)
327 files_struct *fsp;
328 struct notify_change_request *req;
331 * Paranoia checks, the fsp referenced must must have the request in
332 * its list of pending requests
335 fsp = remove_req->fsp;
336 SMB_ASSERT(fsp->notify != NULL);
338 for (req = fsp->notify->requests; req; req = req->next) {
339 if (req == remove_req) {
340 break;
344 if (req == NULL) {
345 smb_panic("notify_req not found in fsp's requests");
348 DLIST_REMOVE(fsp->notify->requests, req);
349 DLIST_REMOVE(sconn->smb1.notify_mid_maps, req->mid_map);
350 TALLOC_FREE(req);
353 /****************************************************************************
354 Delete entries by mid from the change notify pending queue. Always send reply.
355 *****************************************************************************/
357 void remove_pending_change_notify_requests_by_mid(
358 struct smbd_server_connection *sconn, uint64_t mid)
360 struct notify_mid_map *map;
362 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
363 if (map->mid == mid) {
364 break;
368 if (map == NULL) {
369 return;
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 void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
379 struct smbd_server_connection *sconn = smbreq->sconn;
380 struct notify_mid_map *map;
382 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
383 if (map->req->req == smbreq) {
384 break;
388 if (map == NULL) {
389 return;
392 change_notify_reply(map->req->req,
393 NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
394 change_notify_remove_request(sconn, map->req);
397 /****************************************************************************
398 Delete entries by fnum from the change notify pending queue.
399 *****************************************************************************/
401 void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
402 NTSTATUS status)
404 if (fsp->notify == NULL) {
405 return;
408 while (fsp->notify->requests != NULL) {
409 change_notify_reply(fsp->notify->requests->req,
410 status, 0, NULL,
411 fsp->notify->requests->reply_fn);
412 change_notify_remove_request(fsp->conn->sconn,
413 fsp->notify->requests);
417 void notify_fname(connection_struct *conn, uint32 action, uint32 filter,
418 const char *path)
420 struct notify_context *notify_ctx = conn->sconn->notify_ctx;
421 char *fullpath;
423 if (path[0] == '.' && path[1] == '/') {
424 path += 2;
426 fullpath = talloc_asprintf(talloc_tos(), "%s/%s", conn->connectpath,
427 path);
428 if (fullpath == NULL) {
429 DEBUG(0, ("asprintf failed\n"));
430 return;
432 notify_trigger(notify_ctx, action, filter, fullpath);
433 TALLOC_FREE(fullpath);
436 static void notify_fsp(files_struct *fsp, uint32 action, const char *name)
438 struct notify_change *change, *changes;
439 char *tmp;
441 if (fsp->notify == NULL) {
443 * Nobody is waiting, don't queue
445 return;
449 * Someone has triggered a notify previously, queue the change for
450 * later.
453 if ((fsp->notify->num_changes > 1000) || (name == NULL)) {
455 * The real number depends on the client buf, just provide a
456 * guard against a DoS here. If name == NULL the CN backend is
457 * alerting us to a problem. Possibly dropped events. Clear
458 * queued changes and send the catch-all response to the client
459 * if a request is pending.
461 TALLOC_FREE(fsp->notify->changes);
462 fsp->notify->num_changes = -1;
463 if (fsp->notify->requests != NULL) {
464 change_notify_reply(fsp->notify->requests->req,
465 NT_STATUS_OK,
466 fsp->notify->requests->max_param,
467 fsp->notify,
468 fsp->notify->requests->reply_fn);
469 change_notify_remove_request(fsp->conn->sconn,
470 fsp->notify->requests);
472 return;
475 /* If we've exceeded the server side queue or received a NULL name
476 * from the underlying CN implementation, don't queue up any more
477 * requests until we can send a catch-all response to the client */
478 if (fsp->notify->num_changes == -1) {
479 return;
482 if (!(changes = talloc_realloc(
483 fsp->notify, fsp->notify->changes,
484 struct notify_change, fsp->notify->num_changes+1))) {
485 DEBUG(0, ("talloc_realloc failed\n"));
486 return;
489 fsp->notify->changes = changes;
491 change = &(fsp->notify->changes[fsp->notify->num_changes]);
493 if (!(tmp = talloc_strdup(changes, name))) {
494 DEBUG(0, ("talloc_strdup failed\n"));
495 return;
498 string_replace(tmp, '/', '\\');
499 change->name = tmp;
501 change->action = action;
502 fsp->notify->num_changes += 1;
504 if (fsp->notify->requests == NULL) {
506 * Nobody is waiting, so don't send anything. The ot
508 return;
511 if (action == NOTIFY_ACTION_OLD_NAME) {
513 * We have to send the two rename events in one reply. So hold
514 * the first part back.
516 return;
520 * Someone is waiting for the change, trigger the reply immediately.
522 * TODO: do we have to walk the lists of requests pending?
525 change_notify_reply(fsp->notify->requests->req,
526 NT_STATUS_OK,
527 fsp->notify->requests->max_param,
528 fsp->notify,
529 fsp->notify->requests->reply_fn);
531 change_notify_remove_request(fsp->conn->sconn, fsp->notify->requests);
534 char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32 filter)
536 char *result = NULL;
538 result = talloc_strdup(mem_ctx, "");
540 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
541 result = talloc_asprintf_append(result, "FILE_NAME|");
542 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
543 result = talloc_asprintf_append(result, "DIR_NAME|");
544 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
545 result = talloc_asprintf_append(result, "ATTRIBUTES|");
546 if (filter & FILE_NOTIFY_CHANGE_SIZE)
547 result = talloc_asprintf_append(result, "SIZE|");
548 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
549 result = talloc_asprintf_append(result, "LAST_WRITE|");
550 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
551 result = talloc_asprintf_append(result, "LAST_ACCESS|");
552 if (filter & FILE_NOTIFY_CHANGE_CREATION)
553 result = talloc_asprintf_append(result, "CREATION|");
554 if (filter & FILE_NOTIFY_CHANGE_EA)
555 result = talloc_asprintf_append(result, "EA|");
556 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
557 result = talloc_asprintf_append(result, "SECURITY|");
558 if (filter & FILE_NOTIFY_CHANGE_STREAM_NAME)
559 result = talloc_asprintf_append(result, "STREAM_NAME|");
560 if (filter & FILE_NOTIFY_CHANGE_STREAM_SIZE)
561 result = talloc_asprintf_append(result, "STREAM_SIZE|");
562 if (filter & FILE_NOTIFY_CHANGE_STREAM_WRITE)
563 result = talloc_asprintf_append(result, "STREAM_WRITE|");
565 if (result == NULL) return NULL;
566 if (*result == '\0') return result;
568 result[strlen(result)-1] = '\0';
569 return result;
572 struct sys_notify_context *sys_notify_context_create(TALLOC_CTX *mem_ctx,
573 struct tevent_context *ev)
575 struct sys_notify_context *ctx;
577 if (!(ctx = talloc(mem_ctx, struct sys_notify_context))) {
578 DEBUG(0, ("talloc failed\n"));
579 return NULL;
582 ctx->ev = ev;
583 ctx->private_data = NULL;
584 return ctx;