r20634: A *LOT* more work is necessary before touching notify remotely starts to...
[samba.git] / source / smbd / notify.c
1 /*
2    Unix SMB/CIFS implementation.
3    change notify handling
4    Copyright (C) Andrew Tridgell 2000
5    Copyright (C) Jeremy Allison 1994-1998
6
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.
11
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.
16
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.
20 */
21
22 #include "includes.h"
23
24 static struct cnotify_fns *cnotify;
25
26 /****************************************************************************
27  This is the structure to queue to implement NT change
28  notify. It consists of smb_size bytes stored from the
29  transact command (to keep the mid, tid etc around).
30  Plus the fid to examine and notify private data.
31 *****************************************************************************/
32
33 struct change_notify {
34         struct change_notify *next, *prev;
35         files_struct *fsp;
36         connection_struct *conn;
37         uint32 flags;
38         char request_buf[smb_size];
39         void *change_data;
40 };
41
42 static struct change_notify *change_notify_list;
43
44 /****************************************************************************
45  Setup the common parts of the return packet and send it.
46 *****************************************************************************/
47
48 static void change_notify_reply_packet(char *inbuf, NTSTATUS error_code)
49 {
50         char outbuf[smb_size+38];
51
52         memset(outbuf, '\0', sizeof(outbuf));
53         construct_reply_common(inbuf, outbuf);
54
55         ERROR_NT(error_code);
56
57         /*
58          * Seems NT needs a transact command with an error code
59          * in it. This is a longer packet than a simple error.
60          */
61         set_message(outbuf,18,0,False);
62
63         show_msg(outbuf);
64         if (!send_smb(smbd_server_fd(),outbuf))
65                 exit_server_cleanly("change_notify_reply_packet: send_smb failed.");
66 }
67
68 /****************************************************************************
69  Remove an entry from the list and free it, also closing any
70  directory handle if necessary.
71 *****************************************************************************/
72
73 static void change_notify_remove(struct change_notify *cnbp)
74 {
75         cnotify->remove_notify(cnbp->change_data);
76         DLIST_REMOVE(change_notify_list, cnbp);
77         ZERO_STRUCTP(cnbp);
78         SAFE_FREE(cnbp);
79 }
80
81 /****************************************************************************
82  Delete entries by fnum from the change notify pending queue.
83 *****************************************************************************/
84
85 void remove_pending_change_notify_requests_by_fid(files_struct *fsp, NTSTATUS status)
86 {
87         struct change_notify *cnbp, *next;
88
89         for (cnbp=change_notify_list; cnbp; cnbp=next) {
90                 next=cnbp->next;
91                 if (cnbp->fsp->fnum == fsp->fnum) {
92                         change_notify_reply_packet(cnbp->request_buf,status);
93                         change_notify_remove(cnbp);
94                 }
95         }
96 }
97
98 /****************************************************************************
99  Delete entries by mid from the change notify pending queue. Always send reply.
100 *****************************************************************************/
101
102 void remove_pending_change_notify_requests_by_mid(int mid)
103 {
104         struct change_notify *cnbp, *next;
105
106         for (cnbp=change_notify_list; cnbp; cnbp=next) {
107                 next=cnbp->next;
108                 if(SVAL(cnbp->request_buf,smb_mid) == mid) {
109                         change_notify_reply_packet(cnbp->request_buf,NT_STATUS_CANCELLED);
110                         change_notify_remove(cnbp);
111                 }
112         }
113 }
114
115 /****************************************************************************
116  Delete entries by filename and cnum from the change notify pending queue.
117  Always send reply.
118 *****************************************************************************/
119
120 void remove_pending_change_notify_requests_by_filename(files_struct *fsp, NTSTATUS status)
121 {
122         struct change_notify *cnbp, *next;
123
124         for (cnbp=change_notify_list; cnbp; cnbp=next) {
125                 next=cnbp->next;
126                 /*
127                  * We know it refers to the same directory if the connection number and
128                  * the filename are identical.
129                  */
130                 if((cnbp->fsp->conn == fsp->conn) && strequal(cnbp->fsp->fsp_name,fsp->fsp_name)) {
131                         change_notify_reply_packet(cnbp->request_buf,status);
132                         change_notify_remove(cnbp);
133                 }
134         }
135 }
136
137 /****************************************************************************
138  Set the current change notify timeout to the lowest value across all service
139  values.
140 ****************************************************************************/
141
142 void set_change_notify_timeout(int val)
143 {
144         if (val > 0) {
145                 cnotify->select_time = MIN(cnotify->select_time, val);
146         }
147 }
148
149 /****************************************************************************
150  Longest time to sleep for before doing a change notify scan.
151 ****************************************************************************/
152
153 int change_notify_timeout(void)
154 {
155         return cnotify->select_time;
156 }
157
158 /****************************************************************************
159  Process the change notify queue. Note that this is only called as root.
160  Returns True if there are still outstanding change notify requests on the
161  queue.
162 *****************************************************************************/
163
164 BOOL process_pending_change_notify_queue(time_t t)
165 {
166         struct change_notify *cnbp, *next;
167         uint16 vuid;
168
169         for (cnbp=change_notify_list; cnbp; cnbp=next) {
170                 next=cnbp->next;
171
172                 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID : SVAL(cnbp->request_buf,smb_uid);
173
174                 if (cnotify->check_notify(cnbp->conn, vuid, cnbp->fsp->fsp_name, cnbp->flags, cnbp->change_data, t)) {
175                         DEBUG(10,("process_pending_change_notify_queue: dir %s changed !\n", cnbp->fsp->fsp_name ));
176                         change_notify_reply_packet(cnbp->request_buf,STATUS_NOTIFY_ENUM_DIR);
177                         change_notify_remove(cnbp);
178                 }
179         }
180
181         return (change_notify_list != NULL);
182 }
183
184 /****************************************************************************
185  Now queue an entry on the notify change list.
186  We only need to save smb_size bytes from this incoming packet
187  as we will always by returning a 'read the directory yourself'
188  error.
189 ****************************************************************************/
190
191 BOOL change_notify_set(char *inbuf, files_struct *fsp, connection_struct *conn, uint32 flags)
192 {
193         struct change_notify *cnbp;
194
195         if((cnbp = SMB_MALLOC_P(struct change_notify)) == NULL) {
196                 DEBUG(0,("change_notify_set: malloc fail !\n" ));
197                 return -1;
198         }
199
200         ZERO_STRUCTP(cnbp);
201
202         memcpy(cnbp->request_buf, inbuf, smb_size);
203         cnbp->fsp = fsp;
204         cnbp->conn = conn;
205         cnbp->flags = flags;
206         cnbp->change_data = cnotify->register_notify(conn, fsp->fsp_name, flags);
207         
208         if (!cnbp->change_data) {
209                 SAFE_FREE(cnbp);
210                 return False;
211         }
212
213         DLIST_ADD(change_notify_list, cnbp);
214
215         /* Push the MID of this packet on the signing queue. */
216         srv_defer_sign_response(SVAL(inbuf,smb_mid));
217
218         return True;
219 }
220
221 int change_notify_fd(void)
222 {
223         if (cnotify) {
224                 return cnotify->notification_fd;
225         }
226
227         return -1;
228 }
229
230 /****************************************************************************
231  Initialise the change notify subsystem.
232 ****************************************************************************/
233
234 BOOL init_change_notify(void)
235 {
236         cnotify = NULL;
237
238 #if HAVE_KERNEL_CHANGE_NOTIFY
239         if (cnotify == NULL && lp_kernel_change_notify())
240                 cnotify = kernel_notify_init();
241 #endif
242 #if HAVE_FAM_CHANGE_NOTIFY
243         if (cnotify == NULL && lp_fam_change_notify())
244                 cnotify = fam_notify_init();
245 #endif
246         if (!cnotify) cnotify = hash_notify_init();
247         
248         if (!cnotify) {
249                 DEBUG(0,("Failed to init change notify system\n"));
250                 return False;
251         }
252
253         return True;
254 }