openldap.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. require 'tempfile'
  2. # Base provider class for OpenLDAP defined types
  3. class Puppet::Provider::Openldap < Puppet::Provider
  4. # Initialize provider structure
  5. initvars
  6. # Declare binaries that we use
  7. commands original_ldapadd: 'ldapadd',
  8. original_ldapmodify: 'ldapmodify',
  9. original_slapcat: 'slapcat'
  10. # Class-level wrapper methods for ldap binaries
  11. def self.ldapadd(path)
  12. original_ldapadd('-cQY', 'EXTERNAL', '-H', 'ldapi:///', '-f', path)
  13. end
  14. def self.ldapmodify(path)
  15. original_ldapmodify('-Y', 'EXTERNAL', '-H', 'ldapi:///', '-f', path)
  16. end
  17. def self.slapcat(filter, dn = '', base = 'cn=config')
  18. arguments = [
  19. '-b', base,
  20. '-o', 'ldif-wrap=no',
  21. '-H', "ldap:///#{dn}???#{filter}"
  22. ]
  23. original_slapcat(*arguments)
  24. end
  25. # Instance-level wrapper methods
  26. def ldapadd(*args)
  27. self.class.ldapadd(*args)
  28. end
  29. def ldapmodify(*args)
  30. self.class.ldapmodify(*args)
  31. end
  32. def slapcat(*args)
  33. self.class.slapcat(*args)
  34. end
  35. # Create temporary LDIF file
  36. def self.temp_ldif(name = 'openldap_ldif')
  37. Tempfile.new(name)
  38. end
  39. def temp_ldif(*args)
  40. self.class.temp_ldif(*args)
  41. end
  42. # Methods to generate LDIF fragments
  43. def delimit
  44. "-\n"
  45. end
  46. def cn_config
  47. dn('cn=config')
  48. end
  49. def dn(dn)
  50. "dn: #{dn}\n"
  51. end
  52. def changetype(t)
  53. "changetype: #{t}\n"
  54. end
  55. def add(key)
  56. "add: olc#{key}\n"
  57. end
  58. def del(key)
  59. "delete: olc#{key}\n"
  60. end
  61. def replace(key)
  62. "replace: olc#{key}\n"
  63. end
  64. def key_with_value(key, value)
  65. "olc#{key}: #{value}\n"
  66. end
  67. end