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; namespace Icewind\SMB;
use Icewind\SMB\Exception\ConnectException;
use Icewind\SMB\Exception\ConnectionException; use Icewind\SMB\Exception\ConnectionException;
class RawConnection { class RawConnection {
@ -25,6 +26,9 @@ class RawConnection {
* *
* $pipes[0] holds STDIN for smbclient * $pipes[0] holds STDIN for smbclient
* $pipes[1] holds STDOUT 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; private $pipes;
@ -33,32 +37,44 @@ class RawConnection {
*/ */
private $process; 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->command = $command;
$this->env = $env; $this->env = $env;
$this->connect();
} }
private function connect() { public function connect() {
$descriptorSpec = array( if (is_null($this->getAuthStream())) {
0 => array('pipe', 'r'), // child reads from stdin throw new ConnectException('Authentication not set before connecting');
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 $descriptorSpec = [
4 => array('pipe', 'r'), // child reads from fd#4 0 => ['pipe', 'r'], // child reads from stdin
5 => array('pipe', 'w') // child writes to fd#5 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); 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!! 'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!!
'LC_ALL' => Server::LOCALE, 'LC_ALL' => Server::LOCALE,
'LANG' => Server::LOCALE, 'LANG' => Server::LOCALE,
'COLUMNS' => 8192 // prevent smbclient from line-wrapping it's output 'COLUMNS' => 8192 // prevent smbclient from line-wrapping it's output
)); ]);
$this->process = proc_open($this->command, $descriptorSpec, $this->pipes, '/', $env); $this->process = proc_open($this->command, $descriptorSpec, $this->pipes, '/', $env);
if (!$this->isValid()) { if (!$this->isValid()) {
throw new ConnectionException(); throw new ConnectionException();
} }
$this->connected = true;
} }
/** /**
@ -129,7 +145,7 @@ class RawConnection {
} }
public function getAuthStream() { public function getAuthStream() {
return $this->pipes[3]; return $this->authStream;
} }
public function getFileInputStream() { public function getFileInputStream() {
@ -143,14 +159,10 @@ class RawConnection {
public function writeAuthentication($user, $password) { public function writeAuthentication($user, $password) {
$auth = ($password === false) $auth = ($password === false)
? "username=$user" ? "username=$user"
: "username=$user\npassword=$password"; : "username=$user\npassword=$password\n";
if (fwrite($this->getAuthStream(), $auth) === false) { $this->authStream = fopen('php://temp', 'w+');
fclose($this->getAuthStream()); fwrite($this->getAuthStream(), $auth);
return false;
}
fclose($this->getAuthStream());
return true;
} }
public function close($terminate = true) { public function close($terminate = true) {
@ -163,8 +175,8 @@ class RawConnection {
$status = proc_get_status($this->process); $status = proc_get_status($this->process);
$ppid = $status['pid']; $ppid = $status['pid'];
$pids = preg_split('/\s+/', `ps -o pid --no-heading --ppid $ppid`); $pids = preg_split('/\s+/', `ps -o pid --no-heading --ppid $ppid`);
foreach($pids as $pid) { foreach ($pids as $pid) {
if(is_numeric($pid)) { if (is_numeric($pid)) {
//9 is the SIGKILL signal //9 is the SIGKILL signal
posix_kill($pid, 9); posix_kill($pid, 9);
} }

View file

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

View file

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