static int check_spn_write_rights(struct ldb_context *ldb,
				  TALLOC_CTX *mem_ctx,
				  const char *spn,
				  struct ldb_dn *dn)
{
	int ret;
	struct ldb_message *msg = NULL;
	struct ldb_message_element *del_el = NULL;
	struct ldb_message_element *add_el = NULL;
	struct ldb_val val = {
		.data = discard_const_p(uint8_t, spn),
		.length = strlen(spn)
	};

	msg = ldb_msg_new(mem_ctx);
	if (msg == NULL) {
		return ldb_oom(ldb);
	}
	msg->dn = dn;

	ret = ldb_msg_add_empty(msg,
				"servicePrincipalName",
				LDB_FLAG_MOD_DELETE,
				&del_el);
	if (ret != LDB_SUCCESS) {
		talloc_free(msg);
		return ret;
	}

	del_el->values = talloc_array(msg->elements, struct ldb_val, 1);
	if (del_el->values == NULL) {
		talloc_free(msg);
		return ret;
	}

	del_el->values[0] = val;
	del_el->num_values = 1;

	ret = ldb_msg_add_empty(msg,
				"servicePrincipalName",
				LDB_FLAG_MOD_ADD,
				&add_el);
	if (ret != LDB_SUCCESS) {
		talloc_free(msg);
		return ret;
	}

	add_el->values = talloc_array(msg->elements, struct ldb_val, 1);
	if (add_el->values == NULL) {
		talloc_free(msg);
		return ret;
	}

	add_el->values[0] = val;
	add_el->num_values = 1;

	ret = ldb_modify(ldb, msg);
	if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
		DBG_ERR("hmm I think we're OK, but not sure\n");
	} else if (ret != LDB_SUCCESS) {
		DBG_ERR("SPN write rights check failed with %d\n", ret);
		talloc_free(msg);
		return ret;
	}
	talloc_free(msg);
	return LDB_SUCCESS;
}
static int check_spn_write_rights(struct ldb_context *ldb,
				  TALLOC_CTX *mem_ctx,
				  const char *spn,
				  struct ldb_dn *dn)
{
	int ret;
	struct ldb_message *msg = NULL;
	struct ldb_message_element *del_el = NULL;
	struct ldb_message_element *add_el = NULL;
	struct ldb_val val = {
		.data = discard_const_p(uint8_t, spn),
		.length = strlen(spn)
	};

	msg = ldb_msg_new(mem_ctx);
	if (msg == NULL) {
		return ldb_oom(ldb);
	}
	msg->dn = dn;

	ret = ldb_msg_add_empty(msg,
				"servicePrincipalName",
				LDB_FLAG_MOD_DELETE,
				&del_el);
	if (ret != LDB_SUCCESS) {
		talloc_free(msg);
		return ret;
	}

	del_el->values = talloc_array(msg->elements, struct ldb_val, 1);
	if (del_el->values == NULL) {
		talloc_free(msg);
		return ret;
	}

	del_el->values[0] = val;
	del_el->num_values = 1;

	ret = ldb_msg_add_empty(msg,
				"servicePrincipalName",
				LDB_FLAG_MOD_ADD,
				&add_el);
	if (ret != LDB_SUCCESS) {
		talloc_free(msg);
		return ret;
	}

	add_el->values = talloc_array(msg->elements, struct ldb_val, 1);
	if (add_el->values == NULL) {
		talloc_free(msg);
		return ret;
	}

	add_el->values[0] = val;
	add_el->num_values = 1;

	ret = ldb_modify(ldb, msg);
	if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
		DBG_ERR("hmm I think we're OK, but not sure\n");
	} else if (ret != LDB_SUCCESS) {
		DBG_ERR("SPN write rights check failed with %d\n", ret);
		talloc_free(msg);
		return ret;
	}
	talloc_free(msg);
	return LDB_SUCCESS;
}