|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 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 GuzzleHttp\Client; |
|
21
|
|
|
use RuntimeException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Fetch and store the CRL used by OpenVPN. |
|
25
|
|
|
*/ |
|
26
|
|
|
class CrlFetcher |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
private $crlUrl; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
private $crlPath; |
|
33
|
|
|
|
|
34
|
|
|
/** @var \GuzzleHttp\Client */ |
|
35
|
|
|
private $client; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $crlUrl the URL location to fetch from |
|
39
|
|
|
* @param string $crlPath the folder to write to |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($crlUrl, $crlPath, Client $client = null) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->crlUrl = $crlUrl; |
|
44
|
|
|
$this->crlPath = $crlPath; |
|
45
|
|
|
if (null === $client) { |
|
46
|
|
|
$client = new Client(); |
|
47
|
|
|
} |
|
48
|
|
|
$this->client = $client; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Fetch and store the CRL. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function fetch() |
|
55
|
|
|
{ |
|
56
|
|
|
$crlFile = sprintf('%s/ca.crl', $this->crlPath); |
|
57
|
|
|
$tmpFile = sprintf('%s.tmp', $crlFile); |
|
58
|
|
|
|
|
59
|
|
|
$response = $this->client->get($this->crlUrl); |
|
60
|
|
|
$crlData = $response->getBody(); |
|
61
|
|
|
|
|
62
|
|
|
if (!is_dir($this->crlPath)) { |
|
63
|
|
|
@mkdir($this->crlPath); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (false === @file_put_contents($tmpFile, $crlData)) { |
|
67
|
|
|
// unable to write tmp file |
|
68
|
|
|
throw new RuntimeException('unable to write CRL'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (false === @rename($tmpFile, $crlFile)) { |
|
72
|
|
|
// unable to rename tmp file to crl file |
|
73
|
|
|
if (false === @unlink($tmpFile)) { |
|
74
|
|
|
throw new RuntimeException('unable to delete temporary CRL'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
throw new RuntimeException('unable to rename temporary CRL to active CRL'); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: