r22110: don't cache the S-1-22-{1,2} domain SID/uig/gid lookups in idmap_cache
[Samba.git] / source / nsswitch / idmap_cache.c
blob6e5febf2ba881ce2ef1b497ec0d64e4b2b99ced7
1 /*
2 Unix SMB/CIFS implementation.
3 ID Mapping Cache
5 based on gencache
7 Copyright (C) Simo Sorce 2006
8 Copyright (C) Rafal Szczesniak 2002
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/
24 #include "includes.h"
25 #include "winbindd.h"
27 #define TIMEOUT_LEN 12
28 #define IDMAP_CACHE_DATA_FMT "%12u/%s"
29 #define IDMAP_READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
31 struct idmap_cache_ctx {
32 TDB_CONTEXT *tdb;
35 static int idmap_cache_destructor(struct idmap_cache_ctx *cache)
37 int ret = 0;
39 if (cache && cache->tdb) {
40 ret = tdb_close(cache->tdb);
41 cache->tdb = NULL;
44 return ret;
47 struct idmap_cache_ctx *idmap_cache_init(TALLOC_CTX *memctx)
49 struct idmap_cache_ctx *cache;
50 char* cache_fname = NULL;
52 cache = talloc(memctx, struct idmap_cache_ctx);
53 if ( ! cache) {
54 DEBUG(0, ("Out of memory!\n"));
55 return NULL;
58 cache_fname = lock_path("idmap_cache.tdb");
60 DEBUG(10, ("Opening cache file at %s\n", cache_fname));
62 cache->tdb = tdb_open_log(cache_fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
64 if (!cache->tdb) {
65 DEBUG(5, ("Attempt to open %s has failed.\n", cache_fname));
66 return NULL;
69 talloc_set_destructor(cache, idmap_cache_destructor);
71 return cache;
74 void idmap_cache_shutdown(struct idmap_cache_ctx *cache)
76 talloc_free(cache);
79 NTSTATUS idmap_cache_build_sidkey(TALLOC_CTX *ctx, char **sidkey, const struct id_map *id)
81 *sidkey = talloc_asprintf(ctx, "IDMAP/SID/%s", sid_string_static(id->sid));
82 if ( ! *sidkey) {
83 DEBUG(1, ("failed to build sidkey, OOM?\n"));
84 return NT_STATUS_NO_MEMORY;
87 return NT_STATUS_OK;
90 NTSTATUS idmap_cache_build_idkey(TALLOC_CTX *ctx, char **idkey, const struct id_map *id)
92 *idkey = talloc_asprintf(ctx, "IDMAP/%s/%lu",
93 (id->xid.type==ID_TYPE_UID)?"UID":"GID",
94 (unsigned long)id->xid.id);
95 if ( ! *idkey) {
96 DEBUG(1, ("failed to build idkey, OOM?\n"));
97 return NT_STATUS_NO_MEMORY;
100 return NT_STATUS_OK;
103 NTSTATUS idmap_cache_set(struct idmap_cache_ctx *cache, const struct id_map *id)
105 NTSTATUS ret;
106 time_t timeout = time(NULL) + lp_idmap_cache_time();
107 TDB_DATA databuf;
108 char *sidkey;
109 char *idkey;
110 char *valstr;
112 /* Don't cache lookups in the S-1-22-{1,2} domain */
113 if ( (id->xid.type == ID_TYPE_UID) &&
114 sid_check_is_in_unix_users(id->sid) )
116 return NT_STATUS_OK;
118 if ( (id->xid.type == ID_TYPE_GID) &&
119 sid_check_is_in_unix_groups(id->sid) )
121 return NT_STATUS_OK;
125 ret = idmap_cache_build_sidkey(cache, &sidkey, id);
126 if (!NT_STATUS_IS_OK(ret)) return ret;
128 /* use sidkey as the local memory ctx */
129 ret = idmap_cache_build_idkey(sidkey, &idkey, id);
130 if (!NT_STATUS_IS_OK(ret)) {
131 goto done;
134 /* save SID -> ID */
136 /* use sidkey as the local memory ctx */
137 valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, idkey);
138 if (!valstr) {
139 DEBUG(0, ("Out of memory!\n"));
140 ret = NT_STATUS_NO_MEMORY;
141 goto done;
144 databuf = string_term_tdb_data(valstr);
145 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
146 " %s (%d seconds %s)\n", sidkey, valstr , ctime(&timeout),
147 (int)(timeout - time(NULL)),
148 timeout > time(NULL) ? "ahead" : "in the past"));
150 if (tdb_store_bystring(cache->tdb, sidkey, databuf, TDB_REPLACE) != 0) {
151 DEBUG(3, ("Failed to store cache entry!\n"));
152 ret = NT_STATUS_UNSUCCESSFUL;
153 goto done;
156 /* save ID -> SID */
158 /* use sidkey as the local memory ctx */
159 valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, sidkey);
160 if (!valstr) {
161 DEBUG(0, ("Out of memory!\n"));
162 ret = NT_STATUS_NO_MEMORY;
163 goto done;
166 databuf = string_term_tdb_data(valstr);
167 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
168 " %s (%d seconds %s)\n", idkey, valstr, ctime(&timeout),
169 (int)(timeout - time(NULL)),
170 timeout > time(NULL) ? "ahead" : "in the past"));
172 if (tdb_store_bystring(cache->tdb, idkey, databuf, TDB_REPLACE) != 0) {
173 DEBUG(3, ("Failed to store cache entry!\n"));
174 ret = NT_STATUS_UNSUCCESSFUL;
175 goto done;
178 ret = NT_STATUS_OK;
180 done:
181 talloc_free(sidkey);
182 return ret;
185 NTSTATUS idmap_cache_del(struct idmap_cache_ctx *cache, const struct id_map *id)
187 NTSTATUS ret;
188 char *sidkey = NULL;
189 char *idkey = NULL;
191 ret = idmap_cache_build_sidkey(cache, &sidkey, id);
192 if (!NT_STATUS_IS_OK(ret)) return ret;
194 ret = idmap_cache_build_idkey(cache, &idkey, id);
195 if (!NT_STATUS_IS_OK(ret)) {
196 goto done;
199 /* delete SID */
201 DEBUG(10, ("Deleting cache entry (key = %s)\n", sidkey));
203 if (tdb_delete_bystring(cache->tdb, sidkey) != 0) {
204 DEBUG(3, ("Failed to delete cache entry!\n"));
207 /* delete ID */
209 DEBUG(10, ("Deleting cache entry (key = %s)\n", idkey));
211 if (tdb_delete_bystring(cache->tdb, idkey) != 0) {
212 DEBUG(3, ("Failed to delete cache entry!\n"));
215 done:
216 talloc_free(sidkey);
217 talloc_free(idkey);
218 return ret;
221 NTSTATUS idmap_cache_set_negative_sid(struct idmap_cache_ctx *cache, const struct id_map *id)
223 NTSTATUS ret;
224 time_t timeout = time(NULL) + lp_idmap_negative_cache_time();
225 TDB_DATA databuf;
226 char *sidkey;
227 char *valstr;
229 ret = idmap_cache_build_sidkey(cache, &sidkey, id);
230 if (!NT_STATUS_IS_OK(ret)) return ret;
232 /* use sidkey as the local memory ctx */
233 valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, "IDMAP/NEGATIVE");
234 if (!valstr) {
235 DEBUG(0, ("Out of memory!\n"));
236 ret = NT_STATUS_NO_MEMORY;
237 goto done;
240 databuf = string_term_tdb_data(valstr);
241 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
242 " %s (%d seconds %s)\n", sidkey, valstr, ctime(&timeout),
243 (int)(timeout - time(NULL)),
244 timeout > time(NULL) ? "ahead" : "in the past"));
246 if (tdb_store_bystring(cache->tdb, sidkey, databuf, TDB_REPLACE) != 0) {
247 DEBUG(3, ("Failed to store cache entry!\n"));
248 ret = NT_STATUS_UNSUCCESSFUL;
249 goto done;
252 done:
253 talloc_free(sidkey);
254 return ret;
257 NTSTATUS idmap_cache_set_negative_id(struct idmap_cache_ctx *cache, const struct id_map *id)
259 NTSTATUS ret;
260 time_t timeout = time(NULL) + lp_idmap_negative_cache_time();
261 TDB_DATA databuf;
262 char *idkey;
263 char *valstr;
265 ret = idmap_cache_build_idkey(cache, &idkey, id);
266 if (!NT_STATUS_IS_OK(ret)) return ret;
268 /* use idkey as the local memory ctx */
269 valstr = talloc_asprintf(idkey, IDMAP_CACHE_DATA_FMT, (int)timeout, "IDMAP/NEGATIVE");
270 if (!valstr) {
271 DEBUG(0, ("Out of memory!\n"));
272 ret = NT_STATUS_NO_MEMORY;
273 goto done;
276 databuf = string_term_tdb_data(valstr);
277 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
278 " %s (%d seconds %s)\n", idkey, valstr, ctime(&timeout),
279 (int)(timeout - time(NULL)),
280 timeout > time(NULL) ? "ahead" : "in the past"));
282 if (tdb_store_bystring(cache->tdb, idkey, databuf, TDB_REPLACE) != 0) {
283 DEBUG(3, ("Failed to store cache entry!\n"));
284 ret = NT_STATUS_UNSUCCESSFUL;
285 goto done;
288 done:
289 talloc_free(idkey);
290 return ret;
293 NTSTATUS idmap_cache_fill_map(struct id_map *id, const char *value)
295 char *rem;
297 /* see if it is a sid */
298 if ( ! strncmp("IDMAP/SID/", value, 10)) {
300 if ( ! string_to_sid(id->sid, &value[10])) {
301 goto failed;
304 id->status = ID_MAPPED;
306 return NT_STATUS_OK;
309 /* not a SID see if it is an UID or a GID */
310 if ( ! strncmp("IDMAP/UID/", value, 10)) {
312 /* a uid */
313 id->xid.type = ID_TYPE_UID;
315 } else if ( ! strncmp("IDMAP/GID/", value, 10)) {
317 /* a gid */
318 id->xid.type = ID_TYPE_GID;
320 } else {
322 /* a completely bogus value bail out */
323 goto failed;
326 id->xid.id = strtol(&value[10], &rem, 0);
327 if (*rem != '\0') {
328 goto failed;
331 id->status = ID_MAPPED;
333 return NT_STATUS_OK;
335 failed:
336 DEBUG(1, ("invalid value: %s\n", value));
337 id->status = ID_UNKNOWN;
338 return NT_STATUS_INTERNAL_DB_CORRUPTION;
341 BOOL idmap_cache_is_negative(const char *val)
343 if ( ! strcmp("IDMAP/NEGATIVE", val)) {
344 return True;
346 return False;
349 /* search the cahce for the SID an return a mapping if found *
351 * 3 cases are possible
353 * 1 map found
354 * in this case id->status = ID_MAPPED and NT_STATUS_OK is returned
355 * 2 map not found
356 * in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned
357 * 3 negative cache found
358 * in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned
360 * As a special case if the cache is expired NT_STATUS_SYNCHRONIZATION_REQUIRED
361 * is returned instead of NT_STATUS_OK. In this case revalidation of the cache
362 * is needed.
365 NTSTATUS idmap_cache_map_sid(struct idmap_cache_ctx *cache, struct id_map *id)
367 NTSTATUS ret;
368 TDB_DATA databuf;
369 time_t t;
370 char *sidkey;
371 char *endptr;
373 /* make sure it is marked as not mapped by default */
374 id->status = ID_UNKNOWN;
376 ret = idmap_cache_build_sidkey(cache, &sidkey, id);
377 if (!NT_STATUS_IS_OK(ret)) return ret;
379 databuf = tdb_fetch_bystring(cache->tdb, sidkey);
381 if (databuf.dptr == NULL) {
382 DEBUG(10, ("Cache entry with key = %s couldn't be found\n", sidkey));
383 return NT_STATUS_NONE_MAPPED;
386 t = strtol((const char *)databuf.dptr, &endptr, 10);
388 if ((endptr == NULL) || (*endptr != '/')) {
389 DEBUG(2, ("Invalid gencache data format: %s\n", (const char *)databuf.dptr));
390 /* remove the entry */
391 tdb_delete_bystring(cache->tdb, sidkey);
392 ret = NT_STATUS_NONE_MAPPED;
393 goto done;
396 /* check it is not negative */
397 if (strcmp("IDMAP/NEGATIVE", endptr+1) != 0) {
399 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
400 "timeout = %s", t > time(NULL) ? "valid" :
401 "expired", sidkey, endptr+1, ctime(&t)));
403 /* this call if successful will also mark the entry as mapped */
404 ret = idmap_cache_fill_map(id, endptr+1);
405 if ( ! NT_STATUS_IS_OK(ret)) {
406 /* if not valid form delete the entry */
407 tdb_delete_bystring(cache->tdb, sidkey);
408 ret = NT_STATUS_NONE_MAPPED;
409 goto done;
412 /* here ret == NT_STATUS_OK and id->status = ID_MAPPED */
414 if (t <= time(NULL)) {
415 /* If we've been told to be offline - stay in
416 that state... */
417 if (lp_winbind_offline_logon() &&
418 get_global_winbindd_state_offline())
420 DEBUG(10,("idmap_cache_map_sid: winbindd is "
421 "globally offline.\n"));
422 } else {
423 /* We're expired, set an error code
424 for upper layer */
425 ret = NT_STATUS_SYNCHRONIZATION_REQUIRED;
428 } else {
429 if (t <= time(NULL)) {
430 /* If we've been told to be offline - stay in
431 that state... */
432 if (lp_winbind_offline_logon() &&
433 get_global_winbindd_state_offline())
435 DEBUG(10,("idmap_cache_map_sid: winbindd is "
436 "globally offline.\n"));
437 } else {
438 /* We're expired, delete the entry and return
439 not mapped */
440 tdb_delete_bystring(cache->tdb, sidkey);
441 ret = NT_STATUS_NONE_MAPPED;
443 } else {
444 /* this is not mapped as it was a negative cache hit */
445 id->status = ID_UNMAPPED;
446 ret = NT_STATUS_OK;
450 done:
451 SAFE_FREE(databuf.dptr);
452 talloc_free(sidkey);
453 return ret;
456 /* search the cahce for the ID an return a mapping if found *
458 * 3 cases are possible
460 * 1 map found
461 * in this case id->status = ID_MAPPED and NT_STATUS_OK is returned
462 * 2 map not found
463 * in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned
464 * 3 negative cache found
465 * in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned
467 * As a special case if the cache is expired NT_STATUS_SYNCHRONIZATION_REQUIRED
468 * is returned instead of NT_STATUS_OK. In this case revalidation of the cache
469 * is needed.
472 NTSTATUS idmap_cache_map_id(struct idmap_cache_ctx *cache, struct id_map *id)
474 NTSTATUS ret;
475 TDB_DATA databuf;
476 time_t t;
477 char *idkey;
478 char *endptr;
480 /* make sure it is marked as not mapped by default */
481 id->status = ID_UNKNOWN;
483 ret = idmap_cache_build_idkey(cache, &idkey, id);
484 if (!NT_STATUS_IS_OK(ret)) return ret;
486 databuf = tdb_fetch_bystring(cache->tdb, idkey);
488 if (databuf.dptr == NULL) {
489 DEBUG(10, ("Cache entry with key = %s couldn't be found\n", idkey));
490 return NT_STATUS_NONE_MAPPED;
493 t = strtol((const char *)databuf.dptr, &endptr, 10);
495 if ((endptr == NULL) || (*endptr != '/')) {
496 DEBUG(2, ("Invalid gencache data format: %s\n", (const char *)databuf.dptr));
497 /* remove the entry */
498 tdb_delete_bystring(cache->tdb, idkey);
499 ret = NT_STATUS_NONE_MAPPED;
500 goto done;
503 /* check it is not negative */
504 if (strcmp("IDMAP/NEGATIVE", endptr+1) != 0) {
506 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
507 "timeout = %s", t > time(NULL) ? "valid" :
508 "expired", idkey, endptr+1, ctime(&t)));
510 /* this call if successful will also mark the entry as mapped */
511 ret = idmap_cache_fill_map(id, endptr+1);
512 if ( ! NT_STATUS_IS_OK(ret)) {
513 /* if not valid form delete the entry */
514 tdb_delete_bystring(cache->tdb, idkey);
515 ret = NT_STATUS_NONE_MAPPED;
516 goto done;
519 /* here ret == NT_STATUS_OK and id->mapped = True */
521 if (t <= time(NULL)) {
522 /* If we've been told to be offline - stay in
523 that state... */
524 if (lp_winbind_offline_logon() &&
525 get_global_winbindd_state_offline())
527 DEBUG(10,("idmap_cache_map_sid: winbindd is "
528 "globally offline.\n"));
529 } else {
530 /* We're expired, set an error code
531 for upper layer */
532 ret = NT_STATUS_SYNCHRONIZATION_REQUIRED;
535 } else {
536 if (t <= time(NULL)) {
537 /* If we've been told to be offline - stay in
538 that state... */
539 if (lp_winbind_offline_logon() &&
540 get_global_winbindd_state_offline())
542 DEBUG(10,("idmap_cache_map_sid: winbindd is "
543 "globally offline.\n"));
544 } else {
545 /* We're expired, delete the entry and
546 return not mapped */
547 tdb_delete_bystring(cache->tdb, idkey);
548 ret = NT_STATUS_NONE_MAPPED;
550 } else {
551 /* this is not mapped is it was a negative cache hit */
552 id->status = ID_UNMAPPED;
553 ret = NT_STATUS_OK;
556 done:
557 SAFE_FREE(databuf.dptr);
558 talloc_free(idkey);
559 return ret;