Ejemplo n.º 1
0
        public static SECURITY_INFOS GetSecurityInfos(string ObjectName, SE_OBJECT_TYPE ObjectType)
        {
            var pSidOwner           = IntPtr.Zero;
            var pSidGroup           = IntPtr.Zero;
            var pDacl               = IntPtr.Zero;
            var pSacl               = IntPtr.Zero;
            var pSecurityDescriptor = IntPtr.Zero;
            var info = SecurityInfos.DiscretionaryAcl | SecurityInfos.Owner;

            var infos = new SECURITY_INFOS();

            // get the security infos
            var errorReturn = GetNamedSecurityInfo(ObjectName, ObjectType, info, out pSidOwner, out pSidGroup, out pDacl, out pSacl, out pSecurityDescriptor);

            if (errorReturn != 0)
            {
                return(infos);
            }

            if (ConvertSecurityDescriptorToStringSecurityDescriptor(pSecurityDescriptor, 1, SecurityInfos.DiscretionaryAcl | SecurityInfos.Owner, out var pSddlString, out _))
            {
                infos.SDDL = Marshal.PtrToStringUni(pSddlString) ?? string.Empty;
            }
            var ownerSid = new SecurityIdentifier(pSidOwner);

            infos.Owner = ownerSid.Value;

            if (pSddlString != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(pSddlString);
            }

            if (pSecurityDescriptor != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(pSecurityDescriptor);
            }

            return(infos);
        }
Ejemplo n.º 2
0
        public static SECURITY_INFOS GetSecurityInfos(string ObjectName, SE_OBJECT_TYPE ObjectType)
        {
            var pSidOwner           = IntPtr.Zero;
            var pSidGroup           = IntPtr.Zero;
            var pDacl               = IntPtr.Zero;
            var pSacl               = IntPtr.Zero;
            var pSecurityDescriptor = IntPtr.Zero;
            var info = SecurityInfos.DiscretionaryAcl | SecurityInfos.Owner;

            var infos = new SECURITY_INFOS();

            // get the security infos
            var errorReturn = GetNamedSecurityInfo(ObjectName, ObjectType, info, out pSidOwner, out pSidGroup, out pDacl, out pSacl, out pSecurityDescriptor);

            if (errorReturn != 0)
            {
                return(infos);
            }
            else
            {
                var len = 0;
                //IntPtr pBuffer = IntPtr.Zero;
                var sddlString = "";

                if (ConvertSecurityDescriptorToStringSecurityDescriptor(pSecurityDescriptor, 1, SecurityInfos.DiscretionaryAcl | SecurityInfos.Owner, out sddlString, out len))
                {
                    infos.SecurityDescriptor = new RawSecurityDescriptor(sddlString);
                    infos.SDDL = sddlString;
                }


                var ownerSid = new SecurityIdentifier(pSidOwner);
                infos.Owner = ownerSid.Value;
            }

            return(infos);
        }