Passed
Push — 3.0 ( d2a6b7...b247bd )
by Rubén
04:14
created

LdapStd::pickServer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
30
/**
31
 * Class LdapStd
32
 *
33
 * LDAP authentication based on an standard implementation
34
 *
35
 * @package SP\Auth\Ldap
36
 */
37
final class LdapStd extends Ldap
38
{
39
    const FILTER_USER_OBJECT = '(|(objectClass=inetOrgPerson)(objectClass=person)(objectClass=simpleSecurityObject))';
40
    const FILTER_GROUP_OBJECT = '(|(objectClass=groupOfNames)(objectClass=groupOfUniqueNames)(objectClass=group))';
41
    const FILTER_USER_ATTRIBUTES = ['samaccountname', 'cn', 'uid', 'userPrincipalName'];
42
    const FILTER_GROUP_ATTRIBUTES = ['memberOf', 'groupMembership'];
43
44
    /**
45
     * Devolver el filtro para objetos del tipo grupo
46
     *
47
     * @return string
48
     */
49
    public function getGroupObjectFilter(): string
50
    {
51
        return self::FILTER_GROUP_OBJECT;
52
    }
53
54
    /**
55
     * Devolver el filtro para comprobar la pertenecia al grupo
56
     *
57
     * @return string
58
     * @throws \SP\Core\Exceptions\SPException
59
     */
60
    public function getGroupMembershipFilter(): string
61
    {
62
        if (empty($this->ldapParams->getGroup())) {
63
            return self::FILTER_USER_OBJECT;
64
        }
65
66
        return '(&(|'
67
            . LdapUtil::getAttributesForFilter(
68
                self::FILTER_GROUP_ATTRIBUTES,
69
                $this->getGroupDn())
70
            . ')' . self::FILTER_USER_OBJECT
71
            . ')';
72
    }
73
74
    /**
75
     * Obtener el filtro para buscar el usuario
76
     *
77
     * @param string $userLogin
78
     *
79
     * @return mixed
80
     */
81
    public function getUserDnFilter(string $userLogin): string
82
    {
83
        return '(&(|'
84
            . LdapUtil::getAttributesForFilter(self::FILTER_USER_ATTRIBUTES, $userLogin)
85
            . ')'
86
            . self::FILTER_USER_OBJECT
87
            . ')';
88
    }
89
90
    /**
91
     * Buscar al usuario en un grupo.
92
     *
93
     * @param string $userDn
94
     * @param string $userLogin
95
     * @param array  $groupsDn
96
     *
97
     * @return bool
98
     * @throws LdapException
99
     */
100
    public function isUserInGroup(string $userDn, string $userLogin, array $groupsDn): bool
101
    {
102
        // Comprobar si está establecido el filtro de grupo o el grupo coincide con
103
        // los grupos del usuario
104
        if (empty($this->ldapParams->getGroup())
105
            || $this->ldapParams->getGroup() === '*'
106
            || in_array($this->getGroupDn(), $groupsDn)
107
        ) {
108
            $this->eventDispatcher->notifyEvent('ldap.check.group',
109
                new Event($this, EventMessage::factory()
110
                    ->addDescription(__u('User in group verified'))
111
                    ->addDetail(__u('User'), $userDn)
112
                    ->addDetail(__u('Group'), $this->ldapParams->getGroup())));
113
114
            return true;
115
        }
116
117
        return $this->checkUserInGroupByFilter($userDn);
118
    }
119
120
    /**
121
     * @param string $userDn
122
     *
123
     * @return bool
124
     * @throws LdapException
125
     */
126
    private function checkUserInGroupByFilter(string $userDn): bool
127
    {
128
        $groupName = ldap_escape($this->getGroupFromParams(), null, LDAP_ESCAPE_FILTER);
129
        $userDN = ldap_escape($userDn, null, LDAP_ESCAPE_FILTER);
130
        $filter = '(&(cn=' . $groupName . ')'
131
            . '(|(memberUid=' . $userDN . ')(member=' . $userDN . ')(uniqueMember=' . $userDN . '))'
132
            . self::FILTER_GROUP_OBJECT
133
            . ')';
134
135
        $searchResults = $this->ldapActions->getObjects($filter, ['dn']);
136
137
        if (isset($searchResults['count'])
138
            && (int)$searchResults['count'] === 0
139
        ) {
140
            $this->eventDispatcher->notifyEvent('ldap.check.group',
141
                new Event($this, EventMessage::factory()
142
                    ->addDescription(__u('User does not belong to the group'))
143
                    ->addDetail(__u('User'), $userDn)
144
                    ->addDetail(__u('Group'), $groupName)
145
                    ->addDetail('LDAP FILTER', $filter)));
146
147
            return false;
148
        }
149
150
        return true;
151
    }
152
153
    /**
154
     * Obtener el servidor de LDAP a utilizar
155
     *
156
     * @return mixed
157
     */
158
    protected function pickServer()
159
    {
160
        return $this->ldapParams->getServer();
161
    }
162
}