2 Unix SMB/CIFS implementation.
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.
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;
39 static BOOL notify_marshall_changes(int num_changes,
40 struct notify_change *changes,
46 for (i=0; i<num_changes; i++) {
47 struct notify_change *c = &changes[i];
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)) {
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;
69 if (!prs_uint32("action", ps, 1, &u32_tmp)) goto fail;
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);
87 SAFE_FREE(uni_name.buffer);
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,
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);
112 if (!send_smb(smbd_server_fd(),outbuf))
113 exit_server_cleanly("change_notify_reply_packet: send_smb "
117 void change_notify_reply(const char *request_buf, uint32 max_param_count,
118 int num_changes, struct notify_change *changes)
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);
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);
135 if (prs_offset(&ps) > max_param_count) {
137 * We exceed what the client is willing to accept. Send
140 change_notify_reply_packet(request_buf, NT_STATUS_OK);
144 if (!(outbuf = SMB_MALLOC_ARRAY(char, buflen))) {
145 change_notify_reply_packet(request_buf, NT_STATUS_NO_MEMORY);
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.");
161 NTSTATUS change_notify_add_request(const char *inbuf, uint32 max_param_count,
162 uint32 filter, BOOL recursive,
163 struct files_struct *fsp)
165 struct notify_change_request *request = NULL;
166 struct notify_mid_map *map = NULL;
168 if (!(request = SMB_MALLOC_P(struct notify_change_request))
169 || !(map = SMB_MALLOC_P(struct notify_mid_map))) {
171 return NT_STATUS_NO_MEMORY;
174 request->mid_map = map;
177 memcpy(request->request_buf, inbuf, sizeof(request->request_buf));
178 request->max_param_count = max_param_count;
179 request->filter = filter;
181 request->backend_data = NULL;
183 DLIST_ADD_END(fsp->notify->requests, request,
184 struct notify_change_request *);
186 map->mid = SVAL(inbuf, smb_mid);
187 DLIST_ADD(notify_changes_by_mid, map);
189 /* Push the MID of this packet on the signing queue. */
190 srv_defer_sign_response(SVAL(inbuf,smb_mid));
195 static void change_notify_remove_request(struct notify_change_request *remove_req)
198 struct notify_change_request *req;
201 * Paranoia checks, the fsp referenced must must have the request in
202 * its list of pending requests
205 fsp = remove_req->fsp;
206 SMB_ASSERT(fsp->notify != NULL);
208 for (req = fsp->notify->requests; req; req = req->next) {
209 if (req == remove_req) {
215 smb_panic("notify_req not found in fsp's requests\n");
218 DLIST_REMOVE(fsp->notify->requests, req);
219 DLIST_REMOVE(notify_changes_by_mid, req->mid_map);
220 SAFE_FREE(req->mid_map);
221 TALLOC_FREE(req->backend_data);
225 /****************************************************************************
226 Delete entries by mid from the change notify pending queue. Always send reply.
227 *****************************************************************************/
229 void remove_pending_change_notify_requests_by_mid(uint16 mid)
231 struct notify_mid_map *map;
233 for (map = notify_changes_by_mid; map; map = map->next) {
234 if (map->mid == mid) {
243 change_notify_reply_packet(map->req->request_buf, NT_STATUS_CANCELLED);
244 change_notify_remove_request(map->req);
247 /****************************************************************************
248 Delete entries by fnum from the change notify pending queue.
249 *****************************************************************************/
251 void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
254 if (fsp->notify == NULL) {
258 while (fsp->notify->requests != NULL) {
259 change_notify_reply_packet(
260 fsp->notify->requests->request_buf, status);
261 change_notify_remove_request(fsp->notify->requests);
265 /* notify message definition
275 #define MSG_NOTIFY_MESSAGE_SIZE 25 /* Includes at least the '\0' terminator */
277 struct notify_message {
285 static DATA_BLOB notify_message_to_buf(const struct notify_message *msg)
290 len = strlen(msg->name);
292 result = data_blob(NULL, MSG_NOTIFY_MESSAGE_SIZE + len);
297 SDEV_T_VAL(result.data, 0, msg->dev);
298 SINO_T_VAL(result.data, 8, msg->inode);
299 SIVAL(result.data, 16, msg->filter);
300 SIVAL(result.data, 20, msg->action);
301 memcpy(result.data+24, msg->name, len+1);
306 static BOOL buf_to_notify_message(void *buf, size_t len,
307 struct notify_message *msg)
309 if (len < MSG_NOTIFY_MESSAGE_SIZE) {
310 DEBUG(0, ("Got invalid notify message of len %d\n",
315 msg->dev = DEV_T_VAL(buf, 0);
316 msg->inode = INO_T_VAL(buf, 8);
317 msg->filter = IVAL(buf, 16);
318 msg->action = IVAL(buf, 20);
319 msg->name = ((char *)buf)+24;
323 void notify_action(connection_struct *conn, const char *parent,
324 const char *name, uint32 filter, uint32_t action)
326 struct share_mode_lock *lck;
327 SMB_STRUCT_STAT sbuf;
329 struct notify_message msg;
332 struct process_id *pids;
335 DEBUG(10, ("notify_action: parent=%s, name=%s, action=%u\n",
336 parent, name, (unsigned)action));
338 if (SMB_VFS_STAT(conn, parent, &sbuf) != 0) {
340 * Not 100% critical, ignore failure
348 if (asprintf(&fullpath, "%s/%s/%s", conn->connectpath,
349 parent, name) != -1) {
350 notify_trigger(conn->notify_ctx, action, filter,
357 if (!(lck = get_share_mode_lock(NULL, sbuf.st_dev, sbuf.st_ino,
362 msg.dev = sbuf.st_dev;
363 msg.inode = sbuf.st_ino;
366 msg.name = CONST_DISCARD(char *, name);
368 blob = notify_message_to_buf(&msg);
369 if (blob.data == NULL) {
370 DEBUG(0, ("notify_message_to_buf failed\n"));
377 become_root_uid_only();
379 for (i=0; i<lck->num_share_modes; i++) {
380 struct share_mode_entry *e = &lck->share_modes[i];
382 struct process_id *tmp;
384 for (j=0; j<num_pids; j++) {
385 if (procid_equal(&e->pid, &pids[j])) {
392 * Already sent to that process, skip it
397 message_send_pid(lck->share_modes[i].pid, MSG_SMB_NOTIFY,
398 blob.data, blob.length, True);
400 if (!(tmp = TALLOC_REALLOC_ARRAY(lck, pids, struct process_id,
402 DEBUG(0, ("realloc failed\n"));
406 pids[num_pids] = e->pid;
410 unbecome_root_uid_only();
412 data_blob_free(&blob);
416 void notify_fname(connection_struct *conn, uint32 action, uint32 filter,
421 if (asprintf(&fullpath, "%s/%s", conn->connectpath, path) == -1) {
422 DEBUG(0, ("asprintf failed\n"));
426 notify_trigger(conn->notify_ctx, action, filter, fullpath);
430 void notify_fsp(files_struct *fsp, uint32 action, const char *name)
432 struct notify_change *change, *changes;
435 if (fsp->notify == NULL) {
437 * Nobody is waiting, don't queue
442 if (!(name2 = talloc_strdup(fsp->notify, name))) {
443 DEBUG(0, ("talloc_strdup failed\n"));
447 string_replace(name2, '/', '\\');
450 * Someone has triggered a notify previously, queue the change for
451 * later. TODO: Limit the number of changes queued, test how filters
452 * apply here. Do we have to store them?
455 if ((fsp->notify->num_changes > 30) || (name == NULL)) {
457 * W2k3 seems to store at most 30 changes.
459 TALLOC_FREE(fsp->notify->changes);
461 fsp->notify->num_changes = -1;
465 if (fsp->notify->num_changes == -1) {
469 if (!(changes = TALLOC_REALLOC_ARRAY(
470 fsp->notify, fsp->notify->changes,
471 struct notify_change, fsp->notify->num_changes+1))) {
472 DEBUG(0, ("talloc_realloc failed\n"));
477 fsp->notify->changes = changes;
479 change = &(fsp->notify->changes[fsp->notify->num_changes]);
481 change->name = talloc_move(changes, &name2);
482 change->action = action;
483 fsp->notify->num_changes += 1;
485 if (fsp->notify->requests == NULL) {
487 * Nobody is waiting, so don't send anything. The ot
492 if (action == NOTIFY_ACTION_OLD_NAME) {
494 * We have to send the two rename events in one reply. So hold
495 * the first part back.
501 * Someone is waiting for the change, trigger the reply immediately.
503 * TODO: do we have to walk the lists of requests pending?
506 change_notify_reply(fsp->notify->requests->request_buf,
507 fsp->notify->requests->max_param_count,
508 fsp->notify->num_changes,
509 fsp->notify->changes);
511 change_notify_remove_request(fsp->notify->requests);
513 TALLOC_FREE(fsp->notify->changes);
514 fsp->notify->num_changes = 0;
517 static void notify_message_callback(int msgtype, struct process_id pid,
518 void *buf, size_t len,
521 struct notify_message msg;
524 if (!buf_to_notify_message(buf, len, &msg)) {
528 DEBUG(10, ("Received notify_message for 0x%x/%.0f: %d\n",
529 (unsigned)msg.dev, (double)msg.inode, msg.action));
531 for(fsp = fsp_find_di_first(msg.dev, msg.inode); fsp;
532 fsp = fsp_find_di_next(fsp)) {
533 if ((fsp->notify != NULL)
534 && (fsp->notify->requests != NULL)
535 && (fsp->notify->requests->filter & msg.filter)) {
536 notify_fsp(fsp, msg.action, msg.name);
541 char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32 filter)
545 result = talloc_strdup(mem_ctx, "");
547 if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
548 result = talloc_asprintf_append(result, "FILE_NAME|");
549 if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
550 result = talloc_asprintf_append(result, "DIR_NAME|");
551 if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
552 result = talloc_asprintf_append(result, "ATTRIBUTES|");
553 if (filter & FILE_NOTIFY_CHANGE_SIZE)
554 result = talloc_asprintf_append(result, "SIZE|");
555 if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
556 result = talloc_asprintf_append(result, "LAST_WRITE|");
557 if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
558 result = talloc_asprintf_append(result, "LAST_ACCESS|");
559 if (filter & FILE_NOTIFY_CHANGE_CREATION)
560 result = talloc_asprintf_append(result, "CREATION|");
561 if (filter & FILE_NOTIFY_CHANGE_EA)
562 result = talloc_asprintf_append(result, "EA|");
563 if (filter & FILE_NOTIFY_CHANGE_SECURITY)
564 result = talloc_asprintf_append(result, "SECURITY|");
565 if (filter & FILE_NOTIFY_CHANGE_STREAM_NAME)
566 result = talloc_asprintf_append(result, "STREAM_NAME|");
567 if (filter & FILE_NOTIFY_CHANGE_STREAM_SIZE)
568 result = talloc_asprintf_append(result, "STREAM_SIZE|");
569 if (filter & FILE_NOTIFY_CHANGE_STREAM_WRITE)
570 result = talloc_asprintf_append(result, "STREAM_WRITE|");
572 if (result == NULL) return NULL;
573 if (*result == '\0') return result;
575 result[strlen(result)-1] = '\0';
579 /****************************************************************************
580 Initialise the change notify subsystem.
581 ****************************************************************************/
583 BOOL init_change_notify(void)
587 #if HAVE_KERNEL_CHANGE_NOTIFY
588 if (cnotify == NULL && lp_kernel_change_notify())
589 cnotify = kernel_notify_init(smbd_event_context());
591 #if HAVE_FAM_CHANGE_NOTIFY
592 if (cnotify == NULL && lp_fam_change_notify())
593 cnotify = fam_notify_init(smbd_event_context());
595 if (!cnotify) cnotify = hash_notify_init();
598 DEBUG(0,("Failed to init change notify system\n"));
602 message_register(MSG_SMB_NOTIFY, notify_message_callback, NULL);
607 struct sys_notify_context *sys_notify_context_create(struct share_params *scfg,
609 struct event_context *ev)
611 struct sys_notify_context *ctx;
613 if (!(ctx = TALLOC_P(mem_ctx, struct sys_notify_context))) {
614 DEBUG(0, ("talloc failed\n"));
619 ctx->private_data = NULL;
623 NTSTATUS sys_notify_watch(struct sys_notify_context *ctx,
624 struct notify_entry *e,
625 void (*callback)(struct sys_notify_context *ctx,
627 struct notify_event *ev),
628 void *private_data, void *handle)
631 return inotify_watch(ctx, e, callback, private_data, handle);