#include "includes.h"
#include "winbindd.h"
#define TIMEOUT_LEN 12
#define IDMAP_CACHE_DATA_FMT "%12u/%s"
#define IDMAP_READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
struct idmap_cache_ctx {
TDB_CONTEXT *tdb;
};
static int idmap_cache_destructor(struct idmap_cache_ctx *cache)
{
int ret = 0;
if (cache && cache->tdb) {
ret = tdb_close(cache->tdb);
cache->tdb = NULL;
}
return ret;
}
struct idmap_cache_ctx *idmap_cache_init(TALLOC_CTX *memctx)
{
struct idmap_cache_ctx *cache;
char* cache_fname = NULL;
cache = talloc(memctx, struct idmap_cache_ctx);
if ( ! cache) {
DEBUG(0, ("Out of memory!\n"));
return NULL;
}
cache_fname = lock_path("idmap_cache.tdb");
DEBUG(10, ("Opening cache file at %s\n", cache_fname));
cache->tdb = tdb_open_log(cache_fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
if (!cache->tdb) {
DEBUG(5, ("Attempt to open %s has failed.\n", cache_fname));
return NULL;
}
talloc_set_destructor(cache, idmap_cache_destructor);
return cache;
}
void idmap_cache_shutdown(struct idmap_cache_ctx *cache)
{
talloc_free(cache);
}
NTSTATUS idmap_cache_build_sidkey(TALLOC_CTX *ctx, char **sidkey, const struct id_map *id)
{
*sidkey = talloc_asprintf(ctx, "IDMAP/SID/%s", sid_string_static(id->sid));
if ( ! *sidkey) {
DEBUG(1, ("failed to build sidkey, OOM?\n"));
return NT_STATUS_NO_MEMORY;
}
return NT_STATUS_OK;
}
NTSTATUS idmap_cache_build_idkey(TALLOC_CTX *ctx, char **idkey, const struct id_map *id)
{
*idkey = talloc_asprintf(ctx, "IDMAP/%s/%lu",
(id->xid.type==ID_TYPE_UID)?"UID":"GID",
(unsigned long)id->xid.id);
if ( ! *idkey) {
DEBUG(1, ("failed to build idkey, OOM?\n"));
return NT_STATUS_NO_MEMORY;
}
return NT_STATUS_OK;
}
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;
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;
ret = idmap_cache_build_idkey(sidkey, &idkey, id);
if (!NT_STATUS_IS_OK(ret)) {
goto done;
}
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;
}
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;
}
NTSTATUS idmap_cache_del(struct idmap_cache_ctx *cache, const struct id_map *id)
{
NTSTATUS ret;
TDB_DATA keybuf;
char *sidkey = NULL;
char *idkey = NULL;
ret = idmap_cache_build_sidkey(cache, &sidkey, id);
if (!NT_STATUS_IS_OK(ret)) return ret;
ret = idmap_cache_build_idkey(cache, &idkey, id);
if (!NT_STATUS_IS_OK(ret)) {
goto done;
}
keybuf.dptr = sidkey;
keybuf.dsize = strlen(sidkey)+1;
DEBUG(10, ("Deleting cache entry (key = %s)\n", keybuf.dptr));
if (tdb_delete(cache->tdb, keybuf) != 0) {
DEBUG(3, ("Failed to delete cache entry!\n"));
}
keybuf.dptr = idkey;
keybuf.dsize = strlen(idkey)+1;
DEBUG(10, ("Deleting cache entry (key = %s)\n", keybuf.dptr));
if (tdb_delete(cache->tdb, keybuf) != 0) {
DEBUG(3, ("Failed to delete cache entry!\n"));
}
done:
talloc_free(sidkey);
talloc_free(idkey);
return ret;
}
NTSTATUS idmap_cache_set_negative_sid(struct idmap_cache_ctx *cache, const struct id_map *id)
{
NTSTATUS ret;
time_t timeout = time(NULL) + lp_idmap_negative_cache_time();
TDB_DATA keybuf, databuf;
char *sidkey;
char *valstr;
ret = idmap_cache_build_sidkey(cache, &sidkey, id);
if (!NT_STATUS_IS_OK(ret)) return ret;
valstr = talloc_asprintf(sidkey, IDMAP_CACHE_DATA_FMT, (int)timeout, "IDMAP/NEGATIVE");
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;
}
done:
talloc_free(sidkey);
return ret;
}
NTSTATUS idmap_cache_set_negative_id(struct idmap_cache_ctx *cache, const struct id_map *id)
{
NTSTATUS ret;
time_t timeout = time(NULL) + lp_idmap_negative_cache_time();
TDB_DATA keybuf, databuf;
char *idkey;
char *valstr;
ret = idmap_cache_build_idkey(cache, &idkey, id);
if (!NT_STATUS_IS_OK(ret)) return ret;
valstr = talloc_asprintf(idkey, IDMAP_CACHE_DATA_FMT, (int)timeout, "IDMAP/NEGATIVE");
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;
}
done:
talloc_free(idkey);
return ret;
}
NTSTATUS idmap_cache_fill_map(struct id_map *id, const char *value)
{
char *rem;
if ( ! strncmp("IDMAP/SID/", value, 10)) {
if ( ! string_to_sid(id->sid, &value[10])) {
goto failed;
}
id->status = ID_MAPPED;
return NT_STATUS_OK;
}
if ( ! strncmp("IDMAP/UID/", value, 10)) {
id->xid.type = ID_TYPE_UID;
} else if ( ! strncmp("IDMAP/GID/", value, 10)) {
id->xid.type = ID_TYPE_GID;
} else {
goto failed;
}
id->xid.id = strtol(&value[10], &rem, 0);
if (*rem != '\0') {
goto failed;
}
id->status = ID_MAPPED;
return NT_STATUS_OK;
failed:
DEBUG(1, ("invalid value: %s\n", value));
id->status = ID_UNKNOWN;
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
BOOL idmap_cache_is_negative(const char *val)
{
if ( ! strcmp("IDMAP/NEGATIVE", val)) {
return True;
}
return False;
}
NTSTATUS idmap_cache_map_sid(struct idmap_cache_ctx *cache, struct id_map *id)
{
NTSTATUS ret;
TDB_DATA keybuf, databuf;
time_t t, now;
char *sidkey;
char *endptr;
id->status = ID_UNKNOWN;
ret = idmap_cache_build_sidkey(cache, &sidkey, id);
if (!NT_STATUS_IS_OK(ret)) return ret;
keybuf.dptr = sidkey;
keybuf.dsize = strlen(sidkey)+1;
databuf = tdb_fetch(cache->tdb, keybuf);
if (databuf.dptr == NULL) {
DEBUG(10, ("Cache entry with key = %s couldn't be found\n", sidkey));
return NT_STATUS_NONE_MAPPED;
}
t = strtol(databuf.dptr, &endptr, 10);
if ((endptr == NULL) || (*endptr != '/')) {
DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr));
tdb_delete(cache->tdb, keybuf);
ret = NT_STATUS_NONE_MAPPED;
goto done;
}
now = time(NULL);
if (strcmp("IDMAP/NEGATIVE", endptr+1) != 0) {
DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
"timeout = %s", t > now ? "valid" :
"expired", sidkey, endptr+1, ctime(&t)));
ret = idmap_cache_fill_map(id, endptr+1);
if ( ! NT_STATUS_IS_OK(ret)) {
tdb_delete(cache->tdb, keybuf);
ret = NT_STATUS_NONE_MAPPED;
goto done;
}
if (t <= now) {
id->status = ID_EXPIRED;
ret = NT_STATUS_SYNCHRONIZATION_REQUIRED;
}
} else {
if (t <= now) {
tdb_delete(cache->tdb, keybuf);
ret = NT_STATUS_NONE_MAPPED;
} else {
id->status = ID_UNMAPPED;
ret = NT_STATUS_OK;
}
}
done:
SAFE_FREE(databuf.dptr);
talloc_free(sidkey);
return ret;
}
NTSTATUS idmap_cache_map_id(struct idmap_cache_ctx *cache, struct id_map *id)
{
NTSTATUS ret;
TDB_DATA keybuf, databuf;
time_t t, now;
char *idkey;
char *endptr;
id->status = ID_UNKNOWN;
ret = idmap_cache_build_idkey(cache, &idkey, id);
if (!NT_STATUS_IS_OK(ret)) return ret;
keybuf.dptr = idkey;
keybuf.dsize = strlen(idkey)+1;
databuf = tdb_fetch(cache->tdb, keybuf);
if (databuf.dptr == NULL) {
DEBUG(10, ("Cache entry with key = %s couldn't be found\n", idkey));
return NT_STATUS_NONE_MAPPED;
}
t = strtol(databuf.dptr, &endptr, 10);
if ((endptr == NULL) || (*endptr != '/')) {
DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr));
tdb_delete(cache->tdb, keybuf);
ret = NT_STATUS_NONE_MAPPED;
goto done;
}
now = time(NULL);
if (strcmp("IDMAP/NEGATIVE", endptr+1) != 0) {
DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
"timeout = %s", t > now ? "valid" :
"expired", idkey, endptr+1, ctime(&t)));
ret = idmap_cache_fill_map(id, endptr+1);
if ( ! NT_STATUS_IS_OK(ret)) {
tdb_delete(cache->tdb, keybuf);
ret = NT_STATUS_NONE_MAPPED;
goto done;
}
if (t <= now) {
id->status = ID_EXPIRED;
ret = NT_STATUS_SYNCHRONIZATION_REQUIRED;
}
} else {
if (t <= now) {
tdb_delete(cache->tdb, keybuf);
ret = NT_STATUS_NONE_MAPPED;
} else {
id->status = ID_UNMAPPED;
ret = NT_STATUS_OK;
}
}
done:
SAFE_FREE(databuf.dptr);
talloc_free(idkey);
return ret;
}