|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* sysPass |
|
4
|
|
|
* |
|
5
|
|
|
* @author nuxsmin |
|
6
|
|
|
* @link https://syspass.org |
|
7
|
|
|
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org |
|
8
|
|
|
* |
|
9
|
|
|
* This file is part of sysPass. |
|
10
|
|
|
* |
|
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU General Public License |
|
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace SP\Providers\Auth\Ldap; |
|
26
|
|
|
|
|
27
|
|
|
use SP\Core\Events\Event; |
|
28
|
|
|
use SP\Core\Events\EventMessage; |
|
29
|
|
|
use SP\Http\Address; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Class LdapMsAzureAd |
|
33
|
|
|
* |
|
34
|
|
|
* LDAP authentication based on Azure Active Directory |
|
35
|
|
|
* |
|
36
|
|
|
* @package SP\Auth\Ldap |
|
37
|
|
|
*/ |
|
38
|
|
|
final class LdapMsAzureAd extends Ldap |
|
39
|
|
|
{ |
|
40
|
|
|
const FILTER_USER_OBJECT = '(|(objectCategory=person)(objectClass=user))'; |
|
41
|
|
|
const FILTER_GROUP_OBJECT = '(objectCategory=group)'; |
|
42
|
|
|
const FILTER_USER_ATTRIBUTES = ['samaccountname', 'cn', 'uid', 'userPrincipalName']; |
|
43
|
|
|
const FILTER_GROUP_ATTRIBUTES = ['memberOf', 'groupMembership', 'memberof:1.2.840.113556.1.4.1941:']; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Devolver el filtro para comprobar la pertenecia al grupo |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
* @throws \SP\Core\Exceptions\SPException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getGroupMembershipFilter(): string |
|
52
|
|
|
{ |
|
53
|
|
|
if (empty($this->ldapParams->getGroup())) { |
|
54
|
|
|
return self::FILTER_USER_OBJECT; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return '(&(|' |
|
58
|
|
|
. LdapUtil::getAttributesForFilter( |
|
59
|
|
|
self::FILTER_GROUP_ATTRIBUTES, |
|
60
|
|
|
$this->getGroupDn()) |
|
61
|
|
|
. ')' |
|
62
|
|
|
. self::FILTER_USER_OBJECT |
|
63
|
|
|
. ')'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Obtener el filtro para buscar el usuario |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $userLogin |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getUserDnFilter(string $userLogin): string |
|
74
|
|
|
{ |
|
75
|
|
|
return '(&(|' |
|
76
|
|
|
. LdapUtil::getAttributesForFilter(self::FILTER_USER_ATTRIBUTES, $userLogin) |
|
77
|
|
|
. ')' |
|
78
|
|
|
. self::FILTER_USER_OBJECT |
|
79
|
|
|
. ')'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Devolver el filtro para objetos del tipo grupo |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getGroupObjectFilter(): string |
|
88
|
|
|
{ |
|
89
|
|
|
return self::FILTER_GROUP_OBJECT; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Buscar al usuario en un grupo. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $userDn |
|
96
|
|
|
* @param string $userLogin |
|
97
|
|
|
* @param array $groupsDn |
|
98
|
|
|
* |
|
99
|
|
|
* @return bool |
|
100
|
|
|
* @throws LdapException |
|
101
|
|
|
*/ |
|
102
|
|
|
public function isUserInGroup(string $userDn, string $userLogin, array $groupsDn): bool |
|
103
|
|
|
{ |
|
104
|
|
|
// Comprobar si está establecido el filtro de grupo o el grupo coincide con |
|
105
|
|
|
// los grupos del usuario |
|
106
|
|
|
if (empty($this->ldapParams->getGroup()) |
|
107
|
|
|
|| $this->ldapParams->getGroup() === '*' |
|
108
|
|
|
|| in_array($this->getGroupDn(), $groupsDn) |
|
109
|
|
|
) { |
|
110
|
|
|
$this->eventDispatcher->notifyEvent('ldap.check.group', |
|
111
|
|
|
new Event($this, EventMessage::factory() |
|
112
|
|
|
->addDescription(__u('User in group verified')) |
|
113
|
|
|
->addDetail(__u('User'), $userLogin) |
|
114
|
|
|
->addDetail(__u('Group'), $this->ldapParams->getGroup()))); |
|
115
|
|
|
|
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $this->checkUserInGroupByFilter($userLogin); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param string $userLogin |
|
124
|
|
|
* |
|
125
|
|
|
* @return bool |
|
126
|
|
|
* @throws LdapException |
|
127
|
|
|
*/ |
|
128
|
|
|
private function checkUserInGroupByFilter(string $userLogin): bool |
|
129
|
|
|
{ |
|
130
|
|
|
$groupDn = $this->getGroupDn(); |
|
131
|
|
|
|
|
132
|
|
|
$filter = '(&(|' |
|
133
|
|
|
. LdapUtil::getAttributesForFilter(self::FILTER_USER_ATTRIBUTES, $userLogin) |
|
134
|
|
|
. ')(|' |
|
135
|
|
|
. LdapUtil::getAttributesForFilter(self::FILTER_GROUP_ATTRIBUTES, $groupDn) |
|
136
|
|
|
. '))'; |
|
137
|
|
|
|
|
138
|
|
|
$searchResults = $this->ldapActions->getObjects($filter, ['dn']); |
|
139
|
|
|
|
|
140
|
|
|
if (isset($searchResults['count']) |
|
141
|
|
|
&& (int)$searchResults['count'] === 0 |
|
142
|
|
|
) { |
|
143
|
|
|
$this->eventDispatcher->notifyEvent('ldap.check.group', |
|
144
|
|
|
new Event($this, EventMessage::factory() |
|
145
|
|
|
->addDescription(__u('User does not belong to the group')) |
|
146
|
|
|
->addDetail(__u('User'), $userLogin) |
|
147
|
|
|
->addDetail(__u('Group'), $groupDn) |
|
148
|
|
|
->addDetail('LDAP FILTER', $filter))); |
|
149
|
|
|
|
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$this->eventDispatcher->notifyEvent('ldap.check.group', |
|
154
|
|
|
new Event($this, EventMessage::factory() |
|
155
|
|
|
->addDescription(__u('User in group verified')) |
|
156
|
|
|
->addDetail(__u('User'), $userLogin) |
|
157
|
|
|
->addDetail(__u('Group'), $groupDn))); |
|
158
|
|
|
|
|
159
|
|
|
return true; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Obtener el servidor de LDAP a utilizar |
|
164
|
|
|
* |
|
165
|
|
|
* @return mixed |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function pickServer() |
|
168
|
|
|
{ |
|
169
|
|
|
$server = $this->ldapParams->getServer(); |
|
170
|
|
|
|
|
171
|
|
|
if (preg_match(Address::PATTERN_IP_ADDRESS, $server)) { |
|
172
|
|
|
return $server; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$dnsServerQuery = '_msdcs' . substr($server, strpos($server, '.')); |
|
176
|
|
|
|
|
177
|
|
|
logger(sprintf('Querying DNS zone: %s', $dnsServerQuery)); |
|
178
|
|
|
|
|
179
|
|
|
$records = dns_get_record($dnsServerQuery, DNS_NS); |
|
180
|
|
|
|
|
181
|
|
|
if (empty($records)) { |
|
182
|
|
|
return $server; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$adServers = []; |
|
186
|
|
|
|
|
187
|
|
|
foreach ($records as $record) { |
|
188
|
|
|
$adServers[] = $record['target']; |
|
189
|
|
|
}; |
|
190
|
|
|
|
|
191
|
|
|
return count($adServers) > 0 ? array_rand($adServers) : $server; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|