(How) can I enumerate programmatically Users in Well Known groups (SidTypeWellKnownGroup) ?
Ex: "Autenticated Users" or "Domain Users"
You can probably do this in WMI, but I'm not that familiar with it; I prefer using the plain old C APIs anyway. You can use the Network Management functions to do this. Given the group name, you can use NetGroupGetUsers to get all the members of a group. To get the group name you can use CreateWellKnownSid (or AllocateAndInitializeSid) and LookupAccountSid.
Related
I have a collection of SID values and need to display if they are in a given group. I know I can do that with CheckTokenMembership, if I had a token handle. How do I get a token handle from an SID in Windows?
Use LookupAccountSid to get the account for the SID
Use NetUserGetGroups to get all groups this account is a member of. EDIT Note from eryksun: Check target group's SID first. If it's SidTypeGroup, call NetUserGetGroups. If it's SidTypeAlias, call NetUserGetLocalGroups (AKA aliases, such as "BUILTIN\Administrators")
Use LookupAccountName to get the SIDs of the group.
Check if account SID (step 1) is member of the SID collection (step 3). Use EqualSid function.
You find appropriate old code one the old win32.mvps.org pages. Sadly the page is gone. But Wayback machine helps. Here a link to the old fksec samples
You find an easy to use sid class there. It also provides sid::MemberOf and sid::Members functions. The code isn't my style but it works.
I would like to get distinguish name "CN=XXX,CN=Users,DC=contoso,DC=com"
I know there is a function "LookupAccountSid" that get only the name of the account for SID.
Is there any similar function that get the distinguish name?
on c++
If you want user account name associated with the caller thread, use GetUserNameEx function with the NameFullyQualifiedDN parameter. You could obtain additional information using IADsADSystemInfo interface.
If you want to enumerate domain accounts, you could use ADSI interfaces.
Is there a possibility to query information from the AD in c++?
I'm especially interested in the value of the second field of "User logon name (pre-Windows 2000):" on the "Account" tab.
Other languages would also be fine.
Thanks in advance!
Yes. You can either use ADSI (via COM) or LDAP (via the wldap32 library) as two starting points. The attribute you want is called sAMAccountName.
I am writing some C++ code for the Windows(XP/7) platform to check the permission associated to a file. I want to verify that the file I am reading cannot be written by accounts with non elevated privileges. This is what I am currently doing:
I get the DACL associated with the file calling GetNamedSecurityInfo
I call CreateWellKnownSid with well known Sids, such as WinAuthenticatedUserSid (the Users group)
I call BuildTrusteeWithSid to build a TRUSTEE with the previous SID
I call GetEffectiveRightsFromAcl with the previously created trustee to get the effective acl
I check that the ACL does not contain the Write Flag set.
This code is working perfectly for the Users group. What about the other groups such as Everyone, Guests, or other specific users which may have specific write access on the file? I would like to find a solution where I don't need to enumerate all possible SID and check against all of them. Is there a SID I could use such as "anything but not admin"?
Regards,
Ant
I'd take a slightly different approach:
Get all ACE's via GetExplicitEntriesFromAcl, for the file and all its parents
Select the GRANT_ACCESS and SET_ACCESS ACE's
Get the list of trustees from the selected ACE's
For each of the trustees, perform an access check. GetEffectiveRightsFromAcl is probably the easiest solution here.
You'll want to think specifically what you want to do with the OWNER. He can alter the rights at any time.
Using my c++ program how can I find out what group the current user running my program belongs to? So my program need to figure out a couple of things :
The current username of the user
The group the user belongs to
How can do the above 2 using c++ on a RedHat / Linux machine?
With getuid(2) and getgid(2). See credentials(7) for more information.
Use getpwuid(3) and getgrgid(3) for the names.
You can find some of the information via getgid() (real GID) and getegid() (effective GID). For the other auxilliary groups, you need to use getgroups().
In practice, the real and effective GID are normally the same, but it is the effective GID that is used when creating a file. Usually, the group list returned by getgroups() includes the real group - though it is not clear that it actually has to do so.
You use getuid(2) and getgid(2) to get the numeric user and group ids, then use getpwuid(3) and getgrgid(3) to look up those ids in the user/group databases and turn them into text names.