Find LDAP Groups and Member emails - active-directory

I am trying to search for All Groups and Members under a specific OU in my Active Directory.
The below query works fine.
ldapsearch -o ldif-wrap=no -b OU=BUSINESS_DOMAIN,OU=ONE,DC=myserver,DC=com -s one '(cn=*)' member cn
I can get the group names and member names(CN) from the AD. But is there a way to get the member emails as well?
Should I iterate over each member and trigger a separate LDAP Search programmatically or can I create a chained query?
EDIT
Based on the comments, adding more clarity- I am looking for email id of all users embedded under groups under a given OU.

This should work:
ldapsearch -o ldif-wrap=no -b OU=BUSINESS_DOMAIN,OU=ONE,DC=myserver,DC=com -s one '(cn=*)' member cn mail
Assuming you are using the LDAP standard email address attribute "mail" for the email address.

Related

LDAP query for membership in Active Directory Security Group

Given a username and a group, I need a simple LDAP query to run that can query if the username is a member of an Active Directory security group.
Here is what I have tried, but it is not running:
<LDAP://DC=subdomain,DC=domain,DC=com>;(&(objectClass=user)(sAMAccountName=myusername)(memberof=CN=Domain Admins,OU=Users,DC=subdomain,DC=domain,DC=com))
Am I missing something? Single quotes around Domain Admins? Or something else?
Generally LDAP queries for groups require the fully distinguished name of the user and the Group.
If you know the specific group then a LDAP Query like:
ldapsearch -H ldaps://server.domain.com:636 -x -D "adminguy#domain.com" -W -b "CN=myusername,CN=Users,DC=domain,DC=com" -s sub -a always -z 1000 "(&(sAMAccountName=myusername)(Memberof=CN=Domain Admins,OU=Users,DC=subdomain,DC=domain,DC=com))" "objectClass"
That returns a DN implies there the user sAMAccountName=myusername is a member of that specific Group.
If no DNS are returned then there is not sAMAccountName=myusername that is a member of that specific group.
However, this would not include any nested groups.

Create new group from specific title and group

I'm really bad at LDAP so I don't really know where to start.
Is it possible to make a new distrubution or security group with users from another group that has a specific Title? There are too many users to add them manually.
It would be helpful to know how you want to interact with AD -- writing a program (language?) or using an LDAP client (which one?). I'll provide info on using ldifde.exe from the remote server administration tools. To get a list of DNs that are in another group and have a specific title, use a filter with the title and membership requirement
ldifde -f output.txt -r "(&(title=Desired Title)(memberOf=cn=GroupName,ou=Groups,dc=example,dc=com))" -l "NULL"
This returns user records that are direct members of the specified group (you'll need the fully qualified LDAP DN of the group, wildcards are not supported for memberOf -- the example is a group named "GroupName" in an OU called "Groups" at the root of the example.com AD).
Then you need to create a new group with the identified user DNs as members. The output.txt file will have a lot of lines that say "changetype: add\n\n" ... get rid of those so you've just got DNs. Change the "dn: " to "member: ". Prepend the following information to the list of group members to create an LDIF file to create a new global security group:
dn: CN=GroupName,OU=Groups,dc=example,dc=com
changetype: add
objectClass: group
cn: GroupName
distinguishedName: CN=GroupName,OU=Groups,dc=example,dc=com
instanceType: 4
name: GroupName
sAMAccountName: GroupName
groupType: -2147483646
objectCategory: CN=Group,CN=Schema,CN=Configuration,dc=example,dc=com
member: CN=Person1,OU=ResourceUsers,dc=example,dc=com
member: CN=Person2,OU=ResourceUsers,dc=example,dc=com
member: CN=Person3,OU=ResourceUsers,dc=example,dc=com
member: CN=Person4,OU=ResourceUsers,dc=example,dc=com
member: CN=Person5,OU=ResourceUsers,dc=example,dc=com
Import the LDIF file using:
ldifde -i -v -k -y -f output.txt
The group will be created & populated with the accounts listed in the "member" attribute.
This doesn't do anything if you add a new person to the first group with the desired title -- this is a one-time snapshot. You can easily identify people who should be added/removed from the second group
Need to be added -- anyone with desired title who is a member of SourceGroupName but not a member of CreatedGroupName:
(&(title=Desired Title)(memberOf=cn=SourceGroupName,ou=Groups,dc=example,dc=com)(!(memberOf=cn=CreatedGroupName,ou=Groups,dc=example,dc=com)))
Need to be removed -- anyone who is a member of CreatedGroupName who is not a member of SourceGroupName or does not have the desired title.
(&(memberOf=cn=CreatedGroupName,ou=Groups,dc=example,dc=com)(|(!(title=Desired Title))(!(memberOf=cn=SourceGroupName,ou=Groups,dc=example,dc=com))))
Add/remove members from the new group as individuals match one of these two filters.

Using ldapmodify to remove all users from an AD group

What is the best way to remove all members from a group in AD?
The help for ldapmodify doesn't seem to support the ability to remove all members of a group.
I found an example of removing a particular dn using:
ldapmodify -h 127.0.0.1 -D "cn=admin" -w xxxx -f modStaticGrp.ldif
Where modStaticGrp.ldif contains:
dn: cn=group1, o=Your Company
changetype: modify
delete: member
member: cn=jeff, cn=tim, o=Your Company
I would like to have a bash script with ldapmodify running a command that removes all the users in a group. What would be the best way to accomplish this?
ldapmodify doesn't seem to support the ability to remove all members of a group.
Yes it does. See man ldapmodify and man ldif. Just truncate your LDIF after the delete: member line. If no attribute values to delete are supplied, the entire attribute is deleted.

Query Active Directory users who are managed by given manager's sAMAccountName

I'm trying to search active directory users whose manager's username is given in the search request, but I always get 0 records regardless of the manager's username I pass.
To achieve this, I executed the following LDAP query:
(manager=sAMAccountName=Administrator)
I also tried by manager's common name like this:
(manager=cn=John Smith)
Can anyone write me an LDAP query that returns all users whose manager's sAMAccountName=administrator ?
manager has distinguished name syntax, therefore, if manager is used in an assertion, the full DN must be used as the value. Neither of the examples you gave meet this criteria. You must correct the filter to use a distinguished name.
The syntax of manager:
attributeTypes: ( 0.9.2342.19200300.100.1.10 NAME 'manager'
EQUALITY distinguishedNameMatch SYNTAX 1.3.6.1.4.1.1466.115.121.1.12
X-ORIGIN 'RFC 4524' )
To determine the syntax, use the LDAP Parameters Assignment page. On that page, search for the OID following the SYNTAX keyword (1.3.6.1.4.1.1466.115.121.1.12). That shows that it's DN syntax. Also, the EQUALITY matching rule is distinguishedNameMatch.
An example of an assertion in a filter using the correct syntax:
manager=cn=Manager Number One,ou=managers,ou=people,dc=example,dc=com
All attributes values used in an assertion must have the syntax defined for that attribute type in the schema.
Update
Verify the entries exist with a known good tool such as ldapsearch to ensure that the correct parameters are known for the search request. For example:
$ ldapsearch -h hostname -p port -b 'dc=sahara,dc=local' \
-D [your-bind-dn] -w [your-bind-dn-password] \
-s sub \
'(manager=cn=Izzeddeen Alkarajeh,ou=managers,ou=people,dc=sahara,dc=local)' \
1.1
If this search returns no entries, check with the LDAP administrators to ensure that the BIND DN in use has permission o read those entries.
see also
LDAP: Mastering Search Filters
LDAP: Search best practices
LDAP: Programming practices
I know this is old but I figured out a way to do this in C# that I have yet to find on stackoverflow.
using (var pc = new PrincipalContext(ContextType.Domain, "yourdomain.com"))
using (var user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, "samAccountName"))
{
DirectoryEntry de = (DirectoryEntry)user.GetUnderlyingObject();
if (de.Properties["directReports"].Count != 0)
managedFound = de.Properties["directReports"];
}
This will give you a list of strings that you can then parse out the CN using this:
managedUserName = Regex.Match(managedFound.ToString(), #"CN[=].*?[,]").Value.Replace("CN=", "").Replace(",", "");
Then, the following to get the User properties:
UserPrincipal managedUser = UserPrincipal.FindByIdentity(pc, IdentityType.Name, managedUserName);

How to get the nested groups in LDAP/AD?

We have an LDAP login problem of a specific user and I'm suspecting that this is due to cyclic groups assignment in LDAP, i.e. the user is assigned to groups A,B,C,D. Group A contains sub-groups E,F,G and group E contains group A again.
If I query for the user I can see that he has been assigned with 50+ groups and each group may contain more groups and each of those may contain more....
My question is if there's a query I can run to get the nested groups inside those main groups all the way down instead of going each group and do it manually?
The server is AD
To find all the groups that "user1" is a member of (adaptation of this answer see AD search filter):
Set the base to the groups container DN; for example root DN (dc=dom,dc=fr)
Set the scope to subtree
Use the following filter : (member:1.2.840.113556.1.4.1941:=cn=user1,cn=users,DC=x)
Example with LDIFDE.EXE (native command line AD search on windows) :
ldifde -f t.txt -d "DC=dom,DC=fr" -r "(member:1.2.840.113556.1.4.1941:=CN=jblanc,OU=MonOu,DC=dom,DC=fr)"
Remark : as far as I remember there is a small syntax difference with in brackets user DN name. '1.2.840.113556.1.4.1941' is not working in W2K3 SP1, it begins to work with SP2. I presume it's the same with W2K3 R2. I test here with W2K8R2.
With Apache Directory Studio :
Result :

Resources