source: vendor/3.6.0/source3/smbd/notify.c

Last change on this file was 740, checked in by Silvan Scherrer, 13 years ago

Samba Server: update vendor to 3.6.0

File size: 14.6 KB
Line 
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
7
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.
12
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.
17
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/>.
20*/
21
22#include "includes.h"
23#include "smbd/smbd.h"
24#include "smbd/globals.h"
25#include "../librpc/gen_ndr/ndr_notify.h"
26
27struct 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;
38};
39
40static void notify_fsp(files_struct *fsp, uint32 action, const char *name);
41
42/*
43 * For NTCancel, we need to find the notify_change_request indexed by
44 * mid. Separate list here.
45 */
46
47struct notify_mid_map {
48 struct notify_mid_map *prev, *next;
49 struct notify_change_request *req;
50 uint64_t mid;
51};
52
53static bool notify_change_record_identical(struct notify_change *c1,
54 struct notify_change *c2)
55{
56 /* Note this is deliberately case sensitive. */
57 if (c1->action == c2->action &&
58 strcmp(c1->name, c2->name) == 0) {
59 return True;
60 }
61 return False;
62}
63
64static bool notify_marshall_changes(int num_changes,
65 uint32 max_offset,
66 struct notify_change *changes,
67 DATA_BLOB *final_blob)
68{
69 int i;
70
71 if (num_changes == -1) {
72 return false;
73 }
74
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;
80
81 /* Coalesce any identical records. */
82 while (i+1 < num_changes &&
83 notify_change_record_identical(&changes[i],
84 &changes[i+1])) {
85 i++;
86 }
87
88 c = &changes[i];
89
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);
94
95 /*
96 * Offset to next entry, only if there is one
97 */
98
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;
103 }
104
105 if (DEBUGLEVEL >= 10) {
106 NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION, &m);
107 }
108
109 if (!data_blob_append(talloc_tos(), final_blob,
110 blob.data, blob.length)) {
111 data_blob_free(&blob);
112 return false;
113 }
114
115 data_blob_free(&blob);
116
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;
123 }
124 }
125
126 return True;
127}
128
129/****************************************************************************
130 Setup the common parts of the return packet and send it.
131*****************************************************************************/
132
133void 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))
140{
141 DATA_BLOB blob = data_blob_null;
142
143 if (!NT_STATUS_IS_OK(error_code)) {
144 reply_fn(req, error_code, NULL, 0);
145 return;
146 }
147
148 if (max_param == 0 || notify_buf == NULL) {
149 reply_fn(req, NT_STATUS_OK, NULL, 0);
150 return;
151 }
152
153 if (!notify_marshall_changes(notify_buf->num_changes, max_param,
154 notify_buf->changes, &blob)) {
155 /*
156 * We exceed what the client is willing to accept. Send
157 * nothing.
158 */
159 data_blob_free(&blob);
160 }
161
162 reply_fn(req, NT_STATUS_OK, blob.data, blob.length);
163
164 data_blob_free(&blob);
165
166 TALLOC_FREE(notify_buf->changes);
167 notify_buf->num_changes = 0;
168}
169
170static void notify_callback(void *private_data, const struct notify_event *e)
171{
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);
175}
176
177NTSTATUS change_notify_create(struct files_struct *fsp, uint32 filter,
178 bool recursive)
179{
180 char *fullpath;
181 struct notify_entry e;
182 NTSTATUS status;
183
184 SMB_ASSERT(fsp->notify == NULL);
185
186 if (!(fsp->notify = TALLOC_ZERO_P(NULL, struct notify_change_buf))) {
187 DEBUG(0, ("talloc failed\n"));
188 return NT_STATUS_NO_MEMORY;
189 }
190
191 /* Do notify operations on the base_name. */
192 if (asprintf(&fullpath, "%s/%s", fsp->conn->connectpath,
193 fsp->fsp_name->base_name) == -1) {
194 DEBUG(0, ("asprintf failed\n"));
195 TALLOC_FREE(fsp->notify);
196 return NT_STATUS_NO_MEMORY;
197 }
198
199 ZERO_STRUCT(e);
200 e.path = fullpath;
201 e.dir_fd = fsp->fh->fd;
202 e.dir_id = fsp->file_id;
203 e.filter = filter;
204 e.subdir_filter = 0;
205 if (recursive) {
206 e.subdir_filter = filter;
207 }
208
209 status = notify_add(fsp->conn->notify_ctx, &e, notify_callback, fsp);
210 SAFE_FREE(fullpath);
211
212 return status;
213}
214
215NTSTATUS change_notify_add_request(struct smb_request *req,
216 uint32 max_param,
217 uint32 filter, bool recursive,
218 struct files_struct *fsp,
219 void (*reply_fn)(struct smb_request *req,
220 NTSTATUS error_code,
221 uint8_t *buf, size_t len))
222{
223 struct notify_change_request *request = NULL;
224 struct notify_mid_map *map = NULL;
225 struct smbd_server_connection *sconn = req->sconn;
226
227 DEBUG(10, ("change_notify_add_request: Adding request for %s: "
228 "max_param = %d\n", fsp_str_dbg(fsp), (int)max_param));
229
230 if (!(request = talloc(NULL, struct notify_change_request))
231 || !(map = talloc(request, struct notify_mid_map))) {
232 TALLOC_FREE(request);
233 return NT_STATUS_NO_MEMORY;
234 }
235
236 request->mid_map = map;
237 map->req = request;
238
239 request->req = talloc_move(request, &req);
240 request->max_param = max_param;
241 request->filter = filter;
242 request->fsp = fsp;
243 request->reply_fn = reply_fn;
244 request->backend_data = NULL;
245
246 DLIST_ADD_END(fsp->notify->requests, request,
247 struct notify_change_request *);
248
249 map->mid = request->req->mid;
250 DLIST_ADD(sconn->smb1.notify_mid_maps, map);
251
252 return NT_STATUS_OK;
253}
254
255static void change_notify_remove_request(struct smbd_server_connection *sconn,
256 struct notify_change_request *remove_req)
257{
258 files_struct *fsp;
259 struct notify_change_request *req;
260
261 /*
262 * Paranoia checks, the fsp referenced must must have the request in
263 * its list of pending requests
264 */
265
266 fsp = remove_req->fsp;
267 SMB_ASSERT(fsp->notify != NULL);
268
269 for (req = fsp->notify->requests; req; req = req->next) {
270 if (req == remove_req) {
271 break;
272 }
273 }
274
275 if (req == NULL) {
276 smb_panic("notify_req not found in fsp's requests");
277 }
278
279 DLIST_REMOVE(fsp->notify->requests, req);
280 DLIST_REMOVE(sconn->smb1.notify_mid_maps, req->mid_map);
281 TALLOC_FREE(req);
282}
283
284/****************************************************************************
285 Delete entries by mid from the change notify pending queue. Always send reply.
286*****************************************************************************/
287
288void remove_pending_change_notify_requests_by_mid(
289 struct smbd_server_connection *sconn, uint64_t mid)
290{
291 struct notify_mid_map *map;
292
293 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
294 if (map->mid == mid) {
295 break;
296 }
297 }
298
299 if (map == NULL) {
300 return;
301 }
302
303 change_notify_reply(map->req->req,
304 NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
305 change_notify_remove_request(sconn, map->req);
306}
307
308void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
309{
310 struct smbd_server_connection *sconn = smbreq->sconn;
311 struct notify_mid_map *map;
312
313 for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
314 if (map->req->req == smbreq) {
315 break;
316 }
317 }
318
319 if (map == NULL) {
320 return;
321 }
322
323 change_notify_reply(map->req->req,
324 NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
325 change_notify_remove_request(sconn, map->req);
326}
327
328/****************************************************************************
329 Delete entries by fnum from the change notify pending queue.
330*****************************************************************************/
331
332void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
333 NTSTATUS status)
334{
335 if (fsp->notify == NULL) {
336 return;
337 }
338
339 while (fsp->notify->requests != NULL) {
340 change_notify_reply(fsp->notify->requests->req,
341 status, 0, NULL,
342 fsp->notify->requests->reply_fn);
343 change_notify_remove_request(fsp->conn->sconn,
344 fsp->notify->requests);
345 }
346}
347
348void notify_fname(connection_struct *conn, uint32 action, uint32 filter,
349 const char *path)
350{
351 char *fullpath;
352 char *parent;
353 const char *name;
354
355 if (path[0] == '.' && path[1] == '/') {
356 path += 2;
357 }
358 if (parent_dirname(talloc_tos(), path, &parent, &name)) {
359 struct smb_filename smb_fname_parent;
360
361 ZERO_STRUCT(smb_fname_parent);
362 smb_fname_parent.base_name = parent;
363
364 if (SMB_VFS_STAT(conn, &smb_fname_parent) != -1) {
365 notify_onelevel(conn->notify_ctx, action, filter,
366 SMB_VFS_FILE_ID_CREATE(conn, &smb_fname_parent.st),
367 name);
368 }
369 }
370
371 fullpath = talloc_asprintf(talloc_tos(), "%s/%s", conn->connectpath,
372 path);
373 if (fullpath == NULL) {
374 DEBUG(0, ("asprintf failed\n"));
375 return;
376 }
377 notify_trigger(conn->notify_ctx, action, filter, fullpath);
378 TALLOC_FREE(fullpath);
379}
380
381static void notify_fsp(files_struct *fsp, uint32 action, const char *name)
382{
383 struct notify_change *change, *changes;
384 char *tmp;
385
386 if (fsp->notify == NULL) {
387 /*
388 * Nobody is waiting, don't queue
389 */
390 return;
391 }
392
393 /*
394 * Someone has triggered a notify previously, queue the change for
395 * later.
396 */
397
398 if ((fsp->notify->num_changes > 1000) || (name == NULL)) {
399 /*
400 * The real number depends on the client buf, just provide a
401 * guard against a DoS here. If name == NULL the CN backend is
402 * alerting us to a problem. Possibly dropped events. Clear
403 * queued changes and send the catch-all response to the client
404 * if a request is pending.
405 */
406 TALLOC_FREE(fsp->notify->changes);
407 fsp->notify->num_changes = -1;
408 if (fsp->notify->requests != NULL) {
409 change_notify_reply(fsp->notify->requests->req,
410 NT_STATUS_OK,
411 fsp->notify->requests->max_param,
412 fsp->notify,
413 fsp->notify->requests->reply_fn);
414 change_notify_remove_request(fsp->conn->sconn,
415 fsp->notify->requests);
416 }
417 return;
418 }
419
420 /* If we've exceeded the server side queue or received a NULL name
421 * from the underlying CN implementation, don't queue up any more
422 * requests until we can send a catch-all response to the client */
423 if (fsp->notify->num_changes == -1) {
424 return;
425 }
426
427 if (!(changes = TALLOC_REALLOC_ARRAY(
428 fsp->notify, fsp->notify->changes,
429 struct notify_change, fsp->notify->num_changes+1))) {
430 DEBUG(0, ("talloc_realloc failed\n"));
431 return;
432 }
433
434 fsp->notify->changes = changes;
435
436 change = &(fsp->notify->changes[fsp->notify->num_changes]);
437
438 if (!(tmp = talloc_strdup(changes, name))) {
439 DEBUG(0, ("talloc_strdup failed\n"));
440 return;
441 }
442
443 string_replace(tmp, '/', '\\');
444 change->name = tmp;
445
446 change->action = action;
447 fsp->notify->num_changes += 1;
448
449 if (fsp->notify->requests == NULL) {
450 /*
451 * Nobody is waiting, so don't send anything. The ot
452 */
453 return;
454 }
455
456 if (action == NOTIFY_ACTION_OLD_NAME) {
457 /*
458 * We have to send the two rename events in one reply. So hold
459 * the first part back.
460 */
461 return;
462 }
463
464 /*
465 * Someone is waiting for the change, trigger the reply immediately.
466 *
467 * TODO: do we have to walk the lists of requests pending?
468 */
469
470 change_notify_reply(fsp->notify->requests->req,
471 NT_STATUS_OK,
472 fsp->notify->requests->max_param,
473 fsp->notify,
474 fsp->notify->requests->reply_fn);
475
476 change_notify_remove_request(fsp->conn->sconn, fsp->notify->requests);
477}
478
479char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32 filter)
480{
481 char *result = NULL;
482
483 result = talloc_strdup(mem_ctx, "");
484
485 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
486 result = talloc_asprintf_append(result, "FILE_NAME|");
487 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
488 result = talloc_asprintf_append(result, "DIR_NAME|");
489 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
490 result = talloc_asprintf_append(result, "ATTRIBUTES|");
491 if (filter & FILE_NOTIFY_CHANGE_SIZE)
492 result = talloc_asprintf_append(result, "SIZE|");
493 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
494 result = talloc_asprintf_append(result, "LAST_WRITE|");
495 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
496 result = talloc_asprintf_append(result, "LAST_ACCESS|");
497 if (filter & FILE_NOTIFY_CHANGE_CREATION)
498 result = talloc_asprintf_append(result, "CREATION|");
499 if (filter & FILE_NOTIFY_CHANGE_EA)
500 result = talloc_asprintf_append(result, "EA|");
501 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
502 result = talloc_asprintf_append(result, "SECURITY|");
503 if (filter & FILE_NOTIFY_CHANGE_STREAM_NAME)
504 result = talloc_asprintf_append(result, "STREAM_NAME|");
505 if (filter & FILE_NOTIFY_CHANGE_STREAM_SIZE)
506 result = talloc_asprintf_append(result, "STREAM_SIZE|");
507 if (filter & FILE_NOTIFY_CHANGE_STREAM_WRITE)
508 result = talloc_asprintf_append(result, "STREAM_WRITE|");
509
510 if (result == NULL) return NULL;
511 if (*result == '\0') return result;
512
513 result[strlen(result)-1] = '\0';
514 return result;
515}
516
517struct sys_notify_context *sys_notify_context_create(connection_struct *conn,
518 TALLOC_CTX *mem_ctx,
519 struct event_context *ev)
520{
521 struct sys_notify_context *ctx;
522
523 if (!(ctx = TALLOC_P(mem_ctx, struct sys_notify_context))) {
524 DEBUG(0, ("talloc failed\n"));
525 return NULL;
526 }
527
528 ctx->ev = ev;
529 ctx->conn = conn;
530 ctx->private_data = NULL;
531 return ctx;
532}
533
534NTSTATUS sys_notify_watch(struct sys_notify_context *ctx,
535 struct notify_entry *e,
536 void (*callback)(struct sys_notify_context *ctx,
537 void *private_data,
538 struct notify_event *ev),
539 void *private_data, void *handle)
540{
541 return SMB_VFS_NOTIFY_WATCH(ctx->conn, ctx, e, callback, private_data,
542 handle);
543}
544
Note: See TracBrowser for help on using the repository browser.