ticket47900_test.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2015 Red Hat, Inc.
  3. # All rights reserved.
  4. #
  5. # License: GPL (version 3 or any later version).
  6. # See LICENSE for details.
  7. # --- END COPYRIGHT BLOCK ---
  8. #
  9. import os
  10. import sys
  11. import time
  12. import ldap
  13. import logging
  14. import pytest
  15. from lib389 import DirSrv, Entry, tools
  16. from lib389.tools import DirSrvTools
  17. from lib389._constants import *
  18. from lib389.properties import *
  19. log = logging.getLogger(__name__)
  20. CONFIG_DN = 'cn=config'
  21. ADMIN_NAME = 'passwd_admin'
  22. ADMIN_DN = 'cn=%s,%s' % (ADMIN_NAME, SUFFIX)
  23. ADMIN_PWD = 'adminPassword_1'
  24. ENTRY_NAME = 'Joe Schmo'
  25. ENTRY_DN = 'cn=%s,%s' % (ENTRY_NAME, SUFFIX)
  26. INVALID_PWDS = ('2_Short', 'No_Number', 'N0Special', '{SSHA}bBy8UdtPZwu8uZna9QOYG3Pr41RpIRVDl8wddw==')
  27. class TopologyStandalone(object):
  28. def __init__(self, standalone):
  29. standalone.open()
  30. self.standalone = standalone
  31. @pytest.fixture(scope="module")
  32. def topology(request):
  33. '''
  34. This fixture is used to standalone topology for the 'module'.
  35. '''
  36. standalone = DirSrv(verbose=False)
  37. # Args for the standalone instance
  38. args_instance[SER_HOST] = HOST_STANDALONE
  39. args_instance[SER_PORT] = PORT_STANDALONE
  40. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  41. args_standalone = args_instance.copy()
  42. standalone.allocate(args_standalone)
  43. # Get the status of the instance and restart it if it exists
  44. instance_standalone = standalone.exists()
  45. # Remove the instance
  46. if instance_standalone:
  47. standalone.delete()
  48. # Create the instance
  49. standalone.create()
  50. # Used to retrieve configuration information (dbdir, confdir...)
  51. standalone.open()
  52. def fin():
  53. standalone.delete()
  54. request.addfinalizer(fin)
  55. # Here we have standalone instance up and running
  56. return TopologyStandalone(standalone)
  57. def test_ticket47900(topology):
  58. """
  59. Test that password administrators/root DN can
  60. bypass password syntax/policy.
  61. We need to test how passwords are modified in
  62. existing entries, and when adding new entries.
  63. Create the Password Admin entry, but do not set
  64. it as an admin yet. Use the entry to verify invalid
  65. passwords are caught. Then activate the password
  66. admin and make sure it can bypass password policy.
  67. """
  68. # Prepare the Password Administator
  69. entry = Entry(ADMIN_DN)
  70. entry.setValues('objectclass', 'top', 'person')
  71. entry.setValues('sn', ADMIN_NAME)
  72. entry.setValues('cn', ADMIN_NAME)
  73. entry.setValues('userpassword', ADMIN_PWD)
  74. topology.standalone.log.info("Creating Password Administator entry %s..." % ADMIN_DN)
  75. try:
  76. topology.standalone.add_s(entry)
  77. except ldap.LDAPError as e:
  78. topology.standalone.log.error('Unexpected result ' + e.message['desc'])
  79. assert False
  80. topology.standalone.log.error("Failed to add Password Administator %s, error: %s "
  81. % (ADMIN_DN, e.message['desc']))
  82. assert False
  83. topology.standalone.log.info("Configuring password policy...")
  84. try:
  85. topology.standalone.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE, 'nsslapd-pwpolicy-local' , 'on'),
  86. (ldap.MOD_REPLACE, 'passwordCheckSyntax', 'on'),
  87. (ldap.MOD_REPLACE, 'passwordMinCategories' , '1'),
  88. (ldap.MOD_REPLACE, 'passwordMinTokenLength' , '1'),
  89. (ldap.MOD_REPLACE, 'passwordExp' , 'on'),
  90. (ldap.MOD_REPLACE, 'passwordMinDigits' , '1'),
  91. (ldap.MOD_REPLACE, 'passwordMinSpecials' , '1')])
  92. except ldap.LDAPError as e:
  93. topology.standalone.log.error('Failed configure password policy: ' + e.message['desc'])
  94. assert False
  95. #
  96. # Add an aci to allow everyone all access (just makes things easier)
  97. #
  98. topology.standalone.log.info("Add aci to allow password admin to add/update entries...")
  99. ACI_TARGET = "(target = \"ldap:///%s\")" % SUFFIX
  100. ACI_TARGETATTR = "(targetattr = *)"
  101. ACI_ALLOW = "(version 3.0; acl \"Password Admin Access\"; allow (all) "
  102. ACI_SUBJECT = "(userdn = \"ldap:///anyone\");)"
  103. ACI_BODY = ACI_TARGET + ACI_TARGETATTR + ACI_ALLOW + ACI_SUBJECT
  104. mod = [(ldap.MOD_ADD, 'aci', ACI_BODY)]
  105. try:
  106. topology.standalone.modify_s(SUFFIX, mod)
  107. except ldap.LDAPError as e:
  108. topology.standalone.log.error('Failed to add aci for password admin: ' + e.message['desc'])
  109. assert False
  110. #
  111. # Bind as the Password Admin
  112. #
  113. topology.standalone.log.info("Bind as the Password Administator (before activating)...")
  114. try:
  115. topology.standalone.simple_bind_s(ADMIN_DN, ADMIN_PWD)
  116. except ldap.LDAPError as e:
  117. topology.standalone.log.error('Failed to bind as the Password Admin: ' + e.message['desc'])
  118. assert False
  119. #
  120. # Setup our test entry, and test password policy is working
  121. #
  122. entry = Entry(ENTRY_DN)
  123. entry.setValues('objectclass', 'top', 'person')
  124. entry.setValues('sn', ENTRY_NAME)
  125. entry.setValues('cn', ENTRY_NAME)
  126. #
  127. # Start by attempting to add an entry with an invalid password
  128. #
  129. topology.standalone.log.info("Attempt to add entries with invalid passwords, these adds should fail...")
  130. for passwd in INVALID_PWDS:
  131. failed_as_expected = False
  132. entry.setValues('userpassword', passwd)
  133. topology.standalone.log.info("Create a regular user entry %s with password (%s)..." % (ENTRY_DN, passwd))
  134. try:
  135. topology.standalone.add_s(entry)
  136. except ldap.LDAPError as e:
  137. # We failed as expected
  138. failed_as_expected = True
  139. topology.standalone.log.info('Add failed as expected: password (%s) result (%s)'
  140. % (passwd, e.message['desc']))
  141. if not failed_as_expected:
  142. topology.standalone.log.error("We were incorrectly able to add an entry " +
  143. "with an invalid password (%s)" % (passwd))
  144. assert False
  145. #
  146. # Now activate a password administator, bind as root dn to do the config
  147. # update, then rebind as the password admin
  148. #
  149. topology.standalone.log.info("Activate the Password Administator...")
  150. # Bind as Root DN
  151. try:
  152. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  153. except ldap.LDAPError as e:
  154. topology.standalone.log.error('Root DN failed to authenticate: ' + e.message['desc'])
  155. assert False
  156. # Update config
  157. try:
  158. topology.standalone.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE, 'passwordAdminDN', ADMIN_DN)])
  159. except ldap.LDAPError as e:
  160. topology.standalone.log.error('Failed to add password admin to config: ' + e.message['desc'])
  161. assert False
  162. # Bind as Password Admin
  163. try:
  164. topology.standalone.simple_bind_s(ADMIN_DN, ADMIN_PWD)
  165. except ldap.LDAPError as e:
  166. topology.standalone.log.error('Failed to bind as the Password Admin: ' + e.message['desc'])
  167. assert False
  168. #
  169. # Start adding entries with invalid passwords, delete the entry after each pass.
  170. #
  171. for passwd in INVALID_PWDS:
  172. entry.setValues('userpassword', passwd)
  173. topology.standalone.log.info("Create a regular user entry %s with password (%s)..." % (ENTRY_DN, passwd))
  174. try:
  175. topology.standalone.add_s(entry)
  176. except ldap.LDAPError as e:
  177. topology.standalone.log.error('Failed to add entry with password (%s) result (%s)'
  178. % (passwd, e.message['desc']))
  179. assert False
  180. topology.standalone.log.info('Succesfully added entry (%s)' % ENTRY_DN)
  181. # Delete entry for the next pass
  182. try:
  183. topology.standalone.delete_s(ENTRY_DN)
  184. except ldap.LDAPError as e:
  185. topology.standalone.log.error('Failed to delete entry: %s' % (e.message['desc']))
  186. assert False
  187. #
  188. # Add the entry for the next round of testing (modify password)
  189. #
  190. entry.setValues('userpassword', ADMIN_PWD)
  191. try:
  192. topology.standalone.add_s(entry)
  193. except ldap.LDAPError as e:
  194. topology.standalone.log.error('Failed to add entry with valid password (%s) result (%s)'
  195. % (passwd, e.message['desc']))
  196. assert False
  197. #
  198. # Deactivate the password admin and make sure invalid password updates fail
  199. #
  200. topology.standalone.log.info("Deactivate Password Administator and try invalid password updates...")
  201. # Bind as root DN
  202. try:
  203. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  204. except ldap.LDAPError as e:
  205. topology.standalone.log.error('Root DN failed to authenticate: ' + e.message['desc'])
  206. assert False
  207. # Update config
  208. try:
  209. topology.standalone.modify_s(CONFIG_DN, [(ldap.MOD_DELETE, 'passwordAdminDN', None)])
  210. except ldap.LDAPError as e:
  211. topology.standalone.log.error('Failed to remove password admin from config: ' + e.message['desc'])
  212. assert False
  213. # Bind as Password Admin
  214. try:
  215. topology.standalone.simple_bind_s(ADMIN_DN, ADMIN_PWD)
  216. except ldap.LDAPError as e:
  217. topology.standalone.log.error('Failed to bind as the Password Admin: ' + e.message['desc'])
  218. assert False
  219. #
  220. # Make invalid password updates that should fail
  221. #
  222. for passwd in INVALID_PWDS:
  223. failed_as_expected = False
  224. entry.setValues('userpassword', passwd)
  225. try:
  226. topology.standalone.modify_s(ENTRY_DN, [(ldap.MOD_REPLACE, 'userpassword', passwd)])
  227. except ldap.LDAPError as e:
  228. # We failed as expected
  229. failed_as_expected = True
  230. topology.standalone.log.info('Password update failed as expected: password (%s) result (%s)'
  231. % (passwd, e.message['desc']))
  232. if not failed_as_expected:
  233. topology.standalone.log.error("We were incorrectly able to add an invalid password (%s)"
  234. % (passwd))
  235. assert False
  236. #
  237. # Now activate a password administator
  238. #
  239. topology.standalone.log.info("Activate Password Administator and try updates again...")
  240. # Bind as root DN
  241. try:
  242. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  243. except ldap.LDAPError as e:
  244. topology.standalone.log.error('Root DN failed to authenticate: ' + e.message['desc'])
  245. assert False
  246. # Update config
  247. try:
  248. topology.standalone.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE, 'passwordAdminDN', ADMIN_DN)])
  249. except ldap.LDAPError as e:
  250. topology.standalone.log.error('Failed to add password admin to config: ' + e.message['desc'])
  251. assert False
  252. # Bind as Password Admin
  253. try:
  254. topology.standalone.simple_bind_s(ADMIN_DN, ADMIN_PWD)
  255. except ldap.LDAPError as e:
  256. topology.standalone.log.error('Failed to bind as the Password Admin: ' + e.message['desc'])
  257. assert False
  258. #
  259. # Make the same password updates, but this time they should succeed
  260. #
  261. for passwd in INVALID_PWDS:
  262. entry.setValues('userpassword', passwd)
  263. try:
  264. topology.standalone.modify_s(ENTRY_DN, [(ldap.MOD_REPLACE, 'userpassword', passwd)])
  265. except ldap.LDAPError as e:
  266. topology.standalone.log.error('Password update failed unexpectedly: password (%s) result (%s)'
  267. % (passwd, e.message['desc']))
  268. assert False
  269. topology.standalone.log.info('Password update succeeded (%s)' % passwd)
  270. if __name__ == '__main__':
  271. # Run isolated
  272. # -s for DEBUG mode
  273. CURRENT_FILE = os.path.realpath(__file__)
  274. pytest.main("-s %s" % CURRENT_FILE)