|
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
|
|
|
|
|
18
|
|
|
namespace fkooman\VPN\Server\Config; |
|
19
|
|
|
|
|
20
|
|
|
use RuntimeException; |
|
21
|
|
|
use fkooman\Json\Json; |
|
22
|
|
|
|
|
23
|
|
|
class FileConfigStorage implements ConfigStorageInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
private $configDir; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct($configDir) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->configDir = $configDir; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get configuration specific to a particular user. |
|
35
|
|
|
* |
|
36
|
|
|
* @return UserConfig |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getUserConfig($userId) |
|
39
|
|
|
{ |
|
40
|
|
|
$userData = $this->readFile( |
|
41
|
|
|
sprintf('%s/users/%s', $this->configDir, $userId) |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
return new UserConfig($userData); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Set the configuration for a particular user. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function setUserConfig($userId, UserConfig $userConfig) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->writeFile( |
|
53
|
|
|
sprintf('%s/users/%s', $this->configDir, $userId), |
|
54
|
|
|
$userConfig->toArray() |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the configuration for a particular common name. |
|
60
|
|
|
* |
|
61
|
|
|
* @return CommonNameConfig |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getCommonNameConfig($commonName) |
|
64
|
|
|
{ |
|
65
|
|
|
$commonNameData = $this->readFile( |
|
66
|
|
|
sprintf('%s/common_names/%s', $this->configDir, $commonName) |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
return new CommonNameConfig($commonNameData); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getAllCommonNameConfig($userId) |
|
73
|
|
|
{ |
|
74
|
|
|
$configArray = []; |
|
75
|
|
|
$pathFilter = sprintf('%s/common_names/*', $this->configDir); |
|
76
|
|
|
if (!is_null($userId)) { |
|
77
|
|
|
$pathFilter = sprintf('%s/common_names/%s_*', $this->configDir, $userId); |
|
78
|
|
|
} |
|
79
|
|
|
foreach (glob($pathFilter) as $commonNamePath) { |
|
|
|
|
|
|
80
|
|
|
$commonName = basename($commonNamePath); |
|
81
|
|
|
$configArray[$commonName] = $this->getCommonNameConfig($commonName)->toArray(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $configArray; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set the configuration for a particular common name. |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setCommonNameConfig($commonName, CommonNameConfig $commonNameConfig) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->writeFile( |
|
93
|
|
|
sprintf('%s/common_names/%s', $this->configDir, $commonName), |
|
94
|
|
|
$commonNameConfig->toArray() |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function readFile($fileName) |
|
99
|
|
|
{ |
|
100
|
|
|
if (false === $fileContent = @file_get_contents($fileName)) { |
|
101
|
|
|
return []; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return Json::decode($fileContent); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private function writeFile($fileName, array $fileContent) |
|
108
|
|
|
{ |
|
109
|
|
|
if (false === @file_put_contents($fileName, Json::encode($fileContent))) { |
|
110
|
|
|
throw new RuntimeException(sprintf('unable to write file "%s"', $fileName)); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
$pathFiltercan 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: