Passed
Push — devel-3.0 ( 18c750...febc79 )
by Rubén
03:13
created

LdapConnection::connectTls()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 0
dl 0
loc 20
rs 9.8666
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 $isTls;
64
    /**
65
     * @var bool
66
     */
67
    private $debug;
68
69
    /**
70
     * LdapBase constructor.
71
     *
72
     * @param LdapParams      $ldapParams
73
     * @param EventDispatcher $eventDispatcher
74
     * @param bool            $debug
75
     */
76
    public function __construct(LdapParams $ldapParams, EventDispatcher $eventDispatcher, $debug = false)
77
    {
78
        $this->ldapParams = $ldapParams;
79
        $this->eventDispatcher = $eventDispatcher;
80
        $this->debug = (bool)$debug;
81
    }
82
83
    /**
84
     * Comprobar la conexión al servidor de LDAP.
85
     *
86
     * @throws LdapException
87
     */
88
    public function checkConnection()
89
    {
90
        try {
91
            $this->connectAndBind();
92
93
            $this->eventDispatcher->notifyEvent('ldap.check.connection',
94
                new Event($this, EventMessage::factory()
95
                    ->addDescription(__u('Conexión a LDAP correcta')))
96
            );
97
        } catch (LdapException $e) {
98
            throw $e;
99
        }
100
    }
101
102
    /**
103
     * @return resource
104
     * @throws LdapException
105
     */
106
    public function connectAndBind()
107
    {
108
        if (!$this->isConnected && !$this->isBound) {
109
            $this->isConnected = $this->connect();
110
            $this->isBound = $this->bind();
111
        }
112
113
        return $this->ldapHandler;
114
    }
115
116
    /**
117
     * Realizar la conexión al servidor de LDAP.
118
     *
119
     * @throws LdapException
120
     * @return bool
121
     */
122
    public function connect(): bool
123
    {
124
        if ($this->isConnected) {
125
            return true;
126
        }
127
128
        $this->checkParams();
129
130
        // Habilitar la traza si el modo debug está habilitado
131
        if ($this->debug) {
132
            @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

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