class Puppet::Util::Windows::ADSI::ADSIObject

Common base class shared by the User and Group classes below.

Attributes

object_class[R]

Is either 'user' or 'group'

name[R]

Public Class Methods

delete(name) click to toggle source
    # File lib/puppet/util/windows/adsi.rb
197 def delete(name)
198   Puppet::Util::Windows::ADSI.delete(name, @object_class)
199 end
each(&block) click to toggle source
    # File lib/puppet/util/windows/adsi.rb
234 def each(&block)
235   objects = []
236   list_all.each do |o|
237     # Setting WIN32OLE.codepage in the microsoft_windows feature ensures
238     # values are returned as UTF-8
239     objects << new(o.name)
240   end
241 
242   objects.each(&block)
243 end
exists?(name_or_sid) click to toggle source
    # File lib/puppet/util/windows/adsi.rb
201 def exists?(name_or_sid)
202   well_known = false
203   if (sid = Puppet::Util::Windows::SID.name_to_principal(name_or_sid))
204     # Examples of SidType include SidTypeUser, SidTypeGroup
205     if sid.account_type == "SidType#{@object_class.capitalize}".to_sym
206       # Check if we're getting back a local user when domain-joined
207       return true unless [:MEMBER_WORKSTATION, :MEMBER_SERVER].include?(Puppet::Util::Windows::ADSI.domain_role)
208       # The resource domain and the computer name are not always case-matching
209       return sid.domain.casecmp(Puppet::Util::Windows::ADSI.computer_name) == 0
210     end
211 
212     # 'well known group' is special as it can be a group like Everyone OR a user like SYSTEM
213     # so try to resolve it
214     # https://msdn.microsoft.com/en-us/library/cc234477.aspx
215     well_known = sid.account_type == :SidTypeWellKnownGroup
216     return false if sid.account_type != :SidTypeAlias && !well_known
217     name_or_sid = "#{sid.domain}\\#{sid.account}"
218   end
219 
220   object = Puppet::Util::Windows::ADSI.connect(uri(*parse_name(name_or_sid)))
221   object.Class.downcase == @object_class
222 rescue
223   # special accounts like SYSTEM or special groups like Authenticated Users cannot
224   # resolve via monikers like WinNT://./SYSTEM,user or WinNT://./Authenticated Users,group
225   # -- they'll fail to connect. thus, given a validly resolved SID, this failure is
226   # ambiguous as it may indicate either a group like Service or an account like SYSTEM
227   well_known
228 end
get_sids(adsi_child_collection) click to toggle source

returns Puppet::Util::Windows::SID::Principal[] may contain objects that represent unresolvable SIDs

    # File lib/puppet/util/windows/adsi.rb
175 def get_sids(adsi_child_collection)
176   sids = []
177   adsi_child_collection.each do |m|
178     sids << Puppet::Util::Windows::SID.ads_to_principal(m)
179   end
180 
181   sids
182 end
list_all() click to toggle source
    # File lib/puppet/util/windows/adsi.rb
230 def list_all
231   raise NotImplementedError, _("Subclass must implement class-level method 'list_all'!")
232 end
localized_domains() click to toggle source
    # File lib/puppet/util/windows/adsi.rb
145 def localized_domains
146   @localized_domains ||= [
147     # localized version of BUILTIN
148     # for instance VORDEFINIERT on German Windows
149     Puppet::Util::Windows::SID.sid_to_name('S-1-5-32').upcase,
150     # localized version of NT AUTHORITY (can't use S-1-5)
151     # for instance AUTORITE NT on French Windows
152     Puppet::Util::Windows::SID.name_to_principal('SYSTEM').domain.upcase
153   ]
154 end
name_sid_hash(names, allow_unresolved = false) click to toggle source
    # File lib/puppet/util/windows/adsi.rb
184 def name_sid_hash(names, allow_unresolved = false)
185   return {} if names.nil? || names.empty?
186 
187   sids = names.map do |name|
188     sid = Puppet::Util::Windows::SID.name_to_principal(name, allow_unresolved)
189     raise Puppet::Error.new( _("Could not resolve name: %{name}") % { name: name } ) if !sid
190     [sid.sid, sid]
191   end
192 
193   Hash[ sids ]
194 end
new(name, native_object = nil) click to toggle source
    # File lib/puppet/util/windows/adsi.rb
247 def initialize(name, native_object = nil)
248   @name = name
249   @native_object = native_object
250 end
parse_name(name) click to toggle source
    # File lib/puppet/util/windows/adsi.rb
161 def parse_name(name)
162   if name =~ /\//
163     raise Puppet::Error.new( _("Value must be in DOMAIN\\%{object_class} style syntax") % { object_class: @object_class } )
164   end
165 
166   matches = name.scan(/((.*)\\)?(.*)/)
167   domain = matches[0][1] || '.'
168   account = matches[0][2]
169 
170   return account, domain
171 end
uri(name, host = '.') click to toggle source
    # File lib/puppet/util/windows/adsi.rb
156 def uri(name, host = '.')
157   host = '.' if (localized_domains << Socket.gethostname.upcase).include?(host.upcase)
158   Puppet::Util::Windows::ADSI.uri(name, @object_class, host)
159 end

Public Instance Methods

[](attribute) click to toggle source
    # File lib/puppet/util/windows/adsi.rb
268 def [](attribute)
269   # Setting WIN32OLE.codepage ensures values are returned as UTF-8
270   native_object.Get(attribute)
271 end
[]=(attribute, value) click to toggle source
    # File lib/puppet/util/windows/adsi.rb
273 def []=(attribute, value)
274   native_object.Put(attribute, value)
275 end
commit() click to toggle source
    # File lib/puppet/util/windows/adsi.rb
277 def commit
278   begin
279     native_object.SetInfo
280   rescue WIN32OLERuntimeError => e
281     # ERROR_BAD_USERNAME 2202L from winerror.h
282     if e.message =~ /8007089A/m
283       raise Puppet::Error.new(
284         _("Puppet is not able to create/delete domain %{object_class} objects with the %{object_class} resource.") % { object_class: object_class },
285       )
286     end
287 
288     raise Puppet::Error.new( _("%{object_class} update failed: %{error}") % { object_class: object_class.capitalize, error: e }, e )
289   end
290   self
291 end
native_object() click to toggle source
    # File lib/puppet/util/windows/adsi.rb
260 def native_object
261   @native_object ||= Puppet::Util::Windows::ADSI.connect(self.class.uri(*self.class.parse_name(name)))
262 end
object_class() click to toggle source
    # File lib/puppet/util/windows/adsi.rb
252 def object_class
253   self.class.object_class
254 end
sid() click to toggle source
    # File lib/puppet/util/windows/adsi.rb
264 def sid
265   @sid ||= Puppet::Util::Windows::SID.octet_string_to_principal(native_object.objectSID)
266 end
uri() click to toggle source
    # File lib/puppet/util/windows/adsi.rb
256 def uri
257   self.class.uri(sid.account, sid.domain)
258 end