Re[4]: WinForm и Win32Api
От: Ведмедь Россия  
Дата: 08.07.03 13:46
Оценка:
Здравствуйте, Slinger, Вы писали:

S>На самом деле на GotDotNet есть библиотека (может, та же самая) в namespace Microsoft.Win32.Security — от микрософтовских перцев. Могу кинуть — там все очень разумно. Да, а шаблон бы получил с превеликим удовольствием. Заранее спасибо.


Именно эта библиотечка вроде бы и была на CD. Она и используется у меня.

Вот код, только его надо напильником еще доводить под свои нужды, все никак времени не найду сделать из этого пормальную библиотеку


    #region Перечисления для ISecurityInformation
    public class SI_OBJECT_FLAGS
    {
        public const int SI_EDIT_PERMS     = 0x00000000; // always implied
        public const int SI_EDIT_OWNER     = 0x00000001;
        public const int SI_EDIT_AUDITS    = 0x00000002;
        public const int SI_CONTAINER      = 0x00000004;
        public const int SI_READONLY       = 0x00000008;
        public const int SI_ADVANCED       = 0x00000010;
        public const int SI_RESET   = 0x00000020; //equals to SI_RESET_DACL|SI_RESET_SACL|SI_RESET_OWNER
        public const int SI_OWNER_READONLY = 0x00000040;
        public const int SI_EDIT_PROPERTIES      = 0x00000080;
        public const int SI_OWNER_RECURSE  = 0x00000100;
        public const int SI_NO_ACL_PROTECT = 0x00000200;
        public const int SI_NO_TREE_APPLY  = 0x00000400;
        public const int SI_PAGE_TITLE     = 0x00000800;
        public const int SI_SERVER_IS_DC   = 0x00001000;
        public const int SI_RESET_DACL_TREE      = 0x00004000;
        public const int SI_RESET_SACL_TREE      = 0x00008000;
        public const int SI_OBJECT_GUID    = 0x00010000;
        public const int SI_EDIT_EFFECTIVE = 0x00020000;
        public const int SI_RESET_DACL     = 0x00040000;
        public const int SI_RESET_SACL     = 0x00080000;
        public const int SI_RESET_OWNER    = 0x00100000;
        public const int SI_NO_ADDITIONAL_PERMISSION = 0x00200000;
        public const int SI_MAY_WRITE      = 0x10000000; //not sure if user can write permission
    }

    [Flags]
    public enum GET_SECURITY_REQUEST_INFORMATION
    {
        OWNER_SECURITY_INFORMATION = 1,
        GROUP_SECURITY_INFORMATION = 2,
        DACL_SECURITY_INFORMATION = 4,
        SACL_SECURITY_INFORMATION = 8,
    }
    

    public enum SI_CALLBACK_MESSAGE
    {
        PSPCB_ADDREF = 0,
        PSPCB_RELEASE = 1,
        PSPCB_CREATE = 2,
        PSPCB_SI_INITDIALOG = 0x00401//WM_USER + 1
    }


    public enum SI_ACCESS_FLAG
    {
        SI_ACCESS_SPECIFIC = 0x00010000,
        SI_ACCESS_GENERAL = 0x00020000,
        SI_ACCESS_CONTAINER = 0x00040000,
        SI_ACCESS_PROPERTY = 0x00080000
    }
    #endregion


    #region Структуры для ISecurityInformation



    [StructLayout(LayoutKind.Sequential)]
    public struct SI_OBJECT_INFO
    {
        public int dwFlags;
        public IntPtr hInstance;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string szServerName;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string szObjectName;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string szPageTitle;
        public Guid guidObjectType;
    }//struct SI_OBJECT_INFO


    [StructLayout(LayoutKind.Sequential)]
    public struct SI_INHERIT_TYPE
    {
        public IntPtr guidObjectType;
        public uint dwFlags;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string szName;
    }


    [StructLayout(LayoutKind.Sequential)]
    public struct SI_ACCESS
    {
        public IntPtr    guidObjectType;
        public uint mask;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string szName;
        public uint dwFlags;
        public static readonly int SizeOf = Marshal.SizeOf(typeof( SI_ACCESS ));
    }


    [StructLayout(LayoutKind.Sequential)]
    public struct SI_ACCESS_ARR
    {
        [MarshalAs(UnmanagedType.ByValArray)] public SI_ACCESS[] arrs;
    }

    #endregion


    #region Собственно ISecurityInformation
    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("965FC360-16FF-11d0-91CB-00AA00BBB723")]
    public interface ISecurityInformation
    {
        void GetObjectInformation( ref SI_OBJECT_INFO  object_info);
        void GetSecurity( int RequestInformation, IntPtr SecurityDescriptor, bool fDefault);
        void SetSecurity( int RequestInformation, IntPtr SecurityDescriptor );
        void GetAccessRight( IntPtr guidObject, int dwFlags, 
            [MarshalAs(UnmanagedType.LPArray )]out SI_ACCESS[] access,ref uint access_count, ref uint DefaultAccess );
        
        void MapGeneric( IntPtr guidObjectType, IntPtr AceFlags, IntPtr Mask );
        void GetInheritTypes( ref SI_INHERIT_TYPE InheritType, IntPtr InheritTypesCount);
        void PropertySheetPageCallback( IntPtr hwnd, int uMsg, uint  uPage );
    }
    #endregion
    
    
    public class SetSecurityEventArg : EventArgs
    {
        public SetSecurityEventArg( IntPtr sd )
        {
            SecurityDesciptor = sd;
        }
        public IntPtr SecurityDesciptor;
    }

    public delegate void SetSecurityEvent( SetSecurityEventArg e );

    public class SecurityEditor : ISecurityInformation
    {
        #region API функции
        [DllImport("aclui.dll")] //C:\WINDOWS\system32\
        protected static extern bool EditSecurity( IntPtr hwnd, ISecurityInformation psi );

        [DllImport("advapi32.dll")]
        protected static extern void MapGenericMask( IntPtr Mask, ref Microsoft.Win32.Security.Win32Structs.GENERIC_MAPPING map );

        #endregion

        protected SI_OBJECT_INFO _object_info = new SI_OBJECT_INFO();
        protected SI_ACCESS[]    _access_list = new SI_ACCESS[]{};
        protected Microsoft.Win32.Security.Win32Structs.GENERIC_MAPPING _mapping = new Microsoft.Win32.Security.Win32Structs.GENERIC_MAPPING();

        public event SetSecurityEvent OnSetSecurity; 

        /// <summary>
        /// Иногда удобней не структуру давать и устанавливать, а дать наружу только некоторые поля ( Имя обьекта, PageTitle и т.д )
        /// </summary>
        public SI_OBJECT_INFO ObjectInfo
        {
            get
            {
                return _object_info;
            }
            set
            {
                _object_info = value;
            }
        }
        
        /// <summary>
        /// Как пример
        /// _access_list = new SI_ACCESS[1];
        ///_access_list[0] = new SI_ACCESS();
        ///_access_list[0].szName = "Читать";
        ///_access_list[0].dwFlags = (uint)SI_ACCESS_FLAG.SI_ACCESS_GENERAL;
        ///_access_list[0].mask = (uint)AccessType.STANDARD_RIGHTS_READ;
        /// </summary>
        public SI_ACCESS[] AccessList
        {
            get
            {
                return _access_list;
            }
            set
            {
                _access_list = value;
            }
        }


        public Microsoft.Win32.Security.Win32Structs.GENERIC_MAPPING GenericMapping
        {
            get
            {
                return _mapping;
            }
            set
            {
                _mapping = value;
            }
        }
        public void GetObjectInformation( ref SI_OBJECT_INFO  object_info)
        {
            object_info = _object_info;
        }
        public void GetSecurity( int RequestInformation, IntPtr ppSecurityDescriptor, bool fDefault)
        {
        }

        public void SetSecurity( int RequestInformation, IntPtr sd )
        {
            if( OnSetSecurity != null )
                OnSetSecurity( new SetSecurityEventArg( sd ) );
        }

        public void GetAccessRight( IntPtr guidObject, int dwFlags,out SI_ACCESS[] access,ref uint access_count, ref uint DefaultAccess )
        {
            access = _access_list;
            Accesses = _access_list.Length;
            DefaultAccess = 0;
        }

        public void GetInheritTypes( ref SI_INHERIT_TYPE InheritType, IntPtr InheritTypesCount)
        {
            Console.WriteLine("GetInheritTypes not implemented ");
        }

        public void PropertySheetPageCallback( IntPtr hwnd, int uMsg, uint  uPage )
        {
        }

        public void MapGeneric( IntPtr guidObjectType, IntPtr AceFlags, IntPtr Mask )
        {
            MapGenericMask( Mask, ref _mapping );
            return;
        }

        public void ShowDialog( IWin32Window wnd )
        {
            if( wnd !=null )
            {
                EditSecurity( wnd.Handle, this );
                return;
            }
            EditSecurity( IntPtr.Zero, this );
        }

        public static void EditSecurityDesciptor(
            IWin32Window parent,
            SI_OBJECT_INFO object_info,
            SI_ACCESS[] access_list,
            Microsoft.Win32.Security.Win32Structs.GENERIC_MAPPING mapping,
            SetSecurityEvent on_security
            )
        {
            SecurityEditor editor = new SecurityEditor();
            editor.ObjectInfo = object_info;
            editor.AccessList = access_list;
            editor.GenericMapping = mapping;
            editor.OnSetSecurity += on_security;
            editor.ShowDialog( parent );
        }
    }
Да пребудет с тобой Великий Джа
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.