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

137 lines
4.1 KiB
C#
Raw Normal View History

2022-03-08 05:37:20 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
2022-03-17 19:44:34 +00:00
namespace ASC.ActiveDirectory.Base;
[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
}