source: trunk/samba-3.0.25pre1/source/smbd/notify.c@ 7

Last change on this file since 7 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 12.4 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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23#include "includes.h"
24
25struct notify_change_request {
26 struct notify_change_request *prev, *next;
27 struct files_struct *fsp; /* backpointer for cancel by mid */
28 char request_buf[smb_size];
29 uint32 filter;
30 uint32 max_param_count;
31 uint32 current_bufsize;
32 struct notify_mid_map *mid_map;
33 void *backend_data;
34};
35
36static void notify_fsp(files_struct *fsp, uint32 action, const char *name);
37
38static struct notify_mid_map *notify_changes_by_mid;
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_marshall_changes(int num_changes,
52 struct notify_change *changes,
53 prs_struct *ps)
54{
55 int i;
56 UNISTR uni_name;
57
58 for (i=0; i<num_changes; i++) {
59 struct notify_change *c = &changes[i];
60 size_t namelen;
61 uint32 u32_tmp; /* Temp arg to prs_uint32 to avoid
62 * signed/unsigned issues */
63
64 namelen = convert_string_allocate(
65 NULL, CH_UNIX, CH_UTF16LE, c->name, strlen(c->name)+1,
66 &uni_name.buffer, True);
67 if ((namelen == -1) || (uni_name.buffer == NULL)) {
68 goto fail;
69 }
70
71 namelen -= 2; /* Dump NULL termination */
72
73 /*
74 * Offset to next entry, only if there is one
75 */
76
77 u32_tmp = (i == num_changes-1) ? 0 : namelen + 12;
78 if (!prs_uint32("offset", ps, 1, &u32_tmp)) goto fail;
79
80 u32_tmp = c->action;
81 if (!prs_uint32("action", ps, 1, &u32_tmp)) goto fail;
82
83 u32_tmp = namelen;
84 if (!prs_uint32("namelen", ps, 1, &u32_tmp)) goto fail;
85
86 if (!prs_unistr("name", ps, 1, &uni_name)) goto fail;
87
88 /*
89 * Not NULL terminated, decrease by the 2 UCS2 \0 chars
90 */
91 prs_set_offset(ps, prs_offset(ps)-2);
92
93 SAFE_FREE(uni_name.buffer);
94 }
95
96 return True;
97
98 fail:
99 SAFE_FREE(uni_name.buffer);
100 return False;
101}
102
103/****************************************************************************
104 Setup the common parts of the return packet and send it.
105*****************************************************************************/
106
107static void change_notify_reply_packet(const char *request_buf,
108 NTSTATUS error_code)
109{
110 char outbuf[smb_size+38];
111
112 memset(outbuf, '\0', sizeof(outbuf));
113 construct_reply_common(request_buf, outbuf);
114
115 ERROR_NT(error_code);
116
117 /*
118 * Seems NT needs a transact command with an error code
119 * in it. This is a longer packet than a simple error.
120 */
121 set_message(outbuf,18,0,False);
122
123 show_msg(outbuf);
124 if (!send_smb(smbd_server_fd(),outbuf))
125 exit_server_cleanly("change_notify_reply_packet: send_smb "
126 "failed.");
127}
128
129void change_notify_reply(const char *request_buf, uint32 max_param_count,
130 struct notify_change_buf *notify_buf)
131{
132 char *outbuf = NULL;
133 prs_struct ps;
134 size_t buflen = smb_size+38+max_param_count;
135
136 if (notify_buf->num_changes == -1) {
137 change_notify_reply_packet(request_buf, NT_STATUS_OK);
138 return;
139 }
140
141 if (!prs_init(&ps, 0, NULL, False)
142 || !notify_marshall_changes(notify_buf->num_changes,
143 notify_buf->changes, &ps)) {
144 change_notify_reply_packet(request_buf, NT_STATUS_NO_MEMORY);
145 goto done;
146 }
147
148 if (prs_offset(&ps) > max_param_count) {
149 /*
150 * We exceed what the client is willing to accept. Send
151 * nothing.
152 */
153 change_notify_reply_packet(request_buf, NT_STATUS_OK);
154 goto done;
155 }
156
157 if (!(outbuf = SMB_MALLOC_ARRAY(char, buflen))) {
158 change_notify_reply_packet(request_buf, NT_STATUS_NO_MEMORY);
159 goto done;
160 }
161
162 construct_reply_common(request_buf, outbuf);
163
164 if (send_nt_replies(outbuf, buflen, NT_STATUS_OK, prs_data_p(&ps),
165 prs_offset(&ps), NULL, 0) == -1) {
166 exit_server("change_notify_reply_packet: send_smb failed.");
167 }
168
169 done:
170 SAFE_FREE(outbuf);
171 prs_mem_free(&ps);
172
173 TALLOC_FREE(notify_buf->changes);
174 notify_buf->num_changes = 0;
175}
176
177static void notify_callback(void *private_data, const struct notify_event *e)
178{
179 files_struct *fsp = (files_struct *)private_data;
180 DEBUG(10, ("notify_callback called for %s\n", fsp->fsp_name));
181 notify_fsp(fsp, e->action, e->path);
182}
183
184NTSTATUS change_notify_create(struct files_struct *fsp, uint32 filter,
185 BOOL recursive)
186{
187 char *fullpath;
188 struct notify_entry e;
189 NTSTATUS status;
190
191 SMB_ASSERT(fsp->notify == NULL);
192
193 if (!(fsp->notify = TALLOC_ZERO_P(NULL, struct notify_change_buf))) {
194 DEBUG(0, ("talloc failed\n"));
195 return NT_STATUS_NO_MEMORY;
196 }
197
198 if (asprintf(&fullpath, "%s/%s", fsp->conn->connectpath,
199 fsp->fsp_name) == -1) {
200 DEBUG(0, ("asprintf failed\n"));
201 return NT_STATUS_NO_MEMORY;
202 }
203
204 e.path = fullpath;
205 e.filter = filter;
206 e.subdir_filter = 0;
207 if (recursive) {
208 e.subdir_filter = filter;
209 }
210
211 status = notify_add(fsp->conn->notify_ctx, &e, notify_callback, fsp);
212 SAFE_FREE(fullpath);
213
214 return status;
215}
216
217NTSTATUS change_notify_add_request(const char *inbuf, uint32 max_param_count,
218 uint32 filter, BOOL recursive,
219 struct files_struct *fsp)
220{
221 struct notify_change_request *request = NULL;
222 struct notify_mid_map *map = NULL;
223
224 if (!(request = SMB_MALLOC_P(struct notify_change_request))
225 || !(map = SMB_MALLOC_P(struct notify_mid_map))) {
226 SAFE_FREE(request);
227 return NT_STATUS_NO_MEMORY;
228 }
229
230 request->mid_map = map;
231 map->req = request;
232
233 memcpy(request->request_buf, inbuf, sizeof(request->request_buf));
234 request->max_param_count = max_param_count;
235 request->current_bufsize = 0;
236 request->filter = filter;
237 request->fsp = fsp;
238 request->backend_data = NULL;
239
240 DLIST_ADD_END(fsp->notify->requests, request,
241 struct notify_change_request *);
242
243 map->mid = SVAL(inbuf, smb_mid);
244 DLIST_ADD(notify_changes_by_mid, map);
245
246 /* Push the MID of this packet on the signing queue. */
247 srv_defer_sign_response(SVAL(inbuf,smb_mid));
248
249 return NT_STATUS_OK;
250}
251
252static void change_notify_remove_request(struct notify_change_request *remove_req)
253{
254 files_struct *fsp;
255 struct notify_change_request *req;
256
257 /*
258 * Paranoia checks, the fsp referenced must must have the request in
259 * its list of pending requests
260 */
261
262 fsp = remove_req->fsp;
263 SMB_ASSERT(fsp->notify != NULL);
264
265 for (req = fsp->notify->requests; req; req = req->next) {
266 if (req == remove_req) {
267 break;
268 }
269 }
270
271 if (req == NULL) {
272 smb_panic("notify_req not found in fsp's requests\n");
273 }
274
275 DLIST_REMOVE(fsp->notify->requests, req);
276 DLIST_REMOVE(notify_changes_by_mid, req->mid_map);
277 SAFE_FREE(req->mid_map);
278 TALLOC_FREE(req->backend_data);
279 SAFE_FREE(req);
280}
281
282/****************************************************************************
283 Delete entries by mid from the change notify pending queue. Always send reply.
284*****************************************************************************/
285
286void remove_pending_change_notify_requests_by_mid(uint16 mid)
287{
288 struct notify_mid_map *map;
289
290 for (map = notify_changes_by_mid; map; map = map->next) {
291 if (map->mid == mid) {
292 break;
293 }
294 }
295
296 if (map == NULL) {
297 return;
298 }
299
300 change_notify_reply_packet(map->req->request_buf, NT_STATUS_CANCELLED);
301 change_notify_remove_request(map->req);
302}
303
304/****************************************************************************
305 Delete entries by fnum from the change notify pending queue.
306*****************************************************************************/
307
308void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
309 NTSTATUS status)
310{
311 if (fsp->notify == NULL) {
312 return;
313 }
314
315 while (fsp->notify->requests != NULL) {
316 change_notify_reply_packet(
317 fsp->notify->requests->request_buf, status);
318 change_notify_remove_request(fsp->notify->requests);
319 }
320}
321
322void notify_fname(connection_struct *conn, uint32 action, uint32 filter,
323 const char *path)
324{
325 char *fullpath;
326
327 if (asprintf(&fullpath, "%s/%s", conn->connectpath, path) == -1) {
328 DEBUG(0, ("asprintf failed\n"));
329 return;
330 }
331
332 notify_trigger(conn->notify_ctx, action, filter, fullpath);
333 SAFE_FREE(fullpath);
334}
335
336static void notify_fsp(files_struct *fsp, uint32 action, const char *name)
337{
338 struct notify_change *change, *changes;
339 char *name2;
340
341 if (fsp->notify == NULL) {
342 /*
343 * Nobody is waiting, don't queue
344 */
345 return;
346 }
347
348 if (!(name2 = talloc_strdup(fsp->notify, name))) {
349 DEBUG(0, ("talloc_strdup failed\n"));
350 return;
351 }
352
353 string_replace(name2, '/', '\\');
354
355 /*
356 * Someone has triggered a notify previously, queue the change for
357 * later.
358 */
359
360 if ((fsp->notify->num_changes > 1000) || (name == NULL)) {
361 /*
362 * The real number depends on the client buf, just provide a
363 * guard against a DoS here.
364 */
365 TALLOC_FREE(fsp->notify->changes);
366 TALLOC_FREE(name2);
367 fsp->notify->num_changes = -1;
368 return;
369 }
370
371 if (fsp->notify->num_changes == -1) {
372 return;
373 }
374
375 if (!(changes = TALLOC_REALLOC_ARRAY(
376 fsp->notify, fsp->notify->changes,
377 struct notify_change, fsp->notify->num_changes+1))) {
378 DEBUG(0, ("talloc_realloc failed\n"));
379 TALLOC_FREE(name2);
380 return;
381 }
382
383 fsp->notify->changes = changes;
384
385 change = &(fsp->notify->changes[fsp->notify->num_changes]);
386
387 change->name = talloc_move(changes, &name2);
388 change->action = action;
389 fsp->notify->num_changes += 1;
390
391 if (fsp->notify->requests == NULL) {
392 /*
393 * Nobody is waiting, so don't send anything. The ot
394 */
395 return;
396 }
397
398 if (action == NOTIFY_ACTION_OLD_NAME) {
399 /*
400 * We have to send the two rename events in one reply. So hold
401 * the first part back.
402 */
403 return;
404 }
405
406 /*
407 * Someone is waiting for the change, trigger the reply immediately.
408 *
409 * TODO: do we have to walk the lists of requests pending?
410 */
411
412 change_notify_reply(fsp->notify->requests->request_buf,
413 fsp->notify->requests->max_param_count,
414 fsp->notify);
415
416 change_notify_remove_request(fsp->notify->requests);
417}
418
419char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32 filter)
420{
421 char *result = NULL;
422
423 result = talloc_strdup(mem_ctx, "");
424
425 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
426 result = talloc_asprintf_append(result, "FILE_NAME|");
427 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
428 result = talloc_asprintf_append(result, "DIR_NAME|");
429 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
430 result = talloc_asprintf_append(result, "ATTRIBUTES|");
431 if (filter & FILE_NOTIFY_CHANGE_SIZE)
432 result = talloc_asprintf_append(result, "SIZE|");
433 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
434 result = talloc_asprintf_append(result, "LAST_WRITE|");
435 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
436 result = talloc_asprintf_append(result, "LAST_ACCESS|");
437 if (filter & FILE_NOTIFY_CHANGE_CREATION)
438 result = talloc_asprintf_append(result, "CREATION|");
439 if (filter & FILE_NOTIFY_CHANGE_EA)
440 result = talloc_asprintf_append(result, "EA|");
441 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
442 result = talloc_asprintf_append(result, "SECURITY|");
443 if (filter & FILE_NOTIFY_CHANGE_STREAM_NAME)
444 result = talloc_asprintf_append(result, "STREAM_NAME|");
445 if (filter & FILE_NOTIFY_CHANGE_STREAM_SIZE)
446 result = talloc_asprintf_append(result, "STREAM_SIZE|");
447 if (filter & FILE_NOTIFY_CHANGE_STREAM_WRITE)
448 result = talloc_asprintf_append(result, "STREAM_WRITE|");
449
450 if (result == NULL) return NULL;
451 if (*result == '\0') return result;
452
453 result[strlen(result)-1] = '\0';
454 return result;
455}
456
457struct sys_notify_context *sys_notify_context_create(connection_struct *conn,
458 TALLOC_CTX *mem_ctx,
459 struct event_context *ev)
460{
461 struct sys_notify_context *ctx;
462
463 if (!(ctx = TALLOC_P(mem_ctx, struct sys_notify_context))) {
464 DEBUG(0, ("talloc failed\n"));
465 return NULL;
466 }
467
468 ctx->ev = ev;
469 ctx->conn = conn;
470 ctx->private_data = NULL;
471 return ctx;
472}
473
474NTSTATUS sys_notify_watch(struct sys_notify_context *ctx,
475 struct notify_entry *e,
476 void (*callback)(struct sys_notify_context *ctx,
477 void *private_data,
478 struct notify_event *ev),
479 void *private_data, void *handle)
480{
481 return SMB_VFS_NOTIFY_WATCH(ctx->conn, ctx, e, callback, private_data,
482 handle);
483}
484
Note: See TracBrowser for help on using the repository browser.