Welcome to mirror list, hosted at ThFree Co, Russian Federation.

user_ldap.php « user_ldap « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b197aaa25029c88c0d10882c94ba5fe0044dba25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php

/**
 * ownCloud
 *
 * @author Dominik Schmidt
 * @copyright 2011 Dominik Schmidt dev@dominik-schmidt.de
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library 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 AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

require_once('User/backend.php');

class OC_USER_LDAP extends OC_USER_BACKEND {

	protected $ds;
	protected $configured = false;

	// cached settings
	protected $ldap_host;
	protected $ldap_port;
	protected $ldap_dn;
	protected $ldap_password;
	protected $ldap_base;
	protected $ldap_filter;

	function __construct() {
		$this->ldap_host = OC_APPCONFIG::getValue('user_ldap', 'ldap_host','');
		$this->ldap_port = OC_APPCONFIG::getValue('user_ldap', 'ldap_port', OC_USER_BACKEND_LDAP_DEFAULT_PORT	);
		$this->ldap_dn = OC_APPCONFIG::getValue('user_ldap', 'ldap_dn','');
		$this->ldap_password = OC_APPCONFIG::getValue('user_ldap', 'ldap_password','');
		$this->ldap_base = OC_APPCONFIG::getValue('user_ldap', 'ldap_base','');
		$this->ldap_filter = OC_APPCONFIG::getValue('user_ldap', 'ldap_filter','');

		if( !empty($this->ldap_host)
			&& !empty($this->ldap_port)
			&& !empty($this->ldap_dn)
			&& !empty($this->ldap_password)
			&& !empty($this->ldap_base)
			&& !empty($this->ldap_filter)
		)
		{
			$this->configured = true;
		}
	}

	function __destruct() {
		// close the connection
		if( $this->ds )
			ldap_unbind($this->ds);
	}

	private function getDs() {
		if(!$this->ds) {
			$this->ds = ldap_connect( $this->ldap_host, $this->ldap_port );
		}

		// login
		if(!empty($this->ldap_dn)) {
			$ldap_login = @ldap_bind( $this->ds, $this->ldap_dn, $this->ldap_password );
			if(!$ldap_login)
				return false;
		}

		return $this->ds;
	}

	private function getDn( $uid ) {
		if(!$this->configured)
			return false;

		// connect to server
		$ds = $this->getDs();
		if( !$ds )
			return false;

		// get dn
		$filter = str_replace("%uid", $uid, $this->ldap_filter);
		$sr = ldap_search( $this->getDs(), $this->ldap_base, $filter );
		$entries = ldap_get_entries( $this->getDs(), $sr );

		if( $entries["count"] == 0 )
			return false;

		return $entries[0]["dn"];
	}
	public function checkPassword( $uid, $password ) {
		if(!self::$configured){
			return false;
		}
		$dn = $this->getDn( $uid );
		if( !$dn )
			return false;

		return @ldap_bind( $this->getDs(), $dn, $password );
	}

	public function userExists( $uid ) {
		if(!self::$configured){
			return false;
		}
		$dn = $this->getDn($uid);
		return !empty($dn);
	}

}

?>