|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2015 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
namespace fkooman\VPN\Server; |
|
18
|
|
|
|
|
19
|
|
|
use fkooman\Json\Json; |
|
20
|
|
|
use RuntimeException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Manage static configuration for configurations. |
|
24
|
|
|
* Features: |
|
25
|
|
|
* - disable a configuration based on CN |
|
26
|
|
|
* - set static IPv4/IPv6 address for a CN. |
|
27
|
|
|
*/ |
|
28
|
|
|
class StaticConfig |
|
29
|
|
|
{ |
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $staticConfigDir; |
|
32
|
|
|
|
|
33
|
|
|
/** @var IP */ |
|
34
|
|
|
private $ipRange; |
|
35
|
|
|
|
|
36
|
|
|
/** @var IP */ |
|
37
|
|
|
private $poolRange; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct($staticConfigDir, IP $ipRange, IP $poolRange) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->staticConfigDir = $staticConfigDir; |
|
42
|
|
|
$this->ipRange = $ipRange; |
|
43
|
|
|
$this->poolRange = $poolRange; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function parseConfig($commonName) |
|
47
|
|
|
{ |
|
48
|
|
|
$commonNamePath = sprintf('%s/%s', $this->staticConfigDir, $commonName); |
|
49
|
|
|
// XXX do something if file does not exist |
|
50
|
|
|
try { |
|
51
|
|
|
return Json::decodeFile($commonNamePath); |
|
|
|
|
|
|
52
|
|
|
} catch (RuntimeException $e) { |
|
53
|
|
|
return []; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
View Code Duplication |
public function disableCommonName($commonName) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
Utils::validateCommonName($commonName); |
|
60
|
|
|
|
|
61
|
|
|
$clientConfig = $this->parseConfig($commonName); |
|
62
|
|
|
if (array_key_exists('disable', $clientConfig) && $clientConfig['disable']) { |
|
63
|
|
|
// already disabled |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$clientConfig['disable'] = true; |
|
68
|
|
|
$this->writeFile($commonName, $clientConfig); |
|
69
|
|
|
|
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
View Code Duplication |
public function enableCommonName($commonName) |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
Utils::validateCommonName($commonName); |
|
76
|
|
|
|
|
77
|
|
|
$clientConfig = $this->parseConfig($commonName); |
|
78
|
|
|
if (array_key_exists('disable', $clientConfig) && $clientConfig['disable']) { |
|
79
|
|
|
// it is disabled, enable it |
|
80
|
|
|
$clientConfig['disable'] = false; |
|
81
|
|
|
$this->writeFile($commonName, $clientConfig); |
|
82
|
|
|
|
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// it is not disabled |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function isDisabled($commonName) |
|
91
|
|
|
{ |
|
92
|
|
|
Utils::validateCommonName($commonName); |
|
93
|
|
|
|
|
94
|
|
|
$clientConfig = $this->parseConfig($commonName); |
|
95
|
|
|
|
|
96
|
|
|
return array_key_exists('disable', $clientConfig) && $clientConfig['disable']; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getDisabledCommonNames($userId = null) |
|
100
|
|
|
{ |
|
101
|
|
|
if (!is_null($userId)) { |
|
102
|
|
|
Utils::validateUserId($userId); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$disabledCommonNames = array(); |
|
106
|
|
|
$pathFilter = sprintf('%s/*', $this->staticConfigDir); |
|
107
|
|
|
if (!is_null($userId)) { |
|
108
|
|
|
$pathFilter = sprintf('%s/%s_*', $this->staticConfigDir, $userId); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
foreach (glob($pathFilter) as $commonNamePath) { |
|
|
|
|
|
|
112
|
|
|
$commonName = basename($commonNamePath); |
|
113
|
|
|
|
|
114
|
|
|
if ($this->isDisabled($commonName)) { |
|
115
|
|
|
$disabledCommonNames[] = $commonName; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $disabledCommonNames; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getStaticAddresses($userId = null) |
|
123
|
|
|
{ |
|
124
|
|
|
if (!is_null($userId)) { |
|
125
|
|
|
Utils::validateUserId($userId); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$staticAddresses = array(); |
|
129
|
|
|
$pathFilter = sprintf('%s/*', $this->staticConfigDir); |
|
130
|
|
|
if (!is_null($userId)) { |
|
131
|
|
|
$pathFilter = sprintf('%s/%s_*', $this->staticConfigDir, $userId); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
foreach (glob($pathFilter) as $commonNamePath) { |
|
|
|
|
|
|
135
|
|
|
$commonName = basename($commonNamePath); |
|
136
|
|
|
$ip = $this->getStaticAddress($commonName); |
|
137
|
|
|
if (!is_null($ip['v4'])) { |
|
138
|
|
|
$staticAddresses[$commonName] = $ip; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $staticAddresses; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function getStaticAddress($commonName) |
|
146
|
|
|
{ |
|
147
|
|
|
Utils::validateCommonName($commonName); |
|
148
|
|
|
|
|
149
|
|
|
$clientConfig = $this->parseConfig($commonName); |
|
150
|
|
|
|
|
151
|
|
|
$v4 = null; |
|
152
|
|
|
if (array_key_exists('v4', $clientConfig)) { |
|
153
|
|
|
$v4 = $clientConfig['v4']; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return array( |
|
157
|
|
|
'v4' => $v4, |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function setStaticAddresses($commonName, $v4) |
|
162
|
|
|
{ |
|
163
|
|
|
Utils::validateCommonName($commonName); |
|
164
|
|
|
if (!is_null($v4)) { |
|
165
|
|
|
Utils::validateAddress($v4); |
|
166
|
|
|
|
|
167
|
|
|
// IP MUST be in ipRange |
|
168
|
|
|
if (!$this->ipRange->inRange($v4)) { |
|
169
|
|
|
throw new RuntimeException('IP is out of range'); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
// IP MUST NOT be in poolRange |
|
173
|
|
|
if ($this->poolRange->inRange($v4)) { |
|
174
|
|
|
throw new RuntimeException('IP cannot be in poolRange'); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
// cannot be server IP (we assume for now firstHost is server IP |
|
178
|
|
|
if ($v4 === $this->ipRange->getFirstHost()) { |
|
179
|
|
|
throw new RuntimeException('IP cannot be server IP'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
// cannot be network address of poolRange |
|
183
|
|
|
if ($v4 === $this->poolRange->getNetwork()) { |
|
184
|
|
|
throw new RuntimeException('IP cannot be network address of poolRange'); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
// XXX make sure it is not already in use by another config, it is |
|
188
|
|
|
// okay if it is this config! |
|
189
|
|
|
} |
|
190
|
|
|
$clientConfig = $this->parseConfig($commonName); |
|
191
|
|
|
$clientConfig['v4'] = $v4; |
|
192
|
|
|
$this->writeFile($commonName, $clientConfig); |
|
193
|
|
|
|
|
194
|
|
|
return true; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
private function writeFile($commonName, array $clientConfig) |
|
198
|
|
|
{ |
|
199
|
|
|
$commonNamePath = sprintf('%s/%s', $this->staticConfigDir, $commonName); |
|
200
|
|
|
|
|
201
|
|
|
if (false === @file_put_contents($commonNamePath, Json::encode($clientConfig))) { |
|
|
|
|
|
|
202
|
|
|
throw new RuntimeException('unable to write to static configuration file'); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
$commonNamePathcan contain request data and is used in file inclusion context(s) leading to a potential security vulnerability.General Strategies to prevent injection
In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:
if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) { throw new \InvalidArgumentException('This input is not allowed.'); }For numeric data, we recommend to explicitly cast the data: