int ldb_kv_store(struct ldb_module *module,
		 const struct ldb_message *msg,
		 int flgs)
{
	void *data = ldb_module_get_private(module);
	struct ldb_kv_private *ldb_kv =
	    talloc_get_type(data, struct ldb_kv_private);
	struct ldb_val key;
	struct ldb_val ldb_data;
	int ret = LDB_SUCCESS;
	TALLOC_CTX *key_ctx = talloc_new(module);

	if (key_ctx == NULL) {
		return ldb_module_oom(module);
	}

	if (ldb_kv->read_only) {
		talloc_free(key_ctx);
		return LDB_ERR_UNWILLING_TO_PERFORM;
	}

	key = ldb_kv_key_msg(module, key_ctx, msg);
	if (key.data == NULL) {
		TALLOC_FREE(key_ctx);
		return LDB_ERR_OTHER;
	}

	ret = ldb_pack_data(ldb_module_get_ctx(module),
			    msg, &ldb_data,
			    ldb_kv->pack_format_version);
	if (ret == -1) {
		TALLOC_FREE(key_ctx);
		return LDB_ERR_OTHER;
	}

	ret = ldb_kv->kv_ops->store(ldb_kv, key, ldb_data, flgs);
	if (ret != 0) {
		bool is_special = ldb_dn_is_special(msg->dn);
		ret = ldb_kv->kv_ops->error(ldb_kv);

		/*
		 * LDB_ERR_ENTRY_ALREADY_EXISTS means the DN, not
		 * the GUID, so re-map
		 */
		if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS && !is_special &&
		    ldb_kv->cache->GUID_index_attribute != NULL) {
			ret = LDB_ERR_CONSTRAINT_VIOLATION;
		}
		goto done;
	}

done:
	TALLOC_FREE(key_ctx);
	talloc_free(ldb_data.data);

	return ret;
}
int ldb_kv_store(struct ldb_module *module,
		 const struct ldb_message *msg,
		 int flgs)
{
	void *data = ldb_module_get_private(module);
	struct ldb_kv_private *ldb_kv =
	    talloc_get_type(data, struct ldb_kv_private);
	struct ldb_val key;
	struct ldb_val ldb_data;
	int ret = LDB_SUCCESS;
	TALLOC_CTX *key_ctx = talloc_new(module);

	if (key_ctx == NULL) {
		return ldb_module_oom(module);
	}

	if (ldb_kv->read_only) {
		talloc_free(key_ctx);
		return LDB_ERR_UNWILLING_TO_PERFORM;
	}

	key = ldb_kv_key_msg(module, key_ctx, msg);
	if (key.data == NULL) {
		TALLOC_FREE(key_ctx);
		return LDB_ERR_OTHER;
	}

	ret = ldb_pack_data(ldb_module_get_ctx(module),
			    msg, &ldb_data,
			    ldb_kv->pack_format_version);
	if (ret == -1) {
		TALLOC_FREE(key_ctx);
		return LDB_ERR_OTHER;
	}

	ret = ldb_kv->kv_ops->store(ldb_kv, key, ldb_data, flgs);
	if (ret != 0) {
		bool is_special = ldb_dn_is_special(msg->dn);
		ret = ldb_kv->kv_ops->error(ldb_kv);

		 
		if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS && !is_special &&
		    ldb_kv->cache->GUID_index_attribute != NULL) {
			ret = LDB_ERR_CONSTRAINT_VIOLATION;
		}
		goto done;
	}

done:
	TALLOC_FREE(key_ctx);
	talloc_free(ldb_data.data);

	return ret;
}