psss / rpms / libselinux

Forked from rpms/libselinux 5 years ago
Clone
9a368c5
--- libselinux-1.17.9/include/selinux/selinux.h.rhat	2004-09-08 10:51:34.000000000 -0400
9a368c5
+++ libselinux-1.17.9/include/selinux/selinux.h	2004-09-10 13:24:34.747534140 -0400
9a368c5
@@ -173,6 +173,13 @@
9a368c5
 		 mode_t mode,
9a368c5
 		 security_context_t *con);
9a368c5
 
9a368c5
+/* Match the specified media and against the media contexts 
9a368c5
+   /proc/ide/hdc/media
9a368c5
+   configuration and set *con to refer to the resulting context.
9a368c5
+   Caller must free con via freecon. */
9a368c5
+extern int matchmediacon(const char *path,
9a368c5
+		 security_context_t *con);
9a368c5
+
9a368c5
 /*
9a368c5
   selinux_getenforcemode reads the /etc/selinux/config file and determines 
9a368c5
   whether the machine should be started in enforcing (1), permissive (0) or 
9a368c5
@@ -194,6 +201,7 @@
9a368c5
 extern const char *selinux_default_context_path(void);
9a368c5
 extern const char *selinux_user_contexts_path(void);
9a368c5
 extern const char *selinux_file_context_path(void);
9a368c5
+extern const char *selinux_media_context_path(void);
9a368c5
 extern const char *selinux_contexts_path(void);
9a368c5
 extern const char *selinux_booleans_path(void);
9a368c5
 
9a368c5
--- libselinux-1.17.9/src/selinux_config.c.rhat	2004-09-08 10:51:34.000000000 -0400
9a368c5
+++ libselinux-1.17.9/src/selinux_config.c	2004-09-10 13:24:34.751533684 -0400
9a368c5
@@ -24,7 +24,8 @@
9a368c5
 #define FAILSAFE_CONTEXT  5
9a368c5
 #define DEFAULT_TYPE      6
9a368c5
 #define BOOLEANS          7
9a368c5
-#define NEL               8
9a368c5
+#define MEDIA_CONTEXTS    8
9a368c5
+#define NEL               9
9a368c5
 
9a368c5
 /* New layout is relative to SELINUXDIR/policytype. */
9a368c5
 static char *file_paths[NEL];
9a368c5
@@ -200,6 +201,10 @@
9a368c5
 }
9a368c5
 hidden_def(selinux_file_context_path)
9a368c5
 
9a368c5
+const char *selinux_media_context_path() {
9a368c5
+  return get_path(MEDIA_CONTEXTS);
9a368c5
+}
9a368c5
+
9a368c5
 const char *selinux_contexts_path() {
9a368c5
   return get_path(CONTEXTS_DIR);
9a368c5
 }
9a368c5
--- /dev/null	2004-09-10 04:39:39.953683832 -0400
9a368c5
+++ libselinux-1.17.9/src/matchmediacon.c	2004-09-10 13:24:34.750533798 -0400
9a368c5
@@ -0,0 +1,65 @@
9a368c5
+#include <unistd.h>
9a368c5
+#include <fcntl.h>
9a368c5
+#include <sys/stat.h>
9a368c5
+#include <string.h>
9a368c5
+#include "selinux_internal.h"
9a368c5
+#include <stdio.h>
9a368c5
+#include <stdlib.h>
9a368c5
+#include <ctype.h>
9a368c5
+#include <errno.h>
9a368c5
+#include <limits.h>
9a368c5
+#include <regex.h>
9a368c5
+#include <stdarg.h>
9a368c5
+
9a368c5
+int matchmediacon(const char *media, 
9a368c5
+		 security_context_t *con)
9a368c5
+{
9a368c5
+	const char *path = selinux_media_context_path();
9a368c5
+	FILE *infile;
9a368c5
+	char *ptr, *ptr2;
9a368c5
+	char *target;
9a368c5
+	int found=-1;
9a368c5
+	char current_line[PATH_MAX];
9a368c5
+	if ((infile = fopen(path, "r")) == NULL)
9a368c5
+		return -1;
9a368c5
+	while (!feof_unlocked (infile)) {
9a368c5
+		if (!fgets_unlocked(current_line, sizeof(current_line), infile)) {
9a368c5
+			return -1;
9a368c5
+		}
9a368c5
+		if (current_line[strlen(current_line) - 1])
9a368c5
+			current_line[strlen(current_line) - 1] = 0;
9a368c5
+		/* Skip leading whitespace before the partial context. */
9a368c5
+		ptr = current_line;
9a368c5
+		while (*ptr && isspace(*ptr))
9a368c5
+			ptr++;
9a368c5
+		
9a368c5
+		if (!(*ptr))
9a368c5
+			continue;
9a368c5
+
9a368c5
+
9a368c5
+		/* Find the end of the media context. */
9a368c5
+		ptr2 = ptr;
9a368c5
+		while (*ptr2 && !isspace(*ptr2))
9a368c5
+			ptr2++;
9a368c5
+		if (!(*ptr2))
9a368c5
+			continue;
9a368c5
+		
9a368c5
+		*ptr2++=NULL;
9a368c5
+		if (strcmp (media, ptr) == 0) {
9a368c5
+			found = 1;
9a368c5
+			break;
9a368c5
+		}
9a368c5
+	}
9a368c5
+	if (!found) 
9a368c5
+		return -1;
9a368c5
+
9a368c5
+	/* Skip whitespace. */
9a368c5
+	while (*ptr2 && isspace(*ptr2))
9a368c5
+		ptr2++;
9a368c5
+	if (!(*ptr2)) {
9a368c5
+		return -1;
9a368c5
+	}
9a368c5
+	
9a368c5
+	*con = strdup(ptr2);
9a368c5
+	return 0;
9a368c5
+}
9a368c5
--- libselinux-1.17.9/src/compat_file_path.h.rhat	2004-09-08 10:51:34.000000000 -0400
9a368c5
+++ libselinux-1.17.9/src/compat_file_path.h	2004-09-10 13:24:34.748534026 -0400
9a368c5
@@ -7,3 +7,4 @@
9a368c5
 S_(FAILSAFE_CONTEXT, SECURITYDIR "/failsafe_context")
9a368c5
 S_(DEFAULT_TYPE, SECURITYDIR "/default_type")
9a368c5
 S_(BOOLEANS, SECURITYDIR "/booleans")
9a368c5
+S_(MEDIA_CONTEXTS, SECURITYDIR "/default_media")
9a368c5
--- libselinux-1.17.9/src/file_path_suffixes.h.rhat	2004-09-08 10:51:34.000000000 -0400
9a368c5
+++ libselinux-1.17.9/src/file_path_suffixes.h	2004-09-10 13:24:34.749533912 -0400
9a368c5
@@ -7,3 +7,4 @@
9a368c5
 S_(FAILSAFE_CONTEXT, "/contexts/failsafe_context")
9a368c5
 S_(DEFAULT_TYPE, "/contexts/default_type")
9a368c5
 S_(BOOLEANS, "/booleans")
9a368c5
+S_(MEDIA_CONTEXTS, "/contexts/files/media")
9a368c5
--- /dev/null	2004-09-10 04:39:39.953683832 -0400
9a368c5
+++ libselinux-1.17.9/utils/matchmediacon.c	2004-09-10 13:25:04.099192223 -0400
9a368c5
@@ -0,0 +1,28 @@
9a368c5
+#include <unistd.h>
9a368c5
+#include <stdio.h>
9a368c5
+#include <stdlib.h>
9a368c5
+#include <selinux/selinux.h>
9a368c5
+#include <errno.h>
9a368c5
+#include <string.h>
9a368c5
+
9a368c5
+int main(int argc, char **argv) 
9a368c5
+{
9a368c5
+	char *buf;
9a368c5
+	int rc, i;
9a368c5
+
9a368c5
+	if (argc < 2) {
9a368c5
+		fprintf(stderr, "usage:  %s media...\n", argv[0]);
9a368c5
+		exit(1);
9a368c5
+	}
9a368c5
+
9a368c5
+	for (i = 1; i < argc; i++) {
9a368c5
+		rc = matchmediacon(argv[i], &buf;;
9a368c5
+		if (rc < 0) {
9a368c5
+			fprintf(stderr, "%s: matchmediacon(%s) failed: %s\n", argv[0], argv[i]);
9a368c5
+			exit(2);
9a368c5
+		}
9a368c5
+		printf("%s\t%s\n", argv[i], buf);
9a368c5
+		freecon(buf);
9a368c5
+	}
9a368c5
+	exit(0);
9a368c5
+}