module Net::SSH::Authentication::Pageant::Win

The definition of the Windows methods and data structures used in communicating with the pageant process.

Constants

FILE_MAP_WRITE
INVALID_HANDLE_VALUE

From winbase.h, winnt.h

NULL
PAGE_READWRITE
REVISION

The initial revision level assigned to the security descriptor.

SECURITY_ATTRIBUTES

Contains the security descriptor, this gets passed to the function that constructs the shared memory map.

SECURITY_DESCRIPTOR

The security descriptor holds security information.

SIZEOF_DWORD
SMTO_NORMAL
TOKEN_QUERY

Constants needed for security attribute retrieval. Specifies the access mask corresponding to the desired access rights.

TOKEN_USER

Structs for security attribute functions. Holds the retrieved user access token.

TOKEN_USER_INFORMATION_CLASS

The value of TOKEN_USER from the TOKEN_INFORMATION_CLASS enum.

WM_COPYDATA

Public Class Methods

get_cstr(str) click to toggle source

Get a null-terminated string given a string.

# File lib/net/ssh/authentication/pageant.rb, line 261
def self.get_cstr(str)
  return str + "\000"
end
get_current_user() click to toggle source
# File lib/net/ssh/authentication/pageant.rb, line 210
def self.get_current_user
  token_handle = open_process_token(Win.GetCurrentProcess,
                                    Win::TOKEN_QUERY)
  token_user =  get_token_information(token_handle,
                  Win::TOKEN_USER_INFORMATION_CLASS)
  return token_user
end
get_ptr(data) click to toggle source
# File lib/net/ssh/authentication/pageant.rb, line 167
def self.get_ptr(data)
  return data.to_ptr
end
get_security_attributes_for_user() click to toggle source
# File lib/net/ssh/authentication/pageant.rb, line 188
def self.get_security_attributes_for_user
  user = get_current_user

  psd_information = malloc_ptr(Win::SECURITY_DESCRIPTOR.size)
  raise_error_if_zero(
    Win.InitializeSecurityDescriptor(psd_information,
                                     Win::REVISION))
  raise_error_if_zero(
    Win.SetSecurityDescriptorOwner(psd_information, user.SID,
                                   0))
  raise_error_if_zero(
    Win.IsValidSecurityDescriptor(psd_information))

  nLength = Win::SECURITY_ATTRIBUTES.size
  lpSecurityDescriptor = psd_information
  bInheritHandle = 1
  sa = [nLength, lpSecurityDescriptor.to_i,
        bInheritHandle].pack("LLC")

  return sa
end
get_token_information(token_handle, token_information_class) click to toggle source
# File lib/net/ssh/authentication/pageant.rb, line 229
def self.get_token_information(token_handle,
                               token_information_class)
  # Hold the size of the information to be returned
  preturn_length = malloc_ptr(Win::SIZEOF_DWORD)

  # Going to throw an INSUFFICIENT_BUFFER_ERROR, but that is ok
  # here. This is retrieving the size of the information to be
  # returned.
  Win.GetTokenInformation(token_handle,
                          token_information_class,
                          Win::NULL, 0, preturn_length)
  ptoken_information = malloc_ptr(preturn_length.ptr.to_i)

  # This call is going to write the requested information to
  # the memory location referenced by token_information.
  raise_error_if_zero(
    Win.GetTokenInformation(token_handle,
                            token_information_class,
                            ptoken_information,
                            ptoken_information.size,
                            preturn_length))

  return TOKEN_USER.new(ptoken_information)
end
malloc_ptr(size) click to toggle source
# File lib/net/ssh/authentication/pageant.rb, line 163
def self.malloc_ptr(size)
  return DL.malloc(size)
end
open_process_token(process_handle, desired_access) click to toggle source
# File lib/net/ssh/authentication/pageant.rb, line 218
def self.open_process_token(process_handle, desired_access)
  ptoken_handle = malloc_ptr(Win::SIZEOF_DWORD)

  raise_error_if_zero(
    Win.OpenProcessToken(process_handle, desired_access,
                         ptoken_handle))
  token_handle = ptoken_handle.ptr.to_i

  return token_handle
end
raise_error_if_zero(result) click to toggle source
# File lib/net/ssh/authentication/pageant.rb, line 254
def self.raise_error_if_zero(result)
  if result == 0
    raise "Windows error: #{Win.GetLastError}"
  end
end
set_ptr_data(ptr, data) click to toggle source
# File lib/net/ssh/authentication/pageant.rb, line 171
def self.set_ptr_data(ptr, data)
  ptr[0] = data
end