require 'tempfile' # Base provider class for OpenLDAP defined types class Puppet::Provider::Openldap < Puppet::Provider # Initialize provider structure initvars # Declare binaries that we use commands cmd_ldapadd: 'ldapadd', cmd_ldapmodify: 'ldapmodify', cmd_slapcat: 'slapcat' # Class-level wrapper methods for ldap binaries def self.ldapadd(path) cmd_ldapadd('-cQY', 'EXTERNAL', '-H', 'ldapi:///', '-f', path) end def self.ldapmodify(path) cmd_ldapmodify('-Y', 'EXTERNAL', '-H', 'ldapi:///', '-f', path) end def self.slapcat(filter, dn = '', base = 'cn=config') arguments = [ '-b', base, '-o', 'ldif-wrap=no', '-H', "ldap:///#{dn}???#{filter}" ] cmd_slapcat(*arguments) end # Instance-level wrapper methods def ldapadd(*args) self.class.ldapadd(*args) end def ldapmodify(*args) self.class.ldapmodify(*args) end def slapcat(*args) self.class.slapcat(*args) end # Create temporary LDIF file def self.temp_ldif(name = 'openldap_ldif') Tempfile.new(name) end def temp_ldif(*args) self.class.temp_ldif(*args) end # Methods to generate LDIF fragments def delimit "-\n" end def cn_config dn('cn=config') end def dn(dn) "dn: #{dn}\n" end def changetype(t) "changetype: #{t}\n" end def add(key) "add: olc#{key}\n" end def del(key) "delete: olc#{key}\n" end def replace(key) "replace: olc#{key}\n" end def key_with_value(key, value) "olc#{key}: #{value}\n" end # Turn LDIF output into an array of olc attributes def self.get_lines(items) items.strip .gsub("\n ", '') .split("\n") .select { |e| e =~ %r{^olc} } .map { |e| e.gsub(%r{^olc}, '') } end def get_lines(*args) self.class.get_lines(*args) end # Turn LDIF output into a 2D array of entries def self.get_entries(items) items.strip .split("\n\n") .map do |p| p.gsub("\n ", '') .split("\n") end end def get_entries(*args) self.class.get_entries(*args) end # Split a line and get the last part def self.last_of_split(line, by = ' ') line.split(by, 2).last end def last_of_split(*args) self.class.last_of_split(*args) end end