source: branches/samba-3.5.x/source3/smbd/notify.c@ 793

Last change on this file since 793 was 736, checked in by Silvan Scherrer, 13 years ago

Samba Server 3.5: update branche to 3.5.16

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