Sha256: 7a42a03ed9818c26034c8ee5b94c87196db9c50292c09340a732f2ba01c42fca

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Facter
  module Resolvers
    class Identity < BaseResolver
      NAME_SAM_COMPATIBLE = 2
      @log = Facter::Log.new(self)

      init_resolver

      class << self
        private

        def post_resolve(fact_name, _options)
          @fact_list.fetch(fact_name) { retrieve_facts(fact_name) }
        end

        def find_username
          require_relative '../../../facter/resolvers/windows/ffi/identity_ffi'

          size_ptr = FFI::MemoryPointer.new(:win32_ulong, 1)
          IdentityFFI::GetUserNameExW(NAME_SAM_COMPATIBLE, FFI::Pointer::NULL, size_ptr)
          if FFI.errno != ERROR_MORE_DATA
            @log.debug "failure resolving identity facts: #{FFI.errno}"
            return
          end

          name_ptr = FFI::MemoryPointer.new(:wchar, size_ptr.read_uint32)
          if IdentityFFI::GetUserNameExW(NAME_SAM_COMPATIBLE, name_ptr, size_ptr) == FFI::WIN32FALSE
            @log.debug "failure resolving identity facts: #{FFI.errno}"
            return
          end

          { user: name_ptr.read_wide_string_with_length(size_ptr.read_uint32), privileged: IdentityFFI.privileged? }
        end

        def retrieve_facts(fact_name)
          result = find_username
          return unless result

          build_fact_list(result)
          @fact_list[fact_name]
        end

        def build_fact_list(facts)
          @fact_list[:user] = facts[:user]
          @fact_list[:privileged] = facts[:privileged]
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facter-4.4.3 lib/facter/resolvers/windows/identity.rb
facter-4.4.2 lib/facter/resolvers/windows/identity.rb
facter-4.4.1 lib/facter/resolvers/windows/identity.rb
facter-4.4.0 lib/facter/resolvers/windows/identity.rb