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/globals.h"
24 #include "../librpc/gen_ndr/notify.h"
26 struct notify_change_request {
27 struct notify_change_request *prev, *next;
28 struct files_struct *fsp; /* backpointer for cancel by mid */
29 struct smb_request *req;
32 void (*reply_fn)(struct smb_request *req,
34 uint8_t *buf, size_t len);
35 struct notify_mid_map *mid_map;
39 static void notify_fsp(files_struct *fsp, uint32 action, const char *name);
42 * For NTCancel, we need to find the notify_change_request indexed by
43 * mid. Separate list here.
46 struct notify_mid_map {
47 struct notify_mid_map *prev, *next;
48 struct notify_change_request *req;
52 static bool notify_change_record_identical(struct notify_change *c1,
53 struct notify_change *c2)
55 /* Note this is deliberately case sensitive. */
56 if (c1->action == c2->action &&
57 strcmp(c1->name, c2->name) == 0) {
63 static bool notify_marshall_changes(int num_changes,
65 struct notify_change *changes,
71 if (num_changes == -1) {
75 uni_name.buffer = NULL;
77 for (i=0; i<num_changes; i++) {
78 struct notify_change *c;
81 uint32 u32_tmp; /* Temp arg to prs_uint32 to avoid
82 * signed/unsigned issues */
84 /* Coalesce any identical records. */
85 while (i+1 < num_changes &&
86 notify_change_record_identical(&changes[i],
93 if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF16LE,
94 c->name, strlen(c->name)+1, &uni_name.buffer,
95 &namelen, True) || (uni_name.buffer == NULL)) {
99 namelen -= 2; /* Dump NULL termination */
102 * Offset to next entry, only if there is one
105 u32_tmp = (i == num_changes-1) ? 0 : namelen + 12;
107 /* Align on 4-byte boundary according to MS-CIFS 2.2.7.4.2 */
108 if ((rem = u32_tmp % 4 ) != 0)
111 if (!prs_uint32("offset", ps, 1, &u32_tmp)) goto fail;
114 if (!prs_uint32("action", ps, 1, &u32_tmp)) goto fail;
117 if (!prs_uint32("namelen", ps, 1, &u32_tmp)) goto fail;
119 if (!prs_unistr("name", ps, 1, &uni_name)) goto fail;
122 * Not NULL terminated, decrease by the 2 UCS2 \0 chars
124 prs_set_offset(ps, prs_offset(ps)-2);
127 if (!prs_align_custom(ps, 4)) goto fail;
130 TALLOC_FREE(uni_name.buffer);
132 if (prs_offset(ps) > max_offset) {
133 /* Too much data for client. */
134 DEBUG(10, ("Client only wanted %d bytes, trying to "
135 "marshall %d bytes\n", (int)max_offset,
136 (int)prs_offset(ps)));
144 TALLOC_FREE(uni_name.buffer);
148 /****************************************************************************
149 Setup the common parts of the return packet and send it.
150 *****************************************************************************/
152 void change_notify_reply(struct smb_request *req,
155 struct notify_change_buf *notify_buf,
156 void (*reply_fn)(struct smb_request *req,
158 uint8_t *buf, size_t len))
162 if (!NT_STATUS_IS_OK(error_code)) {
163 reply_fn(req, error_code, NULL, 0);
167 if (max_param == 0 || notify_buf == NULL) {
168 reply_fn(req, NT_STATUS_OK, NULL, 0);
172 prs_init_empty(&ps, NULL, MARSHALL);
174 if (!notify_marshall_changes(notify_buf->num_changes, max_param,
175 notify_buf->changes, &ps)) {
177 * We exceed what the client is willing to accept. Send
181 prs_init_empty(&ps, NULL, MARSHALL);
184 reply_fn(req, NT_STATUS_OK, (uint8_t *)prs_data_p(&ps), prs_offset(&ps));
188 TALLOC_FREE(notify_buf->changes);
189 notify_buf->num_changes = 0;
192 static void notify_callback(void *private_data, const struct notify_event *e)
194 files_struct *fsp = (files_struct *)private_data;
195 DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp)));
196 notify_fsp(fsp, e->action, e->path);
199 NTSTATUS change_notify_create(struct files_struct *fsp, uint32 filter,
203 struct notify_entry e;
206 SMB_ASSERT(fsp->notify == NULL);
208 if (!(fsp->notify = TALLOC_ZERO_P(NULL, struct notify_change_buf))) {
209 DEBUG(0, ("talloc failed\n"));
210 return NT_STATUS_NO_MEMORY;
213 /* Do notify operations on the base_name. */
214 if (asprintf(&fullpath, "%s/%s", fsp->conn->connectpath,
215 fsp->fsp_name->base_name) == -1) {
216 DEBUG(0, ("asprintf failed\n"));
217 TALLOC_FREE(fsp->notify);
218 return NT_STATUS_NO_MEMORY;
223 e.dir_fd = fsp->fh->fd;
224 e.dir_id = fsp->file_id;
228 e.subdir_filter = filter;
231 status = notify_add(fsp->conn->notify_ctx, &e, notify_callback, fsp);
237 NTSTATUS change_notify_add_request(struct smb_request *req,
239 uint32 filter, bool recursive,
240 struct files_struct *fsp,
241 void (*reply_fn)(struct smb_request *req,
243 uint8_t *buf, size_t len))
245 struct notify_change_request *request = NULL;
246 struct notify_mid_map *map = NULL;
247 struct smbd_server_connection *sconn = req->sconn;
249 DEBUG(10, ("change_notify_add_request: Adding request for %s: "
250 "max_param = %d\n", fsp_str_dbg(fsp), (int)max_param));
252 if (!(request = talloc(NULL, struct notify_change_request))
253 || !(map = talloc(request, struct notify_mid_map))) {
254 TALLOC_FREE(request);
255 return NT_STATUS_NO_MEMORY;
258 request->mid_map = map;
261 request->req = talloc_move(request, &req);
262 request->max_param = max_param;
263 request->filter = filter;
265 request->reply_fn = reply_fn;
266 request->backend_data = NULL;
268 DLIST_ADD_END(fsp->notify->requests, request,
269 struct notify_change_request *);
271 map->mid = request->req->mid;
272 DLIST_ADD(sconn->smb1.notify_mid_maps, map);
277 static void change_notify_remove_request(struct smbd_server_connection *sconn,
278 struct notify_change_request *remove_req)
281 struct notify_change_request *req;
284 * Paranoia checks, the fsp referenced must must have the request in
285 * its list of pending requests
288 fsp = remove_req->fsp;
289 SMB_ASSERT(fsp->notify != NULL);
291 for (req = fsp->notify->requests; req; req = req->next) {
292 if (req == remove_req) {
298 smb_panic("notify_req not found in fsp's requests");
301 DLIST_REMOVE(fsp->notify->requests, req);
302 DLIST_REMOVE(sconn->smb1.notify_mid_maps, req->mid_map);
306 /****************************************************************************
307 Delete entries by mid from the change notify pending queue. Always send reply.
308 *****************************************************************************/
310 void remove_pending_change_notify_requests_by_mid(
311 struct smbd_server_connection *sconn, uint64_t mid)
313 struct notify_mid_map *map;
315 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
316 if (map->mid == mid) {
325 change_notify_reply(map->req->req,
326 NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
327 change_notify_remove_request(sconn, map->req);
330 void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
332 struct smbd_server_connection *sconn = smbreq->sconn;
333 struct notify_mid_map *map;
335 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
336 if (map->req->req == smbreq) {
345 change_notify_reply(map->req->req,
346 NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
347 change_notify_remove_request(sconn, map->req);
350 /****************************************************************************
351 Delete entries by fnum from the change notify pending queue.
352 *****************************************************************************/
354 void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
357 if (fsp->notify == NULL) {
361 while (fsp->notify->requests != NULL) {
362 change_notify_reply(fsp->notify->requests->req,
364 fsp->notify->requests->reply_fn);
365 change_notify_remove_request(fsp->conn->sconn,
366 fsp->notify->requests);
370 void notify_fname(connection_struct *conn, uint32 action, uint32 filter,
377 if (path[0] == '.' && path[1] == '/') {
380 if (parent_dirname(talloc_tos(), path, &parent, &name)) {
381 struct smb_filename smb_fname_parent;
383 ZERO_STRUCT(smb_fname_parent);
384 smb_fname_parent.base_name = parent;
386 if (SMB_VFS_STAT(conn, &smb_fname_parent) != -1) {
387 notify_onelevel(conn->notify_ctx, action, filter,
388 SMB_VFS_FILE_ID_CREATE(conn, &smb_fname_parent.st),
393 fullpath = talloc_asprintf(talloc_tos(), "%s/%s", conn->connectpath,
395 if (fullpath == NULL) {
396 DEBUG(0, ("asprintf failed\n"));
399 notify_trigger(conn->notify_ctx, action, filter, fullpath);
400 TALLOC_FREE(fullpath);
403 static void notify_fsp(files_struct *fsp, uint32 action, const char *name)
405 struct notify_change *change, *changes;
408 if (fsp->notify == NULL) {
410 * Nobody is waiting, don't queue
416 * Someone has triggered a notify previously, queue the change for
420 if ((fsp->notify->num_changes > 1000) || (name == NULL)) {
422 * The real number depends on the client buf, just provide a
423 * guard against a DoS here. If name == NULL the CN backend is
424 * alerting us to a problem. Possibly dropped events. Clear
425 * queued changes and send the catch-all response to the client
426 * if a request is pending.
428 TALLOC_FREE(fsp->notify->changes);
429 fsp->notify->num_changes = -1;
430 if (fsp->notify->requests != NULL) {
431 change_notify_reply(fsp->notify->requests->req,
433 fsp->notify->requests->max_param,
435 fsp->notify->requests->reply_fn);
436 change_notify_remove_request(fsp->conn->sconn,
437 fsp->notify->requests);
442 /* If we've exceeded the server side queue or received a NULL name
443 * from the underlying CN implementation, don't queue up any more
444 * requests until we can send a catch-all response to the client */
445 if (fsp->notify->num_changes == -1) {
449 if (!(changes = TALLOC_REALLOC_ARRAY(
450 fsp->notify, fsp->notify->changes,
451 struct notify_change, fsp->notify->num_changes+1))) {
452 DEBUG(0, ("talloc_realloc failed\n"));
456 fsp->notify->changes = changes;
458 change = &(fsp->notify->changes[fsp->notify->num_changes]);
460 if (!(tmp = talloc_strdup(changes, name))) {
461 DEBUG(0, ("talloc_strdup failed\n"));
465 string_replace(tmp, '/', '\\');
468 change->action = action;
469 fsp->notify->num_changes += 1;
471 if (fsp->notify->requests == NULL) {
473 * Nobody is waiting, so don't send anything. The ot
478 if (action == NOTIFY_ACTION_OLD_NAME) {
480 * We have to send the two rename events in one reply. So hold
481 * the first part back.
487 * Someone is waiting for the change, trigger the reply immediately.
489 * TODO: do we have to walk the lists of requests pending?
492 change_notify_reply(fsp->notify->requests->req,
494 fsp->notify->requests->max_param,
496 fsp->notify->requests->reply_fn);
498 change_notify_remove_request(fsp->conn->sconn, fsp->notify->requests);
501 char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32 filter)
505 result = talloc_strdup(mem_ctx, "");
507 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
508 result = talloc_asprintf_append(result, "FILE_NAME|");
509 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
510 result = talloc_asprintf_append(result, "DIR_NAME|");
511 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
512 result = talloc_asprintf_append(result, "ATTRIBUTES|");
513 if (filter & FILE_NOTIFY_CHANGE_SIZE)
514 result = talloc_asprintf_append(result, "SIZE|");
515 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
516 result = talloc_asprintf_append(result, "LAST_WRITE|");
517 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
518 result = talloc_asprintf_append(result, "LAST_ACCESS|");
519 if (filter & FILE_NOTIFY_CHANGE_CREATION)
520 result = talloc_asprintf_append(result, "CREATION|");
521 if (filter & FILE_NOTIFY_CHANGE_EA)
522 result = talloc_asprintf_append(result, "EA|");
523 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
524 result = talloc_asprintf_append(result, "SECURITY|");
525 if (filter & FILE_NOTIFY_CHANGE_STREAM_NAME)
526 result = talloc_asprintf_append(result, "STREAM_NAME|");
527 if (filter & FILE_NOTIFY_CHANGE_STREAM_SIZE)
528 result = talloc_asprintf_append(result, "STREAM_SIZE|");
529 if (filter & FILE_NOTIFY_CHANGE_STREAM_WRITE)
530 result = talloc_asprintf_append(result, "STREAM_WRITE|");
532 if (result == NULL) return NULL;
533 if (*result == '\0') return result;
535 result[strlen(result)-1] = '\0';
539 struct sys_notify_context *sys_notify_context_create(connection_struct *conn,
541 struct event_context *ev)
543 struct sys_notify_context *ctx;
545 if (!(ctx = TALLOC_P(mem_ctx, struct sys_notify_context))) {
546 DEBUG(0, ("talloc failed\n"));
552 ctx->private_data = NULL;
556 NTSTATUS sys_notify_watch(struct sys_notify_context *ctx,
557 struct notify_entry *e,
558 void (*callback)(struct sys_notify_context *ctx,
560 struct notify_event *ev),
561 void *private_data, void *handle)
563 return SMB_VFS_NOTIFY_WATCH(ctx->conn, ctx, e, callback, private_data,