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
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue