int dsdb_find_sid_by_dn(struct ldb_context *ldb, 
			struct ldb_dn *dn, struct dom_sid *sid)
{
	int ret;
	struct ldb_result *res = NULL;
	const char *attrs[] = { "objectSid", NULL };
	TALLOC_CTX *tmp_ctx = talloc_new(ldb);
	struct dom_sid *s;

	ZERO_STRUCTP(sid);

	ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
			     DSDB_SEARCH_SHOW_DELETED |
			     DSDB_SEARCH_SHOW_RECYCLED);
	if (ret != LDB_SUCCESS) {
		talloc_free(tmp_ctx);
		return ret;
	}
	if (res == NULL) {
		talloc_free(tmp_ctx);
		return LDB_ERR_OTHER;
	}
	if (res->count < 1) {
		talloc_free(tmp_ctx);
		return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
	}
	s = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
	if (s == NULL) {
		talloc_free(tmp_ctx);
		return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
	}
	*sid = *s;
	talloc_free(tmp_ctx);
	return LDB_SUCCESS;
}
int dsdb_find_sid_by_dn(struct ldb_context *ldb, 
			struct ldb_dn *dn, struct dom_sid *sid)
{
	int ret;
	struct ldb_result *res = NULL;
	const char *attrs[] = { "objectSid", NULL };
	TALLOC_CTX *tmp_ctx = talloc_new(ldb);
	struct dom_sid *s;

	ZERO_STRUCTP(sid);

	ret = dsdb_search_dn(ldb, tmp_ctx, &res, dn, attrs,
			     DSDB_SEARCH_SHOW_DELETED |
			     DSDB_SEARCH_SHOW_RECYCLED);
	if (ret != LDB_SUCCESS) {
		talloc_free(tmp_ctx);
		return ret;
	}
	if (res == NULL) {
		talloc_free(tmp_ctx);
		return LDB_ERR_OTHER;
	}
	if (res->count < 1) {
		talloc_free(tmp_ctx);
		return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
	}
	s = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
	if (s == NULL) {
		talloc_free(tmp_ctx);
		return ldb_error(ldb, LDB_ERR_NO_SUCH_OBJECT, __func__);
	}
	*sid = *s;
	talloc_free(tmp_ctx);
	return LDB_SUCCESS;
}