r21077: A step to minimize the diff later: This pulls in unmodified files from Samba4,
[Samba.git] / source3 / smbd / notify.c
blob4c82b9fca82dde4576ef1e617bbd21de28cfa133
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
25 static struct cnotify_fns *cnotify;
26 static struct notify_mid_map *notify_changes_by_mid;
29 * For NTCancel, we need to find the notify_change_request indexed by
30 * mid. Separate list here.
33 struct notify_mid_map {
34 struct notify_mid_map *prev, *next;
35 struct notify_change_request *req;
36 uint16 mid;
39 static BOOL notify_marshall_changes(int num_changes,
40 struct notify_change *changes,
41 prs_struct *ps)
43 int i;
44 UNISTR uni_name;
46 for (i=0; i<num_changes; i++) {
47 struct notify_change *c = &changes[i];
48 size_t namelen;
49 uint32 u32_tmp; /* Temp arg to prs_uint32 to avoid
50 * signed/unsigned issues */
52 namelen = convert_string_allocate(
53 NULL, CH_UNIX, CH_UTF16LE, c->name, strlen(c->name)+1,
54 &uni_name.buffer, True);
55 if ((namelen == -1) || (uni_name.buffer == NULL)) {
56 goto fail;
59 namelen -= 2; /* Dump NULL termination */
62 * Offset to next entry, only if there is one
65 u32_tmp = (i == num_changes-1) ? 0 : namelen + 12;
66 if (!prs_uint32("offset", ps, 1, &u32_tmp)) goto fail;
68 u32_tmp = c->action;
69 if (!prs_uint32("action", ps, 1, &u32_tmp)) goto fail;
71 u32_tmp = namelen;
72 if (!prs_uint32("namelen", ps, 1, &u32_tmp)) goto fail;
74 if (!prs_unistr("name", ps, 1, &uni_name)) goto fail;
77 * Not NULL terminated, decrease by the 2 UCS2 \0 chars
79 prs_set_offset(ps, prs_offset(ps)-2);
81 SAFE_FREE(uni_name.buffer);
84 return True;
86 fail:
87 SAFE_FREE(uni_name.buffer);
88 return False;
91 /****************************************************************************
92 Setup the common parts of the return packet and send it.
93 *****************************************************************************/
95 static void change_notify_reply_packet(const char *request_buf,
96 NTSTATUS error_code)
98 char outbuf[smb_size+38];
100 memset(outbuf, '\0', sizeof(outbuf));
101 construct_reply_common(request_buf, outbuf);
103 ERROR_NT(error_code);
106 * Seems NT needs a transact command with an error code
107 * in it. This is a longer packet than a simple error.
109 set_message(outbuf,18,0,False);
111 show_msg(outbuf);
112 if (!send_smb(smbd_server_fd(),outbuf))
113 exit_server_cleanly("change_notify_reply_packet: send_smb "
114 "failed.");
117 void change_notify_reply(const char *request_buf, uint32 max_param_count,
118 int num_changes, struct notify_change *changes)
120 char *outbuf = NULL;
121 prs_struct ps;
122 size_t buflen = smb_size+38+max_param_count;
124 if (num_changes == -1) {
125 change_notify_reply_packet(request_buf, NT_STATUS_OK);
126 return;
129 if (!prs_init(&ps, 0, NULL, False)
130 || !notify_marshall_changes(num_changes, changes, &ps)) {
131 change_notify_reply_packet(request_buf, NT_STATUS_NO_MEMORY);
132 goto done;
135 if (prs_offset(&ps) > max_param_count) {
137 * We exceed what the client is willing to accept. Send
138 * nothing.
140 change_notify_reply_packet(request_buf, NT_STATUS_OK);
141 goto done;
144 if (!(outbuf = SMB_MALLOC_ARRAY(char, buflen))) {
145 change_notify_reply_packet(request_buf, NT_STATUS_NO_MEMORY);
146 goto done;
149 construct_reply_common(request_buf, outbuf);
151 if (send_nt_replies(outbuf, buflen, NT_STATUS_OK, prs_data_p(&ps),
152 prs_offset(&ps), NULL, 0) == -1) {
153 exit_server("change_notify_reply_packet: send_smb failed.");
156 done:
157 SAFE_FREE(outbuf);
158 prs_mem_free(&ps);
161 NTSTATUS change_notify_add_request(const char *inbuf, uint32 max_param_count,
162 uint32 filter, struct files_struct *fsp)
164 struct notify_change_request *request = NULL;
165 struct notify_mid_map *map = NULL;
167 if (!(request = SMB_MALLOC_P(struct notify_change_request))
168 || !(map = SMB_MALLOC_P(struct notify_mid_map))) {
169 SAFE_FREE(request);
170 return NT_STATUS_NO_MEMORY;
173 request->mid_map = map;
174 map->req = request;
176 memcpy(request->request_buf, inbuf, sizeof(request->request_buf));
177 request->max_param_count = max_param_count;
178 request->filter = filter;
179 request->fsp = fsp;
181 request->backend_data = cnotify->notify_add(NULL, smbd_event_context(),
182 fsp, &request->filter);
184 DLIST_ADD_END(fsp->notify->requests, request,
185 struct notify_change_request *);
187 map->mid = SVAL(inbuf, smb_mid);
188 DLIST_ADD(notify_changes_by_mid, map);
190 /* Push the MID of this packet on the signing queue. */
191 srv_defer_sign_response(SVAL(inbuf,smb_mid));
193 return NT_STATUS_OK;
196 static void change_notify_remove_request(struct notify_change_request *remove_req)
198 files_struct *fsp;
199 struct notify_change_request *req;
202 * Paranoia checks, the fsp referenced must must have the request in
203 * its list of pending requests
206 fsp = remove_req->fsp;
207 SMB_ASSERT(fsp->notify != NULL);
209 for (req = fsp->notify->requests; req; req = req->next) {
210 if (req == remove_req) {
211 break;
215 if (req == NULL) {
216 smb_panic("notify_req not found in fsp's requests\n");
219 DLIST_REMOVE(fsp->notify->requests, req);
220 DLIST_REMOVE(notify_changes_by_mid, req->mid_map);
221 SAFE_FREE(req->mid_map);
222 TALLOC_FREE(req->backend_data);
223 SAFE_FREE(req);
226 /****************************************************************************
227 Delete entries by mid from the change notify pending queue. Always send reply.
228 *****************************************************************************/
230 void remove_pending_change_notify_requests_by_mid(uint16 mid)
232 struct notify_mid_map *map;
234 for (map = notify_changes_by_mid; map; map = map->next) {
235 if (map->mid == mid) {
236 break;
240 if (map == NULL) {
241 return;
244 change_notify_reply_packet(map->req->request_buf, NT_STATUS_CANCELLED);
245 change_notify_remove_request(map->req);
248 /****************************************************************************
249 Delete entries by fnum from the change notify pending queue.
250 *****************************************************************************/
252 void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
253 NTSTATUS status)
255 if (fsp->notify == NULL) {
256 return;
259 while (fsp->notify->requests != NULL) {
260 change_notify_reply_packet(
261 fsp->notify->requests->request_buf, status);
262 change_notify_remove_request(fsp->notify->requests);
266 /* notify message definition
268 Offset Data length.
269 0 SMB_DEV_T dev 8
270 8 SMB_INO_T inode 8
271 16 uint32 filter 4
272 20 uint32 action 4
273 24.. name
276 #define MSG_NOTIFY_MESSAGE_SIZE 25 /* Includes at least the '\0' terminator */
278 struct notify_message {
279 SMB_DEV_T dev;
280 SMB_INO_T inode;
281 uint32 filter;
282 uint32 action;
283 char *name;
286 static DATA_BLOB notify_message_to_buf(const struct notify_message *msg)
288 DATA_BLOB result;
289 size_t len;
291 len = strlen(msg->name);
293 result = data_blob(NULL, MSG_NOTIFY_MESSAGE_SIZE + len);
294 if (!result.data) {
295 return result;
298 SDEV_T_VAL(result.data, 0, msg->dev);
299 SINO_T_VAL(result.data, 8, msg->inode);
300 SIVAL(result.data, 16, msg->filter);
301 SIVAL(result.data, 20, msg->action);
302 memcpy(result.data+24, msg->name, len+1);
304 return result;
307 static BOOL buf_to_notify_message(void *buf, size_t len,
308 struct notify_message *msg)
310 if (len < MSG_NOTIFY_MESSAGE_SIZE) {
311 DEBUG(0, ("Got invalid notify message of len %d\n",
312 (int)len));
313 return False;
316 msg->dev = DEV_T_VAL(buf, 0);
317 msg->inode = INO_T_VAL(buf, 8);
318 msg->filter = IVAL(buf, 16);
319 msg->action = IVAL(buf, 20);
320 msg->name = ((char *)buf)+24;
321 return True;
324 void notify_action(connection_struct *conn, const char *parent,
325 const char *name, uint32 filter, uint32_t action)
327 struct share_mode_lock *lck;
328 SMB_STRUCT_STAT sbuf;
329 int i;
330 struct notify_message msg;
331 DATA_BLOB blob;
333 struct process_id *pids;
334 int num_pids;
336 DEBUG(10, ("notify_action: parent=%s, name=%s, action=%u\n",
337 parent, name, (unsigned)action));
339 if (SMB_VFS_STAT(conn, parent, &sbuf) != 0) {
341 * Not 100% critical, ignore failure
343 return;
346 if (!(lck = get_share_mode_lock(NULL, sbuf.st_dev, sbuf.st_ino,
347 NULL, NULL))) {
348 return;
351 msg.dev = sbuf.st_dev;
352 msg.inode = sbuf.st_ino;
353 msg.filter = filter;
354 msg.action = action;
355 msg.name = CONST_DISCARD(char *, name);
357 blob = notify_message_to_buf(&msg);
358 if (blob.data == NULL) {
359 DEBUG(0, ("notify_message_to_buf failed\n"));
360 return;
363 pids = NULL;
364 num_pids = 0;
366 become_root_uid_only();
368 for (i=0; i<lck->num_share_modes; i++) {
369 struct share_mode_entry *e = &lck->share_modes[i];
370 int j;
371 struct process_id *tmp;
373 for (j=0; j<num_pids; j++) {
374 if (procid_equal(&e->pid, &pids[j])) {
375 break;
379 if (j < num_pids) {
381 * Already sent to that process, skip it
383 continue;
386 message_send_pid(lck->share_modes[i].pid, MSG_SMB_NOTIFY,
387 blob.data, blob.length, True);
389 if (!(tmp = TALLOC_REALLOC_ARRAY(lck, pids, struct process_id,
390 num_pids+1))) {
391 DEBUG(0, ("realloc failed\n"));
392 break;
394 pids = tmp;
395 pids[num_pids] = e->pid;
396 num_pids += 1;
399 unbecome_root_uid_only();
401 data_blob_free(&blob);
402 TALLOC_FREE(lck);
405 void notify_fname(connection_struct *conn, const char *path,
406 uint32 filter, uint32 action)
408 char *parent;
409 const char *name;
411 if (!parent_dirname_talloc(tmp_talloc_ctx(), path, &parent, &name)) {
412 return;
415 notify_action(conn, parent, name, filter, action);
416 TALLOC_FREE(parent);
419 void notify_fsp(files_struct *fsp, uint32 action, char *name)
421 struct notify_change *change, *changes;
423 if (fsp->notify == NULL) {
425 * Nobody is waiting, don't queue
427 return;
430 if (fsp->notify->requests != NULL) {
432 * Someone is waiting for the change, trigger the reply
433 * immediately.
435 * TODO: do we have to walk the lists of requests pending?
438 struct notify_change_request *req = fsp->notify->requests;
439 struct notify_change onechange;
441 if (name == NULL) {
443 * Catch-all change, possibly from notify_hash.c
445 change_notify_reply(req->request_buf,
446 req->max_param_count,
447 -1, NULL);
448 return;
451 onechange.action = action;
452 onechange.name = name;
454 change_notify_reply(req->request_buf, req->max_param_count,
455 1, &onechange);
456 change_notify_remove_request(req);
457 return;
461 * Someone has triggered a notify previously, queue the change for
462 * later. TODO: Limit the number of changes queued, test how filters
463 * apply here. Do we have to store them?
466 if ((fsp->notify->num_changes > 30) || (name == NULL)) {
468 * W2k3 seems to store at most 30 changes.
470 TALLOC_FREE(fsp->notify->changes);
471 fsp->notify->num_changes = -1;
472 return;
475 if (fsp->notify->num_changes == -1) {
476 return;
479 if (!(changes = TALLOC_REALLOC_ARRAY(
480 fsp->notify, fsp->notify->changes,
481 struct notify_change, fsp->notify->num_changes+1))) {
482 DEBUG(0, ("talloc_realloc failed\n"));
483 return;
486 fsp->notify->changes = changes;
488 change = &(fsp->notify->changes[fsp->notify->num_changes]);
490 if (!(change->name = talloc_strdup(changes, name))) {
491 DEBUG(0, ("talloc_strdup failed\n"));
492 return;
494 change->action = action;
495 fsp->notify->num_changes += 1;
497 return;
500 static void notify_message_callback(int msgtype, struct process_id pid,
501 void *buf, size_t len,
502 void *private_data)
504 struct notify_message msg;
505 files_struct *fsp;
507 if (!buf_to_notify_message(buf, len, &msg)) {
508 return;
511 DEBUG(10, ("Received notify_message for 0x%x/%.0f: %d\n",
512 (unsigned)msg.dev, (double)msg.inode, msg.action));
514 for(fsp = fsp_find_di_first(msg.dev, msg.inode); fsp;
515 fsp = fsp_find_di_next(fsp)) {
516 if ((fsp->notify != NULL)
517 && (fsp->notify->requests != NULL)
518 && (fsp->notify->requests->filter & msg.filter)) {
519 notify_fsp(fsp, msg.action, msg.name);
524 /****************************************************************************
525 Initialise the change notify subsystem.
526 ****************************************************************************/
528 BOOL init_change_notify(void)
530 cnotify = NULL;
532 #if HAVE_KERNEL_CHANGE_NOTIFY
533 if (cnotify == NULL && lp_kernel_change_notify())
534 cnotify = kernel_notify_init(smbd_event_context());
535 #endif
536 #if HAVE_FAM_CHANGE_NOTIFY
537 if (cnotify == NULL && lp_fam_change_notify())
538 cnotify = fam_notify_init(smbd_event_context());
539 #endif
540 if (!cnotify) cnotify = hash_notify_init();
542 if (!cnotify) {
543 DEBUG(0,("Failed to init change notify system\n"));
544 return False;
547 message_register(MSG_SMB_NOTIFY, notify_message_callback, NULL);
549 return True;