diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mixlib/shellout.rb | 8 | ||||
-rw-r--r-- | lib/mixlib/shellout/unix.rb | 8 | ||||
-rw-r--r-- | lib/mixlib/shellout/windows.rb | 9 | ||||
-rw-r--r-- | lib/mixlib/shellout/windows/core_ext.rb | 18 |
4 files changed, 31 insertions, 12 deletions
diff --git a/lib/mixlib/shellout.rb b/lib/mixlib/shellout.rb index e95e7df..9345eea 100644 --- a/lib/mixlib/shellout.rb +++ b/lib/mixlib/shellout.rb @@ -210,15 +210,17 @@ module Mixlib # TODO migrate to shellout/unix.rb def uid return nil unless user - user.kind_of?(Integer) ? user : Etc.getpwnam(user.to_s).uid + + user.is_a?(Integer) ? user : Etc.getpwnam(user.to_s).uid end # The gid that the subprocess will switch to. If the group attribute is # given as a group name, it is converted to a gid by Etc.getgrnam # TODO migrate to shellout/unix.rb def gid - return group.kind_of?(Integer) ? group : Etc.getgrnam(group.to_s).gid if group + return group.is_a?(Integer) ? group : Etc.getgrnam(group.to_s).gid if group return Etc.getpwuid(uid).gid if using_login? + nil end @@ -231,6 +233,7 @@ module Mixlib # results when the command exited with an unexpected status. def format_for_exception return "Command execution failed. STDOUT/STDERR suppressed for sensitive resource" if sensitive + msg = "" msg << "#{@terminate_reason}\n" if @terminate_reason msg << "---- Begin output of #{command} ----\n" @@ -363,6 +366,7 @@ module Mixlib if login && !user raise InvalidCommandOption, "cannot set login without specifying a user" end + super end end diff --git a/lib/mixlib/shellout/unix.rb b/lib/mixlib/shellout/unix.rb index 321d2fd..b8f42e0 100644 --- a/lib/mixlib/shellout/unix.rb +++ b/lib/mixlib/shellout/unix.rb @@ -53,14 +53,16 @@ module Mixlib # to the user's secondary groups def sgids return nil unless using_login? + user_name = Etc.getpwuid(uid).name - all_seconderies.select { |g| g.mem.include?(user_name) }.map { |g| g.gid } + all_seconderies.select { |g| g.mem.include?(user_name) }.map(&:gid) end # The environment variables that are deduced from simulating logon # Only valid if login is used def logon_environment return {} unless using_login? + entry = Etc.getpwuid(uid) # According to `man su`, the set fields are: # $HOME, $SHELL, $USER, $LOGNAME, $PATH, and $IFS @@ -269,6 +271,7 @@ module Mixlib # Keep this unbuffered for now def write_to_child_stdin return unless input + child_stdin << input child_stdin.close # Kick things off end @@ -337,7 +340,7 @@ module Mixlib set_cwd begin - command.kind_of?(Array) ? exec(*command, close_others: true) : exec(command, close_others: true) + command.is_a?(Array) ? exec(*command, close_others: true) : exec(command, close_others: true) raise "forty-two" # Should never get here rescue Exception => e @@ -365,6 +368,7 @@ module Mixlib def reap_errant_child return if attempt_reap + @terminate_reason = "Command exceeded allowed execution time, process terminated" logger.error("Command exceeded allowed execution time, sending TERM") if logger Process.kill(:TERM, child_pgid) diff --git a/lib/mixlib/shellout/windows.rb b/lib/mixlib/shellout/windows.rb index db4fe32..26130d3 100644 --- a/lib/mixlib/shellout/windows.rb +++ b/lib/mixlib/shellout/windows.rb @@ -110,6 +110,7 @@ module Mixlib unless GetExitCodeProcess(process.process_handle, exit_code) raise get_last_error end + @status = ThingThatLooksSortOfLikeAProcessStatus.new @status.exitstatus = exit_code.unpack("l").first @@ -170,8 +171,9 @@ module Mixlib def consume_output(open_streams, stdout_read, stderr_read) return false if open_streams.length == 0 + ready = IO.select(open_streams, nil, nil, READ_WAIT_TIME) - return true if ! ready + return true unless ready if ready.first.include?(stdout_read) begin @@ -227,6 +229,7 @@ module Mixlib # @return String def combine_args(*args) return args[0] if args.length == 1 + args.map do |arg| if arg =~ /[ \t\n\v"]/ arg = arg.gsub(/(\\*)"/, '\1\1\"') # interior quotes with N preceeding backslashes need 2N+1 backslashes @@ -321,10 +324,12 @@ module Mixlib return true unless quote when "%" return true if env + env = env_first_char = true next else next unless env + if env_first_char env_first_char = false (env = false) && next if c !~ /[A-Za-z_]/ @@ -370,6 +375,7 @@ module Mixlib def unsafe_process?(name, logger) return false unless system_required_processes.include? name + logger.debug( "A request to kill a critical system process - #{name} - was received and skipped." ) @@ -383,6 +389,7 @@ module Mixlib def kill_process_tree(pid, wmi, logger) wmi.query("select * from Win32_Process where ParentProcessID=#{pid}").each do |instance| next if unsafe_process?(instance.wmi_ole_object.name, logger) + child_pid = instance.wmi_ole_object.processid kill_process_tree(child_pid, wmi, logger) kill_process(instance, logger) diff --git a/lib/mixlib/shellout/windows/core_ext.rb b/lib/mixlib/shellout/windows/core_ext.rb index 73f6611..83a31ab 100644 --- a/lib/mixlib/shellout/windows/core_ext.rb +++ b/lib/mixlib/shellout/windows/core_ext.rb @@ -73,19 +73,19 @@ module Process::Functions [:pointer], :bool attach_pfunc :LoadUserProfileW, - [:handle, :pointer], :bool + %i{handle pointer}, :bool attach_pfunc :UnloadUserProfile, - [:handle, :handle], :bool + %i{handle handle}, :bool ffi_lib :advapi32 attach_pfunc :LogonUserW, - [:buffer_in, :buffer_in, :buffer_in, :ulong, :ulong, :pointer], :bool + %i{buffer_in buffer_in buffer_in ulong ulong pointer}, :bool attach_pfunc :CreateProcessAsUserW, - [:ulong, :buffer_in, :buffer_inout, :pointer, :pointer, :int, - :ulong, :buffer_in, :buffer_in, :pointer, :pointer], :bool + %i{ulong buffer_in buffer_inout pointer pointer int + ulong buffer_in buffer_in pointer pointer}, :bool ffi_lib :user32 @@ -93,7 +93,7 @@ module Process::Functions [], :ulong attach_pfunc :GetUserObjectInformationA, - [:ulong, :uint, :buffer_out, :ulong, :pointer], :bool + %i{ulong uint buffer_out ulong pointer}, :bool end # Override Process.create to check for running in the Service window station and doing @@ -109,7 +109,7 @@ module Process class << self def create(args) - unless args.kind_of?(Hash) + unless args.is_a?(Hash) raise TypeError, "hash keyword arguments expected" end @@ -137,6 +137,7 @@ module Process unless valid_keys.include?(key) raise ArgumentError, "invalid key '#{key}'" end + hash[key] = val end @@ -149,6 +150,7 @@ module Process unless valid_si_keys.include?(key) raise ArgumentError, "invalid startup_info key '#{key}'" end + si_hash[key] = val end end @@ -367,6 +369,7 @@ module Process unless GetProfileType(ptr) raise SystemCallError.new("GetProfileType", FFI.errno) end + ptr.read_uint end @@ -374,6 +377,7 @@ module Process unless LoadUserProfileW(token, profile_ptr) raise SystemCallError.new("LoadUserProfileW", FFI.errno) end + true end |