mirror of
https://codeberg.org/icewind/SMB.git
synced 2026-06-03 17:24:07 +02:00
Throw the proper exceptions when the connection can't be established
This commit is contained in:
parent
fc12434d28
commit
2ed803907f
4 changed files with 55 additions and 4 deletions
|
|
@ -23,7 +23,7 @@ class ListShares {
|
|||
}
|
||||
|
||||
public function run() {
|
||||
$auth = escapeshellarg($this->server->getAuthString());
|
||||
$auth = escapeshellarg($this->server->getAuthString()); //TODO: don't pass password as shell argument
|
||||
$command = \SMB\Server::CLIENT . ' -N -U ' . $auth . ' ' . '-gL ' . escapeshellarg($this->server->getHost()) . ' 2> /dev/null';
|
||||
exec($command, $output);
|
||||
return $this->parseOutput($output);
|
||||
|
|
|
|||
|
|
@ -36,11 +36,25 @@ class Connection {
|
|||
'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!!
|
||||
'LC_ALL' => Server::LOCALE
|
||||
));
|
||||
if (!is_resource($this->process)) {
|
||||
if (!$this->isValid()) {
|
||||
throw new ConnectionError();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* check if the connection is still active
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid() {
|
||||
if (is_resource($this->process)) {
|
||||
$status = proc_get_status($this->process);
|
||||
return $status['running'];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* send input to smbclient
|
||||
*
|
||||
|
|
@ -55,10 +69,16 @@ class Connection {
|
|||
/**
|
||||
* get all unprocessed output from smbclient
|
||||
*
|
||||
* @throws ConnectionError
|
||||
* @return array
|
||||
*/
|
||||
public function read() {
|
||||
fgets($this->pipes[1]); //first line is prompt
|
||||
if (!$this->isValid()) {
|
||||
throw new ConnectionError();
|
||||
}
|
||||
$line = trim(fgets($this->pipes[1])); //first line is prompt
|
||||
$this->checkConnectionError($line);
|
||||
|
||||
$output = array();
|
||||
$line = fgets($this->pipes[1]);
|
||||
$length = strlen(self::DELIMITER);
|
||||
|
|
@ -69,6 +89,27 @@ class Connection {
|
|||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if the first line holds a connection failure
|
||||
*
|
||||
* @param $line
|
||||
* @throws AuthenticationException
|
||||
* @throws InvalidHostException
|
||||
*/
|
||||
private function checkConnectionError($line) {
|
||||
$line = rtrim($line, ')');
|
||||
$authError = 'NT_STATUS_LOGON_FAILURE';
|
||||
if (substr($line, -23) === $authError) {
|
||||
$this->pipes = array(null, null);
|
||||
throw new AuthenticationException();
|
||||
}
|
||||
$addressError = 'NT_STATUS_BAD_NETWORK_NAME';
|
||||
if (substr($line, -26) === $addressError) {
|
||||
$this->pipes = array(null, null);
|
||||
throw new InvalidHostException();
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
proc_close($this->process);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,5 +20,11 @@ class NotEmptyException extends \Exception {
|
|||
class ConnectionError extends \Exception {
|
||||
}
|
||||
|
||||
class AuthenticationException extends \Exception {
|
||||
}
|
||||
|
||||
class InvalidHostException extends \Exception {
|
||||
}
|
||||
|
||||
class AccessDeniedException extends \Exception {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,8 @@ class Share {
|
|||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param string $share
|
||||
* @param string $name
|
||||
* @throws ConnectionError
|
||||
*/
|
||||
public function __construct($server, $name) {
|
||||
$this->server = $server;
|
||||
|
|
@ -36,6 +37,9 @@ class Share {
|
|||
' //' . $this->server->getHost() . '/' . $this->name;
|
||||
$this->connection = new Connection($command);
|
||||
$this->connection->write($this->server->getPassword());
|
||||
if (!$this->connection->isValid()) {
|
||||
throw new ConnectionError();
|
||||
}
|
||||
}
|
||||
|
||||
public function connect() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue