summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Kvashenkin <ak@gfoil.ru>2018-09-08 11:58:30 +0300
committerAnton Kvashenkin <ak@gfoil.ru>2018-09-08 11:58:30 +0300
commit030ccf713d832ddcb7c1965700033a9536fe922a (patch)
tree6ef3260aa256f9e66cf40a55ae71450d091184c6
parent68db80ab4b8f21fca6d3a2121ced524ac9f09167 (diff)
downloadohai-030ccf713d832ddcb7c1965700033a9536fe922a.tar.gz
Fix root_group plugin invalid byte sequence on non-English version of Windows
Signed-off-by: Anton Kvashenkin <anton.jugatsu@gmail.com>
-rw-r--r--lib/ohai/plugins/root_group.rb29
-rw-r--r--lib/ohai/util/win32/group_helper.rb73
2 files changed, 27 insertions, 75 deletions
diff --git a/lib/ohai/plugins/root_group.rb b/lib/ohai/plugins/root_group.rb
index 04fb6412..06bb2836 100644
--- a/lib/ohai/plugins/root_group.rb
+++ b/lib/ohai/plugins/root_group.rb
@@ -18,9 +18,34 @@
Ohai.plugin(:RootGroup) do
provides "root_group"
+ #
+ # Performs a WMI query using WIN32OLE from the Ruby Stdlib
+ #
+ # @return [String]
+ #
+ def wmi_property_from_query(wmi_property, wmi_query)
+ @wmi = ::WIN32OLE.connect('winmgmts://')
+ result = @wmi.ExecQuery(wmi_query)
+ return nil unless result.each.count > 0
+ result.each.next.send(wmi_property)
+ end
+
+ # Per http://support.microsoft.com/kb/243330 SID: S-1-5-32-544 is the
+ # internal name for the Administrators group, which lets us work
+ # properly in environments with a renamed or localized name for the
+ # Administrators group.
+ # Use LocalAccount=True because otherwise WMI will attempt to include
+ # (unneeded) Active Directory groups by querying AD, which is a performance
+ # and reliability issue since AD might not be reachable.
+ def windows_root_group_name
+ wmi_property_from_query(
+ :name,
+ "select * from Win32_Group where sid like 'S-1-5-32-544' and LocalAccount=True"
+ )
+ end
+
collect_data(:windows) do
- require "ohai/util/win32/group_helper"
- root_group Ohai::Util::Win32::GroupHelper.windows_root_group_name
+ root_group windows_root_group_name
end
collect_data(:default) do
diff --git a/lib/ohai/util/win32/group_helper.rb b/lib/ohai/util/win32/group_helper.rb
deleted file mode 100644
index 0abc1db7..00000000
--- a/lib/ohai/util/win32/group_helper.rb
+++ /dev/null
@@ -1,73 +0,0 @@
-# Author:: Adam Edwards (<adamed@chef.io>)
-#
-# Copyright:: Copyright (c) 2013-14 Chef Software, Inc.
-#
-# License:: Apache License, Version 2.0
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-require "ohai/util/win32"
-
-module Ohai
- module Util
- class Win32::GroupHelper
-
- # Per http://support.microsoft.com/kb/243330 SID: S-1-5-32-544 is the
- # internal name for the Administrators group, which lets us work
- # properly in environments with a renamed or localized name for the
- # Administrators group
- BUILTIN_ADMINISTRATORS_SID = "S-1-5-32-544".freeze
-
- def self.windows_root_group_name
- administrators_sid_result = FFI::MemoryPointer.new(:pointer)
- convert_result = Win32.convert_string_sid_to_sid(BUILTIN_ADMINISTRATORS_SID, administrators_sid_result)
- last_win32_error = Win32.get_last_error
-
- if convert_result == 0
- raise "ERROR: failed to to convert sid string '#{BUILTIN_ADMINISTRATORS_SID}' to a Windows SID structure because Win32 API function ConvertStringSidToSid returned #{last_win32_error}."
- end
-
- administrators_group_name_buffer = 0.chr * 260
- administrators_group_name_length = [administrators_group_name_buffer.length].pack("L")
- domain_name_length_buffer = [260].pack("L")
- sid_use_result = 0.chr * 4
-
- # Use LookupAccountSid rather than WMI's Win32_Group class because WMI will attempt
- # to include (unneeded) Active Directory groups by querying AD, which is a performance
- # and reliability issue since AD might not be reachable. Additionally, in domains with
- # thousands of groups, the WMI query is very slow, on the order of minutes, even to
- # get the first result. So we use LookupAccountSid which is a purely local lookup
- # of the built-in group, with no need to access AD, and thus no failure modes related
- # to network conditions or query performance.
- lookup_boolean_result = Win32.lookup_account_sid(
- nil,
- administrators_sid_result.read_pointer,
- administrators_group_name_buffer,
- administrators_group_name_length,
- nil,
- domain_name_length_buffer,
- sid_use_result)
-
- last_win32_error = Win32.get_last_error
-
- Win32.local_free(administrators_sid_result.read_pointer)
-
- if lookup_boolean_result == 0
- raise "ERROR: failed to find root group (i.e. builtin\\administrators) for sid #{BUILTIN_ADMINISTRATORS_SID} because Win32 API function LookupAccountSid returned #{last_win32_error}."
- end
-
- administrators_group_name_buffer.strip
- end
- end
- end
-end