Sha256: 9a8a67c1d4990480a1fde3bbc05e421738542f5da10780dbad0d629fa1f57d98

Contents?: true

Size: 1.12 KB

Versions: 8

Compression:

Stored size: 1.12 KB

Contents

require 'puppet/util/windows'

require 'win32/security'

module Puppet::Util::Windows::User
  include Windows::Security
  extend Windows::Security

  def admin?
    require 'facter'

    majversion = Facter.value(:kernelmajversion)
    return false unless majversion

    # if Vista or later, check for unrestricted process token
    return Win32::Security.elevated_security? unless majversion.to_f < 6.0

    # otherwise 2003 or less
    check_token_membership
  end
  module_function :admin?

  def check_token_membership
    sid = 0.chr * 80
    size = [80].pack('L')
    member = 0.chr * 4

    unless CreateWellKnownSid(WinBuiltinAdministratorsSid, nil, sid, size)
      raise Puppet::Util::Windows::Error.new("Failed to create administrators SID")
    end

    unless IsValidSid(sid)
      raise Puppet::Util::Windows::Error.new("Invalid SID")
    end

    unless CheckTokenMembership(nil, sid, member)
      raise Puppet::Util::Windows::Error.new("Failed to check membership")
    end

    # Is administrators SID enabled in calling thread's access token?
    member.unpack('L')[0] == 1
  end
  module_function :check_token_membership
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
librarian-puppet-0.9.4 vendor/gems/ruby/1.8/gems/puppet-2.7.18/lib/puppet/util/windows/user.rb
librarian-puppet-0.9.3 vendor/gems/ruby/1.8/gems/puppet-2.7.18/lib/puppet/util/windows/user.rb
puppet-2.7.18 lib/puppet/util/windows/user.rb
puppet-2.7.17 lib/puppet/util/windows/user.rb
puppet-2.7.16 lib/puppet/util/windows/user.rb
puppet-2.7.14 lib/puppet/util/windows/user.rb
puppet-2.7.13 lib/puppet/util/windows/user.rb
puppet-2.7.12 lib/puppet/util/windows/user.rb