DocSpace-client/common/ASC.ActiveDirectory/LdapUtils.cs

221 lines
6.7 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
using Action = System.Action;
using Constants = ASC.Core.Users.Constants;
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
namespace ASC.ActiveDirectory;
2022-05-05 13:23:05 +00:00
2022-03-17 19:44:34 +00:00
public static class LdapUtils
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
private static readonly Regex _dcRegex = new Regex("dc=([^,]+)", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
public static string DistinguishedNameToDomain(string distinguishedName)
{
if (string.IsNullOrEmpty(distinguishedName))
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
return null;
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 matchList = _dcRegex.Matches(distinguishedName);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
var dcList = matchList.Cast<Match>().Select(match => match.Groups[1].Value).ToList();
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
return !dcList.Any() ? null : string.Join(".", dcList);
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
public static bool IsLoginAccepted(LdapLogin ldapLogin, UserInfo ldapUser, string ldapDomain)
{
if (ldapLogin == null
|| string.IsNullOrEmpty(ldapLogin.ToString())
|| string.IsNullOrEmpty(ldapDomain)
|| ldapUser == null
|| ldapUser.Equals(Constants.LostUser)
|| string.IsNullOrEmpty(ldapUser.Email)
|| string.IsNullOrEmpty(ldapUser.UserName))
2022-03-08 05:37:20 +00:00
{
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
var hasDomain = !string.IsNullOrEmpty(ldapLogin.Domain);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
if (!hasDomain)
{
return ldapLogin.Username.Equals(ldapUser.UserName, StringComparison.InvariantCultureIgnoreCase);
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
var fullLogin = ldapLogin.ToString();
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
if (fullLogin.Equals(ldapUser.Email, StringComparison.InvariantCultureIgnoreCase))
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-03-17 19:44:34 +00:00
if (!ldapDomain.StartsWith(ldapLogin.Domain))
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 alterEmail = ldapUser.UserName.Contains("@")
? ldapUser.UserName
: string.Format("{0}@{1}", ldapUser.UserName, ldapDomain);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
return IsLoginAndEmailSuitable(fullLogin, alterEmail);
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
private static string GetLdapAccessableEmail(string email)
{
try
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
if (string.IsNullOrEmpty(email))
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
return null;
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 login = LdapLogin.ParseLogin(email);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
if (string.IsNullOrEmpty(login.Domain))
2022-06-15 12:39:37 +00:00
{
2022-03-17 19:44:34 +00:00
return email;
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 dotIndex = login.Domain.LastIndexOf(".", StringComparison.Ordinal);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
var accessableEmail = dotIndex > -1 ? string.Format("{0}@{1}", login.Username, login.Domain.Remove(dotIndex)) : email;
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
return accessableEmail;
2022-03-08 05:37:20 +00:00
}
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
return null;
}
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
private static bool IsLoginAndEmailSuitable(string login, string email)
{
try
{
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(email))
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 accessableLogin = GetLdapAccessableEmail(login);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
if (string.IsNullOrEmpty(accessableLogin))
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 accessableEmail = GetLdapAccessableEmail(email);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
if (string.IsNullOrEmpty(accessableEmail))
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
return accessableLogin.Equals(accessableEmail, StringComparison.InvariantCultureIgnoreCase);
}
catch (Exception)
2022-03-08 05:37:20 +00:00
{
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 static string GeneratePassword()
{
return Guid.NewGuid().ToString();
}
2022-03-08 05:37:20 +00:00
2022-06-08 09:42:49 +00:00
public static void SkipErrors(Action method, ILogger log = null)
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
method();
2022-03-08 05:37:20 +00:00
}
2022-03-17 19:44:34 +00:00
catch (Exception ex)
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
if (log != null)
2022-06-15 12:39:37 +00:00
{
2022-06-08 09:42:49 +00:00
log.ErrorSkipErrors(ex);
2022-06-15 12:39:37 +00:00
}
2022-03-08 05:37:20 +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
public static string GetContactsString(this UserInfo userInfo)
{
2022-06-15 12:39:37 +00:00
if (userInfo.ContactsList == null || userInfo.ContactsList.Count == 0)
{
return null;
}
2022-03-17 19:44:34 +00:00
var sBuilder = new StringBuilder();
foreach (var contact in userInfo.Contacts)
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
sBuilder.AppendFormat("{0}|", contact);
2022-03-08 05:37:20 +00:00
}
2022-03-17 19:44:34 +00:00
return sBuilder.ToString();
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
public static string GetUserInfoString(this UserInfo userInfo)
{
return string.Format(
"{{ ID: '{0}' SID: '{1}' Email '{2}' UserName: '{3}' FirstName: '{4}' LastName: '{5}' Title: '{6}' Location: '{7}' Contacts: '{8}' Status: '{9}' }}",
2022-05-05 13:23:05 +00:00
userInfo.Id,
2022-03-17 19:44:34 +00:00
userInfo.Sid,
userInfo.Email,
userInfo.UserName,
userInfo.FirstName,
userInfo.LastName,
userInfo.Title,
userInfo.Location,
userInfo.GetContactsString(),
Enum.GetName(typeof(EmployeeStatus), userInfo.Status));
}
public static string UnescapeLdapString(string ldapString)
{
var sb = new StringBuilder();
for (var i = 0; i < ldapString.Length; i++)
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
var ch = ldapString[i];
if (ch == '\\')
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
if (i + 1 < ldapString.Length && ldapString[i + 1] == ch)
2022-03-08 05:37:20 +00:00
{
sb.Append(ch);
2022-03-17 19:44:34 +00:00
i++;
2022-03-08 05:37:20 +00:00
}
}
2022-03-17 19:44:34 +00:00
else
{
sb.Append(ch);
}
2022-03-08 05:37:20 +00:00
}
2022-03-17 19:44:34 +00:00
return sb.ToString();
2022-03-08 05:37:20 +00:00
}
}