Class: Chef::Util::Windows::NetGroup

Inherits:
Chef::Util::Windows show all
Defined in:
lib/chef/util/windows/net_group.rb

Overview

wrapper around a subset of the NetGroup* APIs. nothing Chef specific, but not complete enough to be its own gem, so util for now.

Instance Method Summary collapse

Constructor Details

#initialize(groupname) ⇒ NetGroup

Returns a new instance of NetGroup



44
45
46
# File 'lib/chef/util/windows/net_group.rb', line 44

def initialize(groupname)
  @name = multi_to_wide(groupname)
end

Instance Method Details

#local_addObject



80
81
82
83
84
85
# File 'lib/chef/util/windows/net_group.rb', line 80

def local_add
  rc = NetLocalGroupAdd.call(nil, 0, pack_str(@name), nil)
  if rc != NERR_Success
    raise ArgumentError, get_last_error(rc)
  end
end

#local_add_members(members) ⇒ Object



91
92
93
# File 'lib/chef/util/windows/net_group.rb', line 91

def local_add_members(members)
  modify_members(members, NetLocalGroupAddMembers)
end

#local_deleteObject



95
96
97
98
99
100
# File 'lib/chef/util/windows/net_group.rb', line 95

def local_delete
  rc = NetLocalGroupDel.call(nil, @name)
  if rc != NERR_Success
    raise ArgumentError, get_last_error(rc)
  end
end

#local_get_membersObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chef/util/windows/net_group.rb', line 48

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, 1, 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 * 3)) #nread * sizeof(LOCALGROUP_MEMBERS_INFO_1)
      memcpy(members, ptr, members.size)

      #3 pointer fields in LOCALGROUP_MEMBERS_INFO_1, offset 2*PTR_SIZE is lgrmi1_name
      nread.times do |i|
        offset = (i * 3) + 2
        member = lpwstr_to_s(members, offset)
        group_members << member
      end
      NetApiBufferFree(ptr)
    else
      raise ArgumentError, get_last_error(rc)
    end
  end
  group_members
end

#local_set_members(members) ⇒ Object



87
88
89
# File 'lib/chef/util/windows/net_group.rb', line 87

def local_set_members(members)
  modify_members(members, NetLocalGroupSetMembers)
end