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.Base;
|
2022-05-05 13:23:05 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
[Scope]
|
|
|
|
|
public abstract class LdapHelper : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public LdapSettings Settings { get; private set; }
|
|
|
|
|
public abstract bool IsConnected { get; }
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-06-15 12:39:37 +00:00
|
|
|
|
protected readonly ILogger<LdapHelper> _logger;
|
|
|
|
|
protected readonly InstanceCrypto _instanceCrypto;
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
protected LdapHelper(
|
2022-06-08 09:42:49 +00:00
|
|
|
|
ILogger<LdapHelper> logger,
|
2022-03-17 19:44:34 +00:00
|
|
|
|
InstanceCrypto instanceCrypto)
|
2022-03-08 05:37:20 +00:00
|
|
|
|
{
|
2022-06-15 12:39:37 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
_instanceCrypto = instanceCrypto;
|
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 void Init(LdapSettings settings)
|
|
|
|
|
{
|
|
|
|
|
Settings = settings;
|
|
|
|
|
}
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public abstract void Connect();
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public abstract Dictionary<string, string[]> GetCapabilities();
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public abstract string SearchDomain();
|
2022-03-10 11:41:46 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public abstract void CheckCredentials(string login, string password, string server, int portNumber,
|
|
|
|
|
bool startTls, bool ssl, bool acceptCertificate, string acceptCertificateHash);
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public abstract bool CheckUserDn(string userDn);
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public abstract List<LdapObject> GetUsers(string filter = null, int limit = -1);
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public abstract LdapObject GetUserBySid(string sid);
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public abstract bool CheckGroupDn(string groupDn);
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public abstract List<LdapObject> GetGroups(Criteria criteria = null);
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public bool UserExistsInGroup(LdapObject domainGroup, LdapObject domainUser, LdapSettings settings) // string memberString, string groupAttribute, string primaryGroupId)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (domainGroup == null || domainUser == null)
|
2022-06-15 12:39:37 +00:00
|
|
|
|
{
|
2022-03-17 19:44:34 +00:00
|
|
|
|
return false;
|
2022-06-15 12:39:37 +00:00
|
|
|
|
}
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
var memberString = domainUser.GetValue(Settings.UserAttribute) as string;
|
|
|
|
|
if (string.IsNullOrEmpty(memberString))
|
2022-06-15 12:39:37 +00:00
|
|
|
|
{
|
2022-03-17 19:44:34 +00:00
|
|
|
|
return false;
|
2022-06-15 12:39:37 +00:00
|
|
|
|
}
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
var groupAttribute = settings.GroupAttribute;
|
|
|
|
|
if (string.IsNullOrEmpty(groupAttribute))
|
2022-06-15 12:39:37 +00:00
|
|
|
|
{
|
2022-03-17 19:44:34 +00:00
|
|
|
|
return false;
|
2022-06-15 12:39:37 +00:00
|
|
|
|
}
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
var userPrimaryGroupId = domainUser.GetValue(LdapConstants.ADSchemaAttributes.PRIMARY_GROUP_ID) as string;
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(userPrimaryGroupId) && domainGroup.Sid.EndsWith("-" + userPrimaryGroupId))
|
|
|
|
|
{
|
|
|
|
|
// Domain Users found
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var members = domainGroup.GetValues(groupAttribute);
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
if (members.Count == 0)
|
2022-06-15 12:39:37 +00:00
|
|
|
|
{
|
2022-03-08 05:37:20 +00:00
|
|
|
|
return false;
|
2022-06-15 12:39:37 +00:00
|
|
|
|
}
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
if (members.Any(member => memberString.Equals(member, StringComparison.InvariantCultureIgnoreCase)
|
|
|
|
|
|| member.Equals(domainUser.DistinguishedName, StringComparison.InvariantCultureIgnoreCase)))
|
2022-06-15 12:39:37 +00:00
|
|
|
|
{
|
2022-03-08 05:37:20 +00:00
|
|
|
|
return true;
|
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.ErrorUserExistsInGroupFailed(e);
|
2022-03-08 05:37:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetPassword(byte[] passwordBytes)
|
|
|
|
|
{
|
|
|
|
|
if (passwordBytes == null || passwordBytes.Length == 0)
|
2022-06-15 12:39:37 +00:00
|
|
|
|
{
|
2022-03-17 19:44:34 +00:00
|
|
|
|
return string.Empty;
|
2022-06-15 12:39:37 +00:00
|
|
|
|
}
|
2022-03-17 19:44:34 +00:00
|
|
|
|
|
|
|
|
|
string password;
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-06-15 12:39:37 +00:00
|
|
|
|
password = _instanceCrypto.Decrypt(passwordBytes, new UnicodeEncoding());
|
2022-03-17 19:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
2022-03-08 05:37:20 +00:00
|
|
|
|
{
|
2022-03-17 19:44:34 +00:00
|
|
|
|
password = string.Empty;
|
|
|
|
|
}
|
|
|
|
|
return password;
|
|
|
|
|
}
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
public byte[] GetPasswordBytes(string password)
|
|
|
|
|
{
|
|
|
|
|
byte[] passwordBytes;
|
2022-03-08 05:37:20 +00:00
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-06-15 12:39:37 +00:00
|
|
|
|
passwordBytes = _instanceCrypto.Encrypt(new UnicodeEncoding().GetBytes(password));
|
2022-03-17 19:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
passwordBytes = Array.Empty<byte>();
|
2022-03-08 05:37:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 19:44:34 +00:00
|
|
|
|
return passwordBytes;
|
2022-03-08 05:37:20 +00:00
|
|
|
|
}
|
2022-03-17 19:44:34 +00:00
|
|
|
|
|
|
|
|
|
public abstract void Dispose();
|
2022-03-08 05:37:20 +00:00
|
|
|
|
}
|