[PATCH PROPOSITION] LDAP Group mapping code patch proposition
Describe the Bug
Hi,
Following this issue: https://github.com/BookStackApp/BookStack/issues/5730
As mentioned by @lucaguindani, auth fails on systems where groups returned in the memberOf attribute are in short format and not in "fqdn"
IE: memberOf: mygroup instead of memberOf: cn=mygroup,ou=other,ou=thing,c=tld
After digging in the code, I added a few lines that triggers a ldap search in case of the checked group is in short format.
File app/Access/LdapService.php
@@ -428,6 +428,12 @@
*/
protected function extractGroupsFromSearchResponseEntry(array $ldapEntry): array
{
+ $ldapConnection = $this->getConnection();
+ $this->bindSystemUser($ldapConnection);
+
+ $followReferrals = $this->config['follow_referrals'] ? 1 : 0;
+ $this->ldap->setOption($ldapConnection, LDAP_OPT_REFERRALS, $followReferrals);
+
$groupsAttr = strtolower($this->config['group_attribute']);
$groupDNs = [];
$count = 0;
@@ -435,9 +441,16 @@
if (isset($ldapEntry[$groupsAttr]['count'])) {
$count = (int) $ldapEntry[$groupsAttr]['count'];
}
-
+ $baseDn = $this->config['base_dn'];
for ($i = 0; $i < $count; $i++) {
$dn = $ldapEntry[$groupsAttr][$i];
+ if (!preg_match("/=$dn;/", $dn)) {
+ $read = $this->ldap->search($ldapConnection, $baseDn, "(cn=$dn)", ['dn']);
+ $result = $this->ldap->getEntries($ldapConnection, $read);
+ if (isset($result[0]['dn'])) {
+ $dn = $result[0]['dn'];
+ }
+ }
if (!in_array($dn, $groupDNs)) {
$groupDNs[] = $dn;
}
That fixed the site on my side.
Not sure if it's the best was to propose a patch but here it is.
Cheers,
Steps to Reproduce
Configure LDAP (with a ldap directory returning group membership in short format) Enable LDAP group mapping in .env Try to connect
Expected Behaviour
The connection should work, but it fails.
Screenshots or Additional Context
No response
Browser Details
No response
Exact BookStack Version
v26.03.1
Thanks for offering this @darxmurf.
I'm not quite comfortable assuming values as CNs, or assuming first matches are those relevant to the user, where the format is non-matching.
I think that anything out of the most common memberof=dn-list scenario may be best served by us providing a logical theme event, so that the logical theme system could be used to implement any desired logic, without us needing to maintain another flow and/or making assumptions, especially as the desire in this area has been fairly minimal.
Hi,
You are right, and my patch was just a quick option to fix bookstack in our environment. After thinking more about this issue, it's more complicated than we think 😄
First and as said in the first post, LDAP can return group membership in 2 ways
- cn=MyGroup,ou=groups,dc=domain,dc=com```
- MyGroup
The 2nd option is straightforward as we have directly the group name that match the Role in bookstack, it's what we have in our case and having BookstackAdmins and BookstackViewers groups will to the job to attribute roles perfectly.
The first option could be more complicated as you can have nested groups like
cn=WikiAdmins, ou=WikiProduction, dn=example, dn=com
cn=WikiAdmins, ou=WikiTesting,dn=example, dn=com
In this case we have 2 groups with the same name but in different OU. I'm not a LDAP guru at all but I think the only way to find the difference between the two WikiAdmins groups is to check the SAMAccountName proprety as this one is unique. But in any case, it's not something BookStack is made for (if I'm not wrong) and this property is not always available.
So in this case, the simplest thing would be to check the syntax of the memberOf value returned and extract the first element with a regex like
$str = "XX=MyGroup,YY=othergroup,zz=xyz,o=com";
$pattern = "/^[a-z0-9]+=([^,]+),.+$/i";
$group=preg_replace($pattern, "$1", $str);
This will extract anything found between the first = sign and the first ,. Assuming group names will not contain , of course. This will not filter things correctly on infrastructures with groups having same names in different sub groups but this can be filtered in another way with LDAP_USER_FILTER
Another point, while digging in the code, I found a function that looks for the parent group of a given group and adds it in the group list. I'm not sure to understand the purpose of this function.
Cheers and thanks for your time!
Okay, I'm just going to close this off as I wouldn't look to support formats outside of DNs right now as per my message above, but I'd be happy for a request to be made for a new logical theme event, for the purpose of being able to set custom LDAP group logic.
Hi, I would say the "simplest" way to do that would be to add a variable like LDAP_SHORT_GROUPS_NAMES=1 in .env to set this up one way or the other. Cheers,