make authfile handling compatible with smbclient 4.6.x

This commit is contained in:
Robin Appelman 2017-08-17 16:00:22 +02:00
commit 6723c97de1
3 changed files with 44 additions and 31 deletions

View file

@ -7,6 +7,7 @@
namespace Icewind\SMB;
use Icewind\SMB\Exception\ConnectException;
use Icewind\SMB\Exception\ConnectionException;
class RawConnection {
@ -25,6 +26,9 @@ class RawConnection {
*
* $pipes[0] holds STDIN for smbclient
* $pipes[1] holds STDOUT for smbclient
* $pipes[3] holds the authfile for smbclient
* $pipes[4] holds the stream for writing files
* $pipes[5] holds the stream for reading files
*/
private $pipes;
@ -33,32 +37,44 @@ class RawConnection {
*/
private $process;
public function __construct($command, $env = array()) {
/**
* @var resource|null $authStream
*/
private $authStream = null;
private $connected = false;
public function __construct($command, array $env = []) {
$this->command = $command;
$this->env = $env;
$this->connect();
}
private function connect() {
$descriptorSpec = array(
0 => array('pipe', 'r'), // child reads from stdin
1 => array('pipe', 'w'), // child writes to stdout
2 => array('pipe', 'w'), // child writes to stderr
3 => array('pipe', 'r'), // child reads from fd#3
4 => array('pipe', 'r'), // child reads from fd#4
5 => array('pipe', 'w') // child writes to fd#5
);
public function connect() {
if (is_null($this->getAuthStream())) {
throw new ConnectException('Authentication not set before connecting');
}
$descriptorSpec = [
0 => ['pipe', 'r'], // child reads from stdin
1 => ['pipe', 'w'], // child writes to stdout
2 => ['pipe', 'w'], // child writes to stderr
3 => $this->getAuthStream(), // child reads from fd#3
4 => ['pipe', 'r'], // child reads from fd#4
5 => ['pipe', 'w'] // child writes to fd#5
];
setlocale(LC_ALL, Server::LOCALE);
$env = array_merge($this->env, array(
$env = array_merge($this->env, [
'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!!
'LC_ALL' => Server::LOCALE,
'LANG' => Server::LOCALE,
'COLUMNS' => 8192 // prevent smbclient from line-wrapping it's output
));
]);
$this->process = proc_open($this->command, $descriptorSpec, $this->pipes, '/', $env);
if (!$this->isValid()) {
throw new ConnectionException();
}
$this->connected = true;
}
/**
@ -129,7 +145,7 @@ class RawConnection {
}
public function getAuthStream() {
return $this->pipes[3];
return $this->authStream;
}
public function getFileInputStream() {
@ -143,14 +159,10 @@ class RawConnection {
public function writeAuthentication($user, $password) {
$auth = ($password === false)
? "username=$user"
: "username=$user\npassword=$password";
: "username=$user\npassword=$password\n";
if (fwrite($this->getAuthStream(), $auth) === false) {
fclose($this->getAuthStream());
return false;
}
fclose($this->getAuthStream());
return true;
$this->authStream = fopen('php://temp', 'w+');
fwrite($this->getAuthStream(), $auth);
}
public function close($terminate = true) {

View file

@ -134,6 +134,7 @@ class Server {
);
$connection = new RawConnection($command);
$connection->writeAuthentication($this->getUser(), $this->getPassword());
$connection->connect();
$output = $connection->readAll();
$parser = new Parser($this->timezoneProvider);

View file

@ -68,8 +68,9 @@ class Share extends AbstractShare {
);
$connection = new Connection($command, $this->parser);
$connection->writeAuthentication($this->server->getUser(), $this->server->getPassword());
$connection->connect();
if (!$connection->isValid()) {
throw new ConnectionException();
throw new ConnectionException($connection->readLine());
}
return $connection;
}
@ -88,7 +89,6 @@ class Share extends AbstractShare {
protected function reconnect() {
$this->connection->reconnect();
$this->connection->writeAuthentication($this->server->getUser(), $this->server->getPassword());
if (!$this->connection->isValid()) {
throw new ConnectionException();
}