sudo: change sudo_user to sudo_as_root.

This commit is contained in:
Ilya Kulakov 2023-03-27 14:06:07 -07:00
parent d470661b37
commit 476d97934f
6 changed files with 74 additions and 49 deletions

View File

@ -105,42 +105,46 @@ module Cask
all_services.each do |service| all_services.each do |service|
ohai "Removing launchctl service #{service}" ohai "Removing launchctl service #{service}"
booleans.each do |with_sudo| booleans.each do |sudo|
sudo_user = with_sudo ? "root" : nil
plist_status = command.run( plist_status = command.run(
"/bin/launchctl", "/bin/launchctl",
args: ["list", service], args: ["list", service],
sudo: with_sudo, sudo: sudo,
sudo_user: sudo_user, sudo_as_root: sudo,
print_stderr: false, print_stderr: false,
).stdout ).stdout
if plist_status.start_with?("{") if plist_status.start_with?("{")
command.run!("/bin/launchctl", args: ["remove", service], sudo: sudo_user) command.run!(
"/bin/launchctl",
args: ["remove", service],
sudo: sudo,
sudo_as_root: sudo,
)
sleep 1 sleep 1
end end
paths = [ paths = [
+"/Library/LaunchAgents/#{service}.plist", +"/Library/LaunchAgents/#{service}.plist",
+"/Library/LaunchDaemons/#{service}.plist", +"/Library/LaunchDaemons/#{service}.plist",
] ]
paths.each { |elt| elt.prepend(Dir.home).freeze } unless with_sudo paths.each { |elt| elt.prepend(Dir.home).freeze } unless sudo
paths = paths.map { |elt| Pathname(elt) }.select(&:exist?) paths = paths.map { |elt| Pathname(elt) }.select(&:exist?)
paths.each do |path| paths.each do |path|
command.run!("/bin/rm", args: ["-f", "--", path], sudo: with_sudo, sudo_user: sudo_user) command.run!("/bin/rm", args: ["-f", "--", path], sudo: sudo, sudo_as_root: sudo)
end end
# undocumented and untested: pass a path to uninstall :launchctl # undocumented and untested: pass a path to uninstall :launchctl
next unless Pathname(service).exist? next unless Pathname(service).exist?
command.run!( command.run!(
"/bin/launchctl", "/bin/launchctl",
args: ["unload", "-w", "--", service], args: ["unload", "-w", "--", service],
sudo: with_sudo, sudo: sudo,
sudo_user: sudo_user, sudo_as_root: sudo,
) )
command.run!( command.run!(
"/bin/rm", "/bin/rm",
args: ["-f", "--", service], args: ["-f", "--", service],
sudo: with_sudo, sudo: sudo,
sudo_user: sudo_user, sudo_as_root: sudo,
) )
sleep 1 sleep 1
end end
@ -316,32 +320,32 @@ module Cask
ohai "Unloading kernel extension #{kext}" ohai "Unloading kernel extension #{kext}"
is_loaded = system_command!( is_loaded = system_command!(
"/usr/sbin/kextstat", "/usr/sbin/kextstat",
args: ["-l", "-b", kext], args: ["-l", "-b", kext],
sudo: true, sudo: true,
sudo_user: "root", sudo_as_root: true,
).stdout ).stdout
if is_loaded.length > 1 if is_loaded.length > 1
system_command!( system_command!(
"/sbin/kextunload", "/sbin/kextunload",
args: ["-b", kext], args: ["-b", kext],
sudo: true, sudo: true,
sudo_user: "root", sudo_as_root: true,
) )
sleep 1 sleep 1
end end
kexts = system_command!( found_kexts = system_command!(
"/usr/sbin/kextfind", "/usr/sbin/kextfind",
args: ["-b", kext], args: ["-b", kext],
sudo: true, sudo: true,
sudo_user: "root", sudo_as_root: true,
).stdout ).stdout.chomp.lines
kexts.chomp.lines.each do |kext_path| found_kexts.each do |kext_path|
ohai "Removing kernel extension #{kext_path}" ohai "Removing kernel extension #{kext_path}"
system_command!( system_command!(
"/bin/rm", "/bin/rm",
args: ["-rf", kext_path], args: ["-rf", kext_path],
sudo: true, sudo: true,
sudo_user: "root", sudo_as_root: true,
) )
end end
end end

View File

@ -22,7 +22,12 @@ module Cask
private private
def delete_keyboard_layout_cache(command: nil, **_) def delete_keyboard_layout_cache(command: nil, **_)
command.run!("/bin/rm", args: ["-f", "--", "/System/Library/Caches/com.apple.IntlDataCache.le*"], sudo: true) command.run!(
"/bin/rm",
args: ["-f", "--", "/System/Library/Caches/com.apple.IntlDataCache.le*"],
sudo: true,
sudo_as_root: true,
)
end end
end end
end end

View File

@ -62,7 +62,14 @@ module Cask
"USER" => User.current, "USER" => User.current,
"USERNAME" => User.current, "USERNAME" => User.current,
} }
command.run!("/usr/sbin/installer", sudo: true, sudo_user: "root", args: args, print_stdout: true, env: env) command.run!(
"/usr/sbin/installer",
sudo: true,
sudo_as_root: true,
args: args,
print_stdout: true,
env: env,
)
end end
end end

View File

@ -30,10 +30,10 @@ module Cask
odebug "Deleting pkg files" odebug "Deleting pkg files"
@command.run!( @command.run!(
"/usr/bin/xargs", "/usr/bin/xargs",
args: ["-0", "--", "/bin/rm", "--"], args: ["-0", "--", "/bin/rm", "--"],
input: pkgutil_bom_files.join("\0"), input: pkgutil_bom_files.join("\0"),
sudo: true, sudo: true,
sudo_user: "root", sudo_as_root: true,
) )
end end
@ -41,10 +41,10 @@ module Cask
odebug "Deleting pkg symlinks and special files" odebug "Deleting pkg symlinks and special files"
@command.run!( @command.run!(
"/usr/bin/xargs", "/usr/bin/xargs",
args: ["-0", "--", "/bin/rm", "--"], args: ["-0", "--", "/bin/rm", "--"],
input: pkgutil_bom_specials.join("\0"), input: pkgutil_bom_specials.join("\0"),
sudo: true, sudo: true,
sudo_user: "root", sudo_as_root: true,
) )
end end
@ -61,7 +61,12 @@ module Cask
sig { void } sig { void }
def forget def forget
odebug "Unregistering pkg receipt (aka forgetting)" odebug "Unregistering pkg receipt (aka forgetting)"
@command.run!("/usr/sbin/pkgutil", args: ["--forget", package_id], sudo: true, sudo_user: "root") @command.run!(
"/usr/sbin/pkgutil",
args: ["--forget", package_id],
sudo: true,
sudo_as_root: true,
)
end end
sig { returns(T::Array[Pathname]) } sig { returns(T::Array[Pathname]) }
@ -114,10 +119,10 @@ module Cask
def rmdir(path) def rmdir(path)
@command.run!( @command.run!(
"/usr/bin/xargs", "/usr/bin/xargs",
args: ["-0", "--", RMDIR_SH.to_s], args: ["-0", "--", RMDIR_SH.to_s],
input: Array(path).join("\0"), input: Array(path).join("\0"),
sudo: true, sudo: true,
sudo_user: "root", sudo_as_root: true,
) )
end end

View File

@ -107,6 +107,9 @@ class SystemCommand
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def sudo?; end def sudo?; end
sig { returns(T::Boolean) }
def sudo_as_root?; end
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def print_stdout?; end def print_stdout?; end

View File

@ -65,7 +65,7 @@ class SystemCommand
executable: T.any(String, Pathname), executable: T.any(String, Pathname),
args: T::Array[T.any(String, Integer, Float, URI::Generic)], args: T::Array[T.any(String, Integer, Float, URI::Generic)],
sudo: T::Boolean, sudo: T::Boolean,
sudo_user: T.nilable(String), sudo_as_root: T::Boolean,
env: T::Hash[String, String], env: T::Hash[String, String],
input: T.any(String, T::Array[String]), input: T.any(String, T::Array[String]),
must_succeed: T::Boolean, must_succeed: T::Boolean,
@ -82,7 +82,7 @@ class SystemCommand
executable, executable,
args: [], args: [],
sudo: false, sudo: false,
sudo_user: nil, sudo_as_root: false,
env: {}, env: {},
input: [], input: [],
must_succeed: false, must_succeed: false,
@ -98,10 +98,10 @@ class SystemCommand
@executable = executable @executable = executable
@args = args @args = args
raise ArgumentError, "sudo_user cannot be set if sudo is false" if !sudo && !sudo_user.nil? raise ArgumentError, "sudo_as_root cannot be set if sudo is false" if !sudo && sudo_as_root
@sudo = sudo @sudo = sudo
@sudo_user = sudo_user @sudo_as_root = sudo_as_root
env.each_key do |name| env.each_key do |name|
next if /^[\w&&\D]\w*$/.match?(name) next if /^[\w&&\D]\w*$/.match?(name)
@ -128,7 +128,7 @@ class SystemCommand
attr_reader :executable, :args, :input, :chdir, :env attr_reader :executable, :args, :input, :chdir, :env
attr_predicate :sudo?, :print_stdout?, :print_stderr?, :must_succeed? attr_predicate :sudo?, :sudo_as_root?, :print_stdout?, :print_stderr?, :must_succeed?
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def debug? def debug?
@ -159,7 +159,8 @@ class SystemCommand
sig { returns(T::Array[String]) } sig { returns(T::Array[String]) }
def sudo_prefix def sudo_prefix
user_flags = @sudo_user.nil? ? [] : ["-u", @sudo_user] user_flags = []
user_flags += ["-u", "root"] if sudo_as_root?
askpass_flags = ENV.key?("SUDO_ASKPASS") ? ["-A"] : [] askpass_flags = ENV.key?("SUDO_ASKPASS") ? ["-A"] : []
["/usr/bin/sudo", *user_flags, *askpass_flags, "-E", *env_args, "--"] ["/usr/bin/sudo", *user_flags, *askpass_flags, "-E", *env_args, "--"]
end end