Exemple #1
0
        public static MessageQueueAccessRights GetAccessMask(QueuePath queuePath, string userName)
        {
            var sid = TranslateUserNameToSid(userName);
            var gchSecurityDescriptor = GetSecurityDescriptorHandle(queuePath, (int)SecurityInformation.Dacl);
            var ace     = GetAce(gchSecurityDescriptor.AddrOfPinnedObject(), sid);
            var aceMask = ace.Mask;

            gchSecurityDescriptor.Free();

            return(aceMask);
        }
Exemple #2
0
        private static GCHandle GetSecurityDescriptorHandle(QueuePath queuePath, int securityInformation)
        {
            byte[] securityDescriptorBytes;
            int    length;
            int    lengthNeeded;
            uint   result;

            string formatName = queuePath.ToString();

            result = Security.MQGetQueueSecurity(formatName, securityInformation, IntPtr.Zero, 0, out lengthNeeded);

            if (result != Security.MQ_ERROR_SECURITY_DESCRIPTOR_TOO_SMALL)
            {
                string message = "There was an error calling MQGetQueueSecurity."
                                 + Environment.NewLine
                                 + "Error Number: " + result.ToString()
                                 + Environment.NewLine
                                 + "Error Message: " + Security.GetErrorMessage(result);

                throw new Exception(message);
            }

            length = lengthNeeded;
            securityDescriptorBytes = new byte[length];

            IntPtr   pSecurityDescriptor   = new IntPtr();
            GCHandle gchSecurityDescriptor = GCHandle.Alloc(securityDescriptorBytes, GCHandleType.Pinned);

            pSecurityDescriptor = gchSecurityDescriptor.AddrOfPinnedObject();

            result = Security.MQGetQueueSecurity(formatName, securityInformation, pSecurityDescriptor, length, out lengthNeeded);

            if (result != Security.MQ_OK)
            {
                gchSecurityDescriptor.Free();

                string message = "There was an error calling MQGetQueueSecurity to read the Security Descriptor. "
                                 + Environment.NewLine
                                 + "Error Number: " + result.ToString()
                                 + Environment.NewLine
                                 + "Error Message: " + Security.GetErrorMessage(result);

                throw new Exception(message);
            }

            var securityDescriptor = new SECURITY_DESCRIPTOR();

            Marshal.PtrToStructure(pSecurityDescriptor, securityDescriptor);

            return(gchSecurityDescriptor);
        }
Exemple #3
0
        public static string GetOwner(QueuePath queuePath)
        {
            IntPtr pOwner;
            bool   ownerDefaulted;

            var gchSecurityDescriptor = GetSecurityDescriptorHandle(queuePath, (int)SecurityInformation.Owner);

            Security.GetSecurityDescriptorOwner(gchSecurityDescriptor.AddrOfPinnedObject(), out pOwner, out ownerDefaulted);

            var    ownerSid      = new SecurityIdentifier(pOwner);
            string ownerUserName = TranslateSidToUserName(ownerSid);

            gchSecurityDescriptor.Free();

            return(ownerUserName);
        }
Exemple #4
0
        private static GCHandle GetSecurityDescriptorHandle(QueuePath queuePath, int securityInformation)
        {
            byte[] securityDescriptorBytes;
            int length;
            int lengthNeeded;
            uint result;

            string formatName = queuePath.ToString();

            result = Security.MQGetQueueSecurity(formatName, securityInformation, IntPtr.Zero, 0, out lengthNeeded);

            if (result != Security.MQ_ERROR_SECURITY_DESCRIPTOR_TOO_SMALL)
            {
                string message = "There was an error calling MQGetQueueSecurity."
                    + Environment.NewLine
                    + "Error Number: " + result.ToString()
                    + Environment.NewLine
                    + "Error Message: " + Security.GetErrorMessage(result);

                throw new Exception(message);
            }

            length = lengthNeeded;
            securityDescriptorBytes = new byte[length];

            IntPtr pSecurityDescriptor = new IntPtr();
            GCHandle gchSecurityDescriptor = GCHandle.Alloc(securityDescriptorBytes, GCHandleType.Pinned);
            pSecurityDescriptor = gchSecurityDescriptor.AddrOfPinnedObject();

            result = Security.MQGetQueueSecurity(formatName, securityInformation, pSecurityDescriptor, length, out lengthNeeded);

            if (result != Security.MQ_OK)
            {
                gchSecurityDescriptor.Free();

                string message = "There was an error calling MQGetQueueSecurity to read the Security Descriptor. "
                    + Environment.NewLine
                    + "Error Number: " + result.ToString()
                    + Environment.NewLine
                    + "Error Message: " + Security.GetErrorMessage(result);

                throw new Exception(message);
            }

            var securityDescriptor = new SECURITY_DESCRIPTOR();
            Marshal.PtrToStructure(pSecurityDescriptor, securityDescriptor);

            return gchSecurityDescriptor;
        }
Exemple #5
0
        public static string GetOwner(QueuePath queuePath)
        {
            IntPtr pOwner;
            bool ownerDefaulted;

            var gchSecurityDescriptor = GetSecurityDescriptorHandle(queuePath, (int)SecurityInformation.Owner);

            Security.GetSecurityDescriptorOwner(gchSecurityDescriptor.AddrOfPinnedObject(), out pOwner, out ownerDefaulted);

            var ownerSid = new SecurityIdentifier(pOwner);
            string ownerUserName = TranslateSidToUserName(ownerSid);

            gchSecurityDescriptor.Free();

            return ownerUserName;
        }
Exemple #6
0
        public static MessageQueueAccessRights GetAccessMask(QueuePath queuePath, string userName)
        {
            var sid = TranslateUserNameToSid(userName);
            var gchSecurityDescriptor = GetSecurityDescriptorHandle(queuePath, (int)SecurityInformation.Dacl);
            var ace = GetAce(gchSecurityDescriptor.AddrOfPinnedObject(), sid);
            var aceMask = ace.Mask;

            gchSecurityDescriptor.Free();

            return aceMask;
        }