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