static void BuildAcldSemaphoreFromConfig() { SemaphoreSecurity semSec = new SemaphoreSecurity(); string authorizationLocation = @".\Server\AuthorizationList.xml"; XPathDocument xpathDoc = new XPathDocument(authorizationLocation); XPathNavigator xpathNav = xpathDoc.CreateNavigator(); xpathNav = xpathNav.SelectSingleNode("remotingAuthorization"); if (xpathNav.HasChildren) { xpathNav.MoveToChild(XPathNodeType.Element); do { AccessControlType accessType = (AccessControlType)Enum.Parse(typeof(AccessControlType), xpathNav.Name, true); xpathNav.MoveToAttribute("users", xpathNav.NamespaceURI); NTAccount account = new NTAccount(xpathNav.Value); SemaphoreAccessRule semRule = new SemaphoreAccessRule( (IdentityReference)account, SemaphoreRights.Synchronize, accessType); Console.WriteLine("{0} access for: {1}", accessType.ToString(), account.Value); xpathNav.MoveToParent(); semSec.AddAccessRule(semRule); } while (xpathNav.MoveToNext(XPathNodeType.Element)); } sem.SetAccessControl(semSec); }
private static Semaphore CreateSemaphore(string fullName, Acl acl) { Semaphore result = null; try { #if !UWP var security = new SemaphoreSecurity(); if (acl != null) { foreach (var identity in acl) { security.AddAccessRule(new SemaphoreAccessRule(identity, SemaphoreRights.Modify | SemaphoreRights.Synchronize, AccessControlType.Allow)); } } result = new Semaphore(1, 1, fullName, out bool _, security); #else result = new Semaphore(1, 1, fullName); #endif return(result); } catch { PlatformSpecificDispose(result); throw; } }
private static Semaphore OpenGlobalSemaphore(string semName, int sInit, int sMax) { Semaphore semaphore = null; bool flag = false; bool flag2 = false; try { semaphore = Semaphore.OpenExisting(semName); } catch (System.Threading.WaitHandleCannotBeOpenedException) { flag = true; } catch (System.UnauthorizedAccessException) { flag2 = true; } if (flag) { //System.Environment.UserDomainName + "\\" + System.Environment.UserName; System.Security.Principal.SecurityIdentifier identity = new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.WorldSid, null); SemaphoreSecurity semaphoreSecurity = new SemaphoreSecurity(); semaphoreSecurity.AddAccessRule(new SemaphoreAccessRule(identity, SemaphoreRights.FullControl, System.Security.AccessControl.AccessControlType.Allow)); bool flag3; semaphore = new Semaphore(sInit, sMax, semName, out flag3, semaphoreSecurity); if (!flag3) { return(null); } } else { if (flag2) { try { semaphore = Semaphore.OpenExisting(semName); //System.Environment.UserDomainName + "\\" + System.Environment.UserName; System.Security.Principal.SecurityIdentifier identity2 = new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.WorldSid, null); SemaphoreSecurity semaphoreSecurity2 = new SemaphoreSecurity(); semaphoreSecurity2.AddAccessRule(new SemaphoreAccessRule(identity2, SemaphoreRights.FullControl, System.Security.AccessControl.AccessControlType.Allow)); semaphore.SetAccessControl(semaphoreSecurity2); semaphore = Semaphore.OpenExisting(semName); } catch (System.UnauthorizedAccessException) { if (semaphore != null) { semaphore.Close(); semaphore.Dispose(); } return(null); } return(semaphore); } } return(semaphore); }
public static Semaphore GetMailboxOperationSemaphore(Guid mailboxGuid, out string name) { name = "SiteMailboxSyncSemaphore_" + mailboxGuid.ToString(); Semaphore result; try { SemaphoreSecurity semaphoreSecurity = new SemaphoreSecurity(); SemaphoreAccessRule rule = new SemaphoreAccessRule(new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null), SemaphoreRights.FullControl, AccessControlType.Allow); semaphoreSecurity.AddAccessRule(rule); bool flag = false; result = new Semaphore(1, 1, name, ref flag, semaphoreSecurity); } catch (IOException innerException) { throw new StorageTransientException(new LocalizedString(string.Format("Failed to create named semaphore for mailbox {0} because of IOException", name)), innerException); } catch (UnauthorizedAccessException innerException2) { throw new StoragePermanentException(new LocalizedString(string.Format("Failed to create named semaphore for mailbox {0} because of UnauthorizedAccessException", name)), innerException2); } catch (WaitHandleCannotBeOpenedException innerException3) { throw new StoragePermanentException(new LocalizedString(string.Format("Failed to create named semaphore for mailbox {0} because of WaitHandleCannotBeOpenedException", name)), innerException3); } return(result); }
public void SetAccessControl(SemaphoreSecurity semaphoreSecurity) { if (semaphoreSecurity == null) { throw new ArgumentNullException("semaphoreSecurity"); } semaphoreSecurity.Persist(base.SafeWaitHandle); }
public void SetAccessControl(SemaphoreSecurity semaphoreSecurity) { if (semaphoreSecurity == null) { throw new ArgumentNullException("semaphoreSecurity"); } throw new NotImplementedException(); }
public void Constructor_IntIntStringBoolSecurity() { bool created = false; SemaphoreSecurity ss = new SemaphoreSecurity(); new Semaphore(0, 1, "secure", out created, ss); Assert.IsTrue(created, "Created"); }
[Category("NotWorking")] // not implemented in Mono public void AccessControl_Unnamed() { Semaphore s = new Semaphore(0, 1, null); SemaphoreSecurity ss = s.GetAccessControl(); Assert.IsNotNull(ss, "GetAccessControl"); s.SetAccessControl(ss); }
private SemaphoreSecurity GetSemaphoreSecurity(WellKnownSidType sid, SemaphoreRights rights, AccessControlType accessControl) { SemaphoreSecurity security = new SemaphoreSecurity(); SecurityIdentifier identity = new SecurityIdentifier(sid, null); SemaphoreAccessRule accessRule = new SemaphoreAccessRule(identity, rights, accessControl); security.AddAccessRule(accessRule); return(security); }
public static Semaphore Create( int initialCount, int maximumCount, string name, out bool createdNew, SemaphoreSecurity semaphoreSecurity) { return(new Semaphore(initialCount, maximumCount, name, out createdNew, semaphoreSecurity)); }
public static void SetAccessControl(this Semaphore semaphore, SemaphoreSecurity semaphoreSecurity) { if (semaphore == null) { throw new ArgumentNullException(nameof(semaphore)); } semaphore.SetAccessControl(semaphoreSecurity); }
public static void SetAccessControl(this Semaphore semaphore, SemaphoreSecurity semaphoreSecurity) { if (semaphoreSecurity == null) { throw new ArgumentNullException(nameof(semaphoreSecurity)); } semaphoreSecurity.Persist(semaphore.GetSafeWaitHandle()); }
public void Semaphore_Create_SpecificSecurity(SemaphoreRights rights, AccessControlType accessControl) { SemaphoreSecurity security = GetSemaphoreSecurity(WellKnownSidType.BuiltinUsersSid, rights, accessControl); CreateAndVerifySemaphore( DefaultInitialCount, DefaultMaximumCount, GetRandomName(), security, expectedCreatedNew: true).Dispose(); }
public static void Start() { var security = new SemaphoreSecurity(); security.AddAccessRule(new SemaphoreAccessRule("Everyone", SemaphoreRights.FullControl, AccessControlType.Allow)); _server = new CassiniDevServer(); _server.StartServer(@"..\..\..\FormUI", 54070, "/", "localhost"); RootUrl = _server.RootUrl; }
internal static bool IsNew(string semaphoreName) { bool isNewInstance; var semaphoreSecurity = new SemaphoreSecurity(); var securityIdentifier = new SecurityIdentifier(WellKnownSidType.WorldSid, null); var semaphoreAccessRule = new SemaphoreAccessRule(securityIdentifier, SemaphoreRights.FullControl, AccessControlType.Allow); semaphoreSecurity.AddAccessRule(semaphoreAccessRule); instanceSemaphore = new Semaphore(0, 1, $"Global\\{semaphoreName}", out isNewInstance, semaphoreSecurity); return(isNewInstance); }
static Semaphore CreateGlobalSemaphoreAccessibleToEveryone(string name) { var semaphoreSecurity = new SemaphoreSecurity(); var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null); var rule = new SemaphoreAccessRule(everyone, SemaphoreRights.FullControl, AccessControlType.Allow); semaphoreSecurity.AddAccessRule(rule); bool createdNew; var semaphore = new Semaphore(1, 1, name, out createdNew, semaphoreSecurity); return(semaphore); }
private static void ShowSecurity(SemaphoreSecurity security) { Console.WriteLine("\r\nCurrent access rules:\r\n"); foreach (SemaphoreAccessRule ar in security.GetAccessRules(true, true, typeof(NTAccount))) { Console.WriteLine(" User: {0}", ar.IdentityReference); Console.WriteLine(" Type: {0}", ar.AccessControlType); Console.WriteLine(" Rights: {0}", ar.SemaphoreRights); Console.WriteLine(); } }
public static void Main() { // Create a string representing the current user. string user = Environment.UserDomainName + "\\" + Environment.UserName; // Create a security object that grants no access. SemaphoreSecurity mSec = new SemaphoreSecurity(); // Add a rule that grants the current user the // right to enter or release the semaphore. SemaphoreAccessRule rule = new SemaphoreAccessRule(user, SemaphoreRights.Synchronize | SemaphoreRights.Modify, AccessControlType.Allow); mSec.AddAccessRule(rule); // Add a rule that denies the current user the // right to change permissions on the semaphore. rule = new SemaphoreAccessRule(user, SemaphoreRights.ChangePermissions, AccessControlType.Deny); mSec.AddAccessRule(rule); // Display the rules in the security object. ShowSecurity(mSec); // Add a rule that allows the current user the // right to read permissions on the semaphore. This rule // is merged with the existing Allow rule. rule = new SemaphoreAccessRule(user, SemaphoreRights.ReadPermissions, AccessControlType.Allow); mSec.AddAccessRule(rule); ShowSecurity(mSec); // Create a rule that allows the current user to // change the owner of the semaphore, and use that rule // to remove the existing allow access rule from // the SemaphoreSecurity object, showing that the user // and access type must match, while the rights are // ignored. Console.WriteLine("Use RemoveAccessRuleAll to remove the Allow rule."); rule = new SemaphoreAccessRule(user, SemaphoreRights.TakeOwnership, AccessControlType.Allow); mSec.RemoveAccessRuleAll(rule); ShowSecurity(mSec); }
public static Semaphore CreateAndGetNamedSemaphore(string accessUserName, string semaphoreName) { Semaphore resultSemaphore = null; string truncatedCreateSemaphoreName = string.Empty; try { bool createdNew = false; truncatedCreateSemaphoreName = CheckRunDataHandles.StripIdOfMachineNames(semaphoreName); // Create rights on the semaphore so the test user can signal it SemaphoreSecurity semaphoreSecurity = new SemaphoreSecurity(); // Allow the user specified in the parameter to signal the semaphore. This user may be the identify specified with the distributed solution. SemaphoreAccessRule rule = new SemaphoreAccessRule(accessUserName, SemaphoreRights.Synchronize | SemaphoreRights.Modify, AccessControlType.Allow); semaphoreSecurity.AddAccessRule(rule); // Allow the current user to signal the semaphore. Note that a different format is required for a local user vs. a domain user string currentUserName = Environment.UserName; // Check if local user or domain user if (!string.IsNullOrEmpty(Environment.UserDomainName)) { currentUserName = Environment.UserDomainName + "\\" + currentUserName; } rule = new SemaphoreAccessRule(currentUserName, SemaphoreRights.Synchronize | SemaphoreRights.Modify, AccessControlType.Allow); semaphoreSecurity.AddAccessRule(rule); // Create the semaphore resultSemaphore = new Semaphore(0, 1, truncatedCreateSemaphoreName, out createdNew, semaphoreSecurity); if (!createdNew) { throw new CheckInfrastructureClientException(string.Format("Semaphore with name='{0}' for user='******' was not created.", truncatedCreateSemaphoreName, accessUserName)); } } catch (UnauthorizedAccessException ex) { if (ex.Message.Contains("Access to the port")) { throw new CheckInfrastructureClientException(string.Format("Semaphore with name='{0}' for user='******' was not created. Is there a duplicate named semaphore?", truncatedCreateSemaphoreName, accessUserName)); } else { throw; } } return(resultSemaphore); }
public void Semaphore_OpenExisting() { string name = GetRandomName(); SemaphoreSecurity expectedSecurity = GetSemaphoreSecurity(WellKnownSidType.BuiltinUsersSid, SemaphoreRights.FullControl, AccessControlType.Allow); using Semaphore semaphoreNew = CreateAndVerifySemaphore(initialCount: 1, maximumCount: 2, name, expectedSecurity, expectedCreatedNew: true); using Semaphore semaphoreExisting = SemaphoreAcl.OpenExisting(name, SemaphoreRights.FullControl); VerifyHandles(semaphoreNew, semaphoreExisting); SemaphoreSecurity actualSecurity = semaphoreExisting.GetAccessControl(); VerifySemaphoreSecurity(expectedSecurity, actualSecurity); }
private Semaphore CreateAndVerifySemaphore(int initialCount, int maximumCount, string name, SemaphoreSecurity expectedSecurity, bool expectedCreatedNew) { Semaphore Semaphore = SemaphoreAcl.Create(initialCount, maximumCount, name, out bool createdNew, expectedSecurity); Assert.NotNull(Semaphore); Assert.Equal(createdNew, expectedCreatedNew); if (expectedSecurity != null) { SemaphoreSecurity actualSecurity = Semaphore.GetAccessControl(); VerifySemaphoreSecurity(expectedSecurity, actualSecurity); } return(Semaphore); }
public void Semaphore_Create_NameMultipleNew(string name) { SemaphoreSecurity security = GetBasicSemaphoreSecurity(); bool expectedCreatedNew = true; using Semaphore semaphore1 = CreateAndVerifySemaphore( DefaultInitialCount, DefaultMaximumCount, name, security, expectedCreatedNew); using Semaphore semaphore2 = CreateAndVerifySemaphore( DefaultInitialCount, DefaultMaximumCount, name, security, expectedCreatedNew); }
public static void Main() { // Create a string representing the current user. string user = Environment.UserDomainName + "\\" + Environment.UserName; // Create a security object that grants no access. SemaphoreSecurity mSec = new SemaphoreSecurity(); // Add a rule that grants the current user the // right to enter or release the semaphore and read the // permissions on the semaphore. SemaphoreAccessRule rule = new SemaphoreAccessRule(user, SemaphoreRights.Synchronize | SemaphoreRights.Modify | SemaphoreRights.ReadPermissions, AccessControlType.Allow); mSec.AddAccessRule(rule); // Add a rule that denies the current user the // right to change permissions on the semaphore. rule = new SemaphoreAccessRule(user, SemaphoreRights.ChangePermissions, AccessControlType.Deny); mSec.AddAccessRule(rule); // Display the rules in the security object. ShowSecurity(mSec); // Create a rule that grants the current user // the right to read permissions on the semaphore, and // take ownership of the semaphore. Use this rule to // remove the right to read permissions from the // Allow rule for the current user. The inclusion // of the right to take ownership has no effect. rule = new SemaphoreAccessRule(user, SemaphoreRights.TakeOwnership | SemaphoreRights.ReadPermissions, AccessControlType.Allow); mSec.RemoveAccessRule(rule); ShowSecurity(mSec); }
public void Semaphore_Create_CreateNewExisting() { string name = GetRandomName(); SemaphoreSecurity security = GetBasicSemaphoreSecurity(); using Semaphore SemaphoreNew = CreateAndVerifySemaphore( DefaultInitialCount, DefaultMaximumCount, name, security, expectedCreatedNew: true); using Semaphore SemaphoreExisting = CreateAndVerifySemaphore( DefaultInitialCount, DefaultMaximumCount, name, security, expectedCreatedNew: false); }
public AspNetTestHost(string physicalDirectory, string virtualDirectory, TimeSpan timeout, Type appDomainProxyType) { if (!typeof(AppDomainProxy).IsAssignableFrom(appDomainProxyType)) { throw new Exception(string.Format("Type: {0} should inherit from {1}", appDomainProxyType, typeof(AppDomainProxy))); } PhysicalDirectory = Path.GetFullPath(physicalDirectory); var security = new SemaphoreSecurity(); security.AddAccessRule(new SemaphoreAccessRule("Everyone", SemaphoreRights.FullControl, AccessControlType.Allow)); bool createdNew_notUsed; var semaphoreName = "Global\\MvcTestingAspNetTestHost" + PhysicalDirectory.GetHashCode(); _enforceSingleInstance = new Semaphore(1, 1, semaphoreName, out createdNew_notUsed, security); try { if (!_enforceSingleInstance.WaitOne(timeout)) { throw new Exception("Could not obtain semaphore: " + semaphoreName); } if (!Directory.Exists(PhysicalDirectory)) { throw new Exception("Could not find directory: " + PhysicalDirectory); } CopyTestBinaries(); _appDomainProxy = (AppDomainProxy)ApplicationHost.CreateApplicationHost(appDomainProxyType, virtualDirectory, PhysicalDirectory); _appDomainProxy.RunCodeInAppDomain(InitHost); } catch { DeleteTestBinaries(); using (_enforceSingleInstance) _enforceSingleInstance.Release(); throw; } }
public Semaphore(int initialCount, int maximumCount, string name, out bool createdNew, SemaphoreSecurity semaphoreSecurity) { if (initialCount < 0) { throw new ArgumentOutOfRangeException("initialCount", "< 0"); } if (maximumCount < 1) { throw new ArgumentOutOfRangeException("maximumCount", "< 1"); } if (initialCount > maximumCount) { throw new ArgumentException("initialCount > maximumCount"); } Handle = CreateSemaphore_internal(initialCount, maximumCount, name, out createdNew); }
private void VerifySemaphoreSecurity(SemaphoreSecurity expectedSecurity, SemaphoreSecurity actualSecurity) { Assert.Equal(typeof(SemaphoreRights), expectedSecurity.AccessRightType); Assert.Equal(typeof(SemaphoreRights), actualSecurity.AccessRightType); List <SemaphoreAccessRule> expectedAccessRules = expectedSecurity.GetAccessRules(includeExplicit: true, includeInherited: false, typeof(SecurityIdentifier)) .Cast <SemaphoreAccessRule>().ToList(); List <SemaphoreAccessRule> actualAccessRules = actualSecurity.GetAccessRules(includeExplicit: true, includeInherited: false, typeof(SecurityIdentifier)) .Cast <SemaphoreAccessRule>().ToList(); Assert.Equal(expectedAccessRules.Count, actualAccessRules.Count); if (expectedAccessRules.Count > 0) { Assert.All(expectedAccessRules, actualAccessRule => { int count = expectedAccessRules.Count(expectedAccessRule => AreAccessRulesEqual(expectedAccessRule, actualAccessRule)); Assert.True(count > 0); }); } }
public static void Main() { // Create a string representing the current user. string user = Environment.UserDomainName + "\\" + Environment.UserName; // Create a security object that grants no access. SemaphoreSecurity mSec = new SemaphoreSecurity(); // Add a rule that grants the current user the // right to enter or release the semaphore and read the // permissions on the semaphore. SemaphoreAccessRule rule = new SemaphoreAccessRule(user, SemaphoreRights.Synchronize | SemaphoreRights.Modify | SemaphoreRights.ReadPermissions, AccessControlType.Allow); mSec.AddAccessRule(rule); // Add a rule that denies the current user the // right to change permissions on the semaphore. rule = new SemaphoreAccessRule(user, SemaphoreRights.ChangePermissions, AccessControlType.Deny); mSec.AddAccessRule(rule); // Display the rules in the security object. ShowSecurity(mSec); // Create a rule that grants the current user // the full control over the semaphore. Use the // ResetAccessRule method to replace both of // the existing rules with the new rule. rule = new SemaphoreAccessRule(user, SemaphoreRights.FullControl, AccessControlType.Allow); mSec.ResetAccessRule(rule); ShowSecurity(mSec); }
public void ConnectByPid(Int32 pid) { var securityMemory = new MemoryMappedFileSecurity(); var securityMutex = new MutexSecurity(); var securitySemaphore = new SemaphoreSecurity(); var securityWaiter = new SemaphoreSecurity(); securityMemory.AddAccessRule(new AccessRule <MemoryMappedFileRights>(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MemoryMappedFileRights.FullControl, AccessControlType.Allow)); securityMutex.AddAccessRule(new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow)); securitySemaphore.AddAccessRule(new SemaphoreAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), SemaphoreRights.FullControl, AccessControlType.Allow)); securityWaiter.AddAccessRule(new SemaphoreAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), SemaphoreRights.FullControl, AccessControlType.Allow)); this.ipcMemory = MemoryMappedFile.CreateNew($"Global\\dr_analyzer_buffer_{pid}", 76012); this.ipcMemory.SetAccessControl(securityMemory); this.ipcMutex = new Mutex(false, $"Global\\dr_analyzer_mutex_{pid}", out bool createdMutex, securityMutex); this.ipcSentSemaphore = new Semaphore(0, 1, $"Global\\dr_analyzer_sent_semaphore_{pid}", out bool createdSentSemaphore, securitySemaphore); this.ipcReceivedSemaphore = new Semaphore(0, 1, $"Global\\dr_analyzer_received_semaphore_{pid}", out bool createdReceivedSemaphore, securitySemaphore); this.ipcWaiterSemaphore = new Semaphore(0, 1, $"Global\\dr_analyzer_waiter_semaphore_{pid}", out bool createdWaiter, securityWaiter); if (!(createdMutex && createdSentSemaphore && createdReceivedSemaphore && createdWaiter)) { this.FreeSharedObjects(); throw new Exception("One of sync object is already created"); } this.queueSem = new Semaphore(0, 1); this.queueMutex = new Mutex(false); this.isExit = false; this.receiverThread = new Thread(this.ReceiverThreadFunc); this.queueThread = new Thread(this.QueueThreadFunc); this.queueThread.Start(); this.receiverThread.Start(); Injector.InjectByPid(pid); this.Active = true; }
public static void Main() { // Create a string representing the current user. string user = Environment.UserDomainName + "\\" + Environment.UserName; // Create a security object that grants no access. SemaphoreSecurity mSec = new SemaphoreSecurity(); // Add a rule that grants the current user the // right to enter or release the semaphore. SemaphoreAccessRule rule = new SemaphoreAccessRule(user, SemaphoreRights.Synchronize | SemaphoreRights.Modify, AccessControlType.Allow); mSec.AddAccessRule(rule); // Add a rule that denies the current user the // right to change permissions on the semaphore. rule = new SemaphoreAccessRule(user, SemaphoreRights.ChangePermissions, AccessControlType.Deny); mSec.AddAccessRule(rule); // Display the rules in the security object. ShowSecurity(mSec); // Add a rule that allows the current user the // right to read permissions on the semaphore. This rule // is merged with the existing Allow rule. rule = new SemaphoreAccessRule(user, SemaphoreRights.ReadPermissions, AccessControlType.Allow); mSec.AddAccessRule(rule); ShowSecurity(mSec); }
public static void SetAccessControl(this Semaphore semaphore, SemaphoreSecurity semaphoreSecurity);