parent
a6c5bb1cc5
commit
3a224569de
@ -0,0 +1,102 @@ |
||||
<?php |
||||
|
||||
namespace traits; |
||||
|
||||
trait su { |
||||
|
||||
/** |
||||
* Returns which if found or command -v to give Executable |
||||
* details such as path detected for program. |
||||
* |
||||
* @return string /usr/bin/which or command -v |
||||
*/ |
||||
public static function getExecutableDetails(): string |
||||
{ |
||||
$witch_exec = \neato::get_user_bin . "which"; |
||||
if (file_exists($witch_exec) && is_executable($witch_exec)) { |
||||
return $witch_exec; |
||||
} |
||||
return "command -v"; |
||||
} |
||||
|
||||
/** |
||||
* gets BIN paths that are trusted by the system |
||||
* |
||||
* @param string $executable_file path+executable |
||||
* |
||||
* @return string|false trusted bin or false is not trustworthy |
||||
*/ |
||||
public static function getTrustedPath(string $executable_file): string|false |
||||
{ |
||||
$dir = dirname($executable_file); |
||||
$prog = basename($executable_file); |
||||
return match($dir."/") { |
||||
\neato::get_user_bin => \neato::get_user_bin.$prog, |
||||
\neato::get_super_user_bin => \neato::get_super_user_bin.$prog, |
||||
\neato::get_bin => \neato::get_bin.$prog, |
||||
\neato::get_super_bin => \neato::get_super_bin.$prog, |
||||
default => false, |
||||
}; |
||||
} |
||||
|
||||
/** |
||||
* becomeRoot user |
||||
* |
||||
* @return string|bool sudo or doas, or true is root, false unknown su root |
||||
* |
||||
* @throws \Exception upon un-trusted BIN path |
||||
*/ |
||||
public static function becomeRoot(): string|bool |
||||
{ |
||||
if (posix_getuid() === 0) { |
||||
return true; |
||||
} |
||||
$use_find_exec = self::getExecutableDetails(); |
||||
|
||||
exec($use_find_exec . ' doas', $output, $exit_code); |
||||
if ($exit_code === 0) { |
||||
$trusted = self::getTrustedPath($output[0]); |
||||
if ($trusted === false) { |
||||
throw new \Exception("Not a trusted BIN path!"); |
||||
} |
||||
return $trusted; |
||||
} |
||||
unset($output); |
||||
exec($use_find_exec . ' sudo', $output, $exit_code); |
||||
if ($exit_code === 0) { |
||||
$trusted = self::getTrustedPath($output[0]); |
||||
if ($trusted === false) { |
||||
throw new \Exception("Not a trusted BIN path!"); |
||||
} |
||||
return $trusted; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public static function becomeNormal(string $username): string|bool |
||||
{ |
||||
if (posix_getuid() > 0) { |
||||
return true; |
||||
} |
||||
$use_find_exec = self::getExecutableDetails(); |
||||
|
||||
exec($use_find_exec . ' doas', $output, $exit_code); |
||||
if ($exit_code === 0) { |
||||
$trusted = self::getTrustedPath($output[0]); |
||||
if ($trusted === false) { |
||||
throw new \Exception("Not a trusted BIN path!"); |
||||
} |
||||
return $trusted . " -u " .$username; |
||||
} |
||||
unset($output); |
||||
exec($use_find_exec . ' sudo', $output, $exit_code); |
||||
if ($exit_code === 0) { |
||||
$trusted = self::getTrustedPath($output[0]); |
||||
if ($trusted === false) { |
||||
throw new \Exception("Not a trusted BIN path!"); |
||||
} |
||||
return $trusted . " -u " .$username; |
||||
} |
||||
return false; |
||||
} |
||||
} |
||||
@ -1,17 +1,65 @@ |
||||
<?php |
||||
|
||||
// php -f view_phar_file_contents.php -- -nosize |
||||
$pharFile = 'neatoDeploy.phar'; |
||||
|
||||
function hasNoSizeFlag(): bool { |
||||
global $argv; |
||||
return in_array('-nosize', $argv) || in_array('--nosize', $argv); |
||||
} |
||||
|
||||
function formatBytes(int $bytes, int $precision = 2): string { |
||||
$units = ['B', 'KB', 'MB', 'GB', 'TB']; |
||||
$bytes = max($bytes, 0); |
||||
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); |
||||
$pow = min($pow, count($units) - 1); |
||||
$bytes /= (1 << (10 * $pow)); // Calculation: 1024^$pow |
||||
return round($bytes, $precision) . $units[$pow]; |
||||
} |
||||
|
||||
try { |
||||
$phar = new Phar($pharFile); |
||||
if (!is_readable($pharFile)) { |
||||
echo "Need to be root!"; |
||||
exit(1); |
||||
} |
||||
// Try different archive types |
||||
$ext = pathinfo($pharFile, PATHINFO_EXTENSION); |
||||
|
||||
switch (strtolower($ext)) { |
||||
case 'phar': |
||||
$archive = new Phar($pharFile, 0); |
||||
break; |
||||
case 'tar': |
||||
case 'zip': |
||||
$archive = new PharData($pharFile); |
||||
break; |
||||
case 'gz': |
||||
case 'tgz': |
||||
$archive = new PharData('compress.zlib://' . $pharFile); |
||||
break; |
||||
default: |
||||
throw new Exception("Unsupported archive type: $ext"); |
||||
} |
||||
|
||||
// Get the iterator for the Phar archive |
||||
$iterator = new RecursiveIteratorIterator($phar); |
||||
$iterator = new RecursiveIteratorIterator($archive); |
||||
|
||||
// Iterate through the contents |
||||
foreach ($iterator as $file) { |
||||
echo $file->getPathname() . PHP_EOL; |
||||
foreach ($iterator as $file) { |
||||
$path = $file->getPathname(); |
||||
$parent = dirname($path); |
||||
$bp = basename($parent); |
||||
if ($bp == $pharFile) { |
||||
$first = ""; |
||||
} else { |
||||
$first = $bp . '/'; |
||||
} |
||||
$lastTwoParts = $first . basename($path); |
||||
if (hasNoSizeFlag()) { |
||||
echo $lastTwoParts . PHP_EOL; |
||||
} else { |
||||
$size = formatBytes($file->getSize()) . "\t "; |
||||
echo $size . $lastTwoParts . PHP_EOL; |
||||
} |
||||
} |
||||
} catch (PharException $e) { |
||||
} catch (PharException | Exception $e) { |
||||
echo "Error reading Phar archive: " . $e->getMessage(); |
||||
} |
||||
|
||||
Loading…
Reference in new issue