|
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 RuntimeException; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Manage the Client Configuration Directory (CCD) used by the OpenVPN |
|
23
|
|
|
* instances running on this machine. It can be used to set client specific |
|
24
|
|
|
* configurations based on the CN. |
|
25
|
|
|
*/ |
|
26
|
|
|
class CcdHandler |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
private $ccdPath; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct($ccdPath) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->ccdPath = $ccdPath; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
private function parseCcd($commonName) |
|
37
|
|
|
{ |
|
38
|
|
|
$commonNamePath = sprintf('%s/%s', $this->ccdPath, $commonName); |
|
39
|
|
|
$clientConfig = array(); |
|
40
|
|
|
|
|
41
|
|
|
$handle = @fopen($commonNamePath, 'r'); |
|
42
|
|
|
if ($handle) { |
|
43
|
|
|
while (false !== $line = fgets($handle, 128)) { |
|
44
|
|
|
$clientConfig[] = trim($line); |
|
45
|
|
|
} |
|
46
|
|
|
fclose($handle); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $clientConfig; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
View Code Duplication |
public function disableCommonName($commonName) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
Utils::validateCommonName($commonName); |
|
55
|
|
|
|
|
56
|
|
|
$clientConfig = $this->parseCcd($commonName); |
|
57
|
|
|
foreach ($clientConfig as $k => $v) { |
|
58
|
|
|
if ('disable' === $v) { |
|
59
|
|
|
// already disabled |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// not yet disabled |
|
65
|
|
|
$clientConfig[] = 'disable'; |
|
66
|
|
|
|
|
67
|
|
|
$this->writeFile($commonName, $clientConfig); |
|
68
|
|
|
|
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
View Code Duplication |
public function enableCommonName($commonName) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
Utils::validateCommonName($commonName); |
|
75
|
|
|
|
|
76
|
|
|
$clientConfig = $this->parseCcd($commonName); |
|
77
|
|
|
foreach ($clientConfig as $k => $v) { |
|
78
|
|
|
if ('disable' === $v) { |
|
79
|
|
|
// it is disabled |
|
80
|
|
|
unset($clientConfig[$k]); |
|
81
|
|
|
$this->writeFile($commonName, $clientConfig); |
|
82
|
|
|
|
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// not disabled |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function isDisabled($commonName) |
|
92
|
|
|
{ |
|
93
|
|
|
Utils::validateCommonName($commonName); |
|
94
|
|
|
|
|
95
|
|
|
$clientConfig = $this->parseCcd($commonName); |
|
96
|
|
|
foreach ($clientConfig as $k => $v) { |
|
97
|
|
|
if ('disable' === $v) { |
|
98
|
|
|
// disabled |
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// not disabled |
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getDisabledCommonNames($userId = null) |
|
108
|
|
|
{ |
|
109
|
|
|
if (!is_null($userId)) { |
|
110
|
|
|
Utils::validateUserId($userId); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$disabledCommonNames = array(); |
|
114
|
|
|
$pathFilter = sprintf('%s/*', $this->ccdPath); |
|
115
|
|
|
if (!is_null($userId)) { |
|
116
|
|
|
$pathFilter = sprintf('%s/%s_*', $this->ccdPath, $userId); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
foreach (glob($pathFilter) as $commonNamePath) { |
|
|
|
|
|
|
120
|
|
|
$commonName = basename($commonNamePath); |
|
121
|
|
|
|
|
122
|
|
|
if ($this->isDisabled($commonName)) { |
|
123
|
|
|
$disabledCommonNames[] = $commonName; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $disabledCommonNames; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getStaticIpAddress($commonName) |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function setStaticIpAddress($commonName, $v4, $v6) |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
|
|
if (!is_null($v4)) { |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if (!is_null($v6)) { |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
private function writeFile($commonName, array $clientConfig) |
|
144
|
|
|
{ |
|
145
|
|
|
$commonNamePath = sprintf('%s/%s', $this->ccdPath, $commonName); |
|
146
|
|
|
|
|
147
|
|
|
if (false === @file_put_contents($commonNamePath, implode(PHP_EOL, $clientConfig).PHP_EOL)) { |
|
|
|
|
|
|
148
|
|
|
throw new RuntimeException('unable to write to CCD file'); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
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.