| | 1 | | using System; |
| | 2 | | using System.DirectoryServices.Protocols; |
| | 3 | | using SharpHoundCommonLib.Enums; |
| | 4 | |
|
| | 5 | | namespace SharpHoundCommonLib { |
| | 6 | | public class LdapConnectionWrapper { |
| 0 | 7 | | public LdapConnection Connection { get; private set; } |
| | 8 | | private readonly IDirectoryObject _rootDseEntry; |
| | 9 | | private string _domainSearchBase; |
| | 10 | | private string _configurationSearchBase; |
| | 11 | | private string _schemaSearchBase; |
| | 12 | | private string _server; |
| 0 | 13 | | private string Guid { get; set; } |
| | 14 | | public readonly bool GlobalCatalog; |
| | 15 | | public readonly string PoolIdentifier; |
| | 16 | |
|
| 0 | 17 | | public LdapConnectionWrapper(LdapConnection connection, IDirectoryObject rootDseEntry, bool globalCatalog, |
| 0 | 18 | | string poolIdentifier) { |
| 0 | 19 | | Connection = connection; |
| 0 | 20 | | _rootDseEntry = rootDseEntry; |
| 0 | 21 | | Guid = new Guid().ToString(); |
| 0 | 22 | | GlobalCatalog = globalCatalog; |
| 0 | 23 | | PoolIdentifier = poolIdentifier; |
| 0 | 24 | | } |
| | 25 | |
|
| 0 | 26 | | public string GetServer() { |
| 0 | 27 | | if (_server != null) { |
| 0 | 28 | | return _server; |
| | 29 | | } |
| | 30 | |
|
| 0 | 31 | | _server = _rootDseEntry.GetProperty(LDAPProperties.DNSHostName); |
| 0 | 32 | | return _server; |
| 0 | 33 | | } |
| | 34 | |
|
| 0 | 35 | | public bool GetSearchBase(NamingContext context, out string searchBase) { |
| 0 | 36 | | searchBase = GetSavedContext(context); |
| 0 | 37 | | if (searchBase != null) { |
| 0 | 38 | | return true; |
| | 39 | | } |
| | 40 | |
|
| 0 | 41 | | searchBase = context switch { |
| 0 | 42 | | NamingContext.Default => _rootDseEntry.GetProperty(LDAPProperties.DefaultNamingContext), |
| 0 | 43 | | NamingContext.Configuration => |
| 0 | 44 | | _rootDseEntry.GetProperty(LDAPProperties.ConfigurationNamingContext), |
| 0 | 45 | | NamingContext.Schema => _rootDseEntry.GetProperty(LDAPProperties.SchemaNamingContext), |
| 0 | 46 | | _ => throw new ArgumentOutOfRangeException(nameof(context), context, null) |
| 0 | 47 | | }; |
| | 48 | |
|
| 0 | 49 | | if (searchBase != null) { |
| 0 | 50 | | SaveContext(context, searchBase); |
| 0 | 51 | | return true; |
| | 52 | | } |
| | 53 | |
|
| 0 | 54 | | return false; |
| 0 | 55 | | } |
| | 56 | |
|
| 0 | 57 | | private string GetSavedContext(NamingContext context) { |
| 0 | 58 | | return context switch { |
| 0 | 59 | | NamingContext.Configuration => _configurationSearchBase, |
| 0 | 60 | | NamingContext.Default => _domainSearchBase, |
| 0 | 61 | | NamingContext.Schema => _schemaSearchBase, |
| 0 | 62 | | _ => throw new ArgumentOutOfRangeException(nameof(context), context, null) |
| 0 | 63 | | }; |
| 0 | 64 | | } |
| | 65 | |
|
| 0 | 66 | | public void SaveContext(NamingContext context, string searchBase) { |
| 0 | 67 | | switch (context) { |
| | 68 | | case NamingContext.Default: |
| 0 | 69 | | _domainSearchBase = searchBase; |
| 0 | 70 | | break; |
| | 71 | | case NamingContext.Configuration: |
| 0 | 72 | | _configurationSearchBase = searchBase; |
| 0 | 73 | | break; |
| | 74 | | case NamingContext.Schema: |
| 0 | 75 | | _schemaSearchBase = searchBase; |
| 0 | 76 | | break; |
| | 77 | | default: |
| 0 | 78 | | throw new ArgumentOutOfRangeException(nameof(context), context, null); |
| | 79 | | } |
| 0 | 80 | | } |
| | 81 | |
|
| 0 | 82 | | protected bool Equals(LdapConnectionWrapper other) { |
| 0 | 83 | | return Guid == other.Guid; |
| 0 | 84 | | } |
| | 85 | |
|
| 0 | 86 | | public override bool Equals(object obj) { |
| 0 | 87 | | if (ReferenceEquals(null, obj)) return false; |
| 0 | 88 | | if (ReferenceEquals(this, obj)) return true; |
| 0 | 89 | | if (obj.GetType() != this.GetType()) return false; |
| 0 | 90 | | return Equals((LdapConnectionWrapper)obj); |
| 0 | 91 | | } |
| | 92 | |
|
| 0 | 93 | | public override int GetHashCode() { |
| 0 | 94 | | return (Guid != null ? Guid.GetHashCode() : 0); |
| 0 | 95 | | } |
| | 96 | | } |
| | 97 | | } |