Esempio n. 1
0
	def test_writenode(self,sh):
		print "Testing network node write..."

		(status, node) = semanage.semanage_node_create(sh)
		if status < 0:
			raise Error("Could not create SENode object")
		if self.verbose: print "SENode object created."

		status = semanage.semanage_node_set_addr(sh, node, semanage.SEMANAGE_PROTO_IP6, "ffee:dddd::bbbb")
		if status < 0:
			raise Error("Could not set SENode address")
	
		status = semanage.semanage_node_set_mask(sh, node, semanage.SEMANAGE_PROTO_IP6, "::ffff:ffff:abcd:0000")
		if status < 0:
			raise Error("Could not set SENode netmask")

		semanage.semanage_node_set_proto(node, semanage.SEMANAGE_PROTO_IP6);
		if self.verbose: print "SENode protocol set: ", \
			semanage.semanage_node_get_proto_str(semanage.SEMANAGE_PROTO_IP6)
		
		(status, con) = semanage.semanage_context_create(sh)
		if status < 0:
			raise Error("Could not create SEContext object")
		if self.verbose: print "SEContext object created (for node)."
		
		status = semanage.semanage_context_set_user(sh, con, "system_u")
		if status < 0:
			raise Error("Could not set context user")
		if self.verbose: print "SEContext user: "******"object_r")
		if status < 0:
			raise Error("Could not set context role")
		if self.verbose: print "SEContext role: ", semanage.semanage_context_get_role(con)
		
		status = semanage.semanage_context_set_type(sh, con, "lo_node_t")
		if status < 0:
			raise Error("Could not set context type")
		if self.verbose: print "SEContext type: ", semanage.semanage_context_get_type(con)

		status = semanage.semanage_context_set_mls(sh, con, "s0:c0.c255")
		if status < 0:
			raise Error("Could not set context MLS fields")
		if self.verbose: print "SEContext mls: ", semanage.semanage_context_get_mls(con)

		status = semanage.semanage_node_set_con(sh, node, con)
		if status < 0:
			raise Error("Could not set SENode context")
		if self.verbose: print "SENode context set: ", con

                (status,key) = semanage.semanage_node_key_extract(sh, node)
		if status < 0:
			raise Error("Could not extract SENode key")
		if self.verbose: print "SENode key extracted: ", key

		(status,exists) = semanage.semanage_node_exists_local(sh,key)
		if status < 0:
			raise Error("Could not check if SENode exists")
		if self.verbose: print "Exists status (commit number): ", status

		if exists:
			(status, old_node) = semanage.semanage_node_query_local(sh, key)
			if status < 0:
				raise Error("Could not query old SENode")
			if self.verbose: print "Query status (commit number): ", status

		print "Starting transaction..."
		status = semanage.semanage_begin_transaction(sh)
		if status < 0:
			raise Error("Could not start semanage transaction")	

		status = semanage.semanage_node_modify_local(sh,key, node)
		if status < 0:
			raise Error("Could not modify SENode")

		status = semanage.semanage_commit(sh)
		if status < 0:
			raise Error("Could not commit test transaction")
		print "Commit status (transaction number): ", status

		status = semanage.semanage_begin_transaction(sh)
		if status < 0:
			raise Error("Could not start semanage transaction")

		if not exists:
			print "Removing network node..."
                        status = semanage.semanage_node_del_local(sh, key)
			if status < 0:
				raise Error("Could not delete test SENode")
                        if self.verbose: print "Network node delete: ", status
                else:
			print "Resetting network node..."
			status = semanage.semanage_node_modify_local(sh, key, old_node)
			if status < 0:
				raise Error("Could not reset test SENode")
			if self.verbose: print "Network node modify: ", status

		status = semanage.semanage_commit(sh)
		if status < 0:
			raise Error("Could not commit reset transaction")
		print "Commit status (transaction number): ", status

		semanage.semanage_context_free(con)
		semanage.semanage_node_key_free(key)
		semanage.semanage_node_free(node)
		if exists: semanage.semanage_node_free(old_node)
Esempio n. 2
0
	def test_writefcontext(self,sh):
		print "Testing file context write..."

		(status, fcon) = semanage.semanage_fcontext_create(sh)
		if status < 0:
			raise Error("Could not create SEFcontext object")
		if self.verbose: print "SEFcontext object created."
	
		status = semanage.semanage_fcontext_set_expr(sh, fcon, "/test/fcontext(/.*)?")
		if status < 0:
			raise Error("Could not set expression")
		if self.verbose: print "SEFContext expr set: ", semanage.semanage_fcontext_get_expr(fcon)

		semanage.semanage_fcontext_set_type(fcon, semanage.SEMANAGE_FCONTEXT_REG)
		if self.verbose:
			ftype = semanage.semanage_fcontext_get_type(fcon)
			print "SEFContext type set: ", semanage.semanage_fcontext_get_type_str(ftype)

		(status, con) = semanage.semanage_context_create(sh)
		if status < 0:
			raise Error("Could not create SEContext object")
		if self.verbose: print "SEContext object created (for file context)."

		status = semanage.semanage_context_set_user(sh, con, "system_u")
		if status < 0:
			raise Error("Could not set context user")
		if self.verbose: print "SEContext user: "******"object_r")
		if status < 0:
			raise Error("Could not set context role")
		if self.verbose: print "SEContext role: ", semanage.semanage_context_get_role(con)

		status = semanage.semanage_context_set_type(sh, con, "default_t")
		if status < 0:
			raise Error("Could not set context type")
		if self.verbose: print "SEContext type: ", semanage.semanage_context_get_type(con)

		status = semanage.semanage_context_set_mls(sh, con, "s0:c0.c255")
		if status < 0:
			raise Error("Could not set context MLS fields")
		if self.verbose: print "SEContext mls: ", semanage.semanage_context_get_mls(con)

		status = semanage.semanage_fcontext_set_con(sh, fcon, con)
		if status < 0:
			raise Error("Could not set SEFcontext context")
		if self.verbose: print "SEFcontext context set: ", con

		(status,key) = semanage.semanage_fcontext_key_extract(sh,fcon)
		if status < 0:
			raise Error("Could not extract SEFcontext key")
		if self.verbose: print "SEFcontext key extracted: ", key

		(status,exists) = semanage.semanage_fcontext_exists_local(sh,key)
		if status < 0:
			raise Error("Could not check if SEFcontext exists")

		if self.verbose: print "Exists status (commit number): ", status
		if exists:
			(status, old_fcontext) = semanage.semanage_fcontext_query_local(sh, key)
			if status < 0:
				raise Error("Could not query old SEFcontext")
			if self.verbose: print "Query status (commit number): ", status

		print "Starting transaction..."
		status = semanage.semanage_begin_transaction(sh)
		if status < 0:
			raise Error("Could not start semanage transaction")

		status = semanage.semanage_fcontext_modify_local(sh,key,fcon)
		if status < 0:
			raise Error("Could not modify SEFcontext")

		status = semanage.semanage_commit(sh)
		if status < 0:
			raise Error("Could not commit test transaction")
		print "Commit status (transaction number): ", status

		status = semanage.semanage_begin_transaction(sh)
		if status < 0:
			raise Error("Could not start semanage transaction")

		if not exists:
			print "Removing file context..."
			status = semanage.semanage_fcontext_del_local(sh, key)
			if status < 0:
				raise Error("Could not delete test SEFcontext")
			if self.verbose: print "File context delete: ", status
		else:
			print "Resetting file context..."
			status = semanage.semanage_fcontext_modify_local(sh, key, old_fcontext)
			if status < 0:
				raise Error("Could not reset test FContext")
			if self.verbose: print "File context modify: ", status

		status = semanage.semanage_commit(sh)
		if status < 0:
			raise Error("Could not commit reset transaction")
		print "Commit status (transaction number): ", status

		semanage.semanage_context_free(con)	
		semanage.semanage_fcontext_key_free(key)
		semanage.semanage_fcontext_free(fcon)
		if exists: semanage.semanage_fcontext_free(old_fcontext)
Esempio n. 3
0
	def test_writeinterface(self,sh):
		print "Testing network interface write..."

		(status, iface) = semanage.semanage_iface_create(sh)
		if status < 0:
			raise Error("Could not create SEIface object")	
		if self.verbose: print "SEIface object created."

		status = semanage.semanage_iface_set_name(sh, iface, "test_iface")
		if status < 0:
			raise Error("Could not set SEIface name")
		if self.verbose: print "SEIface name set: ", semanage.semanage_iface_get_name(iface)	

		(status, con) = semanage.semanage_context_create(sh)
		if status < 0:
			raise Error("Could not create SEContext object")
		if self.verbose: print "SEContext object created (for network interface)"

		status = semanage.semanage_context_set_user(sh, con, "system_u")
		if status < 0:
			raise Error("Could not set interface context user")
		if self.verbose: print "SEContext user: "******"object_r")
		if status < 0:
			raise Error("Could not set interface context role")
		if self.verbose: print "SEContext role: ", semanage.semanage_context_get_role(con)

		status = semanage.semanage_context_set_type(sh, con, "default_t")
		if status < 0:
			raise Error("Could not set interface context type")
		if self.verbose: print "SEContext type: ", semanage.semanage_context_get_type(con)

		status = semanage.semanage_context_set_mls(sh, con, "s0:c0.c255")
		if status < 0:
			raise Error("Could not set interface context MLS fields")
		if self.verbose: print "SEContext mls: ", semanage.semanage_context_get_mls(con)

		status = semanage.semanage_iface_set_ifcon(sh, iface, con)
		if status < 0:
			raise Error("Could not set SEIface interface context")
		if self.verbose: print "SEIface interface context set: ", con

		status = semanage.semanage_iface_set_msgcon(sh, iface, con)
		if status < 0:
			raise Error("Could not set SEIface message context")
		if self.verbose: print "SEIface message context set: ", con

		(status,key) = semanage.semanage_iface_key_extract(sh,iface)
		if status < 0:
			raise Error("Could not extract SEIface key")
		if self.verbose: print "SEIface key extracted: ", key

		(status,exists) = semanage.semanage_iface_exists_local(sh,key)
		if status < 0:
			raise Error("Could not check if SEIface exists")
		if self.verbose: print "Exists status (commit number): ", status

		if exists:
			(status, old_iface) = semanage.semanage_iface_query_local(sh, key)
			if status < 0:
				raise Error("Could not query old SEIface")
			if self.verbose: print "Query status (commit number): ", status

		print "Starting transaction..."
		status = semanage.semanage_begin_transaction(sh)
		if status < 0:
			raise Error("Could not begin semanage transaction")

		status = semanage.semanage_iface_modify_local(sh,key,iface)
		if status < 0:
			raise Error("Could not modify SEIface")

		status = semanage.semanage_commit(sh)
		if status < 0:
			raise Error("Could not commit test transaction")
		print "Commit status (transaction number): ", status

		status = semanage.semanage_begin_transaction(sh)
		if status < 0:
			raise Error("Could not begin semanage transaction")

		if not exists:
			print "Removing interface..."
			status = semanage.semanage_iface_del_local(sh, key)
			if status < 0:
				raise Error("Could not delete test SEIface")
			if self.verbose: print "Interface delete: ", status
		else:
			print "Resetting interface..."
			status = semanage.semanage_iface_modify_local(sh, key, old_iface)
			if status < 0:
				raise Error("Could not reset test SEIface")
			if self.verbose: print "Interface modify: ", status

		status = semanage.semanage_commit(sh)
		if status < 0:
			raise Error("Could not commit reset transaction")
		print "Commit status (transaction number): ", status

		semanage.semanage_context_free(con)
		semanage.semanage_iface_key_free(key)
		semanage.semanage_iface_free(iface)
		if exists: semanage.semanage_iface_free(old_iface)
Esempio n. 4
0
	def test_writeport(self,sh):
		print "Testing port write..."

		(status, port) = semanage.semanage_port_create(sh)
		if status < 0:
			raise Error("Could not create SEPort object")
		if self.verbose: print "SEPort object created."

		semanage.semanage_port_set_range(port,150,200)
		low = semanage.semanage_port_get_low(port)
		high = semanage.semanage_port_get_high(port)
		if self.verbose: print "SEPort range set: ", low, "-", high
		
		semanage.semanage_port_set_proto(port, semanage.SEMANAGE_PROTO_TCP);
		if self.verbose: print "SEPort protocol set: ", \
			semanage.semanage_port_get_proto_str(semanage.SEMANAGE_PROTO_TCP)
		
		(status, con) = semanage.semanage_context_create(sh)
		if status < 0:
			raise Error("Could not create SEContext object")
		if self.verbose: print "SEContext object created (for port)."
		
		status = semanage.semanage_context_set_user(sh, con, "system_u")
		if status < 0:
			raise Error("Could not set context user")
		if self.verbose: print "SEContext user: "******"object_r")
		if status < 0:
			raise Error("Could not set context role")
		if self.verbose: print "SEContext role: ", semanage.semanage_context_get_role(con)
		
		status = semanage.semanage_context_set_type(sh, con, "http_port_t")
		if status < 0:
			raise Error("Could not set context type")
		if self.verbose: print "SEContext type: ", semanage.semanage_context_get_type(con)

		status = semanage.semanage_context_set_mls(sh, con, "s0:c0.c255")
		if status < 0:
			raise Error("Could not set context MLS fields")
		if self.verbose: print "SEContext mls: ", semanage.semanage_context_get_mls(con)

		status = semanage.semanage_port_set_con(sh, port, con)
		if status < 0:
			raise Error("Could not set SEPort context")
		if self.verbose: print "SEPort context set: ", con

                (status,key) = semanage.semanage_port_key_extract(sh,port)
		if status < 0:
			raise Error("Could not extract SEPort key")
		if self.verbose: print "SEPort key extracted: ", key

		(status,exists) = semanage.semanage_port_exists_local(sh,key)
		if status < 0:
			raise Error("Could not check if SEPort exists")
		if self.verbose: print "Exists status (commit number): ", status

		if exists:
			(status, old_port) = semanage.semanage_port_query_local(sh, key)
			if status < 0:
				raise Error("Could not query old SEPort")
			if self.verbose: print "Query status (commit number): ", status

		print "Starting transaction..."
		status = semanage.semanage_begin_transaction(sh)
		if status < 0:
			raise Error("Could not start semanage transaction")	

		status = semanage.semanage_port_modify_local(sh,key,port)
		if status < 0:
			raise Error("Could not modify SEPort")

		status = semanage.semanage_commit(sh)
		if status < 0:
			raise Error("Could not commit test transaction")
		print "Commit status (transaction number): ", status

		status = semanage.semanage_begin_transaction(sh)
		if status < 0:
			raise Error("Could not start semanage transaction")

		if not exists:
			print "Removing port range..."
                        status = semanage.semanage_port_del_local(sh, key)
			if status < 0:
				raise Error("Could not delete test SEPort")
                        if self.verbose: print "Port range delete: ", status
                else:
			print "Resetting port range..."
			status = semanage.semanage_port_modify_local(sh, key, old_port)
			if status < 0:
				raise Error("Could not reset test SEPort")
			if self.verbose: print "Port range modify: ", status

		status = semanage.semanage_commit(sh)
		if status < 0:
			raise Error("Could not commit reset transaction")
		print "Commit status (transaction number): ", status

		semanage.semanage_context_free(con)
		semanage.semanage_port_key_free(key)
		semanage.semanage_port_free(port)
		if exists: semanage.semanage_port_free(old_port)
Esempio n. 5
0
    def test_writeinterface(self, sh):
        print("Testing network interface write...")

        (status, iface) = semanage.semanage_iface_create(sh)
        if status < 0:
            raise Error("Could not create SEIface object")
        if self.verbose:
            print("SEIface object created.")

        status = semanage.semanage_iface_set_name(sh, iface, "test_iface")
        if status < 0:
            raise Error("Could not set SEIface name")
        if self.verbose:
            print("SEIface name set: %s" %
                  semanage.semanage_iface_get_name(iface))

        (status, con) = semanage.semanage_context_create(sh)
        if status < 0:
            raise Error("Could not create SEContext object")
        if self.verbose:
            print("SEContext object created (for network interface)")

        status = semanage.semanage_context_set_user(sh, con, "system_u")
        if status < 0:
            raise Error("Could not set interface context user")
        if self.verbose:
            print("SEContext user: %s" %
                  semanage.semanage_context_get_user(con))

        status = semanage.semanage_context_set_role(sh, con, "object_r")
        if status < 0:
            raise Error("Could not set interface context role")
        if self.verbose:
            print("SEContext role: %s" %
                  semanage.semanage_context_get_role(con))

        status = semanage.semanage_context_set_type(sh, con, "default_t")
        if status < 0:
            raise Error("Could not set interface context type")
        if self.verbose:
            print("SEContext type: %s" %
                  semanage.semanage_context_get_type(con))

        status = semanage.semanage_context_set_mls(sh, con, "s0:c0.c255")
        if status < 0:
            raise Error("Could not set interface context MLS fields")
        if self.verbose:
            print("SEContext mls: %s" % semanage.semanage_context_get_mls(con))

        status = semanage.semanage_iface_set_ifcon(sh, iface, con)
        if status < 0:
            raise Error("Could not set SEIface interface context")
        if self.verbose:
            print("SEIface interface context set: %s" % con)

        status = semanage.semanage_iface_set_msgcon(sh, iface, con)
        if status < 0:
            raise Error("Could not set SEIface message context")
        if self.verbose:
            print("SEIface message context set: %s" % con)

        (status, key) = semanage.semanage_iface_key_extract(sh, iface)
        if status < 0:
            raise Error("Could not extract SEIface key")
        if self.verbose:
            print("SEIface key extracted: %s" % key)

        (status, exists) = semanage.semanage_iface_exists_local(sh, key)
        if status < 0:
            raise Error("Could not check if SEIface exists")
        if self.verbose:
            print("Exists status (commit number): %s" % status)

        if exists:
            (status, old_iface) = semanage.semanage_iface_query_local(sh, key)
            if status < 0:
                raise Error("Could not query old SEIface")
            if self.verbose:
                print("Query status (commit number): %s" % status)

        print("Starting transaction...")
        status = semanage.semanage_begin_transaction(sh)
        if status < 0:
            raise Error("Could not begin semanage transaction")

        status = semanage.semanage_iface_modify_local(sh, key, iface)
        if status < 0:
            raise Error("Could not modify SEIface")

        status = semanage.semanage_commit(sh)
        if status < 0:
            raise Error("Could not commit test transaction")
        print("Commit status (transaction number): %s" % status)

        status = semanage.semanage_begin_transaction(sh)
        if status < 0:
            raise Error("Could not begin semanage transaction")

        if not exists:
            print("Removing interface...")
            status = semanage.semanage_iface_del_local(sh, key)
            if status < 0:
                raise Error("Could not delete test SEIface")
            if self.verbose:
                print("Interface delete: %s" % status)
        else:
            print("Resetting interface...")
            status = semanage.semanage_iface_modify_local(sh, key, old_iface)
            if status < 0:
                raise Error("Could not reset test SEIface")
            if self.verbose:
                print("Interface modify: %s" % status)

        status = semanage.semanage_commit(sh)
        if status < 0:
            raise Error("Could not commit reset transaction")
        print("Commit status (transaction number): %s" % status)

        semanage.semanage_context_free(con)
        semanage.semanage_iface_key_free(key)
        semanage.semanage_iface_free(iface)
        if exists:
            semanage.semanage_iface_free(old_iface)
Esempio n. 6
0
    def test_writefcontext(self, sh):
        print("Testing file context write...")

        (status, fcon) = semanage.semanage_fcontext_create(sh)
        if status < 0:
            raise Error("Could not create SEFcontext object")
        if self.verbose:
            print("SEFcontext object created.")

        status = semanage.semanage_fcontext_set_expr(sh, fcon,
                                                     "/test/fcontext(/.*)?")
        if status < 0:
            raise Error("Could not set expression")
        if self.verbose:
            print("SEFContext expr set: %s" %
                  semanage.semanage_fcontext_get_expr(fcon))

        semanage.semanage_fcontext_set_type(fcon,
                                            semanage.SEMANAGE_FCONTEXT_REG)
        if self.verbose:
            ftype = semanage.semanage_fcontext_get_type(fcon)
            print("SEFContext type set: %s" %
                  semanage.semanage_fcontext_get_type_str(ftype))

        (status, con) = semanage.semanage_context_create(sh)
        if status < 0:
            raise Error("Could not create SEContext object")
        if self.verbose:
            print("SEContext object created (for file context).")

        status = semanage.semanage_context_set_user(sh, con, "system_u")
        if status < 0:
            raise Error("Could not set context user")
        if self.verbose:
            print("SEContext user: %s" %
                  semanage.semanage_context_get_user(con))

        status = semanage.semanage_context_set_role(sh, con, "object_r")
        if status < 0:
            raise Error("Could not set context role")
        if self.verbose:
            print("SEContext role: %s" %
                  semanage.semanage_context_get_role(con))

        status = semanage.semanage_context_set_type(sh, con, "default_t")
        if status < 0:
            raise Error("Could not set context type")
        if self.verbose:
            print("SEContext type: %s" %
                  semanage.semanage_context_get_type(con))

        status = semanage.semanage_context_set_mls(sh, con, "s0:c0.c255")
        if status < 0:
            raise Error("Could not set context MLS fields")
        if self.verbose:
            print("SEContext mls: %s" % semanage.semanage_context_get_mls(con))

        status = semanage.semanage_fcontext_set_con(sh, fcon, con)
        if status < 0:
            raise Error("Could not set SEFcontext context")
        if self.verbose:
            print("SEFcontext context set: %s" % con)

        (status, key) = semanage.semanage_fcontext_key_extract(sh, fcon)
        if status < 0:
            raise Error("Could not extract SEFcontext key")
        if self.verbose:
            print("SEFcontext key extracted: %s" % key)

        (status, exists) = semanage.semanage_fcontext_exists_local(sh, key)
        if status < 0:
            raise Error("Could not check if SEFcontext exists")

        if self.verbose:
            print("Exists status (commit number): %s" % status)
        if exists:
            (status,
             old_fcontext) = semanage.semanage_fcontext_query_local(sh, key)
            if status < 0:
                raise Error("Could not query old SEFcontext")
            if self.verbose:
                print("Query status (commit number): %s" % status)

        print("Starting transaction...")
        status = semanage.semanage_begin_transaction(sh)
        if status < 0:
            raise Error("Could not start semanage transaction")

        status = semanage.semanage_fcontext_modify_local(sh, key, fcon)
        if status < 0:
            raise Error("Could not modify SEFcontext")

        status = semanage.semanage_commit(sh)
        if status < 0:
            raise Error("Could not commit test transaction")
        print("Commit status (transaction number): %s" % status)

        status = semanage.semanage_begin_transaction(sh)
        if status < 0:
            raise Error("Could not start semanage transaction")

        if not exists:
            print("Removing file context...")
            status = semanage.semanage_fcontext_del_local(sh, key)
            if status < 0:
                raise Error("Could not delete test SEFcontext")
            if self.verbose:
                print("File context delete: %s" % status)
        else:
            print("Resetting file context...")
            status = semanage.semanage_fcontext_modify_local(
                sh, key, old_fcontext)
            if status < 0:
                raise Error("Could not reset test FContext")
            if self.verbose:
                print("File context modify: %s" % status)

        status = semanage.semanage_commit(sh)
        if status < 0:
            raise Error("Could not commit reset transaction")
        print("Commit status (transaction number): %s" % status)

        semanage.semanage_context_free(con)
        semanage.semanage_fcontext_key_free(key)
        semanage.semanage_fcontext_free(fcon)
        if exists:
            semanage.semanage_fcontext_free(old_fcontext)
Esempio n. 7
0
    def test_writeport(self, sh):
        print("Testing port write...")

        (status, port) = semanage.semanage_port_create(sh)
        if status < 0:
            raise Error("Could not create SEPort object")
        if self.verbose:
            print("SEPort object created.")

        semanage.semanage_port_set_range(port, 150, 200)
        low = semanage.semanage_port_get_low(port)
        high = semanage.semanage_port_get_high(port)
        if self.verbose:
            print("SEPort range set: %s-%s" % (low, high))

        semanage.semanage_port_set_proto(port, semanage.SEMANAGE_PROTO_TCP)
        if self.verbose:
            print("SEPort protocol set: %s" %
                  semanage.semanage_port_get_proto_str(
                      semanage.SEMANAGE_PROTO_TCP))

        (status, con) = semanage.semanage_context_create(sh)
        if status < 0:
            raise Error("Could not create SEContext object")
        if self.verbose:
            print("SEContext object created (for port).")

        status = semanage.semanage_context_set_user(sh, con, "system_u")
        if status < 0:
            raise Error("Could not set context user")
        if self.verbose:
            print("SEContext user: %s" %
                  semanage.semanage_context_get_user(con))

        status = semanage.semanage_context_set_role(sh, con, "object_r")
        if status < 0:
            raise Error("Could not set context role")
        if self.verbose:
            print("SEContext role: %s" %
                  semanage.semanage_context_get_role(con))

        status = semanage.semanage_context_set_type(sh, con, "http_port_t")
        if status < 0:
            raise Error("Could not set context type")
        if self.verbose:
            print("SEContext type: %s" %
                  semanage.semanage_context_get_type(con))

        status = semanage.semanage_context_set_mls(sh, con, "s0:c0.c255")
        if status < 0:
            raise Error("Could not set context MLS fields")
        if self.verbose:
            print("SEContext mls: %s" % semanage.semanage_context_get_mls(con))

        status = semanage.semanage_port_set_con(sh, port, con)
        if status < 0:
            raise Error("Could not set SEPort context")
        if self.verbose:
            print("SEPort context set: %s" % con)

        (status, key) = semanage.semanage_port_key_extract(sh, port)
        if status < 0:
            raise Error("Could not extract SEPort key")
        if self.verbose:
            print("SEPort key extracted: %s" % key)

        (status, exists) = semanage.semanage_port_exists_local(sh, key)
        if status < 0:
            raise Error("Could not check if SEPort exists")
        if self.verbose:
            print("Exists status (commit number): %s" % status)

        if exists:
            (status, old_port) = semanage.semanage_port_query_local(sh, key)
            if status < 0:
                raise Error("Could not query old SEPort")
            if self.verbose:
                print("Query status (commit number): %s" % status)

        print("Starting transaction...")
        status = semanage.semanage_begin_transaction(sh)
        if status < 0:
            raise Error("Could not start semanage transaction")

        status = semanage.semanage_port_modify_local(sh, key, port)
        if status < 0:
            raise Error("Could not modify SEPort")

        status = semanage.semanage_commit(sh)
        if status < 0:
            raise Error("Could not commit test transaction")
        print("Commit status (transaction number): %s" % status)

        status = semanage.semanage_begin_transaction(sh)
        if status < 0:
            raise Error("Could not start semanage transaction")

        if not exists:
            print("Removing port range...")
            status = semanage.semanage_port_del_local(sh, key)
            if status < 0:
                raise Error("Could not delete test SEPort")
            if self.verbose:
                print("Port range delete: %s" % status)
        else:
            print("Resetting port range...")
            status = semanage.semanage_port_modify_local(sh, key, old_port)
            if status < 0:
                raise Error("Could not reset test SEPort")
            if self.verbose:
                print("Port range modify: %s" % status)

        status = semanage.semanage_commit(sh)
        if status < 0:
            raise Error("Could not commit reset transaction")
        print("Commit status (transaction number): %s" % status)

        semanage.semanage_context_free(con)
        semanage.semanage_port_key_free(key)
        semanage.semanage_port_free(port)
        if exists:
            semanage.semanage_port_free(old_port)
Esempio n. 8
0
    def test_writenode(self, sh):
        print("Testing network node write...")

        (status, node) = semanage.semanage_node_create(sh)
        if status < 0:
            raise Error("Could not create SENode object")
        if self.verbose:
            print("SENode object created.")

        status = semanage.semanage_node_set_addr(sh, node,
                                                 semanage.SEMANAGE_PROTO_IP6,
                                                 "ffee:dddd::bbbb")
        if status < 0:
            raise Error("Could not set SENode address")

        status = semanage.semanage_node_set_mask(sh, node,
                                                 semanage.SEMANAGE_PROTO_IP6,
                                                 "::ffff:ffff:abcd:0000")
        if status < 0:
            raise Error("Could not set SENode netmask")

        semanage.semanage_node_set_proto(node, semanage.SEMANAGE_PROTO_IP6)
        if self.verbose:
            print("SENode protocol set: %s" %
                  semanage.semanage_node_get_proto_str(
                      semanage.SEMANAGE_PROTO_IP6))

        (status, con) = semanage.semanage_context_create(sh)
        if status < 0:
            raise Error("Could not create SEContext object")
        if self.verbose:
            print("SEContext object created (for node).")

        status = semanage.semanage_context_set_user(sh, con, "system_u")
        if status < 0:
            raise Error("Could not set context user")
        if self.verbose:
            print("SEContext user: %s" %
                  semanage.semanage_context_get_user(con))

        status = semanage.semanage_context_set_role(sh, con, "object_r")
        if status < 0:
            raise Error("Could not set context role")
        if self.verbose:
            print("SEContext role: %s" %
                  semanage.semanage_context_get_role(con))

        status = semanage.semanage_context_set_type(sh, con, "lo_node_t")
        if status < 0:
            raise Error("Could not set context type")
        if self.verbose:
            print("SEContext type: %s" %
                  semanage.semanage_context_get_type(con))

        status = semanage.semanage_context_set_mls(sh, con, "s0:c0.c255")
        if status < 0:
            raise Error("Could not set context MLS fields")
        if self.verbose:
            print("SEContext mls: %s" % semanage.semanage_context_get_mls(con))

        status = semanage.semanage_node_set_con(sh, node, con)
        if status < 0:
            raise Error("Could not set SENode context")
        if self.verbose:
            print("SENode context set: %s" % con)

        (status, key) = semanage.semanage_node_key_extract(sh, node)
        if status < 0:
            raise Error("Could not extract SENode key")
        if self.verbose:
            print("SENode key extracted: %s" % key)

        (status, exists) = semanage.semanage_node_exists_local(sh, key)
        if status < 0:
            raise Error("Could not check if SENode exists")
        if self.verbose:
            print("Exists status (commit number): %s" % status)

        if exists:
            (status, old_node) = semanage.semanage_node_query_local(sh, key)
            if status < 0:
                raise Error("Could not query old SENode")
            if self.verbose:
                print("Query status (commit number): %s" % status)

        print("Starting transaction...")
        status = semanage.semanage_begin_transaction(sh)
        if status < 0:
            raise Error("Could not start semanage transaction")

        status = semanage.semanage_node_modify_local(sh, key, node)
        if status < 0:
            raise Error("Could not modify SENode")

        status = semanage.semanage_commit(sh)
        if status < 0:
            raise Error("Could not commit test transaction")
        print("Commit status (transaction number): %s" % status)

        status = semanage.semanage_begin_transaction(sh)
        if status < 0:
            raise Error("Could not start semanage transaction")

        if not exists:
            print("Removing network node...")
            status = semanage.semanage_node_del_local(sh, key)
            if status < 0:
                raise Error("Could not delete test SENode")
            if self.verbose:
                print("Network node delete: %s" % status)
        else:
            print("Resetting network node...")
            status = semanage.semanage_node_modify_local(sh, key, old_node)
            if status < 0:
                raise Error("Could not reset test SENode")
            if self.verbose:
                print("Network node modify: %s" % status)

        status = semanage.semanage_commit(sh)
        if status < 0:
            raise Error("Could not commit reset transaction")
        print("Commit status (transaction number): %s" % status)

        semanage.semanage_context_free(con)
        semanage.semanage_node_key_free(key)
        semanage.semanage_node_free(node)
        if exists:
            semanage.semanage_node_free(old_node)