DocSpace-buildtools/common/ASC.ActiveDirectory/Novell/Extensions/NovellLdapEntryExtension.cs

148 lines
4.5 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.
*
*/
using System.Globalization;
using System.Text;
using ASC.ActiveDirectory.Base;
using ASC.ActiveDirectory.Base.Data;
using ASC.ActiveDirectory.Novell.Data;
2022-03-13 17:00:32 +00:00
using ASC.Common;
2022-03-08 05:37:20 +00:00
using ASC.Common.Logging;
using Microsoft.Extensions.Options;
using Novell.Directory.Ldap;
namespace ASC.ActiveDirectory.Novell.Extensions
{
2022-03-13 17:00:32 +00:00
[Singletone]
2022-03-08 05:37:20 +00:00
public class NovellLdapEntryExtension
{
private readonly IOptionsMonitor<ILog> _options;
public NovellLdapEntryExtension(IOptionsMonitor<ILog> option)
{
_options = option;
}
public object GetAttributeValue(LdapEntry ldapEntry, string attributeName, bool getBytes = false)
{
2022-03-14 16:29:35 +00:00
var attribute = ldapEntry.GetAttribute(attributeName);
2022-03-08 05:37:20 +00:00
if (attribute == null)
return null;
if (!(string.Equals(attributeName, LdapConstants.ADSchemaAttributes.OBJECT_SID,
StringComparison.OrdinalIgnoreCase) || getBytes))
{
return attribute.StringValue;
}
if (attribute.ByteValue == null)
return null;
var value = new byte[attribute.ByteValue.Length];
Buffer.BlockCopy(attribute.ByteValue, 0, value, 0, attribute.ByteValue.Length);
if (getBytes)
{
return value;
}
return DecodeSid(value);
}
public string[] GetAttributeArrayValue(LdapEntry ldapEntry, string attributeName)
{
2022-03-14 16:29:35 +00:00
var attribute = ldapEntry.GetAttribute(attributeName);
2022-03-08 05:37:20 +00:00
return attribute == null ? null : attribute.StringValueArray;
}
private string DecodeSid(byte[] sid)
{
var strSid = new StringBuilder("S-");
// get version
int revision = sid[0];
strSid.Append(revision.ToString(CultureInfo.InvariantCulture));
//next byte is the count of sub-authorities
var countSubAuths = sid[1] & 0xFF;
//get the authority
long authority = 0;
//String rid = "";
for (var i = 2; i <= 7; i++)
{
authority |= ((long)sid[i]) << (8 * (5 - (i - 2)));
}
strSid.Append("-");
strSid.Append(authority);
//iterate all the sub-auths
var offset = 8;
const int size = 4; //4 bytes for each sub auth
for (var j = 0; j < countSubAuths; j++)
{
long subAuthority = 0;
for (var k = 0; k < size; k++)
{
subAuthority |= (long)(sid[offset + k] & 0xFF) << (8 * k);
}
strSid.Append("-");
strSid.Append(subAuthority);
offset += size;
}
return strSid.ToString();
}
/// <summary>
/// Create LDAPObject by LdapEntry
/// </summary>
/// <param name="ldapEntry">init ldapEntry</param>
/// <param name="ldapUniqueIdAttribute"></param>
/// <returns>LDAPObject</returns>
public LdapObject ToLdapObject(LdapEntry ldapEntry, string ldapUniqueIdAttribute = null)
{
if (ldapEntry == null)
throw new ArgumentNullException("ldapEntry");
2022-03-13 17:00:32 +00:00
var novellLdapObject = new NovellLdapObject(_options, this);
novellLdapObject.Init(ldapEntry, ldapUniqueIdAttribute);
return novellLdapObject;
2022-03-08 05:37:20 +00:00
}
/// <summary>
/// Create lis of LDAPObject by LdapEntry list
/// </summary>
/// <param name="entries">list of LdapEntry</param>
/// <param name="ldapUniqueIdAttribute"></param>
/// <returns>list of LDAPObjects</returns>
public List<LdapObject> ToLdapObjects(IEnumerable<LdapEntry> entries, string ldapUniqueIdAttribute = null)
{
return entries.Select(e => ToLdapObject(e, ldapUniqueIdAttribute)).ToList();
}
}
}