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