summaryrefslogtreecommitdiff
path: root/lib/chef/util
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2015-07-29 12:15:34 -0700
committerJay Mundrawala <jdmundrawala@gmail.com>2015-07-29 13:03:12 -0700
commitce7268fd7f09e2f0ca30d664df541ca89e983258 (patch)
treeff0c900fd2cc5c0446be5066ca84a06241e1c151 /lib/chef/util
parent69a8fc2f30188e8947af304d7f804d51d05de00a (diff)
downloadchef-ce7268fd7f09e2f0ca30d664df541ca89e983258.tar.gz
Convert NetLocalGroupAdd to use FFI
Diffstat (limited to 'lib/chef/util')
-rw-r--r--lib/chef/util/windows/net_group.rb215
1 files changed, 109 insertions, 106 deletions
diff --git a/lib/chef/util/windows/net_group.rb b/lib/chef/util/windows/net_group.rb
index 924bd392f9..24ee29257c 100644
--- a/lib/chef/util/windows/net_group.rb
+++ b/lib/chef/util/windows/net_group.rb
@@ -1,106 +1,109 @@
-#
-# Author:: Doug MacEachern (<dougm@vmware.com>)
-# Copyright:: Copyright (c) 2010 VMware, 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 'chef/util/windows'
-
-#wrapper around a subset of the NetGroup* APIs.
-#nothing Chef specific, but not complete enough to be its own gem, so util for now.
-class Chef::Util::Windows::NetGroup < Chef::Util::Windows
-
- private
-
- def pack_str(s)
- [str_to_ptr(s)].pack('L')
- end
-
- def modify_members(members, func)
- buffer = 0.chr * (members.size * PTR_SIZE)
- members.each_with_index do |member,offset|
- buffer[offset*PTR_SIZE,PTR_SIZE] = pack_str(multi_to_wide(member))
- end
- rc = func.call(nil, @name, 3, buffer, members.size)
- if rc != NERR_Success
- raise ArgumentError, get_last_error(rc)
- end
- end
-
- public
-
- def initialize(groupname)
- @name = multi_to_wide(groupname)
- end
-
- def local_get_members
- group_members = []
- handle = 0.chr * PTR_SIZE
- rc = ERROR_MORE_DATA
-
- while rc == ERROR_MORE_DATA
- ptr = 0.chr * PTR_SIZE
- nread = 0.chr * PTR_SIZE
- total = 0.chr * PTR_SIZE
-
- rc = NetLocalGroupGetMembers.call(nil, @name, 0, ptr, -1,
- nread, total, handle)
- if (rc == NERR_Success) || (rc == ERROR_MORE_DATA)
- ptr = ptr.unpack('L')[0]
- nread = nread.unpack('i')[0]
- members = 0.chr * (nread * PTR_SIZE ) #nread * sizeof(LOCALGROUP_MEMBERS_INFO_0)
- memcpy(members, ptr, members.size)
-
- # 1 pointer field in LOCALGROUP_MEMBERS_INFO_0, offset 0 is lgrmi0_sid
- nread.times do |i|
- sid_address = members[i * PTR_SIZE, PTR_SIZE].unpack('L')[0]
- sid_ptr = FFI::Pointer.new(sid_address)
- member_sid = Chef::ReservedNames::Win32::Security::SID.new(sid_ptr)
- group_members << member_sid.to_s
- end
- NetApiBufferFree(ptr)
- else
- raise ArgumentError, get_last_error(rc)
- end
- end
- group_members
- end
-
- def local_add
- rc = NetLocalGroupAdd.call(nil, 0, pack_str(@name), nil)
- if rc != NERR_Success
- raise ArgumentError, get_last_error(rc)
- end
- end
-
- def local_set_members(members)
- modify_members(members, NetLocalGroupSetMembers)
- end
-
- def local_add_members(members)
- modify_members(members, NetLocalGroupAddMembers)
- end
-
- def local_delete_members(members)
- modify_members(members, NetLocalGroupDelMembers)
- end
-
- def local_delete
- rc = NetLocalGroupDel.call(nil, @name)
- if rc != NERR_Success
- raise ArgumentError, get_last_error(rc)
- end
- end
-end
+#
+# Author:: Doug MacEachern (<dougm@vmware.com>)
+# Copyright:: Copyright (c) 2010 VMware, 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 'chef/util/windows'
+require 'chef/win32/net'
+
+#wrapper around a subset of the NetGroup* APIs.
+#nothing Chef specific, but not complete enough to be its own gem, so util for now.
+class Chef::Util::Windows::NetGroup < Chef::Util::Windows
+
+ private
+
+ def pack_str(s)
+ [str_to_ptr(s)].pack('L')
+ end
+
+ def modify_members(members, func)
+ buffer = 0.chr * (members.size * PTR_SIZE)
+ members.each_with_index do |member,offset|
+ buffer[offset*PTR_SIZE,PTR_SIZE] = pack_str(multi_to_wide(member))
+ end
+ rc = func.call(nil, @name, 3, buffer, members.size)
+ if rc != NERR_Success
+ raise ArgumentError, get_last_error(rc)
+ end
+ end
+
+ public
+
+ def initialize(groupname)
+ @name = multi_to_wide(groupname)
+ @groupname = groupname
+ end
+
+ def local_get_members
+ group_members = []
+ handle = 0.chr * PTR_SIZE
+ rc = ERROR_MORE_DATA
+
+ while rc == ERROR_MORE_DATA
+ ptr = 0.chr * PTR_SIZE
+ nread = 0.chr * PTR_SIZE
+ total = 0.chr * PTR_SIZE
+
+ rc = NetLocalGroupGetMembers.call(nil, @name, 0, ptr, -1,
+ nread, total, handle)
+ if (rc == NERR_Success) || (rc == ERROR_MORE_DATA)
+ ptr = ptr.unpack('L')[0]
+ nread = nread.unpack('i')[0]
+ members = 0.chr * (nread * PTR_SIZE ) #nread * sizeof(LOCALGROUP_MEMBERS_INFO_0)
+ memcpy(members, ptr, members.size)
+
+ # 1 pointer field in LOCALGROUP_MEMBERS_INFO_0, offset 0 is lgrmi0_sid
+ nread.times do |i|
+ sid_address = members[i * PTR_SIZE, PTR_SIZE].unpack('L')[0]
+ sid_ptr = FFI::Pointer.new(sid_address)
+ member_sid = Chef::ReservedNames::Win32::Security::SID.new(sid_ptr)
+ group_members << member_sid.to_s
+ end
+ NetApiBufferFree(ptr)
+ else
+ raise ArgumentError, get_last_error(rc)
+ end
+ end
+ group_members
+ end
+
+ def local_add
+ begin
+ Chef::ReservedNames::Win32::NetUser::net_local_group_add(nil, @groupname)
+ rescue Chef::Exceptions::Win32APIError => e
+ raise ArgumentError, e
+ end
+ end
+
+ def local_set_members(members)
+ modify_members(members, NetLocalGroupSetMembers)
+ end
+
+ def local_add_members(members)
+ modify_members(members, NetLocalGroupAddMembers)
+ end
+
+ def local_delete_members(members)
+ modify_members(members, NetLocalGroupDelMembers)
+ end
+
+ def local_delete
+ rc = NetLocalGroupDel.call(nil, @name)
+ if rc != NERR_Success
+ raise ArgumentError, get_last_error(rc)
+ end
+ end
+end