|
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
|
|
|
|
|
19
|
|
|
namespace SURFnet\VPN\Server\Api; |
|
20
|
|
|
|
|
21
|
|
|
use DateTime; |
|
22
|
|
|
use SURFnet\VPN\Common\Config; |
|
23
|
|
|
use SURFnet\VPN\Common\Http\ApiErrorResponse; |
|
24
|
|
|
use SURFnet\VPN\Common\Http\ApiResponse; |
|
25
|
|
|
use SURFnet\VPN\Common\Http\AuthUtils; |
|
26
|
|
|
use SURFnet\VPN\Common\Http\InputValidation; |
|
27
|
|
|
use SURFnet\VPN\Common\Http\Request; |
|
28
|
|
|
use SURFnet\VPN\Common\Http\Service; |
|
29
|
|
|
use SURFnet\VPN\Common\Http\ServiceModuleInterface; |
|
30
|
|
|
use SURFnet\VPN\Common\ProfileConfig; |
|
31
|
|
|
use SURFnet\VPN\Server\Exception\TotpException; |
|
32
|
|
|
use SURFnet\VPN\Server\Exception\YubiKeyException; |
|
33
|
|
|
use SURFnet\VPN\Server\Storage; |
|
34
|
|
|
use SURFnet\VPN\Server\Totp; |
|
35
|
|
|
use SURFnet\VPN\Server\YubiKey; |
|
36
|
|
|
|
|
37
|
|
|
class ConnectionsModule implements ServiceModuleInterface |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var \SURFnet\VPN\Common\Config */ |
|
40
|
|
|
private $config; |
|
41
|
|
|
|
|
42
|
|
|
/** @var \SURFnet\VPN\Server\Storage */ |
|
43
|
|
|
private $storage; |
|
44
|
|
|
|
|
45
|
|
|
/** @var array */ |
|
46
|
|
|
private $groupProviders; |
|
47
|
|
|
|
|
48
|
|
|
public function __construct(Config $config, Storage $storage, array $groupProviders) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->config = $config; |
|
51
|
|
|
$this->storage = $storage; |
|
52
|
|
|
$this->groupProviders = $groupProviders; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function init(Service $service) |
|
56
|
|
|
{ |
|
57
|
|
|
$service->post( |
|
58
|
|
|
'/connect', |
|
59
|
|
|
function (Request $request, array $hookData) { |
|
60
|
|
|
AuthUtils::requireUser($hookData, ['vpn-server-node']); |
|
61
|
|
|
|
|
62
|
|
|
return $this->connect($request); |
|
63
|
|
|
} |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$service->post( |
|
67
|
|
|
'/disconnect', |
|
68
|
|
|
function (Request $request, array $hookData) { |
|
69
|
|
|
AuthUtils::requireUser($hookData, ['vpn-server-node']); |
|
70
|
|
|
|
|
71
|
|
|
return $this->disconnect($request); |
|
72
|
|
|
} |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
$service->post( |
|
76
|
|
|
'/verify_two_factor', |
|
77
|
|
|
function (Request $request, array $hookData) { |
|
78
|
|
|
AuthUtils::requireUser($hookData, ['vpn-server-node']); |
|
79
|
|
|
|
|
80
|
|
|
return $this->verifyTwoFactor($request); |
|
81
|
|
|
} |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function connect(Request $request) |
|
86
|
|
|
{ |
|
87
|
|
|
$profileId = InputValidation::profileId($request->getPostParameter('profile_id')); |
|
88
|
|
|
$commonName = InputValidation::commonName($request->getPostParameter('common_name')); |
|
89
|
|
|
$ip4 = InputValidation::ip4($request->getPostParameter('ip4')); |
|
90
|
|
|
$ip6 = InputValidation::ip6($request->getPostParameter('ip6')); |
|
91
|
|
|
$connectedAt = InputValidation::connectedAt($request->getPostParameter('connected_at')); |
|
92
|
|
|
|
|
93
|
|
|
if (true !== $response = $this->verifyConnection($profileId, $commonName)) { |
|
94
|
|
|
return $response; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->storage->clientConnect($profileId, $commonName, $ip4, $ip6, new DateTime(sprintf('@%d', $connectedAt))); |
|
98
|
|
|
|
|
99
|
|
|
return new ApiResponse('connect'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function disconnect(Request $request) |
|
103
|
|
|
{ |
|
104
|
|
|
$profileId = InputValidation::profileId($request->getPostParameter('profile_id')); |
|
105
|
|
|
$commonName = InputValidation::commonName($request->getPostParameter('common_name')); |
|
106
|
|
|
$ip4 = InputValidation::ip4($request->getPostParameter('ip4')); |
|
107
|
|
|
$ip6 = InputValidation::ip6($request->getPostParameter('ip6')); |
|
108
|
|
|
|
|
109
|
|
|
$connectedAt = InputValidation::connectedAt($request->getPostParameter('connected_at')); |
|
110
|
|
|
$disconnectedAt = InputValidation::disconnectedAt($request->getPostParameter('disconnected_at')); |
|
111
|
|
|
$bytesTransferred = InputValidation::bytesTransferred($request->getPostParameter('bytes_transferred')); |
|
112
|
|
|
|
|
113
|
|
|
$this->storage->clientDisconnect($profileId, $commonName, $ip4, $ip6, new DateTime(sprintf('@%d', $connectedAt)), new DateTime(sprintf('@%d', $disconnectedAt)), $bytesTransferred); |
|
114
|
|
|
|
|
115
|
|
|
return new ApiResponse('disconnect'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function verifyTwoFactor(Request $request) |
|
119
|
|
|
{ |
|
120
|
|
|
$commonName = InputValidation::commonName($request->getPostParameter('common_name')); |
|
121
|
|
|
$twoFactorType = InputValidation::twoFactorType($request->getPostParameter('two_factor_type')); |
|
122
|
|
|
|
|
123
|
|
|
$certInfo = $this->storage->getUserCertificateInfo($commonName); |
|
124
|
|
|
$userId = $certInfo['user_id']; |
|
125
|
|
|
|
|
126
|
|
|
switch ($twoFactorType) { |
|
127
|
|
|
case 'yubi': |
|
128
|
|
|
$yubiKeyOtp = InputValidation::yubiKeyOtp($request->getPostParameter('two_factor_value')); |
|
129
|
|
|
// XXX make sure user has a yubiKeyId first! |
|
130
|
|
|
$yubiKeyId = $this->storage->getYubiKeyId($userId); |
|
131
|
|
|
try { |
|
132
|
|
|
$yubiKey = new YubiKey(); |
|
133
|
|
|
$yubiKey->verify($userId, $yubiKeyOtp, $yubiKeyId); |
|
134
|
|
|
} catch (YubiKeyException $e) { |
|
135
|
|
|
$this->storage->addUserMessage($userId, 'notification', sprintf('[VPN] YubiKey OTP validation failed: %s', $e->getMessage())); |
|
136
|
|
|
|
|
137
|
|
|
return new ApiErrorResponse('verify_two_factor', $e->getMessage()); |
|
138
|
|
|
} |
|
139
|
|
|
break; |
|
140
|
|
|
case 'totp': |
|
141
|
|
|
$totpKey = InputValidation::totpKey($request->getPostParameter('two_factor_value')); |
|
142
|
|
|
try { |
|
143
|
|
|
$totp = new Totp($this->storage); |
|
144
|
|
|
$totp->verify($userId, $totpKey); |
|
145
|
|
|
} catch (TotpException $e) { |
|
146
|
|
|
$this->storage->addUserMessage($userId, 'notification', sprintf('[VPN] TOTP validation failed: %s', $e->getMessage())); |
|
147
|
|
|
|
|
148
|
|
|
return new ApiErrorResponse('verify_two_factor', $e->getMessage()); |
|
149
|
|
|
} |
|
150
|
|
|
break; |
|
151
|
|
|
default: |
|
152
|
|
|
return new ApiErrorResponse('verify_two_factor', 'invalid two factor type'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return new ApiResponse('verify_two_factor'); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
private function verifyConnection($profileId, $commonName) |
|
159
|
|
|
{ |
|
160
|
|
|
// verify status of certificate/user |
|
161
|
|
|
if (false === $result = $this->storage->getUserCertificateInfo($commonName)) { |
|
162
|
|
|
// if a certificate does no longer exist, we cannot figure out the user |
|
163
|
|
|
return new ApiErrorResponse('connect', 'user or certificate does not exist'); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
View Code Duplication |
if ($result['user_is_disabled']) { |
|
|
|
|
|
|
167
|
|
|
$msg = '[VPN] unable to connect, account is disabled'; |
|
168
|
|
|
$this->storage->addUserMessage($result['user_id'], 'notification', $msg); |
|
169
|
|
|
|
|
170
|
|
|
return new ApiErrorResponse('connect', $msg); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
View Code Duplication |
if ($result['certificate_is_disabled']) { |
|
|
|
|
|
|
174
|
|
|
$msg = sprintf('[VPN] unable to connect, certificate "%s" is disabled', $result['display_name']); |
|
175
|
|
|
$this->storage->addUserMessage($result['user_id'], 'notification', $msg); |
|
176
|
|
|
|
|
177
|
|
|
return new ApiErrorResponse('connect', $msg); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $this->verifyAcl($profileId, $result['user_id']); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
private function verifyAcl($profileId, $externalUserId) |
|
184
|
|
|
{ |
|
185
|
|
|
// verify ACL |
|
186
|
|
|
$profileConfig = new ProfileConfig($this->config->v('vpnProfiles', $profileId)); |
|
187
|
|
|
if ($profileConfig->v('enableAcl')) { |
|
188
|
|
|
// ACL enabled |
|
189
|
|
|
$userGroups = []; |
|
190
|
|
|
foreach ($this->groupProviders as $groupProvider) { |
|
191
|
|
|
$userGroups = array_merge($userGroups, $groupProvider->getGroups($externalUserId)); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
if (false === self::isMember($userGroups, $profileConfig->v('aclGroupList'))) { |
|
195
|
|
|
$msg = '[VPN] unable to connect, account not a member of required group'; |
|
196
|
|
|
$this->storage->addUserMessage($externalUserId, 'notification', $msg); |
|
197
|
|
|
|
|
198
|
|
|
return new ApiErrorResponse('connect', $msg); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return true; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
private static function isMember(array $memberOf, array $aclGroupList) |
|
206
|
|
|
{ |
|
207
|
|
|
// one of the groups must be listed in the profile ACL list |
|
208
|
|
|
foreach ($memberOf as $memberGroup) { |
|
209
|
|
|
if (in_array($memberGroup['id'], $aclGroupList)) { |
|
210
|
|
|
return true; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
return false; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
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.