Exemplo n.º 1
0
        public void PermissionsActuallyWork()
        {
            if (PlatformID.Win32NT != Environment.OSVersion.Platform)
            {
                Assert.Ignore();
            }

            bool   createdNew; EventWaitHandleSecurity security;
            string name = @"Local\MonoTestWaitHandle";

            using (EventWaitHandle handle = new EventWaitHandle(false, EventResetMode.ManualReset,
                                                                name, out createdNew)) {
                Assert.IsFalse(handle.SafeWaitHandle.IsInvalid);
                Assert.IsTrue(createdNew);

                // Make sure our later error will be due to permissions and not some sharing bug.
                bool createdAnotherNew;
                using (EventWaitHandle anotherHandle = new EventWaitHandle(false, EventResetMode.ManualReset,
                                                                           name, out createdAnotherNew)) {
                    Assert.IsFalse(anotherHandle.SafeWaitHandle.IsInvalid);
                    Assert.IsFalse(createdAnotherNew);
                }

                // Let's make a deny all.
                security = handle.GetAccessControl();

                foreach (EventWaitHandleAccessRule rule in security.GetAccessRules
                             (true, false, typeof(SecurityIdentifier)))
                {
                    security.RemoveAccessRuleSpecific(rule);
                }

                Assert.AreEqual(0, security.GetAccessRules(true, false, typeof(SecurityIdentifier)).Count);
                handle.SetAccessControl(security);

                security = handle.GetAccessControl();
                Assert.AreEqual(0, security.GetAccessRules(true, false, typeof(SecurityIdentifier)).Count);

                // MS.NET will throw on the first line below.
                // For Mono testing the latter verifies the rest until the EventWaitHandle bug is fixed.
                // Also, NUnit 2.4 appears to lacks Assert.Pass ().
                EventWaitHandle badHandle = new EventWaitHandle(false, EventResetMode.ManualReset, name);
                if (badHandle.SafeWaitHandle.IsInvalid)
                {
                    throw new UnauthorizedAccessException();
                }
            }
        }
Exemplo n.º 2
0
        private EventWaitHandle CreateAndVerifyEventWaitHandle(bool initialState, EventResetMode mode, string name, EventWaitHandleSecurity expectedSecurity, bool expectedCreatedNew)
        {
            EventWaitHandle eventHandle = CreateEventWaitHandle(initialState, mode, name, expectedSecurity, expectedCreatedNew);

            if (expectedSecurity != null)
            {
                EventWaitHandleSecurity actualSecurity = eventHandle.GetAccessControl();
                VerifyEventWaitHandleSecurity(expectedSecurity, actualSecurity);
            }

            return(eventHandle);
        }
Exemplo n.º 3
0
        public void EventWaitHandle_OpenExisting()
        {
            string name = GetRandomName();
            EventWaitHandleSecurity expectedSecurity = GetEventWaitHandleSecurity(WellKnownSidType.BuiltinUsersSid, EventWaitHandleRights.FullControl, AccessControlType.Allow);

            using EventWaitHandle EventWaitHandleNew = CreateAndVerifyEventWaitHandle(initialState: true, EventResetMode.AutoReset, name, expectedSecurity, expectedCreatedNew: true);

            using EventWaitHandle EventWaitHandleExisting = EventWaitHandleAcl.OpenExisting(name, EventWaitHandleRights.FullControl);

            VerifyHandles(EventWaitHandleNew, EventWaitHandleExisting);
            EventWaitHandleSecurity actualSecurity = EventWaitHandleExisting.GetAccessControl();

            VerifyEventWaitHandleSecurity(expectedSecurity, actualSecurity);
        }
Exemplo n.º 4
0
        public PipeServer(string eventName, string pipeName, Func <uint, byte[], Tuple <ErrCode, byte[], uint> > cmdProc)
        {
            var    eventConnect = new EventWaitHandle(false, EventResetMode.AutoReset, eventName);
            string trustee      = "NT Service\\EpgTimer Service";

            try
            {
                // "EpgTimer Service"のサービスセキュリティ識別子(Service-specific SID)に対するアクセス許可を追加する
                EventWaitHandleSecurity sec = eventConnect.GetAccessControl();
                sec.AddAccessRule(new EventWaitHandleAccessRule(trustee, EventWaitHandleRights.Synchronize, AccessControlType.Allow));
                eventConnect.SetAccessControl(sec);
            }
            catch
            {
                trustee = null;
            }
            var pipe = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, 1024, 1024,
                                                 null, System.IO.HandleInheritability.None, trustee == null ? 0 : PipeAccessRights.ChangePermissions);

            if (trustee != null)
            {
                try
                {
                    PipeSecurity sec = pipe.GetAccessControl();
                    sec.AddAccessRule(new PipeAccessRule(trustee, PipeAccessRights.ReadWrite, AccessControlType.Allow));
                    pipe.SetAccessControl(sec);
                }
                catch
                {
                }
            }

            m_ServerThread = new Thread(new ThreadStart(() =>
            {
                using (eventConnect)
                    using (pipe)
                    {
                        for (;;)
                        {
                            IAsyncResult ar = pipe.BeginWaitForConnection(null, null);
                            eventConnect.Set();
                            using (ar.AsyncWaitHandle)
                            {
                                if (WaitHandle.WaitAny(new WaitHandle[] { m_StopEvent, ar.AsyncWaitHandle }) != 1)
                                {
                                    pipe.Dispose();
                                    ar.AsyncWaitHandle.WaitOne();
                                    break;
                                }
                                pipe.EndWaitForConnection(ar);
                            }
                            if (pipe.IsConnected)
                            {
                                byte[] bHead = new byte[8];
                                if (ReadAll(pipe, bHead, 0, 8) == 8)
                                {
                                    uint cmdParam  = BitConverter.ToUInt32(bHead, 0);
                                    byte[] cmdData = new byte[BitConverter.ToUInt32(bHead, 4)];
                                    if (ReadAll(pipe, cmdData, 0, cmdData.Length) == cmdData.Length)
                                    {
                                        Tuple <ErrCode, byte[], uint> res = cmdProc.Invoke(cmdParam, cmdData);
                                        BitConverter.GetBytes((uint)res.Item1).CopyTo(bHead, 0);
                                        BitConverter.GetBytes(res.Item2 == null ? 0 : res.Item2.Length).CopyTo(bHead, 4);
                                        pipe.Write(bHead, 0, 8);
                                        if (res.Item2 != null && res.Item2.Length > 0)
                                        {
                                            pipe.Write(res.Item2, 0, res.Item2.Length);
                                        }
                                    }
                                }
                                pipe.WaitForPipeDrain();
                                pipe.Disconnect();
                            }
                        }
                    }
            }));
            m_ServerThread.Start();
        }