Removed version number from file header.
[tprouty/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 static void change_notify_reply_packet(char *inbuf, NTSTATUS error_code)
48 {
49         char outbuf[smb_size+38];
50
51         memset(outbuf, '\0', sizeof(outbuf));
52         construct_reply_common(inbuf, outbuf);
53
54         ERROR_NT(error_code);
55
56         /*
57          * Seems NT needs a transact command with an error code
58          * in it. This is a longer packet than a simple error.
59          */
60         set_message(outbuf,18,0,False);
61
62         if (!send_smb(smbd_server_fd(),outbuf))
63                 exit_server("change_notify_reply_packet: send_smb failed.");
64 }
65
66 /****************************************************************************
67  Remove an entry from the list and free it, also closing any
68  directory handle if necessary.
69 *****************************************************************************/
70
71 static void change_notify_remove(struct change_notify *cnbp)
72 {
73         cnotify->remove_notify(cnbp->change_data);
74         DLIST_REMOVE(change_notify_list, cnbp);
75         ZERO_STRUCTP(cnbp);
76         SAFE_FREE(cnbp);
77 }
78
79 /****************************************************************************
80  Delete entries by fnum from the change notify pending queue.
81 *****************************************************************************/
82
83 void remove_pending_change_notify_requests_by_fid(files_struct *fsp)
84 {
85         struct change_notify *cnbp, *next;
86
87         for (cnbp=change_notify_list; cnbp; cnbp=next) {
88                 next=cnbp->next;
89                 if (cnbp->fsp->fnum == fsp->fnum) {
90                         change_notify_remove(cnbp);
91                 }
92         }
93 }
94
95 /****************************************************************************
96  Delete entries by mid from the change notify pending queue. Always send reply.
97 *****************************************************************************/
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
117 void remove_pending_change_notify_requests_by_filename(files_struct *fsp)
118 {
119         struct change_notify *cnbp, *next;
120
121         for (cnbp=change_notify_list; cnbp; cnbp=next) {
122                 next=cnbp->next;
123                 /*
124                  * We know it refers to the same directory if the connection number and
125                  * the filename are identical.
126                  */
127                 if((cnbp->fsp->conn == fsp->conn) && strequal(cnbp->fsp->fsp_name,fsp->fsp_name)) {
128                         change_notify_reply_packet(cnbp->request_buf,NT_STATUS_CANCELLED);
129                         change_notify_remove(cnbp);
130                 }
131         }
132 }
133
134 /****************************************************************************
135  Return true if there are pending change notifies.
136 ****************************************************************************/
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
149 BOOL process_pending_change_notify_queue(time_t t)
150 {
151         struct change_notify *cnbp, *next;
152         uint16 vuid;
153
154         for (cnbp=change_notify_list; cnbp; cnbp=next) {
155                 next=cnbp->next;
156
157                 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID : SVAL(cnbp->request_buf,smb_uid);
158
159                 if (cnotify->check_notify(cnbp->conn, vuid, cnbp->fsp->fsp_name, cnbp->flags, cnbp->change_data, t)) {
160                         DEBUG(10,("process_pending_change_notify_queue: dir %s changed !\n", cnbp->fsp->fsp_name ));
161                         change_notify_reply_packet(cnbp->request_buf,STATUS_NOTIFY_ENUM_DIR);
162                         change_notify_remove(cnbp);
163                 }
164         }
165
166         return (change_notify_list != NULL);
167 }
168
169 /****************************************************************************
170  Now queue an entry on the notify change list.
171  We only need to save smb_size bytes from this incoming packet
172  as we will always by returning a 'read the directory yourself'
173  error.
174 ****************************************************************************/
175
176 BOOL change_notify_set(char *inbuf, files_struct *fsp, connection_struct *conn, uint32 flags)
177 {
178         struct change_notify *cnbp;
179
180         if((cnbp = (struct change_notify *)malloc(sizeof(*cnbp))) == NULL) {
181                 DEBUG(0,("call_nt_transact_notify_change: malloc fail !\n" ));
182                 return -1;
183         }
184
185         ZERO_STRUCTP(cnbp);
186
187         memcpy(cnbp->request_buf, inbuf, smb_size);
188         cnbp->fsp = fsp;
189         cnbp->conn = conn;
190         cnbp->flags = flags;
191         cnbp->change_data = cnotify->register_notify(conn, fsp->fsp_name, flags);
192         
193         if (!cnbp->change_data) {
194                 SAFE_FREE(cnbp);
195                 return False;
196         }
197
198         DLIST_ADD(change_notify_list, cnbp);
199
200         return True;
201 }
202
203 /****************************************************************************
204  Initialise the change notify subsystem.
205 ****************************************************************************/
206
207 BOOL init_change_notify(void)
208 {
209 #if HAVE_KERNEL_CHANGE_NOTIFY
210         cnotify = kernel_notify_init();
211 #endif
212         if (!cnotify) cnotify = hash_notify_init();
213         
214         if (!cnotify) {
215                 DEBUG(0,("Failed to init change notify system\n"));
216                 return False;
217         }
218
219         return True;
220 }