internal ADObject GetDomain() { this.Init(); GetADDomainRequest getADDomainRequest = new GetADDomainRequest(); GetADDomainResponse aDDomain = this._topoMgmt.GetADDomain(this._sessionHandle, getADDomainRequest); if (aDDomain.Domain == null) { return null; } else { ActiveDirectoryDomain domain = aDDomain.Domain; ADObject aDObject = new ADObject(); aDObject.Add("objectGUID", domain.ObjectGuid); aDObject.Add("name", domain.Name); aDObject.Add("distinguishedName", domain.DistinguishedName); aDObject.Add("objectClass", domain.ObjectClass); aDObject.Add("msDS-AllowedDNSSuffixes", domain.AllowedDNSSuffixes); aDObject.Add("objectSid", new SecurityIdentifier(domain.DomainSID, 0)); aDObject.Add("msDS-Behavior-Version", (ADDomainMode)domain.DomainMode); aDObject.Add("managedBy", domain.ManagedBy); aDObject.Add("msDS-LogonTimeSyncInterval", domain.LastLogonReplicationInterval); aDObject.Add("SubordinateReferences", domain.SubordinateReferences); aDObject.Add("DNSRoot", domain.DNSRoot); aDObject.Add("LostAndFoundContainer", domain.LostAndFoundContainer); aDObject.Add("DeletedObjectsContainer", domain.DeletedObjectsContainer); aDObject.Add("QuotasContainer", domain.QuotasContainer); aDObject.Add("ReadOnlyReplicaDirectoryServers", domain.ReadOnlyReplicaDirectoryServer); aDObject.Add("ReplicaDirectoryServers", domain.ReplicaDirectoryServer); aDObject.Add("LinkedGroupPolicyObjects", domain.AppliedGroupPolicies); aDObject.Add("ChildDomains", domain.ChildDomains); aDObject.Add("ComputersContainer", domain.ComputersContainer); aDObject.Add("DomainControllersContainer", domain.DomainControllersContainer); aDObject.Add("ForeignSecurityPrincipalsContainer", domain.ForeignSecurityPrincipalsContainer); aDObject.Add("Forest", domain.Forest); aDObject.Add("InfrastructureMaster", domain.InfrastructureMaster); aDObject.Add("NetBIOSName", domain.NetBIOSName); aDObject.Add("PDCEmulator", domain.PDCEmulator); aDObject.Add("ParentDomain", domain.ParentDomain); aDObject.Add("RIDMaster", domain.RIDMaster); aDObject.Add("SystemsContainer", domain.SystemsContainer); aDObject.Add("UsersContainer", domain.UsersContainer); ADPropertyValueCollection aDPropertyValueCollection = new ADPropertyValueCollection(); if (domain.ObjectTypes != null) { aDPropertyValueCollection.AddRange(domain.ObjectTypes); } aDObject.ObjectTypes = aDPropertyValueCollection; return aDObject; } }
private ADObject CreateRichADObject(ADObject resultEntry) { ADPropertyValueCollection item; ADObject aDObject = new ADObject(); aDObject.DistinguishedName = resultEntry.DistinguishedName; bool flag = false; bool flag1 = false; bool flag2 = false; foreach (string propertyName in resultEntry.PropertyNames) { if (flag || string.Compare(propertyName, "distinguishedName", StringComparison.OrdinalIgnoreCase) != 0) { if (flag1 || string.Compare(propertyName, "objectClass", StringComparison.OrdinalIgnoreCase) != 0) { if (flag2 || string.Compare(propertyName, "objectGUID", StringComparison.OrdinalIgnoreCase) != 0) { if (!this._schemaTranslationEnabled) { aDObject.Add(propertyName, resultEntry[propertyName]); } else { aDObject.Add(propertyName, this._typeConverter.ConvertFromRaw(propertyName, resultEntry[propertyName])); } } else { item = resultEntry[propertyName]; if (item.Count <= 0) { Guid? nullable = null; aDObject.ObjectGuid = nullable; } else { object objectGuidObj = item[0]; if (objectGuidObj is string) { byte[] objGuidBytes = Encoding.Default.GetBytes ((string)objectGuidObj); try { aDObject.ObjectGuid = new Guid?(new Guid(objGuidBytes)); } catch { aDObject.ObjectGuid = null; } } else if (objectGuidObj is byte[]) { aDObject.ObjectGuid = new Guid?(new Guid((byte[])item[0])); } else { aDObject.ObjectGuid = null; } } flag2 = true; } } else { item = resultEntry[propertyName]; if (item.Count <= 0) { aDObject.ObjectClass = null; } else { aDObject.ObjectClass = (string)item[item.Count - 1]; aDObject.ObjectTypes = item; } flag1 = true; } } else { flag = true; } } aDObject.IsSearchResult = true; return aDObject; }
private ADObject CreateRichADObject(ADObject resultEntry) { ADPropertyValueCollection item; ADObject aDObject = new ADObject(); aDObject.DistinguishedName = resultEntry.DistinguishedName; bool flag = false; bool flag1 = false; bool flag2 = false; foreach (string propertyName in resultEntry.PropertyNames) { if (flag || string.Compare(propertyName, "distinguishedName", StringComparison.OrdinalIgnoreCase) != 0) { if (flag1 || string.Compare(propertyName, "objectClass", StringComparison.OrdinalIgnoreCase) != 0) { if (flag2 || string.Compare(propertyName, "objectGUID", StringComparison.OrdinalIgnoreCase) != 0) { if (!this._schemaTranslationEnabled) { aDObject.Add(propertyName, resultEntry[propertyName]); } else { aDObject.Add(propertyName, this._typeConverter.ConvertFromRaw(propertyName, resultEntry[propertyName])); } } else { item = resultEntry[propertyName]; if (item.Count <= 0) { Guid?nullable = null; aDObject.ObjectGuid = nullable; } else { object objectGuidObj = item[0]; if (objectGuidObj is string) { byte[] objGuidBytes = Encoding.Default.GetBytes((string)objectGuidObj); try { aDObject.ObjectGuid = new Guid?(new Guid(objGuidBytes)); } catch { aDObject.ObjectGuid = null; } } else if (objectGuidObj is byte[]) { aDObject.ObjectGuid = new Guid?(new Guid((byte[])item[0])); } else { aDObject.ObjectGuid = null; } } flag2 = true; } } else { item = resultEntry[propertyName]; if (item.Count <= 0) { aDObject.ObjectClass = null; } else { aDObject.ObjectClass = (string)item[item.Count - 1]; aDObject.ObjectTypes = item; } flag1 = true; } } else { flag = true; } } aDObject.IsSearchResult = true; return(aDObject); }
public void ClearProperty(string path, Collection<string> propertyToClear) { this.Trace(DebugLogLevel.Verbose, "Entering ClearProperty"); PSObject pSObject = new PSObject(); if (!this.IsValidRootDSEPath(path)) { path = this.ValidateAndNormalizePath(path); if (propertyToClear != null) { if (base.ShouldProcess(path, "Clear")) { ADProviderCommonParameters dynamicParameters = base.DynamicParameters as ADProviderCommonParameters; ADSessionInfo sessionInfo = this.GetSessionInfo(dynamicParameters, base.Credential, this.ExtendedDriveInfo); if (this.ValidateDynamicParameters(path, dynamicParameters, sessionInfo)) { ADObject aDObject = new ADObject(); aDObject.DistinguishedName = path; ADActiveObject aDActiveObject = new ADActiveObject(sessionInfo, aDObject); foreach (string str in propertyToClear) { aDObject.Add(str, null); } using (aDActiveObject) { try { aDActiveObject.Update(); this.Trace(DebugLogLevel.Verbose, "ClearProperty: ADObject updated in store."); aDObject = this.GetValidatedADObject(path, propertyToClear, dynamicParameters, base.Credential, this.ExtendedDriveInfo); } catch (ADException aDException1) { ADException aDException = aDException1; base.WriteError(ADUtilities.GetErrorRecord(aDException, "ADProvider:ClearProperty:ADError", path)); this.Trace(DebugLogLevel.Error, "Leaving ClearProperty: ADException: AD error"); return; } catch (UnauthorizedAccessException unauthorizedAccessException1) { UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1; base.WriteError(ADUtilities.GetErrorRecord(unauthorizedAccessException, "ADProvider:ClearProperty:AccessDenied", path)); this.Trace(DebugLogLevel.Error, "Leaving ClearProperty: ADException: access denied"); return; } catch (AuthenticationException authenticationException1) { AuthenticationException authenticationException = authenticationException1; base.WriteError(ADUtilities.GetErrorRecord(authenticationException, "ADProvider:ClearProperty:InvalidCredentials", path)); this.Trace(DebugLogLevel.Error, "Leaving ClearProperty: ADException: invalid credentials"); return; } catch (InvalidOperationException invalidOperationException1) { InvalidOperationException invalidOperationException = invalidOperationException1; base.WriteError(ADUtilities.GetErrorRecord(invalidOperationException, "ADProvider:ClearProperty:InvalidOperation", path)); this.Trace(DebugLogLevel.Error, "Leaving ClearProperty: ADException: invalid operation"); return; } foreach (string str1 in propertyToClear) { if (!aDObject.Contains(str1)) { continue; } pSObject.Properties.Add(new PSNoteProperty(str1, aDObject[str1].Value)); } this.WriteADObjectProperties(aDObject, pSObject, dynamicParameters, this.ExtendedDriveInfo); this.Trace(DebugLogLevel.Verbose, "Leaving ClearProperty"); return; } return; } else { this.Trace(DebugLogLevel.Error, "Leaving ClearProperty: ValidateDynamicParameters: returned false"); return; } } else { this.Trace(DebugLogLevel.Info, "Leaving ClearProperty: ShouldProcess returned false."); return; } } else { base.WriteError(ADUtilities.GetErrorRecord(new ArgumentException(StringResources.ADProviderPropertiesToClearNotSpecified), "ADProvider:ClearProperty:propertyToClear", path)); this.Trace(DebugLogLevel.Error, "Leaving ClearProperty: ArgumentException: propertyToClear is clear"); return; } } else { base.WriteError(ADUtilities.GetErrorRecord(new NotSupportedException(StringResources.ADProviderOperationNotSupportedForRootDSE), "NotSupported", path)); this.Trace(DebugLogLevel.Error, "Leaving ClearProperty: NotSupportedException: path is rootdse"); return; } }
internal ADObject GetDomain() { this.Init(); GetADDomainRequest getADDomainRequest = new GetADDomainRequest(); GetADDomainResponse aDDomain = this._topoMgmt.GetADDomain(this._sessionHandle, getADDomainRequest); if (aDDomain.Domain == null) { return(null); } else { ActiveDirectoryDomain domain = aDDomain.Domain; ADObject aDObject = new ADObject(); aDObject.Add("objectGUID", domain.ObjectGuid); aDObject.Add("name", domain.Name); aDObject.Add("distinguishedName", domain.DistinguishedName); aDObject.Add("objectClass", domain.ObjectClass); aDObject.Add("msDS-AllowedDNSSuffixes", domain.AllowedDNSSuffixes); aDObject.Add("objectSid", new SecurityIdentifier(domain.DomainSID, 0)); aDObject.Add("msDS-Behavior-Version", (ADDomainMode)domain.DomainMode); aDObject.Add("managedBy", domain.ManagedBy); aDObject.Add("msDS-LogonTimeSyncInterval", domain.LastLogonReplicationInterval); aDObject.Add("SubordinateReferences", domain.SubordinateReferences); aDObject.Add("DNSRoot", domain.DNSRoot); aDObject.Add("LostAndFoundContainer", domain.LostAndFoundContainer); aDObject.Add("DeletedObjectsContainer", domain.DeletedObjectsContainer); aDObject.Add("QuotasContainer", domain.QuotasContainer); aDObject.Add("ReadOnlyReplicaDirectoryServers", domain.ReadOnlyReplicaDirectoryServer); aDObject.Add("ReplicaDirectoryServers", domain.ReplicaDirectoryServer); aDObject.Add("LinkedGroupPolicyObjects", domain.AppliedGroupPolicies); aDObject.Add("ChildDomains", domain.ChildDomains); aDObject.Add("ComputersContainer", domain.ComputersContainer); aDObject.Add("DomainControllersContainer", domain.DomainControllersContainer); aDObject.Add("ForeignSecurityPrincipalsContainer", domain.ForeignSecurityPrincipalsContainer); aDObject.Add("Forest", domain.Forest); aDObject.Add("InfrastructureMaster", domain.InfrastructureMaster); aDObject.Add("NetBIOSName", domain.NetBIOSName); aDObject.Add("PDCEmulator", domain.PDCEmulator); aDObject.Add("ParentDomain", domain.ParentDomain); aDObject.Add("RIDMaster", domain.RIDMaster); aDObject.Add("SystemsContainer", domain.SystemsContainer); aDObject.Add("UsersContainer", domain.UsersContainer); ADPropertyValueCollection aDPropertyValueCollection = new ADPropertyValueCollection(); if (domain.ObjectTypes != null) { aDPropertyValueCollection.AddRange(domain.ObjectTypes); } aDObject.ObjectTypes = aDPropertyValueCollection; return(aDObject); } }
public void SetSecurityDescriptor(string path, ObjectSecurity securityDescriptor) { ADPropertyValueCollection aDPropertyValueCollection; this.Trace(DebugLogLevel.Verbose, "Entering SetSecurityDescriptor"); Collection<string> strs = new Collection<string>(); if (!this.IsValidRootDSEPath(path)) { path = this.ValidateAndNormalizePath(path); if (securityDescriptor != null) { ActiveDirectorySecurity activeDirectorySecurity = securityDescriptor as ActiveDirectorySecurity; if (activeDirectorySecurity != null) { if (base.ShouldProcess(path, "Set")) { ADSessionInfo sessionInfo = this.GetSessionInfo(null, base.Credential, this.ExtendedDriveInfo); ADObject aDObject = new ADObject(); aDObject.DistinguishedName = path; ADActiveObject aDActiveObject = new ADActiveObject(sessionInfo, aDObject); strs.Add("ntSecurityDescriptor"); aDActiveObject.SecurityDescriptorFlags = SecurityMasks.None; using (aDActiveObject) { try { if (!aDObject.Contains("ntSecurityDescriptor")) { aDPropertyValueCollection = new ADPropertyValueCollection(); aDObject.Add("ntSecurityDescriptor", aDPropertyValueCollection); } else { aDPropertyValueCollection = aDObject["ntSecurityDescriptor"]; } aDObject["ntSecurityDescriptor"].Value = activeDirectorySecurity; aDActiveObject.Update(); aDObject = this.GetValidatedADObject(path, strs, null, base.Credential, this.ExtendedDriveInfo); } catch (ADException aDException1) { ADException aDException = aDException1; base.WriteError(ADUtilities.GetErrorRecord(aDException, "ADProvider:SetSecurityDescriptor:ADError", path)); this.Trace(DebugLogLevel.Error, "Leaving SetSecurityDescriptor: ADException: AD error"); return; } catch (UnauthorizedAccessException unauthorizedAccessException1) { UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1; base.WriteError(ADUtilities.GetErrorRecord(unauthorizedAccessException, "ADProvider:SetSecurityDescriptor:AccessDenied", path)); this.Trace(DebugLogLevel.Error, "Leaving SetSecurityDescriptor: ADException: access denied"); return; } catch (AuthenticationException authenticationException1) { AuthenticationException authenticationException = authenticationException1; base.WriteError(ADUtilities.GetErrorRecord(authenticationException, "ADProvider:SetSecurityDescriptor:InvalidCredentials", path)); this.Trace(DebugLogLevel.Error, "Leaving SetSecurityDescriptor: ADException: invalid credentials"); return; } catch (InvalidOperationException invalidOperationException1) { InvalidOperationException invalidOperationException = invalidOperationException1; base.WriteError(ADUtilities.GetErrorRecord(invalidOperationException, "ADProvider:SetSecurityDescriptor:InvalidOperation", path)); this.Trace(DebugLogLevel.Error, "Leaving SetSecurityDescriptor: ADException: invalid operation"); return; } this.WriteADObjectSecurityDescriptor(aDObject, activeDirectorySecurity, null, this.ExtendedDriveInfo); this.Trace(DebugLogLevel.Verbose, "Leaving SetSecurityDescriptor"); return; } return; } else { this.Trace(DebugLogLevel.Info, "Leaving SetSecurityDescriptor: ShouldProcess returned false."); return; } } else { base.WriteError(ADUtilities.GetErrorRecord(new ArgumentException("securityDescriptor"), "ADProvider:SetSecurityDescriptor:InvalidSecurityDescriptor", path)); this.Trace(DebugLogLevel.Error, "Leaving SetSecurityDescriptor: ArgumentException: securityDescriptor is of incorrect type"); return; } } else { base.WriteError(ADUtilities.GetErrorRecord(new ArgumentException("securityDescriptor"), "ADProvider:SetSecurityDescriptor:InvalidSecurityDescriptor", path)); this.Trace(DebugLogLevel.Error, "Leaving SetSecurityDescriptor: ArgumentException: securityDescriptor is null"); return; } } else { base.WriteError(ADUtilities.GetErrorRecord(new NotSupportedException(StringResources.ADProviderOperationNotSupportedForRootDSE), "NotSupported", path)); this.Trace(DebugLogLevel.Error, "Leaving SetSecurityDescriptor: NotSupportedException: path is rootdse"); return; } }
public void SetProperty(string path, PSObject propertyValue) { ADPropertyValueCollection aDPropertyValueCollection; ADPropertyValueCollection item; ADPropertyValueCollection aDPropertyValueCollection1; ADPropertyValueCollection item1; PSObject pSObject = new PSObject(); Collection<string> strs = new Collection<string>(); this.Trace(DebugLogLevel.Verbose, "Entering SetProperty"); if (propertyValue != null) { if (!this.IsValidRootDSEPath(path)) { path = this.ValidateAndNormalizePath(path); ADProviderSetPropertyParameters dynamicParameters = base.DynamicParameters as ADProviderSetPropertyParameters; ADSessionInfo sessionInfo = this.GetSessionInfo(dynamicParameters, base.Credential, this.ExtendedDriveInfo); if (this.ValidateDynamicParameters(path, dynamicParameters, sessionInfo)) { ADObject aDObject = new ADObject(); aDObject.DistinguishedName = path; ADActiveObject aDActiveObject = new ADActiveObject(sessionInfo, aDObject); if (dynamicParameters != null) { if (dynamicParameters.ReplacePropertyValue != null) { foreach (string key in dynamicParameters.ReplacePropertyValue.Keys) { object obj = dynamicParameters.ReplacePropertyValue[key]; if (!aDObject.Contains(key)) { aDPropertyValueCollection = new ADPropertyValueCollection(); aDObject.Add(key, aDPropertyValueCollection); } else { aDPropertyValueCollection = aDObject[key]; aDPropertyValueCollection.Clear(); } if (obj.GetType() != typeof(object[])) { aDPropertyValueCollection.Add(obj); } else { object[] objArray = (object[])obj; for (int i = 0; i < (int)objArray.Length; i++) { object obj1 = objArray[i]; aDPropertyValueCollection.Add(obj1); } } strs.Add(key); } } if (dynamicParameters.AddPropertyValue != null) { foreach (string str in dynamicParameters.AddPropertyValue.Keys) { object item2 = dynamicParameters.AddPropertyValue[str]; if (item2 != null) { if (!aDObject.Contains(str)) { item = new ADPropertyValueCollection(); item.TrackChanges = true; aDObject.Add(str, item); } else { item = aDObject[str]; } if (item2.GetType() != typeof(object[])) { item.Add(item2); } else { object[] objArray1 = (object[])item2; for (int j = 0; j < (int)objArray1.Length; j++) { object obj2 = objArray1[j]; item.Add(obj2); } } strs.Add(str); } else { base.WriteError(ADUtilities.GetErrorRecord(new ArgumentException(StringResources.ADProviderPropertyValueCannotBeNull), "ADProvider: SetProperty: InvalidArgument", path)); this.Trace(DebugLogLevel.Error, "Leaving SetProperty: ArgumentException: addPropertyValue has null value."); return; } } } if (dynamicParameters.RemovePropertyValue != null) { foreach (string key1 in dynamicParameters.RemovePropertyValue.Keys) { object item3 = dynamicParameters.RemovePropertyValue[key1]; if (!aDObject.Contains(key1)) { aDPropertyValueCollection1 = new ADPropertyValueCollection(); aDPropertyValueCollection1.TrackChanges = true; aDObject.Add(key1, aDPropertyValueCollection1); } else { aDPropertyValueCollection1 = aDObject[key1]; } if (item3.GetType() != typeof(object[])) { aDPropertyValueCollection1.Remove(item3); } else { object[] objArray2 = (object[])item3; for (int k = 0; k < (int)objArray2.Length; k++) { object obj3 = objArray2[k]; aDPropertyValueCollection1.Remove(obj3); } } strs.Add(key1); } } } foreach (PSMemberInfo property in propertyValue.Properties) { string name = property.Name; object value = property.Value; if (!aDObject.Contains(name)) { item1 = new ADPropertyValueCollection(); item1.TrackChanges = true; aDObject.Add(name, item1); } else { item1 = aDObject[name]; } if (value.GetType() != typeof(object[])) { item1.Value = value; } else { item1.Clear(); object[] objArray3 = (object[])value; for (int l = 0; l < (int)objArray3.Length; l++) { object obj4 = objArray3[l]; item1.Add(obj4); } } strs.Add(name); } if (base.ShouldProcess(path, "Set")) { using (aDActiveObject) { try { aDActiveObject.Update(); aDObject = this.GetValidatedADObject(path, strs, dynamicParameters, base.Credential, this.ExtendedDriveInfo); } catch (ADException aDException1) { ADException aDException = aDException1; base.WriteError(ADUtilities.GetErrorRecord(aDException, "ADProvider:SetProperty:ADError", path)); this.Trace(DebugLogLevel.Error, "Leaving SetProperty: ADException: AD error"); return; } catch (UnauthorizedAccessException unauthorizedAccessException1) { UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1; base.WriteError(ADUtilities.GetErrorRecord(unauthorizedAccessException, "ADProvider:SetProperty:AccessDenied", path)); this.Trace(DebugLogLevel.Error, "Leaving SetProperty: ADException: access denied"); return; } catch (AuthenticationException authenticationException1) { AuthenticationException authenticationException = authenticationException1; base.WriteError(ADUtilities.GetErrorRecord(authenticationException, "ADProvider:SetProperty:InvalidCredentials", path)); this.Trace(DebugLogLevel.Error, "Leaving SetProperty: ADException: invalid credentials"); return; } catch (InvalidOperationException invalidOperationException1) { InvalidOperationException invalidOperationException = invalidOperationException1; base.WriteError(ADUtilities.GetErrorRecord(invalidOperationException, "ADProvider:SetProperty:InvalidOperation", path)); this.Trace(DebugLogLevel.Error, "Leaving SetProperty: ADException: invalid operation"); return; } } foreach (string str1 in strs) { pSObject.Properties.Add(new PSNoteProperty(str1, aDObject[str1].Value)); } this.WriteADObjectProperties(aDObject, pSObject, dynamicParameters, this.ExtendedDriveInfo); this.Trace(DebugLogLevel.Verbose, "Leaving SetProperty"); return; } else { this.Trace(DebugLogLevel.Info, "Leaving SetProperty: ShouldProcess returned false."); } return; } else { this.Trace(DebugLogLevel.Error, "Leaving GetProperty: ValidateDynamicParameters: returned false"); return; } } else { base.WriteError(ADUtilities.GetErrorRecord(new NotSupportedException(StringResources.ADProviderOperationNotSupportedForRootDSE), "NotSupported", path)); this.Trace(DebugLogLevel.Error, "Leaving SetProperty: NotSupportedException: path is rootdse"); return; } } else { base.WriteError(ADUtilities.GetErrorRecord(new ArgumentNullException("propertyValue"), "ADProvider:SetProperty:NullArgument", path)); this.Trace(DebugLogLevel.Error, "Leaving SetProperty: ArgumentNullException: propertyValue is null"); return; } }
protected override void NewItem(string path, string itemTypeName, object newItemValue) { string root; this.Trace(DebugLogLevel.Verbose, "Entering NewItem"); Collection<string> strs = new Collection<string>(); if (this.IsValidPath(path)) { string childName = this.GetChildName(path); ADProvider aDProvider = this; string str = path; if (this.ExtendedDriveInfo != null) { root = this.ExtendedDriveInfo.Root; } else { root = ""; } string parentPath = aDProvider.GetParentPath(str, root); if (!this.IsValidRootDSEPath(parentPath)) { parentPath = this.ValidateAndNormalizePath(parentPath); string str1 = ADPathModule.MakePath(parentPath, childName, ADPathFormat.X500); if (string.IsNullOrEmpty(itemTypeName)) { base.WriteError(ADUtilities.GetErrorRecord(new ArgumentException("itemTypeName"), "ADProvider:NewItem:InvalidItemTypeName", path)); this.Trace(DebugLogLevel.Error, "Leaving NewItem: ArgumentException: itemType is invalid (null or empty)"); } if (base.ShouldProcess(path, "New")) { ADProviderCommonParameters dynamicParameters = base.DynamicParameters as ADProviderCommonParameters; ADSessionInfo sessionInfo = this.GetSessionInfo(dynamicParameters, base.Credential, this.ExtendedDriveInfo); if (this.ValidateDynamicParameters(path, dynamicParameters, sessionInfo)) { if (itemTypeName.Equals("directory", StringComparison.OrdinalIgnoreCase)) { itemTypeName = "organizationalUnit"; } ADObject aDObject = new ADObject(str1, itemTypeName); if (newItemValue != null) { Hashtable hashtables = newItemValue as Hashtable; if (hashtables == null) { base.WriteError(ADUtilities.GetErrorRecord(new ArgumentNullException("newItemValue"), "ADProvider:NewItem:InvalidNewItemValue", path)); this.Trace(DebugLogLevel.Error, "Leaving NewItem: ArgumentNullException: newItemValue is null"); } foreach (string key in hashtables.Keys) { object item = hashtables[key]; if (item != null) { aDObject.Add(key, hashtables[key]); strs.Add(key); } else { base.WriteError(ADUtilities.GetErrorRecord(new ArgumentException("newItemValue", "Property value to be added cannot be null."), "ADProvider: NewItem: InvalidArgument", path)); this.Trace(DebugLogLevel.Error, "Leaving NewItem: ArgumentException: newItemValue has null value."); return; } } } ADActiveObject aDActiveObject = null; try { try { aDActiveObject = new ADActiveObject(sessionInfo, aDObject); aDActiveObject.Create(); if (ProtectedFromDeletionUtil.ShouldProtectByDefault(aDObject.ObjectClass)) { aDObject.TrackChanges = true; if (ProtectedFromDeletionUtil.ProtectFromAccidentalDeletion(aDObject, sessionInfo)) { aDActiveObject.Update(); } } aDObject = this.GetValidatedADObject(str1, strs, base.DynamicParameters as ADProviderCommonParameters, base.Credential, this.ExtendedDriveInfo); sessionInfo.ServerType = this.GetServerType(sessionInfo); } catch (ADException aDException1) { ADException aDException = aDException1; base.WriteError(ADUtilities.GetErrorRecord(aDException, "ADProvider:NewItem:ADError", path)); this.Trace(DebugLogLevel.Error, "Leaving NewItem: ADException: AD error"); return; } catch (UnauthorizedAccessException unauthorizedAccessException1) { UnauthorizedAccessException unauthorizedAccessException = unauthorizedAccessException1; base.WriteError(ADUtilities.GetErrorRecord(unauthorizedAccessException, "ADProvider:NewItem:AccessDenied", path)); this.Trace(DebugLogLevel.Error, "Leaving NewItem: ADException: access denied"); return; } catch (AuthenticationException authenticationException1) { AuthenticationException authenticationException = authenticationException1; base.WriteError(ADUtilities.GetErrorRecord(authenticationException, "ADProvider:NewItem:InvalidCredentials", path)); this.Trace(DebugLogLevel.Error, "Leaving NewItem: ADException: invalid credentials"); return; } catch (InvalidOperationException invalidOperationException1) { InvalidOperationException invalidOperationException = invalidOperationException1; base.WriteError(ADUtilities.GetErrorRecord(invalidOperationException, "ADProvider:NewItem:InvalidOperation", path)); this.Trace(DebugLogLevel.Error, "Leaving NewItem: ADException: invalid operation"); return; } this.WriteADObject(aDObject, sessionInfo, base.DynamicParameters as ADProviderCommonParameters, this.ExtendedDriveInfo); this.Trace(DebugLogLevel.Verbose, "Leaving NewItem"); return; } finally { if (aDActiveObject != null) { this.Trace(DebugLogLevel.Verbose, "Leaving NewItem: Calling Dispose on ADActiveObject."); aDActiveObject.Dispose(); } } return; } else { this.Trace(DebugLogLevel.Info, "Leaving NewItem: ValidateDynamicParameters returned false."); return; } } else { this.Trace(DebugLogLevel.Info, "Leaving NewItem: ShouldProcess returned false."); return; } } else { base.WriteError(ADUtilities.GetErrorRecord(new NotSupportedException(StringResources.ADProviderOperationNotSupportedForRootDSE), "NotSupported", path)); this.Trace(DebugLogLevel.Error, "Leaving NewItem: NotSupportedException: path is rootdse"); return; } } else { base.WriteError(ADUtilities.GetErrorRecord(new ArgumentException(path), "InvalidPath", path)); this.Trace(DebugLogLevel.Error, "Leaving NewItem: ArgumentException: path is invalid"); return; } }
internal static bool UnprotectFromAccidentalDeletion(ADObject directoryObj, CmdletSessionInfo cmdletSessionInfo) { ADObject cachedObject; bool hasValue; string value = directoryObj["distinguishedName"].Value as string; if (!directoryObj.Contains("nTSecurityDescriptor") || !directoryObj.Contains("sdRightsEffective")) { cachedObject = ProtectedFromDeletionUtil.GetCachedObject(value, ProtectedFromDeletionUtil.AttributesToFetchOnObject, cmdletSessionInfo); } else { cachedObject = directoryObj; ProtectedFromDeletionUtil.AddObjectToCache(directoryObj, cmdletSessionInfo); } if (cachedObject != null) { bool? nullable = ProtectedFromDeletionUtil.IsProtectedFromDeletion(cachedObject, cmdletSessionInfo); if (nullable.HasValue) { bool? nullable1 = nullable; if (nullable1.GetValueOrDefault()) { hasValue = false; } else { hasValue = nullable1.HasValue; } if (!hasValue) { if (Utils.HasFlagsSet((int)cachedObject["sdRightsEffective"].Value, 4)) { ActiveDirectorySecurity activeDirectorySecurity = (ActiveDirectorySecurity)cachedObject["nTSecurityDescriptor"].Value; activeDirectorySecurity.RemoveAccessRule(ProtectedFromDeletionUtil.ACEConstants.DeleteAndDeleteTreeAccessRule); if (!directoryObj.Contains("nTSecurityDescriptor")) { directoryObj.Add("nTSecurityDescriptor", activeDirectorySecurity); } else { directoryObj["nTSecurityDescriptor"].Value = activeDirectorySecurity; } return true; } else { object[] objArray = new object[1]; objArray[0] = directoryObj["distinguishedName"].Value; throw new ADException(string.Format(CultureInfo.CurrentCulture, StringResources.InsufficientPermissionsToProtectObject, objArray)); } } else { return false; } } else { object[] value1 = new object[1]; value1[0] = directoryObj["distinguishedName"].Value; throw new ADException(string.Format(CultureInfo.CurrentCulture, StringResources.InsufficientPermissionsToProtectObject, value1)); } } else { object[] objArray1 = new object[1]; objArray1[0] = directoryObj["distinguishedName"].Value; throw new ADException(string.Format(CultureInfo.CurrentCulture, StringResources.InsufficientPermissionsToProtectObject, objArray1)); } }
internal static bool ProtectFromAccidentalDeletion(ADObject directoryObj, CmdletSessionInfo cmdletSessionInfo) { ADObject cachedObject; string value = directoryObj["distinguishedName"].Value as string; if (!directoryObj.Contains("nTSecurityDescriptor") || !directoryObj.Contains("sdRightsEffective")) { cachedObject = ProtectedFromDeletionUtil.GetCachedObject(value, ProtectedFromDeletionUtil.AttributesToFetchOnObject, cmdletSessionInfo); } else { cachedObject = directoryObj; ProtectedFromDeletionUtil.AddObjectToCache(directoryObj, cmdletSessionInfo); } if (cachedObject == null || !cachedObject.Contains("nTSecurityDescriptor") || cachedObject["nTSecurityDescriptor"].Value == null) { object[] objArray = new object[1]; objArray[0] = directoryObj["distinguishedName"].Value; throw new ADException(string.Format(CultureInfo.CurrentCulture, StringResources.InsufficientPermissionsToProtectObject, objArray)); } else { if (ProtectedFromDeletionUtil.EveryoneDeniedDeleteAndDeleteTree(cachedObject) || Utils.HasFlagsSet((int)cachedObject["sdRightsEffective"].Value, 4)) { if (!Utils.IsNamingContext(cachedObject) && !Utils.IsDeleted(cachedObject)) { string parentPath = ADPathModule.GetParentPath(value, null, ADPathFormat.X500); ADObject aDObject = ProtectedFromDeletionUtil.GetCachedObject(parentPath, ProtectedFromDeletionUtil.AttributesToFetchOnParent, cmdletSessionInfo); if (aDObject != null && !ProtectedFromDeletionUtil.EveryoneDeniedDeleteChild(aDObject)) { if (Utils.HasFlagsSet((int)aDObject["sdRightsEffective"].Value, 4)) { ActiveDirectorySecurity activeDirectorySecurity = (ActiveDirectorySecurity)aDObject["nTSecurityDescriptor"].Value; activeDirectorySecurity.AddAccessRule(ProtectedFromDeletionUtil.ACEConstants.DeleteChildAccessRule); using (ADActiveObject aDActiveObject = new ADActiveObject(cmdletSessionInfo.ADSessionInfo, aDObject)) { aDObject.TrackChanges = true; aDObject["nTSecurityDescriptor"].Value = activeDirectorySecurity; aDActiveObject.Update(); } } else { object[] value1 = new object[2]; value1[0] = directoryObj["distinguishedName"].Value; value1[1] = aDObject["distinguishedName"].Value; throw new ADException(string.Format(CultureInfo.CurrentCulture, StringResources.InsufficientPermissionsToProtectObjectParent, value1)); } } } ActiveDirectorySecurity activeDirectorySecurity1 = (ActiveDirectorySecurity)cachedObject["nTSecurityDescriptor"].Value; if (ProtectedFromDeletionUtil.EveryoneDeniedDeleteAndDeleteTree(cachedObject)) { return false; } else { activeDirectorySecurity1.AddAccessRule(ProtectedFromDeletionUtil.ACEConstants.DeleteAndDeleteTreeAccessRule); if (!directoryObj.Contains("nTSecurityDescriptor")) { directoryObj.Add("nTSecurityDescriptor", activeDirectorySecurity1); } else { directoryObj["nTSecurityDescriptor"].Value = activeDirectorySecurity1; } return true; } } else { object[] objArray1 = new object[1]; objArray1[0] = directoryObj["distinguishedName"].Value; throw new ADException(string.Format(CultureInfo.CurrentCulture, StringResources.InsufficientPermissionsToProtectObject, objArray1)); } } }