Revert commit "0551284dc08eb93ef7b2b2227a45e5ec21d482fb" - simplify
[Samba/fernandojvsilva.git] / source3 / smbd / notify.c
blobe430fcfc04c4a039f6ac24e107a08200f025400e
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/globals.h"
25 struct notify_change_request {
26 struct notify_change_request *prev, *next;
27 struct files_struct *fsp; /* backpointer for cancel by mid */
28 struct smb_request *req;
29 uint32 filter;
30 uint32 max_param;
31 void (*reply_fn)(struct smb_request *req,
32 NTSTATUS error_code,
33 uint8_t *buf, size_t len);
34 struct notify_mid_map *mid_map;
35 void *backend_data;
38 static void notify_fsp(files_struct *fsp, uint32 action, const char *name);
41 * For NTCancel, we need to find the notify_change_request indexed by
42 * mid. Separate list here.
45 struct notify_mid_map {
46 struct notify_mid_map *prev, *next;
47 struct notify_change_request *req;
48 uint16 mid;
51 static bool notify_change_record_identical(struct notify_change *c1,
52 struct notify_change *c2)
54 /* Note this is deliberately case sensitive. */
55 if (c1->action == c2->action &&
56 strcmp(c1->name, c2->name) == 0) {
57 return True;
59 return False;
62 static bool notify_marshall_changes(int num_changes,
63 uint32 max_offset,
64 struct notify_change *changes,
65 prs_struct *ps)
67 int i;
68 UNISTR uni_name;
70 if (num_changes == -1) {
71 return false;
74 uni_name.buffer = NULL;
76 for (i=0; i<num_changes; i++) {
77 struct notify_change *c;
78 size_t namelen;
79 uint32 u32_tmp; /* Temp arg to prs_uint32 to avoid
80 * signed/unsigned issues */
82 /* Coalesce any identical records. */
83 while (i+1 < num_changes &&
84 notify_change_record_identical(&changes[i],
85 &changes[i+1])) {
86 i++;
89 c = &changes[i];
91 if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF16LE,
92 c->name, strlen(c->name)+1, &uni_name.buffer,
93 &namelen, True) || (uni_name.buffer == NULL)) {
94 goto fail;
97 namelen -= 2; /* Dump NULL termination */
100 * Offset to next entry, only if there is one
103 u32_tmp = (i == num_changes-1) ? 0 : namelen + 12;
104 if (!prs_uint32("offset", ps, 1, &u32_tmp)) goto fail;
106 u32_tmp = c->action;
107 if (!prs_uint32("action", ps, 1, &u32_tmp)) goto fail;
109 u32_tmp = namelen;
110 if (!prs_uint32("namelen", ps, 1, &u32_tmp)) goto fail;
112 if (!prs_unistr("name", ps, 1, &uni_name)) goto fail;
115 * Not NULL terminated, decrease by the 2 UCS2 \0 chars
117 prs_set_offset(ps, prs_offset(ps)-2);
119 TALLOC_FREE(uni_name.buffer);
121 if (prs_offset(ps) > max_offset) {
122 /* Too much data for client. */
123 DEBUG(10, ("Client only wanted %d bytes, trying to "
124 "marshall %d bytes\n", (int)max_offset,
125 (int)prs_offset(ps)));
126 return False;
130 return True;
132 fail:
133 TALLOC_FREE(uni_name.buffer);
134 return False;
137 /****************************************************************************
138 Setup the common parts of the return packet and send it.
139 *****************************************************************************/
141 void change_notify_reply(connection_struct *conn,
142 struct smb_request *req,
143 NTSTATUS error_code,
144 uint32_t max_param,
145 struct notify_change_buf *notify_buf,
146 void (*reply_fn)(struct smb_request *req,
147 NTSTATUS error_code,
148 uint8_t *buf, size_t len))
150 prs_struct ps;
152 if (!NT_STATUS_IS_OK(error_code)) {
153 reply_fn(req, error_code, NULL, 0);
154 return;
157 if (max_param == 0 || notify_buf == NULL) {
158 reply_fn(req, NT_STATUS_OK, NULL, 0);
159 return;
162 prs_init_empty(&ps, NULL, MARSHALL);
164 if (!notify_marshall_changes(notify_buf->num_changes, max_param,
165 notify_buf->changes, &ps)) {
167 * We exceed what the client is willing to accept. Send
168 * nothing.
170 prs_mem_free(&ps);
171 prs_init_empty(&ps, NULL, MARSHALL);
174 reply_fn(req, NT_STATUS_OK, (uint8_t *)prs_data_p(&ps), prs_offset(&ps));
176 prs_mem_free(&ps);
178 TALLOC_FREE(notify_buf->changes);
179 notify_buf->num_changes = 0;
182 static void notify_callback(void *private_data, const struct notify_event *e)
184 files_struct *fsp = (files_struct *)private_data;
185 DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp)));
186 notify_fsp(fsp, e->action, e->path);
189 NTSTATUS change_notify_create(struct files_struct *fsp, uint32 filter,
190 bool recursive)
192 char *fullpath;
193 struct notify_entry e;
194 NTSTATUS status;
196 SMB_ASSERT(fsp->notify == NULL);
198 if (!(fsp->notify = TALLOC_ZERO_P(NULL, struct notify_change_buf))) {
199 DEBUG(0, ("talloc failed\n"));
200 return NT_STATUS_NO_MEMORY;
203 /* Do notify operations on the base_name. */
204 if (asprintf(&fullpath, "%s/%s", fsp->conn->connectpath,
205 fsp->fsp_name->base_name) == -1) {
206 DEBUG(0, ("asprintf failed\n"));
207 TALLOC_FREE(fsp->notify);
208 return NT_STATUS_NO_MEMORY;
211 ZERO_STRUCT(e);
212 e.path = fullpath;
213 e.dir_fd = fsp->fh->fd;
214 e.dir_id = fsp->file_id;
215 e.filter = filter;
216 e.subdir_filter = 0;
217 if (recursive) {
218 e.subdir_filter = filter;
221 status = notify_add(fsp->conn->notify_ctx, &e, notify_callback, fsp);
222 SAFE_FREE(fullpath);
224 return status;
227 NTSTATUS change_notify_add_request(struct smb_request *req,
228 uint32 max_param,
229 uint32 filter, bool recursive,
230 struct files_struct *fsp,
231 void (*reply_fn)(struct smb_request *req,
232 NTSTATUS error_code,
233 uint8_t *buf, size_t len))
235 struct notify_change_request *request = NULL;
236 struct notify_mid_map *map = NULL;
237 struct smbd_server_connection *sconn = smbd_server_conn;
239 DEBUG(10, ("change_notify_add_request: Adding request for %s: "
240 "max_param = %d\n", fsp_str_dbg(fsp), (int)max_param));
242 if (!(request = talloc(NULL, struct notify_change_request))
243 || !(map = talloc(request, struct notify_mid_map))) {
244 TALLOC_FREE(request);
245 return NT_STATUS_NO_MEMORY;
248 request->mid_map = map;
249 map->req = request;
251 request->req = talloc_move(request, &req);
252 request->max_param = max_param;
253 request->filter = filter;
254 request->fsp = fsp;
255 request->reply_fn = reply_fn;
256 request->backend_data = NULL;
258 DLIST_ADD_END(fsp->notify->requests, request,
259 struct notify_change_request *);
261 map->mid = request->req->mid;
262 DLIST_ADD(sconn->smb1.notify_mid_maps, map);
264 return NT_STATUS_OK;
267 static void change_notify_remove_request(struct notify_change_request *remove_req)
269 files_struct *fsp;
270 struct notify_change_request *req;
271 struct smbd_server_connection *sconn = smbd_server_conn;
274 * Paranoia checks, the fsp referenced must must have the request in
275 * its list of pending requests
278 fsp = remove_req->fsp;
279 SMB_ASSERT(fsp->notify != NULL);
281 for (req = fsp->notify->requests; req; req = req->next) {
282 if (req == remove_req) {
283 break;
287 if (req == NULL) {
288 smb_panic("notify_req not found in fsp's requests");
291 DLIST_REMOVE(fsp->notify->requests, req);
292 DLIST_REMOVE(sconn->smb1.notify_mid_maps, req->mid_map);
293 TALLOC_FREE(req);
296 /****************************************************************************
297 Delete entries by mid from the change notify pending queue. Always send reply.
298 *****************************************************************************/
300 void remove_pending_change_notify_requests_by_mid(uint16 mid)
302 struct notify_mid_map *map;
303 struct smbd_server_connection *sconn = smbd_server_conn;
305 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
306 if (map->mid == mid) {
307 break;
311 if (map == NULL) {
312 return;
315 change_notify_reply(map->req->fsp->conn, map->req->req,
316 NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
317 change_notify_remove_request(map->req);
320 void smbd_notify_cancel_by_smbreq(struct smbd_server_connection *sconn,
321 const struct smb_request *smbreq)
323 struct notify_mid_map *map;
325 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
326 if (map->req->req == smbreq) {
327 break;
331 if (map == NULL) {
332 return;
335 change_notify_reply(map->req->fsp->conn, map->req->req,
336 NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
337 change_notify_remove_request(map->req);
340 /****************************************************************************
341 Delete entries by fnum from the change notify pending queue.
342 *****************************************************************************/
344 void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
345 NTSTATUS status)
347 if (fsp->notify == NULL) {
348 return;
351 while (fsp->notify->requests != NULL) {
352 change_notify_reply(fsp->conn, fsp->notify->requests->req,
353 status, 0, NULL,
354 fsp->notify->requests->reply_fn);
355 change_notify_remove_request(fsp->notify->requests);
359 void notify_fname(connection_struct *conn, uint32 action, uint32 filter,
360 const char *path)
362 char *fullpath;
363 char *parent;
364 const char *name;
366 if (path[0] == '.' && path[1] == '/') {
367 path += 2;
369 if (parent_dirname(talloc_tos(), path, &parent, &name)) {
370 struct smb_filename *smb_fname_parent = NULL;
371 NTSTATUS status;
373 status = create_synthetic_smb_fname(talloc_tos(), parent, NULL,
374 NULL, &smb_fname_parent);
375 if (!NT_STATUS_IS_OK(status)) {
376 return;
378 if (SMB_VFS_STAT(conn, smb_fname_parent) != -1) {
379 notify_onelevel(conn->notify_ctx, action, filter,
380 SMB_VFS_FILE_ID_CREATE(conn, &smb_fname_parent->st),
381 name);
383 TALLOC_FREE(smb_fname_parent);
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->conn,
425 fsp->notify->requests->req,
426 NT_STATUS_OK,
427 fsp->notify->requests->max_param,
428 fsp->notify,
429 fsp->notify->requests->reply_fn);
430 change_notify_remove_request(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_ARRAY(
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->conn,
486 fsp->notify->requests->req,
487 NT_STATUS_OK,
488 fsp->notify->requests->max_param,
489 fsp->notify,
490 fsp->notify->requests->reply_fn);
492 change_notify_remove_request(fsp->notify->requests);
495 char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32 filter)
497 char *result = NULL;
499 result = talloc_strdup(mem_ctx, "");
501 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
502 result = talloc_asprintf_append(result, "FILE_NAME|");
503 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
504 result = talloc_asprintf_append(result, "DIR_NAME|");
505 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
506 result = talloc_asprintf_append(result, "ATTRIBUTES|");
507 if (filter & FILE_NOTIFY_CHANGE_SIZE)
508 result = talloc_asprintf_append(result, "SIZE|");
509 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
510 result = talloc_asprintf_append(result, "LAST_WRITE|");
511 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
512 result = talloc_asprintf_append(result, "LAST_ACCESS|");
513 if (filter & FILE_NOTIFY_CHANGE_CREATION)
514 result = talloc_asprintf_append(result, "CREATION|");
515 if (filter & FILE_NOTIFY_CHANGE_EA)
516 result = talloc_asprintf_append(result, "EA|");
517 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
518 result = talloc_asprintf_append(result, "SECURITY|");
519 if (filter & FILE_NOTIFY_CHANGE_STREAM_NAME)
520 result = talloc_asprintf_append(result, "STREAM_NAME|");
521 if (filter & FILE_NOTIFY_CHANGE_STREAM_SIZE)
522 result = talloc_asprintf_append(result, "STREAM_SIZE|");
523 if (filter & FILE_NOTIFY_CHANGE_STREAM_WRITE)
524 result = talloc_asprintf_append(result, "STREAM_WRITE|");
526 if (result == NULL) return NULL;
527 if (*result == '\0') return result;
529 result[strlen(result)-1] = '\0';
530 return result;
533 struct sys_notify_context *sys_notify_context_create(connection_struct *conn,
534 TALLOC_CTX *mem_ctx,
535 struct event_context *ev)
537 struct sys_notify_context *ctx;
539 if (!(ctx = TALLOC_P(mem_ctx, struct sys_notify_context))) {
540 DEBUG(0, ("talloc failed\n"));
541 return NULL;
544 ctx->ev = ev;
545 ctx->conn = conn;
546 ctx->private_data = NULL;
547 return ctx;
550 NTSTATUS sys_notify_watch(struct sys_notify_context *ctx,
551 struct notify_entry *e,
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, callback, private_data,
558 handle);