|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace SURFnet\VPN\Server; |
|
20
|
|
|
|
|
21
|
|
|
use SURFnet\VPN\Common\FileIO; |
|
22
|
|
|
use RuntimeException; |
|
23
|
|
|
|
|
24
|
|
|
class TlsAuth |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
private $dataDir; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct($dataDir) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->dataDir = $dataDir; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function init() |
|
35
|
|
|
{ |
|
36
|
|
|
$taFile = sprintf('%s/ta.key', $this->dataDir); |
|
37
|
|
|
|
|
38
|
|
|
// generate the TA file if it does not exist |
|
39
|
|
|
if (!@file_exists($taFile)) { |
|
40
|
|
|
$this->execOpenVpn(['--genkey', '--secret', $taFile]); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function get() |
|
45
|
|
|
{ |
|
46
|
|
|
$taFile = sprintf('%s/ta.key', $this->dataDir); |
|
47
|
|
|
|
|
48
|
|
|
return FileIO::readFile($taFile); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
View Code Duplication |
private function execOpenVpn(array $argv) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
$command = sprintf( |
|
54
|
|
|
'/usr/sbin/openvpn %s >/dev/null 2>/dev/null', |
|
55
|
|
|
implode(' ', $argv) |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
exec( |
|
59
|
|
|
$command, |
|
60
|
|
|
$commandOutput, |
|
61
|
|
|
$returnValue |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
if (0 !== $returnValue) { |
|
65
|
|
|
throw new RuntimeException( |
|
66
|
|
|
sprintf('command "%s" did not complete successfully: "%s"', $command, $commandOutput) |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
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.