update icewind/smb to 3.6.0

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2023-08-10 17:40:03 +02:00
parent 970ac3d7eb
commit 91bcb90c7e
No known key found for this signature in database
GPG key ID: 42B69D8A64526EFB
13 changed files with 296 additions and 129 deletions

View file

@ -8,7 +8,7 @@
"classmap-authoritative": true
},
"require": {
"icewind/smb": "3.5.4",
"icewind/smb": "3.6.0",
"icewind/streams": "0.7.7"
}
}

View file

@ -4,20 +4,20 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "1419e286d2372dfbce44dd73ddbab2ff",
"content-hash": "dc5d93a9b45922aeec3823baf867acdc",
"packages": [
{
"name": "icewind/smb",
"version": "v3.5.4",
"version": "v3.6.0",
"source": {
"type": "git",
"url": "https://github.com/icewind1991/SMB.git",
"reference": "76995aa11c14e39bccd0f2370ed63b2f8f623a6d"
"reference": "e0e86b16640f5892dd00408ed50ad18357dac6c1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/icewind1991/SMB/zipball/76995aa11c14e39bccd0f2370ed63b2f8f623a6d",
"reference": "76995aa11c14e39bccd0f2370ed63b2f8f623a6d",
"url": "https://api.github.com/repos/icewind1991/SMB/zipball/e0e86b16640f5892dd00408ed50ad18357dac6c1",
"reference": "e0e86b16640f5892dd00408ed50ad18357dac6c1",
"shasum": ""
},
"require": {
@ -49,9 +49,9 @@
"description": "php wrapper for smbclient and libsmbclient-php",
"support": {
"issues": "https://github.com/icewind1991/SMB/issues",
"source": "https://github.com/icewind1991/SMB/tree/v3.5.4"
"source": "https://github.com/icewind1991/SMB/tree/v3.6.0"
},
"time": "2022-05-30T15:18:19+00:00"
"time": "2023-08-10T13:17:39+00:00"
},
{
"name": "icewind/streams",

View file

@ -32,6 +32,7 @@ return array(
'Icewind\\SMB\\Exception\\InvalidPathException' => $vendorDir . '/icewind/smb/src/Exception/InvalidPathException.php',
'Icewind\\SMB\\Exception\\InvalidRequestException' => $vendorDir . '/icewind/smb/src/Exception/InvalidRequestException.php',
'Icewind\\SMB\\Exception\\InvalidResourceException' => $vendorDir . '/icewind/smb/src/Exception/InvalidResourceException.php',
'Icewind\\SMB\\Exception\\InvalidTicket' => $vendorDir . '/icewind/smb/src/Exception/InvalidTicket.php',
'Icewind\\SMB\\Exception\\InvalidTypeException' => $vendorDir . '/icewind/smb/src/Exception/InvalidTypeException.php',
'Icewind\\SMB\\Exception\\NoLoginServerException' => $vendorDir . '/icewind/smb/src/Exception/NoLoginServerException.php',
'Icewind\\SMB\\Exception\\NoRouteToHostException' => $vendorDir . '/icewind/smb/src/Exception/NoRouteToHostException.php',
@ -50,6 +51,7 @@ return array(
'Icewind\\SMB\\ITimeZoneProvider' => $vendorDir . '/icewind/smb/src/ITimeZoneProvider.php',
'Icewind\\SMB\\KerberosApacheAuth' => $vendorDir . '/icewind/smb/src/KerberosApacheAuth.php',
'Icewind\\SMB\\KerberosAuth' => $vendorDir . '/icewind/smb/src/KerberosAuth.php',
'Icewind\\SMB\\KerberosTicket' => $vendorDir . '/icewind/smb/src/KerberosTicket.php',
'Icewind\\SMB\\Native\\NativeFileInfo' => $vendorDir . '/icewind/smb/src/Native/NativeFileInfo.php',
'Icewind\\SMB\\Native\\NativeReadStream' => $vendorDir . '/icewind/smb/src/Native/NativeReadStream.php',
'Icewind\\SMB\\Native\\NativeServer' => $vendorDir . '/icewind/smb/src/Native/NativeServer.php',

View file

@ -52,6 +52,7 @@ class ComposerStaticInit98fe9b281934250b3a93f69a5ce843b3
'Icewind\\SMB\\Exception\\InvalidPathException' => __DIR__ . '/..' . '/icewind/smb/src/Exception/InvalidPathException.php',
'Icewind\\SMB\\Exception\\InvalidRequestException' => __DIR__ . '/..' . '/icewind/smb/src/Exception/InvalidRequestException.php',
'Icewind\\SMB\\Exception\\InvalidResourceException' => __DIR__ . '/..' . '/icewind/smb/src/Exception/InvalidResourceException.php',
'Icewind\\SMB\\Exception\\InvalidTicket' => __DIR__ . '/..' . '/icewind/smb/src/Exception/InvalidTicket.php',
'Icewind\\SMB\\Exception\\InvalidTypeException' => __DIR__ . '/..' . '/icewind/smb/src/Exception/InvalidTypeException.php',
'Icewind\\SMB\\Exception\\NoLoginServerException' => __DIR__ . '/..' . '/icewind/smb/src/Exception/NoLoginServerException.php',
'Icewind\\SMB\\Exception\\NoRouteToHostException' => __DIR__ . '/..' . '/icewind/smb/src/Exception/NoRouteToHostException.php',
@ -70,6 +71,7 @@ class ComposerStaticInit98fe9b281934250b3a93f69a5ce843b3
'Icewind\\SMB\\ITimeZoneProvider' => __DIR__ . '/..' . '/icewind/smb/src/ITimeZoneProvider.php',
'Icewind\\SMB\\KerberosApacheAuth' => __DIR__ . '/..' . '/icewind/smb/src/KerberosApacheAuth.php',
'Icewind\\SMB\\KerberosAuth' => __DIR__ . '/..' . '/icewind/smb/src/KerberosAuth.php',
'Icewind\\SMB\\KerberosTicket' => __DIR__ . '/..' . '/icewind/smb/src/KerberosTicket.php',
'Icewind\\SMB\\Native\\NativeFileInfo' => __DIR__ . '/..' . '/icewind/smb/src/Native/NativeFileInfo.php',
'Icewind\\SMB\\Native\\NativeReadStream' => __DIR__ . '/..' . '/icewind/smb/src/Native/NativeReadStream.php',
'Icewind\\SMB\\Native\\NativeServer' => __DIR__ . '/..' . '/icewind/smb/src/Native/NativeServer.php',

View file

@ -2,17 +2,17 @@
"packages": [
{
"name": "icewind/smb",
"version": "v3.5.4",
"version_normalized": "3.5.4.0",
"version": "v3.6.0",
"version_normalized": "3.6.0.0",
"source": {
"type": "git",
"url": "https://github.com/icewind1991/SMB.git",
"reference": "76995aa11c14e39bccd0f2370ed63b2f8f623a6d"
"reference": "e0e86b16640f5892dd00408ed50ad18357dac6c1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/icewind1991/SMB/zipball/76995aa11c14e39bccd0f2370ed63b2f8f623a6d",
"reference": "76995aa11c14e39bccd0f2370ed63b2f8f623a6d",
"url": "https://api.github.com/repos/icewind1991/SMB/zipball/e0e86b16640f5892dd00408ed50ad18357dac6c1",
"reference": "e0e86b16640f5892dd00408ed50ad18357dac6c1",
"shasum": ""
},
"require": {
@ -25,7 +25,7 @@
"phpunit/phpunit": "^8.5|^9.3.8",
"psalm/phar": "^4.3"
},
"time": "2022-05-30T15:18:19+00:00",
"time": "2023-08-10T13:17:39+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
@ -46,7 +46,7 @@
"description": "php wrapper for smbclient and libsmbclient-php",
"support": {
"issues": "https://github.com/icewind1991/SMB/issues",
"source": "https://github.com/icewind1991/SMB/tree/v3.5.4"
"source": "https://github.com/icewind1991/SMB/tree/v3.6.0"
},
"install-path": "../icewind/smb"
},

View file

@ -3,7 +3,7 @@
'name' => 'files_external/3rdparty',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => '979f4033ca1cd0be7f255f028d4cc637a216440d',
'reference' => '733d6b54097417b3f49673ac0491aebdb46648fd',
'type' => 'library',
'install_path' => __DIR__ . '/../',
'aliases' => array(),
@ -13,16 +13,16 @@
'files_external/3rdparty' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => '979f4033ca1cd0be7f255f028d4cc637a216440d',
'reference' => '733d6b54097417b3f49673ac0491aebdb46648fd',
'type' => 'library',
'install_path' => __DIR__ . '/../',
'aliases' => array(),
'dev_requirement' => false,
),
'icewind/smb' => array(
'pretty_version' => 'v3.5.4',
'version' => '3.5.4.0',
'reference' => '76995aa11c14e39bccd0f2370ed63b2f8f623a6d',
'pretty_version' => 'v3.6.0',
'version' => '3.6.0.0',
'reference' => 'e0e86b16640f5892dd00408ed50ad18357dac6c1',
'type' => 'library',
'install_path' => __DIR__ . '/../icewind/smb',
'aliases' => array(),

View file

@ -65,19 +65,20 @@ $server = $serverFactory->createServer('localhost', $auth);
By re-using a client ticket you can create a single sign-on setup where the user authenticates against
the web service using kerberos. And the web server can forward that ticket to the smb server, allowing it
to act on the behalf of the user without requiring the user to enter his passord.
to act on the behalf of the user without requiring the user to enter his password.
The setup for such a system is fairly involved and requires roughly the following this
- The web server is authenticated against kerberos with a machine account
- Delegation is enabled for the web server's machine account
- Apache is setup to perform kerberos authentication and save the ticket in it's environment
- The web server is setup to perform kerberos authentication and save the ticket in it's environment
- Php has the krb5 extension installed
- The client authenticates using a ticket with forwarding enabled
```php
$serverFactory = new ServerFactory();
$auth = new KerberosApacheAuth();
$auth = new KerberosAuth();
$auth->setTicket(KerberosTicket::fromEnv());
$server = $serverFactory->createServer('localhost', $auth);
```

View file

@ -0,0 +1,26 @@
<?php
use Icewind\SMB\KerberosTicket;
require('vendor/autoload.php');
if (php_sapi_name() == "cli") {
echo "Note: this example shouldn't be called from cli as it expects apache to setup the keberos ticket\n";
}
// dfs paths not working
$host = 'krb.domain.test';
$share = 'netlogon';
$auth = new \Icewind\SMB\KerberosAuth();
$auth->setTicket(KerberosTicket::fromEnv());
$serverFactory = new \Icewind\SMB\ServerFactory();
$server = $serverFactory->createServer($host, $auth);
$share = $server->getShare($share);
$files = $share->dir('/');
foreach ($files as $file) {
echo $file->getName() . "\n";
}

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace Icewind\SMB\Exception;
class InvalidTicket extends Exception {
}

View file

@ -23,114 +23,40 @@ namespace Icewind\SMB;
use Icewind\SMB\Exception\DependencyException;
use Icewind\SMB\Exception\Exception;
use Icewind\SMB\Exception\InvalidTicket;
/**
* Use existing kerberos ticket to authenticate and reuse the apache ticket cache (mod_auth_kerb)
*
* @deprecated Use `KerberosAuth` with `$auth->setTicket(KerberosTicket::fromEnv())` instead
*/
class KerberosApacheAuth extends KerberosAuth implements IAuth {
/** @var string */
private $ticketPath = "";
/** @var bool */
private $init = false;
/** @var string|false */
private $ticketName;
public function __construct() {
$this->ticketName = getenv("KRB5CCNAME");
public function getTicket(): KerberosTicket {
if ($this->ticket === null) {
$ticket = KerberosTicket::fromEnv();
if ($ticket === null) {
throw new InvalidTicket("No ticket found in environment");
}
$this->ticket = $ticket;
}
return $this->ticket;
}
/**
* Copy the ticket to a temporary location and use that ticket for authentication
*
* @return void
*/
public function copyTicket(): void {
if (!$this->checkTicket()) {
return;
}
$krb5 = new \KRB5CCache();
$krb5->open($this->ticketName);
$tmpFilename = tempnam("/tmp", "krb5cc_php_");
$tmpCacheFile = "FILE:" . $tmpFilename;
$krb5->save($tmpCacheFile);
$this->ticketPath = $tmpFilename;
$this->ticketName = $tmpCacheFile;
}
/**
* Pass the ticket to smbclient by memory instead of path
*
* @return void
*/
public function passTicketFromMemory(): void {
if (!$this->checkTicket()) {
return;
}
$krb5 = new \KRB5CCache();
$krb5->open($this->ticketName);
$this->ticketName = (string)$krb5->getName();
$this->ticket = KerberosTicket::load($this->getTicket()->save());
}
/**
* Check if a valid kerberos ticket is present
*
* @return bool
* @psalm-assert-if-true string $this->ticketName
*/
public function checkTicket(): bool {
//read apache kerberos ticket cache
if (!$this->ticketName) {
return false;
}
$krb5 = new \KRB5CCache();
$krb5->open($this->ticketName);
/** @psalm-suppress MixedArgument */
return count($krb5->getEntries()) > 0;
}
private function init(): void {
if ($this->init) {
return;
}
$this->init = true;
// inspired by https://git.typo3.org/TYPO3CMS/Extensions/fal_cifs.git
if (!extension_loaded("krb5")) {
// https://pecl.php.net/package/krb5
throw new DependencyException('Ensure php-krb5 is installed.');
}
//read apache kerberos ticket cache
if (!$this->checkTicket()) {
throw new Exception('No kerberos ticket cache environment variable (KRB5CCNAME) found.');
}
// note that even if the ticketname is the value we got from `getenv("KRB5CCNAME")` we still need to set the env variable ourselves
// this is because `getenv` also reads the variables passed from the SAPI (apache-php) and we need to set the variable in the OS's env
putenv("KRB5CCNAME=" . $this->ticketName);
}
public function getExtraCommandLineArguments(): string {
$this->init();
return parent::getExtraCommandLineArguments();
}
public function setExtraSmbClientOptions($smbClientState): void {
$this->init();
try {
parent::setExtraSmbClientOptions($smbClientState);
} catch (Exception $e) {
// suppress
}
}
public function __destruct() {
if (!empty($this->ticketPath) && file_exists($this->ticketPath) && is_file($this->ticketPath)) {
unlink($this->ticketPath);
}
return $this->getTicket()->isValid();
}
}

View file

@ -27,6 +27,17 @@ use Icewind\SMB\Exception\Exception;
* Use existing kerberos ticket to authenticate
*/
class KerberosAuth implements IAuth {
/** @var ?KerberosTicket */
protected $ticket = null;
public function getTicket(): ?KerberosTicket {
return $this->ticket;
}
public function setTicket(?KerberosTicket $ticket): void {
$this->ticket = $ticket;
}
public function getUsername(): ?string {
return 'dummy';
}
@ -39,11 +50,25 @@ class KerberosAuth implements IAuth {
return null;
}
private function setEnv():void {
$ticket = $this->getTicket();
if ($ticket) {
$ticket->validate();
// note that even if the ticket name is the value we got from `getenv("KRB5CCNAME")` we still need to set the env variable ourselves
// this is because `getenv` also reads the variables passed from the SAPI (apache-php) and we need to set the variable in the OS's env
putenv("KRB5CCNAME=" . $ticket->getCacheName());
}
}
public function getExtraCommandLineArguments(): string {
$this->setEnv();
return '-k';
}
public function setExtraSmbClientOptions($smbClientState): void {
$this->setEnv();
$success = (bool)smbclient_option_set($smbClientState, SMBCLIENT_OPT_USE_KERBEROS, true);
$success = $success && smbclient_option_set($smbClientState, SMBCLIENT_OPT_FALLBACK_AFTER_KERBEROS, false);

View file

@ -0,0 +1,100 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace Icewind\SMB;
use Icewind\SMB\Exception\InvalidTicket;
use KRB5CCache;
class KerberosTicket {
/** @var KRB5CCache */
private $krb5;
/** @var string */
private $cacheName;
public function __construct(KRB5CCache $krb5, string $cacheName) {
$this->krb5 = $krb5;
$this->cacheName = $cacheName;
}
public function getCacheName(): string {
return $this->cacheName;
}
public function getName(): string{
return $this->krb5->getName();
}
public function isValid(): bool {
return count($this->krb5->getEntries()) > 0;
}
public function validate(): void {
if (!$this->isValid()) {
throw new InvalidTicket("No kerberos ticket found.");
}
}
/**
* Load the ticket from the cache specified by the KRB5CCNAME variable.
*
* @return KerberosTicket|null
*/
public static function fromEnv(): ?KerberosTicket {
$ticketName = getenv("KRB5CCNAME");
if (!$ticketName) {
return null;
}
$krb5 = new KRB5CCache();
$krb5->open($ticketName);
return new KerberosTicket($krb5, $ticketName);
}
public static function load(string $ticket): KerberosTicket {
$tmpFilename = tempnam(sys_get_temp_dir(), "krb5cc_php_");
file_put_contents($tmpFilename, $ticket);
register_shutdown_function(function () use ($tmpFilename) {
if (file_exists($tmpFilename)) {
unlink($tmpFilename);
}
});
$ticketName = "FILE:" . $tmpFilename;
$krb5 = new KRB5CCache();
$krb5->open($ticketName);
return new KerberosTicket($krb5, $ticketName);
}
public function save(): string {
if (substr($this->cacheName, 0, 5) === 'FILE:') {
$ticket = file_get_contents(substr($this->cacheName, 5));
} else {
$tmpFilename = tempnam(sys_get_temp_dir(), "krb5cc_php_");
$tmpCacheFile = "FILE:" . $tmpFilename;
$this->krb5->save($tmpCacheFile);
$ticket = file_get_contents($tmpFilename);
unlink($tmpFilename);
}
return $ticket;
}
}

View file

@ -8,6 +8,7 @@
namespace Icewind\SMB\Native;
use Icewind\SMB\Exception\AlreadyExistsException;
use Icewind\SMB\Exception\ConnectionException;
use Icewind\SMB\Exception\ConnectionRefusedException;
use Icewind\SMB\Exception\ConnectionResetException;
use Icewind\SMB\Exception\Exception;
@ -32,9 +33,6 @@ class NativeState {
/** @var resource|null */
protected $state = null;
/** @var bool */
protected $handlerSet = false;
/** @var bool */
protected $connected = false;
@ -67,7 +65,9 @@ class NativeState {
];
protected function handleError(?string $path): void {
/** @var int $error */
if (!$this->state) {
return;
}
$error = smbclient_state_errno($this->state);
if ($error === 0) {
return;
@ -120,7 +120,6 @@ class NativeState {
// __deconstruct() of KerberosAuth should not caled too soon
$this->auth = $auth;
/** @var bool $result */
$result = @smbclient_state_init($this->state, $auth->getWorkgroup(), $auth->getUsername(), $auth->getPassword());
$this->testResult($result, '');
@ -133,6 +132,9 @@ class NativeState {
* @return resource
*/
public function opendir(string $uri) {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var resource $result */
$result = @smbclient_opendir($this->state, $uri);
@ -146,6 +148,9 @@ class NativeState {
* @return array{"type": string, "comment": string, "name": string}|false
*/
public function readdir($dir, string $path) {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var array{"type": string, "comment": string, "name": string}|false $result */
$result = @smbclient_readdir($this->state, $dir);
@ -159,8 +164,10 @@ class NativeState {
* @return bool
*/
public function closedir($dir, string $path): bool {
/** @var bool $result */
$result = smbclient_closedir($this->state, $dir);
if (!$this->state) {
throw new ConnectionException("Not connected");
}
$result = @smbclient_closedir($this->state, $dir);
$this->testResult($result, $path);
return $result;
@ -172,7 +179,9 @@ class NativeState {
* @return bool
*/
public function rename(string $old, string $new): bool {
/** @var bool $result */
if (!$this->state) {
throw new ConnectionException("Not connected");
}
$result = @smbclient_rename($this->state, $old, $this->state, $new);
$this->testResult($result, $new);
@ -184,7 +193,9 @@ class NativeState {
* @return bool
*/
public function unlink(string $uri): bool {
/** @var bool $result */
if (!$this->state) {
throw new ConnectionException("Not connected");
}
$result = @smbclient_unlink($this->state, $uri);
$this->testResult($result, $uri);
@ -197,7 +208,9 @@ class NativeState {
* @return bool
*/
public function mkdir(string $uri, int $mask = 0777): bool {
/** @var bool $result */
if (!$this->state) {
throw new ConnectionException("Not connected");
}
$result = @smbclient_mkdir($this->state, $uri, $mask);
$this->testResult($result, $uri);
@ -209,7 +222,9 @@ class NativeState {
* @return bool
*/
public function rmdir(string $uri): bool {
/** @var bool $result */
if (!$this->state) {
throw new ConnectionException("Not connected");
}
$result = @smbclient_rmdir($this->state, $uri);
$this->testResult($result, $uri);
@ -221,6 +236,9 @@ class NativeState {
* @return array{"mtime": int, "size": int, "mode": int}
*/
public function stat(string $uri): array {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var array{"mtime": int, "size": int, "mode": int} $result */
$result = @smbclient_stat($this->state, $uri);
@ -234,6 +252,9 @@ class NativeState {
* @return array{"mtime": int, "size": int, "mode": int}
*/
public function fstat($file, string $path): array {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var array{"mtime": int, "size": int, "mode": int} $result */
$result = @smbclient_fstat($this->state, $file);
@ -248,6 +269,9 @@ class NativeState {
* @return resource
*/
public function open(string $uri, string $mode, int $mask = 0666) {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var resource $result */
$result = @smbclient_open($this->state, $uri, $mode, $mask);
@ -261,6 +285,9 @@ class NativeState {
* @return resource
*/
public function create(string $uri, int $mask = 0666) {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var resource $result */
$result = @smbclient_creat($this->state, $uri, $mask);
@ -275,6 +302,9 @@ class NativeState {
* @return string
*/
public function read($file, int $bytes, string $path): string {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var string $result */
$result = @smbclient_read($this->state, $file, $bytes);
@ -290,10 +320,19 @@ class NativeState {
* @return int
*/
public function write($file, string $data, string $path, ?int $length = null): int {
/** @var int $result */
$result = @smbclient_write($this->state, $file, $data, $length);
if (!$this->state) {
throw new ConnectionException("Not connected");
}
if ($length) {
$result = @smbclient_write($this->state, $file, $data, $length);
} else {
$result = @smbclient_write($this->state, $file, $data);
}
$this->testResult($result, $path);
if ($result === false) {
return 0;
}
return $result;
}
@ -302,10 +341,18 @@ class NativeState {
* @param int $offset
* @param int $whence SEEK_SET | SEEK_CUR | SEEK_END
* @param string|null $path
* @return int|false new file offset as measured from the start of the file on success.
*
* @return false|int new file offset as measured from the start of the file on success.
*/
public function lseek($file, int $offset, int $whence = SEEK_SET, string $path = null) {
/** @var int|false $result */
if (!$this->state) {
throw new ConnectionException("Not connected");
}
// psalm doesn't think int|false == int|false for some reason, so we do a needless annotation to help it out
/**
* @psalm-suppress UnnecessaryVarAnnotation
* @var int|false $result
*/
$result = @smbclient_lseek($this->state, $file, $offset, $whence);
$this->testResult($result, $path);
@ -319,7 +366,9 @@ class NativeState {
* @return bool
*/
public function ftruncate($file, int $size, string $path): bool {
/** @var bool $result */
if (!$this->state) {
throw new ConnectionException("Not connected");
}
$result = @smbclient_ftruncate($this->state, $file, $size);
$this->testResult($result, $path);
@ -332,7 +381,9 @@ class NativeState {
* @return bool
*/
public function close($file, string $path): bool {
/** @var bool $result */
if (!$this->state) {
return false;
}
$result = @smbclient_close($this->state, $file);
$this->testResult($result, $path);
@ -345,6 +396,9 @@ class NativeState {
* @return string
*/
public function getxattr(string $uri, string $key) {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var string $result */
$result = @smbclient_getxattr($this->state, $uri, $key);
@ -360,6 +414,9 @@ class NativeState {
* @return bool
*/
public function setxattr(string $uri, string $key, string $value, int $flags = 0) {
if (!$this->state) {
throw new ConnectionException("Not connected");
}
/** @var bool $result */
$result = @smbclient_setxattr($this->state, $uri, $key, $value, $flags);
@ -368,7 +425,7 @@ class NativeState {
}
public function __destruct() {
if ($this->connected) {
if ($this->connected && $this->state) {
if (smbclient_state_free($this->state) === false) {
throw new Exception("Failed to free smb state");
}