s3-rpc_server: Use struct pipes_struct.
[Samba/gbeck.git] / source3 / smbd / notify.c
blob011f38fb7e513769f931c948e3d7c0978c847b18
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"
24 #include "../librpc/gen_ndr/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;
30 uint32 filter;
31 uint32 max_param;
32 void (*reply_fn)(struct smb_request *req,
33 NTSTATUS error_code,
34 uint8_t *buf, size_t len);
35 struct notify_mid_map *mid_map;
36 void *backend_data;
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;
49 uint64_t mid;
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) {
58 return True;
60 return False;
63 static bool notify_marshall_changes(int num_changes,
64 uint32 max_offset,
65 struct notify_change *changes,
66 DATA_BLOB *final_blob)
68 int i;
70 if (num_changes == -1) {
71 return false;
74 for (i=0; i<num_changes; i++) {
75 enum ndr_err_code ndr_err;
76 struct notify_change *c;
77 struct FILE_NOTIFY_INFORMATION m;
78 DATA_BLOB blob;
80 /* Coalesce any identical records. */
81 while (i+1 < num_changes &&
82 notify_change_record_identical(&changes[i],
83 &changes[i+1])) {
84 i++;
87 c = &changes[i];
89 m.FileName1 = c->name;
90 m.FileNameLength = strlen_m(c->name)*2;
91 m.Action = c->action;
92 m.NextEntryOffset = (i == num_changes-1) ? 0 : ndr_size_FILE_NOTIFY_INFORMATION(&m, 0);
95 * Offset to next entry, only if there is one
98 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &m,
99 (ndr_push_flags_fn_t)ndr_push_FILE_NOTIFY_INFORMATION);
100 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
101 return false;
104 if (DEBUGLEVEL >= 10) {
105 NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION, &m);
108 if (!data_blob_append(talloc_tos(), final_blob,
109 blob.data, blob.length)) {
110 data_blob_free(&blob);
111 return false;
114 data_blob_free(&blob);
116 if (final_blob->length > max_offset) {
117 /* Too much data for client. */
118 DEBUG(10, ("Client only wanted %d bytes, trying to "
119 "marshall %d bytes\n", (int)max_offset,
120 (int)final_blob->length));
121 return False;
125 return True;
128 /****************************************************************************
129 Setup the common parts of the return packet and send it.
130 *****************************************************************************/
132 void change_notify_reply(struct smb_request *req,
133 NTSTATUS error_code,
134 uint32_t max_param,
135 struct notify_change_buf *notify_buf,
136 void (*reply_fn)(struct smb_request *req,
137 NTSTATUS error_code,
138 uint8_t *buf, size_t len))
140 DATA_BLOB blob = data_blob_null;
142 if (!NT_STATUS_IS_OK(error_code)) {
143 reply_fn(req, error_code, NULL, 0);
144 return;
147 if (max_param == 0 || notify_buf == NULL) {
148 reply_fn(req, NT_STATUS_OK, NULL, 0);
149 return;
152 if (!notify_marshall_changes(notify_buf->num_changes, max_param,
153 notify_buf->changes, &blob)) {
155 * We exceed what the client is willing to accept. Send
156 * nothing.
158 data_blob_free(&blob);
161 reply_fn(req, NT_STATUS_OK, blob.data, blob.length);
163 data_blob_free(&blob);
165 TALLOC_FREE(notify_buf->changes);
166 notify_buf->num_changes = 0;
169 static void notify_callback(void *private_data, const struct notify_event *e)
171 files_struct *fsp = (files_struct *)private_data;
172 DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp)));
173 notify_fsp(fsp, e->action, e->path);
176 NTSTATUS change_notify_create(struct files_struct *fsp, uint32 filter,
177 bool recursive)
179 char *fullpath;
180 struct notify_entry e;
181 NTSTATUS status;
183 SMB_ASSERT(fsp->notify == NULL);
185 if (!(fsp->notify = TALLOC_ZERO_P(NULL, struct notify_change_buf))) {
186 DEBUG(0, ("talloc failed\n"));
187 return NT_STATUS_NO_MEMORY;
190 /* Do notify operations on the base_name. */
191 if (asprintf(&fullpath, "%s/%s", fsp->conn->connectpath,
192 fsp->fsp_name->base_name) == -1) {
193 DEBUG(0, ("asprintf failed\n"));
194 TALLOC_FREE(fsp->notify);
195 return NT_STATUS_NO_MEMORY;
198 ZERO_STRUCT(e);
199 e.path = fullpath;
200 e.dir_fd = fsp->fh->fd;
201 e.dir_id = fsp->file_id;
202 e.filter = filter;
203 e.subdir_filter = 0;
204 if (recursive) {
205 e.subdir_filter = filter;
208 status = notify_add(fsp->conn->notify_ctx, &e, notify_callback, fsp);
209 SAFE_FREE(fullpath);
211 return status;
214 NTSTATUS change_notify_add_request(struct smb_request *req,
215 uint32 max_param,
216 uint32 filter, bool recursive,
217 struct files_struct *fsp,
218 void (*reply_fn)(struct smb_request *req,
219 NTSTATUS error_code,
220 uint8_t *buf, size_t len))
222 struct notify_change_request *request = NULL;
223 struct notify_mid_map *map = NULL;
224 struct smbd_server_connection *sconn = req->sconn;
226 DEBUG(10, ("change_notify_add_request: Adding request for %s: "
227 "max_param = %d\n", fsp_str_dbg(fsp), (int)max_param));
229 if (!(request = talloc(NULL, struct notify_change_request))
230 || !(map = talloc(request, struct notify_mid_map))) {
231 TALLOC_FREE(request);
232 return NT_STATUS_NO_MEMORY;
235 request->mid_map = map;
236 map->req = request;
238 request->req = talloc_move(request, &req);
239 request->max_param = max_param;
240 request->filter = filter;
241 request->fsp = fsp;
242 request->reply_fn = reply_fn;
243 request->backend_data = NULL;
245 DLIST_ADD_END(fsp->notify->requests, request,
246 struct notify_change_request *);
248 map->mid = request->req->mid;
249 DLIST_ADD(sconn->smb1.notify_mid_maps, map);
251 return NT_STATUS_OK;
254 static void change_notify_remove_request(struct smbd_server_connection *sconn,
255 struct notify_change_request *remove_req)
257 files_struct *fsp;
258 struct notify_change_request *req;
261 * Paranoia checks, the fsp referenced must must have the request in
262 * its list of pending requests
265 fsp = remove_req->fsp;
266 SMB_ASSERT(fsp->notify != NULL);
268 for (req = fsp->notify->requests; req; req = req->next) {
269 if (req == remove_req) {
270 break;
274 if (req == NULL) {
275 smb_panic("notify_req not found in fsp's requests");
278 DLIST_REMOVE(fsp->notify->requests, req);
279 DLIST_REMOVE(sconn->smb1.notify_mid_maps, req->mid_map);
280 TALLOC_FREE(req);
283 /****************************************************************************
284 Delete entries by mid from the change notify pending queue. Always send reply.
285 *****************************************************************************/
287 void remove_pending_change_notify_requests_by_mid(
288 struct smbd_server_connection *sconn, uint64_t mid)
290 struct notify_mid_map *map;
292 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
293 if (map->mid == mid) {
294 break;
298 if (map == NULL) {
299 return;
302 change_notify_reply(map->req->req,
303 NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
304 change_notify_remove_request(sconn, map->req);
307 void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
309 struct smbd_server_connection *sconn = smbreq->sconn;
310 struct notify_mid_map *map;
312 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
313 if (map->req->req == smbreq) {
314 break;
318 if (map == NULL) {
319 return;
322 change_notify_reply(map->req->req,
323 NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
324 change_notify_remove_request(sconn, map->req);
327 /****************************************************************************
328 Delete entries by fnum from the change notify pending queue.
329 *****************************************************************************/
331 void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
332 NTSTATUS status)
334 if (fsp->notify == NULL) {
335 return;
338 while (fsp->notify->requests != NULL) {
339 change_notify_reply(fsp->notify->requests->req,
340 status, 0, NULL,
341 fsp->notify->requests->reply_fn);
342 change_notify_remove_request(fsp->conn->sconn,
343 fsp->notify->requests);
347 void notify_fname(connection_struct *conn, uint32 action, uint32 filter,
348 const char *path)
350 char *fullpath;
351 char *parent;
352 const char *name;
354 if (path[0] == '.' && path[1] == '/') {
355 path += 2;
357 if (parent_dirname(talloc_tos(), path, &parent, &name)) {
358 struct smb_filename smb_fname_parent;
360 ZERO_STRUCT(smb_fname_parent);
361 smb_fname_parent.base_name = parent;
363 if (SMB_VFS_STAT(conn, &smb_fname_parent) != -1) {
364 notify_onelevel(conn->notify_ctx, action, filter,
365 SMB_VFS_FILE_ID_CREATE(conn, &smb_fname_parent.st),
366 name);
370 fullpath = talloc_asprintf(talloc_tos(), "%s/%s", conn->connectpath,
371 path);
372 if (fullpath == NULL) {
373 DEBUG(0, ("asprintf failed\n"));
374 return;
376 notify_trigger(conn->notify_ctx, action, filter, fullpath);
377 TALLOC_FREE(fullpath);
380 static void notify_fsp(files_struct *fsp, uint32 action, const char *name)
382 struct notify_change *change, *changes;
383 char *tmp;
385 if (fsp->notify == NULL) {
387 * Nobody is waiting, don't queue
389 return;
393 * Someone has triggered a notify previously, queue the change for
394 * later.
397 if ((fsp->notify->num_changes > 1000) || (name == NULL)) {
399 * The real number depends on the client buf, just provide a
400 * guard against a DoS here. If name == NULL the CN backend is
401 * alerting us to a problem. Possibly dropped events. Clear
402 * queued changes and send the catch-all response to the client
403 * if a request is pending.
405 TALLOC_FREE(fsp->notify->changes);
406 fsp->notify->num_changes = -1;
407 if (fsp->notify->requests != NULL) {
408 change_notify_reply(fsp->notify->requests->req,
409 NT_STATUS_OK,
410 fsp->notify->requests->max_param,
411 fsp->notify,
412 fsp->notify->requests->reply_fn);
413 change_notify_remove_request(fsp->conn->sconn,
414 fsp->notify->requests);
416 return;
419 /* If we've exceeded the server side queue or received a NULL name
420 * from the underlying CN implementation, don't queue up any more
421 * requests until we can send a catch-all response to the client */
422 if (fsp->notify->num_changes == -1) {
423 return;
426 if (!(changes = TALLOC_REALLOC_ARRAY(
427 fsp->notify, fsp->notify->changes,
428 struct notify_change, fsp->notify->num_changes+1))) {
429 DEBUG(0, ("talloc_realloc failed\n"));
430 return;
433 fsp->notify->changes = changes;
435 change = &(fsp->notify->changes[fsp->notify->num_changes]);
437 if (!(tmp = talloc_strdup(changes, name))) {
438 DEBUG(0, ("talloc_strdup failed\n"));
439 return;
442 string_replace(tmp, '/', '\\');
443 change->name = tmp;
445 change->action = action;
446 fsp->notify->num_changes += 1;
448 if (fsp->notify->requests == NULL) {
450 * Nobody is waiting, so don't send anything. The ot
452 return;
455 if (action == NOTIFY_ACTION_OLD_NAME) {
457 * We have to send the two rename events in one reply. So hold
458 * the first part back.
460 return;
464 * Someone is waiting for the change, trigger the reply immediately.
466 * TODO: do we have to walk the lists of requests pending?
469 change_notify_reply(fsp->notify->requests->req,
470 NT_STATUS_OK,
471 fsp->notify->requests->max_param,
472 fsp->notify,
473 fsp->notify->requests->reply_fn);
475 change_notify_remove_request(fsp->conn->sconn, fsp->notify->requests);
478 char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32 filter)
480 char *result = NULL;
482 result = talloc_strdup(mem_ctx, "");
484 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
485 result = talloc_asprintf_append(result, "FILE_NAME|");
486 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
487 result = talloc_asprintf_append(result, "DIR_NAME|");
488 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
489 result = talloc_asprintf_append(result, "ATTRIBUTES|");
490 if (filter & FILE_NOTIFY_CHANGE_SIZE)
491 result = talloc_asprintf_append(result, "SIZE|");
492 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
493 result = talloc_asprintf_append(result, "LAST_WRITE|");
494 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
495 result = talloc_asprintf_append(result, "LAST_ACCESS|");
496 if (filter & FILE_NOTIFY_CHANGE_CREATION)
497 result = talloc_asprintf_append(result, "CREATION|");
498 if (filter & FILE_NOTIFY_CHANGE_EA)
499 result = talloc_asprintf_append(result, "EA|");
500 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
501 result = talloc_asprintf_append(result, "SECURITY|");
502 if (filter & FILE_NOTIFY_CHANGE_STREAM_NAME)
503 result = talloc_asprintf_append(result, "STREAM_NAME|");
504 if (filter & FILE_NOTIFY_CHANGE_STREAM_SIZE)
505 result = talloc_asprintf_append(result, "STREAM_SIZE|");
506 if (filter & FILE_NOTIFY_CHANGE_STREAM_WRITE)
507 result = talloc_asprintf_append(result, "STREAM_WRITE|");
509 if (result == NULL) return NULL;
510 if (*result == '\0') return result;
512 result[strlen(result)-1] = '\0';
513 return result;
516 struct sys_notify_context *sys_notify_context_create(connection_struct *conn,
517 TALLOC_CTX *mem_ctx,
518 struct event_context *ev)
520 struct sys_notify_context *ctx;
522 if (!(ctx = TALLOC_P(mem_ctx, struct sys_notify_context))) {
523 DEBUG(0, ("talloc failed\n"));
524 return NULL;
527 ctx->ev = ev;
528 ctx->conn = conn;
529 ctx->private_data = NULL;
530 return ctx;
533 NTSTATUS sys_notify_watch(struct sys_notify_context *ctx,
534 struct notify_entry *e,
535 void (*callback)(struct sys_notify_context *ctx,
536 void *private_data,
537 struct notify_event *ev),
538 void *private_data, void *handle)
540 return SMB_VFS_NOTIFY_WATCH(ctx->conn, ctx, e, callback, private_data,
541 handle);