Blame SOURCES/0171-tests-secontext-add-secontext-field-getters.patch

2682e8
From 4951286eb634c00c11883b851c91f3a21975eabd Mon Sep 17 00:00:00 2001
2682e8
From: Eugene Syromyatnikov <evgsyr@gmail.com>
2682e8
Date: Tue, 18 Jan 2022 18:03:57 +0100
2682e8
Subject: [PATCH 171/174] tests/secontext: add secontext field getters
2682e8
2682e8
* tests/secontext.h (get_secontext_field, get_secontext_field_file): New
2682e8
declarations.
2682e8
* tests/secontext.c (get_type_from_context): Rename to...
2682e8
(get_secontext_field): ...this;  remove "static" qualifier;  add "field"
2682e8
argument, use it.
2682e8
(raw_expected_secontext_short_file, raw_secontext_short_pid): Replace
2682e8
get_type_from_context call with get_secontext_field.
2682e8
(get_secontext_field_file): New function.
2682e8
(raw_secontext_short_file): Replace body with get_secontext_field_file
2682e8
call.
2682e8
---
2682e8
 tests/secontext.c | 27 +++++++++++++++------------
2682e8
 tests/secontext.h | 20 ++++++++++++++++++++
2682e8
 2 files changed, 35 insertions(+), 12 deletions(-)
2682e8
2682e8
diff --git a/tests/secontext.c b/tests/secontext.c
2682e8
index 848eea9..52211ed 100644
2682e8
--- a/tests/secontext.c
2682e8
+++ b/tests/secontext.c
2682e8
@@ -56,8 +56,8 @@ strip_trailing_newlines(char *context)
2682e8
 	return context;
2682e8
 }
2682e8
 
2682e8
-static char *
2682e8
-get_type_from_context(const char *full_context)
2682e8
+char *
2682e8
+get_secontext_field(const char *full_context, enum secontext_field field)
2682e8
 {
2682e8
 	int saved_errno = errno;
2682e8
 
2682e8
@@ -72,7 +72,7 @@ get_type_from_context(const char *full_context)
2682e8
 	char *context = NULL;
2682e8
 	for (token = strtok_r(ctx_copy, ":", &saveptr), i = 0;
2682e8
 	     token; token = strtok_r(NULL, ":", &saveptr), i++) {
2682e8
-		if (i == 2) {
2682e8
+		if (i == field) {
2682e8
 			context = xstrdup(token);
2682e8
 			break;
2682e8
 		}
2682e8
@@ -122,7 +122,7 @@ raw_expected_secontext_short_file(const char *filename)
2682e8
 	int saved_errno = errno;
2682e8
 
2682e8
 	char *ctx = raw_expected_secontext_full_file(filename);
2682e8
-	char *type = get_type_from_context(ctx);
2682e8
+	char *type = get_secontext_field(ctx, SECONTEXT_TYPE);
2682e8
 	free(ctx);
2682e8
 
2682e8
 	errno = saved_errno;
2682e8
@@ -144,20 +144,23 @@ raw_secontext_full_file(const char *filename)
2682e8
 	return full_secontext;
2682e8
 }
2682e8
 
2682e8
-static char *
2682e8
-raw_secontext_short_file(const char *filename)
2682e8
+char *
2682e8
+get_secontext_field_file(const char *file, enum secontext_field field)
2682e8
 {
2682e8
-	int saved_errno = errno;
2682e8
-
2682e8
-	char *ctx = raw_secontext_full_file(filename);
2682e8
-	char *type = get_type_from_context(ctx);
2682e8
+	char *ctx = raw_secontext_full_file(file);
2682e8
+	char *type =  get_secontext_field(ctx, field);
2682e8
 	free(ctx);
2682e8
 
2682e8
-	errno = saved_errno;
2682e8
 	return type;
2682e8
 }
2682e8
 
2682e8
 static char *
2682e8
+raw_secontext_short_file(const char *filename)
2682e8
+{
2682e8
+	return get_secontext_field_file(filename, SECONTEXT_TYPE);
2682e8
+}
2682e8
+
2682e8
+static char *
2682e8
 raw_secontext_full_pid(pid_t pid)
2682e8
 {
2682e8
 	int saved_errno = errno;
2682e8
@@ -178,7 +181,7 @@ raw_secontext_short_pid(pid_t pid)
2682e8
 	int saved_errno = errno;
2682e8
 
2682e8
 	char *ctx = raw_secontext_full_pid(pid);
2682e8
-	char *type = get_type_from_context(ctx);
2682e8
+	char *type = get_secontext_field(ctx, SECONTEXT_TYPE);
2682e8
 	free(ctx);
2682e8
 
2682e8
 	errno = saved_errno;
2682e8
diff --git a/tests/secontext.h b/tests/secontext.h
2682e8
index 1d0251a..e5571d5 100644
2682e8
--- a/tests/secontext.h
2682e8
+++ b/tests/secontext.h
2682e8
@@ -23,6 +23,15 @@ enum secontext_field {
2682e8
 
2682e8
 #if defined TEST_SECONTEXT && defined HAVE_SELINUX_RUNTIME
2682e8
 
2682e8
+/**
2682e8
+ * Parse a SELinux context string and return a specified field, duplicated
2682e8
+ * in a separate string.  The caller is responsible for freeing the memory
2682e8
+ * pointed by the returned value.
2682e8
+ */
2682e8
+char *get_secontext_field(const char *full_context, enum secontext_field field);
2682e8
+
2682e8
+char *get_secontext_field_file(const char *file, enum secontext_field field);
2682e8
+
2682e8
 void update_secontext_field(const char *file, enum secontext_field field,
2682e8
 			    const char *newvalue);
2682e8
 
2682e8
@@ -48,6 +57,17 @@ void update_secontext_field(const char *file, enum secontext_field field,
2682e8
 
2682e8
 #else
2682e8
 
2682e8
+static inline char *
2682e8
+get_secontext_field(const char *ctx, enum secontext_field field)
2682e8
+{
2682e8
+	return NULL;
2682e8
+}
2682e8
+static inline char *
2682e8
+get_secontext_field_file(const char *file, enum secontext_field field)
2682e8
+{
2682e8
+	return NULL;
2682e8
+}
2682e8
+
2682e8
 static inline void
2682e8
 update_secontext_field(const char *file, enum secontext_field field,
2682e8
 		       const char *newvalue)
2682e8
-- 
2682e8
2.1.4
2682e8