if we are adding a new sambaAccount, make sure that we add a
[Samba.git] / source / smbd / notify_hash.c
blob90eb88ac81448f5857140abc7db8de0ee68be8e0
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 change notify handling - hash based implementation
5 Copyright (C) Jeremy Allison 1994-1998
6 Copyright (C) Andrew Tridgell 2000
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.
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.
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.
23 #include "includes.h"
25 struct change_data {
26 time_t last_check_time; /* time we last checked this entry */
27 time_t modify_time; /* Info from the directory we're monitoring. */
28 time_t status_time; /* Info from the directory we're monitoring. */
29 time_t total_time; /* Total time of all directory entries - don't care if it wraps. */
30 unsigned int num_entries; /* Zero or the number of files in the directory. */
31 unsigned int mode_sum;
32 unsigned char name_hash[16];
35 /****************************************************************************
36 Create the hash we will use to determine if the contents changed.
37 *****************************************************************************/
39 static BOOL notify_hash(connection_struct *conn, char *path, uint32 flags,
40 struct change_data *data, struct change_data *old_data)
42 SMB_STRUCT_STAT st;
43 pstring full_name;
44 char *p;
45 char *fname;
46 size_t remaining_len;
47 size_t fullname_len;
48 void *dp;
50 ZERO_STRUCTP(data);
52 if(vfs_stat(conn,path, &st) == -1)
53 return False;
55 data->modify_time = st.st_mtime;
56 data->status_time = st.st_ctime;
58 if (old_data) {
60 * Shortcut to avoid directory scan if the time
61 * has changed - we always must return true then.
63 if (old_data->modify_time != data->modify_time ||
64 old_data->status_time != data->status_time ) {
65 return True;
70 * If we are to watch for changes that are only stored
71 * in inodes of files, not in the directory inode, we must
72 * scan the directory and produce a unique identifier with
73 * which we can determine if anything changed. We use the
74 * modify and change times from all the files in the
75 * directory, added together (ignoring wrapping if it's
76 * larger than the max time_t value).
79 dp = OpenDir(conn, path, True);
80 if (dp == NULL)
81 return False;
83 data->num_entries = 0;
85 pstrcpy(full_name, path);
86 pstrcat(full_name, "/");
88 fullname_len = strlen(full_name);
89 remaining_len = sizeof(full_name) - fullname_len - 1;
90 p = &full_name[fullname_len];
92 while ((fname = ReadDirName(dp))) {
93 if(strequal(fname, ".") || strequal(fname, ".."))
94 continue;
96 data->num_entries++;
97 safe_strcpy(p, fname, remaining_len);
99 ZERO_STRUCT(st);
102 * Do the stat - but ignore errors.
104 vfs_stat(conn,full_name, &st);
107 * Always sum the times.
110 data->total_time += (st.st_mtime + st.st_ctime);
113 * If requested hash the names.
116 if (flags & (FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_FILE)) {
117 int i;
118 unsigned char tmp_hash[16];
119 mdfour(tmp_hash, (unsigned char *)fname, strlen(fname));
120 for (i=0;i<16;i++)
121 data->name_hash[i] ^= tmp_hash[i];
125 * If requested sum the mode_t's.
128 if (flags & (FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_SECURITY))
129 data->mode_sum = st.st_mode;
132 CloseDir(dp);
134 return True;
137 /****************************************************************************
138 Register a change notify request.
139 *****************************************************************************/
141 static void *hash_register_notify(connection_struct *conn, char *path, uint32 flags)
143 struct change_data data;
145 if (!notify_hash(conn, path, flags, &data, NULL))
146 return NULL;
148 data.last_check_time = time(NULL);
150 return (void *)memdup(&data, sizeof(data));
153 /****************************************************************************
154 Check if a change notify should be issued.
155 A time of zero means instantaneous check - don't modify the last check time.
156 *****************************************************************************/
158 static BOOL hash_check_notify(connection_struct *conn, uint16 vuid, char *path, uint32 flags, void *datap, time_t t)
160 struct change_data *data = (struct change_data *)datap;
161 struct change_data data2;
163 if (t && t < data->last_check_time + lp_change_notify_timeout())
164 return False;
166 if (!change_to_user(conn,vuid))
167 return True;
168 if (!set_current_service(conn,True)) {
169 change_to_root_user();
170 return True;
173 if (!notify_hash(conn, path, flags, &data2, data) ||
174 data2.modify_time != data->modify_time ||
175 data2.status_time != data->status_time ||
176 data2.total_time != data->total_time ||
177 data2.num_entries != data->num_entries ||
178 data2.mode_sum != data->mode_sum ||
179 memcmp(data2.name_hash, data->name_hash, sizeof(data2.name_hash))) {
180 change_to_root_user();
181 return True;
184 if (t)
185 data->last_check_time = t;
187 change_to_root_user();
189 return False;
192 /****************************************************************************
193 Remove a change notify data structure.
194 *****************************************************************************/
196 static void hash_remove_notify(void *datap)
198 SAFE_FREE(datap);
201 /****************************************************************************
202 Setup hash based change notify.
203 ****************************************************************************/
205 struct cnotify_fns *hash_notify_init(void)
207 static struct cnotify_fns cnotify;
209 cnotify.register_notify = hash_register_notify;
210 cnotify.check_notify = hash_check_notify;
211 cnotify.remove_notify = hash_remove_notify;
212 cnotify.select_time = lp_change_notify_timeout();
214 return &cnotify;
218 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
219 change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_NOTIFY_ENUM_DIR);
221 chain_size = 0;
222 file_chain_reset();
224 uint16 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
225 SVAL(cnbp->request_buf,smb_uid);