static int rdn_name_add(struct ldb_module *module, struct ldb_request *req)
{
	struct ldb_context *ldb;
	struct ldb_request *down_req;
	struct rename_context *ac;
	struct ldb_message *msg;
	struct ldb_message_element *attribute;
	const struct ldb_schema_attribute *a;
	const char *rdn_name;
	const struct ldb_val *rdn_val_p;
	struct ldb_val rdn_val;
	unsigned int i;
	int ret;

	ldb = ldb_module_get_ctx(module);

	/* do not manipulate our control entries */
	if (ldb_dn_is_special(req->op.add.message->dn)) {
		return ldb_next_request(module, req);
	}

	ac = talloc_zero(req, struct rename_context);
	if (ac == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	ac->module = module;
	ac->req = req;

	msg = ldb_msg_copy_shallow(req, req->op.add.message);
	if (msg == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	rdn_name = ldb_dn_get_rdn_name(msg->dn);
	if (rdn_name == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}
	
	rdn_val_p = ldb_dn_get_rdn_val(msg->dn);
	if (rdn_val_p == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}
	if (rdn_val_p->length == 0) {
		ldb_asprintf_errstring(ldb, "Empty RDN value on %s not permitted!",
				       ldb_dn_get_linearized(req->op.add.message->dn));
		return LDB_ERR_INVALID_DN_SYNTAX;
	}
	rdn_val = ldb_val_dup(msg, rdn_val_p);

	/* Perhaps someone above us tried to set this? Then ignore it */
	ldb_msg_remove_attr(msg, "name");

	ret = ldb_msg_add_value(msg, "name", &rdn_val, NULL);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	a = ldb_schema_attribute_by_name(ldb, rdn_name);
	if (a == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	attribute = ldb_msg_find_element(msg, rdn_name);
	if (!attribute) {
		/* add entry with normalised RDN information if possible */
		if (a->name != NULL && strcmp(a->name, "*") != 0) {
			ret = ldb_msg_add_value(msg, a->name, &rdn_val, NULL);
		} else {
			ret = ldb_msg_add_value(msg, rdn_name, &rdn_val, NULL);
		}
		if (ret != LDB_SUCCESS) {
			return ret;
		}
	} else {
		/* normalise attribute name if possible */
		if (a->name != NULL && strcmp(a->name, "*") != 0) {
			attribute->name = a->name;
		}
		/* normalise attribute value */
		for (i = 0; i < attribute->num_values; i++) {
			bool matched;
			if (a->syntax->operator_fn) {
				ret = a->syntax->operator_fn(ldb, LDB_OP_EQUALITY, a,
							     &rdn_val, &attribute->values[i], &matched);
				if (ret != LDB_SUCCESS) return ret;
			} else {
				matched = (a->syntax->comparison_fn(ldb, msg,
								    &rdn_val, &attribute->values[i]) == 0);
			}
			if (matched) {
				/* overwrite so it matches in case */
				attribute->values[i] = rdn_val;
				break;
			}
		}
		if (i == attribute->num_values) {
			char *rdn_errstring = talloc_asprintf(ac,
				"RDN mismatch on %s: %s (%.*s) should match one of:", 
				ldb_dn_get_linearized(msg->dn), rdn_name, 
				(int)rdn_val.length, (const char *)rdn_val.data);
			for (i = 0; i < attribute->num_values; i++) {
				rdn_errstring = talloc_asprintf_append(
					rdn_errstring, " (%.*s)",
					(int)attribute->values[i].length, 
					(const char *)attribute->values[i].data);
			}
			ldb_set_errstring(ldb, rdn_errstring);
			/* Match AD's error here */
			return LDB_ERR_INVALID_DN_SYNTAX;
		}
	}

	ret = ldb_build_add_req(&down_req, ldb, req,
				msg,
				req->controls,
				ac, rdn_name_add_callback,
				req);
	if (ret != LDB_SUCCESS) {
		return ret;
	}

	talloc_steal(down_req, msg);

	/* go on with the call chain */
	return ldb_next_request(module, down_req);
}