s4:dsdb:tests: Also pass tests if asserted identity is present
[Samba.git] / source3 / smbd / statcache.c
blob4138a9287ada93275220afb9e9fb39f18581082d
1 /*
2 Unix SMB/CIFS implementation.
3 stat cache code
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Jeremy Allison 1999-2007
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
7 Copyright (C) Volker Lendecke 2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "../lib/util/memcache.h"
25 #include "smbd/smbd.h"
26 #include "messages.h"
27 #include "serverid.h"
28 #include "smbprofile.h"
29 #include <tdb.h>
31 #define STAT_CACHE_TWRP_TOKEN "%016" PRIx64 "@%s"
32 #define STAT_CACHE_TWRP_TOKEN_LEN 17
34 /****************************************************************************
35 Stat cache code used in unix_convert.
36 *****************************************************************************/
38 /**
39 * Add an entry into the stat cache.
41 * @param full_orig_name The original name as specified by the client
42 * @param orig_translated_path The name on our filesystem.
44 * @note Only the first strlen(orig_translated_path) characters are stored
45 * into the cache. This means that full_orig_name will be internally
46 * truncated.
50 void stat_cache_add( const char *full_orig_name,
51 const char *translated_path_in,
52 NTTIME twrp,
53 bool case_sensitive)
55 size_t translated_path_length;
56 char *translated_path = NULL;
57 char *original_path;
58 size_t original_path_length;
59 TALLOC_CTX *ctx = talloc_tos();
61 if (!lp_stat_cache()) {
62 return;
66 * Don't cache trivial valid directory entries such as . and ..
69 if ((*full_orig_name == '\0')
70 || ISDOT(full_orig_name) || ISDOTDOT(full_orig_name)) {
71 return;
74 translated_path = talloc_asprintf(ctx,
75 STAT_CACHE_TWRP_TOKEN,
76 twrp,
77 translated_path_in);
78 if (translated_path == NULL) {
79 return;
83 * If we are in case insentive mode, we don't need to
84 * store names that need no translation - else, it
85 * would be a waste.
88 if (!case_sensitive && (strcmp(full_orig_name, translated_path) == 0)) {
89 TALLOC_FREE(translated_path);
90 return;
94 * Remove any trailing '/' characters from the
95 * translated path.
98 translated_path_length = strlen(translated_path);
100 if(translated_path[translated_path_length-1] == '/') {
101 translated_path_length--;
104 if(case_sensitive) {
105 original_path = talloc_asprintf(ctx,
106 STAT_CACHE_TWRP_TOKEN,
107 twrp,
108 full_orig_name);
109 } else {
110 char *upper = NULL;
112 upper = talloc_strdup_upper(ctx, full_orig_name);
113 if (upper == NULL) {
114 TALLOC_FREE(translated_path);
115 return;
117 original_path = talloc_asprintf(ctx,
118 STAT_CACHE_TWRP_TOKEN,
119 twrp,
120 upper);
121 TALLOC_FREE(upper);
124 if (!original_path) {
125 TALLOC_FREE(translated_path);
126 return;
129 original_path_length = strlen(original_path);
131 if(original_path[original_path_length-1] == '/') {
132 original_path[original_path_length-1] = '\0';
133 original_path_length--;
136 if (original_path_length != translated_path_length) {
137 if (original_path_length < translated_path_length) {
138 DEBUG(0, ("OOPS - tried to store stat cache entry "
139 "for weird length paths [%s] %lu and [%s] %lu)!\n",
140 original_path,
141 (unsigned long)original_path_length,
142 translated_path,
143 (unsigned long)translated_path_length));
144 TALLOC_FREE(original_path);
145 TALLOC_FREE(translated_path);
146 return;
149 /* we only want to index by the first part of original_path,
150 up to the length of translated_path */
152 original_path[translated_path_length] = '\0';
153 original_path_length = translated_path_length;
156 /* Ensure we're null terminated. */
157 translated_path[translated_path_length] = '\0';
160 * New entry or replace old entry.
163 memcache_add(
164 smbd_memcache(), STAT_CACHE,
165 data_blob_const(original_path, original_path_length),
166 data_blob_const(translated_path, translated_path_length + 1));
168 DEBUG(5,("stat_cache_add: Added entry (%lx:size %x) %s -> %s\n",
169 (unsigned long)translated_path,
170 (unsigned int)translated_path_length,
171 original_path,
172 translated_path));
174 TALLOC_FREE(original_path);
175 TALLOC_FREE(translated_path);
179 * Look through the stat cache for an entry
181 * @param conn A connection struct to do the stat() with.
182 * @param posix_paths Whether to lookup using stat() or lstat()
183 * @param name The path we are attempting to cache, modified by this routine
184 * to be correct as far as the cache can tell us. We assume that
185 * it is a talloc'ed string from top of stack, we free it if
186 * necessary.
187 * @param dirpath The path as far as the stat cache told us. Also talloced
188 * from top of stack.
189 * @param start A pointer into name, for where to 'start' in fixing the rest
190 * of the name up.
191 * @param psd A stat buffer, NOT from the cache, but just a side-effect.
193 * @return True if we translated (and did a successful stat on) the entire
194 * name.
198 bool stat_cache_lookup(connection_struct *conn,
199 char **pp_name,
200 char **pp_dirpath,
201 char **pp_start,
202 NTTIME twrp,
203 SMB_STRUCT_STAT *pst)
205 char *chk_name;
206 size_t namelen;
207 bool sizechanged = False;
208 unsigned int num_components = 0;
209 char *translated_path;
210 size_t translated_path_length;
211 DATA_BLOB data_val;
212 char *name;
213 TALLOC_CTX *ctx = talloc_tos();
214 struct smb_filename smb_fname;
215 int ret;
217 *pp_dirpath = NULL;
218 *pp_start = *pp_name;
220 if (!lp_stat_cache()) {
221 return False;
224 name = *pp_name;
225 namelen = strlen(name);
227 DO_PROFILE_INC(statcache_lookups);
230 * Don't lookup trivial valid directory entries.
232 if ((*name == '\0') || ISDOT(name) || ISDOTDOT(name)) {
233 return False;
236 if (conn->case_sensitive) {
237 chk_name = talloc_asprintf(ctx,
238 STAT_CACHE_TWRP_TOKEN,
239 twrp,
240 name);
241 if (!chk_name) {
242 DEBUG(0, ("stat_cache_lookup: strdup failed!\n"));
243 return False;
246 } else {
247 char *upper = NULL;
249 upper = talloc_strdup_upper(ctx,name);
250 if (upper == NULL) {
251 DBG_ERR("talloc_strdup_upper failed!\n");
252 return False;
254 chk_name = talloc_asprintf(ctx,
255 STAT_CACHE_TWRP_TOKEN,
256 twrp,
257 upper);
258 if (!chk_name) {
259 DEBUG(0, ("stat_cache_lookup: talloc_strdup_upper failed!\n"));
260 return False;
264 * In some language encodings the length changes
265 * if we uppercase. We need to treat this differently
266 * below.
268 if (strlen(chk_name) != namelen) {
269 sizechanged = True;
273 while (1) {
274 char *sp;
276 data_val = data_blob_null;
278 if (memcache_lookup(
279 smbd_memcache(), STAT_CACHE,
280 data_blob_const(chk_name, strlen(chk_name)),
281 &data_val)) {
282 break;
285 DEBUG(10,("stat_cache_lookup: lookup failed for name [%s]\n",
286 chk_name ));
288 * Didn't find it - remove last component for next try.
290 if (!(sp = strrchr_m(chk_name, '/'))) {
292 * We reached the end of the name - no match.
294 DO_PROFILE_INC(statcache_misses);
295 TALLOC_FREE(chk_name);
296 return False;
299 *sp = '\0';
302 * Count the number of times we have done this, we'll
303 * need it when reconstructing the string.
305 num_components++;
307 if ((*chk_name == '\0')
308 || ISDOT(chk_name) || ISDOTDOT(chk_name)) {
309 DO_PROFILE_INC(statcache_misses);
310 TALLOC_FREE(chk_name);
311 return False;
315 SMB_ASSERT(data_val.length >= STAT_CACHE_TWRP_TOKEN_LEN);
317 translated_path = talloc_strdup(
318 ctx,(char *)data_val.data + STAT_CACHE_TWRP_TOKEN_LEN);
319 if (!translated_path) {
320 smb_panic("talloc failed");
322 translated_path_length = data_val.length - 1 - STAT_CACHE_TWRP_TOKEN_LEN;
324 DEBUG(10,("stat_cache_lookup: lookup succeeded for name [%s] "
325 "-> [%s]\n", chk_name, translated_path ));
326 DO_PROFILE_INC(statcache_hits);
328 smb_fname = (struct smb_filename) {
329 .base_name = translated_path,
330 .twrp = twrp,
333 ret = vfs_stat(conn, &smb_fname);
334 if (ret != 0) {
335 /* Discard this entry - it doesn't exist in the filesystem. */
336 memcache_delete(smbd_memcache(), STAT_CACHE,
337 data_blob_const(chk_name, strlen(chk_name)));
338 TALLOC_FREE(chk_name);
339 TALLOC_FREE(translated_path);
340 return False;
343 * Only copy the stat struct back if we actually hit the full path
345 if (num_components == 0) {
346 *pst = smb_fname.st;
349 if (!sizechanged) {
350 memcpy(*pp_name, translated_path,
351 MIN(namelen, translated_path_length));
352 } else {
353 if (num_components == 0) {
354 name = talloc_strndup(ctx, translated_path,
355 translated_path_length);
356 } else {
357 char *sp;
359 sp = strnrchr_m(name, '/', num_components);
360 if (sp) {
361 name = talloc_asprintf(ctx,"%.*s%s",
362 (int)translated_path_length,
363 translated_path, sp);
364 } else {
365 name = talloc_strndup(ctx,
366 translated_path,
367 translated_path_length);
370 if (name == NULL) {
372 * TODO: Get us out of here with a real error message
374 smb_panic("talloc failed");
376 TALLOC_FREE(*pp_name);
377 *pp_name = name;
381 /* set pointer for 'where to start' on fixing the rest of the name */
382 *pp_start = &name[translated_path_length];
383 if (**pp_start == '/') {
384 ++*pp_start;
387 *pp_dirpath = translated_path;
388 TALLOC_FREE(chk_name);
389 return (namelen == translated_path_length);
392 /***************************************************************************
393 Tell all smbd's to delete an entry.
394 **************************************************************************/
396 void smbd_send_stat_cache_delete_message(struct messaging_context *msg_ctx,
397 const char *name)
399 #ifdef DEVELOPER
400 messaging_send_all(msg_ctx,
401 MSG_SMB_STAT_CACHE_DELETE,
402 name,
403 strlen(name)+1);
404 #endif
407 /***************************************************************************
408 Delete an entry.
409 **************************************************************************/
411 void stat_cache_delete(const char *name)
413 char *upper = talloc_strdup_upper(talloc_tos(), name);
414 char *lname = NULL;
416 if (upper == NULL) {
417 return;
420 lname = talloc_asprintf(talloc_tos(),
421 STAT_CACHE_TWRP_TOKEN,
422 (uint64_t)0,
423 upper);
424 TALLOC_FREE(upper);
425 if (lname == NULL) {
426 return;
428 DEBUG(10,("stat_cache_delete: deleting name [%s] -> %s\n",
429 lname, name ));
431 memcache_delete(smbd_memcache(), STAT_CACHE,
432 data_blob_const(lname, talloc_get_size(lname)-1));
433 TALLOC_FREE(lname);
436 /***************************************************************************
437 Initializes or clears the stat cache.
438 **************************************************************************/
440 bool reset_stat_cache( void )
442 if (!lp_stat_cache())
443 return True;
445 memcache_flush(smbd_memcache(), STAT_CACHE);
447 return True;