Module: Puppet::Util::Windows::ADSI

Extended by:
FFI::Library
Defined in:
lib/puppet/util/windows.rb,
lib/puppet/util/windows/adsi.rb

Defined Under Namespace

Modules: Shared Classes: Group, User, UserProfile

Constant Summary collapse

MAX_COMPUTERNAME_LENGTH =

taken from winbase.h

31

Class Method Summary collapse

Methods included from FFI::Library

attach_function_private

Class Method Details

.computer_nameObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/puppet/util/windows/adsi.rb', line 34

def computer_name
  unless @computer_name
    max_length = MAX_COMPUTERNAME_LENGTH + 1 # NULL terminated
    FFI::MemoryPointer.new(max_length * 2) do |buffer| # wide string
      FFI::MemoryPointer.new(:dword, 1) do |buffer_size|
        buffer_size.write_dword(max_length) # length in TCHARs

        if GetComputerNameW(buffer, buffer_size) == FFI::WIN32_FALSE
          raise Puppet::Util::Windows::Error.new(_("Failed to get computer name"))
        end
        @computer_name = buffer.read_wide_string(buffer_size.read_dword)
      end
    end
  end
  @computer_name
end

.computer_uri(host = '.') ⇒ Object



51
52
53
# File 'lib/puppet/util/windows/adsi.rb', line 51

def computer_uri(host = '.')
  "WinNT://#{host}"
end

.connect(uri) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/puppet/util/windows/adsi.rb', line 15

def connect(uri)
  begin
    WIN32OLE.connect(uri)
  rescue WIN32OLERuntimeError => e
    raise Puppet::Error.new( _("ADSI connection error: %{e}") % { e: e }, e )
  end
end

.connectable?(uri) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
# File 'lib/puppet/util/windows/adsi.rb', line 7

def connectable?(uri)
  begin
    !! connect(uri)
  rescue
    false
  end
end

.create(name, resource_type) ⇒ Object



23
24
25
# File 'lib/puppet/util/windows/adsi.rb', line 23

def create(name, resource_type)
  Puppet::Util::Windows::ADSI.connect(computer_uri).Create(resource_type, name)
end

.delete(name, resource_type) ⇒ Object



27
28
29
# File 'lib/puppet/util/windows/adsi.rb', line 27

def delete(name, resource_type)
  Puppet::Util::Windows::ADSI.connect(computer_uri).Delete(resource_type, name)
end

.execquery(query) ⇒ Object



93
94
95
# File 'lib/puppet/util/windows/adsi.rb', line 93

def execquery(query)
  wmi_connection.execquery(query)
end

.sid_uri(sid) ⇒ Object

This method should only be used to generate WinNT://<SID> style monikers used for IAdsGroup::Add / IAdsGroup::Remove. These URIs are not useable to resolve an account with WIN32OLE.connect

Raises:



79
80
81
82
83
# File 'lib/puppet/util/windows/adsi.rb', line 79

def sid_uri(sid)
  raise Puppet::Error.new( _("Must use a valid SID::Principal") ) if !sid.kind_of?(Puppet::Util::Windows::SID::Principal)

  "WinNT://#{sid.sid}"
end

.sid_uri_safe(sid) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method should only be used to generate WinNT://<SID> style monikers used for IAdsGroup::Add / IAdsGroup::Remove. These URIs are not usable to resolve an account with WIN32OLE.connect Valid input is a SID::Principal, S-X-X style SID string or any valid account name with or without domain prefix



65
66
67
68
69
70
71
72
73
74
# File 'lib/puppet/util/windows/adsi.rb', line 65

def sid_uri_safe(sid)
  return sid_uri(sid) if sid.kind_of?(Puppet::Util::Windows::SID::Principal)

  begin
    sid = Puppet::Util::Windows::SID.name_to_sid_object(sid)
    sid_uri(sid)
  rescue Puppet::Util::Windows::Error, Puppet::Error
    nil
  end
end

.uri(resource_name, resource_type, host = '.') ⇒ Object



85
86
87
# File 'lib/puppet/util/windows/adsi.rb', line 85

def uri(resource_name, resource_type, host = '.')
  "#{computer_uri(host)}/#{resource_name},#{resource_type}"
end

.wmi_connectionObject



89
90
91
# File 'lib/puppet/util/windows/adsi.rb', line 89

def wmi_connection
  connect(wmi_resource_uri)
end

.wmi_resource_uri(host = '.') ⇒ Object



55
56
57
# File 'lib/puppet/util/windows/adsi.rb', line 55

def wmi_resource_uri( host = '.' )
  "winmgmts:{impersonationLevel=impersonate}!//#{host}/root/cimv2"
end