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

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