ticket47900_test.py 12 KB

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