r20451: Survive the first of the filter-tests (mkdir).
[Samba/gebeck_regimport.git] / source3 / smbd / notify.c
blobf94ff76e7b8a68002a408fb6870a54baea5c6e5f
1 /*
2 Unix SMB/CIFS implementation.
3 change notify handling
4 Copyright (C) Andrew Tridgell 2000
5 Copyright (C) Jeremy Allison 1994-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 static struct cnotify_fns *cnotify;
25 static struct notify_mid_map *notify_changes_by_mid;
27 /****************************************************************************
28 This is the structure to queue to implement NT change
29 notify. It consists of smb_size bytes stored from the
30 transact command (to keep the mid, tid etc around).
31 Plus the fid to examine and notify private data.
32 *****************************************************************************/
34 struct change_notify {
35 struct change_notify *next, *prev;
36 files_struct *fsp;
37 uint32 flags;
38 uint32 max_param_count;
39 char request_buf[smb_size];
40 void *change_data;
43 static struct change_notify *change_notify_list;
45 static BOOL notify_marshall_changes(unsigned num_changes,
46 struct notify_change *changes,
47 prs_struct *ps)
49 int i;
50 UNISTR uni_name;
52 for (i=0; i<num_changes; i++) {
53 struct notify_change *c = &changes[i];
54 size_t namelen;
55 uint32 u32_tmp; /* Temp arg to prs_uint32 to avoid
56 * signed/unsigned issues */
58 namelen = convert_string_allocate(
59 NULL, CH_UNIX, CH_UTF16LE, c->name, strlen(c->name)+1,
60 &uni_name.buffer, True);
61 if ((namelen == -1) || (uni_name.buffer == NULL)) {
62 goto fail;
65 namelen -= 2; /* Dump NULL termination */
68 * Offset to next entry, only if there is one
71 u32_tmp = (i == num_changes-1) ? 0 : namelen + 12;
72 if (!prs_uint32("offset", ps, 1, &u32_tmp)) goto fail;
74 u32_tmp = c->action;
75 if (!prs_uint32("action", ps, 1, &u32_tmp)) goto fail;
77 u32_tmp = namelen;
78 if (!prs_uint32("namelen", ps, 1, &u32_tmp)) goto fail;
80 if (!prs_unistr("name", ps, 1, &uni_name)) goto fail;
83 * Not NULL terminated, decrease by the 2 UCS2 \0 chars
85 prs_set_offset(ps, prs_offset(ps)-2);
87 SAFE_FREE(uni_name.buffer);
90 return True;
92 fail:
93 SAFE_FREE(uni_name.buffer);
94 return False;
97 /****************************************************************************
98 Setup the common parts of the return packet and send it.
99 *****************************************************************************/
101 void change_notify_reply_packet(const char *request_buf, NTSTATUS error_code)
103 char outbuf[smb_size+38];
105 memset(outbuf, '\0', sizeof(outbuf));
106 construct_reply_common(request_buf, outbuf);
108 ERROR_NT(error_code);
111 * Seems NT needs a transact command with an error code
112 * in it. This is a longer packet than a simple error.
114 set_message(outbuf,18,0,False);
116 show_msg(outbuf);
117 if (!send_smb(smbd_server_fd(),outbuf))
118 exit_server_cleanly("change_notify_reply_packet: send_smb failed.");
121 void change_notify_reply(const char *request_buf, uint32 max_param_count,
122 unsigned num_changes, struct notify_change *changes)
124 char *outbuf = NULL;
125 prs_struct ps;
126 size_t buflen = smb_size+38+max_param_count;
128 if (!prs_init(&ps, 0, NULL, False)
129 || !notify_marshall_changes(num_changes, changes, &ps)) {
130 change_notify_reply_packet(request_buf, NT_STATUS_NO_MEMORY);
131 goto done;
134 if (prs_offset(&ps) > max_param_count) {
136 * We exceed what the client is willing to accept. Send
137 * nothing.
139 change_notify_reply_packet(request_buf, NT_STATUS_OK);
140 goto done;
143 if (!(outbuf = SMB_MALLOC_ARRAY(char, buflen))) {
144 change_notify_reply_packet(request_buf, NT_STATUS_NO_MEMORY);
145 goto done;
148 construct_reply_common(request_buf, outbuf);
150 if (send_nt_replies(outbuf, buflen, NT_STATUS_OK, prs_data_p(&ps),
151 prs_offset(&ps), NULL, 0) == -1) {
152 exit_server("change_notify_reply_packet: send_smb failed.");
155 done:
156 SAFE_FREE(outbuf);
157 prs_mem_free(&ps);
160 /****************************************************************************
161 Remove an entry from the list and free it, also closing any
162 directory handle if necessary.
163 *****************************************************************************/
165 static void change_notify_remove(struct change_notify *cnbp)
167 cnotify->remove_notify(cnbp->change_data);
168 DLIST_REMOVE(change_notify_list, cnbp);
169 ZERO_STRUCTP(cnbp);
170 SAFE_FREE(cnbp);
173 NTSTATUS change_notify_add_request(const char *inbuf, uint32 max_param_count,
174 uint32 filter, struct files_struct *fsp)
176 struct notify_change_request *request = NULL;
177 struct notify_mid_map *map = NULL;
179 if (!(request = SMB_MALLOC_P(struct notify_change_request))
180 || !(map = SMB_MALLOC_P(struct notify_mid_map))) {
181 SAFE_FREE(request);
182 return NT_STATUS_NO_MEMORY;
185 request->mid_map = map;
186 map->req = request;
188 memcpy(request->request_buf, inbuf, sizeof(request->request_buf));
189 request->max_param_count = max_param_count;
190 request->filter = filter;
191 request->fsp = fsp;
192 DLIST_ADD_END(fsp->notify->requests, request,
193 struct notify_change_request *);
195 map->mid = SVAL(inbuf, smb_mid);
196 DLIST_ADD(notify_changes_by_mid, map);
198 /* Push the MID of this packet on the signing queue. */
199 srv_defer_sign_response(SVAL(inbuf,smb_mid));
201 return NT_STATUS_OK;
204 static void change_notify_remove_request(struct notify_change_request *remove_req)
206 files_struct *fsp;
207 struct notify_change_request *req;
210 * Paranoia checks, the fsp referenced must must have the request in
211 * its list of pending requests
214 fsp = remove_req->fsp;
215 SMB_ASSERT(fsp->notify != NULL);
217 for (req = fsp->notify->requests; req; req = req->next) {
218 if (req == remove_req) {
219 break;
222 SMB_ASSERT(req != NULL);
224 DLIST_REMOVE(fsp->notify->requests, req);
225 DLIST_REMOVE(notify_changes_by_mid, req->mid_map);
226 SAFE_FREE(req->mid_map);
227 SAFE_FREE(req);
230 /****************************************************************************
231 Delete entries by mid from the change notify pending queue. Always send reply.
232 *****************************************************************************/
234 void remove_pending_change_notify_requests_by_mid(uint16 mid)
236 struct notify_mid_map *map;
238 for (map = notify_changes_by_mid; map; map = map->next) {
239 if (map->mid == mid) {
240 break;
244 if (map == NULL) {
245 return;
248 change_notify_reply_packet(map->req->request_buf, NT_STATUS_CANCELLED);
249 change_notify_remove_request(map->req);
252 /****************************************************************************
253 Delete entries by fnum from the change notify pending queue.
254 *****************************************************************************/
256 void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
257 NTSTATUS status)
259 if (fsp->notify == NULL) {
260 return;
263 while (fsp->notify->requests != NULL) {
264 change_notify_reply_packet(
265 fsp->notify->requests->request_buf, status);
266 change_notify_remove_request(fsp->notify->requests);
270 /****************************************************************************
271 Delete entries by filename and cnum from the change notify pending queue.
272 Always send reply.
273 *****************************************************************************/
275 void remove_pending_change_notify_requests_by_filename(files_struct *fsp, NTSTATUS status)
277 struct change_notify *cnbp, *next;
279 for (cnbp=change_notify_list; cnbp; cnbp=next) {
280 next=cnbp->next;
282 * We know it refers to the same directory if the connection number and
283 * the filename are identical.
285 if((cnbp->fsp->conn == fsp->conn) && strequal(cnbp->fsp->fsp_name,fsp->fsp_name)) {
286 change_notify_reply_packet(cnbp->request_buf, status);
287 change_notify_remove(cnbp);
292 /****************************************************************************
293 Set the current change notify timeout to the lowest value across all service
294 values.
295 ****************************************************************************/
297 void set_change_notify_timeout(int val)
299 if (val > 0) {
300 cnotify->select_time = MIN(cnotify->select_time, val);
304 /****************************************************************************
305 Longest time to sleep for before doing a change notify scan.
306 ****************************************************************************/
308 int change_notify_timeout(void)
310 return cnotify->select_time;
313 /****************************************************************************
314 Process the change notify queue. Note that this is only called as root.
315 Returns True if there are still outstanding change notify requests on the
316 queue.
317 *****************************************************************************/
319 BOOL process_pending_change_notify_queue(time_t t)
321 struct change_notify *cnbp, *next;
322 uint16 vuid;
324 for (cnbp=change_notify_list; cnbp; cnbp=next) {
325 next=cnbp->next;
327 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID : SVAL(cnbp->request_buf,smb_uid);
329 if (cnbp->fsp->notify->num_changes != 0) {
330 DEBUG(10,("process_pending_change_notify_queue: %s "
331 "has %d changes!\n", cnbp->fsp->fsp_name,
332 cnbp->fsp->notify->num_changes));
333 change_notify_reply(cnbp->request_buf,
334 cnbp->max_param_count,
335 cnbp->fsp->notify->num_changes,
336 cnbp->fsp->notify->changes);
337 change_notify_remove(cnbp);
338 continue;
341 if (cnotify->check_notify(cnbp->fsp->conn, vuid,
342 cnbp->fsp->fsp_name, cnbp->flags,
343 cnbp->change_data, t)) {
344 DEBUG(10,("process_pending_change_notify_queue: dir "
345 "%s changed !\n", cnbp->fsp->fsp_name ));
346 change_notify_reply(cnbp->request_buf,
347 cnbp->max_param_count,
348 cnbp->fsp->notify->num_changes,
349 cnbp->fsp->notify->changes);
350 change_notify_remove(cnbp);
354 return (change_notify_list != NULL);
357 /****************************************************************************
358 Now queue an entry on the notify change list.
359 We only need to save smb_size bytes from this incoming packet
360 as we will always by returning a 'read the directory yourself'
361 error.
362 ****************************************************************************/
364 BOOL change_notify_set(char *inbuf, files_struct *fsp, connection_struct *conn,
365 uint32 flags, uint32 max_param_count)
367 struct change_notify *cnbp;
369 if((cnbp = SMB_MALLOC_P(struct change_notify)) == NULL) {
370 DEBUG(0,("change_notify_set: malloc fail !\n" ));
371 return False;
374 ZERO_STRUCTP(cnbp);
376 memcpy(cnbp->request_buf, inbuf, smb_size);
377 cnbp->fsp = fsp;
378 cnbp->flags = flags;
379 cnbp->max_param_count = max_param_count;
380 cnbp->change_data = cnotify->register_notify(conn, fsp->fsp_name,
381 flags);
383 if (!cnbp->change_data) {
384 SAFE_FREE(cnbp);
385 return False;
388 DLIST_ADD(change_notify_list, cnbp);
390 /* Push the MID of this packet on the signing queue. */
391 srv_defer_sign_response(SVAL(inbuf,smb_mid));
393 return True;
396 int change_notify_fd(void)
398 if (cnotify) {
399 return cnotify->notification_fd;
402 return -1;
405 /* notify message definition
407 Offset Data length.
408 0 SMB_DEV_T dev 8
409 8 SMB_INO_T inode 8
410 16 uint32 filter 4
411 20 uint32 action 4
412 24.. name
415 #define MSG_NOTIFY_MESSAGE_SIZE 25 /* Includes at least the '\0' terminator */
417 struct notify_message {
418 SMB_DEV_T dev;
419 SMB_INO_T inode;
420 uint32 filter;
421 uint32 action;
422 char *name;
425 static DATA_BLOB notify_message_to_buf(const struct notify_message *msg)
427 DATA_BLOB result;
428 size_t len;
430 len = strlen(msg->name);
432 result = data_blob(NULL, MSG_NOTIFY_MESSAGE_SIZE + len);
433 if (!result.data) {
434 return result;
437 SDEV_T_VAL(result.data, 0, msg->dev);
438 SINO_T_VAL(result.data, 8, msg->inode);
439 SIVAL(result.data, 16, msg->filter);
440 SIVAL(result.data, 20, msg->action);
441 memcpy(result.data+24, msg->name, len+1);
443 return result;
446 static BOOL buf_to_notify_message(void *buf, size_t len,
447 struct notify_message *msg)
449 if (len < MSG_NOTIFY_MESSAGE_SIZE) {
450 DEBUG(0, ("Got invalid notify message of len %d\n",
451 (int)len));
452 return False;
455 msg->dev = DEV_T_VAL(buf, 0);
456 msg->inode = INO_T_VAL(buf, 8);
457 msg->filter = IVAL(buf, 16);
458 msg->action = IVAL(buf, 20);
459 msg->name = ((char *)buf)+24;
460 return True;
463 void notify_action(connection_struct *conn, const char *parent,
464 const char *name, uint32 filter, uint32_t action)
466 struct share_mode_lock *lck;
467 SMB_STRUCT_STAT sbuf;
468 int i;
469 struct notify_message msg;
470 DATA_BLOB blob;
472 struct process_id *pids;
473 int num_pids;
475 DEBUG(10, ("notify_action: parent=%s, name=%s, action=%u\n",
476 parent, name, (unsigned)action));
478 if (SMB_VFS_STAT(conn, parent, &sbuf) != 0) {
480 * Not 100% critical, ignore failure
482 return;
485 if (!(lck = get_share_mode_lock(NULL, sbuf.st_dev, sbuf.st_ino,
486 NULL, NULL))) {
487 return;
490 msg.dev = sbuf.st_dev;
491 msg.inode = sbuf.st_ino;
492 msg.filter = filter;
493 msg.action = action;
494 msg.name = CONST_DISCARD(char *, name);
496 blob = notify_message_to_buf(&msg);
497 if (blob.data == NULL) {
498 DEBUG(0, ("notify_message_to_buf failed\n"));
499 return;
502 pids = NULL;
503 num_pids = 0;
505 become_root_uid_only();
507 for (i=0; i<lck->num_share_modes; i++) {
508 struct share_mode_entry *e = &lck->share_modes[i];
509 int j;
510 struct process_id *tmp;
512 for (j=0; j<num_pids; j++) {
513 if (procid_equal(&e->pid, &pids[j])) {
514 break;
518 if (j < num_pids) {
520 * Already sent to that process, skip it
522 continue;
525 message_send_pid(lck->share_modes[i].pid, MSG_SMB_NOTIFY,
526 blob.data, blob.length, True);
528 if (!(tmp = TALLOC_REALLOC_ARRAY(lck, pids, struct process_id,
529 num_pids+1))) {
530 DEBUG(0, ("realloc failed\n"));
531 break;
533 pids = tmp;
534 pids[num_pids] = e->pid;
535 num_pids += 1;
538 unbecome_root_uid_only();
540 data_blob_free(&blob);
541 TALLOC_FREE(lck);
544 static void notify_fsp(files_struct *fsp, struct notify_message *msg)
546 struct notify_change *change, *changes;
548 if (fsp->notify == NULL) {
550 * Nobody is waiting, don't queue
552 return;
555 if ((fsp->notify->requests != NULL)
556 && (fsp->notify->requests->filter & msg->filter)) {
558 * Someone is waiting for the change, trigger the reply
559 * immediately.
561 * TODO: do we have to walk the lists of requests pending?
564 struct notify_change_request *req = fsp->notify->requests;
565 struct notify_change onechange;
567 onechange.action = msg->action;
568 onechange.name = msg->name;
570 change_notify_reply(req->request_buf, req->max_param_count,
571 1, &onechange);
572 change_notify_remove_request(req);
573 return;
577 * Someone has triggered a notify previously, queue the change for
578 * later. TODO: Limit the number of changes queued, test how filters
579 * apply here. Do we have to store them?
582 if (!(changes = TALLOC_REALLOC_ARRAY(
583 fsp->notify, fsp->notify->changes,
584 struct notify_change, fsp->notify->num_changes+1))) {
585 DEBUG(0, ("talloc_realloc failed\n"));
586 return;
589 fsp->notify->changes = changes;
591 change = &(fsp->notify->changes[fsp->notify->num_changes]);
593 if (!(change->name = talloc_strdup(changes, msg->name))) {
594 DEBUG(0, ("talloc_strdup failed\n"));
595 return;
597 change->action = msg->action;
598 fsp->notify->num_changes += 1;
600 return;
603 static void notify_message_callback(int msgtype, struct process_id pid,
604 void *buf, size_t len)
606 struct notify_message msg;
607 files_struct *fsp;
609 if (!buf_to_notify_message(buf, len, &msg)) {
610 return;
613 DEBUG(10, ("Received notify_message for 0x%x/%.0f: %d\n",
614 (unsigned)msg.dev, (double)msg.inode, msg.action));
616 for(fsp = fsp_find_di_first(msg.dev, msg.inode); fsp;
617 fsp = fsp_find_di_next(fsp)) {
618 notify_fsp(fsp, &msg);
622 /****************************************************************************
623 Initialise the change notify subsystem.
624 ****************************************************************************/
626 BOOL init_change_notify(void)
628 cnotify = NULL;
630 #if HAVE_KERNEL_CHANGE_NOTIFY
631 if (cnotify == NULL && lp_kernel_change_notify())
632 cnotify = kernel_notify_init();
633 #endif
634 #if HAVE_FAM_CHANGE_NOTIFY
635 if (cnotify == NULL && lp_fam_change_notify())
636 cnotify = fam_notify_init();
637 #endif
638 if (!cnotify) cnotify = hash_notify_init();
640 if (!cnotify) {
641 DEBUG(0,("Failed to init change notify system\n"));
642 return False;
645 message_register(MSG_SMB_NOTIFY, notify_message_callback);
647 return True;