namespace TB.ADBlock
{
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Globalization;
using System.Text;
public class Utils
{
public static char[] InvalidSAMAccountNameChars = new char[] { '/', '\\', '[', ']', ':', ';', '|', '=', ',', '+', '*', '?', '<', '>', '"' };
internal static bool CanAsContainer(DirectoryEntry de)
{
string schemaClassName = de.SchemaClassName;
if (!(schemaClassName == SchemaClass.organizationalUnit.ToString("F")) && !(schemaClassName == SchemaClass.builtinDomain.ToString("F")))
{
return (schemaClassName == SchemaClass.container.ToString("F"));
}
return true;
}
public static string ConvertDNToDomainName(string dn)
{
StringBuilder sb = new StringBuilder();
foreach (string dc in dn.Split(new char[] { ',' }))
{
sb.AppendFormat("{0}.", dc.Trim().Split(new char[] { '=' })[1]);
}
return sb.ToString(0, sb.Length - 1);
}
public static string ConvertDomainNameToDN(string domainName)
{
StringBuilder sb = new StringBuilder();
foreach (string dc in domainName.Split(new char[] { '.' }))
{
sb.AppendFormat("DC={0},", dc.ToUpper());
}
return sb.ToString(0, sb.Length - 1);
}
public static string ConvertGuidToNativeGuid(Guid guid)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in guid.ToByteArray())
{
sb.AppendFormat("{0:x2}", b);
}
return sb.ToString();
}
public static Guid ConvertNativeGuidToGuid(string nativeGuid)
{
new StringBuilder();
byte[] byteArr = new byte[0x10];
for (int i = 0; i < 0x10; i++)
{
char CS$0$0000 = nativeGuid[2 * i];
char CS$0$0001 = nativeGuid[(2 * i) + 1];
byteArr[i] = byte.Parse(CS$0$0000.ToString().ToUpper() + CS$0$0001.ToString().ToUpper(), NumberStyles.HexNumber);
}
return new Guid(byteArr);
}
public static string ConvertToOctetString(byte[] ids)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in ids)
{
sb.AppendFormat(@"\{0:x2}", b);
}
return sb.ToString();
}
public static string Escape4Query(string cn)
{
return cn.Replace("(", @"\28").Replace(")", @"\29").Replace(@"\", @"\5c");
}
internal static string EscapeDN(string odn)
{
return odn.Replace(@"\", @"\\").Replace("#", @"\#").Replace("+", @"\+").Replace("=", @"\=").Replace("\"", "\\\"").Replace(">", @"\>").Replace("<", @"\<").Replace(",", @"\,").Replace(";", @"\;").Replace("/", @"\/");
}
public static string EscapeDNBackslashedChar(string odn)
{
return odn.Replace("/", @"\/");
}
public static string GenerateADsPath(Guid guid)
{
return ("LDAP://<GUID=" + ConvertGuidToNativeGuid(guid) + ">");
}
public static string GenerateDN(string rdn, string parentDN)
{
if (!string.IsNullOrEmpty(parentDN))
{
return string.Format("{0},{1}", rdn, parentDN);
}
return rdn;
}
public static string GenerateRDNCN(string cn)
{
return string.Format("CN={0}", EscapeDN(cn));
}
public static string GenerateRDNOU(string ou)
{
return string.Format("OU={0}", EscapeDN(ou));
}
public static string GetParentDN(string dn)
{
int i = 0;
while (i < dn.Length)
{
if ((dn[i] == ',') && (dn[i - 1] != '\\'))
{
break;
}
i++;
}
if (i == dn.Length)
{
return null;
}
return dn.Substring(i + 1);
}
internal static string GetProperty(DirectoryEntry de, string propertyName)
{
if (de.Properties.Contains(propertyName))
{
return de.Properties[propertyName][0].ToString();
}
return null;
}
internal static string GetProperty(SearchResult result, string propertyName)
{
if (result.Properties.Contains(propertyName))
{
return result.Properties[propertyName][0].ToString();
}
return null;
}
public static string GetRDN(string dn)
{
return SplitDN(dn)[0];
}
public static string GetRDNValue(string dn)
{
string part = SplitDN(dn)[0];
return part.Substring(part.IndexOf('=') + 1).Trim();
}
internal static string RemoveLDAPPre(string adsPath)
{
return adsPath.Substring(7);
}
internal static void SetProperty(DirectoryEntry de, string propertyName, int propertyValue)
{
if (de.Properties.Contains(propertyName))
{
de.Properties[propertyName][0] = propertyValue;
}
else
{
de.Properties[propertyName].Add(propertyValue);
}
}
internal static void SetProperty(DirectoryEntry de, string propertyName, string propertyValue)
{
if (de.Properties.Contains(propertyName))
{
if (string.IsNullOrEmpty(propertyValue))
{
de.Properties[propertyName].Value = null;
}
else
{
de.Properties[propertyName][0] = propertyValue;
}
}
else if (!string.IsNullOrEmpty(propertyValue))
{
de.Properties[propertyName].Add(propertyValue);
}
}
public static string[] SplitDN(string dn)
{
List<string> parts = new List<string>();
int l = 0;
int i = 0;
while (i < dn.Length)
{
if ((dn[i] == ',') && (dn[i - 1] != '\\'))
{
parts.Add(dn.Substring(l, i - l));
l = i + 1;
}
i++;
}
if (l < (i - 1))
{
string last = dn.Substring(l, i - l);
if (!string.IsNullOrEmpty(last.Trim()))
{
parts.Add(last.Trim());
}
}
return parts.ToArray();
}
internal static string UnEscapeDN(string dn)
{
return dn.Replace(@"\#", "#").Replace(@"\+", "+").Replace(@"\=", "=").Replace("\\\"", "\"").Replace(@"\>", ">").Replace(@"\<", "<").Replace(@"\,", ",").Replace(@"\;", ";").Replace(@"\/", "/").Replace(@"\\", @"\");
}
public static string UnEscapeDNBackslashedChar(string dn)
{
return dn.Replace(@"\/", "/");
}
}
}
{
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Globalization;
using System.Text;
public class Utils
{
public static char[] InvalidSAMAccountNameChars = new char[] { '/', '\\', '[', ']', ':', ';', '|', '=', ',', '+', '*', '?', '<', '>', '"' };
internal static bool CanAsContainer(DirectoryEntry de)
{
string schemaClassName = de.SchemaClassName;
if (!(schemaClassName == SchemaClass.organizationalUnit.ToString("F")) && !(schemaClassName == SchemaClass.builtinDomain.ToString("F")))
{
return (schemaClassName == SchemaClass.container.ToString("F"));
}
return true;
}
public static string ConvertDNToDomainName(string dn)
{
StringBuilder sb = new StringBuilder();
foreach (string dc in dn.Split(new char[] { ',' }))
{
sb.AppendFormat("{0}.", dc.Trim().Split(new char[] { '=' })[1]);
}
return sb.ToString(0, sb.Length - 1);
}
public static string ConvertDomainNameToDN(string domainName)
{
StringBuilder sb = new StringBuilder();
foreach (string dc in domainName.Split(new char[] { '.' }))
{
sb.AppendFormat("DC={0},", dc.ToUpper());
}
return sb.ToString(0, sb.Length - 1);
}
public static string ConvertGuidToNativeGuid(Guid guid)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in guid.ToByteArray())
{
sb.AppendFormat("{0:x2}", b);
}
return sb.ToString();
}
public static Guid ConvertNativeGuidToGuid(string nativeGuid)
{
new StringBuilder();
byte[] byteArr = new byte[0x10];
for (int i = 0; i < 0x10; i++)
{
char CS$0$0000 = nativeGuid[2 * i];
char CS$0$0001 = nativeGuid[(2 * i) + 1];
byteArr[i] = byte.Parse(CS$0$0000.ToString().ToUpper() + CS$0$0001.ToString().ToUpper(), NumberStyles.HexNumber);
}
return new Guid(byteArr);
}
public static string ConvertToOctetString(byte[] ids)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in ids)
{
sb.AppendFormat(@"\{0:x2}", b);
}
return sb.ToString();
}
public static string Escape4Query(string cn)
{
return cn.Replace("(", @"\28").Replace(")", @"\29").Replace(@"\", @"\5c");
}
internal static string EscapeDN(string odn)
{
return odn.Replace(@"\", @"\\").Replace("#", @"\#").Replace("+", @"\+").Replace("=", @"\=").Replace("\"", "\\\"").Replace(">", @"\>").Replace("<", @"\<").Replace(",", @"\,").Replace(";", @"\;").Replace("/", @"\/");
}
public static string EscapeDNBackslashedChar(string odn)
{
return odn.Replace("/", @"\/");
}
public static string GenerateADsPath(Guid guid)
{
return ("LDAP://<GUID=" + ConvertGuidToNativeGuid(guid) + ">");
}
public static string GenerateDN(string rdn, string parentDN)
{
if (!string.IsNullOrEmpty(parentDN))
{
return string.Format("{0},{1}", rdn, parentDN);
}
return rdn;
}
public static string GenerateRDNCN(string cn)
{
return string.Format("CN={0}", EscapeDN(cn));
}
public static string GenerateRDNOU(string ou)
{
return string.Format("OU={0}", EscapeDN(ou));
}
public static string GetParentDN(string dn)
{
int i = 0;
while (i < dn.Length)
{
if ((dn[i] == ',') && (dn[i - 1] != '\\'))
{
break;
}
i++;
}
if (i == dn.Length)
{
return null;
}
return dn.Substring(i + 1);
}
internal static string GetProperty(DirectoryEntry de, string propertyName)
{
if (de.Properties.Contains(propertyName))
{
return de.Properties[propertyName][0].ToString();
}
return null;
}
internal static string GetProperty(SearchResult result, string propertyName)
{
if (result.Properties.Contains(propertyName))
{
return result.Properties[propertyName][0].ToString();
}
return null;
}
public static string GetRDN(string dn)
{
return SplitDN(dn)[0];
}
public static string GetRDNValue(string dn)
{
string part = SplitDN(dn)[0];
return part.Substring(part.IndexOf('=') + 1).Trim();
}
internal static string RemoveLDAPPre(string adsPath)
{
return adsPath.Substring(7);
}
internal static void SetProperty(DirectoryEntry de, string propertyName, int propertyValue)
{
if (de.Properties.Contains(propertyName))
{
de.Properties[propertyName][0] = propertyValue;
}
else
{
de.Properties[propertyName].Add(propertyValue);
}
}
internal static void SetProperty(DirectoryEntry de, string propertyName, string propertyValue)
{
if (de.Properties.Contains(propertyName))
{
if (string.IsNullOrEmpty(propertyValue))
{
de.Properties[propertyName].Value = null;
}
else
{
de.Properties[propertyName][0] = propertyValue;
}
}
else if (!string.IsNullOrEmpty(propertyValue))
{
de.Properties[propertyName].Add(propertyValue);
}
}
public static string[] SplitDN(string dn)
{
List<string> parts = new List<string>();
int l = 0;
int i = 0;
while (i < dn.Length)
{
if ((dn[i] == ',') && (dn[i - 1] != '\\'))
{
parts.Add(dn.Substring(l, i - l));
l = i + 1;
}
i++;
}
if (l < (i - 1))
{
string last = dn.Substring(l, i - l);
if (!string.IsNullOrEmpty(last.Trim()))
{
parts.Add(last.Trim());
}
}
return parts.ToArray();
}
internal static string UnEscapeDN(string dn)
{
return dn.Replace(@"\#", "#").Replace(@"\+", "+").Replace(@"\=", "=").Replace("\\\"", "\"").Replace(@"\>", ">").Replace(@"\<", "<").Replace(@"\,", ",").Replace(@"\;", ";").Replace(@"\/", "/").Replace(@"\\", @"\");
}
public static string UnEscapeDNBackslashedChar(string dn)
{
return dn.Replace(@"\/", "/");
}
}
}