/usr/lib/ruby/vendor_ruby/mixlib/shellout/windows/core_ext.rb is in ruby-mixlib-shellout 2.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | #--
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Author:: John Keiser (<jkeiser@opscode.com>)
# Copyright:: Copyright (c) 2011, 2012 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'win32/process'
# Add new constants for Logon
module Process::Constants
LOGON32_LOGON_INTERACTIVE = 0x00000002
LOGON32_PROVIDER_DEFAULT = 0x00000000
UOI_NAME = 0x00000002
end
# Define the functions needed to check with Service windows station
module Process::Functions
module FFI::Library
# Wrapper method for attach_function + private
def attach_pfunc(*args)
attach_function(*args)
private args[0]
end
end
extend FFI::Library
ffi_lib :advapi32
attach_pfunc :LogonUserW,
[:buffer_in, :buffer_in, :buffer_in, :ulong, :ulong, :pointer], :bool
attach_pfunc :CreateProcessAsUserW,
[:ulong, :buffer_in, :buffer_in, :pointer, :pointer, :bool,
:ulong, :buffer_in, :buffer_in, :pointer, :pointer], :bool
ffi_lib :user32
attach_pfunc :GetProcessWindowStation,
[], :ulong
attach_pfunc :GetUserObjectInformationA,
[:ulong, :uint, :buffer_out, :ulong, :pointer], :bool
end
# Override Process.create to check for running in the Service window station and doing
# a full logon with LogonUser, instead of a CreateProcessWithLogon
module Process
include Process::Constants
include Process::Structs
def create(args)
unless args.kind_of?(Hash)
raise TypeError, 'hash keyword arguments expected'
end
valid_keys = %w[
app_name command_line inherit creation_flags cwd environment
startup_info thread_inherit process_inherit close_handles with_logon
domain password
]
valid_si_keys = %w[
startf_flags desktop title x y x_size y_size x_count_chars
y_count_chars fill_attribute sw_flags stdin stdout stderr
]
# Set default values
hash = {
'app_name' => nil,
'creation_flags' => 0,
'close_handles' => true
}
# Validate the keys, and convert symbols and case to lowercase strings.
args.each{ |key, val|
key = key.to_s.downcase
unless valid_keys.include?(key)
raise ArgumentError, "invalid key '#{key}'"
end
hash[key] = val
}
si_hash = {}
# If the startup_info key is present, validate its subkeys
if hash['startup_info']
hash['startup_info'].each{ |key, val|
key = key.to_s.downcase
unless valid_si_keys.include?(key)
raise ArgumentError, "invalid startup_info key '#{key}'"
end
si_hash[key] = val
}
end
# The +command_line+ key is mandatory unless the +app_name+ key
# is specified.
unless hash['command_line']
if hash['app_name']
hash['command_line'] = hash['app_name']
hash['app_name'] = nil
else
raise ArgumentError, 'command_line or app_name must be specified'
end
end
env = nil
# The env string should be passed as a string of ';' separated paths.
if hash['environment']
env = hash['environment']
unless env.respond_to?(:join)
env = hash['environment'].split(File::PATH_SEPARATOR)
end
env = env.map{ |e| e + 0.chr }.join('') + 0.chr
env.to_wide_string! if hash['with_logon']
end
# Process SECURITY_ATTRIBUTE structure
process_security = nil
if hash['process_inherit']
process_security = SECURITY_ATTRIBUTES.new
process_security[:nLength] = 12
process_security[:bInheritHandle] = true
end
# Thread SECURITY_ATTRIBUTE structure
thread_security = nil
if hash['thread_inherit']
thread_security = SECURITY_ATTRIBUTES.new
thread_security[:nLength] = 12
thread_security[:bInheritHandle] = true
end
# Automatically handle stdin, stdout and stderr as either IO objects
# or file descriptors. This won't work for StringIO, however. It also
# will not work on JRuby because of the way it handles internal file
# descriptors.
#
['stdin', 'stdout', 'stderr'].each{ |io|
if si_hash[io]
if si_hash[io].respond_to?(:fileno)
handle = get_osfhandle(si_hash[io].fileno)
else
handle = get_osfhandle(si_hash[io])
end
if handle == INVALID_HANDLE_VALUE
ptr = FFI::MemoryPointer.new(:int)
if windows_version >= 6 && get_errno(ptr) == 0
errno = ptr.read_int
else
errno = FFI.errno
end
raise SystemCallError.new("get_osfhandle", errno)
end
# Most implementations of Ruby on Windows create inheritable
# handles by default, but some do not. RF bug #26988.
bool = SetHandleInformation(
handle,
HANDLE_FLAG_INHERIT,
HANDLE_FLAG_INHERIT
)
raise SystemCallError.new("SetHandleInformation", FFI.errno) unless bool
si_hash[io] = handle
si_hash['startf_flags'] ||= 0
si_hash['startf_flags'] |= STARTF_USESTDHANDLES
hash['inherit'] = true
end
}
procinfo = PROCESS_INFORMATION.new
startinfo = STARTUPINFO.new
unless si_hash.empty?
startinfo[:cb] = startinfo.size
startinfo[:lpDesktop] = si_hash['desktop'] if si_hash['desktop']
startinfo[:lpTitle] = si_hash['title'] if si_hash['title']
startinfo[:dwX] = si_hash['x'] if si_hash['x']
startinfo[:dwY] = si_hash['y'] if si_hash['y']
startinfo[:dwXSize] = si_hash['x_size'] if si_hash['x_size']
startinfo[:dwYSize] = si_hash['y_size'] if si_hash['y_size']
startinfo[:dwXCountChars] = si_hash['x_count_chars'] if si_hash['x_count_chars']
startinfo[:dwYCountChars] = si_hash['y_count_chars'] if si_hash['y_count_chars']
startinfo[:dwFillAttribute] = si_hash['fill_attribute'] if si_hash['fill_attribute']
startinfo[:dwFlags] = si_hash['startf_flags'] if si_hash['startf_flags']
startinfo[:wShowWindow] = si_hash['sw_flags'] if si_hash['sw_flags']
startinfo[:cbReserved2] = 0
startinfo[:hStdInput] = si_hash['stdin'] if si_hash['stdin']
startinfo[:hStdOutput] = si_hash['stdout'] if si_hash['stdout']
startinfo[:hStdError] = si_hash['stderr'] if si_hash['stderr']
end
app = nil
cmd = nil
# Convert strings to wide character strings if present
if hash['app_name']
app = hash['app_name'].to_wide_string
end
if hash['command_line']
cmd = hash['command_line'].to_wide_string
end
if hash['cwd']
cwd = hash['cwd'].to_wide_string
end
inherit = hash['inherit'] || false
if hash['with_logon']
logon = hash['with_logon'].to_wide_string
if hash['password']
passwd = hash['password'].to_wide_string
else
raise ArgumentError, 'password must be specified if with_logon is used'
end
if hash['domain']
domain = hash['domain'].to_wide_string
end
hash['creation_flags'] |= CREATE_UNICODE_ENVIRONMENT
winsta_name = FFI::MemoryPointer.new(:char, 256)
return_size = FFI::MemoryPointer.new(:ulong)
bool = GetUserObjectInformationA(
GetProcessWindowStation(), # Window station handle
UOI_NAME, # Information to get
winsta_name, # Buffer to receive information
winsta_name.size, # Size of buffer
return_size # Size filled into buffer
)
unless bool
raise SystemCallError.new("GetUserObjectInformationA", FFI.errno)
end
winsta_name = winsta_name.read_string(return_size.read_ulong)
# If running in the service windows station must do a log on to get
# to the interactive desktop. Running process user account must have
# the 'Replace a process level token' permission. This is necessary as
# the logon (which happens with CreateProcessWithLogon) must have an
# interactive windows station to attach to, which is created with the
# LogonUser cann with the LOGON32_LOGON_INTERACTIVE flag.
if winsta_name =~ /^Service-0x0-.*$/i
token = FFI::MemoryPointer.new(:ulong)
bool = LogonUserW(
logon, # User
domain, # Domain
passwd, # Password
LOGON32_LOGON_INTERACTIVE, # Logon Type
LOGON32_PROVIDER_DEFAULT, # Logon Provider
token # User token handle
)
unless bool
raise SystemCallError.new("LogonUserW", FFI.errno)
end
token = token.read_ulong
begin
bool = CreateProcessAsUserW(
token, # User token handle
app, # App name
cmd, # Command line
process_security, # Process attributes
thread_security, # Thread attributes
inherit, # Inherit handles
hash['creation_flags'], # Creation Flags
env, # Environment
cwd, # Working directory
startinfo, # Startup Info
procinfo # Process Info
)
ensure
CloseHandle(token)
end
unless bool
raise SystemCallError.new("CreateProcessAsUserW (You must hold the 'Replace a process level token' permission)", FFI.errno)
end
else
bool = CreateProcessWithLogonW(
logon, # User
domain, # Domain
passwd, # Password
LOGON_WITH_PROFILE, # Logon flags
app, # App name
cmd, # Command line
hash['creation_flags'], # Creation flags
env, # Environment
cwd, # Working directory
startinfo, # Startup Info
procinfo # Process Info
)
end
unless bool
raise SystemCallError.new("CreateProcessWithLogonW", FFI.errno)
end
else
bool = CreateProcessW(
app, # App name
cmd, # Command line
process_security, # Process attributes
thread_security, # Thread attributes
inherit, # Inherit handles?
hash['creation_flags'], # Creation flags
env, # Environment
cwd, # Working directory
startinfo, # Startup Info
procinfo # Process Info
)
unless bool
raise SystemCallError.new("CreateProcessW", FFI.errno)
end
end
# Automatically close the process and thread handles in the
# PROCESS_INFORMATION struct unless explicitly told not to.
if hash['close_handles']
CloseHandle(procinfo[:hProcess]) if procinfo[:hProcess]
CloseHandle(procinfo[:hThread]) if procinfo[:hThread]
# Set fields to nil so callers don't attempt to close the handle
# which can result in the wrong handle being closed or an
# exception in some circumstances
procinfo[:hProcess] = nil
procinfo[:hThread] = nil
end
ProcessInfo.new(
procinfo[:hProcess],
procinfo[:hThread],
procinfo[:dwProcessId],
procinfo[:dwThreadId]
)
end
module_function :create
end
|