2 Unix SMB/CIFS implementation.
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.*/
26 #define TIMEOUT_LEN 12
27 #define IDMAP_CACHE_DATA_FMT "%12u/%s"
28 #define IDMAP_READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
30 struct idmap_cache_ctx {
34 static int idmap_cache_destructor(struct idmap_cache_ctx *cache)
38 if (cache && cache->tdb) {
39 ret = tdb_close(cache->tdb);
46 struct idmap_cache_ctx *idmap_cache_init(TALLOC_CTX *memctx)
48 struct idmap_cache_ctx *cache;
49 char* cache_fname = NULL;
51 cache = talloc(memctx, struct idmap_cache_ctx);
53 DEBUG(0, ("Out of memory!\n"));
57 cache_fname = lock_path("idmap_cache.tdb");
59 DEBUG(10, ("Opening cache file at %s\n", cache_fname));
61 cache->tdb = tdb_open_log(cache_fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
64 DEBUG(5, ("Attempt to open %s has failed.\n", cache_fname));
68 talloc_set_destructor(cache, idmap_cache_destructor);
73 void idmap_cache_shutdown(struct idmap_cache_ctx *cache)
78 NTSTATUS idmap_cache_build_sidkey(TALLOC_CTX *ctx, char **sidkey, const struct id_map *id)
80 *sidkey = talloc_asprintf(ctx, "IDMAP/SID/%s", sid_string_static(id->sid));
82 DEBUG(1, ("failed to build sidkey, OOM?\n"));
83 return NT_STATUS_NO_MEMORY;
89 NTSTATUS idmap_cache_build_idkey(TALLOC_CTX *ctx, char **idkey, const struct id_map *id)
91 *idkey = talloc_asprintf(ctx, "IDMAP/%s/%lu",
92 (id->xid.type==ID_TYPE_UID)?"UID":"GID",
93 (unsigned long)id->xid.id);
95 DEBUG(1, ("failed to build idkey, OOM?\n"));
96 return NT_STATUS_NO_MEMORY;
102 NTSTATUS idmap_cache_set(struct idmap_cache_ctx *cache, const struct id_map *id)
105 time_t timeout = time(NULL) + lp_idmap_expire_time();
106 TDB_DATA keybuf, databuf;
111 ret = idmap_cache_build_sidkey(cache, &sidkey, id);
112 if (!NT_STATUS_IS_OK(ret)) return ret;
114 /* use sidkey as the local memory ctx */
115 ret = idmap_cache_build_idkey(sidkey, &idkey, id);
116 if (!NT_STATUS_IS_OK(ret)) {
122 /* use sidkey as the local memory ctx */
123 valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, idkey);
125 DEBUG(0, ("Out of memory!\n"));
126 ret = NT_STATUS_NO_MEMORY;
130 keybuf.dptr = sidkey;
131 keybuf.dsize = strlen(sidkey)+1;
132 databuf.dptr = valstr;
133 databuf.dsize = strlen(valstr)+1;
134 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
135 " %s (%d seconds %s)\n", keybuf.dptr, valstr , ctime(&timeout),
136 (int)(timeout - time(NULL)),
137 timeout > time(NULL) ? "ahead" : "in the past"));
139 if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) {
140 DEBUG(3, ("Failed to store cache entry!\n"));
141 ret = NT_STATUS_UNSUCCESSFUL;
147 /* use sidkey as the local memory ctx */
148 valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, sidkey);
150 DEBUG(0, ("Out of memory!\n"));
151 ret = NT_STATUS_NO_MEMORY;
156 keybuf.dsize = strlen(idkey)+1;
157 databuf.dptr = valstr;
158 databuf.dsize = strlen(valstr)+1;
159 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
160 " %s (%d seconds %s)\n", keybuf.dptr, valstr, ctime(&timeout),
161 (int)(timeout - time(NULL)),
162 timeout > time(NULL) ? "ahead" : "in the past"));
164 if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) {
165 DEBUG(3, ("Failed to store cache entry!\n"));
166 ret = NT_STATUS_UNSUCCESSFUL;
177 NTSTATUS idmap_cache_del(struct idmap_cache_ctx *cache, const struct id_map *id)
184 ret = idmap_cache_build_sidkey(cache, &sidkey, id);
185 if (!NT_STATUS_IS_OK(ret)) return ret;
187 ret = idmap_cache_build_idkey(cache, &idkey, id);
188 if (!NT_STATUS_IS_OK(ret)) {
194 keybuf.dptr = sidkey;
195 keybuf.dsize = strlen(sidkey)+1;
196 DEBUG(10, ("Deleting cache entry (key = %s)\n", keybuf.dptr));
198 if (tdb_delete(cache->tdb, keybuf) != 0) {
199 DEBUG(3, ("Failed to delete cache entry!\n"));
205 keybuf.dsize = strlen(idkey)+1;
206 DEBUG(10, ("Deleting cache entry (key = %s)\n", keybuf.dptr));
208 if (tdb_delete(cache->tdb, keybuf) != 0) {
209 DEBUG(3, ("Failed to delete cache entry!\n"));
218 NTSTATUS idmap_cache_set_negative_sid(struct idmap_cache_ctx *cache, const struct id_map *id)
221 time_t timeout = time(NULL) + lp_idmap_negative_time();
222 TDB_DATA keybuf, databuf;
226 ret = idmap_cache_build_sidkey(cache, &sidkey, id);
227 if (!NT_STATUS_IS_OK(ret)) return ret;
229 /* use sidkey as the local memory ctx */
230 valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, "IDMAP/NEGATIVE");
232 DEBUG(0, ("Out of memory!\n"));
233 ret = NT_STATUS_NO_MEMORY;
237 keybuf.dptr = sidkey;
238 keybuf.dsize = strlen(sidkey)+1;
239 databuf.dptr = valstr;
240 databuf.dsize = strlen(valstr)+1;
241 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
242 " %s (%d seconds %s)\n", keybuf.dptr, valstr, ctime(&timeout),
243 (int)(timeout - time(NULL)),
244 timeout > time(NULL) ? "ahead" : "in the past"));
246 if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) {
247 DEBUG(3, ("Failed to store cache entry!\n"));
248 ret = NT_STATUS_UNSUCCESSFUL;
257 NTSTATUS idmap_cache_set_negative_id(struct idmap_cache_ctx *cache, const struct id_map *id)
260 time_t timeout = time(NULL) + lp_idmap_negative_time();
261 TDB_DATA keybuf, databuf;
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");
271 DEBUG(0, ("Out of memory!\n"));
272 ret = NT_STATUS_NO_MEMORY;
277 keybuf.dsize = strlen(idkey)+1;
278 databuf.dptr = valstr;
279 databuf.dsize = strlen(valstr)+1;
280 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
281 " %s (%d seconds %s)\n", keybuf.dptr, valstr, ctime(&timeout),
282 (int)(timeout - time(NULL)),
283 timeout > time(NULL) ? "ahead" : "in the past"));
285 if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) {
286 DEBUG(3, ("Failed to store cache entry!\n"));
287 ret = NT_STATUS_UNSUCCESSFUL;
296 NTSTATUS idmap_cache_fill_map(struct id_map *id, const char *value)
300 /* see if it is a sid */
301 if ( ! strncmp("IDMAP/SID/", value, 10)) {
303 if ( ! string_to_sid(id->sid, &value[10])) {
307 id->status = ID_MAPPED;
312 /* not a SID see if it is an UID or a GID */
313 if ( ! strncmp("IDMAP/UID/", value, 10)) {
316 id->xid.type = ID_TYPE_UID;
318 } else if ( ! strncmp("IDMAP/GID/", value, 10)) {
321 id->xid.type = ID_TYPE_GID;
325 /* a completely bogus value bail out */
329 id->xid.id = strtol(&value[10], &rem, 0);
334 id->status = ID_MAPPED;
339 DEBUG(1, ("invalid value: %s\n", value));
340 id->status = ID_UNKNOWN;
341 return NT_STATUS_INTERNAL_DB_CORRUPTION;
344 BOOL idmap_cache_is_negative(const char *val)
346 if ( ! strcmp("IDMAP/NEGATIVE", val)) {
352 /* search the cahce for the SID an return a mapping if found *
354 * 3 cases are possible
357 * in this case id->status = ID_MAPPED and NT_STATUS_OK is returned
359 * in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned
360 * 3 negative cache found
361 * in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned
363 * As a special case if the cache is expired NT_STATUS_SYNCHRONIZATION_REQUIRED
364 * is returned instead of NT_STATUS_OK. In this case revalidation of the cache
368 NTSTATUS idmap_cache_map_sid(struct idmap_cache_ctx *cache, struct id_map *id)
371 TDB_DATA keybuf, databuf;
376 /* make sure it is marked as not mapped by default */
377 id->status = ID_UNKNOWN;
379 ret = idmap_cache_build_sidkey(cache, &sidkey, id);
380 if (!NT_STATUS_IS_OK(ret)) return ret;
382 keybuf.dptr = sidkey;
383 keybuf.dsize = strlen(sidkey)+1;
385 databuf = tdb_fetch(cache->tdb, keybuf);
387 if (databuf.dptr == NULL) {
388 DEBUG(10, ("Cache entry with key = %s couldn't be found\n", sidkey));
389 return NT_STATUS_NONE_MAPPED;
392 t = strtol(databuf.dptr, &endptr, 10);
394 if ((endptr == NULL) || (*endptr != '/')) {
395 DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr));
396 /* remove the entry */
397 tdb_delete(cache->tdb, keybuf);
398 ret = NT_STATUS_NONE_MAPPED;
402 /* check it is not negative */
403 if (strcmp("IDMAP/NEGATIVE", endptr+1) != 0) {
405 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
406 "timeout = %s", t > time(NULL) ? "valid" :
407 "expired", sidkey, endptr+1, ctime(&t)));
409 /* this call if successful will also mark the entry as mapped */
410 ret = idmap_cache_fill_map(id, endptr+1);
411 if ( ! NT_STATUS_IS_OK(ret)) {
412 /* if not valid form delete the entry */
413 tdb_delete(cache->tdb, keybuf);
414 ret = NT_STATUS_NONE_MAPPED;
418 /* here ret == NT_STATUS_OK and id->status = ID_MAPPED */
420 if (t <= time(NULL)) {
421 /* We're expired, set an error code for upper layer */
422 ret = NT_STATUS_SYNCHRONIZATION_REQUIRED;
425 if (t <= time(NULL)) {
426 /* We're expired, delete the entry and return not mapped */
427 tdb_delete(cache->tdb, keybuf);
428 ret = NT_STATUS_NONE_MAPPED;
430 /* this is not mapped as it was a negative cache hit */
431 id->status = ID_UNMAPPED;
437 SAFE_FREE(databuf.dptr);
442 /* search the cahce for the ID an return a mapping if found *
444 * 3 cases are possible
447 * in this case id->status = ID_MAPPED and NT_STATUS_OK is returned
449 * in this case id->status = ID_UNKNOWN and NT_STATUS_NONE_MAPPED is returned
450 * 3 negative cache found
451 * in this case id->status = ID_UNMAPPED and NT_STATUS_OK is returned
453 * As a special case if the cache is expired NT_STATUS_SYNCHRONIZATION_REQUIRED
454 * is returned instead of NT_STATUS_OK. In this case revalidation of the cache
458 NTSTATUS idmap_cache_map_id(struct idmap_cache_ctx *cache, struct id_map *id)
461 TDB_DATA keybuf, databuf;
466 /* make sure it is marked as not mapped by default */
467 id->status = ID_UNKNOWN;
469 ret = idmap_cache_build_idkey(cache, &idkey, id);
470 if (!NT_STATUS_IS_OK(ret)) return ret;
473 keybuf.dsize = strlen(idkey)+1;
475 databuf = tdb_fetch(cache->tdb, keybuf);
477 if (databuf.dptr == NULL) {
478 DEBUG(10, ("Cache entry with key = %s couldn't be found\n", idkey));
479 return NT_STATUS_NONE_MAPPED;
482 t = strtol(databuf.dptr, &endptr, 10);
484 if ((endptr == NULL) || (*endptr != '/')) {
485 DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr));
486 /* remove the entry */
487 tdb_delete(cache->tdb, keybuf);
488 ret = NT_STATUS_NONE_MAPPED;
492 /* check it is not negative */
493 if (strcmp("IDMAP/NEGATIVE", endptr+1) != 0) {
495 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
496 "timeout = %s", t > time(NULL) ? "valid" :
497 "expired", idkey, endptr+1, ctime(&t)));
499 /* this call if successful will also mark the entry as mapped */
500 ret = idmap_cache_fill_map(id, endptr+1);
501 if ( ! NT_STATUS_IS_OK(ret)) {
502 /* if not valid form delete the entry */
503 tdb_delete(cache->tdb, keybuf);
504 ret = NT_STATUS_NONE_MAPPED;
508 /* here ret == NT_STATUS_OK and id->mapped = True */
510 if (t <= time(NULL)) {
511 /* We're expired, set an error code for upper layer */
512 ret = NT_STATUS_SYNCHRONIZATION_REQUIRED;
515 if (t <= time(NULL)) {
516 /* We're expired, delete the entry and return not mapped */
517 tdb_delete(cache->tdb, keybuf);
518 ret = NT_STATUS_NONE_MAPPED;
520 /* this is not mapped is it was a negative cache hit */
521 id->status = ID_UNMAPPED;
526 SAFE_FREE(databuf.dptr);