DocSpace-buildtools/common/ASC.ActiveDirectory/Novell/NovellLdapHelper.cs

296 lines
10 KiB
C#
Raw Normal View History

2022-05-05 13:23:05 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
namespace ASC.ActiveDirectory.Novell;
2022-05-05 13:23:05 +00:00
2022-03-17 19:44:34 +00:00
[Scope]
public class NovellLdapHelper : LdapHelper
2022-03-08 05:37:20 +00:00
{
2022-03-18 09:36:35 +00:00
private readonly NovellLdapSearcher _lDAPSearcher;
2022-03-17 19:44:34 +00:00
private readonly IConfiguration _configuration;
private readonly IServiceProvider _serviceProvider;
2022-06-15 12:39:37 +00:00
private readonly LdapObjectExtension _ldapObjectExtension;
2022-03-08 05:37:20 +00:00
2022-06-15 12:39:37 +00:00
public NovellLdapHelper(IServiceProvider serviceProvider, ILogger<LdapHelper> logger, InstanceCrypto instanceCrypto, IConfiguration configuration, NovellLdapSearcher novellLdapSearcher, LdapObjectExtension ldapObjectExtension) :
2022-06-08 09:42:49 +00:00
base(logger, instanceCrypto)
2022-03-17 19:44:34 +00:00
{
2022-03-18 09:36:35 +00:00
_lDAPSearcher = novellLdapSearcher;
2022-03-17 19:44:34 +00:00
_configuration = configuration;
_serviceProvider = serviceProvider;
2022-06-15 12:39:37 +00:00
_ldapObjectExtension = ldapObjectExtension;
2022-03-17 19:44:34 +00:00
}
2022-03-10 11:41:46 +00:00
2022-05-18 10:50:39 +00:00
public new void Init(LdapSettings settings)
2022-03-17 19:44:34 +00:00
{
var password = string.IsNullOrEmpty(settings.Password)
? GetPassword(settings.PasswordBytes)
: settings.Password;
2022-03-08 05:37:20 +00:00
2022-03-18 09:36:35 +00:00
_lDAPSearcher.Init(settings.Login, password, settings.Server, settings.PortNumber,
2022-03-17 19:44:34 +00:00
settings.StartTls, settings.Ssl, settings.AcceptCertificate, settings.AcceptCertificateHash);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
base.Init(settings);
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
public override bool IsConnected
{
2022-03-18 09:36:35 +00:00
get { return _lDAPSearcher.IsConnected; }
2022-03-17 19:44:34 +00:00
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
public override void Connect()
{
2022-03-18 09:36:35 +00:00
_lDAPSearcher.Connect();
2022-03-08 05:37:20 +00:00
2022-03-18 09:36:35 +00:00
Settings.AcceptCertificate = _lDAPSearcher.AcceptCertificate;
Settings.AcceptCertificateHash = _lDAPSearcher.AcceptCertificateHash;
2022-03-17 19:44:34 +00:00
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
public override Dictionary<string, string[]> GetCapabilities()
{
2022-03-18 09:36:35 +00:00
return _lDAPSearcher.GetCapabilities();
2022-03-17 19:44:34 +00:00
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
public override string SearchDomain()
{
try
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
var capabilities = GetCapabilities();
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
if (capabilities.Count > 0)
{
if (capabilities.ContainsKey("defaultNamingContext"))
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
var dnList = capabilities["defaultNamingContext"];
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
var dn = dnList.FirstOrDefault(dc =>
!string.IsNullOrEmpty(dc) &&
dc.IndexOf("dc=", StringComparison.InvariantCultureIgnoreCase) != -1);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
var domain = LdapUtils.DistinguishedNameToDomain(dn);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
if (!string.IsNullOrEmpty(domain))
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
return domain;
2022-06-15 12:39:37 +00:00
}
2022-03-17 19:44:34 +00:00
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
if (capabilities.ContainsKey("rootDomainNamingContext"))
{
var dnList = capabilities["rootDomainNamingContext"];
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
var dn = dnList.FirstOrDefault(dc =>
!string.IsNullOrEmpty(dc) &&
dc.IndexOf("dc=", StringComparison.InvariantCultureIgnoreCase) != -1);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
var domain = LdapUtils.DistinguishedNameToDomain(dn);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
if (!string.IsNullOrEmpty(domain))
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
return domain;
2022-06-15 12:39:37 +00:00
}
2022-03-17 19:44:34 +00:00
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
if (capabilities.ContainsKey("namingContexts"))
{
var dnList = capabilities["namingContexts"];
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
var dn = dnList.FirstOrDefault(dc =>
!string.IsNullOrEmpty(dc) &&
dc.IndexOf("dc=", StringComparison.InvariantCultureIgnoreCase) != -1);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
var domain = LdapUtils.DistinguishedNameToDomain(dn);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
if (!string.IsNullOrEmpty(domain))
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
return domain;
2022-06-15 12:39:37 +00:00
}
2022-03-08 05:37:20 +00:00
}
}
}
2022-03-17 19:44:34 +00:00
catch (Exception e)
2022-03-08 05:37:20 +00:00
{
2022-06-15 12:39:37 +00:00
_logger.WarnSearchDomainFailed(e);
2022-03-08 05:37:20 +00:00
}
2022-03-17 19:44:34 +00:00
try
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
var searchResult =
2022-03-18 09:36:35 +00:00
_lDAPSearcher.Search(Settings.UserDN, NovellLdapSearcher.LdapScope.Sub, Settings.UserFilter, limit: 1)
2022-03-17 19:44:34 +00:00
.FirstOrDefault();
2022-03-08 05:37:20 +00:00
2022-06-15 12:39:37 +00:00
return searchResult != null ? _ldapObjectExtension.GetDomainFromDn(searchResult) : null;
2022-03-17 19:44:34 +00:00
}
catch (Exception e)
{
2022-06-15 12:39:37 +00:00
_logger.WarnSearchDomainFailed(e);
2022-03-17 19:44:34 +00:00
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
return null;
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
public override void CheckCredentials(string login, string password, string server, int portNumber,
bool startTls, bool ssl, bool acceptCertificate, string acceptCertificateHash)
{
2022-03-25 07:38:09 +00:00
using var novellLdapSearcher = _serviceProvider.GetRequiredService<NovellLdapSearcher>();
2022-03-17 19:44:34 +00:00
novellLdapSearcher.Init(login, password, server, portNumber, startTls, ssl, acceptCertificate, acceptCertificateHash);
novellLdapSearcher.Connect();
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
public override bool CheckUserDn(string userDn)
{
string[] attributes = { LdapConstants.ADSchemaAttributes.OBJECT_CLASS };
2022-03-08 05:37:20 +00:00
2022-03-18 09:36:35 +00:00
var searchResult = _lDAPSearcher.Search(userDn, NovellLdapSearcher.LdapScope.Base,
2022-03-17 19:44:34 +00:00
LdapConstants.OBJECT_FILTER, attributes, 1);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
if (searchResult.Any())
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
return true;
2022-06-15 12:39:37 +00:00
}
2022-03-08 05:37:20 +00:00
2022-06-15 12:39:37 +00:00
_logger.ErrorWrongUserDnParameter(userDn);
2022-03-17 19:44:34 +00:00
return false;
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
public override bool CheckGroupDn(string groupDn)
{
string[] attributes = { LdapConstants.ADSchemaAttributes.OBJECT_CLASS };
2022-03-08 05:37:20 +00:00
2022-03-18 09:36:35 +00:00
var searchResult = _lDAPSearcher.Search(groupDn, NovellLdapSearcher.LdapScope.Base,
2022-03-17 19:44:34 +00:00
LdapConstants.OBJECT_FILTER, attributes, 1);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
if (searchResult.Any())
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
return true;
2022-06-15 12:39:37 +00:00
}
2022-03-08 05:37:20 +00:00
2022-06-15 12:39:37 +00:00
_logger.ErrorWrongGroupDnParameter(groupDn);
2022-03-17 19:44:34 +00:00
return false;
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
public override List<LdapObject> GetUsers(string filter = null, int limit = -1)
{
var list = new List<LdapObject>();
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
try
{
if (!string.IsNullOrEmpty(Settings.UserFilter) && !Settings.UserFilter.StartsWith("(") &&
!Settings.UserFilter.EndsWith(")"))
{
Settings.UserFilter = string.Format("({0})", Settings.UserFilter);
2022-03-08 05:37:20 +00:00
}
2022-03-17 19:44:34 +00:00
if (!string.IsNullOrEmpty(filter) && !filter.StartsWith("(") &&
!filter.EndsWith(")"))
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
filter = string.Format("({0})", Settings.UserFilter);
2022-03-08 05:37:20 +00:00
}
2022-03-17 19:44:34 +00:00
var searchfilter = string.IsNullOrEmpty(filter)
? Settings.UserFilter
: string.Format("(&{0}{1})", Settings.UserFilter, filter);
2022-03-18 09:36:35 +00:00
list = _lDAPSearcher.Search(Settings.UserDN, NovellLdapSearcher.LdapScope.Sub, searchfilter, limit: limit);
2022-03-17 19:44:34 +00:00
2022-03-08 05:37:20 +00:00
return list;
}
2022-03-17 19:44:34 +00:00
catch (Exception e)
2022-03-08 05:37:20 +00:00
{
2022-06-15 12:39:37 +00:00
_logger.ErrorGetUsersFailed(filter, limit, e);
2022-03-17 19:44:34 +00:00
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
return list;
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
public override LdapObject GetUserBySid(string sid)
{
try
{
var ldapUniqueIdAttribute = _configuration["ldap:unique:id"];
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
Criteria criteria;
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
if (ldapUniqueIdAttribute == null)
{
criteria = Criteria.Any(
Expression.Equal(LdapConstants.RfcLDAPAttributes.ENTRY_UUID, sid),
Expression.Equal(LdapConstants.RfcLDAPAttributes.NS_UNIQUE_ID, sid),
Expression.Equal(LdapConstants.RfcLDAPAttributes.GUID, sid),
Expression.Equal(LdapConstants.ADSchemaAttributes.OBJECT_SID, sid)
);
2022-03-08 05:37:20 +00:00
}
2022-03-17 19:44:34 +00:00
else
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
criteria = Criteria.All(Expression.Equal(ldapUniqueIdAttribute, sid));
2022-03-08 05:37:20 +00:00
}
2022-03-17 19:44:34 +00:00
var searchfilter = string.Format("(&{0}{1})", Settings.UserFilter, criteria);
2022-03-08 05:37:20 +00:00
2022-03-18 09:36:35 +00:00
var list = _lDAPSearcher.Search(Settings.UserDN, NovellLdapSearcher.LdapScope.Sub, searchfilter, limit: 1);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
return list.FirstOrDefault();
}
catch (Exception e)
{
2022-06-15 12:39:37 +00:00
_logger.ErrorGetUserBySidFailed(sid, e);
2022-03-17 19:44:34 +00:00
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
return null;
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
public override List<LdapObject> GetGroups(Criteria criteria = null)
{
var list = new List<LdapObject>();
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
try
{
if (!string.IsNullOrEmpty(Settings.GroupFilter) && !Settings.GroupFilter.StartsWith("(") &&
!Settings.GroupFilter.EndsWith(")"))
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
Settings.GroupFilter = string.Format("({0})", Settings.GroupFilter);
2022-03-08 05:37:20 +00:00
}
2022-03-17 19:44:34 +00:00
var searchfilter = criteria == null
? Settings.GroupFilter
: string.Format("(&{0}{1})", Settings.GroupFilter, criteria);
2022-03-08 05:37:20 +00:00
2022-03-18 09:36:35 +00:00
list = _lDAPSearcher.Search(Settings.GroupDN, NovellLdapSearcher.LdapScope.Sub, searchfilter);
2022-03-17 19:44:34 +00:00
}
catch (Exception e)
2022-03-08 05:37:20 +00:00
{
2022-06-15 12:39:37 +00:00
_logger.ErrorGetGroupsFailed(criteria, e);
2022-03-08 05:37:20 +00:00
}
2022-03-17 19:44:34 +00:00
return list;
}
public override void Dispose()
{
2022-03-18 09:36:35 +00:00
_lDAPSearcher.Dispose();
2022-03-08 05:37:20 +00:00
}
}