|
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 SURFnet\VPN\Common\Config; |
|
22
|
|
|
use SURFnet\VPN\Common\Http\ApiResponse; |
|
23
|
|
|
use SURFnet\VPN\Common\Http\AuthUtils; |
|
24
|
|
|
use SURFnet\VPN\Common\Http\InputValidation; |
|
25
|
|
|
use SURFnet\VPN\Common\Http\Request; |
|
26
|
|
|
use SURFnet\VPN\Common\Http\Service; |
|
27
|
|
|
use SURFnet\VPN\Common\Http\ServiceModuleInterface; |
|
28
|
|
|
use SURFnet\VPN\Common\ProfileConfig; |
|
29
|
|
|
use SURFnet\VPN\Server\Storage; |
|
30
|
|
|
|
|
31
|
|
|
class ConnectionsModule implements ServiceModuleInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var \SURFnet\VPN\Common\Config */ |
|
34
|
|
|
private $config; |
|
35
|
|
|
|
|
36
|
|
|
/** @var \SURFnet\VPN\Server\Storage */ |
|
37
|
|
|
private $storage; |
|
38
|
|
|
|
|
39
|
|
|
/** @var array */ |
|
40
|
|
|
private $groupProviders; |
|
41
|
|
|
|
|
42
|
|
|
public function __construct(Config $config, Storage $storage, array $groupProviders) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->config = $config; |
|
45
|
|
|
$this->storage = $storage; |
|
46
|
|
|
$this->groupProviders = $groupProviders; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function init(Service $service) |
|
50
|
|
|
{ |
|
51
|
|
|
$service->post( |
|
52
|
|
|
'/connect', |
|
53
|
|
|
function (Request $request, array $hookData) { |
|
54
|
|
|
AuthUtils::requireUser($hookData, ['vpn-server-node']); |
|
55
|
|
|
|
|
56
|
|
|
return $this->connect($request); |
|
57
|
|
|
} |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$service->post( |
|
61
|
|
|
'/disconnect', |
|
62
|
|
|
function (Request $request, array $hookData) { |
|
63
|
|
|
AuthUtils::requireUser($hookData, ['vpn-server-node']); |
|
64
|
|
|
|
|
65
|
|
|
return $this->disconnect($request); |
|
66
|
|
|
} |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$service->post( |
|
70
|
|
|
'/verify_otp', |
|
71
|
|
|
function (Request $request, array $hookData) { |
|
72
|
|
|
AuthUtils::requireUser($hookData, ['vpn-server-node']); |
|
73
|
|
|
|
|
74
|
|
|
return $this->verifyOtp($request); |
|
75
|
|
|
} |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function connect(Request $request) |
|
80
|
|
|
{ |
|
81
|
|
|
$profileId = InputValidation::profileId($request->getPostParameter('profile_id')); |
|
82
|
|
|
$commonName = InputValidation::commonName($request->getPostParameter('common_name')); |
|
83
|
|
|
$ip4 = InputValidation::ip4($request->getPostParameter('ip4')); |
|
84
|
|
|
$ip6 = InputValidation::ip6($request->getPostParameter('ip6')); |
|
85
|
|
|
$connectedAt = InputValidation::connectedAt($request->getPostParameter('connected_at')); |
|
86
|
|
|
|
|
87
|
|
|
if (true !== $response = $this->verifyConnection($profileId, $commonName)) { |
|
88
|
|
|
return $response; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
View Code Duplication |
if (false == $this->storage->clientConnect($profileId, $commonName, $ip4, $ip6, $connectedAt)) { |
|
|
|
|
|
|
92
|
|
|
return new ApiResponse('connect', ['ok' => false, 'error' => 'unable to write connect event to log']); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return new ApiResponse('connect', ['ok' => true]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function verifyConnection($profileId, $commonName) |
|
99
|
|
|
{ |
|
100
|
|
|
// verify status of certificate/user |
|
101
|
|
|
if (false === $result = $this->storage->getUserCertificateStatus($commonName)) { |
|
102
|
|
|
return new ApiResponse('connect', ['ok' => false, 'error' => 'user or certificate does not exist']); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
View Code Duplication |
if ($result['user_is_disabled']) { |
|
|
|
|
|
|
106
|
|
|
return new ApiResponse('connect', ['ok' => false, 'error' => 'user is disabled']); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
View Code Duplication |
if ($result['certificate_is_disabled']) { |
|
|
|
|
|
|
110
|
|
|
return new ApiResponse('connect', ['ok' => false, 'error' => 'certificate is disabled']); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $this->verifyAcl($profileId, $result['external_user_id']); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
private function verifyAcl($profileId, $externalUserId) |
|
117
|
|
|
{ |
|
118
|
|
|
// verify ACL |
|
119
|
|
|
$profileConfig = new ProfileConfig($this->config->v('vpnProfiles', $profileId)); |
|
120
|
|
|
if ($profileConfig->v('enableAcl')) { |
|
121
|
|
|
// ACL enabled |
|
122
|
|
|
$userGroups = []; |
|
123
|
|
|
foreach ($this->groupProviders as $groupProvider) { |
|
124
|
|
|
$userGroups = array_merge($userGroups, $groupProvider->getGroups($externalUserId)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if (false === self::isMember($userGroups, $profileConfig->v('aclGroupList'))) { |
|
128
|
|
|
return new ApiResponse('connect', ['ok' => false, 'error' => 'user not in ACL']); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
private static function isMember(array $memberOf, array $aclGroupList) |
|
136
|
|
|
{ |
|
137
|
|
|
// one of the groups must be listed in the profile ACL list |
|
138
|
|
|
foreach ($memberOf as $memberGroup) { |
|
139
|
|
|
if (in_array($memberGroup['id'], $aclGroupList)) { |
|
140
|
|
|
return true; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function disconnect(Request $request) |
|
148
|
|
|
{ |
|
149
|
|
|
$profileId = InputValidation::profileId($request->getPostParameter('profile_id')); |
|
150
|
|
|
$commonName = InputValidation::commonName($request->getPostParameter('common_name')); |
|
151
|
|
|
$ip4 = InputValidation::ip4($request->getPostParameter('ip4')); |
|
152
|
|
|
$ip6 = InputValidation::ip6($request->getPostParameter('ip6')); |
|
153
|
|
|
|
|
154
|
|
|
$connectedAt = InputValidation::connectedAt($request->getPostParameter('connected_at')); |
|
155
|
|
|
$disconnectedAt = InputValidation::disconnectedAt($request->getPostParameter('disconnected_at')); |
|
156
|
|
|
$bytesTransferred = InputValidation::bytesTransferred($request->getPostParameter('bytes_transferred')); |
|
157
|
|
|
|
|
158
|
|
View Code Duplication |
if (false === $this->storage->clientDisconnect($profileId, $commonName, $ip4, $ip6, $connectedAt, $disconnectedAt, $bytesTransferred)) { |
|
|
|
|
|
|
159
|
|
|
return new ApiResponse('disconnect', ['ok' => false, 'error' => 'unable to write disconnect event to log']); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return new ApiResponse('disconnect', ['ok' => true]); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function verifyOtp(Request $request) |
|
|
|
|
|
|
166
|
|
|
{ |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.