struct winbindd_domain *domain;
char *p;
NTSTATUS status;
- DOM_SID sid;
+ struct dom_sid sid;
uint32 rid;
fstring keystr;
fstring dom_name;
rid = atoi(p);
- sid_copy(&sid, &domain->sid);
- sid_append_rid(&sid, rid);
+ sid_compose(&sid, &domain->sid, rid);
sid_to_fstring(keystr, &sid);
key2 = string_term_tdb_data(keystr);
}
if (dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
- DEBUG(0, ("Unable to dtore idmap version in databse\n"));
+ DEBUG(0, ("Unable to store idmap version in databse\n"));
return False;
}
if (!idmap_tdb_upgrade(db)) {
db->transaction_cancel(db);
- DEBUG(0, ("Unable to open idmap database, it's in an old formati, and upgrade failed!\n"));
+ DEBUG(0, ("Unable to open idmap database, it's in an old format, and upgrade failed!\n"));
ret = NT_STATUS_INTERNAL_DB_ERROR;
goto done;
}
static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
{
- bool ret;
+ NTSTATUS ret;
const char *hwmkey;
const char *hwmtype;
uint32_t high_hwm;
/* fetch a new id and increment it */
ret = dbwrap_change_uint32_atomic(idmap_alloc_db, hwmkey, &hwm, 1);
- if (ret != 0) {
- DEBUG(0, ("Fatal error while fetching a new %s value\n!", hwmtype));
+ if (!NT_STATUS_IS_OK(ret)) {
+ DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
+ hwmtype, nt_errstr(ret)));
idmap_alloc_db->transaction_cancel(idmap_alloc_db);
- return NT_STATUS_UNSUCCESSFUL;
+ return ret;
}
/* recheck it is in the range */
const char *hwmtype;
uint32_t hwm;
uint32_t high_hwm;
+ NTSTATUS ret;
/* Get current high water mark */
switch (xid->type) {
hwm = xid->id;
- if ((hwm = dbwrap_store_uint32(idmap_alloc_db, hwmkey, hwm)) == -1) {
- return NT_STATUS_INTERNAL_DB_ERROR;
- }
-
/* Warn if it is out of range */
if (hwm >= high_hwm) {
DEBUG(0, ("Warning: %s range full!! (max: %lu)\n",
hwmtype, (unsigned long)high_hwm));
}
- return NT_STATUS_OK;
+ ret = dbwrap_trans_store_uint32(idmap_alloc_db, hwmkey, hwm);
+
+ return ret;
}
/**********************************
{
NTSTATUS ret;
struct idmap_tdb_context *ctx;
- char *config_option = NULL;
- const char *range;
+
+ DEBUG(10, ("idmap_tdb_db_init called for domain '%s'\n", dom->name));
ctx = talloc(dom, struct idmap_tdb_context);
if ( ! ctx) {
return NT_STATUS_NO_MEMORY;
}
- config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
- if ( ! config_option) {
- DEBUG(0, ("Out of memory!\n"));
- ret = NT_STATUS_NO_MEMORY;
- goto failed;
- }
+ if (strequal(dom->name, "*")) {
+ uid_t low_uid = 0;
+ uid_t high_uid = 0;
+ gid_t low_gid = 0;
+ gid_t high_gid = 0;
- ret = idmap_tdb_open_db(ctx, false, &ctx->db);
- if ( ! NT_STATUS_IS_OK(ret)) {
- goto failed;
+ ctx->filter_low_id = 0;
+ ctx->filter_high_id = 0;
+
+ if (lp_idmap_uid(&low_uid, &high_uid)) {
+ ctx->filter_low_id = low_uid;
+ ctx->filter_high_id = high_uid;
+ } else {
+ DEBUG(3, ("Warning: 'idmap uid' not set!\n"));
+ }
+
+ if (lp_idmap_gid(&low_gid, &high_gid)) {
+ if ((low_gid != low_uid) || (high_gid != high_uid)) {
+ DEBUG(1, ("Warning: 'idmap uid' and 'idmap gid'"
+ " ranges do not agree -- building "
+ "intersection\n"));
+ ctx->filter_low_id = MAX(ctx->filter_low_id,
+ low_gid);
+ ctx->filter_high_id = MIN(ctx->filter_high_id,
+ high_gid);
+ }
+ } else {
+ DEBUG(3, ("Warning: 'idmap gid' not set!\n"));
+ }
+ } else {
+ char *config_option = NULL;
+ const char *range;
+
+ config_option = talloc_asprintf(ctx, "idmap config %s", dom->name);
+ if ( ! config_option) {
+ DEBUG(0, ("Out of memory!\n"));
+ ret = NT_STATUS_NO_MEMORY;
+ goto failed;
+ }
+
+ range = lp_parm_const_string(-1, config_option, "range", NULL);
+ if (( ! range) ||
+ (sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2))
+ {
+ ctx->filter_low_id = 0;
+ ctx->filter_high_id = 0;
+ }
+
+ talloc_free(config_option);
}
- range = lp_parm_const_string(-1, config_option, "range", NULL);
- if (( ! range) ||
- (sscanf(range, "%u - %u", &ctx->filter_low_id, &ctx->filter_high_id) != 2) ||
- (ctx->filter_low_id > ctx->filter_high_id)) {
+ if (ctx->filter_low_id > ctx->filter_high_id) {
ctx->filter_low_id = 0;
ctx->filter_high_id = 0;
}
+ DEBUG(10, ("idmap_tdb_db_init: filter range %u-%u loaded for domain "
+ "'%s'\n", ctx->filter_low_id, ctx->filter_high_id, dom->name));
+
+ ret = idmap_tdb_open_db(ctx, false, &ctx->db);
+ if ( ! NT_STATUS_IS_OK(ret)) {
+ goto failed;
+ }
+
dom->private_data = ctx;
- talloc_free(config_option);
return NT_STATUS_OK;
failed:
} else { /* Unknown record type ! */
DEBUG(2, ("Found INVALID record %s -> %s\n", keystr, (const char *)data.dptr));
ret = NT_STATUS_INTERNAL_DB_ERROR;
+ goto done;
}
/* apply filters before returning result */
return ret;
}
-/**********************************
- remove a mapping.
-**********************************/
-
-static NTSTATUS idmap_tdb_remove_mapping(struct idmap_domain *dom,
- const struct id_map *map)
-{
- struct idmap_tdb_context *ctx;
- NTSTATUS ret;
- TDB_DATA ksid, kid, data;
- char *ksidstr, *kidstr;
- fstring tmp;
-
- if (!map || !map->sid) {
- return NT_STATUS_INVALID_PARAMETER;
- }
-
- ksidstr = kidstr = NULL;
- data.dptr = NULL;
-
- /* TODO: should we filter a remove_mapping using low/high filters ? */
-
- ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
-
- switch (map->xid.type) {
-
- case ID_TYPE_UID:
- kidstr = talloc_asprintf(ctx, "UID %lu",
- (unsigned long)map->xid.id);
- break;
-
- case ID_TYPE_GID:
- kidstr = talloc_asprintf(ctx, "GID %lu",
- (unsigned long)map->xid.id);
- break;
-
- default:
- DEBUG(2, ("INVALID unix ID type: 0x02%x\n", map->xid.type));
- return NT_STATUS_INVALID_PARAMETER;
- }
-
- if (kidstr == NULL) {
- DEBUG(0, ("ERROR: Out of memory!\n"));
- ret = NT_STATUS_NO_MEMORY;
- goto done;
- }
-
- if ((ksidstr = talloc_asprintf(
- ctx, "%s", sid_to_fstring(tmp, map->sid))) == NULL) {
- DEBUG(0, ("Out of memory!\n"));
- ret = NT_STATUS_NO_MEMORY;
- goto done;
- }
-
- DEBUG(10, ("Checking %s <-> %s map\n", ksidstr, kidstr));
- ksid = string_term_tdb_data(ksidstr);
- kid = string_term_tdb_data(kidstr);
-
- if (ctx->db->transaction_start(ctx->db) != 0) {
- DEBUG(0, ("Failed to start transaction for %s\n",
- ksidstr));
- return NT_STATUS_INTERNAL_DB_ERROR;
- }
-
- /* Check if sid is present in database */
- data = dbwrap_fetch(ctx->db, NULL, ksid);
- if (!data.dptr) {
- ctx->db->transaction_cancel(ctx->db);
- DEBUG(10,("Record %s not found\n", ksidstr));
- ret = NT_STATUS_NONE_MAPPED;
- goto done;
- }
-
- /* Check if sid is mapped to the specified ID */
- if ((data.dsize != kid.dsize) ||
- (memcmp(data.dptr, kid.dptr, data.dsize) != 0)) {
- ctx->db->transaction_cancel(ctx->db);
- DEBUG(10,("Specified SID does not map to specified ID\n"));
- DEBUGADD(10,("Actual mapping is %s -> %s\n", ksidstr,
- (const char *)data.dptr));
- ret = NT_STATUS_NONE_MAPPED;
- goto done;
- }
-
- DEBUG(10, ("Removing %s <-> %s map\n", ksidstr, kidstr));
-
- /* Delete previous mappings. */
-
- DEBUG(10, ("Deleting existing mapping %s -> %s\n", ksidstr, kidstr ));
- ret = dbwrap_delete(ctx->db, ksid);
- if (!NT_STATUS_IS_OK(ret)) {
- DEBUG(0,("Warning: Failed to delete %s: %s\n",
- ksidstr, nt_errstr(ret)));
- }
-
- DEBUG(10,("Deleting existing mapping %s -> %s\n", kidstr, ksidstr ));
- ret = dbwrap_delete(ctx->db, kid);
- if (!NT_STATUS_IS_OK(ret)) {
- DEBUG(0,("Warning: Failed to delete %s: %s\n",
- kidstr, nt_errstr(ret)));
- }
-
- if (ctx->db->transaction_commit(ctx->db) != 0) {
- DEBUG(0, ("Failed to commit transaction for (%s -> %s)\n",
- ksidstr, kidstr));
- ret = NT_STATUS_INTERNAL_DB_ERROR;
- goto done;
- }
-
- ret = NT_STATUS_OK;
-
-done:
- talloc_free(ksidstr);
- talloc_free(kidstr);
- talloc_free(data.dptr);
- return ret;
-}
-
/**********************************
Close the idmap tdb instance
**********************************/
return NT_STATUS_OK;
}
-struct dump_data {
- TALLOC_CTX *memctx;
- struct id_map **maps;
- int *num_maps;
- NTSTATUS ret;
-};
-
-static int idmap_tdb_dump_one_entry(struct db_record *rec, void *pdata)
-{
- struct dump_data *data = talloc_get_type(pdata, struct dump_data);
- struct id_map *maps;
- int num_maps = *data->num_maps;
-
- /* ignore any record but the ones with a SID as key */
- if (strncmp((const char *)rec->key.dptr, "S-", 2) == 0) {
-
- maps = talloc_realloc(NULL, *data->maps, struct id_map, num_maps+1);
- if ( ! maps) {
- DEBUG(0, ("Out of memory!\n"));
- data->ret = NT_STATUS_NO_MEMORY;
- return -1;
- }
- *data->maps = maps;
- maps[num_maps].sid = talloc(maps, DOM_SID);
- if ( ! maps[num_maps].sid) {
- DEBUG(0, ("Out of memory!\n"));
- data->ret = NT_STATUS_NO_MEMORY;
- return -1;
- }
-
- if (!string_to_sid(maps[num_maps].sid, (const char *)rec->key.dptr)) {
- DEBUG(10,("INVALID record %s\n", (const char *)rec->key.dptr));
- /* continue even with errors */
- return 0;
- }
-
- /* Try a UID record. */
- if (sscanf((const char *)rec->value.dptr, "UID %u", &(maps[num_maps].xid.id)) == 1) {
- maps[num_maps].xid.type = ID_TYPE_UID;
- maps[num_maps].status = ID_MAPPED;
- *data->num_maps = num_maps + 1;
-
- /* Try a GID record. */
- } else
- if (sscanf((const char *)rec->value.dptr, "GID %u", &(maps[num_maps].xid.id)) == 1) {
- maps[num_maps].xid.type = ID_TYPE_GID;
- maps[num_maps].status = ID_MAPPED;
- *data->num_maps = num_maps + 1;
-
- /* Unknown record type ! */
- } else {
- maps[num_maps].status = ID_UNKNOWN;
- DEBUG(2, ("Found INVALID record %s -> %s\n",
- (const char *)rec->key.dptr,
- (const char *)rec->value.dptr));
- /* do not increment num_maps */
- }
- }
-
- return 0;
-}
-
-/**********************************
- Dump all mappings out
-**********************************/
-
-static NTSTATUS idmap_tdb_dump_data(struct idmap_domain *dom, struct id_map **maps, int *num_maps)
-{
- struct idmap_tdb_context *ctx;
- struct dump_data *data;
- NTSTATUS ret = NT_STATUS_OK;
-
- ctx = talloc_get_type(dom->private_data, struct idmap_tdb_context);
-
- data = TALLOC_ZERO_P(ctx, struct dump_data);
- if ( ! data) {
- DEBUG(0, ("Out of memory!\n"));
- return NT_STATUS_NO_MEMORY;
- }
- data->maps = maps;
- data->num_maps = num_maps;
- data->ret = NT_STATUS_OK;
-
- ctx->db->traverse_read(ctx->db, idmap_tdb_dump_one_entry, data);
-
- if ( ! NT_STATUS_IS_OK(data->ret)) {
- ret = data->ret;
- }
-
- talloc_free(data);
- return ret;
-}
-
static struct idmap_methods db_methods = {
.init = idmap_tdb_db_init,
.unixids_to_sids = idmap_tdb_unixids_to_sids,
.sids_to_unixids = idmap_tdb_sids_to_unixids,
.set_mapping = idmap_tdb_set_mapping,
- .remove_mapping = idmap_tdb_remove_mapping,
- .dump_data = idmap_tdb_dump_data,
.close_fn = idmap_tdb_close
};