예제 #1
0
        public static MutexSecurity GetAccessControl(this Mutex mutex)
        {
            if (mutex == null)
            {
                throw new ArgumentNullException(nameof(mutex));
            }

            return(mutex.GetAccessControl());
        }
예제 #2
0
		public void CanSetAndGetMutexSecurity ()
		{
			if (PlatformID.Win32NT != Environment.OSVersion.Platform) {
				Assert.Ignore (); return;
			}

			MutexAccessRule rule; SecurityIdentifier sid;
			AuthorizationRuleCollection rulesA, rulesB, rulesC;
			bool createdNew; MutexSecurity security;
			string name = @"Local\MonoTestMutex";

			using (Mutex mutex = new Mutex(false, name, out createdNew)) {
				Assert.IsTrue (createdNew);

				security = mutex.GetAccessControl ();
				rulesA = security.GetAccessRules (true, false, typeof (SecurityIdentifier));
				Assert.AreNotEqual (0, rulesA.Count);

				// Contrary to what you'd expect, these classes only try to persist sections that
				// that were *changed*. Awful, eh? To be fair, if you retrieve and modify it's fine.
				security = new MutexSecurity ();
				mutex.SetAccessControl (security);

				security = mutex.GetAccessControl ();
				rulesB = security.GetAccessRules (true, false, typeof (SecurityIdentifier));
				Assert.AreEqual (rulesA.Count, rulesB.Count);

				// And here's our dummy change. Observe...
				sid = new SecurityIdentifier( "S-1-5-12-3456-7890");
				rule = new MutexAccessRule (sid, MutexRights.Synchronize, AccessControlType.Allow);

				security = new MutexSecurity ();
				security.RemoveAccessRuleSpecific (rule);
				mutex.SetAccessControl (security);

				security = mutex.GetAccessControl ();
				rulesC = security.GetAccessRules (true, false, typeof (SecurityIdentifier));
				Assert.AreEqual (0, rulesC.Count);
			}
		}
예제 #3
0
        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            client = new WlanClient();
            //Miofile = "C:\\" + DateTime.Now.ToLongDateString() + "-" +DateTime.Now.ToShortTimeString() + "_catture.txt";
            //System.IO.File.AppendAllText("c:\\catture.txt", "PARTOOOOOOOOOOOOOOOOOOOOO\r\n");

            // Initialize data structure
            aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = default_interval;
            //System.IO.File.AppendAllText("c:\\catture.txt", "PARTOOOOOOOOOOOOOOOOOOOOO\r\n");

            //initialize the mutex
            mut = new Mutex(false,"Global\\ServiceMutex");
            MutexSecurity mSec = mut.GetAccessControl();
            var rule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                               MutexRights.Modify
                                | MutexRights.Synchronize
                                | MutexRights.TakeOwnership
                                | MutexRights.ReadPermissions,
                               AccessControlType.Allow);
            mSec.AddAccessRule(rule);
            mut.SetAccessControl(mSec);
            // Set the time interval

            //System.IO.File.AppendAllText("c:\\catture.txt", "PARTOOOOOOOOOOOOOOOOOOOOO\r\n");
            // File Mapping creation
            try
            {
                mmf = MemoryMappedFile.CreateNew("Global\\Broadcast", MappedFileDimension, MemoryMappedFileAccess.ReadWrite);
            }
            catch (Exception e)
            {
                System.IO.File.AppendAllText("c:\\catture.txt", e.ToString());
            }
            var mmfSec = mmf.GetAccessControl();
            mmfSec.SetAccessRule(new AccessRule<MemoryMappedFileRights>(new SecurityIdentifier(WellKnownSidType.WorldSid, null),MemoryMappedFileRights.FullControl | MemoryMappedFileRights.Read, AccessControlType.Allow));
            mmf.SetAccessControl(mmfSec);
            //System.IO.File.AppendAllText("c:\\catture.txt", "PARTOOOOOOOOOOOOOOOOOOOOO\r\n");
            stream = mmf.CreateViewStream(0, MappedFileDimension, MemoryMappedFileAccess.ReadWrite);
            //System.IO.File.AppendAllText("c:\\catture.txt", "FINISCO PARTENZA\r\n");

            aTimer.Enabled = true;
        }
예제 #4
0
            private Mutex CreateMutex()
            {
            	m_mutexname = GetMutexName(m_filename);

                try
                {
                    // Open the mutex.
                    m_mutex = Mutex.OpenExisting(m_mutexname);
                }
                catch (WaitHandleCannotBeOpenedException)
                {
                    // The named mutex does not exist.
                    MutexSecurity mSec = new MutexSecurity();

                    MutexAccessRule rule = new MutexAccessRule(
                        new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                        MutexRights.FullControl, AccessControlType.Allow);
                    mSec.AddAccessRule(rule);

                	bool mutexWasCreated;
                	m_mutex = new Mutex(false, m_mutexname, out mutexWasCreated, mSec);
                }
                catch (UnauthorizedAccessException)
                {
                    // The named mutex exists, but the user does not have the security access required to use it.
                    LogLog.Warn("The named mutex exists, but the user does not have the security access required to use it.");
                    try
                    {
                        m_mutex = Mutex.OpenExisting(m_mutexname, MutexRights.ReadPermissions | MutexRights.ChangePermissions);

                        // Get the current ACL. This requires MutexRights.ReadPermissions.
                        MutexSecurity mSec = m_mutex.GetAccessControl();

                        // Now grant the user the correct rights.
                        MutexAccessRule rule = new MutexAccessRule(
                            new SecurityIdentifier(WellKnownSidType.WorldSid, null), 
                            MutexRights.FullControl, AccessControlType.Allow);
                        mSec.AddAccessRule(rule);

                        // Update the ACL. This requires MutexRights.ChangePermissions.
                        m_mutex.SetAccessControl(mSec);

                        LogLog.Debug("Updated mutex security.");

                       m_mutex = Mutex.OpenExisting(m_mutexname);

                    }
                    catch (UnauthorizedAccessException ex)
                    {
                        LogLog.Error("Unable to change permissions on mutex.", ex);
                        m_mutex = new Mutex(false, m_mutexname);
                    }
                }

                return m_mutex;
            }
 public static MutexSecurity GetAccessControl(this Mutex mutex)
 {
     return(mutex.GetAccessControl());
 }
 public static MutexSecurity GetAccessControl(Mutex mutex)
 {
     return mutex.GetAccessControl();
 }
예제 #7
0
		public void PermissionsActuallyWork ()
		{
			if (PlatformID.Win32NT != Environment.OSVersion.Platform) {
				Assert.Ignore (); return;
			}

			bool createdNew; MutexSecurity security;
			string name = @"Local\MonoTestMutex";

			using (Mutex mutex = new Mutex (false, name, out createdNew)) {
				Assert.IsFalse (mutex.SafeWaitHandle.IsInvalid);
				Assert.IsTrue (createdNew);

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

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

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

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

				security = mutex.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 Mutex bug is fixed.
				// Also, NUnit 2.4 appears to lacks Assert.Pass ().
				Mutex badMutex = new Mutex(false, name);
				if (badMutex.SafeWaitHandle.IsInvalid)
					throw new UnauthorizedAccessException ();
			}
		}