//[DebuggerStepThrough] void OnSelectPicture() { var openFileDialog = new OpenFileDialog(); openFileDialog.Filter = ImageExtensions.GraphicFilter; if (openFileDialog.ShowDialog().Value) using (new WaitWrapper()) { _newImage = true; _sourceName = openFileDialog.FileName; if (ImageExtensions.IsSVGGraphics(_sourceName)) { _drawing = SVGConverters.ReadDrawing(_sourceName); _wmf = null; ImageBrush = new DrawingBrush(_drawing); _imageType = ResourceType.Drawing; _svg = File.ReadAllBytes(_sourceName); } else if (ImageExtensions.IsWMFGraphics(_sourceName)) { _wmf = WMFConverter.ReadWMF(_sourceName); _drawing = _wmf == null ? null : _wmf.ToDrawing(); if (_drawing == null) { ImageBrush = new VisualBrush(_wmf.Canvas); _imageType = ResourceType.Visual; } else { _wmf = null; ImageBrush = new DrawingBrush(_drawing); _imageType = ResourceType.Drawing; } } else { _drawing = null; _wmf = null; if (new FileInfo(_sourceName).Length > 0) { ImageBrush = new ImageBrush(new BitmapImage(new Uri(_sourceName))); _imageType = ResourceType.Image; } else { MessageBoxService.Show("Невозможно загрузить пустое изображение"); return; } } OnPropertyChanged(() => ImageBrush); if (UpdateProperty != null) UpdateProperty(false); } }
public ImagePropertiesViewModel(IElementBackground element) { _drawing = null; _wmf = null; _newImage = false; _element = element; _sourceName = _element.BackgroundSourceName; _imageSource = _element.BackgroundImageSource; _svgImageSource = _element.BackgroundSVGImageSource; _imageType = _element.ImageType; SelectPictureCommand = new RelayCommand(OnSelectPicture); RemovePictureCommand = new RelayCommand(OnRemovePicture, CanRemovePicture); UpdateImage(); }
private static CommonSecurityDescriptor CreateInternal(ResourceType resourceType, bool isContainer, string name, SafeHandle handle, AccessControlSections includeSections, bool createByName, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext) { int error; RawSecurityDescriptor rawSD; if (createByName && name == null) { throw new ArgumentNullException(nameof(name)); } else if (!createByName && handle == null) { throw new ArgumentNullException(nameof(handle)); } error = Win32.GetSecurityInfo(resourceType, name, handle, includeSections, out rawSD); if (error != Interop.Errors.ERROR_SUCCESS) { System.Exception exception = null; if (exceptionFromErrorCode != null) { exception = exceptionFromErrorCode(error, name, handle, exceptionContext); } if (exception == null) { if (error == Interop.Errors.ERROR_ACCESS_DENIED) { exception = new UnauthorizedAccessException(); } else if (error == Interop.Errors.ERROR_INVALID_OWNER) { exception = new InvalidOperationException(SR.AccessControl_InvalidOwner); } else if (error == Interop.Errors.ERROR_INVALID_PRIMARY_GROUP) { exception = new InvalidOperationException(SR.AccessControl_InvalidGroup); } else if (error == Interop.Errors.ERROR_INVALID_PARAMETER) { exception = new InvalidOperationException(SR.Format(SR.AccessControl_UnexpectedError, error)); } else if (error == Interop.Errors.ERROR_INVALID_NAME) { exception = new ArgumentException( SR.Argument_InvalidName, nameof(name)); } else if (error == Interop.Errors.ERROR_FILE_NOT_FOUND) { exception = (name == null ? new FileNotFoundException() : new FileNotFoundException(name)); } else if (error == Interop.Errors.ERROR_NO_SECURITY_ON_OBJECT) { exception = new NotSupportedException(SR.AccessControl_NoAssociatedSecurity); } else { Debug.Assert(false, string.Format(CultureInfo.InvariantCulture, "Win32GetSecurityInfo() failed with unexpected error code {0}", error)); exception = new InvalidOperationException(SR.Format(SR.AccessControl_UnexpectedError, error)); } } throw exception; } return new CommonSecurityDescriptor(isContainer, false /* isDS */, rawSD, true); }
protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle handle, AccessControlSections includeSections, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext) : this(resourceType, CreateInternal(resourceType, isContainer, null, handle, includeSections, false, exceptionFromErrorCode, exceptionContext), exceptionFromErrorCode) { }
protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle handle, AccessControlSections includeSections) : this(isContainer, resourceType, handle, includeSections, null, null) { }
private void EnumerateServers(NETRESOURCE pRsrc, ResourceScope scope, ResourceType type, ResourceUsage usage, ResourceDisplayType displayType,string kPath) { uint bufferSize = 16384; IntPtr buffer = Marshal.AllocHGlobal((int) bufferSize); IntPtr handle = IntPtr.Zero; ErrorCodes result; uint cEntries = 1; bool serverenum = false; result = WNetOpenEnum(scope, type, usage, pRsrc, out handle); if (result == ErrorCodes.NO_ERROR) { do { result = WNetEnumResource(handle, ref cEntries, buffer, ref bufferSize); if ((result == ErrorCodes.NO_ERROR)) { Marshal.PtrToStructure(buffer, pRsrc); if(String.Compare(kPath,"")==0) { if ((pRsrc.dwDisplayType == displayType) || (pRsrc.dwDisplayType == ResourceDisplayType.RESOURCEDISPLAYTYPE_DOMAIN)) aData.Add(pRsrc.lpRemoteName + "|" + pRsrc.dwDisplayType ); if ((pRsrc.dwUsage & ResourceUsage.RESOURCEUSAGE_CONTAINER )== ResourceUsage.RESOURCEUSAGE_CONTAINER ) { if ((pRsrc.dwDisplayType == displayType)) { EnumerateServers(pRsrc, scope, type, usage, displayType,kPath); } } } else { if (pRsrc.dwDisplayType == displayType) { aData.Add(pRsrc.lpRemoteName); EnumerateServers(pRsrc, scope, type, usage, displayType,kPath); //return; serverenum = true; } if (!serverenum) { if (pRsrc.dwDisplayType == ResourceDisplayType.RESOURCEDISPLAYTYPE_SHARE) { aData.Add(pRsrc.lpRemoteName + "-share"); } } else { serverenum =false; } if((kPath.IndexOf(pRsrc.lpRemoteName)>=0)||(String.Compare(pRsrc.lpRemoteName,"Microsoft Windows Network")==0)) { EnumerateServers(pRsrc, scope, type, usage, displayType,kPath); //return; } //} } } else if (result != ErrorCodes.ERROR_NO_MORE_ITEMS) break; } while (result != ErrorCodes.ERROR_NO_MORE_ITEMS); WNetCloseEnum(handle); } Marshal.FreeHGlobal((IntPtr) buffer); }
protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, string name, AccessControlSections includeSections) : this(isContainer, resourceType, name, includeSections, null, null) { }
internal NativeObjectSecurity(ResourceType resourceType, CommonSecurityDescriptor securityDescriptor) : this(resourceType, securityDescriptor, null) { }
internal NativeObjectSecurity(ResourceType resourceType, CommonSecurityDescriptor securityDescriptor, ExceptionFromErrorCode exceptionFromErrorCode) : base(securityDescriptor) { _resourceType = resourceType; _exceptionFromErrorCode = exceptionFromErrorCode; }
protected NativeObjectSecurity(bool isContainer, ResourceType resourceType) : base(isContainer) { _resourceType = resourceType; }
protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext) : this(isContainer, resourceType) { _exceptionContext = exceptionContext; _exceptionFromErrorCode = exceptionFromErrorCode; }
[HandleProcessCorruptedStateExceptions] // #endif // FEATURE_CORRUPTING_EXCEPTIONS internal static int GetSecurityInfo( ResourceType resourceType, string name, SafeHandle handle, AccessControlSections accessControlSections, out RawSecurityDescriptor resultSd ) { resultSd = null; // // Demand unmanaged code permission // The integrator layer is free to assert this permission // and, in turn, demand another permission of its caller // new SecurityPermission( SecurityPermissionFlag.UnmanagedCode ).Demand(); int errorCode; IntPtr SidOwner, SidGroup, Dacl, Sacl, ByteArray; SecurityInfos SecurityInfos = 0; Privilege privilege = null; if (( accessControlSections & AccessControlSections.Owner ) != 0 ) { SecurityInfos |= SecurityInfos.Owner; } if (( accessControlSections & AccessControlSections.Group ) != 0 ) { SecurityInfos |= SecurityInfos.Group; } if (( accessControlSections & AccessControlSections.Access ) != 0 ) { SecurityInfos |= SecurityInfos.DiscretionaryAcl; } if (( accessControlSections & AccessControlSections.Audit ) != 0 ) { SecurityInfos |= SecurityInfos.SystemAcl; privilege = new Privilege( Privilege.Security ); } // Ensure that the finally block will execute RuntimeHelpers.PrepareConstrainedRegions(); try { if ( privilege != null ) { try { privilege.Enable(); } catch (PrivilegeNotHeldException) { // we will ignore this exception and press on just in case this is a remote resource } } if ( name != null ) { errorCode = ( int )Win32Native.GetSecurityInfoByName( name, ( uint )resourceType, ( uint )SecurityInfos, out SidOwner, out SidGroup, out Dacl, out Sacl, out ByteArray ); } else if (handle != null) { if (handle.IsInvalid) { throw new ArgumentException( Environment.GetResourceString( "Argument_InvalidSafeHandle" ), "handle" ); } else { errorCode = ( int )Win32Native.GetSecurityInfoByHandle( handle, ( uint )resourceType, ( uint )SecurityInfos, out SidOwner, out SidGroup, out Dacl, out Sacl, out ByteArray ); } } else { // both are null, shouldn't happen throw new SystemException(); } if ( errorCode == Win32Native.ERROR_SUCCESS && IntPtr.Zero.Equals(ByteArray) ) { // // This means that the object doesn't have a security descriptor. And thus we throw // a specific exception for the caller to catch and handle properly. // throw new InvalidOperationException(Environment.GetResourceString( "InvalidOperation_NoSecurityDescriptor" )); } else if (errorCode == Win32Native.ERROR_NOT_ALL_ASSIGNED || errorCode == Win32Native.ERROR_PRIVILEGE_NOT_HELD) { throw new PrivilegeNotHeldException( Privilege.Security ); } else if ( errorCode == Win32Native.ERROR_ACCESS_DENIED || errorCode == Win32Native.ERROR_CANT_OPEN_ANONYMOUS ) { throw new UnauthorizedAccessException(); } if ( errorCode != Win32Native.ERROR_SUCCESS ) { goto Error; } } catch { // protection against exception filter-based luring attacks if ( privilege != null ) { privilege.Revert(); } throw; } finally { if ( privilege != null ) { privilege.Revert(); } } // // Extract data from the returned pointer // uint Length = Win32Native.GetSecurityDescriptorLength( ByteArray ); byte[] BinaryForm = new byte[Length]; Marshal.Copy( ByteArray, BinaryForm, 0, ( int )Length ); Win32Native.LocalFree( ByteArray ); resultSd = new RawSecurityDescriptor( BinaryForm, 0 ); return Win32Native.ERROR_SUCCESS; Error: if ( errorCode == Win32Native.ERROR_NOT_ENOUGH_MEMORY ) { throw new OutOfMemoryException(); } return errorCode; }
[HandleProcessCorruptedStateExceptions] // #endif // FEATURE_CORRUPTING_EXCEPTIONS internal static int SetSecurityInfo( ResourceType type, string name, SafeHandle handle, SecurityInfos securityInformation, SecurityIdentifier owner, SecurityIdentifier group, GenericAcl sacl, GenericAcl dacl ) { int errorCode; int Length; byte[] OwnerBinary = null, GroupBinary = null, SaclBinary = null, DaclBinary = null; Privilege securityPrivilege = null; // // Demand unmanaged code permission // The integrator layer is free to assert this permission // and, in turn, demand another permission of its caller // new SecurityPermission( SecurityPermissionFlag.UnmanagedCode ).Demand(); if ( owner != null ) { Length = owner.BinaryLength; OwnerBinary = new byte[Length]; owner.GetBinaryForm( OwnerBinary, 0 ); } if ( group != null ) { Length = group.BinaryLength; GroupBinary = new byte[Length]; group.GetBinaryForm( GroupBinary, 0 ); } if ( dacl != null ) { Length = dacl.BinaryLength; DaclBinary = new byte[Length]; dacl.GetBinaryForm( DaclBinary, 0 ); } if ( sacl != null ) { Length = sacl.BinaryLength; SaclBinary = new byte[Length]; sacl.GetBinaryForm( SaclBinary, 0 ); } if ( ( securityInformation & SecurityInfos.SystemAcl ) != 0 ) { // // Enable security privilege if trying to set a SACL. // Note: even setting it by handle needs this privilege enabled! // securityPrivilege = new Privilege( Privilege.Security ); } // Ensure that the finally block will execute RuntimeHelpers.PrepareConstrainedRegions(); try { if ( securityPrivilege != null ) { try { securityPrivilege.Enable(); } catch (PrivilegeNotHeldException) { // we will ignore this exception and press on just in case this is a remote resource } } if ( name != null ) { errorCode = ( int )Win32Native.SetSecurityInfoByName( name, ( uint )type, ( uint )securityInformation, OwnerBinary, GroupBinary, DaclBinary, SaclBinary ); } else if (handle != null) { if (handle.IsInvalid) { throw new ArgumentException( Environment.GetResourceString( "Argument_InvalidSafeHandle" ), "handle" ); } else { errorCode = ( int )Win32Native.SetSecurityInfoByHandle( handle, ( uint )type, ( uint )securityInformation, OwnerBinary, GroupBinary, DaclBinary, SaclBinary ); } } else { // both are null, shouldn't happen Contract.Assert( false, "Internal error: both name and handle are null" ); throw new InvalidProgramException(); } if (errorCode == Win32Native.ERROR_NOT_ALL_ASSIGNED || errorCode == Win32Native.ERROR_PRIVILEGE_NOT_HELD) { throw new PrivilegeNotHeldException( Privilege.Security ); } else if ( errorCode == Win32Native.ERROR_ACCESS_DENIED || errorCode == Win32Native.ERROR_CANT_OPEN_ANONYMOUS ) { throw new UnauthorizedAccessException(); } else if ( errorCode != Win32Native.ERROR_SUCCESS ) { goto Error; } } catch { // protection against exception filter-based luring attacks if ( securityPrivilege != null ) { securityPrivilege.Revert(); } throw; } finally { if ( securityPrivilege != null ) { securityPrivilege.Revert(); } } return 0; Error: if ( errorCode == Win32Native.ERROR_NOT_ENOUGH_MEMORY ) { throw new OutOfMemoryException(); } return errorCode; }
public ServerEnum(ResourceScope scope, ResourceType type, ResourceUsage usage, ResourceDisplayType displayType,string kPath) { NETRESOURCE netRoot = new NETRESOURCE(); EnumerateServers(netRoot, scope, type, usage, displayType,kPath); }
// // Wrapper around advapi32.SetNamedSecurityInfoW and advapi32.SetSecurityInfo // internal static int SetSecurityInfo( ResourceType type, string name, SafeHandle handle, SecurityInfos securityInformation, SecurityIdentifier owner, SecurityIdentifier group, GenericAcl sacl, GenericAcl dacl) { int errorCode; int Length; byte[] OwnerBinary = null, GroupBinary = null, SaclBinary = null, DaclBinary = null; Privilege securityPrivilege = null; if (owner != null) { Length = owner.BinaryLength; OwnerBinary = new byte[Length]; owner.GetBinaryForm(OwnerBinary, 0); } if (group != null) { Length = group.BinaryLength; GroupBinary = new byte[Length]; group.GetBinaryForm(GroupBinary, 0); } if (dacl != null) { Length = dacl.BinaryLength; DaclBinary = new byte[Length]; dacl.GetBinaryForm(DaclBinary, 0); } if (sacl != null) { Length = sacl.BinaryLength; SaclBinary = new byte[Length]; sacl.GetBinaryForm(SaclBinary, 0); } if ((securityInformation & SecurityInfos.SystemAcl) != 0) { // // Enable security privilege if trying to set a SACL. // Note: even setting it by handle needs this privilege enabled! // securityPrivilege = new Privilege(Privilege.Security); } try { if (securityPrivilege != null) { try { securityPrivilege.Enable(); } catch (PrivilegeNotHeldException) { // we will ignore this exception and press on just in case this is a remote resource } } if (name != null) { errorCode = (int)Interop.mincore.SetSecurityInfoByName(name, (uint)type, (uint)securityInformation, OwnerBinary, GroupBinary, DaclBinary, SaclBinary); } else if (handle != null) { if (handle.IsInvalid) { throw new ArgumentException( SR.Argument_InvalidSafeHandle, nameof(handle)); } else { errorCode = (int)Interop.mincore.SetSecurityInfoByHandle(handle, (uint)type, (uint)securityInformation, OwnerBinary, GroupBinary, DaclBinary, SaclBinary); } } else { // both are null, shouldn't happen Contract.Assert(false, "Internal error: both name and handle are null"); throw new ArgumentException(); } if (errorCode == Interop.mincore.Errors.ERROR_NOT_ALL_ASSIGNED || errorCode == Interop.mincore.Errors.ERROR_PRIVILEGE_NOT_HELD) { throw new PrivilegeNotHeldException(Privilege.Security); } else if (errorCode == Interop.mincore.Errors.ERROR_ACCESS_DENIED || errorCode == Interop.mincore.Errors.ERROR_CANT_OPEN_ANONYMOUS) { throw new UnauthorizedAccessException(); } else if (errorCode != Interop.mincore.Errors.ERROR_SUCCESS) { goto Error; } } catch { // protection against exception filter-based luring attacks if (securityPrivilege != null) { securityPrivilege.Revert(); } throw; } finally { if (securityPrivilege != null) { securityPrivilege.Revert(); } } return 0; Error: if (errorCode == Interop.mincore.Errors.ERROR_NOT_ENOUGH_MEMORY) { throw new OutOfMemoryException(); } return errorCode; }
protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, string name, AccessControlSections includeSections, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext) : this(resourceType, CreateInternal(resourceType, isContainer, name, null, includeSections, true, exceptionFromErrorCode, exceptionContext), exceptionFromErrorCode) { }
// // Wrapper around advapi32.GetSecurityInfo // internal static int GetSecurityInfo( ResourceType resourceType, string name, SafeHandle handle, AccessControlSections accessControlSections, out RawSecurityDescriptor resultSd ) { resultSd = null; int errorCode; IntPtr SidOwner, SidGroup, Dacl, Sacl, ByteArray; SecurityInfos SecurityInfos = 0; Privilege privilege = null; if ((accessControlSections & AccessControlSections.Owner) != 0) { SecurityInfos |= SecurityInfos.Owner; } if ((accessControlSections & AccessControlSections.Group) != 0) { SecurityInfos |= SecurityInfos.Group; } if ((accessControlSections & AccessControlSections.Access) != 0) { SecurityInfos |= SecurityInfos.DiscretionaryAcl; } if ((accessControlSections & AccessControlSections.Audit) != 0) { SecurityInfos |= SecurityInfos.SystemAcl; privilege = new Privilege(Privilege.Security); } try { if (privilege != null) { try { privilege.Enable(); } catch (PrivilegeNotHeldException) { // we will ignore this exception and press on just in case this is a remote resource } } if (name != null) { errorCode = (int)Interop.mincore.GetSecurityInfoByName(name, (uint)resourceType, (uint)SecurityInfos, out SidOwner, out SidGroup, out Dacl, out Sacl, out ByteArray); } else if (handle != null) { if (handle.IsInvalid) { throw new ArgumentException( SR.Argument_InvalidSafeHandle, nameof(handle)); } else { errorCode = (int)Interop.mincore.GetSecurityInfoByHandle(handle, (uint)resourceType, (uint)SecurityInfos, out SidOwner, out SidGroup, out Dacl, out Sacl, out ByteArray); } } else { // both are null, shouldn't happen // Changing from SystemException to ArgumentException as this code path is indicative of a null name argument // as well as an accessControlSections argument with an audit flag throw new ArgumentException(); } if (errorCode == Interop.mincore.Errors.ERROR_SUCCESS && IntPtr.Zero.Equals(ByteArray)) { // // This means that the object doesn't have a security descriptor. And thus we throw // a specific exception for the caller to catch and handle properly. // throw new InvalidOperationException(SR.InvalidOperation_NoSecurityDescriptor); } else if (errorCode == Interop.mincore.Errors.ERROR_NOT_ALL_ASSIGNED || errorCode == Interop.mincore.Errors.ERROR_PRIVILEGE_NOT_HELD) { throw new PrivilegeNotHeldException(Privilege.Security); } else if (errorCode == Interop.mincore.Errors.ERROR_ACCESS_DENIED || errorCode == Interop.mincore.Errors.ERROR_CANT_OPEN_ANONYMOUS) { throw new UnauthorizedAccessException(); } if (errorCode != Interop.mincore.Errors.ERROR_SUCCESS) { goto Error; } } catch { // protection against exception filter-based luring attacks if (privilege != null) { privilege.Revert(); } throw; } finally { if (privilege != null) { privilege.Revert(); } } // // Extract data from the returned pointer // uint Length = Interop.mincore.GetSecurityDescriptorLength(ByteArray); byte[] BinaryForm = new byte[Length]; Marshal.Copy(ByteArray, BinaryForm, 0, (int)Length); Interop.mincore_obsolete.LocalFree(ByteArray); resultSd = new RawSecurityDescriptor(BinaryForm, 0); return Interop.mincore.Errors.ERROR_SUCCESS; Error: if (errorCode == Interop.mincore.Errors.ERROR_NOT_ENOUGH_MEMORY) { throw new OutOfMemoryException(); } return errorCode; }
private static extern ErrorCodes WNetOpenEnum(ResourceScope dwScope, ResourceType dwType, ResourceUsage dwUsage, NETRESOURCE p, out IntPtr lphEnum);