|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
namespace SURFnet\VPN\Server; |
|
19
|
|
|
|
|
20
|
|
|
use SURFnet\VPN\Server\Exception\ConnectionException; |
|
21
|
|
|
use RuntimeException; |
|
22
|
|
|
|
|
23
|
|
|
class Connection |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
private $baseDir; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct($baseDir) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->baseDir = $baseDir; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function connect(array $envData) |
|
34
|
|
|
{ |
|
35
|
|
|
$userId = self::getUserId($envData['common_name']); |
|
36
|
|
|
|
|
37
|
|
|
$dataDir = sprintf('%s/data/%s', $this->baseDir, $envData['INSTANCE_ID']); |
|
38
|
|
|
|
|
39
|
|
|
// is the user account disabled? |
|
40
|
|
|
if (@file_exists(sprintf('%s/users/disabled/%s', $dataDir, $userId))) { |
|
41
|
|
|
throw new ConnectionException('client not allowed, user is disabled'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// is the common name disabled? |
|
45
|
|
|
if (@file_exists(sprintf('%s/common_names/disabled/%s', $dataDir, $envData['common_name']))) { |
|
46
|
|
|
throw new ConnectionException('client not allowed, CN is disabled'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$configDir = sprintf('%s/config/%s', $this->baseDir, $envData['INSTANCE_ID']); |
|
50
|
|
|
|
|
51
|
|
|
// read the instance/pool configuration |
|
52
|
|
|
$instanceConfig = InstanceConfig::fromFile( |
|
53
|
|
|
sprintf('%s/config.yaml', $configDir) |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
// is the ACL enabled? |
|
57
|
|
|
if ($instanceConfig->pool($envData['POOL_ID'])->v('enableAcl', false)) { |
|
58
|
|
|
$aclGroupProvider = $instanceConfig->pool($envData['POOL_ID'])->v('aclGroupProvider'); |
|
59
|
|
|
$groupProviderConfig = $instanceConfig->groupProvider($aclGroupProvider); |
|
60
|
|
|
$groupProviderClass = sprintf('SURFnet\VPN\Server\GroupProvider\%s', $aclGroupProvider); |
|
61
|
|
|
$groupProvider = new $groupProviderClass($groupProviderConfig); |
|
62
|
|
|
$aclGroupList = $instanceConfig->pool($envData['POOL_ID'])->v('aclGroupList', []); |
|
63
|
|
|
|
|
64
|
|
|
if (false === self::isMember($groupProvider->getGroups($userId), $aclGroupList)) { |
|
65
|
|
|
throw new ConnectionException(sprintf('client not allowed, not a member of "%s"', implode(',', $aclGroupList))); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
View Code Duplication |
private static function getUserId($commonName) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
if (false === $uPos = strpos($commonName, '_')) { |
|
73
|
|
|
throw new RuntimeException('unable to extract userId from commonName'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return substr($commonName, 0, $uPos); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private static function isMember(array $memberOf, array $aclGroupList) |
|
80
|
|
|
{ |
|
81
|
|
|
// one of the groups must be listed in the pool ACL list |
|
82
|
|
|
foreach ($memberOf as $memberGroup) { |
|
83
|
|
|
if (in_array($memberGroup['id'], $aclGroupList)) { |
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.