How to get Active Directory object SID using LDAP? - active-directory

I have the command that returns all groups from active directory:
conn.search(f'cn=users,DC=dc,DC=testdomain,DC=link', '(objectclass=group)',paged_size = 1000)
In the entery DN I see the group names.
The question is how I can get the SID of each group, so that I have both the Name and the SID of the object.

This now returns the objectsid
conn.search(f'cn=users,DC=dc,DC=testdomain,DC=link',
'(objectclass=group)',
attributes=['objectsid', 'sAMAccountName'],paged_size = 1000)

Related

Cannot get list of groups users in AD - memberOf function not working

This query works fine:
(&(objectCategory=user)(objectClass=user)(memberOf=*) )
but any ever, with selection of group - with no result
(&(objectCategory=user)(objectClass=user)(memberOf=myGroup) )
(&(objectCategory=user)(objectClass=user)(memberOf=CN=myGroup) )
(&(objectCategory=user)(objectClass=user)(memberOf:1.2.840.113556.1.4.1941:=CN=myGroup) )
It's because memberof attribute stores dn values, so you have to provide a dn in the filter.
And by dn I mean a full dn all the way to the root of the ldap directory tree.
If your group : CN=myGroup is in the branch ou=groups and the baseDn of your directory is dc=local,dc=com, you have to specify a filter like :
memberof=CN=myGroup,ou=groups,dc=local,dc=com

LDAP query for deleted users

The normal way to query a directory for users is (&(objectClass=user)(objectCategory=person)). The normal way to query for deleted objects is to add (isDeleted=TRUE).
However, the objectCategory attribute does not exist on tombstone objects, so a query for (&(objectClass=user)(objectCategory=person)(isDeleted=TRUE)) will get you nothing.
If you remove the (objectCategory=person) part, you'll get computers too, as they inherit from user.
Is it possible to retrieve only deleted users?
If not, is it possible to tell from the returned tombstone object if it's a user or not?
Try an LDAP filter like:
(&(isDeleted=TRUE)(userAccountControl:1.2.840.113556.1.4.803:=512))
This should retrieve most deleted user type entries.
python3 code
import ldap
from ldap.controls.simple import ValueLessRequestControl
...
base =
scope = ldap.SCOPE_SUBTREE
filterstr = '(&(objectClass=user)(isDeleted=TRUE))'
attrlist =
result_set = []
ct = ldap.controls.simple.ValueLessRequestControl('1.2.840.113556.1.4.417', True)
result_id = l.search_ext(base, scope, filterstr, attrlist, serverctrls=[ct, ])
for i in range(0, 100):
result_type, result_data = l.result(result_id, 0)
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
else:
break
...

Can't select User ID from Salesforce

I'm trying to select user's subordinates from Salesforce, but a simple query
SELECT Id FROM User WHERE ManagerId=xxxxxxxxx
returns bunch of null values, when I run
SELECT Id,Name FROM User WHERE ManagerId=xxxxxxxx
I get the correct names, still no IDs.
Is this a permission issue? I can't find anything when I login to portal.
I'm running the queries via API on Sandbox environment.
Try this (both works for me allways):
Id myId = [Select Id From User Where Username = 'myUserName'].Id;
System.debug('#### myId: ' + myId);
List<User> myIdList = [Select Id From User Where Username = 'myUserName' Limit 1];
System.debug('#### myId from list: ' + myIdList[0].Id);
Portal Licence doesn't allow to query User. However you have still access to the name of the user through OwnerId, CreatedById, LastModifiedById using in an inputfield.
i.e :
If you want to have access to user through the portal you need a custom object and synchronise your records with User by trigger.

get active directory users whose FirstName in active directory is not empty or NULL

Searcher.Filter = "(&(objectCategory=person)(objectClass=user)(!givenName=""))"
I'am using the above active directory search filter to try and get users whose FirstName in active directory is not empty or NULL but the filter still returns null entries.
What should I change in the filter to only retreive users with a first name that is not NULL or Empty
Can you try :
Searcher.Filter = "(&(objectCategory=user)(objectClass=user)(givenName=*))"
To build a filter you can use registered query in Active-Directory MMC :

Finding a user's manager record in Active Directory

Using Active Directory, am trying to find the SamAccountName and email of the user’s manager.
I find the logged on user in the AD by search where sAMAccountName = Domain\Account. I then retrieve the manager property, which looks like this, for example:
CN=Doe\, Jane E.,OU=Employees,OU=Users,OU=Detroit,OU=United States,DC=na,DC=gmc,DC=gmc,DC=com"
How can I use this presumed key to find the user record for this person? What field would I match on?
If I remember correctly, that is their Distinguished Name, which means you can use it as the direct reference to their profile
LDAP://CN=Doe, Jane E.,OU=Employees,OU=Users,OU=Detroit,OU=United States,DC=na,DC=gmc,DC=gmc,DC=com
I also think it will return that name if the profile exists. If it has been deleted then I believe it runs a GUID of some sort (based on memory - this might be incorrect)
The entry for the manager is the manager's Binding String. You can feed it back into a request to active directory by binding it to an object that will return the manager's information.
(This is a post from old time, but I thought might be useful for others in the community)
You can use string stripping and find it like this:
REPLACE(SUBSTRING(manager, 4, CHARINDEX('OU=', manager)-5), '\', '')
Full working query (just change DOMAIN to your own):
SELECT Top 901 manager, REPLACE(SUBSTRING(manager, 4, CHARINDEX('OU=',
manager)-5), '\', '')
FROM OPENQUERY( ADSI, 'SELECT manager FROM ''LDAP://DC=DOMAIN,DC=local''
WHERE objectCategory = ''Person'' AND objectClass= ''user''
AND userprincipalname = ''*'' AND mail = ''*'' AND SN = ''*'' ')

Resources