public Entry ReadEntry() { while (_ldif.Read()) { if (_complete) { _complete = false; return(_work); } } // Bugfix - github issue 4 // Without this extra check, we require an extra newline (or other non-entry content) // at the end of the file. if (_complete) { _complete = false; return(_work); } return(null); }
public void LdifReaderConstructorTest() { LdifReader target = new LdifReader(@"c:\temp\testinput.ldif"); target.OnAttributeValue += delegate(object sender, AttributeEventArgs args) { Console.WriteLine("Attr name {0}, value {1}", args.Name, args.Value); }; target.OnBeginEntry += delegate(object sender, DnEventArgs args) { Console.WriteLine("Begin: {0}", args.DistinguishedName); }; target.OnEndEntry += delegate(object sender, DnEventArgs args) { Console.WriteLine("End: {0}", args.DistinguishedName); }; while (target.Read()) { // Do stuff! } Assert.Inconclusive("TODO: Implement code to verify target"); }
public void ReformatLdif(LdifReader r, LdifWriter w, Encoding passwordEncoding, string defaultPwd, List <string> ignoredAttrs, bool allowMsExchange, Func <string, bool> ignoreIt) { if (!allowMsExchange) { ignoredAttrs.Add("showinaddressbook"); ignoredAttrs.Add("legacyexchangedn"); ignoredAttrs.Add("homemta"); ignoredAttrs.Add("homemdb"); ignoredAttrs.Add("mailnickname"); ignoredAttrs.Add("mdbusedefaults"); ignoredAttrs.Add("publicdelegatesbl"); ignoredAttrs.Add("protocolsettings"); ignoredAttrs.Add("publicdelegates"); ignoredAttrs.Add("deleteditemflags"); ignoredAttrs.Add("mDBStorageQuota".ToLowerInvariant()); ignoredAttrs.Add("mDBOverQuotaLimit".ToLowerInvariant()); ignoredAttrs.Add("garbageCollPeriod".ToLowerInvariant()); ignoredAttrs.Add("mDBOverHardQuotaLimit".ToLowerInvariant()); ignoredAttrs.Add("altrecipient"); ignoredAttrs.Add("deliverandredirect"); ignoredAttrs.Add("securityprotocol"); ignoredAttrs.Add("reporttooriginator"); ignoredAttrs.Add("reporttoowner"); ignoredAttrs.Add("oOFReplyToOriginator".ToLowerInvariant()); ignoredAttrs.Add("mapirecipient"); ignoredAttrs.Add("internetencoding"); ignoredAttrs.Add("targetaddress"); ignoredAttrs.Add("altrecipientbl"); ignoredAttrs.Add("delivcontlength"); ignoredAttrs.Add("submissioncontlength"); } bool ignored = false, hasPw = false; int pwdScore = 0; string thisDn = null; r.OnBeginEntry += (s, a) => { thisDn = a.DistinguishedName; hasPw = false; if (ignoreIt != null && ignoreIt.Invoke(a.DistinguishedName)) { ignored = true; } // Ignore domain trusts / special accounts // This part could use some rethinking. else if (a.DistinguishedName.Contains("$,CN=Users,") || a.DistinguishedName.Contains("krbtgt") || a.DistinguishedName.Contains("ForeignSecurityP")) { ignored = true; } else if (!allowMsExchange && a.DistinguishedName.Contains("Exchange System")) { ignored = true; } else { ignored = false; w.BeginEntry(a.DistinguishedName); } }; r.OnEndEntry += (s, a) => { if (!ignored) { if (pwdScore > 1 && !hasPw) { w.WriteAttr("unicodePwd", passwordEncoding.GetBytes( string.Format("\"{0}\"", defaultPwd)) ); } w.EndEntry(); } pwdScore = 0; }; r.OnAttributeValue += (s, a) => { if (!ignored) { if (a.Name == "unicodePwd") { hasPw = true; } else if (a.Name == "objectCategory" && ((string)a.Value).StartsWith("CN=Person")) { pwdScore++; } else if (a.Name == "objectClass" && "user".Equals(a.Value)) { pwdScore++; } if (string.Equals(a.Name, "objectSID", StringComparison.InvariantCultureIgnoreCase)) { if (thisDn.IndexOf("ForeignSecurity", StringComparison.InvariantCultureIgnoreCase) > 0) { if (a.Value is byte[]) { System.Security.Principal.SecurityIdentifier sid = new System.Security.Principal.SecurityIdentifier((byte[])a.Value, 0); w.WriteAttr(a.Name, sid.ToString()); } else if (a.Value is string) { w.WriteAttr(a.Name, (string)a.Value); } } } else { if (a.Value != null && !ignoredAttrs.Contains(a.Name.ToLowerInvariant())) { if (allowMsExchange || (!a.Name.StartsWith("msExch", StringComparison.InvariantCultureIgnoreCase) && !a.Name.StartsWith("extensionAttribute"))) { if (a.Value is string) { w.WriteAttr(a.Name, (string)a.Value); } else if (a.Value is byte[]) { w.WriteAttr(a.Name, (byte[])a.Value); } else { Console.Error.WriteLine("Warn: type of {0} is {1}", a.Name, a.Value.GetType()); w.WriteAttr(a.Name, Convert.ToString(a.Value)); } } } } } }; while (r.Read()) { // Keep reading } w.Close(); }
public void ReformatLdif(LdifReader r, LdifWriter w, Encoding passwordEncoding, string defaultPwd, List<string> ignoredAttrs, bool allowMsExchange, Func<string, bool> ignoreIt) { if (!allowMsExchange) { ignoredAttrs.Add("showinaddressbook"); ignoredAttrs.Add("legacyexchangedn"); ignoredAttrs.Add("homemta"); ignoredAttrs.Add("homemdb"); ignoredAttrs.Add("mailnickname"); ignoredAttrs.Add("mdbusedefaults"); ignoredAttrs.Add("publicdelegatesbl"); ignoredAttrs.Add("protocolsettings"); ignoredAttrs.Add("publicdelegates"); ignoredAttrs.Add("deleteditemflags"); ignoredAttrs.Add("mDBStorageQuota".ToLowerInvariant()); ignoredAttrs.Add("mDBOverQuotaLimit".ToLowerInvariant()); ignoredAttrs.Add("garbageCollPeriod".ToLowerInvariant()); ignoredAttrs.Add("mDBOverHardQuotaLimit".ToLowerInvariant()); ignoredAttrs.Add("altrecipient"); ignoredAttrs.Add("deliverandredirect"); ignoredAttrs.Add("securityprotocol"); ignoredAttrs.Add("reporttooriginator"); ignoredAttrs.Add("reporttoowner"); ignoredAttrs.Add("oOFReplyToOriginator".ToLowerInvariant()); ignoredAttrs.Add("mapirecipient"); ignoredAttrs.Add("internetencoding"); ignoredAttrs.Add("targetaddress"); ignoredAttrs.Add("altrecipientbl"); ignoredAttrs.Add("delivcontlength"); ignoredAttrs.Add("submissioncontlength"); } bool ignored = false, hasPw = false; int pwdScore = 0; string thisDn = null; r.OnBeginEntry += (s, a) => { thisDn = a.DistinguishedName; hasPw = false; if (ignoreIt != null && ignoreIt.Invoke(a.DistinguishedName)) { ignored = true; } // Ignore domain trusts / special accounts // This part could use some rethinking. else if (a.DistinguishedName.Contains("$,CN=Users,") || a.DistinguishedName.Contains("krbtgt") || a.DistinguishedName.Contains("ForeignSecurityP")) { ignored = true; } else if (!allowMsExchange && a.DistinguishedName.Contains("Exchange System")) { ignored = true; } else { ignored = false; w.BeginEntry(a.DistinguishedName); } }; r.OnEndEntry += (s, a) => { if (!ignored) { if (pwdScore > 1 && !hasPw) w.WriteAttr("unicodePwd", passwordEncoding.GetBytes( string.Format("\"{0}\"", defaultPwd)) ); w.EndEntry(); } pwdScore = 0; }; r.OnAttributeValue += (s, a) => { if (!ignored) { if (a.Name == "unicodePwd") hasPw = true; else if (a.Name == "objectCategory" && ((string)a.Value).StartsWith("CN=Person")) pwdScore++; else if (a.Name == "objectClass" && "user".Equals(a.Value)) pwdScore++; if (string.Equals(a.Name, "objectSID", StringComparison.InvariantCultureIgnoreCase)) { if (thisDn.IndexOf("ForeignSecurity", StringComparison.InvariantCultureIgnoreCase) > 0) { if (a.Value is byte[]) { System.Security.Principal.SecurityIdentifier sid = new System.Security.Principal.SecurityIdentifier((byte[])a.Value, 0); w.WriteAttr(a.Name, sid.ToString()); } else if (a.Value is string) { w.WriteAttr(a.Name, (string)a.Value); } } } else { if (a.Value != null && !ignoredAttrs.Contains(a.Name.ToLowerInvariant())) { if (allowMsExchange || (!a.Name.StartsWith("msExch", StringComparison.InvariantCultureIgnoreCase) && !a.Name.StartsWith("extensionAttribute"))) { if (a.Value is string) { w.WriteAttr(a.Name, (string)a.Value); } else if (a.Value is byte[]) { w.WriteAttr(a.Name, (byte[])a.Value); } else { Console.Error.WriteLine("Warn: type of {0} is {1}", a.Name, a.Value.GetType()); w.WriteAttr(a.Name, Convert.ToString(a.Value)); } } } } } }; while (r.Read()) { // Keep reading } w.Close(); }