| 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 pytest |
| 10 |
from lib389.tasks import * |
| 11 |
from lib389.utils import * |
| 12 |
from test389.topologies import topology_st |
| 13 |
from lib389.plugins import WhoamiPlugin |
| 14 |
|
| 15 |
pytestmark = pytest.mark.tier1 |
| 16 |
|
| 17 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 18 |
log = logging.getLogger(__name__) |
| 19 |
|
| 20 |
|
| 21 |
def test_pluginpath_validation(topology_st): |
| 22 |
"""Test pluginpath validation: relative and absolute paths |
| 23 |
With the inclusion of ticket 47601 - we do allow plugin paths |
| 24 |
outside the default location |
| 25 |
|
| 26 |
:id: 99f1fb2f-051d-4fd9-93d0-592dcd9b4c22 |
| 27 |
:setup: Standalone instance |
| 28 |
:steps: |
| 29 |
1. Copy the library to a temporary directory |
| 30 |
2. Add valid plugin paths |
| 31 |
* using the absolute path to the current library |
| 32 |
* using new remote location |
| 33 |
3. Set plugin path back to the default |
| 34 |
4. Check invalid path (no library present) |
| 35 |
5. Check invalid relative path (no library present) |
| 36 |
|
| 37 |
:expectedresults: |
| 38 |
1. This should pass |
| 39 |
2. This should pass |
| 40 |
3. This should pass |
| 41 |
4. This should fail |
| 42 |
5. This should fail |
| 43 |
""" |
| 44 |
|
| 45 |
inst = topology_st.standalone |
| 46 |
whoami = WhoamiPlugin(inst) |
| 47 |
# /tmp nowadays comes with noexec bit set on some systems |
| 48 |
# so instead let's write somewhere where dirsrv user has access |
| 49 |
tmp_dir = inst.get_bak_dir() |
| 50 |
plugin_dir = inst.get_plugin_dir() |
| 51 |
|
| 52 |
# Copy the library to our tmp directory |
| 53 |
try: |
| 54 |
shutil.copy('%s/libwhoami-plugin.so' % plugin_dir, tmp_dir) |
| 55 |
except IOError as e: |
| 56 |
log.fatal('Failed to copy %s/libwhoami-plugin.so to the tmp directory %s, error: %s' % ( |
| 57 |
plugin_dir, tmp_dir, e.strerror)) |
| 58 |
assert False |
| 59 |
|
| 60 |
# |
| 61 |
# Test adding valid plugin paths |
| 62 |
# |
| 63 |
# Try using the absolute path to the current library |
| 64 |
whoami.replace('nsslapd-pluginPath', '%s/libwhoami-plugin' % plugin_dir) |
| 65 |
|
| 66 |
# Try using new remote location |
| 67 |
# If SELinux is enabled, plugin can't be loaded as it's not labeled properly |
| 68 |
if selinux_present(): |
| 69 |
import selinux |
| 70 |
if selinux.is_selinux_enabled(): |
| 71 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 72 |
whoami.replace('nsslapd-pluginPath', '%s/libwhoami-plugin' % tmp_dir) |
| 73 |
# Label it with lib_t, so it can be executed |
| 74 |
# We can't use selinux.setfilecon() here, because py.test needs to have mac_admin capability |
| 75 |
# Instead we can call chcon directly: |
| 76 |
subprocess.check_call(['/usr/bin/chcon', '-t', 'lib_t', '%s/libwhoami-plugin.so' % tmp_dir]) |
| 77 |
# And try to change the path again |
| 78 |
whoami.replace('nsslapd-pluginPath', '%s/libwhoami-plugin' % tmp_dir) |
| 79 |
else: |
| 80 |
whoami.replace('nsslapd-pluginPath', '%s/libwhoami-plugin' % tmp_dir) |
| 81 |
|
| 82 |
# Set plugin path back to the default |
| 83 |
whoami.replace('nsslapd-pluginPath', 'libwhoami-plugin') |
| 84 |
|
| 85 |
# |
| 86 |
# Test invalid path (no library present) |
| 87 |
# |
| 88 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 89 |
whoami.replace('nsslapd-pluginPath', '/bin/libwhoami-plugin') |
| 90 |
# No exception?! This is an error |
| 91 |
log.error('Invalid plugin path was incorrectly accepted by the server!') |
| 92 |
|
| 93 |
# |
| 94 |
# Test invalid relative path (no library present) |
| 95 |
# |
| 96 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 97 |
whoami.replace('nsslapd-pluginPath', '../libwhoami-plugin') |
| 98 |
# No exception?! This is an error |
| 99 |
log.error('Invalid plugin path was incorrectly accepted by the server!') |
| 100 |
|
| 101 |
log.info('Test complete') |
| 102 |
|
| 103 |
|
| 104 |
if __name__ == '__main__': |
| 105 |
# Run isolated |
| 106 |
# -s for DEBUG mode |
| 107 |
CURRENT_FILE = os.path.realpath(__file__) |
| 108 |
pytest.main("-s %s" % CURRENT_FILE) |
| 109 |
|