]> BookStack Code Mirror - bookstack/commitdiff
Merge pull request #1096 from christophert/add-ldaptlsinsecure
authorDan Brown <redacted>
Sat, 22 Dec 2018 16:38:50 +0000 (16:38 +0000)
committerGitHub <redacted>
Sat, 22 Dec 2018 16:38:50 +0000 (16:38 +0000)
Add option to disable LDAPS Certificate Validation

1  2 
.env.example
app/Auth/Access/LdapService.php
config/services.php

diff --combined .env.example
index 6e015335efa711ccfd1adeb1134df655a516c30a,3ca612f647dec72a243a448670988d6ef9d862e2..11dafa2ab26b75870f660324ed3ac45d2338036e
@@@ -48,7 -48,6 +48,7 @@@ GITHUB_APP_ID=fals
  GITHUB_APP_SECRET=false
  GOOGLE_APP_ID=false
  GOOGLE_APP_SECRET=false
 +GOOGLE_SELECT_ACCOUNT=false
  OKTA_BASE_URL=false
  OKTA_APP_ID=false
  OKTA_APP_SECRET=false
@@@ -77,6 -76,8 +77,8 @@@ LDAP_GROUP_ATTRIBUTE="memberOf
  # Would you like to remove users from roles on BookStack if they do not match on LDAP
  # If false, the ldap groups-roles sync will only add users to roles
  LDAP_REMOVE_FROM_GROUPS=false
+ # Set this option to disable LDAPS Certificate Verification
+ LDAP_TLS_INSECURE=false
  
  # Mail settings
  MAIL_DRIVER=smtp
index b49ecf129fc1d5b606c60ecb668f2e0c6736eea1,9e626bbacf4527311a83e1de479aeaff7b128760..1e95ac513a340705d251d394045d39f1d0cdbe93
@@@ -107,7 -107,6 +107,7 @@@ class LdapServic
          if ($ldapUser === null) {
              return false;
          }
 +
          if ($ldapUser['uid'] !== $user->external_auth_id) {
              return false;
          }
          }
          $hostName = $ldapServer[0] . ($hasProtocol?':':'') . $ldapServer[1];
          $defaultPort = $ldapServer[0] === 'ldaps' ? 636 : 389;
+         /*
+          * Check if TLS_INSECURE is set. The handle is set to NULL due to the nature of
+          * the LDAP_OPT_X_TLS_REQUIRE_CERT option. It can only be set globally and not
+          * per handle.
+          */
+         if($this->config['tls_insecure']) {
+             $this->ldap->setOption(NULL, LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_NEVER);
+         }
          $ldapConnection = $this->ldap->connect($hostName, count($ldapServer) > 2 ? intval($ldapServer[2]) : $defaultPort);
  
          if ($ldapConnection === false) {
          $newAttrs = [];
          foreach ($attrs as $key => $attrText) {
              $newKey = '${' . $key . '}';
 -            $newAttrs[$newKey] = $attrText;
 +            $newAttrs[$newKey] = $this->ldap->escape($attrText);
          }
          return strtr($filterString, $newAttrs);
      }
          $baseDn = $this->config['base_dn'];
          $groupsAttr = strtolower($this->config['group_attribute']);
  
 -        $groups = $this->ldap->searchAndGetEntries($ldapConnection, $baseDn, 'CN='.$groupName, [$groupsAttr]);
 +        $groupFilter = 'CN=' . $this->ldap->escape($groupName);
 +        $groups = $this->ldap->searchAndGetEntries($ldapConnection, $baseDn, $groupFilter, [$groupsAttr]);
          if ($groups['count'] === 0) {
              return [];
          }
      /**
       * Filter out LDAP CN and DN language in a ldap search return
       * Gets the base CN (common name) of the string
 -     * @param string $ldapSearchReturn
 +     * @param array $userGroupSearchResponse
       * @return array
       */
 -    protected function groupFilter($ldapSearchReturn)
 +    protected function groupFilter(array $userGroupSearchResponse)
      {
          $groupsAttr = strtolower($this->config['group_attribute']);
          $ldapGroups = [];
          $count = 0;
 -        if (isset($ldapSearchReturn[$groupsAttr]['count'])) {
 -            $count = (int) $ldapSearchReturn[$groupsAttr]['count'];
 +
 +        if (isset($userGroupSearchResponse[$groupsAttr]['count'])) {
 +            $count = (int) $userGroupSearchResponse[$groupsAttr]['count'];
          }
 +
          for ($i=0; $i<$count; $i++) {
 -            $dnComponents = ldap_explode_dn($ldapSearchReturn[$groupsAttr][$i], 1);
 +            $dnComponents = $this->ldap->explodeDn($userGroupSearchResponse[$groupsAttr][$i], 1);
              if (!in_array($dnComponents[0], $ldapGroups)) {
                  $ldapGroups[] = $dnComponents[0];
              }
          }
 +
          return $ldapGroups;
      }
  
diff --combined config/services.php
index 857a7caa28ca197904fcf61fc3c7ff7ac9709e7b,98b1fce8eead176c29e9b52fb9b2f6b4c5cdb5cb..ba16488918ab085cdc35e00fd5b29e9c0a958e59
@@@ -59,7 -59,6 +59,7 @@@ return 
          'name'          => 'Google',
          'auto_register' => env('GOOGLE_AUTO_REGISTER', false),
          'auto_confirm' => env('GOOGLE_AUTO_CONFIRM_EMAIL', false),
 +        'select_account' => env('GOOGLE_SELECT_ACCOUNT', false),
      ],
  
      'slack'   => [
                'user_to_groups' => env('LDAP_USER_TO_GROUPS',false),
                'group_attribute' => env('LDAP_GROUP_ATTRIBUTE', 'memberOf'),
                'remove_from_groups' => env('LDAP_REMOVE_FROM_GROUPS',false),
+               'tls_insecure' => env('LDAP_TLS_INSECURE', false),
        ]
  
  ];