From 9fbb05e449ba74fd35df40ddbea32efbdeb3f50e Mon Sep 17 00:00:00 2001 From: Jay Mundrawala Date: Wed, 11 Feb 2015 14:27:12 -0800 Subject: Added GetFileSecurity --- lib/chef/win32/api/security.rb | 1 + lib/chef/win32/security.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/chef/win32/api/security.rb b/lib/chef/win32/api/security.rb index a813c36bdd..f76e39c7e2 100644 --- a/lib/chef/win32/api/security.rb +++ b/lib/chef/win32/api/security.rb @@ -361,6 +361,7 @@ class Chef safe_attach_function :EqualSid, [ :pointer, :pointer ], :BOOL safe_attach_function :FreeSid, [ :pointer ], :pointer safe_attach_function :GetAce, [ :pointer, :DWORD, :pointer ], :BOOL + safe_attach_function :GetFileSecurityW, [:LPCWSTR, :DWORD, :pointer, :DWORD, :pointer], :BOOL safe_attach_function :GetLengthSid, [ :pointer ], :DWORD safe_attach_function :GetNamedSecurityInfoW, [ :LPWSTR, :SE_OBJECT_TYPE, :DWORD, :pointer, :pointer, :pointer, :pointer, :pointer ], :DWORD safe_attach_function :GetSecurityDescriptorControl, [ :pointer, :PWORD, :LPDWORD], :BOOL diff --git a/lib/chef/win32/security.rb b/lib/chef/win32/security.rb index 48ca78647f..61f71256e8 100644 --- a/lib/chef/win32/security.rb +++ b/lib/chef/win32/security.rb @@ -148,6 +148,24 @@ class Chef GetLengthSid(sid) end + def self.get_file_security(path, info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION) + size_ptr = FFI::MemoryPointer.new(:ulong) + + success = GetFileSecurityW(path.to_wstring, info, nil, 0, size_ptr) + + if !success && FFI::LastError.error != ERROR_INSUFFICIENT_BUFFER + Chef::ReservedNames::Win32::Error.raise! + end + + security_descriptor_ptr = FFI::MemoryPointer.new(size_ptr.read_ulong) + unless GetFileSecurityW(path.to_wstring, info, security_descriptor_ptr, size_ptr.read_ulong, size_ptr) + Chef::ReservedNames::Win32::Error.raise! + end + + SecurityDescriptor.new(security_descriptor_ptr) + end + + def self.get_named_security_info(path, type = :SE_FILE_OBJECT, info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION) security_descriptor = FFI::MemoryPointer.new :pointer hr = GetNamedSecurityInfoW(path.to_wstring, type, info, nil, nil, nil, nil, security_descriptor) -- cgit v1.2.1