ticket48272_test.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import pytest
  2. from lib389.tasks import *
  3. from lib389.utils import *
  4. from lib389.topologies import topology_st
  5. DEBUGGING = os.getenv('DEBUGGING', False)
  6. if DEBUGGING:
  7. logging.getLogger(__name__).setLevel(logging.DEBUG)
  8. else:
  9. logging.getLogger(__name__).setLevel(logging.INFO)
  10. log = logging.getLogger(__name__)
  11. USER1 = 'user1'
  12. USER1_DOMAIN = '[email protected]'
  13. PW = 'password'
  14. USER1_DN = 'uid=user1,ou=People,%s' % DEFAULT_SUFFIX
  15. USER1_CONFLICT_DN = 'uid=user1,%s' % DEFAULT_SUFFIX
  16. def _create_user(inst, name, dn):
  17. inst.add_s(Entry((
  18. dn, {
  19. 'objectClass': 'top account simplesecurityobject'.split(),
  20. 'uid': name,
  21. 'userpassword': PW
  22. })))
  23. def _bind(name, cred):
  24. # Returns true or false if it worked.
  25. if DEBUGGING:
  26. print('test 48272 BINDING AS %s:%s' % (name, cred))
  27. status = True
  28. conn = ldap.initialize("ldap://%s:%s" % (HOST_STANDALONE, PORT_STANDALONE))
  29. try:
  30. conn.simple_bind_s(name, cred)
  31. conn.unbind_s()
  32. except ldap.INVALID_CREDENTIALS:
  33. status = False
  34. return status
  35. def test_ticket48272(topology_st):
  36. """
  37. Test the functionality of the addn bind plugin. This should allow users
  38. of the type "name" or "[email protected]" to bind.
  39. """
  40. # There will be a better way to do this in the future.
  41. topology_st.standalone.add_s(Entry((
  42. "cn=addn,cn=plugins,cn=config", {
  43. "objectClass": "top nsSlapdPlugin extensibleObject".split(),
  44. "cn": "addn",
  45. "nsslapd-pluginPath": "libaddn-plugin",
  46. "nsslapd-pluginInitfunc": "addn_init",
  47. "nsslapd-pluginType": "preoperation",
  48. "nsslapd-pluginEnabled": "on",
  49. "nsslapd-pluginId": "addn",
  50. "nsslapd-pluginVendor": "389 Project",
  51. "nsslapd-pluginVersion": "1.3.6.0",
  52. "nsslapd-pluginDescription": "Allow AD DN style bind names to LDAP",
  53. "addn_default_domain": "example.com",
  54. }
  55. )))
  56. topology_st.standalone.add_s(Entry((
  57. "cn=example.com,cn=addn,cn=plugins,cn=config", {
  58. "objectClass": "top extensibleObject".split(),
  59. "cn": "example.com",
  60. "addn_base": "ou=People,%s" % DEFAULT_SUFFIX,
  61. "addn_filter": "(&(objectClass=account)(uid=%s))",
  62. }
  63. )))
  64. topology_st.standalone.restart(60)
  65. # Add a user
  66. _create_user(topology_st.standalone, USER1, USER1_DN)
  67. # Make sure our binds still work.
  68. assert (_bind(USER1_DN, PW))
  69. # Test an anonymous bind
  70. for i in range(0, 10):
  71. # Test bind as name
  72. assert (_bind(USER1, PW))
  73. # Make sure that name@fakedom fails
  74. assert (_bind(USER1_DOMAIN, PW))
  75. # Add a conflicting user to an alternate subtree
  76. _create_user(topology_st.standalone, USER1, USER1_CONFLICT_DN)
  77. # Change the plugin to search from the rootdn instead
  78. # This means we have a conflicting user in scope now!
  79. topology_st.standalone.modify_s("cn=example.com,cn=addn,cn=plugins,cn=config",
  80. [(ldap.MOD_REPLACE, 'addn_base', DEFAULT_SUFFIX)])
  81. topology_st.standalone.restart(60)
  82. # Make sure our binds still work.
  83. assert (_bind(USER1_DN, PW))
  84. assert (_bind(USER1_CONFLICT_DN, PW))
  85. for i in range(0, 10):
  86. # Test bind as name fails
  87. try:
  88. _bind(USER1, PW)
  89. assert (False)
  90. except:
  91. pass
  92. # Test bind as name@domain fails too
  93. try:
  94. _bind(USER1_DOMAIN, PW)
  95. assert (False)
  96. except:
  97. pass
  98. log.info('Test PASSED')
  99. if __name__ == '__main__':
  100. # Run isolated
  101. # -s for DEBUG mode
  102. CURRENT_FILE = os.path.realpath(__file__)
  103. pytest.main("-s %s" % CURRENT_FILE)