You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
191 lines
7.2 KiB
191 lines
7.2 KiB
<?php
|
|
|
|
final class neato {
|
|
|
|
const get_opt = '/opt/';
|
|
const get_etc = '/etc/';
|
|
const get_bin = '/bin/';
|
|
const get_user_bin = '/usr/bin/';
|
|
const get_super_user_bin = '/usr/sbin/';
|
|
const get_user_local_bin = '/usr/local/bin/';
|
|
|
|
protected function __construct() {
|
|
|
|
}
|
|
|
|
public static function is_installed($prog) {
|
|
exec(self::get_user_bin . 'dpkg -s ' . safe_cmd($prog) . ' | ' . self::get_bin . 'grep "install ok installed"', $out, $exit_code);
|
|
exec(self::get_user_bin . 'dpkg -s ' . safe_cmd($prog) . ' | ' . self::get_bin . 'grep ^Version', $output, $code);
|
|
$version = str_replace('Version: ', '', $output[0]);
|
|
return ($exit_code == 0) ? ['installed' => true, 'version' => $version] : ['installed' => false];
|
|
}
|
|
|
|
public static function service($name, $action = 'restart') {
|
|
exec(self::get_super_user_bin . 'service ' . safe_cmd($name, $action), $output, $exit_code);
|
|
display($output);
|
|
check_for_error($exit_code, "Unable to {$action} Service called: {$name}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function systemctl($name, $action = 'enable') {
|
|
exec(self::get_bin . 'systemctl ' . safe_cmd($action, $name), $output, $exit_code);
|
|
check_for_error($exit_code, "Unable to {$action} Service called: {$name}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function upgrade($prog) {
|
|
exec(self::get_user_bin . 'apt-get upgrade -y ' . safe_cmd_quotes($prog) . stderr(), $output, $exit_code);
|
|
display($output);
|
|
check_for_error($exit_code, "Unable to upgrade: {$prog}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function install($prog) {
|
|
exec(self::get_user_bin . 'apt-get install -y ' . safe_cmd_quotes($prog) . stderr(), $output, $exit_code);
|
|
display($output);
|
|
check_for_error($exit_code, "Unable to install: {$prog}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function purge($prog) {
|
|
exec(self::get_user_bin . 'apt-get --purge remove -y ' . safe_cmd_quotes($prog) . stderr(), $output, $exit_code);
|
|
display($output);
|
|
check_for_error($exit_code, "Unable to uninstall: {$prog}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function uninstall($prog) {
|
|
exec(self::get_user_bin . 'apt-get remove -y ' . safe_cmd_quotes($prog) . stderr(), $output, $exit_code);
|
|
display($output);
|
|
check_for_error($exit_code, "Unable to uninstall: {$prog}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function add_repo($repo) {
|
|
exec(self::get_user_bin . 'add-apt-repository -y -u ' . safe_cmd_quotes($repo) . stderr(), $output, $exit_code);
|
|
display($output); // -u = DO UPDATE once done...
|
|
check_for_error($exit_code, "Unable to uninstall: {$repo}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function update() {
|
|
exec(self::get_user_bin . 'apt-get update -y ' . stderr(), $output, $exit_code);
|
|
display($output);
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function full_update() {
|
|
exec(self::get_user_bin . 'apt-get update -y && '. self::get_user_bin . 'apt-get upgrade -y && ' . self::get_user_bin . 'apt-get autoremove -y && ' . self::get_user_bin . 'apt-get autoclean -y' . stderr(), $output, $exit_code);
|
|
display($output);
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function no_sticky_bit($file) {
|
|
if (! file_exists($file)) {
|
|
return true;
|
|
}
|
|
exec(self::get_user_bin . 'chmod -s ' . safe_cmd($file), $output, $exit_code);
|
|
check_for_error($exit_code, "Unable to remove sticky bit with chmod: {$file}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function chmod_on_folders($dir, $kind) {
|
|
if (!is_dir($dir)) {
|
|
$exit_code = false;
|
|
} else {
|
|
$perm = get_perms($kind);
|
|
exec(self::get_user_bin . 'find ' . safe_cmd($dir) . ' -type d -exec ' . self::get_bin . 'chmod ' . $perm . ' {} \;', $output, $exit_code);
|
|
}
|
|
check_for_error($exit_code, "Unable to chmod folders in: {$dir}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function chmod_on_files($dir, $kind) {
|
|
if (!is_dir($dir)) {
|
|
$exit_code = false;
|
|
} else {
|
|
$perm = get_perms($kind);
|
|
exec(self::get_user_bin . 'find ' . safe_cmd($dir) . ' -type f -exec ' . self::get_bin . 'chmod ' . $perm . ' {} \;', $output, $exit_code);
|
|
}
|
|
check_for_error($exit_code, "Unable to chmod files in: {$dir}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function write_protect_file($file) {
|
|
if (!is_file($file)) {
|
|
$exit_code = false;
|
|
} else {
|
|
exec(self::get_user_bin . 'chattr +i ' . safe_cmd($file), $output, $exit_code);
|
|
}
|
|
check_for_error($exit_code, "Unable to write protect: {$file}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function unwrite_protect_file($file) {
|
|
if (!is_file($file)) {
|
|
$exit_code = false;
|
|
} else {
|
|
exec(self::get_user_bin . 'chattr -i ' . safe_cmd($file), $output, $exit_code);
|
|
}
|
|
check_for_error($exit_code, "Unable to un-write protect: {$file}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function userdel($username) {
|
|
exec(self::get_super_user_bin . 'userdel ' . safe_cmd($username), $output, $exit_code);
|
|
if ($exit_code === true || $exit_code === 0) {
|
|
display(getTermColors("Deleted user account named: $username", ['color'=>'green']));
|
|
}
|
|
check_for_error($exit_code, "Unable to delete user: {$username}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function useradd($username) {
|
|
exec(self::get_super_user_bin . 'useradd ' . safe_cmd($username), $output, $exit_code);
|
|
check_for_error($exit_code, "Unable to add new user: {$username}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function lock_status($username) {
|
|
exec(self::get_user_bin . 'passwd -S ' . safe_cmd($username) . " | awk '{print $2}'", $output, $exit_code);
|
|
$sw = $output[0] ?? "";
|
|
switch ($sw) {
|
|
case "P": echo "Account is not locked"; break;
|
|
case "NP": echo "Account has no password"; break;
|
|
case "L": echo "Account is Locked"; break;
|
|
default: echo "Account does not exist?!"; break;
|
|
}
|
|
check_for_error($exit_code, "Unable to view account: {$username}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function passwd($username) {
|
|
exec(self::get_user_bin . 'passwd ' . safe_cmd($username), $output, $exit_code);
|
|
check_for_error($exit_code, "Unable to set user password: {$username}");
|
|
return $exit_code;
|
|
}
|
|
|
|
// Details about age of passwords
|
|
public static function chage($username) {
|
|
exec(self::get_user_bin . 'chage -l ' . safe_cmd($username), $output, $exit_code);
|
|
check_for_error($exit_code, "Unable to view user password changes: {$username}");
|
|
return $exit_code;
|
|
}
|
|
// yyyy-mm-dd
|
|
public static function lock(string $username, string $expires_on="") {
|
|
$exp = (! empty($expires_on)) ? "--expiredate ". safe_cmd($expires_on) . " " : "";
|
|
exec(self::get_super_user_bin . 'usermod -L '. $exp . safe_cmd($username), $output, $exit_code);
|
|
check_for_error($exit_code, "Unable to Lock user account: {$username}");
|
|
return $exit_code;
|
|
}
|
|
|
|
public static function unlock(string $username, string $expires_on="") {
|
|
$exp = (! empty($expires_on)) ? "--expiredate ". safe_cmd($expires_on) . " " : "--expiredate '' ";
|
|
exec(self::get_super_user_bin . 'usermod -U ' . $exp . safe_cmd($username), $output, $exit_code);
|
|
check_for_error($exit_code, "Unable to Unlock user account: {$username}");
|
|
return $exit_code;
|
|
}
|
|
|
|
}
|
|
|
|
// end of neato installer commands
|
|
|