68b51aeabb8480bfc1a577105ff7d0573bbfee99
[nivanova/samba-autobuild/.git] / source3 / 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, NTSTATUS error_code)
51 {
52         char outbuf[smb_size+38];
53
54         memset(outbuf, '\0', sizeof(outbuf));
55         construct_reply_common(inbuf, outbuf);
56
57         ERROR_NT(error_code);
58
59         /*
60          * Seems NT needs a transact command with an error code
61          * in it. This is a longer packet than a simple error.
62          */
63         set_message(outbuf,18,0,False);
64
65         if (!send_smb(smbd_server_fd(),outbuf))
66                 exit_server("change_notify_reply_packet: send_smb failed.\n");
67 }
68
69 /****************************************************************************
70 remove an entry from the list and free it, also closing any
71 directory handle if necessary
72 Notice the horrible stuff we have to do because this is a singly linked list.
73 *****************************************************************************/
74 static void change_notify_remove(struct change_notify *cnbp)
75 {
76         cnotify->remove_notify(cnbp->change_data);
77         DLIST_REMOVE(change_notify_list, cnbp);
78         ZERO_STRUCTP(cnbp);
79         SAFE_FREE(cnbp);
80 }
81
82
83 /****************************************************************************
84  Delete entries by fnum from the change notify pending queue.
85 *****************************************************************************/
86 void remove_pending_change_notify_requests_by_fid(files_struct *fsp)
87 {
88         struct change_notify *cnbp, *next;
89
90         for (cnbp=change_notify_list; cnbp; cnbp=next) {
91                 next=cnbp->next;
92                 if (cnbp->fsp->fnum == fsp->fnum) {
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 void remove_pending_change_notify_requests_by_mid(int mid)
102 {
103         struct change_notify *cnbp, *next;
104
105         for (cnbp=change_notify_list; cnbp; cnbp=next) {
106                 next=cnbp->next;
107                 if(SVAL(cnbp->request_buf,smb_mid) == mid) {
108                         change_notify_reply_packet(cnbp->request_buf,NT_STATUS_CANCELLED);
109                         change_notify_remove(cnbp);
110                 }
111         }
112 }
113
114 /****************************************************************************
115  Delete entries by filename and cnum from the change notify pending queue.
116  Always send reply.
117 *****************************************************************************/
118 void remove_pending_change_notify_requests_by_filename(files_struct *fsp)
119 {
120         struct change_notify *cnbp, *next;
121
122         for (cnbp=change_notify_list; cnbp; cnbp=next) {
123                 next=cnbp->next;
124                 /*
125                  * We know it refers to the same directory if the connection number and
126                  * the filename are identical.
127                  */
128                 if((cnbp->fsp->conn == fsp->conn) && strequal(cnbp->fsp->fsp_name,fsp->fsp_name)) {
129                         change_notify_reply_packet(cnbp->request_buf,NT_STATUS_CANCELLED);
130                         change_notify_remove(cnbp);
131                 }
132         }
133 }
134
135 /****************************************************************************
136  Return true if there are pending change notifies.
137 ****************************************************************************/
138 int change_notify_timeout(void)
139 {
140         return cnotify->select_time;
141 }
142
143 /****************************************************************************
144  Process the change notify queue. Note that this is only called as root.
145  Returns True if there are still outstanding change notify requests on the
146  queue.
147 *****************************************************************************/
148 BOOL process_pending_change_notify_queue(time_t t)
149 {
150         struct change_notify *cnbp, *next;
151         uint16 vuid;
152
153         for (cnbp=change_notify_list; cnbp; cnbp=next) {
154                 next=cnbp->next;
155
156                 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID : SVAL(cnbp->request_buf,smb_uid);
157                 
158                 if (cnotify->check_notify(cnbp->conn, vuid, cnbp->fsp->fsp_name, cnbp->flags, cnbp->change_data, t)) {
159                         change_notify_reply_packet(cnbp->request_buf,STATUS_NOTIFY_ENUM_DIR);
160                         change_notify_remove(cnbp);
161                 }
162         }
163
164         return (change_notify_list != NULL);
165 }
166
167 /****************************************************************************
168    * Now queue an entry on the notify change list.
169    * We only need to save smb_size bytes from this incoming packet
170    * as we will always by returning a 'read the directory yourself'
171    * error.
172 ****************************************************************************/
173 BOOL change_notify_set(char *inbuf, files_struct *fsp, connection_struct *conn, uint32 flags)
174 {
175         struct change_notify *cnbp;
176
177         if((cnbp = (struct change_notify *)malloc(sizeof(*cnbp))) == NULL) {
178                 DEBUG(0,("call_nt_transact_notify_change: malloc fail !\n" ));
179                 return -1;
180         }
181
182         ZERO_STRUCTP(cnbp);
183
184         memcpy(cnbp->request_buf, inbuf, smb_size);
185         cnbp->fsp = fsp;
186         cnbp->conn = conn;
187         cnbp->flags = flags;
188         cnbp->change_data = cnotify->register_notify(conn, fsp->fsp_name, flags);
189         
190         if (!cnbp->change_data) {
191                 SAFE_FREE(cnbp);
192                 return False;
193         }
194
195         DLIST_ADD(change_notify_list, cnbp);
196
197         return True;
198 }
199
200
201 /****************************************************************************
202 initialise the change notify subsystem
203 ****************************************************************************/
204 BOOL init_change_notify(void)
205 {
206 #if HAVE_KERNEL_CHANGE_NOTIFY
207         cnotify = kernel_notify_init();
208 #endif
209         if (!cnotify) cnotify = hash_notify_init();
210         
211         if (!cnotify) {
212                 DEBUG(0,("Failed to init change notify system\n"));
213                 return False;
214         }
215
216         return True;
217 }