From 8d95785fa9a1b2897f0524bb876eb3781457875d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come@opensides.be>
Date: Tue, 11 Apr 2017 14:34:01 +0200
Subject: [PATCH] Fixes #5474 Create workstation branch if missing when
 migrating winstation

---
 contrib/bin/fusiondirectory-setup | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/contrib/bin/fusiondirectory-setup b/contrib/bin/fusiondirectory-setup
index 291df4dfc..85b0bed45 100644
--- a/contrib/bin/fusiondirectory-setup
+++ b/contrib/bin/fusiondirectory-setup
@@ -1485,8 +1485,19 @@ sub migrate_winstations
         $entry->replace('cn' => $cn);
         my $newrdn = "cn=".$cn;
         my $dn_old = $entry->dn();
-        my $dn_new = $entry->dn();
-        $dn_new =~ s/^[^,]+,[^,]+,[^,]+,/$newrdn,$workstationrdn,/;
+        $dn_old =~ m/^[^,]+,.*$systemrdn,(.+)$/ or die "Could not parse dn ".$dn_old."\n";
+        my $entrybase = $1;
+        if (!branch_exists($ldap, "$workstationrdn,$entrybase")) {
+          if ($workstationrdn =~ m/^([^,]),([^,])$/) {
+            if (!branch_exists($ldap, "$2,$entrybase")) {
+              create_branch($ldap, $entrybase, $2);
+            }
+            create_branch($ldap, "$2,$entrybase", $1);
+          } else {
+            create_branch($ldap, $entrybase, $workstationrdn);
+          }
+        }
+        my $dn_new = "$newrdn,$workstationrdn,$entrybase";
         $entry->dn($dn_new);
         my @replace = ('fdWorkstation');
         my @classes = $entry->get_value('objectClass');
-- 
GitLab