lib/win32/security.rb in win32-security-0.2.5 vs lib/win32/security.rb in win32-security-0.3.0

- old
+ new

@@ -1,11 +1,11 @@ # This file allows users to require all security related classes from # a single file, instead of having to require individual files. -require File.join(File.dirname(__FILE__), 'security', 'windows', 'constants') -require File.join(File.dirname(__FILE__), 'security', 'windows', 'structs') -require File.join(File.dirname(__FILE__), 'security', 'windows', 'functions') +require_relative 'security/windows/constants' +require_relative 'security/windows/structs' +require_relative 'security/windows/functions' # The Win32 module serves as a namespace only. module Win32 # The Security class encapsulates security aspects of MS Windows. @@ -18,11 +18,11 @@ include Windows::Security::Constants include Windows::Security::Structs extend Windows::Security::Functions # The version of the win32-security library - VERSION = '0.2.5' + VERSION = '0.3.0' # Used by OpenProcessToken TOKEN_QUERY = 8 # Returns whether or not the owner of the current process is running @@ -31,39 +31,13 @@ # On Windows XP an earlier this method is actually just checking to # see if the caller's process is a member of the local Administrator's # group. # def self.elevated_security? - if windows_version < 6 - sid_ptr = FFI::MemoryPointer.new(:pointer) - nt_auth_ptr = FFI::MemoryPointer.new(SID_IDENTIFIER_AUTHORITY,1) + result = false - nt_auth = SID_IDENTIFIER_AUTHORITY.new(nt_auth_ptr) - nt_auth[:Value].to_ptr.put_bytes(0, 0.chr*5 + 5.chr) - - bool = AllocateAndInitializeSid( - nt_auth_ptr, - 2, - SECURITY_BUILTIN_DOMAIN_RID, - DOMAIN_ALIAS_RID_ADMINS, - 0, 0, 0, 0, 0, 0, - sid_ptr - ) - unless bool - raise SystemCallError.new("AllocateAndInitializeSid", FFI.errno) - end - - pbool = FFI::MemoryPointer.new(:long) - - unless CheckTokenMembership(0, sid_ptr.read_pointer, pbool) - raise SystemCallError.new("CheckTokenMembership", FFI.errno) - end - - pbool.read_long != 0 - else - token = FFI::MemoryPointer.new(:uintptr_t) - + FFI::MemoryPointer.new(:uintptr_t) do |token| unless OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, token) raise SystemCallError.new("OpenProcessToken", FFI.errno) end begin @@ -80,16 +54,20 @@ te.size, rl ) raise SystemCallError.new("GetTokenInformation", FFI.errno) unless bool + + result = te.read_ulong != 0 ensure CloseHandle(token) + te.free + rl.free end - - te.read_ulong != 0 end + + result end private def self.windows_version @@ -105,6 +83,6 @@ end end require 'win32/security/sid' require 'win32/security/acl' -#require 'win32/security/ace' +require 'win32/security/ace'