private List <Endpoint> GetUserEndpoints(List <string> userNames) { List <Endpoint> endPoints = new List <Endpoint>(); List <string> citrixClient = CitrixRunningOn(); try { //' Get connection string SqlConnectionStringBuilder connectionString = GetSqlConnectionString(); //' Connect to SQL server instance SqlConnection connection = new SqlConnection { ConnectionString = connectionString.ConnectionString }; //' Invoke SQL command foreach (string userName in userNames) { connection.Open(); SqlCommand command = connection.CreateCommand(); command.CommandText = string.Format("EXEC [dbo].[GetEndpointsForUser] @UserName = N'{0}'", userName); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows == true) { while (reader.Read()) { Endpoint data = new Endpoint(reader["Hostname"].ToString(), reader["OperatingSystem"].ToString(), null, reader["LastLoggedOnUser"].ToString(), false); if (!citrixClient.Contains(data.Hostname) && data.Hostname != runningOn) { endPoints.Add(data); } } reader.Close(); connection.Close(); if (rdp_ComputerGroupEnabled) { ADObject rdp_ComputerGroupObj = GetADObject(rdp_ComputerGroup, ADObjectClass.Group); Parallel.ForEach(endPoints, (endpoint) => { bool computerGroupMembership = false; string computerDescription = string.Empty; ADObject computer = GetADObject(endpoint.Hostname, ADObjectClass.Computer); if (computer.DistingishedName != null) { computerGroupMembership = GetADGroupNestedMemberOf(computer.DistingishedName, rdp_ComputerGroupObj.DistingishedName); } if (computer.Description != null) { computerDescription = computer.Description; } endpoint.Description = computerDescription; endpoint.RDPGW_Allow = computerGroupMembership; }); } else { Parallel.ForEach(endPoints, (endpoint) => { bool computerGroupMembership = false; string computerDescription = string.Empty; ADObject computer = GetADObject(endpoint.Hostname, ADObjectClass.Computer); if (computer.DistingishedName != null) { computerGroupMembership = true; } if (computer.Description != null) { computerDescription = computer.Description; } endpoint.Description = computerDescription; endpoint.RDPGW_Allow = computerGroupMembership; }); } } } } catch (Exception ex) { WriteError((string.Format("Failed to query database for endpoints based on the usernames {0} with error {1}", String.Join(";", userNames), ex)), 1220); } Endpoint customEndpoint = new Endpoint("Custom", "N/A", "N/A", "N/A", false); endPoints.Add(customEndpoint); return(endPoints); }
private void ConditionalRDPConnection(Endpoint endpoint) { if (endpoint != null) { string hostName = string.Empty; bool computerGroupMembership = false; hostName = endpoint.Hostname.ToString(); if (hostName == "Custom") { hostName = Textbox_Computer.Text.ToString(); ADObject computer = GetADObject(hostName, ADObjectClass.Computer); if (computer.DistingishedName != null & computer.DistingishedName != string.Empty) { ADObject rdp_ComputerGroupObj = GetADObject(rdp_ComputerGroup, ADObjectClass.Group); computerGroupMembership = GetADGroupNestedMemberOf(computer.DistingishedName, rdp_ComputerGroupObj.DistingishedName); } else { WriteWarning(string.Format("Computer {0} selected for conection was not found in AD", hostName), 1110); MessageBox.Show(string.Format("Computer {0} selected for conection was not found in AD", hostName), "LDAP Search Exception", MessageBoxButton.OK, MessageBoxImage.Error); } } else { computerGroupMembership = endpoint.RDPGW_Allow; } if (computerGroupMembership) { if (IsPingable(hostName)) { if (IsPortOpen(hostName, 3389, 5)) { WriteInfo(string.Format("User {0} initiated connection to Computer {1}", whoIsRunning, hostName), 1010); Process mstsc = new Process(); mstsc.StartInfo.FileName = "mstsc.exe"; mstsc.StartInfo.Arguments = string.Format("/v:{0} /public {1} {2}", hostName, (MenuItem_MultiMon.IsChecked == true ? "/MultiMon" : string.Empty), (MenuItem_AdminSession.IsChecked == true ? "/admin" : string.Empty)); mstsc.Start(); System.Environment.Exit(0); } else { WriteWarning(string.Format("Computer {0} is not listening to RDP requests", hostName), 1120); MessageBox.Show(string.Format("Computer {0} is not listening to RDP requests", hostName), "RDP port access exception", MessageBoxButton.OK, MessageBoxImage.Error); } } else { WriteWarning(string.Format("Computer {0} is not responding to ping", hostName), 1130); MessageBox.Show(string.Format("Computer {0} is not responding to ping", hostName), "Client ping exception", MessageBoxButton.OK, MessageBoxImage.Error); } } else { WriteWarning(string.Format("Computer {0} is not in the necessary AD Group {1}", hostName, rdp_ComputerGroup), 1140); MessageBox.Show(string.Format("Computer {0} is not the necessary AD Group {1}", hostName, rdp_ComputerGroup), "AD Group Membership Exception", MessageBoxButton.OK, MessageBoxImage.Error); } } }
private ADObject GetADObject(string name, ADObjectClass objectClass) { //' Set empty value for return object and search result ADObject returnValue = new ADObject(null, null, null); SearchResult searchResult = null; //' Get default naming context of current domain string defaultNamingContext = GetADDefaultNamingContext(); //' Construct directory entry for directory searcher DirectoryEntry domain = new DirectoryEntry(defaultNamingContext); DirectorySearcher directorySearcher = new DirectorySearcher(domain); directorySearcher.PropertiesToLoad.Add("distinguishedName"); directorySearcher.PropertiesToLoad.Add("name"); directorySearcher.PropertiesToLoad.Add("description"); switch (objectClass) { case ADObjectClass.DomainController: directorySearcher.Filter = string.Format("(&(objectClass=computer)((dNSHostName={0})))", name); break; case ADObjectClass.Computer: directorySearcher.Filter = string.Format("(&(objectClass=computer)((sAMAccountName={0}$)))", name); break; case ADObjectClass.Group: directorySearcher.Filter = string.Format("(&(objectClass=group)((sAMAccountName={0})))", name); break; case ADObjectClass.User: directorySearcher.Filter = string.Format("(&(objectClass=user)((sAMAccountName={0})))", name); break; case ADObjectClass.OrganizationalUnit: directorySearcher.Filter = string.Format("(&(objectClass=organizationalUnit)((distinguishedName={0})))", name); break; } //' Invoke directory searcher try { searchResult = directorySearcher.FindOne(); } catch (Exception ex) { WriteError((string.Format("Failed to perfom Directory Search with error: {0}", ex)), 1210); } //' Return selected object type value if (searchResult != null) { DirectoryEntry directoryObject = searchResult.GetDirectoryEntry(); returnValue = new ADObject( string.Format("{0}", directoryObject.Properties["name"].Value), string.Format("{0}", directoryObject.Properties["distinguishedName"].Value), string.Format("{0}", directoryObject.Properties["description"].Value) ); } //' Dispose objects directorySearcher.Dispose(); domain.Dispose(); return(returnValue); }