Passed
Push — devel-3.0 ( 5a06ca...84adb8 )
by Rubén
03:54
created

LdapConnection::getErrorCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
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\EventDispatcher;
29
use SP\Core\Events\EventMessage;
30
31
32
/**
33
 * Class LdapConnection
34
 *
35
 * @package SP\Providers\Auth\Ldap
36
 */
37
final class LdapConnection implements LdapConnectionInterface
38
{
39
    const TIMEOUT = 10;
40
    /**
41
     * @var resource
42
     */
43
    private $ldapHandler;
44
    /**
45
     * @var LdapParams
46
     */
47
    private $ldapParams;
48
    /**
49
     * @var EventDispatcher
50
     */
51
    private $eventDispatcher;
52
    /**
53
     * @var bool
54
     */
55
    private $isConnected = false;
56
    /**
57
     * @var bool
58
     */
59
    private $isBound = false;
60
    /**
61
     * @var bool
62
     */
63
    private $debug;
64
65
    /**
66
     * LdapBase constructor.
67
     *
68
     * @param LdapParams      $ldapParams
69
     * @param EventDispatcher $eventDispatcher
70
     * @param bool            $debug
71
     */
72
    public function __construct(LdapParams $ldapParams, EventDispatcher $eventDispatcher, $debug = false)
73
    {
74
        $this->ldapParams = $ldapParams;
75
        $this->eventDispatcher = $eventDispatcher;
76
        $this->debug = (bool)$debug;
77
    }
78
79
    /**
80
     * Comprobar la conexión al servidor de LDAP.
81
     *
82
     * @throws LdapException
83
     */
84
    public function checkConnection()
85
    {
86
        try {
87
            $this->connectAndBind();
88
89
            $this->eventDispatcher->notifyEvent('ldap.check.connection',
90
                new Event($this, EventMessage::factory()
91
                    ->addDescription(__u('Conexión a LDAP correcta')))
92
            );
93
        } catch (LdapException $e) {
94
            throw $e;
95
        }
96
    }
97
98
    /**
99
     * @return resource
100
     * @throws LdapException
101
     */
102
    public function connectAndBind()
103
    {
104
        if (!$this->isConnected && !$this->isBound) {
105
            $this->isConnected = $this->connect();
106
            $this->isBound = $this->bind();
107
        }
108
109
        return $this->ldapHandler;
110
    }
111
112
    /**
113
     * Realizar la conexión al servidor de LDAP.
114
     *
115
     * @throws LdapException
116
     * @return bool
117
     */
118
    public function connect(): bool
119
    {
120
        if ($this->isConnected) {
121
            return true;
122
        }
123
124
        $this->checkParams();
125
126
        // Habilitar la traza si el modo debug está habilitado
127
        if ($this->debug) {
128
            @ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for ldap_set_option(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

128
            /** @scrutinizer ignore-unhandled */ @ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
129
        }
130
131
        $this->ldapHandler = @ldap_connect($this->ldapParams->getServer(), $this->ldapParams->getPort());
132
133
        // Conexión al servidor LDAP
134
        if (!is_resource($this->ldapHandler)) {
135
            $this->eventDispatcher->notifyEvent('ldap.connection',
136
                new Event($this, EventMessage::factory()
137
                    ->addDescription(__u('No es posible conectar con el servidor de LDAP'))
138
                    ->addDetail(__u('Servidor'), $this->ldapParams->getServer()))
139
            );
140
141
            throw new LdapException(__u('No es posible conectar con el servidor de LDAP'));
142
        }
143
144
        @ldap_set_option($this->ldapHandler, LDAP_OPT_NETWORK_TIMEOUT, self::TIMEOUT);
145
        @ldap_set_option($this->ldapHandler, LDAP_OPT_PROTOCOL_VERSION, 3);
146
147
        return true;
148
    }
149
150
    /**
151
     * Comprobar si los parámetros necesario de LDAP están establecidos.
152
     *
153
     * @throws LdapException
154
     */
155
    public function checkParams()
156
    {
157
        if (!$this->ldapParams->getSearchBase()
158
            || !$this->ldapParams->getServer()
159
            || !$this->ldapParams->getBindDn()
160
        ) {
161
            $this->eventDispatcher->notifyEvent('ldap.check.params',
162
                new Event($this, EventMessage::factory()
163
                    ->addDescription(__u('Los parámetros de LDAP no están configurados')))
164
            );
165
166
            throw new LdapException(__u('Los parámetros de LDAP no están configurados'));
167
        }
168
169
//        $this->server = $this->pickServer();
170
//        $this->ldapAuthData->setServer($this->server);
171
    }
172
173
    /**
174
     * Realizar la autentificación con el servidor de LDAP.
175
     *
176
     * @param string $bindDn   con el DN del usuario
177
     * @param string $bindPass con la clave del usuario
178
     *
179
     * @throws LdapException
180
     * @return bool
181
     */
182
    public function bind(string $bindDn = null, string $bindPass = null): bool
183
    {
184
//        $this->ldapAuthData->setAuthenticated($bindDn && $bindPass);
185
186
        $dn = $bindDn ?: $this->ldapParams->getBindDn();
187
        $pass = $bindPass ?: $this->ldapParams->getBindPass();
188
189
        if (@ldap_bind($this->ldapHandler, $dn, $pass) === false) {
190
            $this->eventDispatcher->notifyEvent('ldap.bind',
191
                new Event($this, EventMessage::factory()
192
                    ->addDescription(__u('Error al conectar (BIND)'))
193
                    ->addDetail('LDAP ERROR', self::getLdapErrorMessage($this->ldapHandler))
194
                    ->addDetail('LDAP DN', $dn))
195
            );
196
197
            throw new LdapException(
198
                __u('Error al conectar (BIND)'),
199
                LdapException::ERROR,
200
                self::getLdapErrorMessage($this->ldapHandler),
201
                $this->getErrorCode()
202
            );
203
        }
204
205
        return true;
206
    }
207
208
    /**
209
     * Registrar error de LDAP y devolver el mensaje de error
210
     *
211
     * @param $ldapHandler
212
     *
213
     * @return string
214
     */
215
    public static function getLdapErrorMessage($ldapHandler)
216
    {
217
        return sprintf('%s (%d)', ldap_error($ldapHandler), ldap_errno($ldapHandler));
218
    }
219
220
    /**
221
     * @return int
222
     */
223
    public function getErrorCode()
224
    {
225
        if (is_resource($this->ldapHandler)) {
226
            return ldap_errno($this->ldapHandler);
227
        }
228
229
        return -1;
230
    }
231
232
    /**
233
     * @return bool
234
     */
235
    public function isConnected(): bool
236
    {
237
        return $this->isConnected;
238
    }
239
240
    /**
241
     * @return bool
242
     */
243
    public function isBound(): bool
244
    {
245
        return $this->isBound;
246
    }
247
248
    /**
249
     * @return LdapParams
250
     */
251
    public function getLdapParams(): LdapParams
252
    {
253
        return $this->ldapParams;
254
    }
255
256
    /**
257
     * @return bool
258
     */
259
    public function isDebug(): bool
260
    {
261
        return $this->debug;
262
    }
263
264
    /**
265
     * Realizar la desconexión del servidor de LDAP.
266
     */
267
    public function unbind(): bool
268
    {
269
        if (($this->isConnected || $this->isBound)
270
            && @ldap_unbind($this->ldapHandler) === false
271
        ) {
272
            $this->eventDispatcher->notifyEvent('ldap.unbind',
273
                new Event($this, EventMessage::factory()
274
                    ->addDescription(__u('Error al desconectar del servidor de LDAP'))
275
                    ->addDetail('LDAP ERROR', self::getLdapErrorMessage($this->ldapHandler)))
276
            );
277
278
            return false;
279
        }
280
281
        return true;
282
    }
283
}