struct idmap_context *idmap_init(TALLOC_CTX *mem_ctx,
				 struct tevent_context *ev_ctx,
				 struct loadparm_context *lp_ctx)
{
	struct idmap_context *idmap_ctx;

	idmap_ctx = talloc(mem_ctx, struct idmap_context);
	if (idmap_ctx == NULL) {
		return NULL;
	}

	idmap_ctx->lp_ctx = lp_ctx;

	idmap_ctx->ldb_ctx = ldb_wrap_connect(idmap_ctx, ev_ctx, lp_ctx,
					      "idmap.ldb",
					      system_session(lp_ctx),
					      NULL, 0);
	if (idmap_ctx->ldb_ctx == NULL) {
		goto fail;
	}

	idmap_ctx->samdb = samdb_connect(idmap_ctx,
					 ev_ctx,
					 lp_ctx,
					 system_session(lp_ctx),
					 NULL,
					 0);
	if (idmap_ctx->samdb == NULL) {
		DEBUG(0, ("Failed to load sam.ldb in idmap_init\n"));
		goto fail;
	}

	return idmap_ctx;
fail:
	TALLOC_FREE(idmap_ctx);
	return NULL;
}
struct idmap_context *idmap_init(TALLOC_CTX *mem_ctx,
				 struct tevent_context *ev_ctx,
				 struct loadparm_context *lp_ctx)
{
	struct idmap_context *idmap_ctx;

	idmap_ctx = talloc(mem_ctx, struct idmap_context);
	if (idmap_ctx == NULL) {
		return NULL;
	}

	idmap_ctx->lp_ctx = lp_ctx;

	idmap_ctx->ldb_ctx = ldb_wrap_connect(idmap_ctx, ev_ctx, lp_ctx,
					      "idmap.ldb",
					      system_session(lp_ctx),
					      NULL, 0);
	if (idmap_ctx->ldb_ctx == NULL) {
		goto fail;
	}

	idmap_ctx->samdb = samdb_connect(idmap_ctx,
					 ev_ctx,
					 lp_ctx,
					 system_session(lp_ctx),
					 NULL,
					 0);
	if (idmap_ctx->samdb == NULL) {
		DEBUG(0, ("Failed to load sam.ldb in idmap_init\n"));
		goto fail;
	}

	return idmap_ctx;
fail:
	TALLOC_FREE(idmap_ctx);
	return NULL;
}