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 */
return NT_STATUS_OK;
}
-/**********************************
- Get current highest id.
-**********************************/
-
-static NTSTATUS idmap_tdb_get_hwm(struct unixid *xid)
-{
- const char *hwmkey;
- const char *hwmtype;
- uint32_t hwm;
- uint32_t high_hwm;
-
- /* Get current high water mark */
- switch (xid->type) {
-
- case ID_TYPE_UID:
- hwmkey = HWM_USER;
- hwmtype = "UID";
- high_hwm = idmap_tdb_state.high_uid;
- break;
-
- case ID_TYPE_GID:
- hwmkey = HWM_GROUP;
- hwmtype = "GID";
- high_hwm = idmap_tdb_state.high_gid;
- break;
-
- default:
- return NT_STATUS_INVALID_PARAMETER;
- }
-
- if ((hwm = dbwrap_fetch_int32(idmap_alloc_db, hwmkey)) == -1) {
- return NT_STATUS_INTERNAL_DB_ERROR;
- }
-
- xid->id = hwm;
-
- /* 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;
-}
-
/**********************************
Set high id.
**********************************/
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;
}
/**********************************
} 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
};
.init = idmap_tdb_alloc_init,
.allocate_id = idmap_tdb_allocate_id,
- .get_id_hwm = idmap_tdb_get_hwm,
.set_id_hwm = idmap_tdb_set_hwm,
.close_fn = idmap_tdb_alloc_close
};