2015-07-24 10:07:13 +02:00
|
|
|
import unittest
|
|
|
|
import sys
|
2013-10-11 14:36:01 +02:00
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
2015-07-24 10:07:13 +02:00
|
|
|
object_list = ['login', 'user', 'port', 'module', 'interface', 'node', 'fcontext', 'boolean', 'permissive', "dontaudit"]
|
|
|
|
|
2013-10-11 14:36:01 +02:00
|
|
|
|
|
|
|
class SemanageTests(unittest.TestCase):
|
2015-07-24 10:07:13 +02:00
|
|
|
|
2013-10-11 14:36:01 +02:00
|
|
|
def assertDenied(self, err):
|
|
|
|
self.assertTrue('Permission denied' in err,
|
2015-07-24 10:07:13 +02:00
|
|
|
'"Permission denied" not found in %r' % err)
|
|
|
|
|
2013-10-11 14:36:01 +02:00
|
|
|
def assertNotFound(self, err):
|
|
|
|
self.assertTrue('not found' in err,
|
2015-07-24 10:07:13 +02:00
|
|
|
'"not found" not found in %r' % err)
|
2013-10-11 14:36:01 +02:00
|
|
|
|
|
|
|
def assertFailure(self, status):
|
|
|
|
self.assertTrue(status != 0,
|
2015-07-24 10:07:13 +02:00
|
|
|
'"semanage succeeded when it should have failed')
|
2013-10-11 14:36:01 +02:00
|
|
|
|
|
|
|
def assertSuccess(self, status, err):
|
|
|
|
self.assertTrue(status == 0,
|
2015-07-24 10:07:13 +02:00
|
|
|
'"semanage should have succeeded for this test %r' % err)
|
2013-10-11 14:36:01 +02:00
|
|
|
|
|
|
|
def test_extract(self):
|
|
|
|
for object in object_list:
|
2015-07-24 10:07:13 +02:00
|
|
|
if object in ["dontaudit", "module", "permissive"]:
|
2013-10-11 14:36:01 +02:00
|
|
|
continue
|
|
|
|
"Verify semanage %s -E" % object
|
2015-07-24 10:07:13 +02:00
|
|
|
p = Popen(['semanage', object, '-E'], stdout=PIPE)
|
2013-10-11 14:36:01 +02:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
|
|
|
|
def test_input_output(self):
|
|
|
|
print("Verify semanage export -f /tmp/out")
|
2015-07-24 10:07:13 +02:00
|
|
|
p = Popen(['semanage', "export", '-f', '/tmp/out'], stdout=PIPE)
|
2013-10-11 14:36:01 +02:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage export -S targeted -f -")
|
2015-07-24 10:07:13 +02:00
|
|
|
p = Popen(["semanage", "export", "-S", "targeted", "-f", "-"], stdout=PIPE)
|
2013-10-11 14:36:01 +02:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage -S targeted -o -")
|
2015-07-24 10:07:13 +02:00
|
|
|
p = Popen(["semanage", "-S", "targeted", "-o", "-"], stdout=PIPE)
|
2013-10-11 14:36:01 +02:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage import -f /tmp/out")
|
2015-07-24 10:07:13 +02:00
|
|
|
p = Popen(['semanage', "import", '-f', '/tmp/out'], stdout=PIPE)
|
2013-10-11 14:36:01 +02:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage import -S targeted -f /tmp/out")
|
2015-07-24 10:07:13 +02:00
|
|
|
p = Popen(["semanage", "import", "-S", "targeted", "-f", "/tmp/out"], stdout=PIPE)
|
2013-10-11 14:36:01 +02:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage -S targeted -i /tmp/out")
|
2015-07-24 10:07:13 +02:00
|
|
|
p = Popen(["semanage", "-S", "targeted", "-i", "/tmp/out"], stdout=PIPE)
|
2013-10-11 14:36:01 +02:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
|
|
|
|
def test_list(self):
|
|
|
|
for object in object_list:
|
|
|
|
if object in ["dontaudit"]:
|
|
|
|
continue
|
|
|
|
"Verify semanage %s -l" % object
|
2015-07-24 10:07:13 +02:00
|
|
|
p = Popen(['semanage', object, '-l'], stdout=PIPE)
|
2013-10-11 14:36:01 +02:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
|
|
|
|
def test_list_c(self):
|
|
|
|
for object in object_list:
|
|
|
|
if object in ["module", "permissive", "dontaudit"]:
|
|
|
|
continue
|
|
|
|
print("Verify semanage %s -l" % object)
|
2015-07-24 10:07:13 +02:00
|
|
|
p = Popen(['semanage', object, '-lC'], stdout=PIPE)
|
2013-10-11 14:36:01 +02:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
|
|
|
|
def test_fcontext(self):
|
2015-07-24 10:07:13 +02:00
|
|
|
p = Popen(["semanage", "fcontext", "-d", "/ha-web(/.*)?"], stderr=PIPE)
|
|
|
|
out, err = p.communicate()
|
2013-10-11 14:36:01 +02:00
|
|
|
|
2015-07-24 10:07:13 +02:00
|
|
|
print("Verify semanage fcontext -a")
|
|
|
|
p = Popen(["semanage", "fcontext", "-a", "-t", "httpd_sys_content_t", "/ha-web(/.*)?"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage fcontext -m")
|
|
|
|
p = Popen(["semanage", "fcontext", "-m", "-t", "default_t", "/ha-web(/.*)?"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage fcontext -d")
|
|
|
|
p = Popen(["semanage", "fcontext", "-d", "/ha-web(/.*)?"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
2013-10-11 14:36:01 +02:00
|
|
|
|
|
|
|
def test_fcontext_e(self):
|
2015-07-24 10:07:13 +02:00
|
|
|
p = Popen(["semanage", "fcontext", "-d", "/myhome"], stderr=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
p = Popen(["semanage", "fcontext", "-d", "/myhome1"], stderr=PIPE)
|
|
|
|
out, err = p.communicate()
|
2013-10-11 14:36:01 +02:00
|
|
|
|
2015-07-24 10:07:13 +02:00
|
|
|
print("Verify semanage fcontext -a -e")
|
|
|
|
p = Popen(["semanage", "fcontext", "-a", "-e", "/home", "/myhome"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage fcontext -m -e")
|
|
|
|
p = Popen(["semanage", "fcontext", "-a", "-e", "/home", "/myhome1"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage fcontext -d -e")
|
|
|
|
p = Popen(["semanage", "fcontext", "-d", "/myhome1"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
2013-10-11 14:36:01 +02:00
|
|
|
|
|
|
|
def test_port(self):
|
2015-07-24 10:07:13 +02:00
|
|
|
# Cleanup
|
|
|
|
p = Popen(["semanage", "port", "-d", "-p", "tcp", "55"], stdout=PIPE, stderr=PIPE)
|
|
|
|
out, err = p.communicate()
|
2013-10-11 14:36:01 +02:00
|
|
|
|
2015-07-24 10:07:13 +02:00
|
|
|
# test
|
|
|
|
print("Verify semanage port -a")
|
|
|
|
p = Popen(["semanage", "port", "-a", "-t", "ssh_port_t", "-p", "tcp", "55"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage port -m")
|
|
|
|
p = Popen(["semanage", "port", "-m", "-t", "http_port_t", "-p", "tcp", "55"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage port -d")
|
|
|
|
p = Popen(["semanage", "port", "-d", "-p", "tcp", "55"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
2013-10-11 14:36:01 +02:00
|
|
|
|
|
|
|
def test_login(self):
|
2015-07-24 10:07:13 +02:00
|
|
|
# Cleanup
|
|
|
|
p = Popen(["userdel", "-f", "-r", "testlogin"], stderr=PIPE, stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
p = Popen(["semanage", "user", "-d", "testuser_u"], stderr=PIPE, stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
p = Popen(["semanage", "login", "-d", "testlogin"], stderr=PIPE, stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
2013-10-11 14:36:01 +02:00
|
|
|
|
2015-07-24 10:07:13 +02:00
|
|
|
#test
|
|
|
|
print("Verify semanage user -a")
|
|
|
|
p = Popen(["semanage", "user", "-a", "-R", "staff_r", "-r", "s0-s0:c0.c1023", "testuser_u"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify useradd ")
|
|
|
|
p = Popen(["useradd", "testlogin"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage login -a")
|
|
|
|
p = Popen(["semanage", "login", "-a", "-s", "testuser_u", "testlogin"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage login -m -r")
|
|
|
|
p = Popen(["semanage", "login", "-m", "-r", "s0-s0:c1", "testlogin"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage login -m -s")
|
|
|
|
p = Popen(["semanage", "login", "-m", "-s", "staff_u", "testlogin"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage login -m -s -r")
|
|
|
|
p = Popen(["semanage", "login", "-m", "-s", "testuser_u", "-r", "s0", "testlogin"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage login -d")
|
|
|
|
p = Popen(["semanage", "login", "-d", "testlogin"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
print("Verify userdel ")
|
|
|
|
p = Popen(["userdel", "-f", "-r", "testlogin"], stderr=PIPE, stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage user -d")
|
|
|
|
p = Popen(["semanage", "user", "-d", "testuser_u"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
2013-10-11 14:36:01 +02:00
|
|
|
|
|
|
|
def test_user(self):
|
2015-07-24 10:07:13 +02:00
|
|
|
# Cleanup
|
|
|
|
p = Popen(["semanage", "user", "-d", "testuser_u"], stderr=PIPE, stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
2013-10-11 14:36:01 +02:00
|
|
|
|
2015-07-24 10:07:13 +02:00
|
|
|
# test
|
|
|
|
print("Verify semanage user -a")
|
|
|
|
p = Popen(["semanage", "user", "-a", "-R", "staff_r", "-r", "s0-s0:c0.c1023", "testuser_u"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage user -m -R")
|
|
|
|
p = Popen(["semanage", "user", "-m", "-R", "sysadm_r unconfined_r", "testuser_u"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage user -m -r")
|
|
|
|
p = Popen(["semanage", "user", "-m", "-r", "s0-s0:c1", "testuser_u"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage user -d")
|
|
|
|
p = Popen(["semanage", "user", "-d", "testuser_u"], stdout=PIPE)
|
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
2013-10-11 14:36:01 +02:00
|
|
|
|
|
|
|
def test_boolean(self):
|
|
|
|
import selinux
|
2015-07-24 10:07:13 +02:00
|
|
|
boolean_status = {0: "--off", 1: "--on"}
|
|
|
|
boolean_state = selinux.security_get_boolean_active("httpd_anon_write")
|
2013-10-11 14:36:01 +02:00
|
|
|
# Test
|
|
|
|
print("Verify semanage boolean -m %s httpd_anon_write" % boolean_status[not boolean_state])
|
2015-07-24 10:07:13 +02:00
|
|
|
p = Popen(["semanage", "boolean", "-m", boolean_status[(not boolean_state)], "httpd_anon_write"], stdout=PIPE)
|
2013-10-11 14:36:01 +02:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
print("Verify semanage boolean -m %s httpd_anon_write" % boolean_status[boolean_state])
|
2015-07-24 10:07:13 +02:00
|
|
|
p = Popen(["semanage", "boolean", "-m", boolean_status[boolean_state], "httpd_anon_write"], stdout=PIPE)
|
2013-10-11 14:36:01 +02:00
|
|
|
out, err = p.communicate()
|
|
|
|
self.assertSuccess(p.returncode, err)
|
|
|
|
|
2015-07-24 10:07:13 +02:00
|
|
|
|
2013-10-11 14:36:01 +02:00
|
|
|
def semanage_suite():
|
|
|
|
semanage_suite = unittest.TestSuite()
|
|
|
|
semanage_suite.addTest(unittest.makeSuite(SemanageTests))
|
|
|
|
|
|
|
|
return semanage_suite
|
|
|
|
|
2015-07-24 10:07:13 +02:00
|
|
|
|
2013-10-11 14:36:01 +02:00
|
|
|
def semanage_custom_suite(test_list):
|
2015-07-24 10:07:13 +02:00
|
|
|
suiteSemanage = unittest.TestSuite()
|
2013-10-11 14:36:01 +02:00
|
|
|
for t in test_list:
|
|
|
|
suiteSemanage.addTest(SemanageTests(t))
|
|
|
|
|
|
|
|
return suiteSemanage
|
|
|
|
|
2015-07-24 10:07:13 +02:00
|
|
|
|
2013-10-11 14:36:01 +02:00
|
|
|
def semanage_run_test(suite):
|
|
|
|
unittest.TextTestRunner(verbosity=2).run(suite)
|
|
|
|
|
2015-07-24 10:07:13 +02:00
|
|
|
|
2013-10-11 14:36:01 +02:00
|
|
|
class CheckTest(argparse.Action):
|
2015-07-24 10:07:13 +02:00
|
|
|
|
2013-10-11 14:36:01 +02:00
|
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
|
|
|
newval = getattr(namespace, self.dest)
|
|
|
|
if not newval:
|
|
|
|
newval = []
|
|
|
|
for v in values:
|
|
|
|
if v not in semanage_test_list:
|
|
|
|
raise ValueError("%s must be an unit test.\nValid tests: %s" % (v, ", ".join(semanage_test_list)))
|
|
|
|
newval.append(v)
|
|
|
|
setattr(namespace, self.dest, newval)
|
|
|
|
|
2015-07-24 10:07:13 +02:00
|
|
|
|
2013-10-11 14:36:01 +02:00
|
|
|
def semanage_args(args):
|
|
|
|
if args.list:
|
|
|
|
print("You can run the following tests:")
|
|
|
|
for i in semanage_test_list:
|
|
|
|
print(i)
|
|
|
|
if args.all:
|
|
|
|
semanage_run_test(semanage_suite())
|
|
|
|
if args.test:
|
|
|
|
semanage_run_test(semanage_custom_suite(args.test))
|
|
|
|
|
2015-07-24 10:07:13 +02:00
|
|
|
|
2013-10-11 14:36:01 +02:00
|
|
|
def gen_semanage_test_args(parser):
|
|
|
|
group = parser.add_mutually_exclusive_group(required=True)
|
|
|
|
group.add_argument('-a', "--all", dest="all", default=False,
|
2015-07-24 10:07:13 +02:00
|
|
|
action="store_true",
|
|
|
|
help=("Run all semanage unit tests"))
|
2013-10-11 14:36:01 +02:00
|
|
|
group.add_argument('-l', "--list", dest="list", default=False,
|
2015-07-24 10:07:13 +02:00
|
|
|
action="store_true",
|
|
|
|
help=("List all semanage unit tests"))
|
2013-10-11 14:36:01 +02:00
|
|
|
group.add_argument('-t', "--test", dest="test", default=[],
|
2015-07-24 10:07:13 +02:00
|
|
|
action=CheckTest, nargs="*",
|
|
|
|
help=("Run selected semanage unit test(s)"))
|
2013-10-11 14:36:01 +02:00
|
|
|
group.set_defaults(func=semanage_args)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import selinux
|
2016-08-04 20:33:54 +02:00
|
|
|
semanage_test_list = [x for x in dir(SemanageTests) if x.startswith("test_")]
|
2013-10-11 14:36:01 +02:00
|
|
|
if selinux.security_getenforce() == 1:
|
|
|
|
parser = argparse.ArgumentParser(description='Semanage unit test script')
|
|
|
|
gen_semanage_test_args(parser)
|
|
|
|
try:
|
|
|
|
args = parser.parse_args()
|
|
|
|
args.func(args)
|
|
|
|
sys.exit(0)
|
2015-07-21 02:38:21 +02:00
|
|
|
except ValueError as e:
|
2013-10-11 14:36:01 +02:00
|
|
|
sys.stderr.write("%s: %s\n" % (e.__class__.__name__, str(e)))
|
|
|
|
sys.exit(1)
|
2015-07-21 02:38:21 +02:00
|
|
|
except IOError as e:
|
2013-10-11 14:36:01 +02:00
|
|
|
sys.stderr.write("%s: %s\n" % (e.__class__.__name__, str(e)))
|
|
|
|
sys.exit(1)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
print("SELinux must be in enforcing mode for this test")
|