From 888867e7b64709e884a2feedd1e2ac38a1051669 Mon Sep 17 00:00:00 2001 From: PetrP Date: Sat, 8 Jan 2022 16:57:10 +0100 Subject: [PATCH] SFTP: fix chgrp() for version < 4 $uid and $gid were flipped with dbfc7622571 --- phpseclib/Net/SFTP.php | 2 +- tests/Functional/Net/SFTPUserStoryTest.php | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php index 50c4b47a..7d488d89 100644 --- a/phpseclib/Net/SFTP.php +++ b/phpseclib/Net/SFTP.php @@ -1779,7 +1779,7 @@ class Net_SFTP extends Net_SSH2 function chgrp($filename, $gid, $recursive = false) { $attr = $this->version < 4 ? - pack('N3', NET_SFTP_ATTR_UIDGID, $gid, -1) : + pack('N3', NET_SFTP_ATTR_UIDGID, -1, $gid) : pack('NNa*Na*', NET_SFTP_ATTR_OWNERGROUP, 0, '', strlen($gid), $gid); return $this->_setstat($filename, $attr, $recursive); diff --git a/tests/Functional/Net/SFTPUserStoryTest.php b/tests/Functional/Net/SFTPUserStoryTest.php index c9235a4b..08baba2c 100644 --- a/tests/Functional/Net/SFTPUserStoryTest.php +++ b/tests/Functional/Net/SFTPUserStoryTest.php @@ -754,5 +754,22 @@ class Functional_Net_SFTPUserStoryTest extends PhpseclibFunctionalTestCase $list_cache_disabled = $sftp->rawlist('.', true); $this->assertEquals($list_cache_enabled, $list_cache_disabled, 'The files should be the same regardless of stat cache', 0.0, 10, true); + + return $sftp; + } + + /** + * @depends testRawlistDisabledStatCache + */ + public function testChownChgrp($sftp) + { + $stat = $sftp->stat(self::$scratchDir); + $this->assertTrue($sftp->chown(self::$scratchDir, $stat['uid'])); + $this->assertTrue($sftp->chgrp(self::$scratchDir, $stat['gid'])); + + $sftp->clearStatCache(); + $stat2 = $sftp->stat(self::$scratchDir); + $this->assertSame($stat['uid'], $stat2['uid']); + $this->assertSame($stat['gid'], $stat2['gid']); } }