-
Notifications
You must be signed in to change notification settings - Fork 302
Added support for LDAP. #1124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
El-Virus
wants to merge
49
commits into
sabre-io:master
Choose a base branch
from
El-Virus:LDAP
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Added support for LDAP. #1124
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
9ef2ecc
add SMTP email sending, LDAP authentication with auto user creation
epsilon-0 74e92aa
Added support for LDAP filters and attribute search
El-Virus 6cc928e
Removed new field hiding system and changed LDAP and SMTP parameters …
El-Virus 32aeff2
Add support for LDAP Groups, minor fixes
El-Virus 16922d2
Added Morphology hook to Standard Settings Controller of Baikal Admin.
El-Virus 75dbdf1
Merge commit '9ef2ecc184c72332f142061452f261e117d60986'
El-Virus 7b2bb3e
Merge commit '74e92aa3c48bc3cdce3d16e3b3fc552cd8cc9318'
El-Virus e8237c9
Merge commit '6cc928e050a77789622a8d450ac60ebba76fe254'
El-Virus 7abdc21
Merge commit '32aeff23ef9bd05d07a7d22a775501c9e36ad47b'
El-Virus 11fd40d
Added missing refresh on "WebDAV authentication type" change
El-Virus 248a4a8
Fix LDAP.php, according to linter.
El-Virus 9806378
Fix Standard.php, according to linter.
El-Virus 4eb5981
Fix (BaikalAdmin) Standard.php, according to linter.
El-Virus 996ea8d
Added Curly Braces to if statements.
El-Virus 7d6067f
Added a couple of missing spaces
El-Virus 42a926b
Added quotation marks surrounding url, and a period.
El-Virus 25741a6
Fixed https://github.com/sabre-io/Baikal/pull/1124#issuecomment-12394…
El-Virus d9c9d3d
Fixed linter errors
El-Virus 7fda407
fix the patternReplace function
epsilon-0 34bc4a9
LDAP bind Password hidden
El-Virus e8b2178
Merge pull request #1 from bsd-ac/LDAP
El-Virus 3e6ab43
Fix LDAP.php, according to linter.
El-Virus 38ca1f0
Epsilon0's merge fix
El-Virus f8d25b4
Merge remote-tracking branch 'refs/remotes/upstream/master'
El-Virus 3d3e756
Actually allow LDAP bind password to be set
El-Virus 6e455eb
Added LDAP Config Struct and default LDAP Params to dist config.
El-Virus 9a175df
Fix LDAP.php, according to linter.
El-Virus 0bfa4a8
Moved Structs folder to correct location.
El-Virus 8633f78
Fix LDAPConfig.php, according to linter.
El-Virus b7d68b3
Added empty value on config set safeguard.
El-Virus bf0288f
Fix LDAP.php's license
El-Virus 4fb8397
Fix LDAPConfig.php's license
El-Virus 171dab0
Changed copyright notice. Added check for empty bind password.
El-Virus 8885a9f
Changed $username to $dn
El-Virus 3f4c6a7
Removed an article from a settings label
El-Virus 8583553
Removed an article from a settings label
El-Virus afb5d38
Added slash to ldap_connect
El-Virus de8e4ff
Fixed typo in settings
El-Virus 12b4121
Remove articles from config page.
El-Virus b492d70
Fixed 'Undefined array key 0' on incorrect username
El-Virus cd967f1
Added check for LDAP extension availability
El-Virus 4b3213a
Fix LDAP.php, according to linter
El-Virus 3677285
Merge commit 'aa7e340113545f8be18b6e8c44d001fc4e684526'
El-Virus d62f271
Applied standard settings morphology hook to initialization wizard.
El-Virus d576221
Merge remote-tracking branch 'refs/remotes/upstream/master'
El-Virus 1ca48d6
Implemented user federation
El-Virus 176d2b8
Fix user federation code style
El-Virus 2c855b2
Fix user federation code style v2
El-Virus 4f2b492
Fix user federation code style v3
El-Virus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| <?php | ||
|
|
||
| namespace Baikal\Core; | ||
|
|
||
| use Baikal\Model\Principal\LDAP as Principal; | ||
|
|
||
| ################################################################# | ||
| # Copyright notice | ||
| # | ||
| # (c) 2022 Aisha Tammy <[email protected]> | ||
| # (c) 2022-2025 El-Virus <[email protected]> | ||
| # All rights reserved | ||
| # | ||
| # http://sabre.io/baikal | ||
| # | ||
| # This script is part of the Baïkal Server project. The Baïkal | ||
| # Server project is free software; you can redistribute it | ||
| # and/or modify it under the terms of the GNU General Public | ||
| # License as published by the Free Software Foundation; either | ||
| # version 2 of the License, or (at your option) any later version. | ||
| # | ||
| # The GNU General Public License can be found at | ||
| # http://www.gnu.org/copyleft/gpl.html. | ||
| # | ||
| # This script is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # This copyright notice MUST APPEAR in all copies of the script! | ||
| ################################################################# | ||
|
|
||
| /** | ||
| * This is an authentication backend that uses ldap. | ||
| */ | ||
| class LDAP extends \Sabre\DAV\Auth\Backend\AbstractBasic { | ||
| /** | ||
| * Reference to PDO connection. | ||
| * | ||
| * @var PDO | ||
| */ | ||
| protected $pdo; | ||
|
|
||
| /** | ||
| * PDO table name we'll be using. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $table_name; | ||
|
|
||
| /** | ||
| * LDAP Config. | ||
| * LDAP Config Struct. | ||
| * | ||
| * @var \Baikal\Model\Structs\LDAPConfig | ||
| */ | ||
| protected $ldap_config; | ||
|
|
||
| /** | ||
| * Replaces patterns for their assigned value using the | ||
| * given username, using cyrus-sasl style replacements. | ||
| * | ||
| * %u - gets replaced by full username | ||
| * %U - gets replaced by user part when the | ||
| * username is an email address | ||
| * %d - gets replaced by domain part when the | ||
| * username is an email address | ||
| * %% - gets replaced by % | ||
| * %1-9 - gets replaced by parts of the the domain | ||
| * split by '.' in reverse order | ||
| * | ||
| * full example for [email protected]: | ||
| * %u = [email protected] | ||
| * %U = jane.doe | ||
| * %d = mail.example.org | ||
| * %1 = org | ||
| * %2 = example | ||
| * %3 = mail | ||
| * | ||
| * @param string $line | ||
| * @param string $username | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function patternReplace($line, $username) { | ||
| $user_split = [$username]; | ||
| $user = $username; | ||
| $domain = ''; | ||
| try { | ||
| $user_split = explode('@', $username, 2); | ||
| $user = $user_split[0]; | ||
| if (2 == count($user_split)) { | ||
| $domain = $user_split[1]; | ||
| } | ||
| } catch (\Exception $ignored) { | ||
| } | ||
| $domain_split = []; | ||
| try { | ||
| $domain_split = array_reverse(explode('.', $domain)); | ||
| } catch (\Exception $ignored) { | ||
| $domain_split = []; | ||
| } | ||
|
|
||
| $parsed_line = ''; | ||
| for ($i = 0; $i < strlen($line); ++$i) { | ||
| if ('%' == $line[$i]) { | ||
| ++$i; | ||
| $next_char = $line[$i]; | ||
| if ('u' == $next_char) { | ||
| $parsed_line .= $username; | ||
| } elseif ('U' == $next_char) { | ||
| $parsed_line .= $user; | ||
| } elseif ('d' == $next_char) { | ||
| $parsed_line .= $domain; | ||
| } elseif ('%' == $next_char) { | ||
| $parsed_line .= '%'; | ||
| } else { | ||
| for ($j = 1; $j <= count($domain_split) && $j <= 9; ++$j) { | ||
| if ($next_char == '' . $j) { | ||
| $parsed_line .= $domain_split[$j - 1]; | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| $parsed_line .= $line[$i]; | ||
| } | ||
| } | ||
|
|
||
| return $parsed_line; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a user can bind with a password. | ||
| * If an error is produced, it will be logged. | ||
| * | ||
| * @param \LDAP\Connection &$conn | ||
| * @param string $dn | ||
| * @param string $password | ||
| * | ||
| * @return bool | ||
| */ | ||
| public static function doesBind(&$conn, $dn, $password) { | ||
| try { | ||
| $bind = ldap_bind($conn, $dn, $password); | ||
| if ($bind) { | ||
| return true; | ||
| } | ||
| } catch (\ErrorException $e) { | ||
| error_log($e->getMessage()); | ||
| error_log(ldap_error($conn)); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Creates the backend object. | ||
| * | ||
| * @param \PD0 $pdo | ||
| * @param string $table_name | ||
| * @param \Baikal\Model\Structs\LDAPConfig $ldap_config | ||
| */ | ||
| public function __construct(\PDO $pdo, $table_name, $ldap_config) { | ||
| $this->pdo = $pdo; | ||
| $this->table_name = $table_name; | ||
| $this->ldap_config = $ldap_config; | ||
| } | ||
|
|
||
| /** | ||
| * Connects to an LDAP server and tries to authenticate. | ||
| * | ||
| * @param string $username | ||
| * @param string $password | ||
| * | ||
| * @return bool | ||
| */ | ||
| protected function ldapOpen($username, $password) { | ||
| try { | ||
| $principal = new Principal($username, $this->ldap_config); | ||
| } catch (\Exception $ignored) { | ||
| return false; | ||
| } | ||
|
|
||
| $conn = ldap_connect($this->ldap_config->ldap_uri); | ||
| if (!$conn) { | ||
| return false; | ||
| } | ||
| if (!ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3)) { | ||
| return false; | ||
| } | ||
|
|
||
| $success = $this->doesBind($conn, $principal->dn, $password); | ||
|
|
||
| ldap_close($conn); | ||
|
|
||
| if ($success) { | ||
| $stmt = $this->pdo->prepare('SELECT 1 FROM ' . $this->table_name . ' WHERE username = ?'); | ||
| $stmt->execute([$username]); | ||
| $result = $stmt->fetchAll(); | ||
|
|
||
| if (empty($result)) { | ||
| $user = new \Baikal\Model\User(); | ||
| $user->set('federation', 'LDAP'); | ||
| $user->set('username', $username); | ||
| $user->persist(); | ||
| } | ||
| } | ||
|
|
||
| return $success; | ||
| } | ||
|
|
||
| /** | ||
| * Validates a username and password by trying to authenticate against LDAP. | ||
| * | ||
| * @param string $username | ||
| * @param string $password | ||
| * | ||
| * @return bool | ||
| */ | ||
| protected function validateUserPass($username, $password) { | ||
| if (!extension_loaded("ldap")) { | ||
| error_log('PHP LDAP extension not enabled'); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return $this->ldapOpen($username, $password); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.