|
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\Api; |
|
19
|
|
|
|
|
20
|
|
|
use SURFnet\VPN\Common\Config; |
|
21
|
|
|
use SURFnet\VPN\Common\ProfileConfig; |
|
22
|
|
|
use SURFnet\VPN\Common\Http\ServiceModuleInterface; |
|
23
|
|
|
use SURFnet\VPN\Common\Http\Service; |
|
24
|
|
|
use SURFnet\VPN\Common\Http\ApiResponse; |
|
25
|
|
|
use SURFnet\VPN\Common\Http\Request; |
|
26
|
|
|
|
|
27
|
|
|
class ConnectionsModule implements ServiceModuleInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var \SURFnet\VPN\Common\Config */ |
|
30
|
|
|
private $config; |
|
31
|
|
|
|
|
32
|
|
|
/** @var Users */ |
|
33
|
|
|
private $users; |
|
34
|
|
|
|
|
35
|
|
|
/** @var CommonNames */ |
|
36
|
|
|
private $commonNames; |
|
37
|
|
|
|
|
38
|
|
|
/** @var ConnectionLog */ |
|
39
|
|
|
private $connectionLog; |
|
40
|
|
|
|
|
41
|
|
|
/** @var array */ |
|
42
|
|
|
private $groupProviders; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct(Config $config, Users $users, CommonNames $commonNames, ConnectionLog $connectionLog, array $groupProviders) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->config = $config; |
|
47
|
|
|
$this->users = $users; |
|
48
|
|
|
$this->commonNames = $commonNames; |
|
49
|
|
|
$this->connectionLog = $connectionLog; |
|
50
|
|
|
$this->groupProviders = $groupProviders; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function init(Service $service) |
|
54
|
|
|
{ |
|
55
|
|
|
$service->post( |
|
56
|
|
|
'/connect', |
|
57
|
|
|
function (Request $request, array $hookData) { |
|
58
|
|
|
Utils::requireUser($hookData, ['vpn-server-node']); |
|
59
|
|
|
|
|
60
|
|
|
$profileId = $request->getPostParameter('profile_id'); |
|
61
|
|
|
InputValidation::profileId($profileId); |
|
62
|
|
|
$commonName = $request->getPostParameter('common_name'); |
|
63
|
|
|
InputValidation::commonName($commonName); |
|
64
|
|
|
$ip4 = $request->getPostParameter('ip4'); |
|
65
|
|
|
InputValidation::ip4($ip4); |
|
66
|
|
|
$ip6 = $request->getPostParameter('ip6'); |
|
67
|
|
|
InputValidation::ip6($ip6); |
|
68
|
|
|
$connectedAt = $request->getPostParameter('connected_at'); |
|
69
|
|
|
InputValidation::connectedAt($connectedAt); |
|
70
|
|
|
|
|
71
|
|
|
$userId = self::getUserId($commonName); |
|
72
|
|
|
|
|
73
|
|
|
// check if user is disabled |
|
74
|
|
View Code Duplication |
if (true === $this->users->isDisabled($userId)) { |
|
|
|
|
|
|
75
|
|
|
return new ApiResponse('connect', ['ok' => false, 'error' => sprintf('user "%s" disabled', $userId)]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// check if the common_name is disabled |
|
79
|
|
View Code Duplication |
if (true === $this->commonNames->isDisabled($commonName)) { |
|
|
|
|
|
|
80
|
|
|
return new ApiResponse('connect', ['ok' => false, 'error' => sprintf('common_name "%s" disabled', $commonName)]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// if the ACL is enabled, verify that the user is allowed to |
|
84
|
|
|
// connect |
|
85
|
|
|
$profileConfig = new ProfileConfig($this->config->v('vpnProfiles', $profileId)); |
|
86
|
|
|
if ($profileConfig->v('enableAcl')) { |
|
87
|
|
|
$userGroups = []; |
|
88
|
|
|
foreach ($this->groupProviders as $groupProvider) { |
|
89
|
|
|
$userGroups = array_merge($userGroups, $groupProvider->getGroups($userId)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (false === self::isMember($userGroups, $profileConfig->v('aclGroupList'))) { |
|
93
|
|
|
return new ApiResponse('connect', ['ok' => false, 'error' => sprintf('user "%s" not in ACL', $userId)]); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
View Code Duplication |
if (false == $this->connectionLog->connect($profileId, $commonName, $ip4, $ip6, $connectedAt)) { |
|
|
|
|
|
|
98
|
|
|
return new ApiResponse('connect', ['ok' => false, 'error' => 'unable to write connect event to log, dropping client']); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return new ApiResponse('connect', ['ok' => true]); |
|
102
|
|
|
} |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
$service->post( |
|
106
|
|
|
'/disconnect', |
|
107
|
|
|
function (Request $request, array $hookData) { |
|
108
|
|
|
Utils::requireUser($hookData, ['vpn-server-node']); |
|
109
|
|
|
|
|
110
|
|
|
$profileId = $request->getPostParameter('profile_id'); |
|
111
|
|
|
InputValidation::profileId($profileId); |
|
112
|
|
|
$commonName = $request->getPostParameter('common_name'); |
|
113
|
|
|
InputValidation::commonName($commonName); |
|
114
|
|
|
$ip4 = $request->getPostParameter('ip4'); |
|
115
|
|
|
InputValidation::ip4($ip4); |
|
116
|
|
|
$ip6 = $request->getPostParameter('ip6'); |
|
117
|
|
|
InputValidation::ip6($ip6); |
|
118
|
|
|
$connectedAt = $request->getPostParameter('connected_at'); |
|
119
|
|
|
InputValidation::connectedAt($connectedAt); |
|
120
|
|
|
|
|
121
|
|
|
$disconnectedAt = $request->getPostParameter('disconnected_at'); |
|
122
|
|
|
InputValidation::disconnectedAt($disconnectedAt); |
|
123
|
|
|
|
|
124
|
|
|
$bytesTransferred = $request->getPostParameter('bytes_transferred'); |
|
125
|
|
|
InputValidation::bytesTransferred($bytesTransferred); |
|
126
|
|
|
|
|
127
|
|
View Code Duplication |
if (false === $this->connectionLog->disconnect($profileId, $commonName, $ip4, $ip6, $connectedAt, $disconnectedAt, $bytesTransferred)) { |
|
|
|
|
|
|
128
|
|
|
return new ApiResponse('disconnect', ['ok' => false, 'error' => 'unable to write disconnect event to log, nothing we can do']); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return new ApiResponse('disconnect', ['ok' => true]); |
|
132
|
|
|
} |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
private static function getUserId($commonName) |
|
137
|
|
|
{ |
|
138
|
|
|
// XXX do not repeat this everywhere |
|
139
|
|
|
|
|
140
|
|
|
// return the part before the first underscore, it is already validated |
|
141
|
|
|
// so we can be sure this is fine |
|
142
|
|
|
return substr($commonName, 0, strpos($commonName, '_')); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
private static function isMember(array $memberOf, array $aclGroupList) |
|
146
|
|
|
{ |
|
147
|
|
|
// one of the groups must be listed in the profile ACL list |
|
148
|
|
|
foreach ($memberOf as $memberGroup) { |
|
149
|
|
|
if (in_array($memberGroup['id'], $aclGroupList)) { |
|
150
|
|
|
return true; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return false; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
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.