Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Aug 10, 2023
1 parent abfd1eb commit 7b6e55d
Show file tree
Hide file tree
Showing 4 changed files with 312 additions and 19 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]: --delegation always http://httpd.domain.test/example-apache-kerberos.php)
curl -s --negotiate -u [email protected]: --delegation always http://httpd.domain.test/example-sso-kerberos.php)
echo $LIST
LIST=$(echo $LIST | tr -d '[:space:]')
[[ $LIST == "test.txt" ]]
Expand All @@ -218,6 +218,8 @@ jobs:
- "7.3"
- "7.4"
- "8.0"
- "8.1"
- "8.2"

steps:
- name: krb5-dev
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
>
<stubs>
<file name="tests/krb.phpstub" preloadClasses="true"/>
<file name="tests/smbclient.phpstub" preloadClasses="true"/>
</stubs>
<projectFiles>
<directory name="src" />
Expand Down
88 changes: 70 additions & 18 deletions src/Native/NativeState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,9 +33,6 @@ class NativeState {
/** @var resource|null */
protected $state = null;

/** @var bool */
protected $handlerSet = false;

/** @var bool */
protected $connected = false;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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, '');
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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;
}

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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");
}
Expand Down
Loading

0 comments on commit 7b6e55d

Please sign in to comment.