This commit is contained in:
Vashchuk Nikita 2022-03-14 19:29:35 +03:00
parent 49a14d7e6c
commit 38bf638352
4 changed files with 25 additions and 89 deletions

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
@ -7,8 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Mono.Security" Version="5.4.0.201" />
<PackageReference Include="Novell.Directory.LDAP" Version="2.3.8.1" />
<PackageReference Include="Novell.Directory.Ldap.NETStandard" Version="3.6.0" />
</ItemGroup>
<ItemGroup>

View File

@ -20,11 +20,8 @@ using System.Text.RegularExpressions;
using ASC.ActiveDirectory.Base.Data;
using ASC.Common.Logging;
using ASC.Core;
using ASC.Core.Users;
using Monocert = Mono.Security.X509;
using Syscert = System.Security.Cryptography.X509Certificates;
namespace ASC.ActiveDirectory
{
@ -133,52 +130,6 @@ namespace ASC.ActiveDirectory
return Guid.NewGuid().ToString();
}
public static bool IsCertInstalled(Syscert.X509Certificate certificate, ILog log = null)
{
try
{
var monoX509 = new Monocert.X509Certificate(certificate.GetRawCertData());
var store = WorkContext.IsMono
? Monocert.X509StoreManager.CurrentUser.TrustedRoot
: Monocert.X509StoreManager.LocalMachine.TrustedRoot;
return store.Certificates.Contains(monoX509);
}
catch (Exception ex)
{
if (log != null)
log.ErrorFormat("IsCertInstalled() failed. Error: {0}", ex);
}
return false;
}
public static bool TryInstallCert(Syscert.X509Certificate certificate, ILog log = null)
{
try
{
var monoX509 = new Monocert.X509Certificate(certificate.GetRawCertData());
var store = WorkContext.IsMono
? Monocert.X509StoreManager.CurrentUser.TrustedRoot
: Monocert.X509StoreManager.LocalMachine.TrustedRoot;
// Add the certificate to the store.
store.Import(monoX509);
store.Certificates.Add(monoX509);
return true;
}
catch (Exception ex)
{
if (log != null)
log.ErrorFormat("TryInstallCert() failed. Error: {0}", ex);
}
return false;
}
public static void SkipErrors(Action method, ILog log = null)
{
try

View File

@ -40,7 +40,7 @@ namespace ASC.ActiveDirectory.Novell.Extensions
}
public object GetAttributeValue(LdapEntry ldapEntry, string attributeName, bool getBytes = false)
{
var attribute = ldapEntry.getAttribute(attributeName);
var attribute = ldapEntry.GetAttribute(attributeName);
if (attribute == null)
return null;
@ -68,7 +68,7 @@ namespace ASC.ActiveDirectory.Novell.Extensions
public string[] GetAttributeArrayValue(LdapEntry ldapEntry, string attributeName)
{
var attribute = ldapEntry.getAttribute(attributeName);
var attribute = ldapEntry.GetAttribute(attributeName);
return attribute == null ? null : attribute.StringValueArray;
}

View File

@ -182,22 +182,10 @@ namespace ASC.ActiveDirectory.Novell
{
var certHash = certificate.GetCertHashString();
if (LdapUtils.IsCertInstalled(certificate, _log))
{
AcceptCertificate = true;
AcceptCertificateHash = certHash;
return true;
}
if (AcceptCertificate)
{
if (AcceptCertificateHash == null || AcceptCertificateHash.Equals(certHash))
{
if (LdapUtils.TryInstallCert(certificate, _log))
{
AcceptCertificateHash = certHash;
}
return true;
}
@ -215,9 +203,9 @@ namespace ASC.ActiveDirectory.Novell
public enum LdapScope
{
Base = LdapConnection.SCOPE_BASE,
One = LdapConnection.SCOPE_ONE,
Sub = LdapConnection.SCOPE_SUB
Base = LdapConnection.ScopeBase,
One = LdapConnection.ScopeOne,
Sub = LdapConnection.ScopeSub
}
public List<LdapObject> Search(LdapScope scope, string searchFilter,
@ -282,12 +270,12 @@ namespace ASC.ActiveDirectory.Novell
var queue = _ldapConnection.Search(searchBase,
(int)scope, searchFilter, attributes, false, ldapSearchConstraints);
while (queue.hasMore())
while (queue.HasMore())
{
LdapEntry nextEntry;
try
{
nextEntry = queue.next();
nextEntry = queue.Next();
if (nextEntry == null)
continue;
@ -406,25 +394,25 @@ namespace ASC.ActiveDirectory.Novell
// initially, cookie must be set to an empty string
var pageSize = 2;
sbyte[] cookie = Array.ConvertAll(Encoding.ASCII.GetBytes(""), b => unchecked((sbyte)b));
byte[] cookie = Array.ConvertAll(Encoding.ASCII.GetBytes(""), b => unchecked(b));
var i = 0;
do
{
var requestControls = new LdapControl[1];
requestControls[0] = new LdapPagedResultsControl(pageSize, cookie);
ldapSearchConstraints.setControls(requestControls);
requestControls[0] = new SimplePagedResultsControl(pageSize, cookie);
ldapSearchConstraints.SetControls(requestControls);
_ldapConnection.Constraints = ldapSearchConstraints;
var res = _ldapConnection.Search(searchBase,
(int)scope, searchFilter, attributes, false, (LdapSearchConstraints)null);
while (res.hasMore())
while (res.HasMore())
{
LdapEntry nextEntry;
try
{
nextEntry = res.next();
nextEntry = res.Next();
if (nextEntry == null)
continue;
@ -441,7 +429,7 @@ namespace ASC.ActiveDirectory.Novell
continue;
}
_log.DebugFormat("{0}. DN: {1}", ++i, nextEntry.DN);
_log.DebugFormat("{0}. DN: {1}", ++i, nextEntry.Dn);
entries.Add(nextEntry);
@ -465,11 +453,11 @@ namespace ASC.ActiveDirectory.Novell
foreach (LdapControl control in controls)
{
/* Is this the LdapPagedResultsResponse control? */
if (!(control is LdapPagedResultsResponse))
if (!(control is SimplePagedResultsControl))
continue;
var response = new LdapPagedResultsResponse(control.ID,
control.Critical, control.getValue());
var response = new SimplePagedResultsControl(control.Id,
control.Critical, control.GetValue());
cookie = response.Cookie;
}
@ -498,15 +486,15 @@ namespace ASC.ActiveDirectory.Novell
ReferralFollowing = true
};
var ldapSearchResults = _ldapConnection.Search("", LdapConnection.SCOPE_BASE, LdapConstants.OBJECT_FILTER,
var ldapSearchResults = _ldapConnection.Search("", LdapConnection.ScopeBase, LdapConstants.OBJECT_FILTER,
new[] { "*", "supportedControls", "supportedCapabilities" }, false, ldapSearchConstraints);
while (ldapSearchResults.hasMore())
while (ldapSearchResults.HasMore())
{
LdapEntry nextEntry;
try
{
nextEntry = ldapSearchResults.next();
nextEntry = ldapSearchResults.Next();
if (nextEntry == null)
continue;
@ -517,7 +505,7 @@ namespace ASC.ActiveDirectory.Novell
continue;
}
var attributeSet = nextEntry.getAttributeSet();
var attributeSet = nextEntry.GetAttributeSet();
var ienum = attributeSet.GetEnumerator();
@ -532,10 +520,8 @@ namespace ASC.ActiveDirectory.Novell
.ToList()
.Select(s =>
{
if (Base64.isLDIFSafe(s)) return s;
var tbyte = SupportClass.ToByteArray(s);
s = Base64.encode(SupportClass.ToSByteArray(tbyte));
if (Base64.IsLdifSafe(s)) return s;
s = Base64.Encode(s);
return s;
}).ToArray();
@ -603,7 +589,7 @@ namespace ASC.ActiveDirectory.Novell
_ldapConnection.SearchConstraints.TimeLimit = 10000;
_ldapConnection.ConnectionTimeout = 10000;
if (_ldapConnection.TLS)
if (_ldapConnection.Tls)
{
_log.Debug("ldapConnection.StopTls();");
_ldapConnection.StopTls();