private bool IsNodeExist(string distinguishedName, DirectoryBindingInfo directoryInformation)
        {
            bool result = false;

            if (distinguishedName == null || string.IsNullOrEmpty(distinguishedName.ToString()))
            {
                return(result);
            }
            using (DirectoryEntry directoryEntry = directoryInformation.GetDirectoryEntry(Path.Combine(directoryInformation.LdapBasePath, distinguishedName.ToString())))
            {
                using (DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry))
                {
                    try
                    {
                        directorySearcher.SearchScope = SearchScope.Base;
                        using (SearchResultCollection searchResultCollection = directorySearcher.FindAll())
                        {
                            if (searchResultCollection != null && searchResultCollection.Count > 0)
                            {
                                result = true;
                            }
                        }
                    }
                    catch (InvalidOperationException)
                    {
                    }
                    catch (DirectoryServicesCOMException)
                    {
                    }
                }
            }
            return(result);
        }
 public void UpdateDelayedProperties(Node node, DirectoryBindingInfo directoryInformation)
 {
     if (node == null)
     {
         node = this.Root;
     }
     foreach (Node node2 in node.Children.Values)
     {
         if (node2.Value != null && node2.Value.DelayedProperties.Count <DirectoryProperty>() > 0)
         {
             this.WriteVerbose(string.Format("Updating object {0} with properties that were delayed.", node2.Value.DistinguishedName));
             using (DirectoryEntry directoryEntry = directoryInformation.GetDirectoryEntry(Path.Combine(directoryInformation.LdapBasePath, node2.Value.DistinguishedName.ToString())))
             {
                 foreach (DirectoryProperty directoryProperty in node2.Value.DelayedProperties)
                 {
                     object value = directoryEntry.Properties[directoryProperty.Name].Value;
                     directoryEntry.Properties[directoryProperty.Name].Value = null;
                     for (int i = 0; i < directoryProperty.Values.Count; i++)
                     {
                         if (this.IsNodeExist(node2.Value.DelayedProperties[directoryProperty.Name][i].ToString(), directoryInformation))
                         {
                             if (directoryProperty.Syntax == ActiveDirectorySyntax.Int64)
                             {
                                 this.WriteVerbose(string.Format("Updating Property {0}: {1} ; {2}", directoryProperty.Name, directoryProperty.Syntax, directoryProperty.Values[i]));
                                 directoryEntry.Properties[directoryProperty.Name].Add(this.GetLargeInteger((long)directoryProperty.Values[i]));
                             }
                             else
                             {
                                 this.WriteVerbose(string.Format("Updating Property {0}: {1} ; {2}", directoryProperty.Name, directoryProperty.Syntax, directoryProperty.Values[i]));
                                 directoryEntry.Properties[directoryProperty.Name].Add(directoryProperty.Values[i]);
                             }
                         }
                         else
                         {
                             this.WriteWarning(string.Format("Could not update property {0} on object {1}.  Distinguished Name {2} was not found on target directory.", directoryProperty.Name, node2.Value.DistinguishedName, directoryProperty.Values[i]));
                         }
                     }
                     if (directoryEntry.Properties[directoryProperty.Name].Value == null)
                     {
                         directoryEntry.Properties[directoryProperty.Name].Value = value;
                     }
                 }
                 try
                 {
                     directoryEntry.CommitChanges();
                 }
                 catch (Exception ex)
                 {
                     this.WriteError(new Exception(string.Format("Error updating delayed properties on object {0}.  Inner exception was {1}.", node2.Value.DistinguishedName, ex.Message)));
                 }
             }
         }
     }
     foreach (Node node3 in node.Children.Values)
     {
         this.UpdateDelayedProperties(node3, directoryInformation);
     }
 }
Exemple #3
0
        public static void Import(DirectoryBindingInfo directoryInfo, DirectoryObjectCollection exportedItems, OrganizationConfigurationTree tree, string sourcedomain, string targetdomain, string sourceOrg, string targetOrg, string sourceSiteName, string targetSiteName)
        {
            string[] array = sourcedomain.Split(new char[]
            {
                '.'
            });
            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < array.Length; i++)
            {
                stringBuilder.Append("DC=");
                stringBuilder.Append(array[i]);
                if (i != array.Length - 1)
                {
                    stringBuilder.Append(',');
                }
            }
            array = targetdomain.Split(new char[]
            {
                '.'
            });
            StringBuilder stringBuilder2 = new StringBuilder();

            for (int j = 0; j < array.Length; j++)
            {
                stringBuilder2.Append("DC=");
                stringBuilder2.Append(array[j]);
                if (j != array.Length - 1)
                {
                    stringBuilder2.Append(',');
                }
            }
            string text     = stringBuilder.ToString();
            string text2    = stringBuilder2.ToString();
            string oldValue = "CN=" + sourceOrg + ",CN=Microsoft Exchange,CN=Services,CN=Configuration," + text;
            string newValue = "CN=" + targetOrg + ",CN=Microsoft Exchange,CN=Services,CN=Configuration," + text2;

            if (string.Compare(sourceOrg, targetOrg) != 0)
            {
                tree.Replace(null, oldValue, newValue);
            }
            string text3 = "CN=" + sourceSiteName + ",CN=Sites,CN=Configuration," + text;
            string text4 = "CN=" + targetSiteName + ",CN=Sites,CN=Configuration," + text2;

            if (string.Compare(text3, text4) != 0)
            {
                tree.Replace(null, text3, text4);
            }
            tree.Replace(null, text, text2);
            tree.Replace(null, sourcedomain, targetdomain);
            tree.Import(null, directoryInfo);
            tree.UpdateDelayedProperties(null, directoryInfo);
        }
        private string GetUniqueSamAccount(string p, DirectoryBindingInfo d)
        {
            DirectoryEntry directoryEntry = d.GetDirectoryEntry(d.LdapBasePath + d.DefaultNamingContextDN);

            using (SearchResultCollection searchResultCollection = new DirectorySearcher(directoryEntry)
            {
                Filter = "(samaccountname=" + p + ")"
            }.FindAll())
            {
                if (searchResultCollection != null && searchResultCollection.Count > 0)
                {
                    Random random = new Random();
                    p = random.Next(0, int.MaxValue).ToString();
                }
            }
            return(p);
        }
 public void Import(Node node, DirectoryBindingInfo directoryInformation)
 {
     if (node == null)
     {
         node = this.Root;
     }
     if (string.IsNullOrEmpty(OrganizationConfigurationTree.homemta))
     {
         using (DirectoryEntry directoryEntry = directoryInformation.GetDirectoryEntry(Path.Combine(directoryInformation.LdapBasePath, directoryInformation.ConfigurationNamingContextDN)))
         {
             SearchResult searchResult = new DirectorySearcher(directoryEntry)
             {
                 Filter = "(CN=Microsoft MTA)"
             }.FindOne();
             if (searchResult != null)
             {
                 OrganizationConfigurationTree.homemta = searchResult.Properties["distinguishedName"][0].ToString();
             }
         }
     }
     if (string.IsNullOrEmpty(OrganizationConfigurationTree.homemdb))
     {
         using (DirectoryEntry directoryEntry2 = directoryInformation.GetDirectoryEntry(Path.Combine(directoryInformation.LdapBasePath, directoryInformation.ConfigurationNamingContextDN)))
         {
             SearchResult searchResult2 = new DirectorySearcher(directoryEntry2)
             {
                 Filter = "(objectclass=msExchPrivateMDB)"
             }.FindOne();
             if (searchResult2 != null)
             {
                 OrganizationConfigurationTree.homemdb = searchResult2.Properties["distinguishedName"][0].ToString();
             }
         }
     }
     if (string.IsNullOrEmpty(OrganizationConfigurationTree.msexchowningserver))
     {
         using (DirectoryEntry directoryEntry3 = directoryInformation.GetDirectoryEntry(Path.Combine(directoryInformation.LdapBasePath, directoryInformation.ConfigurationNamingContextDN)))
         {
             SearchResult searchResult3 = new DirectorySearcher(directoryEntry3)
             {
                 Filter = "(msexchowningserver=*)"
             }.FindOne();
             if (searchResult3 != null)
             {
                 OrganizationConfigurationTree.msexchowningserver = searchResult3.Properties["msexchowningserver"][0].ToString();
                 OrganizationConfigurationTree.msexchmasterserveroravailabilitygroup = searchResult3.Properties["msexchmasterserveroravailabilitygroup"][0].ToString();
             }
         }
     }
     foreach (Node node2 in node.Children.Values)
     {
         if (node2.Value != null && !this.IsNodeExist(node2.Value.DistinguishedName, directoryInformation))
         {
             if (this.IsNodeExist(node2.Value.ParentDistinguishedName, directoryInformation))
             {
                 this.WriteVerbose(string.Format("Trying to import node {0}", node2.Value.DistinguishedName));
                 using (DirectoryEntry directoryEntry4 = directoryInformation.GetDirectoryEntry(Path.Combine(directoryInformation.LdapBasePath, node2.Value.ParentDistinguishedName.ToString())))
                 {
                     using (DirectoryEntry directoryEntry5 = directoryEntry4.Children.Add(node2.Name, node2.Value.ObjectClass))
                     {
                         if (node2.Value.Properties.Contains("samaccountname"))
                         {
                             node2.Value.Properties["samaccountname"][0] = this.GetUniqueSamAccount(node2.Value.Properties["samaccountname"][0].ToString(), directoryInformation);
                         }
                         foreach (DirectoryProperty directoryProperty in node2.Value.Properties)
                         {
                             if (directoryProperty.Name.ToLower() == "homemdb")
                             {
                                 directoryProperty.Values[0] = OrganizationConfigurationTree.homemdb;
                             }
                             if (directoryProperty.Name.ToLower() == "homemta")
                             {
                                 directoryProperty.Values[0] = OrganizationConfigurationTree.homemta;
                             }
                             if (directoryProperty.Name.ToLower() == "msexchowningserver" || directoryProperty.Name.ToLower() == "offlineabserver")
                             {
                                 directoryProperty.Values[0] = OrganizationConfigurationTree.msexchowningserver;
                             }
                             if (directoryProperty.Name.ToLower() == "msexchmasterserveroravailabilitygroup")
                             {
                                 directoryProperty.Values[0] = OrganizationConfigurationTree.msexchmasterserveroravailabilitygroup;
                             }
                             this.AddObjectProperties(directoryProperty, directoryEntry5, node2, true, directoryInformation);
                         }
                         try
                         {
                             directoryEntry5.CommitChanges();
                         }
                         catch (Exception ex)
                         {
                             this.WriteError(new Exception(string.Format("Error committing changes to object {0}.  Inner exception was {1}.", node2.Value.DistinguishedName, ex.Message)));
                         }
                         if (node2.Value.ObjectClass.ToLower() == "user" && node2.Value.Properties["useraccountcontrol"][0].ToString() == "512")
                         {
                             directoryEntry5.Invoke("SetPassword", new object[]
                             {
                                 Guid.NewGuid().ToString()
                             });
                             directoryEntry5.Properties["useraccountcontrol"].Value = 512;
                             try
                             {
                                 directoryEntry5.CommitChanges();
                             }
                             catch (Exception ex2)
                             {
                                 this.WriteError(new Exception(string.Format("Error setting temp password on object {0}.  Inner exception was {1}.", node2.Value.DistinguishedName, ex2.Message)));
                             }
                         }
                         foreach (DirectoryProperty property in node2.Value.Properties)
                         {
                             this.AddObjectProperties(property, directoryEntry5, node2, false, directoryInformation);
                             try
                             {
                                 directoryEntry5.CommitChanges();
                             }
                             catch (Exception ex3)
                             {
                                 this.WriteError(new Exception(string.Format("Error setting non mandatory properties on object {0}.  Inner exception was {1}.", node2.Value.DistinguishedName, ex3.Message)));
                             }
                         }
                     }
                     continue;
                 }
             }
             this.WriteError(new Exception("Can't import object " + node2.Value.DistinguishedName + " because its parent object does not exist."));
         }
     }
     foreach (Node node3 in node.Children.Values)
     {
         this.Import(node3, directoryInformation);
     }
 }
 private void AddObjectProperties(DirectoryProperty property, DirectoryEntry newObject, Node childNode, bool importOnlyRequiredProperties, DirectoryBindingInfo directoryInformation)
 {
     if (!property.IsSystemOnly && !property.IsBackLink && property.Values.Count > 0 && !this.excludedProperties.Contains(property.Name.ToLower()) && property.IsRequired == importOnlyRequiredProperties)
     {
         if (property.IsLink)
         {
             bool flag = false;
             foreach (object obj in property.Values)
             {
                 string distinguishedName = (string)obj;
                 if (!this.IsNodeExist(distinguishedName, directoryInformation))
                 {
                     flag = true;
                     break;
                 }
             }
             if (flag)
             {
                 if (property.IsRequired)
                 {
                     newObject.Properties[property.Name].Value = directoryInformation.SchemaNamingContextDN;
                 }
                 childNode.Value.DelayedProperties.Add(property);
                 return;
             }
             for (int i = 0; i < property.Values.Count; i++)
             {
                 this.WriteVerbose(string.Format("Adding Property {0}: {1} ; {2}", property.Name, property.Syntax, property.Values[i]));
                 newObject.Properties[property.Name].Add(property.Values[i]);
             }
             return;
         }
         else
         {
             for (int j = 0; j < property.Values.Count; j++)
             {
                 if (property.Syntax == ActiveDirectorySyntax.Int64)
                 {
                     this.WriteVerbose(string.Format("Adding Property {0}: {1} ; 2", property.Name, property.Syntax, property.Values[j]));
                     newObject.Properties[property.Name].Add(this.GetLargeInteger((long)property.Values[j]));
                 }
                 else
                 {
                     this.WriteVerbose(string.Format("Adding Property {0}: {1}; {2}", property.Name, property.Syntax, property.Values[j]));
                     newObject.Properties[property.Name].Add(property.Values[j]);
                 }
             }
         }
     }
 }
Exemple #7
0
        private static SearchResultCollection GetSchema(string[] propertiesToLoad, string filter, DirectoryBindingInfo directoryBindingInformation)
        {
            new List <SearchResult>();
            DirectoryEntry    directoryEntry    = directoryBindingInformation.GetDirectoryEntry(Path.Combine(directoryBindingInformation.LdapBasePath, directoryBindingInformation.SchemaNamingContextDN));
            DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry);

            directorySearcher.SearchScope = SearchScope.Subtree;
            directorySearcher.PageSize    = int.MaxValue;
            if (!string.IsNullOrEmpty(filter))
            {
                directorySearcher.Filter = filter;
            }
            if (propertiesToLoad != null && propertiesToLoad.Length > 0)
            {
                directorySearcher.PropertiesToLoad.AddRange(propertiesToLoad);
            }
            return(directorySearcher.FindAll());
        }
Exemple #8
0
        public static void UpdateDirectoryObjectProperties(DirectoryObjectCollection exportedItems, DirectoryBindingInfo targetDirectoryBindingInfo)
        {
            DirectoryObjectCollection directoryObjectCollection;

            using (SearchResultCollection schema = OrganizationMigrationManager.GetSchema(null, "(|(objectClass=attributeSchema)(objectClass=classSchema))", targetDirectoryBindingInfo))
            {
                directoryObjectCollection = new DirectoryObjectCollection("Schema", schema);
            }
            foreach (DirectoryObject directoryObject in exportedItems)
            {
                using (ActiveDirectorySchemaClass activeDirectorySchemaClass = ActiveDirectorySchemaClass.FindByName(targetDirectoryBindingInfo.GetDirectoryContext(DirectoryContextType.Forest), directoryObject.Properties["objectclass"][directoryObject.Properties["objectclass"].Values.Count - 1].ToString()))
                {
                    foreach (object obj in activeDirectorySchemaClass.MandatoryProperties)
                    {
                        ActiveDirectorySchemaProperty activeDirectorySchemaProperty = (ActiveDirectorySchemaProperty)obj;
                        if (directoryObject.Properties.Contains(activeDirectorySchemaProperty.Name))
                        {
                            directoryObject.Properties[activeDirectorySchemaProperty.Name].IsRequired = true;
                        }
                    }
                    foreach (object obj2 in activeDirectorySchemaClass.GetAllProperties())
                    {
                        ActiveDirectorySchemaProperty activeDirectorySchemaProperty2 = (ActiveDirectorySchemaProperty)obj2;
                        if (directoryObject.Properties.Contains(activeDirectorySchemaProperty2.Name))
                        {
                            directoryObject.Properties[activeDirectorySchemaProperty2.Name].Syntax = activeDirectorySchemaProperty2.Syntax;
                        }
                    }
                }
                foreach (DirectoryProperty directoryProperty in directoryObject.Properties)
                {
                    DirectoryObject directoryObjectByLdapDisplayName = directoryObjectCollection.GetDirectoryObjectByLdapDisplayName(directoryProperty.Name);
                    if (directoryObjectByLdapDisplayName != null)
                    {
                        if (directoryObjectByLdapDisplayName.Properties.Contains("linkID") && directoryObjectByLdapDisplayName.Properties["linkID"].Values != null && int.Parse(directoryObjectByLdapDisplayName.Properties["linkID"][0].ToString()) % 2 != 0)
                        {
                            directoryProperty.IsBackLink = true;
                        }
                        if (directoryObjectByLdapDisplayName.Properties.Contains("attributeSyntax") && directoryObjectByLdapDisplayName.Properties["attributeSyntax"].Values != null && directoryObjectByLdapDisplayName.Properties["attributeSyntax"][0].ToString().CompareTo("2.5.5.1") == 0)
                        {
                            directoryProperty.IsLink = true;
                        }
                        if (directoryObjectByLdapDisplayName.Properties.Contains("systemOnly") && directoryObjectByLdapDisplayName.Properties["systemOnly"].Values != null && directoryObjectByLdapDisplayName.Properties["systemOnly"][0].ToString().ToLower() == "true")
                        {
                            directoryProperty.IsSystemOnly = true;
                        }
                    }
                }
            }
        }