+ /* We need to, before changing the DB, check if the new DN
+ * exists, so we can return this error to the caller with an
+ * unmodified DB */
+ tdb_key = ltdb_key(module, req->op.rename.newdn);
+ if (!tdb_key.dptr) {
+ talloc_free(msg);
+ return LDB_ERR_OPERATIONS_ERROR;
+ }
+
+ tdb_key_old = ltdb_key(module, req->op.rename.olddn);
+ if (!tdb_key_old.dptr) {
+ talloc_free(msg);
+ talloc_free(tdb_key.dptr);
+ return LDB_ERR_OPERATIONS_ERROR;
+ }
+
+ /* Only declare a conflict if the new DN already exists, and it isn't a case change on the old DN */
+ if (tdb_key_old.dsize != tdb_key.dsize || memcmp(tdb_key.dptr, tdb_key_old.dptr, tdb_key.dsize) != 0) {
+ if (tdb_exists(ltdb->tdb, tdb_key)) {
+ talloc_free(tdb_key_old.dptr);
+ talloc_free(tdb_key.dptr);
+ ldb_asprintf_errstring(ldb_module_get_ctx(module),
+ "Entry %s already exists",
+ ldb_dn_get_linearized(req->op.rename.newdn));
+ /* finding the new record already in the DB is an error */
+ talloc_free(msg);
+ return LDB_ERR_ENTRY_ALREADY_EXISTS;
+ }
+ }
+ talloc_free(tdb_key_old.dptr);
+ talloc_free(tdb_key.dptr);
+