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

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