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

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