DocSpace-buildtools/common/ASC.ActiveDirectory/Base/LdapHelper.cs

147 lines
5.0 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.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-03-17 19:44:34 +00:00
protected readonly ILog Log;
2022-03-18 09:36:35 +00:00
protected readonly InstanceCrypto InstanceCrypto;
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
protected LdapHelper(
IOptionsMonitor<ILog> option,
InstanceCrypto instanceCrypto)
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
Log = option.Get("ASC");
2022-03-18 09:36:35 +00:00
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)
return false;
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))
return false;
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
var groupAttribute = settings.GroupAttribute;
if (string.IsNullOrEmpty(groupAttribute))
return false;
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-03-08 05:37:20 +00:00
return false;
2022-03-17 19:44:34 +00:00
if (members.Any(member => memberString.Equals(member, StringComparison.InvariantCultureIgnoreCase)
|| member.Equals(domainUser.DistinguishedName, StringComparison.InvariantCultureIgnoreCase)))
2022-03-08 05:37:20 +00:00
return true;
}
}
2022-03-17 19:44:34 +00:00
catch (Exception e)
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
Log.ErrorFormat("UserExistsInGroup() failed. Error: {0}", 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)
return string.Empty;
string password;
try
{
2022-04-26 14:03:41 +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-03-18 09:36:35 +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
}