LDAP query for membership in Active Directory Security Group - active-directory

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.

Related

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.

Find LDAP Groups and Member emails

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.

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.

openldap in bash - get group member's by sAMAccountName?

I have been using a command similar to the following to query for group membership:
ldapsearch -H ldap://999.999.999.99\
-LLL -D \
"CN=BindCN,OU=Group,OU=Functional,OU=Users,DC=domain,DC=com" \
-x -w password \
-b "OU=GroupName,OU=Shares,DC=domain,DC=com" \
"cn=groupCN" \
-s sub member
This will list all members of the group by DN:
member;range=0-1499: CN=Last\, First (F),OU=Employees,OU=Users
,DC=domain,DC=com
member;range=0-1499: CN=Last\, First (F),OU=Employees,OU=Users
,DC=domain,DC=com
member;range=0-1499: CN=Last\, First (F),OU=Employees,OU=Users
,DC=domain,DC=com
...
Which is alright, but say that I have a list of sAMAccountName's that is 5000 lines, and I want to see if any are in the above group (which has 5000 members.) Is there any way for me to query group members by sAMAccountName?
Yes the DN in LDAP is always unique. It is not possible for the RDN (CN) to be the same within the same container.
The DN is the Fully Distinguished Name (ie CN=somecn,OU=Employees,OU=Users,DC=domain,DC=com)
You will need to query for the samAccountName(s) which will return the DNs and then resolve the group members from the DNs.
-jim

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