Throw the proper exceptions when the connection can't be established

This commit is contained in:
Robin Appelman 2013-03-14 23:23:46 +01:00
commit 2ed803907f
4 changed files with 55 additions and 4 deletions

View file

@ -23,7 +23,7 @@ class ListShares {
} }
public function run() { 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'; $command = \SMB\Server::CLIENT . ' -N -U ' . $auth . ' ' . '-gL ' . escapeshellarg($this->server->getHost()) . ' 2> /dev/null';
exec($command, $output); exec($command, $output);
return $this->parseOutput($output); return $this->parseOutput($output);

View file

@ -36,11 +36,25 @@ class Connection {
'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!! 'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!!
'LC_ALL' => Server::LOCALE 'LC_ALL' => Server::LOCALE
)); ));
if (!is_resource($this->process)) { if (!$this->isValid()) {
throw new ConnectionError(); 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 * send input to smbclient
* *
@ -55,10 +69,16 @@ class Connection {
/** /**
* get all unprocessed output from smbclient * get all unprocessed output from smbclient
* *
* @throws ConnectionError
* @return array * @return array
*/ */
public function read() { 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(); $output = array();
$line = fgets($this->pipes[1]); $line = fgets($this->pipes[1]);
$length = strlen(self::DELIMITER); $length = strlen(self::DELIMITER);
@ -69,6 +89,27 @@ class Connection {
return $output; 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() { public function __destruct() {
proc_close($this->process); proc_close($this->process);
} }

View file

@ -20,5 +20,11 @@ class NotEmptyException extends \Exception {
class ConnectionError extends \Exception { class ConnectionError extends \Exception {
} }
class AuthenticationException extends \Exception {
}
class InvalidHostException extends \Exception {
}
class AccessDeniedException extends \Exception { class AccessDeniedException extends \Exception {
} }

View file

@ -26,7 +26,8 @@ class Share {
/** /**
* @param Server $server * @param Server $server
* @param string $share * @param string $name
* @throws ConnectionError
*/ */
public function __construct($server, $name) { public function __construct($server, $name) {
$this->server = $server; $this->server = $server;
@ -36,6 +37,9 @@ class Share {
' //' . $this->server->getHost() . '/' . $this->name; ' //' . $this->server->getHost() . '/' . $this->name;
$this->connection = new Connection($command); $this->connection = new Connection($command);
$this->connection->write($this->server->getPassword()); $this->connection->write($this->server->getPassword());
if (!$this->connection->isValid()) {
throw new ConnectionError();
}
} }
public function connect() { public function connect() {