enable the Linux change notify code and change some notify debug code
[gd/samba/.git] / source3 / smbd / notify.c
1 #define OLD_NTDOMAIN 1
2 /*
3    Unix SMB/Netbios implementation.
4    Version 3.0
5    change notify handling
6    Copyright (C) Andrew Tridgell 2000
7    Copyright (C) Jeremy Allison 1994-1998
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 extern int DEBUGLEVEL;
27
28 static struct cnotify_fns *cnotify;
29
30 /****************************************************************************
31  This is the structure to queue to implement NT change
32  notify. It consists of smb_size bytes stored from the
33  transact command (to keep the mid, tid etc around).
34  Plus the fid to examine and notify private data
35 *****************************************************************************/
36
37 struct change_notify {
38         struct change_notify *next, *prev;
39         files_struct *fsp;
40         connection_struct *conn;
41         uint32 flags;
42         char request_buf[smb_size];
43         void *change_data;
44 };
45
46 static struct change_notify *change_notify_list;
47
48 /****************************************************************************
49  Setup the common parts of the return packet and send it.
50 *****************************************************************************/
51 static void change_notify_reply_packet(char *inbuf, uint32 error_code)
52 {
53         char outbuf[smb_size+38];
54
55         memset(outbuf, '\0', sizeof(outbuf));
56         construct_reply_common(inbuf, outbuf);
57
58         /*
59          * If we're returning a 'too much in the directory changed' we need to
60          * set this is an NT error status flags. If we don't then the (probably
61          * untested) code in the NT redirector has a bug in that it doesn't re-issue
62          * the change notify.... Ah - I *love* it when I get so deeply into this I
63          * can even determine how MS failed to test stuff and why.... :-). JRA.
64          */
65         
66         SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
67         ERROR(0,error_code);
68
69         /*
70          * Seems NT needs a transact command with an error code
71          * in it. This is a longer packet than a simple error.
72          */
73         set_message(outbuf,18,0,False);
74
75         send_smb(smbd_server_fd(),outbuf);
76 }
77
78 /****************************************************************************
79 remove an entry from the list and free it, also closing any
80 directory handle if necessary
81 Notice the horrible stuff we have to do because this is a singly linked list.
82 *****************************************************************************/
83 static void change_notify_remove(struct change_notify *cnbp)
84 {
85         cnotify->remove_notify(cnbp->change_data);
86         DLIST_REMOVE(change_notify_list, cnbp);
87         ZERO_STRUCTP(cnbp);
88         free(cnbp);
89 }
90
91
92 /****************************************************************************
93  Delete entries by fnum from the change notify pending queue.
94 *****************************************************************************/
95 void remove_pending_change_notify_requests_by_fid(files_struct *fsp)
96 {
97         struct change_notify *cnbp, *next;
98
99         for (cnbp=change_notify_list; cnbp; cnbp=next) {
100                 next=cnbp->next;
101                 if (cnbp->fsp->fnum == fsp->fnum) {
102                         change_notify_remove(cnbp);
103                 }
104         }
105 }
106
107 /****************************************************************************
108  Delete entries by mid from the change notify pending queue. Always send reply.
109 *****************************************************************************/
110 void remove_pending_change_notify_requests_by_mid(int mid)
111 {
112         struct change_notify *cnbp, *next;
113
114         for (cnbp=change_notify_list; cnbp; cnbp=next) {
115                 next=cnbp->next;
116                 if(SVAL(cnbp->request_buf,smb_mid) == mid) {
117                         change_notify_reply_packet(cnbp->request_buf,NT_STATUS_CANCELLED);
118                         change_notify_remove(cnbp);
119                 }
120         }
121 }
122
123 /****************************************************************************
124  Delete entries by filename and cnum from the change notify pending queue.
125  Always send reply.
126 *****************************************************************************/
127 void remove_pending_change_notify_requests_by_filename(files_struct *fsp)
128 {
129         struct change_notify *cnbp, *next;
130
131         for (cnbp=change_notify_list; cnbp; cnbp=next) {
132                 next=cnbp->next;
133                 /*
134                  * We know it refers to the same directory if the connection number and
135                  * the filename are identical.
136                  */
137                 if((cnbp->fsp->conn == fsp->conn) && strequal(cnbp->fsp->fsp_name,fsp->fsp_name)) {
138                         change_notify_reply_packet(cnbp->request_buf,NT_STATUS_CANCELLED);
139                         change_notify_remove(cnbp);
140                 }
141         }
142 }
143
144 /****************************************************************************
145  Return true if there are pending change notifies.
146 ****************************************************************************/
147 BOOL change_notifies_pending(void)
148 {
149         return (change_notify_list != NULL);
150 }
151
152 /****************************************************************************
153  Process the change notify queue. Note that this is only called as root.
154  Returns True if there are still outstanding change notify requests on the
155  queue.
156 *****************************************************************************/
157 BOOL process_pending_change_notify_queue(time_t t)
158 {
159         struct change_notify *cnbp, *next;
160         uint16 vuid;
161
162         for (cnbp=change_notify_list; cnbp; cnbp=next) {
163                 next=cnbp->next;
164
165                 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID : SVAL(cnbp->request_buf,smb_uid);
166                 
167                 if (cnotify->check_notify(cnbp->conn, vuid, cnbp->fsp->fsp_name, cnbp->flags, cnbp->change_data, t)) {
168                         change_notify_reply_packet(cnbp->request_buf,STATUS_NOTIFY_ENUM_DIR);
169                         change_notify_remove(cnbp);
170                 }
171         }
172
173         return (change_notify_list != NULL);
174 }
175
176 /****************************************************************************
177    * Now queue an entry on the notify change list.
178    * We only need to save smb_size bytes from this incoming packet
179    * as we will always by returning a 'read the directory yourself'
180    * error.
181 ****************************************************************************/
182 BOOL change_notify_set(char *inbuf, files_struct *fsp, connection_struct *conn, uint32 flags)
183 {
184         struct change_notify *cnbp;
185
186         if((cnbp = (struct change_notify *)malloc(sizeof(*cnbp))) == NULL) {
187                 DEBUG(0,("call_nt_transact_notify_change: malloc fail !\n" ));
188                 return -1;
189         }
190
191         ZERO_STRUCTP(cnbp);
192
193         memcpy(cnbp->request_buf, inbuf, smb_size);
194         cnbp->fsp = fsp;
195         cnbp->conn = conn;
196         cnbp->flags = flags;
197         cnbp->change_data = cnotify->register_notify(conn, fsp->fsp_name, flags);
198         
199         if (!cnbp->change_data) {
200                 free(cnbp);
201                 return False;
202         }
203
204         DLIST_ADD(change_notify_list, cnbp);
205
206         return True;
207 }
208
209
210 /****************************************************************************
211 initialise the change notify subsystem
212 ****************************************************************************/
213 BOOL init_change_notify(void)
214 {
215 #if HAVE_KERNEL_CHANGE_NOTIFY
216         cnotify = kernel_notify_init();
217 #endif
218         if (!cnotify) cnotify = hash_notify_init();
219         
220         if (!cnotify) {
221                 DEBUG(0,("Failed to init change notify system\n"));
222                 return False;
223         }
224
225         return True;
226 }
227
228
229 #undef OLD_NTDOMAIN