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

140 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.Novell.Extensions;
[Singletone]
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-04-26 14:03:41 +00:00
try
{
var attribute = ldapEntry.GetAttribute(attributeName);
2022-03-17 19:44:34 +00:00
2022-04-26 14:03:41 +00:00
if (attribute == null)
return null;
2022-03-08 05:37:20 +00:00
2022-04-26 14:03:41 +00:00
if (!(string.Equals(attributeName, LdapConstants.ADSchemaAttributes.OBJECT_SID,
StringComparison.OrdinalIgnoreCase) || getBytes))
{
return attribute.StringValue;
}
2022-03-08 05:37:20 +00:00
2022-04-26 14:03:41 +00:00
if (attribute.ByteValue == null)
return null;
var value = new byte[attribute.ByteValue.Length];
2022-03-08 05:37:20 +00:00
2022-04-26 14:03:41 +00:00
Buffer.BlockCopy(attribute.ByteValue, 0, value, 0, attribute.ByteValue.Length);
2022-03-08 05:37:20 +00:00
2022-04-26 14:03:41 +00:00
if (getBytes)
{
return value;
}
2022-03-17 19:44:34 +00:00
2022-04-26 14:03:41 +00:00
return DecodeSid(value);
}
catch (Exception)
2022-03-08 05:37:20 +00:00
{
2022-04-26 14:03:41 +00:00
return null;
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 string[] GetAttributeArrayValue(LdapEntry ldapEntry, string attributeName)
{
var attribute = ldapEntry.GetAttribute(attributeName);
return attribute == null ? null : attribute.StringValueArray;
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
private string DecodeSid(byte[] sid)
{
var strSid = new StringBuilder("S-");
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
// get version
int revision = sid[0];
strSid.Append(revision.ToString(CultureInfo.InvariantCulture));
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
//next byte is the count of sub-authorities
var countSubAuths = sid[1] & 0xFF;
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
//get the authority
long authority = 0;
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
//String rid = "";
for (var i = 2; i <= 7; i++)
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
authority |= ((long)sid[i]) << (8 * (5 - (i - 2)));
2022-03-08 05:37:20 +00:00
}
2022-03-17 19:44:34 +00:00
strSid.Append("-");
strSid.Append(authority);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
//iterate all the sub-auths
var offset = 8;
const int size = 4; //4 bytes for each sub auth
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
for (var j = 0; j < countSubAuths; j++)
{
long subAuthority = 0;
for (var k = 0; k < size; k++)
2022-03-08 05:37:20 +00:00
{
2022-03-17 19:44:34 +00:00
subAuthority |= (long)(sid[offset + k] & 0xFF) << (8 * k);
2022-03-08 05:37:20 +00:00
}
strSid.Append("-");
2022-03-17 19:44:34 +00:00
strSid.Append(subAuthority);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
offset += size;
2022-03-08 05:37:20 +00:00
}
2022-03-17 19:44:34 +00:00
return strSid.ToString();
}
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
/// <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
2022-03-17 19:44:34 +00:00
var novellLdapObject = new NovellLdapObject(_options, this);
novellLdapObject.Init(ldapEntry, ldapUniqueIdAttribute);
2022-03-08 05:37:20 +00:00
2022-03-17 19:44:34 +00:00
return novellLdapObject;
}
/// <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();
2022-03-08 05:37:20 +00:00
}
}