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
, 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
))) {
170 return NT_STATUS_NO_MEMORY
;
173 request
->mid_map
= map
;
176 memcpy(request
->request_buf
, inbuf
, sizeof(request
->request_buf
));
177 request
->max_param_count
= max_param_count
;
178 request
->filter
= filter
;
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
));
196 static void change_notify_remove_request(struct notify_change_request
*remove_req
)
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
) {
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
);
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
) {
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
,
255 if (fsp
->notify
== NULL
) {
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
276 #define MSG_NOTIFY_MESSAGE_SIZE 25 /* Includes at least the '\0' terminator */
278 struct notify_message
{
286 static DATA_BLOB
notify_message_to_buf(const struct notify_message
*msg
)
291 len
= strlen(msg
->name
);
293 result
= data_blob(NULL
, MSG_NOTIFY_MESSAGE_SIZE
+ len
);
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);
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",
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;
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
;
330 struct notify_message msg
;
333 struct process_id
*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
346 if (!(lck
= get_share_mode_lock(NULL
, sbuf
.st_dev
, sbuf
.st_ino
,
351 msg
.dev
= sbuf
.st_dev
;
352 msg
.inode
= sbuf
.st_ino
;
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"));
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
];
371 struct process_id
*tmp
;
373 for (j
=0; j
<num_pids
; j
++) {
374 if (procid_equal(&e
->pid
, &pids
[j
])) {
381 * Already sent to that process, skip it
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
,
391 DEBUG(0, ("realloc failed\n"));
395 pids
[num_pids
] = e
->pid
;
399 unbecome_root_uid_only();
401 data_blob_free(&blob
);
405 void notify_fname(connection_struct
*conn
, const char *path
,
406 uint32 filter
, uint32 action
)
411 if (!parent_dirname_talloc(tmp_talloc_ctx(), path
, &parent
, &name
)) {
415 notify_action(conn
, parent
, name
, filter
, action
);
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
430 if (fsp
->notify
->requests
!= NULL
) {
432 * Someone is waiting for the change, trigger the reply
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
;
443 * Catch-all change, possibly from notify_hash.c
445 change_notify_reply(req
->request_buf
,
446 req
->max_param_count
,
451 onechange
.action
= action
;
452 onechange
.name
= name
;
454 change_notify_reply(req
->request_buf
, req
->max_param_count
,
456 change_notify_remove_request(req
);
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;
475 if (fsp
->notify
->num_changes
== -1) {
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"));
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"));
494 change
->action
= action
;
495 fsp
->notify
->num_changes
+= 1;
500 static void notify_message_callback(int msgtype
, struct process_id pid
,
501 void *buf
, size_t len
,
504 struct notify_message msg
;
507 if (!buf_to_notify_message(buf
, len
, &msg
)) {
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)
532 #if HAVE_KERNEL_CHANGE_NOTIFY
533 if (cnotify
== NULL
&& lp_kernel_change_notify())
534 cnotify
= kernel_notify_init(smbd_event_context());
536 #if HAVE_FAM_CHANGE_NOTIFY
537 if (cnotify
== NULL
&& lp_fam_change_notify())
538 cnotify
= fam_notify_init(smbd_event_context());
540 if (!cnotify
) cnotify
= hash_notify_init();
543 DEBUG(0,("Failed to init change notify system\n"));
547 message_register(MSG_SMB_NOTIFY
, notify_message_callback
, NULL
);