forked from netwrix/pingcastle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPingCastleReportCollection.cs
201 lines (182 loc) · 5.71 KB
/
PingCastleReportCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// Copyright (c) Ping Castle. All rights reserved.
// https://www.pingcastle.com
//
// Licensed under the Non-Profit OSL. See LICENSE file in the project root for full license information.
//
using PingCastle.Data;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Security.Principal;
using System.Text;
namespace PingCastle.Data
{
public class PingCastleReportCollection<T> : ICollection<T> where T : IPingCastleReport
{
Dictionary<string, T> data { get; set; }
// database of FQDN, all SID
Dictionary<string, List<string>> AmbigiousNameReference { get; set; }
Dictionary<string, DomainKey> DomainReference { get; set; }
public PingCastleReportCollection()
{
data = new Dictionary<string, T>();
AmbigiousNameReference = new Dictionary<string, List<string>>();
DomainReference = new Dictionary<string, DomainKey>();
}
public void Add(T item)
{
if (item == null)
return;
// taking the more recent report
var sid = item.Domain.DomainSID;
if (data.ContainsKey(sid))
{
if (data[sid].GenerationDate < item.GenerationDate)
{
data[sid] = item;
}
}
else
{
data.Add(sid, item);
}
}
public void EnrichInformation()
{
// complete domain information
// indeed, data can be FQDN only (trust with remote deleted)
// NetBIOS + SID (SID History and unable to locate a server)
// SID Only (SID History from removed domain)
AmbigiousNameReference.Clear();
var sidreference = new Dictionary<string, DomainKey>();
// first pass - build reference based on SID
foreach (var item in data.Values)
{
foreach (var domainKey in item.DomainKnown)
{
if (domainKey.IsComplete && !sidreference.ContainsKey(domainKey.DomainSID))
{
sidreference.Add(domainKey.DomainSID, domainKey);
}
}
}
// second pass, build AmbigiousNameReference
foreach (var domain in sidreference.Values)
{
if (!AmbigiousNameReference.ContainsKey(domain.DomainName))
AmbigiousNameReference[domain.DomainName] = new List<string> { domain.DomainSID };
else if (!string.IsNullOrEmpty(domain.DomainSID))
if (!AmbigiousNameReference[domain.DomainName].Contains(domain.DomainSID))
AmbigiousNameReference[domain.DomainName].Add(domain.DomainSID);
}
// third pass, update incomplete information based on the information we have
foreach (var item in data.Values)
{
foreach (var domainKey in item.DomainKnown)
{
if (domainKey.IsComplete)
{
continue;
}
// try to complete based on sid
if (!String.IsNullOrEmpty(domainKey.DomainSID) && sidreference.ContainsKey(domainKey.DomainSID))
{
var reference = sidreference[domainKey.DomainSID];
if (string.IsNullOrEmpty(domainKey.DomainNetBIOS))
domainKey.DomainNetBIOS = reference.DomainNetBIOS;
if (string.IsNullOrEmpty(domainKey.DomainName))
domainKey.DomainName = reference.DomainName;
}
else if (!String.IsNullOrEmpty(domainKey.DomainName))
{
foreach (var reference in sidreference.Values)
{
if (reference.DomainName == domainKey.DomainName)
{
if (string.IsNullOrEmpty(domainKey.DomainNetBIOS))
domainKey.DomainNetBIOS = reference.DomainNetBIOS;
break;
}
}
}
else if (!String.IsNullOrEmpty(domainKey.DomainNetBIOS))
{
foreach (var reference in sidreference.Values)
{
if (reference.DomainNetBIOS == domainKey.DomainNetBIOS)
{
if (string.IsNullOrEmpty(domainKey.DomainName))
{
domainKey.DomainName = reference.DomainName;
}
if (string.IsNullOrEmpty(domainKey.DomainName))
domainKey.DomainName = reference.DomainName;
break;
}
}
}
}
}
}
public void Clear()
{
data.Clear();
}
public bool Contains(T item)
{
return data.ContainsValue(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
data.Values.CopyTo(array, arrayIndex);
}
public int Count
{
get { return data.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(T item)
{
if (item == null)
return false;
return data.Remove(item.Domain.DomainSID);
}
public IEnumerator<T> GetEnumerator()
{
return data.Values.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return data.Values.GetEnumerator();
}
public T GetDomain(DomainKey key)
{
if (key == null)
return default(T);
if (key.DomainSID != null)
{
if (data.ContainsKey(key.DomainSID))
return data[key.DomainSID];
return default(T);
}
foreach (T hc in data.Values)
{
if (string.Equals(hc.Domain.DomainName, key.DomainName, StringComparison.InvariantCultureIgnoreCase))
return hc;
}
return default(T);
}
public bool HasDomainAmbigiousName(DomainKey domainKey)
{
if (domainKey == null || string.IsNullOrEmpty(domainKey.DomainName))
return false;
if (AmbigiousNameReference.ContainsKey(domainKey.DomainName))
return AmbigiousNameReference[domainKey.DomainName].Count > 1;
return true;
}
}
}