ticket47384_test.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. import shutil
  16. from lib389 import DirSrv, Entry, tools, tasks
  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. class TopologyStandalone(object):
  25. def __init__(self, standalone):
  26. standalone.open()
  27. self.standalone = standalone
  28. @pytest.fixture(scope="module")
  29. def topology(request):
  30. global installation1_prefix
  31. if installation1_prefix:
  32. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  33. # Creating standalone instance ...
  34. standalone = DirSrv(verbose=False)
  35. args_instance[SER_HOST] = HOST_STANDALONE
  36. args_instance[SER_PORT] = PORT_STANDALONE
  37. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  38. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  39. args_standalone = args_instance.copy()
  40. standalone.allocate(args_standalone)
  41. instance_standalone = standalone.exists()
  42. if instance_standalone:
  43. standalone.delete()
  44. standalone.create()
  45. standalone.open()
  46. # Delete each instance in the end
  47. def fin():
  48. standalone.delete()
  49. if os.geteuid() == 0:
  50. os.system('setenforce 1')
  51. request.addfinalizer(fin)
  52. return TopologyStandalone(standalone)
  53. def test_ticket47384(topology):
  54. '''
  55. Test pluginpath validation: relative and absolute paths
  56. With the inclusion of ticket 47601 - we do allow plugin paths
  57. outside the default location
  58. '''
  59. if os.geteuid() != 0:
  60. log.warn('This script must be run as root')
  61. return
  62. os.system('setenforce 0')
  63. PLUGIN_DN = 'cn=%s,cn=plugins,cn=config' % PLUGIN_WHOAMI
  64. tmp_dir = '/tmp'
  65. plugin_dir = get_plugin_dir(topology.standalone.prefix)
  66. # Copy the library to our tmp directory
  67. try:
  68. shutil.copy('%s/libwhoami-plugin.so' % plugin_dir, tmp_dir)
  69. except IOError as e:
  70. log.fatal('Failed to copy libwhoami-plugin.so to the tmp directory, error: '
  71. + e.strerror)
  72. assert False
  73. try:
  74. shutil.copy('%s/libwhoami-plugin.la' % plugin_dir, tmp_dir)
  75. except IOError as e:
  76. log.warn('Failed to copy ' + plugin_dir +
  77. '/libwhoami-plugin.la to the tmp directory, error: '
  78. + e.strerror)
  79. #
  80. # Test adding valid plugin paths
  81. #
  82. # Try using the absolute path to the current library
  83. try:
  84. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE,
  85. 'nsslapd-pluginPath', '%s/libwhoami-plugin' % plugin_dir)])
  86. except ldap.LDAPError as e:
  87. log.error('Failed to set valid plugin path (%s): error (%s)' %
  88. ('%s/libwhoami-plugin' % plugin_dir, e.message['desc']))
  89. assert False
  90. # Try using new remote location
  91. try:
  92. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE,
  93. 'nsslapd-pluginPath', '%s/libwhoami-plugin' % tmp_dir)])
  94. except ldap.LDAPError as e:
  95. log.error('Failed to set valid plugin path (%s): error (%s)' %
  96. ('%s/libwhoami-plugin' % tmp_dir, e.message['desc']))
  97. assert False
  98. # Set plugin path back to the default
  99. try:
  100. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE,
  101. 'nsslapd-pluginPath', 'libwhoami-plugin')])
  102. except ldap.LDAPError as e:
  103. log.error('Failed to set valid relative plugin path (%s): error (%s)' %
  104. ('libwhoami-plugin' % tmp_dir, e.message['desc']))
  105. assert False
  106. #
  107. # Test invalid path (no library present)
  108. #
  109. try:
  110. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE,
  111. 'nsslapd-pluginPath', '/bin/libwhoami-plugin')])
  112. # No exception?! This is an error
  113. log.error('Invalid plugin path was incorrectly accepted by the server!')
  114. assert False
  115. except ldap.UNWILLING_TO_PERFORM:
  116. # Correct, operation should be rejected
  117. pass
  118. except ldap.LDAPError as e:
  119. log.error('Failed to set invalid plugin path (%s): error (%s)' %
  120. ('/bin/libwhoami-plugin', e.message['desc']))
  121. #
  122. # Test invalid relative path (no library present)
  123. #
  124. try:
  125. topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_REPLACE,
  126. 'nsslapd-pluginPath', '../libwhoami-plugin')])
  127. # No exception?! This is an error
  128. log.error('Invalid plugin path was incorrectly accepted by the server!')
  129. assert False
  130. except ldap.UNWILLING_TO_PERFORM:
  131. # Correct, operation should be rejected
  132. pass
  133. except ldap.LDAPError as e:
  134. log.error('Failed to set invalid plugin path (%s): error (%s)' %
  135. ('../libwhoami-plugin', e.message['desc']))
  136. log.info('Test complete')
  137. if __name__ == '__main__':
  138. # Run isolated
  139. # -s for DEBUG mode
  140. CURRENT_FILE = os.path.realpath(__file__)
  141. pytest.main("-s %s" % CURRENT_FILE)