r25076: Checking in patches for 3.0.26
[Samba.git] / source / nsswitch / idmap_cache.c
blob037b1cb1e81a2e71014ba1c7363b7ff48a386cce
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 keybuf, 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 keybuf.dptr = sidkey;
145 keybuf.dsize = strlen(sidkey)+1;
146 databuf.dptr = valstr;
147 databuf.dsize = strlen(valstr)+1;
148 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
149 " %s (%d seconds %s)\n", keybuf.dptr, valstr , ctime(&timeout),
150 (int)(timeout - time(NULL)),
151 timeout > time(NULL) ? "ahead" : "in the past"));
153 if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) {
154 DEBUG(3, ("Failed to store cache entry!\n"));
155 ret = NT_STATUS_UNSUCCESSFUL;
156 goto done;
159 /* save ID -> SID */
161 /* use sidkey as the local memory ctx */
162 valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, sidkey);
163 if (!valstr) {
164 DEBUG(0, ("Out of memory!\n"));
165 ret = NT_STATUS_NO_MEMORY;
166 goto done;
169 keybuf.dptr = idkey;
170 keybuf.dsize = strlen(idkey)+1;
171 databuf.dptr = valstr;
172 databuf.dsize = strlen(valstr)+1;
173 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
174 " %s (%d seconds %s)\n", keybuf.dptr, valstr, ctime(&timeout),
175 (int)(timeout - time(NULL)),
176 timeout > time(NULL) ? "ahead" : "in the past"));
178 if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) {
179 DEBUG(3, ("Failed to store cache entry!\n"));
180 ret = NT_STATUS_UNSUCCESSFUL;
181 goto done;
184 ret = NT_STATUS_OK;
186 done:
187 talloc_free(sidkey);
188 return ret;
191 NTSTATUS idmap_cache_del(struct idmap_cache_ctx *cache, const struct id_map *id)
193 NTSTATUS ret;
194 TDB_DATA keybuf;
195 char *sidkey = NULL;
196 char *idkey = NULL;
198 ret = idmap_cache_build_sidkey(cache, &sidkey, id);
199 if (!NT_STATUS_IS_OK(ret)) return ret;
201 ret = idmap_cache_build_idkey(cache, &idkey, id);
202 if (!NT_STATUS_IS_OK(ret)) {
203 goto done;
206 /* delete SID */
208 keybuf.dptr = sidkey;
209 keybuf.dsize = strlen(sidkey)+1;
210 DEBUG(10, ("Deleting cache entry (key = %s)\n", keybuf.dptr));
212 if (tdb_delete(cache->tdb, keybuf) != 0) {
213 DEBUG(3, ("Failed to delete cache entry!\n"));
216 /* delete ID */
218 keybuf.dptr = idkey;
219 keybuf.dsize = strlen(idkey)+1;
220 DEBUG(10, ("Deleting cache entry (key = %s)\n", keybuf.dptr));
222 if (tdb_delete(cache->tdb, keybuf) != 0) {
223 DEBUG(3, ("Failed to delete cache entry!\n"));
226 done:
227 talloc_free(sidkey);
228 talloc_free(idkey);
229 return ret;
232 NTSTATUS idmap_cache_set_negative_sid(struct idmap_cache_ctx *cache, const struct id_map *id)
234 NTSTATUS ret;
235 time_t timeout = time(NULL) + lp_idmap_negative_cache_time();
236 TDB_DATA keybuf, databuf;
237 char *sidkey;
238 char *valstr;
240 ret = idmap_cache_build_sidkey(cache, &sidkey, id);
241 if (!NT_STATUS_IS_OK(ret)) return ret;
243 /* use sidkey as the local memory ctx */
244 valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, "IDMAP/NEGATIVE");
245 if (!valstr) {
246 DEBUG(0, ("Out of memory!\n"));
247 ret = NT_STATUS_NO_MEMORY;
248 goto done;
251 keybuf.dptr = sidkey;
252 keybuf.dsize = strlen(sidkey)+1;
253 databuf.dptr = valstr;
254 databuf.dsize = strlen(valstr)+1;
255 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
256 " %s (%d seconds %s)\n", keybuf.dptr, valstr, ctime(&timeout),
257 (int)(timeout - time(NULL)),
258 timeout > time(NULL) ? "ahead" : "in the past"));
260 if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) {
261 DEBUG(3, ("Failed to store cache entry!\n"));
262 ret = NT_STATUS_UNSUCCESSFUL;
263 goto done;
266 done:
267 talloc_free(sidkey);
268 return ret;
271 NTSTATUS idmap_cache_set_negative_id(struct idmap_cache_ctx *cache, const struct id_map *id)
273 NTSTATUS ret;
274 time_t timeout = time(NULL) + lp_idmap_negative_cache_time();
275 TDB_DATA keybuf, databuf;
276 char *idkey;
277 char *valstr;
279 ret = idmap_cache_build_idkey(cache, &idkey, id);
280 if (!NT_STATUS_IS_OK(ret)) return ret;
282 /* use idkey as the local memory ctx */
283 valstr = talloc_asprintf(idkey, IDMAP_CACHE_DATA_FMT, (int)timeout, "IDMAP/NEGATIVE");
284 if (!valstr) {
285 DEBUG(0, ("Out of memory!\n"));
286 ret = NT_STATUS_NO_MEMORY;
287 goto done;
290 keybuf.dptr = idkey;
291 keybuf.dsize = strlen(idkey)+1;
292 databuf.dptr = valstr;
293 databuf.dsize = strlen(valstr)+1;
294 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
295 " %s (%d seconds %s)\n", keybuf.dptr, valstr, ctime(&timeout),
296 (int)(timeout - time(NULL)),
297 timeout > time(NULL) ? "ahead" : "in the past"));
299 if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) {
300 DEBUG(3, ("Failed to store cache entry!\n"));
301 ret = NT_STATUS_UNSUCCESSFUL;
302 goto done;
305 done:
306 talloc_free(idkey);
307 return ret;
310 NTSTATUS idmap_cache_fill_map(struct id_map *id, const char *value)
312 char *rem;
314 /* see if it is a sid */
315 if ( ! strncmp("IDMAP/SID/", value, 10)) {
317 if ( ! string_to_sid(id->sid, &value[10])) {
318 goto failed;
321 id->status = ID_MAPPED;
323 return NT_STATUS_OK;
326 /* not a SID see if it is an UID or a GID */
327 if ( ! strncmp("IDMAP/UID/", value, 10)) {
329 /* a uid */
330 id->xid.type = ID_TYPE_UID;
332 } else if ( ! strncmp("IDMAP/GID/", value, 10)) {
334 /* a gid */
335 id->xid.type = ID_TYPE_GID;
337 } else {
339 /* a completely bogus value bail out */
340 goto failed;
343 id->xid.id = strtol(&value[10], &rem, 0);
344 if (*rem != '\0') {
345 goto failed;
348 id->status = ID_MAPPED;
350 return NT_STATUS_OK;
352 failed:
353 DEBUG(1, ("invalid value: %s\n", value));
354 id->status = ID_UNKNOWN;
355 return NT_STATUS_INTERNAL_DB_CORRUPTION;
358 BOOL idmap_cache_is_negative(const char *val)
360 if ( ! strcmp("IDMAP/NEGATIVE", val)) {
361 return True;
363 return False;
366 /* search the cahce for the SID an return a mapping if found *
368 * 4 cases are possible
370 * 1 map found
371 * in this case id->status = ID_MAPPED and NT_STATUS_OK is returned
372 * 2 map not found
373 * in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned
374 * 3 negative cache found
375 * in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned
376 * 4 map found but timer expired
377 * in this case id->status = ID_EXPIRED and NT_STATUS_SYNCHRONIZATION_REQUIRED
378 * is returned. In this case revalidation of the cache is needed.
381 NTSTATUS idmap_cache_map_sid(struct idmap_cache_ctx *cache, struct id_map *id)
383 NTSTATUS ret;
384 TDB_DATA keybuf, databuf;
385 time_t t, now;
386 char *sidkey;
387 char *endptr;
389 /* make sure it is marked as unknown by default */
390 id->status = ID_UNKNOWN;
392 ret = idmap_cache_build_sidkey(cache, &sidkey, id);
393 if (!NT_STATUS_IS_OK(ret)) return ret;
395 keybuf.dptr = sidkey;
396 keybuf.dsize = strlen(sidkey)+1;
398 databuf = tdb_fetch(cache->tdb, keybuf);
400 if (databuf.dptr == NULL) {
401 DEBUG(10, ("Cache entry with key = %s couldn't be found\n", sidkey));
402 return NT_STATUS_NONE_MAPPED;
405 t = strtol(databuf.dptr, &endptr, 10);
407 if ((endptr == NULL) || (*endptr != '/')) {
408 DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr));
409 /* remove the entry */
410 tdb_delete(cache->tdb, keybuf);
411 ret = NT_STATUS_NONE_MAPPED;
412 goto done;
415 now = time(NULL);
417 /* check it is not negative */
418 if (strcmp("IDMAP/NEGATIVE", endptr+1) != 0) {
420 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
421 "timeout = %s", t > now ? "valid" :
422 "expired", sidkey, endptr+1, ctime(&t)));
424 /* this call if successful will also mark the entry as mapped */
425 ret = idmap_cache_fill_map(id, endptr+1);
426 if ( ! NT_STATUS_IS_OK(ret)) {
427 /* if not valid form delete the entry */
428 tdb_delete(cache->tdb, keybuf);
429 ret = NT_STATUS_NONE_MAPPED;
430 goto done;
433 /* here ret == NT_STATUS_OK and id->status = ID_MAPPED */
435 if (t <= now) {
437 /* we have it, but it is expired */
438 id->status = ID_EXPIRED;
440 /* We're expired, set an error code
441 for upper layer */
442 ret = NT_STATUS_SYNCHRONIZATION_REQUIRED;
444 } else {
445 if (t <= now) {
446 /* We're expired, delete the NEGATIVE entry and return
447 not mapped */
448 tdb_delete(cache->tdb, keybuf);
449 ret = NT_STATUS_NONE_MAPPED;
450 } else {
451 /* this is not mapped as it was a negative cache hit */
452 id->status = ID_UNMAPPED;
453 ret = NT_STATUS_OK;
457 done:
458 SAFE_FREE(databuf.dptr);
459 talloc_free(sidkey);
460 return ret;
463 /* search the cahce for the ID an return a mapping if found *
465 * 3 cases are possible
467 * 1 map found
468 * in this case id->status = ID_MAPPED and NT_STATUS_OK is returned
469 * 2 map not found
470 * in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned
471 * 3 negative cache found
472 * in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned
473 * 4 map found but timer expired
474 * in this case id->status = ID_EXPIRED and NT_STATUS_SYNCHRONIZATION_REQUIRED
475 * is returned. In this case revalidation of the cache is needed.
478 NTSTATUS idmap_cache_map_id(struct idmap_cache_ctx *cache, struct id_map *id)
480 NTSTATUS ret;
481 TDB_DATA keybuf, databuf;
482 time_t t, now;
483 char *idkey;
484 char *endptr;
486 /* make sure it is marked as unknown by default */
487 id->status = ID_UNKNOWN;
489 ret = idmap_cache_build_idkey(cache, &idkey, id);
490 if (!NT_STATUS_IS_OK(ret)) return ret;
492 keybuf.dptr = idkey;
493 keybuf.dsize = strlen(idkey)+1;
495 databuf = tdb_fetch(cache->tdb, keybuf);
497 if (databuf.dptr == NULL) {
498 DEBUG(10, ("Cache entry with key = %s couldn't be found\n", idkey));
499 return NT_STATUS_NONE_MAPPED;
502 t = strtol(databuf.dptr, &endptr, 10);
504 if ((endptr == NULL) || (*endptr != '/')) {
505 DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr));
506 /* remove the entry */
507 tdb_delete(cache->tdb, keybuf);
508 ret = NT_STATUS_NONE_MAPPED;
509 goto done;
512 now = time(NULL);
514 /* check it is not negative */
515 if (strcmp("IDMAP/NEGATIVE", endptr+1) != 0) {
517 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
518 "timeout = %s", t > now ? "valid" :
519 "expired", idkey, endptr+1, ctime(&t)));
521 /* this call if successful will also mark the entry as mapped */
522 ret = idmap_cache_fill_map(id, endptr+1);
523 if ( ! NT_STATUS_IS_OK(ret)) {
524 /* if not valid form delete the entry */
525 tdb_delete(cache->tdb, keybuf);
526 ret = NT_STATUS_NONE_MAPPED;
527 goto done;
530 /* here ret == NT_STATUS_OK and id->mapped = ID_MAPPED */
532 if (t <= now) {
534 /* we have it, but it is expired */
535 id->status = ID_EXPIRED;
537 /* We're expired, set an error code
538 for upper layer */
539 ret = NT_STATUS_SYNCHRONIZATION_REQUIRED;
541 } else {
542 if (t <= now) {
543 /* We're expired, delete the NEGATIVE entry and return
544 not mapped */
545 tdb_delete(cache->tdb, keybuf);
546 ret = NT_STATUS_NONE_MAPPED;
547 } else {
548 /* this is not mapped as it was a negative cache hit */
549 id->status = ID_UNMAPPED;
550 ret = NT_STATUS_OK;
553 done:
554 SAFE_FREE(databuf.dptr);
555 talloc_free(idkey);
556 return ret;