From 7b6e55d546b0b60b01212200c1fab98db504ba3f Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 10 Aug 2023 15:02:01 +0200 Subject: [PATCH] fixes --- .github/workflows/ci.yaml | 4 +- psalm.xml | 1 + src/Native/NativeState.php | 88 +++++++++++--- tests/smbclient.phpstub | 238 +++++++++++++++++++++++++++++++++++++ 4 files changed, 312 insertions(+), 19 deletions(-) create mode 100644 tests/smbclient.phpstub diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 103b678..25875cb 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -197,7 +197,7 @@ jobs: run: | DC_IP=$(docker inspect dc --format '{{.NetworkSettings.IPAddress}}') LIST=$(docker run --rm --name client -v /tmp/shared:/shared --dns $DC_IP --hostname client.domain.test icewind1991/samba-krb-test-client \ - curl -s --negotiate -u testuser@DOMAIN.TEST: --delegation always http://httpd.domain.test/example-apache-kerberos.php) + curl -s --negotiate -u testuser@DOMAIN.TEST: --delegation always http://httpd.domain.test/example-sso-kerberos.php) echo $LIST LIST=$(echo $LIST | tr -d '[:space:]') [[ $LIST == "test.txt" ]] @@ -218,6 +218,8 @@ jobs: - "7.3" - "7.4" - "8.0" + - "8.1" + - "8.2" steps: - name: krb5-dev diff --git a/psalm.xml b/psalm.xml index a93d7c3..f841e92 100644 --- a/psalm.xml +++ b/psalm.xml @@ -8,6 +8,7 @@ > + diff --git a/src/Native/NativeState.php b/src/Native/NativeState.php index 088e6f7..6fa657a 100644 --- a/src/Native/NativeState.php +++ b/src/Native/NativeState.php @@ -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 @@ public function init(IAuth $auth, IOptions $options) { // __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 @@ public function init(IAuth $auth, IOptions $options) { * @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 @@ public function opendir(string $uri) { * @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 @@ public function readdir($dir, string $path) { * @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 @@ public function closedir($dir, string $path): bool { * @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 @@ public function rename(string $old, string $new): bool { * @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 @@ public function unlink(string $uri): bool { * @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 @@ public function mkdir(string $uri, int $mask = 0777): bool { * @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 @@ public function rmdir(string $uri): bool { * @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 @@ public function stat(string $uri): array { * @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 @@ public function fstat($file, string $path): array { * @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 @@ public function open(string $uri, string $mode, int $mask = 0666) { * @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 @@ public function create(string $uri, int $mask = 0666) { * @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 @@ public function read($file, int $bytes, string $path): string { * @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,13 @@ public function write($file, string $data, string $path, ?int $length = null): i * @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"); + } $result = @smbclient_lseek($this->state, $file, $offset, $whence); $this->testResult($result, $path); @@ -319,7 +361,9 @@ public function lseek($file, int $offset, int $whence = SEEK_SET, string $path = * @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 +376,9 @@ public function ftruncate($file, int $size, string $path): bool { * @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 +391,9 @@ public function close($file, string $path): bool { * @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 +409,9 @@ public function getxattr(string $uri, string $key) { * @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 +420,7 @@ public function setxattr(string $uri, string $key, string $value, int $flags = 0 } 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"); } diff --git a/tests/smbclient.phpstub b/tests/smbclient.phpstub new file mode 100644 index 0000000..5b5180e --- /dev/null +++ b/tests/smbclient.phpstub @@ -0,0 +1,238 @@ +