ticket1347760_test.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2016 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, tasks
  16. from lib389.tools import DirSrvTools
  17. from lib389._constants import *
  18. from lib389.properties import *
  19. from lib389.tasks import *
  20. from lib389.utils import *
  21. logging.getLogger(__name__).setLevel(logging.DEBUG)
  22. log = logging.getLogger(__name__)
  23. installation1_prefix = None
  24. CONFIG_DN = 'cn=config'
  25. BOU = 'BOU'
  26. BINDOU = 'ou=%s,%s' % (BOU, DEFAULT_SUFFIX)
  27. BUID = 'buser123'
  28. TUID = 'tuser0'
  29. BINDDN = 'uid=%s,%s' % (BUID, BINDOU)
  30. BINDPW = BUID
  31. TESTDN = 'uid=%s,ou=people,%s' % (TUID, DEFAULT_SUFFIX)
  32. TESTPW = TUID
  33. BOGUSDN = 'uid=bogus,%s' % DEFAULT_SUFFIX
  34. BOGUSDN2 = 'uid=bogus,ou=people,%s' % DEFAULT_SUFFIX
  35. BOGUSSUFFIX = 'uid=bogus,ou=people,dc=bogus'
  36. GROUPOU = 'ou=groups,%s' % DEFAULT_SUFFIX
  37. BOGUSOU = 'ou=OU,%s' % DEFAULT_SUFFIX
  38. logging.getLogger(__name__).setLevel(logging.DEBUG)
  39. log = logging.getLogger(__name__)
  40. installation1_prefix = None
  41. class TopologyStandalone(object):
  42. def __init__(self, standalone):
  43. standalone.open()
  44. self.standalone = standalone
  45. @pytest.fixture(scope="module")
  46. def topology(request):
  47. global installation1_prefix
  48. if installation1_prefix:
  49. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  50. # Creating standalone instance ...
  51. standalone = DirSrv(verbose=False)
  52. args_instance[SER_HOST] = HOST_STANDALONE
  53. args_instance[SER_PORT] = PORT_STANDALONE
  54. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  55. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  56. args_standalone = args_instance.copy()
  57. standalone.allocate(args_standalone)
  58. instance_standalone = standalone.exists()
  59. if instance_standalone:
  60. standalone.delete()
  61. standalone.create()
  62. standalone.open()
  63. # Delete each instance in the end
  64. def fin():
  65. standalone.delete()
  66. request.addfinalizer(fin)
  67. # Clear out the tmp dir
  68. standalone.clearTmpDir(__file__)
  69. return TopologyStandalone(standalone)
  70. def pattern_accesslog(file, log_pattern):
  71. try:
  72. pattern_accesslog.last_pos += 1
  73. except AttributeError:
  74. pattern_accesslog.last_pos = 0
  75. found = None
  76. file.seek(pattern_accesslog.last_pos)
  77. # Use a while true iteration because 'for line in file: hit a
  78. # python bug that break file.tell()
  79. while True:
  80. line = file.readline()
  81. found = log_pattern.search(line)
  82. if ((line == '') or (found)):
  83. break
  84. pattern_accesslog.last_pos = file.tell()
  85. if found:
  86. return line
  87. else:
  88. return None
  89. def check_op_result(server, op, dn, superior, exists, rc):
  90. targetdn = dn
  91. if op == 'search':
  92. if exists:
  93. opstr = 'Searching existing entry'
  94. else:
  95. opstr = 'Searching non-existing entry'
  96. elif op == 'add':
  97. if exists:
  98. opstr = 'Adding existing entry'
  99. else:
  100. opstr = 'Adding non-existing entry'
  101. elif op == 'modify':
  102. if exists:
  103. opstr = 'Modifying existing entry'
  104. else:
  105. opstr = 'Modifying non-existing entry'
  106. elif op == 'modrdn':
  107. if superior != None:
  108. targetdn = superior
  109. if exists:
  110. opstr = 'Moving to existing superior'
  111. else:
  112. opstr = 'Moving to non-existing superior'
  113. else:
  114. if exists:
  115. opstr = 'Renaming existing entry'
  116. else:
  117. opstr = 'Renaming non-existing entry'
  118. elif op == 'delete':
  119. if exists:
  120. opstr = 'Deleting existing entry'
  121. else:
  122. opstr = 'Deleting non-existing entry'
  123. if ldap.SUCCESS == rc:
  124. expstr = 'be ok'
  125. else:
  126. expstr = 'fail with %s' % rc.__name__
  127. log.info('%s %s, which should %s.' % (opstr, targetdn, expstr))
  128. hit = 0
  129. try:
  130. if op == 'search':
  131. centry = server.search_s(dn, ldap.SCOPE_BASE, 'objectclass=*')
  132. elif op == 'add':
  133. server.add_s(Entry((dn, {'objectclass': 'top extensibleObject'.split(),
  134. 'cn': 'test entry'})))
  135. elif op == 'modify':
  136. server.modify_s(dn, [(ldap.MOD_REPLACE, 'description', 'test')])
  137. elif op == 'modrdn':
  138. if superior != None:
  139. server.rename_s(dn, 'uid=new', newsuperior=superior, delold=1)
  140. else:
  141. server.rename_s(dn, 'uid=new', delold=1)
  142. elif op == 'delete':
  143. server.delete_s(dn)
  144. else:
  145. log.fatal('Unknown operation %s' % op)
  146. assert False
  147. except ldap.LDAPError as e:
  148. hit = 1
  149. log.info("Exception (expected): %s" % type(e).__name__)
  150. log.info('Desc ' + e.message['desc'])
  151. assert isinstance(e, rc)
  152. if e.message.has_key('matched'):
  153. log.info('Matched is returned: ' + e.message['matched'])
  154. if rc != ldap.NO_SUCH_OBJECT:
  155. assert False
  156. if ldap.SUCCESS == rc:
  157. if op == 'search':
  158. log.info('Search should return none')
  159. assert len(centry) == 0
  160. else:
  161. if 0 == hit:
  162. log.info('Expected to fail with %s, but passed' % rc.__name__)
  163. assert False
  164. log.info('PASSED\n')
  165. def test_ticket1347760(topology):
  166. """
  167. Prevent revealing the entry info to whom has no access rights.
  168. """
  169. log.info('Testing Bug 1347760 - Information disclosure via repeated use of LDAP ADD operation, etc.')
  170. log.info('Disabling accesslog logbuffering')
  171. topology.standalone.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE, 'nsslapd-accesslog-logbuffering', 'off')])
  172. log.info('Bind as {%s,%s}' % (DN_DM, PASSWORD))
  173. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  174. log.info('Adding ou=%s a bind user belongs to.' % BOU)
  175. topology.standalone.add_s(Entry((BINDOU, {
  176. 'objectclass': 'top organizationalunit'.split(),
  177. 'ou': BOU})))
  178. log.info('Adding a bind user.')
  179. topology.standalone.add_s(Entry((BINDDN,
  180. {'objectclass': "top person organizationalPerson inetOrgPerson".split(),
  181. 'cn': 'bind user',
  182. 'sn': 'user',
  183. 'userPassword': BINDPW})))
  184. log.info('Adding a test user.')
  185. topology.standalone.add_s(Entry((TESTDN,
  186. {'objectclass': "top person organizationalPerson inetOrgPerson".split(),
  187. 'cn': 'test user',
  188. 'sn': 'user',
  189. 'userPassword': TESTPW})))
  190. log.info('Deleting aci in %s.' % DEFAULT_SUFFIX)
  191. topology.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_DELETE, 'aci', None)])
  192. log.info('Bind case 1. the bind user has no rights to read the entry itself, bind should be successful.')
  193. log.info('Bind as {%s,%s} who has no access rights.' % (BINDDN, BINDPW))
  194. try:
  195. topology.standalone.simple_bind_s(BINDDN, BINDPW)
  196. except ldap.LDAPError as e:
  197. log.info('Desc ' + e.message['desc'])
  198. assert False
  199. file_path = os.path.join(topology.standalone.prefix, 'var/log/dirsrv/slapd-%s/access' % topology.standalone.serverid)
  200. file_obj = open(file_path, "r")
  201. log.info('Access log path: %s' % file_path)
  202. log.info('Bind case 2-1. the bind user does not exist, bind should fail with error %s' % ldap.INVALID_CREDENTIALS.__name__)
  203. log.info('Bind as {%s,%s} who does not exist.' % (BOGUSDN, 'bogus'))
  204. try:
  205. topology.standalone.simple_bind_s(BOGUSDN, 'bogus')
  206. except ldap.LDAPError as e:
  207. log.info("Exception (expected): %s" % type(e).__name__)
  208. log.info('Desc ' + e.message['desc'])
  209. assert isinstance(e, ldap.INVALID_CREDENTIALS)
  210. regex = re.compile('No such entry')
  211. cause = pattern_accesslog(file_obj, regex)
  212. if cause == None:
  213. log.fatal('Cause not found - %s' % cause)
  214. assert False
  215. else:
  216. log.info('Cause found - %s' % cause)
  217. log.info('Bind case 2-2. the bind user\'s suffix does not exist, bind should fail with error %s' % ldap.INVALID_CREDENTIALS.__name__)
  218. log.info('Bind as {%s,%s} who does not exist.' % (BOGUSSUFFIX, 'bogus'))
  219. try:
  220. topology.standalone.simple_bind_s(BOGUSSUFFIX, 'bogus')
  221. except ldap.LDAPError as e:
  222. log.info("Exception (expected): %s" % type(e).__name__)
  223. log.info('Desc ' + e.message['desc'])
  224. assert isinstance(e, ldap.INVALID_CREDENTIALS)
  225. regex = re.compile('No such suffix')
  226. cause = pattern_accesslog(file_obj, regex)
  227. if cause == None:
  228. log.fatal('Cause not found - %s' % cause)
  229. assert False
  230. else:
  231. log.info('Cause found - %s' % cause)
  232. log.info('Bind case 2-3. the bind user\'s password is wrong, bind should fail with error %s' % ldap.INVALID_CREDENTIALS.__name__)
  233. log.info('Bind as {%s,%s} who does not exist.' % (BINDDN, 'bogus'))
  234. try:
  235. topology.standalone.simple_bind_s(BINDDN, 'bogus')
  236. except ldap.LDAPError as e:
  237. log.info("Exception (expected): %s" % type(e).__name__)
  238. log.info('Desc ' + e.message['desc'])
  239. assert isinstance(e, ldap.INVALID_CREDENTIALS)
  240. regex = re.compile('Invalid credentials')
  241. cause = pattern_accesslog(file_obj, regex)
  242. if cause == None:
  243. log.fatal('Cause not found - %s' % cause)
  244. assert False
  245. else:
  246. log.info('Cause found - %s' % cause)
  247. log.info('Adding aci for %s to %s.' % (BINDDN, BINDOU))
  248. acival = '(targetattr="*")(version 3.0; acl "%s"; allow(all) userdn = "ldap:///%s";)' % (BUID, BINDDN)
  249. log.info('aci: %s' % acival)
  250. log.info('Bind as {%s,%s}' % (DN_DM, PASSWORD))
  251. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  252. topology.standalone.modify_s(BINDOU, [(ldap.MOD_ADD, 'aci', acival)])
  253. log.info('Bind case 3. the bind user has the right to read the entry itself, bind should be successful.')
  254. log.info('Bind as {%s,%s} which should be ok.\n' % (BINDDN, BINDPW))
  255. topology.standalone.simple_bind_s(BINDDN, BINDPW)
  256. log.info('The following operations are against the subtree the bind user %s has no rights.' % BINDDN)
  257. # Search
  258. exists = True
  259. rc = ldap.SUCCESS
  260. log.info('Search case 1. the bind user has no rights to read the search entry, it should return no search results with %s' % rc)
  261. check_op_result(topology.standalone, 'search', TESTDN, None, exists, rc)
  262. exists = False
  263. rc = ldap.SUCCESS
  264. log.info('Search case 2-1. the search entry does not exist, the search should return no search results with %s' % rc.__name__)
  265. check_op_result(topology.standalone, 'search', BOGUSDN, None, exists, rc)
  266. exists = False
  267. rc = ldap.SUCCESS
  268. log.info('Search case 2-2. the search entry does not exist, the search should return no search results with %s' % rc.__name__)
  269. check_op_result(topology.standalone, 'search', BOGUSDN2, None, exists, rc)
  270. # Add
  271. exists = True
  272. rc = ldap.INSUFFICIENT_ACCESS
  273. log.info('Add case 1. the bind user has no rights AND the adding entry exists, it should fail with %s' % rc.__name__)
  274. check_op_result(topology.standalone, 'add', TESTDN, None, exists, rc)
  275. exists = False
  276. rc = ldap.INSUFFICIENT_ACCESS
  277. log.info('Add case 2-1. the bind user has no rights AND the adding entry does not exist, it should fail with %s' % rc.__name__)
  278. check_op_result(topology.standalone, 'add', BOGUSDN, None, exists, rc)
  279. exists = False
  280. rc = ldap.INSUFFICIENT_ACCESS
  281. log.info('Add case 2-2. the bind user has no rights AND the adding entry does not exist, it should fail with %s' % rc.__name__)
  282. check_op_result(topology.standalone, 'add', BOGUSDN2, None, exists, rc)
  283. # Modify
  284. exists = True
  285. rc = ldap.INSUFFICIENT_ACCESS
  286. log.info('Modify case 1. the bind user has no rights AND the modifying entry exists, it should fail with %s' % rc.__name__)
  287. check_op_result(topology.standalone, 'modify', TESTDN, None, exists, rc)
  288. exists = False
  289. rc = ldap.INSUFFICIENT_ACCESS
  290. log.info('Modify case 2-1. the bind user has no rights AND the modifying entry does not exist, it should fail with %s' % rc.__name__)
  291. check_op_result(topology.standalone, 'modify', BOGUSDN, None, exists, rc)
  292. exists = False
  293. rc = ldap.INSUFFICIENT_ACCESS
  294. log.info('Modify case 2-2. the bind user has no rights AND the modifying entry does not exist, it should fail with %s' % rc.__name__)
  295. check_op_result(topology.standalone, 'modify', BOGUSDN2, None, exists, rc)
  296. # Modrdn
  297. exists = True
  298. rc = ldap.INSUFFICIENT_ACCESS
  299. log.info('Modrdn case 1. the bind user has no rights AND the renaming entry exists, it should fail with %s' % rc.__name__)
  300. check_op_result(topology.standalone, 'modrdn', TESTDN, None, exists, rc)
  301. exists = False
  302. rc = ldap.INSUFFICIENT_ACCESS
  303. log.info('Modrdn case 2-1. the bind user has no rights AND the renaming entry does not exist, it should fail with %s' % rc.__name__)
  304. check_op_result(topology.standalone, 'modrdn', BOGUSDN, None, exists, rc)
  305. exists = False
  306. rc = ldap.INSUFFICIENT_ACCESS
  307. log.info('Modrdn case 2-2. the bind user has no rights AND the renaming entry does not exist, it should fail with %s' % rc.__name__)
  308. check_op_result(topology.standalone, 'modrdn', BOGUSDN2, None, exists, rc)
  309. exists = True
  310. rc = ldap.INSUFFICIENT_ACCESS
  311. log.info('Modrdn case 3. the bind user has no rights AND the node moving an entry to exists, it should fail with %s' % rc.__name__)
  312. check_op_result(topology.standalone, 'modrdn', TESTDN, GROUPOU, exists, rc)
  313. exists = False
  314. rc = ldap.INSUFFICIENT_ACCESS
  315. log.info('Modrdn case 4-1. the bind user has no rights AND the node moving an entry to does not, it should fail with %s' % rc.__name__)
  316. check_op_result(topology.standalone, 'modrdn', TESTDN, BOGUSOU, exists, rc)
  317. exists = False
  318. rc = ldap.INSUFFICIENT_ACCESS
  319. log.info('Modrdn case 4-2. the bind user has no rights AND the node moving an entry to does not, it should fail with %s' % rc.__name__)
  320. check_op_result(topology.standalone, 'modrdn', TESTDN, BOGUSOU, exists, rc)
  321. # Delete
  322. exists = True
  323. rc = ldap.INSUFFICIENT_ACCESS
  324. log.info('Delete case 1. the bind user has no rights AND the deleting entry exists, it should fail with %s' % rc.__name__)
  325. check_op_result(topology.standalone, 'delete', TESTDN, None, exists, rc)
  326. exists = False
  327. rc = ldap.INSUFFICIENT_ACCESS
  328. log.info('Delete case 2-1. the bind user has no rights AND the deleting entry does not exist, it should fail with %s' % rc.__name__)
  329. check_op_result(topology.standalone, 'delete', BOGUSDN, None, exists, rc)
  330. exists = False
  331. rc = ldap.INSUFFICIENT_ACCESS
  332. log.info('Delete case 2-2. the bind user has no rights AND the deleting entry does not exist, it should fail with %s' % rc.__name__)
  333. check_op_result(topology.standalone, 'delete', BOGUSDN2, None, exists, rc)
  334. log.info('EXTRA: Check no regressions')
  335. log.info('Adding aci for %s to %s.' % (BINDDN, DEFAULT_SUFFIX))
  336. acival = '(targetattr="*")(version 3.0; acl "%s-all"; allow(all) userdn = "ldap:///%s";)' % (BUID, BINDDN)
  337. log.info('Bind as {%s,%s}' % (DN_DM, PASSWORD))
  338. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  339. topology.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_ADD, 'aci', acival)])
  340. log.info('Bind as {%s,%s}.' % (BINDDN, BINDPW))
  341. try:
  342. topology.standalone.simple_bind_s(BINDDN, BINDPW)
  343. except ldap.LDAPError as e:
  344. log.info('Desc ' + e.message['desc'])
  345. assert False
  346. exists = False
  347. rc = ldap.NO_SUCH_OBJECT
  348. log.info('Search case. the search entry does not exist, the search should fail with %s' % rc.__name__)
  349. check_op_result(topology.standalone, 'search', BOGUSDN2, None, exists, rc)
  350. file_obj.close()
  351. exists = True
  352. rc = ldap.ALREADY_EXISTS
  353. log.info('Add case. the adding entry already exists, it should fail with %s' % rc.__name__)
  354. check_op_result(topology.standalone, 'add', TESTDN, None, exists, rc)
  355. exists = False
  356. rc = ldap.NO_SUCH_OBJECT
  357. log.info('Modify case. the modifying entry does not exist, it should fail with %s' % rc.__name__)
  358. check_op_result(topology.standalone, 'modify', BOGUSDN, None, exists, rc)
  359. exists = False
  360. rc = ldap.NO_SUCH_OBJECT
  361. log.info('Modrdn case 1. the renaming entry does not exist, it should fail with %s' % rc.__name__)
  362. check_op_result(topology.standalone, 'modrdn', BOGUSDN, None, exists, rc)
  363. exists = False
  364. rc = ldap.NO_SUCH_OBJECT
  365. log.info('Modrdn case 2. the node moving an entry to does not, it should fail with %s' % rc.__name__)
  366. check_op_result(topology.standalone, 'modrdn', TESTDN, BOGUSOU, exists, rc)
  367. exists = False
  368. rc = ldap.NO_SUCH_OBJECT
  369. log.info('Delete case. the deleting entry does not exist, it should fail with %s' % rc.__name__)
  370. check_op_result(topology.standalone, 'delete', BOGUSDN, None, exists, rc)
  371. log.info('SUCCESS')
  372. if __name__ == '__main__':
  373. # Run isolated
  374. # -s for DEBUG mode
  375. CURRENT_FILE = os.path.realpath(__file__)
  376. pytest.main("-s %s" % CURRENT_FILE)