def test_fcontexts(self,sh): print "Testing file contexts..." (status, flist) = semanage.semanage_fcontext_list(sh) if status < 0: raise Error("Could not list file contexts") print "Query status (commit number): ", status if (len(flist) == 0): print "No file contexts found!" print "This is not necessarily a test failure." return for fcon in flist: if self.verbose: print "File Context reference: ", fcon expr = semanage.semanage_fcontext_get_expr(fcon) type = semanage.semanage_fcontext_get_type(fcon) type_str = semanage.semanage_fcontext_get_type_str(type) con = semanage.semanage_fcontext_get_con(fcon) if not con: con_str = "<<none>>" else: (rc, con_str) = semanage.semanage_context_to_string(sh,con) if rc < 0: con_str = "" print "File Expr: ", expr, " [", type_str, "] Context: ", con_str semanage.semanage_fcontext_free(fcon)
def list_contexts(directory): directory_len = (len(directory)) handle = semanage.semanage_handle_create() semanage.semanage_connect(handle) (rc, fclist) = semanage.semanage_fcontext_list(handle) (rc, fclocal) = semanage.semanage_fcontext_list_local(handle) (rc, fchome) = semanage.semanage_fcontext_list_homedirs(handle) contexts = [] for fcontext in fclist + fclocal + fchome: expression = semanage.semanage_fcontext_get_expr(fcontext) if expression[0:directory_len] == directory: context = semanage.semanage_fcontext_get_con(fcontext) if context: contexts.append(semanage.semanage_context_get_type(context)) selabel = selinux.selabel_open(selinux.SELABEL_CTX_FILE, None, 0) (rc, context) = selinux.selabel_lookup(selabel, directory, 0) if context == None: if exists(directory) == False: exit(3) context = selinux.getfilecon(directory)[1] contexts.append(context.split(':')[2]) return contexts
def test_fcontexts(self, sh): print("Testing file contexts...") (status, flist) = semanage.semanage_fcontext_list(sh) if status < 0: raise Error("Could not list file contexts") print("Query status (commit number): %s" % status) if len(flist) == 0: print("No file contexts found!") print("This is not necessarily a test failure.") return for fcon in flist: if self.verbose: print("File Context reference: %s" % fcon) expr = semanage.semanage_fcontext_get_expr(fcon) type = semanage.semanage_fcontext_get_type(fcon) type_str = semanage.semanage_fcontext_get_type_str(type) con = semanage.semanage_fcontext_get_con(fcon) if not con: con_str = "<<none>>" else: (rc, con_str) = semanage.semanage_context_to_string(sh, con) if rc < 0: con_str = "" print("File Expr: %s [%s] Context: %s" % (expr, type_str, con_str)) semanage.semanage_fcontext_free(fcon)
def test_fcontexts(self, sh): print "Testing file contexts..." (status, flist) = semanage.semanage_fcontext_list(sh) if status < 0: raise Error("Could not list file contexts") print "Query status (commit number): ", status if (len(flist) == 0): print "No file contexts found!" print "This is not necessarily a test failure." return for fcon in flist: if self.verbose: print "File Context reference: ", fcon expr = semanage.semanage_fcontext_get_expr(fcon) type = semanage.semanage_fcontext_get_type(fcon) type_str = semanage.semanage_fcontext_get_type_str(type) con = semanage.semanage_fcontext_get_con(fcon) if not con: con_str = "<<none>>" else: (rc, con_str) = semanage.semanage_context_to_string(sh, con) if rc < 0: con_str = "" print "File Expr: ", expr, " [", type_str, "] Context: ", con_str semanage.semanage_fcontext_free(fcon)
def list_contexts(directory): directory_len = len(directory) handle = semanage.semanage_handle_create() semanage.semanage_connect(handle) (rc, fclist) = semanage.semanage_fcontext_list(handle) (rc, fclocal) = semanage.semanage_fcontext_list_local(handle) (rc, fchome) = semanage.semanage_fcontext_list_homedirs(handle) contexts = [] for fcontext in fclist + fclocal + fchome: expression = semanage.semanage_fcontext_get_expr(fcontext) if expression[0:directory_len] == directory: context = semanage.semanage_fcontext_get_con(fcontext) if context: contexts.append(semanage.semanage_context_get_type(context)) selabel = selinux.selabel_open(selinux.SELABEL_CTX_FILE, None, 0) try: (rc, context) = selinux.selabel_lookup(selabel, directory, 0) except FileNotFoundError: # File context definition containing "<<none>>" triggers exception context = None if context: contexts.append(context.split(":")[2]) # Get the real label (ls -lZ) - may differ from what selabel_lookup returns try: context = selinux.getfilecon(directory)[1] except FileNotFoundError: context = None if context: contexts.append(context.split(":")[2]) return contexts