static int samldb_schema_add_handle_mapiid(struct samldb_ctx *ac)
{
	int ret;
	bool ok;
	struct ldb_message_element *el;
	const char *enc_str;
	struct ldb_context *ldb;
	struct ldb_dn *schema_dn;
	struct dsdb_schema *schema;
	int32_t new_mapiid = 0;

	/*
	 * The mAPIID of a new attribute should be automatically generated
	 * if a specific OID is put as the mAPIID, as according to
	 * [MS-ADTS] 3.1.1.2.3.2.
	 */

	ldb = ldb_module_get_ctx(ac->module);
	schema = dsdb_get_schema(ldb, ac);
	schema_dn = ldb_get_schema_basedn(ldb);

	ret = dsdb_get_expected_new_values(ac,
					   ac->msg,
					   "mAPIID",
					   &el,
					   ac->req->operation);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	if (el == NULL) {
		return LDB_SUCCESS;
	}

	enc_str = ldb_binary_encode(ac, el->values[0]);
	if (enc_str == NULL) {
		return ldb_module_oom(ac->module);
	}

	ok = (strcmp(enc_str, "1.2.840.113556.1.2.49") == 0);
	if (ok) {
		ret = samldb_generate_next_mapiid(ac, schema,
						  &new_mapiid);
		if (ret != LDB_SUCCESS) {
			return ret;
		}

		ldb_msg_remove_element(ac->msg, el);
		ret = samdb_msg_add_int(ldb, ac->msg, ac->msg,
					"mAPIID", new_mapiid);
		return ret;
	}

	schema_dn = ldb_get_schema_basedn(ldb_module_get_ctx(ac->module));
	ret = samldb_unique_attr_check(ac, "mAPIID", NULL, schema_dn);
	if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
		return LDB_ERR_UNWILLING_TO_PERFORM;
	} else {
		return ret;
	}
}
static int samldb_schema_add_handle_mapiid(struct samldb_ctx *ac)
{
	int ret;
	bool ok;
	struct ldb_message_element *el;
	const char *enc_str;
	struct ldb_context *ldb;
	struct ldb_dn *schema_dn;
	struct dsdb_schema *schema;
	int32_t new_mapiid = 0;

	 

	ldb = ldb_module_get_ctx(ac->module);
	schema = dsdb_get_schema(ldb, ac);
	schema_dn = ldb_get_schema_basedn(ldb);

	ret = dsdb_get_expected_new_values(ac,
					   ac->msg,
					   "mAPIID",
					   &el,
					   ac->req->operation);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	if (el == NULL) {
		return LDB_SUCCESS;
	}

	enc_str = ldb_binary_encode(ac, el->values[0]);
	if (enc_str == NULL) {
		return ldb_module_oom(ac->module);
	}

	ok = (strcmp(enc_str, "1.2.840.113556.1.2.49") == 0);
	if (ok) {
		ret = samldb_generate_next_mapiid(ac, schema,
						  &new_mapiid);
		if (ret != LDB_SUCCESS) {
			return ret;
		}

		ldb_msg_remove_element(ac->msg, el);
		ret = samdb_msg_add_int(ldb, ac->msg, ac->msg,
					"mAPIID", new_mapiid);
		return ret;
	}

	schema_dn = ldb_get_schema_basedn(ldb_module_get_ctx(ac->module));
	ret = samldb_unique_attr_check(ac, "mAPIID", NULL, schema_dn);
	if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
		return LDB_ERR_UNWILLING_TO_PERFORM;
	} else {
		return ret;
	}
}