void idmap_cache_set_sid2gid(const struct dom_sid *sid, gid_t gid) { time_t now = time(NULL); time_t timeout; fstring sidstr, key, value; if (!is_null_sid(sid)) { fstr_sprintf(key, "IDMAP/SID2GID/%s", sid_to_fstring(sidstr, sid)); fstr_sprintf(value, "%d", (int)gid); timeout = (gid == -1) ? lp_idmap_negative_cache_time() : lp_idmap_cache_time(); gencache_set(key, value, now + timeout); } if (gid != -1) { fstr_sprintf(key, "IDMAP/GID2SID/%d", (int)gid); if (is_null_sid(sid)) { /* negative gid mapping */ fstrcpy(value, "-"); timeout = lp_idmap_negative_cache_time(); } else { sid_to_fstring(value, sid); timeout = lp_idmap_cache_time(); } gencache_set(key, value, now + timeout); } }
NTSTATUS idmap_cache_set(struct idmap_cache_ctx *cache, const struct id_map *id) { NTSTATUS ret; time_t timeout = time(NULL) + lp_idmap_cache_time(); TDB_DATA keybuf, databuf; char *sidkey; char *idkey; char *valstr; /* Don't cache lookups in the S-1-22-{1,2} domain */ if ( (id->xid.type == ID_TYPE_UID) && sid_check_is_in_unix_users(id->sid) ) { return NT_STATUS_OK; } if ( (id->xid.type == ID_TYPE_GID) && sid_check_is_in_unix_groups(id->sid) ) { return NT_STATUS_OK; } ret = idmap_cache_build_sidkey(cache, &sidkey, id); if (!NT_STATUS_IS_OK(ret)) return ret; /* use sidkey as the local memory ctx */ ret = idmap_cache_build_idkey(sidkey, &idkey, id); if (!NT_STATUS_IS_OK(ret)) { goto done; } /* save SID -> ID */ /* use sidkey as the local memory ctx */ valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, idkey); if (!valstr) { DEBUG(0, ("Out of memory!\n")); ret = NT_STATUS_NO_MEMORY; goto done; } keybuf.dptr = sidkey; keybuf.dsize = strlen(sidkey)+1; databuf.dptr = valstr; databuf.dsize = strlen(valstr)+1; DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout =" " %s (%d seconds %s)\n", keybuf.dptr, valstr , ctime(&timeout), (int)(timeout - time(NULL)), timeout > time(NULL) ? "ahead" : "in the past")); if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) { DEBUG(3, ("Failed to store cache entry!\n")); ret = NT_STATUS_UNSUCCESSFUL; goto done; } /* save ID -> SID */ /* use sidkey as the local memory ctx */ valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, sidkey); if (!valstr) { DEBUG(0, ("Out of memory!\n")); ret = NT_STATUS_NO_MEMORY; goto done; } keybuf.dptr = idkey; keybuf.dsize = strlen(idkey)+1; databuf.dptr = valstr; databuf.dsize = strlen(valstr)+1; DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout =" " %s (%d seconds %s)\n", keybuf.dptr, valstr, ctime(&timeout), (int)(timeout - time(NULL)), timeout > time(NULL) ? "ahead" : "in the past")); if (tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE) != 0) { DEBUG(3, ("Failed to store cache entry!\n")); ret = NT_STATUS_UNSUCCESSFUL; goto done; } ret = NT_STATUS_OK; done: talloc_free(sidkey); return ret; }