allow getting acls of files

This commit is contained in:
Robin Appelman 2020-02-10 15:52:48 +01:00
commit 3c5e45dc54
12 changed files with 237 additions and 13 deletions

View file

@ -7,6 +7,7 @@
namespace Icewind\SMB\Wrapped;
use Icewind\SMB\ACL;
use Icewind\SMB\IFileInfo;
class FileInfo implements IFileInfo {
@ -35,19 +36,26 @@ class FileInfo implements IFileInfo {
*/
protected $mode;
/**
* @var callable
*/
protected $aclCallback;
/**
* @param string $path
* @param string $name
* @param int $size
* @param int $time
* @param int $mode
* @param callable $aclCallback
*/
public function __construct($path, $name, $size, $time, $mode) {
public function __construct($path, $name, $size, $time, $mode, callable $aclCallback) {
$this->path = $path;
$this->name = $name;
$this->size = $size;
$this->time = $time;
$this->mode = $mode;
$this->aclCallback = $aclCallback;
}
/**
@ -112,4 +120,11 @@ class FileInfo implements IFileInfo {
public function isArchived() {
return (bool)($this->mode & IFileInfo::MODE_ARCHIVE);
}
/**
* @return ACL[]
*/
public function getAcls(): array {
return ($this->aclCallback)();
}
}

View file

@ -150,7 +150,7 @@ class Parser {
];
}
public function parseDir($output, $basePath) {
public function parseDir($output, $basePath, callable $aclCallback) {
//last line is used space
array_pop($output);
$regex = '/^\s*(.*?)\s\s\s\s+(?:([NDHARS]*)\s+)?([0-9]+)\s+(.*)$/';
@ -162,7 +162,10 @@ class Parser {
if ($name !== '.' and $name !== '..') {
$mode = $this->parseMode($mode);
$time = strtotime($time . ' ' . $this->timeZone);
$content[] = new FileInfo($basePath . '/' . $name, $name, $size, $time, $mode);
$path = $basePath . '/' . $name;
$content[] = new FileInfo($path, $name, $size, $time, $mode, function () use ($aclCallback, $path) {
return $aclCallback($path);
});
}
}
}

View file

@ -8,6 +8,7 @@
namespace Icewind\SMB\Wrapped;
use Icewind\SMB\AbstractShare;
use Icewind\SMB\ACL;
use Icewind\SMB\Exception\ConnectionException;
use Icewind\SMB\Exception\DependencyException;
use Icewind\SMB\Exception\FileInUseException;
@ -153,7 +154,9 @@ class Share extends AbstractShare {
$this->execute('cd /');
return $this->parser->parseDir($output, $path);
return $this->parser->parseDir($output, $path, function ($path) {
return $this->getAcls($path);
});
}
/**
@ -186,7 +189,9 @@ class Share extends AbstractShare {
$this->parseOutput($output, $path);
}
$stat = $this->parser->parseStat($output);
return new FileInfo($path, basename($path), $stat['size'], $stat['mtime'], $stat['mode']);
return new FileInfo($path, basename($path), $stat['size'], $stat['mtime'], $stat['mode'], function () use ($path) {
return $this->getAcls($path);
});
}
/**
@ -421,13 +426,13 @@ class Share extends AbstractShare {
* @param string[] $lines
* @param string $path
*
* @throws NotFoundException
* @return bool
* @throws \Icewind\SMB\Exception\AlreadyExistsException
* @throws \Icewind\SMB\Exception\AccessDeniedException
* @throws \Icewind\SMB\Exception\NotEmptyException
* @throws \Icewind\SMB\Exception\InvalidTypeException
* @throws \Icewind\SMB\Exception\Exception
* @return bool
* @throws NotFoundException
*/
protected function parseOutput($lines, $path = '') {
if (count($lines) === 0) {
@ -470,6 +475,84 @@ class Share extends AbstractShare {
return '"' . $path . '"';
}
protected function getAcls($path) {
$commandPath = $this->system->getSmbcAclsPath();
if (!$commandPath) {
return [];
}
$command = sprintf(
'%s %s %s %s/%s %s',
$commandPath,
$this->getAuthFileArgument(),
$this->server->getAuth()->getExtraCommandLineArguments(),
escapeshellarg('//' . $this->server->getHost()),
escapeshellarg($this->name),
escapeshellarg($path)
);
$connection = new RawConnection($command);
$connection->writeAuthentication($this->server->getAuth()->getUsername(), $this->server->getAuth()->getPassword());
$connection->connect();
if (!$connection->isValid()) {
throw new ConnectionException($connection->readLine());
}
$rawAcls = $connection->readAll();
var_dump($rawAcls);
$acls = [];
foreach ($rawAcls as $acl) {
[$type, $acl] = explode(':', $acl, 2);
if ($type !== 'ACL') {
continue;
}
[$user, $permissions] = explode(':', $acl, 2);
[$type, $flags, $mask] = explode('/', $permissions);
$type = $type === 'ALLOWED' ? ACL::TYPE_ALLOW : ACL::TYPE_DENY;
$flagsInt = 0;
foreach (explode('|', $flags) as $flagString) {
if ($flagString === 'OI') {
$flagsInt += ACL::FLAG_OBJECT_INHERIT;
} elseif ($flagString === 'CI') {
$flagsInt += ACL::FLAG_CONTAINER_INHERIT;
}
}
if (substr($mask, 0, 2) === '0x') {
$maskInt = hexdec($mask);
} else {
$maskInt = 0;
foreach (explode('|', $mask) as $maskString) {
if ($maskString === 'R') {
$maskInt += ACL::MASK_READ;
} elseif ($maskString === 'W') {
$maskInt += ACL::MASK_WRITE;
} elseif ($maskString === 'X') {
$maskInt += ACL::MASK_EXECUTE;
} elseif ($maskString === 'D') {
$maskInt += ACL::MASK_DELETE;
} elseif ($maskString === 'READ') {
$maskInt += ACL::MASK_READ + ACL::MASK_EXECUTE;
} elseif ($maskString === 'CHANGE') {
$maskInt += ACL::MASK_READ + ACL::MASK_EXECUTE + ACL::MASK_WRITE + ACL::MASK_DELETE;
} elseif ($maskString === 'FULL') {
$maskInt += ACL::MASK_READ + ACL::MASK_EXECUTE + ACL::MASK_WRITE + ACL::MASK_DELETE;
}
}
}
if (isset($acls[$user])) {
$existing = $acls[$user];
$maskInt += $existing->getMask();
}
$acls[$user] = new ACL($type, $flagsInt, $maskInt);
}
return $acls;
}
public function __destruct() {
unset($this->connection);
}