|
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\OpenVpn; |
|
19
|
|
|
|
|
20
|
|
|
use fkooman\VPN\Server\OpenVpn\Exception\ManagementSocketException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Abstraction to use the OpenVPN management interface using a socket |
|
24
|
|
|
* connection. |
|
25
|
|
|
*/ |
|
26
|
|
|
class ManagementSocket implements ManagementSocketInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var resource */ |
|
29
|
|
|
private $socket; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->socket = null; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Connect to an OpenVPN management socket. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $socketAddress the socket to connect to, e.g.: |
|
40
|
|
|
* "tcp://localhost:7505" |
|
41
|
|
|
*/ |
|
42
|
|
|
public function open($socketAddress, $timeOut = 5) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->socket = @stream_socket_client($socketAddress, $errno, $errstr, $timeOut); |
|
45
|
|
|
if (false === $this->socket) { |
|
46
|
|
|
throw new ManagementSocketException( |
|
47
|
|
|
sprintf('%s (%s)', $errstr, $errno) |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
// turn off logging as the output may interfere with our management |
|
51
|
|
|
// session, we do not care about the output |
|
52
|
|
|
$this->command('log off'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function command($command) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->requireOpenSocket(); |
|
58
|
|
|
$this->write( |
|
59
|
|
|
sprintf("%s\n", $command) |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
return $this->read(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function close() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->requireOpenSocket(); |
|
68
|
|
|
if (false === @fclose($this->socket)) { |
|
69
|
|
|
throw new ManagementSocketException('unable to close the socket'); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function write($data) |
|
74
|
|
|
{ |
|
75
|
|
|
if (false === @fwrite($this->socket, $data)) { |
|
|
|
|
|
|
76
|
|
|
throw new ManagementSocketException('unable to write to socket'); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function read() |
|
81
|
|
|
{ |
|
82
|
|
|
$dataBuffer = array(); |
|
83
|
|
|
while (!feof($this->socket) && !$this->isEndOfResponse(end($dataBuffer))) { |
|
84
|
|
|
if (false === $readData = @fgets($this->socket, 4096)) { |
|
85
|
|
|
throw new ManagementSocketException('unable to read from socket'); |
|
86
|
|
|
} |
|
87
|
|
|
$dataBuffer[] = trim($readData); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $dataBuffer; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function isEndOfResponse($lastLine) |
|
94
|
|
|
{ |
|
95
|
|
|
$endMarkers = array('END', 'SUCCESS: ', 'ERROR: '); |
|
96
|
|
|
foreach ($endMarkers as $endMarker) { |
|
97
|
|
|
if (0 === strpos($lastLine, $endMarker)) { |
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
private function requireOpenSocket() |
|
106
|
|
|
{ |
|
107
|
|
|
if (is_null($this->socket)) { |
|
108
|
|
|
throw new ManagementSocketException('socket not open'); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
$datacan contain request data and is used in file manipulation 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: