|
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; |
|
19
|
|
|
|
|
20
|
|
|
use RuntimeException; |
|
21
|
|
|
|
|
22
|
|
|
class Utils |
|
23
|
|
|
{ |
|
24
|
|
|
public static function exec($cmd, $throwExceptionOnFailure = true) |
|
25
|
|
|
{ |
|
26
|
|
|
exec($cmd, $output, $returnValue); |
|
27
|
|
|
|
|
28
|
|
|
if (0 !== $returnValue) { |
|
29
|
|
|
if ($throwExceptionOnFailure) { |
|
30
|
|
|
throw new RuntimeException( |
|
31
|
|
|
sprintf('command "%s" did not complete successfully (%d)', $cmd, $returnValue) |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function writeTempConfig($tmpConfig, array $configFileData) |
|
38
|
|
|
{ |
|
39
|
|
|
if (false === @file_put_contents($tmpConfig, implode(PHP_EOL, $configFileData))) { |
|
40
|
|
|
throw new RuntimeException('unable to write temporary config file'); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $configData the current OpenVPN configuration file with |
|
46
|
|
|
* keys and certificates |
|
47
|
|
|
* |
|
48
|
|
|
* @return array the extracted keys and certificates |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function extractCertificates($configData) |
|
51
|
|
|
{ |
|
52
|
|
|
$serverConfig = []; |
|
53
|
|
|
|
|
54
|
|
|
foreach (array('cert', 'ca', 'key', 'tls-auth', 'dh') as $inlineType) { |
|
55
|
|
|
$pattern = sprintf('/\<%s\>(.*)\<\/%s\>/msU', $inlineType, $inlineType); |
|
56
|
|
|
if (1 !== preg_match($pattern, $configData, $matches)) { |
|
57
|
|
|
throw new DomainException('inline type not found'); |
|
58
|
|
|
} |
|
59
|
|
|
$serverConfig[$inlineType] = trim($matches[1]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$parsedCert = openssl_x509_parse($serverConfig['cert']); |
|
63
|
|
|
$serverConfig['valid_from'] = $parsedCert['validFrom_time_t']; |
|
64
|
|
|
$serverConfig['valid_to'] = $parsedCert['validTo_time_t']; |
|
65
|
|
|
$serverConfig['cn'] = $parsedCert['subject']['CN']; |
|
66
|
|
|
|
|
67
|
|
|
return $serverConfig; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|