Blame SOURCES/libsemanage-rhel.patch

528c33
diff --git libsemanage-2.5/ChangeLog libsemanage-2.5/ChangeLog
528c33
index 9b4d5b7..10fc784 100644
528c33
--- libsemanage-2.5/ChangeLog
528c33
+++ libsemanage-2.5/ChangeLog
528c33
@@ -1,3 +1,13 @@
528c33
+	* genhomedircon: add support for %group syntax, from Gary Tierney.
528c33
+	* genhomedircon: generate contexts for logins mapped to the default user, from Gary Tierney.
528c33
+	* Validate and compile file contexts before installing, from Stephen Smalley.
528c33
+	* Swap tcp and udp protocol numbers, from Miroslav Vadkerti.
528c33
+	* Sort object files for deterministic linking order, from Laurent Bigonville.
528c33
+	* Support overriding Makefile RANLIB, from Julien Pivotto.
528c33
+	* Respect CC and PKG_CONFIG environment variable, from Julien Pivotto.
528c33
+	* Fix multiple spelling errors, from Laurent Bigonville.
528c33
+	* genhomedircon: %{USERID} and %{USERNAME} support and code cleanups, from Jason Zaman.
528c33
+
528c33
 2.5 2016-02-23
528c33
 	* Do not overwrite CFLAGS in test Makefile, from Nicolas Iooss.
528c33
 	* Fix uninitialized variable in direct_commit and direct_api, from Nicolas Iooss.
528c33
diff --git libsemanage-2.5/include/semanage/handle.h libsemanage-2.5/include/semanage/handle.h
528c33
index 6cad529..c816590 100644
528c33
--- libsemanage-2.5/include/semanage/handle.h
528c33
+++ libsemanage-2.5/include/semanage/handle.h
528c33
@@ -130,7 +130,7 @@ int semanage_commit(semanage_handle_t *);
528c33
 #define SEMANAGE_CAN_READ 1
528c33
 #define SEMANAGE_CAN_WRITE 2
528c33
 /* returns SEMANAGE_CAN_READ or SEMANAGE_CAN_WRITE if the store is readable
528c33
- * or writable, respectively. <0 if an error occured */
528c33
+ * or writable, respectively. <0 if an error occurred */
528c33
 int semanage_access_check(semanage_handle_t * sh);
528c33
 
528c33
 /* returns 0 if not connected, 1 if connected */
e92a8c
diff --git libsemanage-2.5/include/semanage/modules.h libsemanage-2.5/include/semanage/modules.h
e92a8c
index 4b93e54..50940e2 100644
e92a8c
--- libsemanage-2.5/include/semanage/modules.h
e92a8c
+++ libsemanage-2.5/include/semanage/modules.h
e92a8c
@@ -39,7 +39,7 @@ int semanage_module_install_file(semanage_handle_t *,
e92a8c
 int semanage_module_remove(semanage_handle_t *, char *module_name);
e92a8c
 
e92a8c
 /* semanage_module_info is for getting information on installed
e92a8c
-   modules, only name at this time */
e92a8c
+   modules, only name and version at this time */
e92a8c
 typedef struct semanage_module_info semanage_module_info_t;
e92a8c
 
e92a8c
 /* Look up a module using @modkey. The module's raw data is returned as a
e92a8c
@@ -64,6 +64,7 @@ void semanage_module_info_datum_destroy(semanage_module_info_t *);
e92a8c
 semanage_module_info_t *semanage_module_list_nth(semanage_module_info_t * list,
e92a8c
 						 int n);
e92a8c
 const char *semanage_module_get_name(semanage_module_info_t *);
e92a8c
+const char *semanage_module_get_version(semanage_module_info_t *);
e92a8c
 
e92a8c
 /* Module Info */
e92a8c
 
e92a8c
@@ -104,6 +105,14 @@ int semanage_module_info_get_name(semanage_handle_t *sh,
e92a8c
 				  semanage_module_info_t *modinfo,
e92a8c
 				  const char **name);
e92a8c
 
e92a8c
+/* Get @module_version from @modinfo. Caller should not free @module_version.
e92a8c
+ *
e92a8c
+ * Returns 0 on success and -1 on error.
e92a8c
+ */
e92a8c
+int semanage_module_info_get_version(semanage_handle_t *sh,
e92a8c
+				      semanage_module_info_t *modinfo,
e92a8c
+				      const char **version);
e92a8c
+
e92a8c
 /* Get @lang_ext from @modinfo. Caller should not free @lang_ext.
e92a8c
  *
e92a8c
  * Returns 0 on success and -1 on error.
e92a8c
@@ -138,6 +147,14 @@ int semanage_module_info_set_name(semanage_handle_t *sh,
e92a8c
 				  semanage_module_info_t *modinfo,
e92a8c
 				  const char *name);
e92a8c
 
e92a8c
+/* Set @version in @modinfo.
e92a8c
+ *
e92a8c
+ * Returns 0 on success and -1 on error.
e92a8c
+ */
e92a8c
+int semanage_module_info_set_version(semanage_handle_t *sh,
e92a8c
+				      semanage_module_info_t *modinfo,
e92a8c
+				      const char *version);
e92a8c
+
e92a8c
 /* Set @lang_ext in @modinfo.
e92a8c
  *
e92a8c
  * Returns 0 on success and -1 on error.
528c33
diff --git libsemanage-2.5/man/man5/semanage.conf.5 libsemanage-2.5/man/man5/semanage.conf.5
528c33
index 8f8de55..1009d97 100644
528c33
--- libsemanage-2.5/man/man5/semanage.conf.5
528c33
+++ libsemanage-2.5/man/man5/semanage.conf.5
528c33
@@ -33,7 +33,7 @@ Specify an alternative root path to use for the store. The default is "/"
528c33
 
528c33
 .TP
528c33
 .B store-root
528c33
-Specify an alternative store_root path to use. The default is "/var/lib/selinux"
528c33
+Specify an alternative store_root path to use. The default is "/etc/selinux"
528c33
 
528c33
 .TP
528c33
 .B compiler-directory
528c33
diff --git libsemanage-2.5/src/Makefile libsemanage-2.5/src/Makefile
528c33
index d1fcc0b..96ee652 100644
528c33
--- libsemanage-2.5/src/Makefile
528c33
+++ libsemanage-2.5/src/Makefile
528c33
@@ -5,6 +5,7 @@ PYTHON ?= python
528c33
 PYPREFIX ?= $(notdir $(PYTHON))
528c33
 RUBY ?= ruby
528c33
 RUBYPREFIX ?= $(notdir $(RUBY))
528c33
+PKG_CONFIG ?= pkg-config
528c33
 
528c33
 # Installation directories.
528c33
 PREFIX ?= $(DESTDIR)/usr
528c33
@@ -12,11 +13,11 @@ LIBDIR ?= $(PREFIX)/lib
528c33
 SHLIBDIR ?= $(DESTDIR)/lib
528c33
 INCLUDEDIR ?= $(PREFIX)/include
528c33
 PYLIBVER ?= $(shell $(PYTHON) -c 'import sys;print("python%d.%d" % sys.version_info[0:2])')
528c33
-PYINC ?= $(shell pkg-config --cflags $(PYPREFIX))
528c33
+PYINC ?= $(shell $(PKG_CONFIG) --cflags $(PYPREFIX))
528c33
 PYLIBDIR ?= $(LIBDIR)/$(PYLIBVER)
528c33
 RUBYLIBVER ?= $(shell $(RUBY) -e 'print RUBY_VERSION.split(".")[0..1].join(".")')
528c33
 RUBYPLATFORM ?= $(shell $(RUBY) -e 'print RUBY_PLATFORM')
528c33
-RUBYINC ?= $(shell pkg-config --cflags ruby-$(RUBYLIBVER))
528c33
+RUBYINC ?= $(shell $(PKG_CONFIG) --cflags ruby-$(RUBYLIBVER))
528c33
 RUBYINSTALL ?= $(LIBDIR)/ruby/site_ruby/$(RUBYLIBVER)/$(RUBYPLATFORM)
528c33
 
528c33
 LIBBASE=$(shell basename $(LIBDIR))
528c33
@@ -51,7 +52,7 @@ SWIGRUBYSO=$(RUBYPREFIX)_semanage.so
528c33
 LIBSO=$(TARGET).$(LIBVERSION)
528c33
 
528c33
 GENERATED=$(SWIGCOUT) $(SWIGRUBYCOUT) semanageswig_python_exception.i
528c33
-SRCS= $(filter-out $(GENERATED),$(wildcard *.c))
528c33
+SRCS= $(filter-out $(GENERATED),$(sort $(wildcard *.c)))
528c33
 
528c33
 OBJS= $(patsubst %.c,%.o,$(SRCS)) conf-scan.o conf-parse.o
528c33
 LOBJS= $(patsubst %.c,%.lo,$(SRCS)) conf-scan.lo conf-parse.lo
528c33
@@ -61,7 +62,7 @@ SWIG_CFLAGS += -Wno-error -Wno-unused-but-set-variable -Wno-unused-variable -Wno
528c33
 		-Wno-unused-parameter
528c33
 
528c33
 override CFLAGS += -I../include -I$(INCLUDEDIR) -D_GNU_SOURCE 
528c33
-RANLIB=ranlib
528c33
+RANLIB ?= ranlib
528c33
 
528c33
 SWIG = swig -Wall -python -o $(SWIGCOUT) -outdir ./
528c33
 
528c33
diff --git libsemanage-2.5/src/conf-parse.y libsemanage-2.5/src/conf-parse.y
528c33
index b527e89..e2b6028 100644
528c33
--- libsemanage-2.5/src/conf-parse.y
528c33
+++ libsemanage-2.5/src/conf-parse.y
528c33
@@ -340,7 +340,7 @@ static int semanage_conf_init(semanage_conf_t * conf)
528c33
 	conf->store_type = SEMANAGE_CON_DIRECT;
528c33
 	conf->store_path = strdup(basename(selinux_policy_root()));
528c33
 	conf->ignoredirs = NULL;
528c33
-	conf->store_root_path = strdup("/var/lib/selinux");
528c33
+	conf->store_root_path = strdup("/etc/selinux");
528c33
 	conf->compiler_directory_path = strdup("/usr/libexec/selinux/hll");
528c33
 	conf->policyvers = sepol_policy_kern_vers_max();
528c33
 	conf->target_platform = SEPOL_TARGET_SELINUX;
528c33
diff --git libsemanage-2.5/src/database.h libsemanage-2.5/src/database.h
528c33
index e460379..6a4a164 100644
528c33
--- libsemanage-2.5/src/database.h
528c33
+++ libsemanage-2.5/src/database.h
528c33
@@ -148,7 +148,7 @@ typedef struct dbase_table {
528c33
 	 * This function must be invoked before using
528c33
 	 * any of the database functions above. It may be invoked
528c33
 	 * multiple times, and will update the cache if a commit
528c33
-	 * occured between invocations */
528c33
+	 * occurred between invocations */
528c33
 	int (*cache) (struct semanage_handle * handle, dbase_t * dbase);
528c33
 
528c33
 	/* Forgets all changes that haven't been written
e92a8c
diff --git libsemanage-2.5/src/direct_api.c libsemanage-2.5/src/direct_api.c
6f01c7
index 2187b65..1efe141 100644
e92a8c
--- libsemanage-2.5/src/direct_api.c
e92a8c
+++ libsemanage-2.5/src/direct_api.c
6f01c7
@@ -146,9 +146,6 @@ int semanage_direct_connect(semanage_handle_t * sh)
6f01c7
 		if (semanage_create_store(sh, 1))
6f01c7
 			goto err;
6f01c7
 
6f01c7
-	if (semanage_access_check(sh) < SEMANAGE_CAN_READ)
6f01c7
-		goto err;
6f01c7
-
6f01c7
 	sh->u.direct.translock_file_fd = -1;
6f01c7
 	sh->u.direct.activelock_file_fd = -1;
6f01c7
 
6f01c7
@@ -275,7 +272,9 @@ int semanage_direct_connect(semanage_handle_t * sh)
6f01c7
 
6f01c7
 	/* set the disable dontaudit value */
6f01c7
 	path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_DISABLE_DONTAUDIT);
6f01c7
-	if (access(path, F_OK) == 0)
6f01c7
+
6f01c7
+	struct stat sb;
6f01c7
+	if (stat(path, &sb) == 0)
6f01c7
 		sepol_set_disable_dontaudit(sh->sepolh, 1);
6f01c7
 	else
6f01c7
 		sepol_set_disable_dontaudit(sh->sepolh, 0);
6f01c7
@@ -345,10 +344,6 @@ static int semanage_direct_disconnect(semanage_handle_t * sh)
6f01c7
 
6f01c7
 static int semanage_direct_begintrans(semanage_handle_t * sh)
6f01c7
 {
6f01c7
-
6f01c7
-	if (semanage_access_check(sh) != SEMANAGE_CAN_WRITE) {
6f01c7
-		return -1;
6f01c7
-	}
6f01c7
 	if (semanage_get_trans_lock(sh) < 0) {
6f01c7
 		return -1;
6f01c7
 	}
6f01c7
@@ -363,6 +358,35 @@ static int semanage_direct_begintrans(semanage_handle_t * sh)
e92a8c
 
e92a8c
 /********************* utility functions *********************/
e92a8c
 
e92a8c
+/* Takes a module stored in 'module_data' and parses its headers.
e92a8c
+ * Sets reference variables 'module_name' to module's name, and
e92a8c
+ * 'version' to module's version.  The caller is responsible for
e92a8c
+ * free()ing 'module_name', and 'version'; they will be
e92a8c
+ * set to NULL upon entering this function.  Returns 0 on success, -1
e92a8c
+ * if out of memory.
e92a8c
+ */
e92a8c
+static int parse_module_headers(semanage_handle_t * sh, char *module_data,
e92a8c
+                               size_t data_len, char **module_name,
e92a8c
+                               char **version)
e92a8c
+{
e92a8c
+       struct sepol_policy_file *pf;
e92a8c
+       int file_type;
e92a8c
+       *module_name = *version = NULL;
e92a8c
+
e92a8c
+       if (sepol_policy_file_create(&pf)) {
e92a8c
+               ERR(sh, "Out of memory!");
e92a8c
+               return -1;
e92a8c
+       }
e92a8c
+       sepol_policy_file_set_mem(pf, module_data, data_len);
e92a8c
+       sepol_policy_file_set_handle(pf, sh->sepolh);
e92a8c
+       if (module_data != NULL && data_len > 0)
e92a8c
+           sepol_module_package_info(pf, &file_type, module_name,
e92a8c
+                                     version);
e92a8c
+       sepol_policy_file_free(pf);
e92a8c
+
e92a8c
+       return 0;
e92a8c
+}
e92a8c
+
e92a8c
 #include <stdlib.h>
e92a8c
 #include <bzlib.h>
e92a8c
 #include <string.h>
6f01c7
@@ -588,13 +612,33 @@ static int semanage_direct_update_user_extra(semanage_handle_t * sh, cil_db_t *c
6f01c7
 	}
6f01c7
 
6f01c7
 	if (size > 0) {
6f01c7
+		/*
6f01c7
+		 * Write the users_extra entries from CIL modules.
6f01c7
+		 * This file is used as our baseline when we do not require
6f01c7
+		 * re-linking.
6f01c7
+		 */
6f01c7
+		ofilename = semanage_path(SEMANAGE_TMP,
6f01c7
+					  SEMANAGE_USERS_EXTRA_LINKED);
6f01c7
+		if (ofilename == NULL) {
6f01c7
+			retval = -1;
6f01c7
+			goto cleanup;
6f01c7
+		}
6f01c7
+		retval = write_file(sh, ofilename, data, size);
6f01c7
+		if (retval < 0)
6f01c7
+			goto cleanup;
6f01c7
+
6f01c7
+		/*
6f01c7
+		 * Write the users_extra file; users_extra.local
6f01c7
+		 * will be merged into this file.
6f01c7
+		 */
6f01c7
 		ofilename = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA);
6f01c7
 		if (ofilename == NULL) {
6f01c7
-			return retval;
6f01c7
+			retval = -1;
6f01c7
+			goto cleanup;
6f01c7
 		}
6f01c7
 		retval = write_file(sh, ofilename, data, size);
6f01c7
 		if (retval < 0)
6f01c7
-			return retval;
6f01c7
+			goto cleanup;
6f01c7
 
6f01c7
 		pusers_extra->dtable->drop_cache(pusers_extra->dbase);
6f01c7
 		
6f01c7
@@ -623,11 +667,33 @@ static int semanage_direct_update_seuser(semanage_handle_t * sh, cil_db_t *cildb
6f01c7
 	}
6f01c7
 
6f01c7
 	if (size > 0) {
6f01c7
+		/*
6f01c7
+		 * Write the seusers entries from CIL modules.
6f01c7
+		 * This file is used as our baseline when we do not require
6f01c7
+		 * re-linking.
6f01c7
+		 */
6f01c7
+		ofilename = semanage_path(SEMANAGE_TMP,
6f01c7
+					  SEMANAGE_SEUSERS_LINKED);
6f01c7
+		if (ofilename == NULL) {
6f01c7
+			retval = -1;
6f01c7
+			goto cleanup;
6f01c7
+		}
6f01c7
+		retval = write_file(sh, ofilename, data, size);
6f01c7
+		if (retval < 0)
6f01c7
+			goto cleanup;
6f01c7
+
6f01c7
+		/*
6f01c7
+		 * Write the seusers file; seusers.local will be merged into
6f01c7
+		 * this file.
6f01c7
+		 */
6f01c7
 		ofilename = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
6f01c7
 		if (ofilename == NULL) {
6f01c7
-			return -1;
6f01c7
+			retval = -1;
6f01c7
+			goto cleanup;
6f01c7
 		}
6f01c7
 		retval = write_file(sh, ofilename, data, size);
6f01c7
+		if (retval < 0)
6f01c7
+			goto cleanup;
6f01c7
 
6f01c7
 		pseusers->dtable->drop_cache(pseusers->dbase);
6f01c7
 	} else {
6f01c7
@@ -1037,8 +1103,9 @@ static int semanage_compile_hll_modules(semanage_handle_t *sh,
6f01c7
 			goto cleanup;
6f01c7
 		}
6f01c7
 
6f01c7
+		struct stat sb;
6f01c7
 		if (semanage_get_ignore_module_cache(sh) == 0 &&
6f01c7
-				access(cil_path, F_OK) == 0) {
6f01c7
+				stat(cil_path, &sb) == 0) {
6f01c7
 			continue;
6f01c7
 		}
6f01c7
 
6f01c7
@@ -1066,21 +1133,18 @@ static int semanage_direct_commit(semanage_handle_t * sh)
6f01c7
 	size_t fc_buffer_len = 0;
6f01c7
 	const char *ofilename = NULL;
6f01c7
 	const char *path;
6f01c7
-	int retval = -1, num_modinfos = 0, i, missing_policy_kern = 0,
6f01c7
-		missing_seusers = 0, missing_fc = 0, missing = 0;
6f01c7
+	int retval = -1, num_modinfos = 0, i;
6f01c7
 	sepol_policydb_t *out = NULL;
6f01c7
 	struct cil_db *cildb = NULL;
6f01c7
 	semanage_module_info_t *modinfos = NULL;
6f01c7
 
6f01c7
-	/* Declare some variables */
6f01c7
-	int modified = 0, fcontexts_modified, ports_modified,
6f01c7
-	    seusers_modified, users_extra_modified, dontaudit_modified,
6f01c7
-	    preserve_tunables_modified, bools_modified = 0,
6f01c7
+	int do_rebuild, do_write_kernel, do_install;
6f01c7
+	int fcontexts_modified, ports_modified, seusers_modified,
6f01c7
 		disable_dontaudit, preserve_tunables;
6f01c7
 	dbase_config_t *users = semanage_user_dbase_local(sh);
6f01c7
 	dbase_config_t *users_base = semanage_user_base_dbase_local(sh);
6f01c7
 	dbase_config_t *pusers_base = semanage_user_base_dbase_policy(sh);
6f01c7
-	dbase_config_t *users_extra = semanage_user_extra_dbase_local(sh);
6f01c7
+	dbase_config_t *pusers_extra = semanage_user_extra_dbase_policy(sh);
6f01c7
 	dbase_config_t *ports = semanage_port_dbase_local(sh);
6f01c7
 	dbase_config_t *pports = semanage_port_dbase_policy(sh);
6f01c7
 	dbase_config_t *bools = semanage_bool_dbase_local(sh);
6f01c7
@@ -1092,13 +1156,23 @@ static int semanage_direct_commit(semanage_handle_t * sh)
6f01c7
 	dbase_config_t *fcontexts = semanage_fcontext_dbase_local(sh);
6f01c7
 	dbase_config_t *pfcontexts = semanage_fcontext_dbase_policy(sh);
6f01c7
 	dbase_config_t *seusers = semanage_seuser_dbase_local(sh);
6f01c7
+	dbase_config_t *pseusers = semanage_seuser_dbase_policy(sh);
6f01c7
+
6f01c7
+	/* Modified flags that we need to use more than once. */
6f01c7
+	ports_modified = ports->dtable->is_modified(ports->dbase);
6f01c7
+	seusers_modified = seusers->dtable->is_modified(seusers->dbase);
6f01c7
+	fcontexts_modified = fcontexts->dtable->is_modified(fcontexts->dbase);
6f01c7
+
6f01c7
+	/* Rebuild if explicitly requested or any module changes occurred. */
6f01c7
+	do_rebuild = sh->do_rebuild | sh->modules_modified;
6f01c7
 
6f01c7
 	/* Create or remove the disable_dontaudit flag file. */
6f01c7
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_DISABLE_DONTAUDIT);
6f01c7
-	if (access(path, F_OK) == 0)
6f01c7
-		dontaudit_modified = !(sepol_get_disable_dontaudit(sh->sepolh) == 1);
6f01c7
+	struct stat sb;
6f01c7
+	if (stat(path, &sb) == 0)
6f01c7
+		do_rebuild |= !(sepol_get_disable_dontaudit(sh->sepolh) == 1);
6f01c7
 	else
6f01c7
-		dontaudit_modified = (sepol_get_disable_dontaudit(sh->sepolh) == 1);
6f01c7
+		do_rebuild |= (sepol_get_disable_dontaudit(sh->sepolh) == 1);
6f01c7
 	if (sepol_get_disable_dontaudit(sh->sepolh) == 1) {
6f01c7
 		FILE *touch;
6f01c7
 		touch = fopen(path, "w");
6f01c7
@@ -1120,10 +1194,10 @@ static int semanage_direct_commit(semanage_handle_t * sh)
6f01c7
 
6f01c7
 	/* Create or remove the preserve_tunables flag file. */
6f01c7
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_PRESERVE_TUNABLES);
6f01c7
-	if (access(path, F_OK) == 0)
6f01c7
-		preserve_tunables_modified = !(sepol_get_preserve_tunables(sh->sepolh) == 1);
6f01c7
+	if (stat(path, &sb) == 0)
6f01c7
+		do_rebuild |= !(sepol_get_preserve_tunables(sh->sepolh) == 1);
6f01c7
 	else
6f01c7
-		preserve_tunables_modified = (sepol_get_preserve_tunables(sh->sepolh) == 1);
6f01c7
+		do_rebuild |= (sepol_get_preserve_tunables(sh->sepolh) == 1);
6f01c7
 	if (sepol_get_preserve_tunables(sh->sepolh) == 1) {
6f01c7
 		FILE *touch;
6f01c7
 		touch = fopen(path, "w");
6f01c7
@@ -1151,54 +1225,75 @@ static int semanage_direct_commit(semanage_handle_t * sh)
6f01c7
 			goto cleanup;
6f01c7
 	}
6f01c7
 
6f01c7
-	/* Decide if anything was modified */
6f01c7
-	fcontexts_modified = fcontexts->dtable->is_modified(fcontexts->dbase);
6f01c7
-	seusers_modified = seusers->dtable->is_modified(seusers->dbase);
6f01c7
-	users_extra_modified =
6f01c7
-	    users_extra->dtable->is_modified(users_extra->dbase);
6f01c7
-	ports_modified = ports->dtable->is_modified(ports->dbase);
6f01c7
-	bools_modified = bools->dtable->is_modified(bools->dbase);
6f01c7
-
6f01c7
-	modified = sh->modules_modified;
6f01c7
-	modified |= seusers_modified;
6f01c7
-	modified |= users_extra_modified;
6f01c7
-	modified |= ports_modified;
6f01c7
-	modified |= users->dtable->is_modified(users_base->dbase);
6f01c7
-	modified |= ifaces->dtable->is_modified(ifaces->dbase);
6f01c7
-	modified |= nodes->dtable->is_modified(nodes->dbase);
6f01c7
-	modified |= dontaudit_modified;
6f01c7
-	modified |= preserve_tunables_modified;
6f01c7
-
6f01c7
-	/* This is for systems that have already migrated with an older version
6f01c7
-	 * of semanage_migrate_store. The older version did not copy policy.kern so
6f01c7
-	 * the policy binary must be rebuilt here.
6f01c7
+	/*
6f01c7
+	 * This is for systems that have already migrated with an older version
6f01c7
+	 * of semanage_migrate_store. The older version did not copy
6f01c7
+	 * policy.kern so the policy binary must be rebuilt here.
6f01c7
+	 * This also ensures that any linked files that are required
6f01c7
+	 * in order to skip re-linking are present; otherwise, we force
6f01c7
+	 * a rebuild.
6f01c7
 	 */
6f01c7
-	if (!sh->do_rebuild && !modified) {
6f01c7
+	if (!do_rebuild) {
6f01c7
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_KERNEL);
6f01c7
-
6f01c7
-		if (access(path, F_OK) != 0) {
6f01c7
-			missing_policy_kern = 1;
6f01c7
+		if (stat(path, &sb) != 0) {
6f01c7
+			do_rebuild = 1;
6f01c7
+			goto rebuild;
6f01c7
 		}
6f01c7
 
6f01c7
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
6f01c7
-
6f01c7
-		if (access(path, F_OK) != 0) {
6f01c7
-			missing_fc = 1;
6f01c7
+		if (stat(path, &sb) != 0) {
6f01c7
+			do_rebuild = 1;
6f01c7
+			goto rebuild;
6f01c7
 		}
6f01c7
 
6f01c7
 		path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
6f01c7
+		if (stat(path, &sb) != 0) {
6f01c7
+			do_rebuild = 1;
6f01c7
+			goto rebuild;
6f01c7
+		}
6f01c7
 
6f01c7
-		if (access(path, F_OK) != 0) {
6f01c7
-			missing_seusers = 1;
6f01c7
+		path = semanage_path(SEMANAGE_TMP, SEMANAGE_LINKED);
6f01c7
+		if (stat(path, &sb) != 0) {
6f01c7
+			do_rebuild = 1;
6f01c7
+			goto rebuild;
6f01c7
 		}
6f01c7
-	}
6f01c7
 
6f01c7
-	missing |= missing_policy_kern;
6f01c7
-	missing |= missing_fc;
6f01c7
-	missing |= missing_seusers;
6f01c7
+		path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
6f01c7
+		if (stat(path, &sb) != 0) {
6f01c7
+			do_rebuild = 1;
6f01c7
+			goto rebuild;
6f01c7
+		}
6f01c7
+
6f01c7
+		path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
6f01c7
+		if (stat(path, &sb) != 0) {
6f01c7
+			do_rebuild = 1;
6f01c7
+			goto rebuild;
6f01c7
+		}
6f01c7
+	}
6f01c7
 
6f01c7
-	/* If there were policy changes, or explicitly requested, rebuild the policy */
6f01c7
-	if (sh->do_rebuild || modified || missing) {
6f01c7
+rebuild:
6f01c7
+	/*
6f01c7
+	 * Now that we know whether or not a rebuild is required,
6f01c7
+	 * we can determine what else needs to be done.
6f01c7
+	 * We need to write the kernel policy if we are rebuilding
6f01c7
+	 * or if any other policy component that lives in the kernel
6f01c7
+	 * policy has been modified.
6f01c7
+	 * We need to install the policy files if any of the managed files
6f01c7
+	 * that live under /etc/selinux (kernel policy, seusers, file contexts)
6f01c7
+	 * will be modified.
6f01c7
+	 */
6f01c7
+	do_write_kernel = do_rebuild | ports_modified |
6f01c7
+		bools->dtable->is_modified(bools->dbase) |
6f01c7
+		ifaces->dtable->is_modified(ifaces->dbase) |
6f01c7
+		nodes->dtable->is_modified(nodes->dbase) |
6f01c7
+		users->dtable->is_modified(users_base->dbase);
6f01c7
+	do_install = do_write_kernel | seusers_modified | fcontexts_modified;
6f01c7
+
6f01c7
+	/*
6f01c7
+	 * If there were policy changes, or explicitly requested, or
6f01c7
+	 * any required files are missing, rebuild the policy.
6f01c7
+	 */
6f01c7
+	if (do_rebuild) {
6f01c7
 		/* =================== Module expansion =============== */
6f01c7
 
6f01c7
 		retval = semanage_get_active_modules(sh, &modinfos, &num_modinfos);
6f01c7
@@ -1287,43 +1382,72 @@ static int semanage_direct_commit(semanage_handle_t * sh)
6f01c7
 			goto cleanup;
6f01c7
 
6f01c7
 		cil_db_destroy(&cildb);
6f01c7
-	
6f01c7
+
6f01c7
+		/* Write the linked policy before merging local changes. */
6f01c7
+		retval = semanage_write_policydb(sh, out,
6f01c7
+						 SEMANAGE_LINKED);
6f01c7
+		if (retval < 0)
6f01c7
+			goto cleanup;
6f01c7
 	} else {
6f01c7
-		/* Load already linked policy */
6f01c7
+		/* Load the existing linked policy, w/o local changes */
6f01c7
 		retval = sepol_policydb_create(&out;;
6f01c7
 		if (retval < 0)
6f01c7
 			goto cleanup;
6f01c7
 
6f01c7
-		retval = semanage_read_policydb(sh, out);
6f01c7
+		retval = semanage_read_policydb(sh, out, SEMANAGE_LINKED);
6f01c7
 		if (retval < 0)
6f01c7
 			goto cleanup;
6f01c7
-	}
6f01c7
 
6f01c7
-	if (sh->do_rebuild || modified || bools_modified) {
6f01c7
-		/* Attach to policy databases that work with a policydb. */
6f01c7
-		dbase_policydb_attach((dbase_policydb_t *) pusers_base->dbase, out);
6f01c7
-		dbase_policydb_attach((dbase_policydb_t *) pports->dbase, out);
6f01c7
-		dbase_policydb_attach((dbase_policydb_t *) pifaces->dbase, out);
6f01c7
-		dbase_policydb_attach((dbase_policydb_t *) pbools->dbase, out);
6f01c7
-		dbase_policydb_attach((dbase_policydb_t *) pnodes->dbase, out);
6f01c7
+		path = semanage_path(SEMANAGE_TMP, SEMANAGE_SEUSERS_LINKED);
6f01c7
+		if (stat(path, &sb) == 0) {
6f01c7
+			retval = semanage_copy_file(path,
6f01c7
+						    semanage_path(SEMANAGE_TMP,
6f01c7
+								  SEMANAGE_STORE_SEUSERS),
6f01c7
+						    sh->conf->file_mode);
6f01c7
+			if (retval < 0)
6f01c7
+				goto cleanup;
6f01c7
+			pseusers->dtable->drop_cache(pseusers->dbase);
6f01c7
+		} else {
6f01c7
+			pseusers->dtable->clear(sh, pseusers->dbase);
6f01c7
+		}
6f01c7
+
6f01c7
+		path = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA_LINKED);
6f01c7
+		if (stat(path, &sb) == 0) {
6f01c7
+			retval = semanage_copy_file(path,
6f01c7
+						    semanage_path(SEMANAGE_TMP,
6f01c7
+								  SEMANAGE_USERS_EXTRA),
6f01c7
+						    sh->conf->file_mode);
6f01c7
+			if (retval < 0)
6f01c7
+				goto cleanup;
6f01c7
+			pusers_extra->dtable->drop_cache(pusers_extra->dbase);
6f01c7
+		} else {
6f01c7
+			pusers_extra->dtable->clear(sh, pusers_extra->dbase);
6f01c7
+		}
6f01c7
+	}
6f01c7
 
6f01c7
-		/* ============= Apply changes, and verify  =============== */
6f01c7
+	/* Attach our databases to the policydb we just created or loaded. */
6f01c7
+	dbase_policydb_attach((dbase_policydb_t *) pusers_base->dbase, out);
6f01c7
+	dbase_policydb_attach((dbase_policydb_t *) pports->dbase, out);
6f01c7
+	dbase_policydb_attach((dbase_policydb_t *) pifaces->dbase, out);
6f01c7
+	dbase_policydb_attach((dbase_policydb_t *) pbools->dbase, out);
6f01c7
+	dbase_policydb_attach((dbase_policydb_t *) pnodes->dbase, out);
6f01c7
 
6f01c7
-		retval = semanage_base_merge_components(sh);
6f01c7
-		if (retval < 0)
6f01c7
-			goto cleanup;
6f01c7
+	/* Merge local changes */
6f01c7
+	retval = semanage_base_merge_components(sh);
6f01c7
+	if (retval < 0)
6f01c7
+		goto cleanup;
6f01c7
 
6f01c7
-		retval = semanage_write_policydb(sh, out);
6f01c7
+	if (do_write_kernel) {
6f01c7
+		/* Write new kernel policy. */
6f01c7
+		retval = semanage_write_policydb(sh, out,
6f01c7
+						 SEMANAGE_STORE_KERNEL);
6f01c7
 		if (retval < 0)
6f01c7
 			goto cleanup;
6f01c7
 
6f01c7
+		/* Run the kernel policy verifier, if any. */
6f01c7
 		retval = semanage_verify_kernel(sh);
6f01c7
 		if (retval < 0)
6f01c7
 			goto cleanup;
6f01c7
-	} else {
6f01c7
-		retval = semanage_base_merge_components(sh);
6f01c7
-		if (retval < 0)
6f01c7
-			goto cleanup;
6f01c7
 	}
6f01c7
 
6f01c7
 	/* ======= Post-process: Validate non-policydb components ===== */
6f01c7
@@ -1332,21 +1456,21 @@ static int semanage_direct_commit(semanage_handle_t * sh)
6f01c7
 	 * Note: those are still cached, even though they've been 
6f01c7
 	 * merged into the main file_contexts. We won't check the 
6f01c7
 	 * large file_contexts - checked at compile time */
6f01c7
-	if (sh->do_rebuild || modified || fcontexts_modified) {
6f01c7
+	if (do_rebuild || fcontexts_modified) {
6f01c7
 		retval = semanage_fcontext_validate_local(sh, out);
6f01c7
 		if (retval < 0)
6f01c7
 			goto cleanup;
6f01c7
 	}
6f01c7
 
6f01c7
 	/* Validate local seusers against policy */
6f01c7
-	if (sh->do_rebuild || modified || seusers_modified) {
6f01c7
+	if (do_rebuild || seusers_modified) {
6f01c7
 		retval = semanage_seuser_validate_local(sh, out);
6f01c7
 		if (retval < 0)
6f01c7
 			goto cleanup;
6f01c7
 	}
6f01c7
 
6f01c7
 	/* Validate local ports for overlap */
6f01c7
-	if (sh->do_rebuild || modified || ports_modified) {
6f01c7
+	if (do_rebuild || ports_modified) {
6f01c7
 		retval = semanage_port_validate_local(sh);
6f01c7
 		if (retval < 0)
6f01c7
 			goto cleanup;
6f01c7
@@ -1367,33 +1491,27 @@ static int semanage_direct_commit(semanage_handle_t * sh)
6f01c7
 	}
6f01c7
 
6f01c7
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL);
6f01c7
-	if (access(path, F_OK) == 0) {
6f01c7
-		retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
6f01c7
-							semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),
6f01c7
-							sh->conf->file_mode);
6f01c7
-		if (retval < 0) {
6f01c7
-			goto cleanup;
6f01c7
-		}
6f01c7
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
6f01c7
+						semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL),
6f01c7
+						sh->conf->file_mode);
6f01c7
+	if (retval < 0 && errno != ENOENT) {
6f01c7
+		goto cleanup;
6f01c7
 	}
6f01c7
 
6f01c7
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC);
6f01c7
-	if (access(path, F_OK) == 0) {
6f01c7
-		retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
6f01c7
-							semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),
6f01c7
-							sh->conf->file_mode);
6f01c7
-		if (retval < 0) {
6f01c7
-			goto cleanup;
6f01c7
-		}
6f01c7
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
6f01c7
+						semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC),
6f01c7
+						sh->conf->file_mode);
6f01c7
+	if (retval < 0 && errno != ENOENT) {
6f01c7
+		goto cleanup;
6f01c7
 	}
6f01c7
 
6f01c7
 	path = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
6f01c7
-	if (access(path, F_OK) == 0) {
6f01c7
-		retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
6f01c7
-							semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),
6f01c7
-							sh->conf->file_mode);
6f01c7
-		if (retval < 0) {
6f01c7
-			goto cleanup;
6f01c7
-		}
6f01c7
+	retval = semanage_copy_file(semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
6f01c7
+						semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_SEUSERS),
6f01c7
+						sh->conf->file_mode);
6f01c7
+	if (retval < 0 && errno != ENOENT) {
6f01c7
+		goto cleanup;
6f01c7
 	}
6f01c7
 
6f01c7
 	/* run genhomedircon if its enabled, this should be the last operation
6f01c7
@@ -1415,9 +1533,8 @@ static int semanage_direct_commit(semanage_handle_t * sh)
6f01c7
 	sepol_policydb_free(out);
6f01c7
 	out = NULL;
6f01c7
 
6f01c7
-	if (sh->do_rebuild || modified || bools_modified || fcontexts_modified) {
6f01c7
+	if (do_install)
6f01c7
 		retval = semanage_install_sandbox(sh);
6f01c7
-	}
6f01c7
 
6f01c7
 cleanup:
6f01c7
 	for (i = 0; i < num_modinfos; i++) {
6f01c7
@@ -1429,14 +1546,12 @@ cleanup:
6f01c7
 		free(mod_filenames[i]);
6f01c7
 	}
6f01c7
 
6f01c7
-	if (modified || bools_modified) {
6f01c7
-		/* Detach from policydb, so it can be freed */
6f01c7
-		dbase_policydb_detach((dbase_policydb_t *) pusers_base->dbase);
6f01c7
-		dbase_policydb_detach((dbase_policydb_t *) pports->dbase);
6f01c7
-		dbase_policydb_detach((dbase_policydb_t *) pifaces->dbase);
6f01c7
-		dbase_policydb_detach((dbase_policydb_t *) pnodes->dbase);
6f01c7
-		dbase_policydb_detach((dbase_policydb_t *) pbools->dbase);
6f01c7
-	}
6f01c7
+	/* Detach from policydb, so it can be freed */
6f01c7
+	dbase_policydb_detach((dbase_policydb_t *) pusers_base->dbase);
6f01c7
+	dbase_policydb_detach((dbase_policydb_t *) pports->dbase);
6f01c7
+	dbase_policydb_detach((dbase_policydb_t *) pifaces->dbase);
6f01c7
+	dbase_policydb_detach((dbase_policydb_t *) pnodes->dbase);
6f01c7
+	dbase_policydb_detach((dbase_policydb_t *) pbools->dbase);
6f01c7
 
6f01c7
 	free(mod_filenames);
6f01c7
 	sepol_policydb_free(out);
6f01c7
@@ -1600,7 +1715,8 @@ static int semanage_direct_extract(semanage_handle_t * sh,
6f01c7
 		goto cleanup;
6f01c7
 	}
6f01c7
 
6f01c7
-	if (access(module_path, F_OK) != 0) {
6f01c7
+	struct stat sb;
6f01c7
+	if (stat(module_path, &sb) != 0) {
6f01c7
 		ERR(sh, "Module does not exist: %s", module_path);
6f01c7
 		rc = -1;
6f01c7
 		goto cleanup;
6f01c7
@@ -1630,7 +1746,7 @@ static int semanage_direct_extract(semanage_handle_t * sh,
6f01c7
 		goto cleanup;
6f01c7
 	}
6f01c7
 
6f01c7
-	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && access(input_file, F_OK) != 0) {
6f01c7
+	if (extract_cil == 1 && strcmp(_modinfo->lang_ext, "cil") && stat(input_file, &sb) != 0) {
6f01c7
 		rc = semanage_compile_module(sh, _modinfo);
6f01c7
 		if (rc < 0) {
6f01c7
 			goto cleanup;
6f01c7
@@ -1931,7 +2047,7 @@ int semanage_direct_mls_enabled(semanage_handle_t * sh)
6f01c7
 	if (retval < 0)
6f01c7
 		goto cleanup;
6f01c7
 
6f01c7
-	retval = semanage_read_policydb(sh, p);
6f01c7
+	retval = semanage_read_policydb(sh, p, SEMANAGE_STORE_KERNEL);
6f01c7
 	if (retval < 0)
6f01c7
 		goto cleanup;
6f01c7
 
6f01c7
@@ -2075,6 +2191,31 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
e92a8c
 	free(tmp);
e92a8c
 	tmp = NULL;
e92a8c
 
e92a8c
+	if (strcmp((*modinfo)->lang_ext, "pp") == 0) {
e92a8c
+		/* try to get a module_version from hll file */
e92a8c
+		int data_len, compressed = 0;
e92a8c
+		char *data = NULL;
e92a8c
+		char fhll[PATH_MAX];
e92a8c
+		ret = semanage_module_get_path(sh,
e92a8c
+					       *modinfo,
e92a8c
+					       SEMANAGE_MODULE_PATH_HLL,
e92a8c
+					       fhll,
e92a8c
+					       sizeof(fhll));
e92a8c
+		if (ret == 0 && (access(fhll, R_OK) == 0)) {
e92a8c
+			if ((data_len = map_file(sh, fhll, &data, &compressed)) > 0) {
e92a8c
+
e92a8c
+				char *module_name = NULL;
e92a8c
+				char *version = NULL;
e92a8c
+				ret = parse_module_headers(sh, data, data_len, &module_name, &version);
e92a8c
+				if (ret == 0 && version != NULL) {
e92a8c
+					ret = semanage_module_info_set_version(sh, *modinfo, version);
e92a8c
+				}
e92a8c
+				free(module_name);
e92a8c
+				free(version);
e92a8c
+				munmap(data, data_len);
e92a8c
+			}
e92a8c
+		}
e92a8c
+	}
e92a8c
 	if (fclose(fp) != 0) {
e92a8c
 		ERR(sh,
e92a8c
 		    "Unable to close %s module lang ext file.",
6f01c7
@@ -2613,7 +2754,8 @@ static int semanage_direct_install_info(semanage_handle_t *sh,
6f01c7
 			goto cleanup;
6f01c7
 		}
6f01c7
 
6f01c7
-		if (access(path, F_OK) == 0) {
6f01c7
+		struct stat sb;
6f01c7
+		if (stat(path, &sb) == 0) {
6f01c7
 			ret = unlink(path);
6f01c7
 			if (ret != 0) {
6f01c7
 				ERR(sh, "Error while removing cached CIL file %s: %s", path, strerror(errno));
528c33
diff --git libsemanage-2.5/src/exception.sh libsemanage-2.5/src/exception.sh
528c33
index 94619d2..d18959c 100644
528c33
--- libsemanage-2.5/src/exception.sh
528c33
+++ libsemanage-2.5/src/exception.sh
528c33
@@ -9,6 +9,6 @@ echo "
528c33
 }
528c33
 "
528c33
 }
528c33
-gcc -x c -c -I../include - -aux-info temp.aux < ../include/semanage/semanage.h
528c33
+${CC:-gcc} -x c -c -I../include - -aux-info temp.aux < ../include/semanage/semanage.h
528c33
 for i in `awk '/extern int/ { print $6 }' temp.aux`; do except $i ; done
528c33
 rm -f -- temp.aux -.o
528c33
diff --git libsemanage-2.5/src/genhomedircon.c libsemanage-2.5/src/genhomedircon.c
6f01c7
index 1a9e87e..32d8d5f 100644
528c33
--- libsemanage-2.5/src/genhomedircon.c
528c33
+++ libsemanage-2.5/src/genhomedircon.c
528c33
@@ -48,6 +48,8 @@
528c33
 #include <errno.h>
528c33
 #include <unistd.h>
528c33
 #include <regex.h>
528c33
+#include <grp.h>
528c33
+#include <search.h>
528c33
 
528c33
 /* paths used in get_home_dirs() */
528c33
 #define PATH_ETC_USERADD "/etc/default/useradd"
528c33
@@ -73,37 +75,45 @@
528c33
    which are searched for and replaced */
528c33
 #define TEMPLATE_HOME_ROOT "HOME_ROOT"
528c33
 #define TEMPLATE_HOME_DIR "HOME_DIR"
528c33
+/* these are legacy */
528c33
 #define TEMPLATE_USER "USER"
528c33
 #define TEMPLATE_ROLE "ROLE"
528c33
+/* new names */
528c33
+#define TEMPLATE_USERNAME "%{USERNAME}"
528c33
+#define TEMPLATE_USERID "%{USERID}"
528c33
+
528c33
 #define TEMPLATE_SEUSER "system_u"
528c33
 #define TEMPLATE_LEVEL "s0"
528c33
 
528c33
-#define FALLBACK_USER "user_u"
528c33
-#define FALLBACK_USER_PREFIX "user"
528c33
-#define FALLBACK_USER_LEVEL "s0"
528c33
+#define FALLBACK_SENAME "user_u"
528c33
+#define FALLBACK_PREFIX "user"
528c33
+#define FALLBACK_LEVEL "s0"
528c33
+#define FALLBACK_NAME "[^/]+"
528c33
+#define FALLBACK_UIDGID "[0-9]+"
528c33
 #define DEFAULT_LOGIN "__default__"
528c33
 
528c33
-typedef struct {
528c33
-	const char *fcfilepath;
528c33
-	int usepasswd;
528c33
-	const char *homedir_template_path;
528c33
-	char *fallback_user;
528c33
-	char *fallback_user_prefix;
528c33
-	char *fallback_user_level;
528c33
-	semanage_handle_t *h_semanage;
528c33
-	sepol_policydb_t *policydb;
528c33
-} genhomedircon_settings_t;
528c33
-
528c33
 typedef struct user_entry {
528c33
 	char *name;
528c33
+	char *uid;
528c33
+	char *gid;
528c33
 	char *sename;
528c33
 	char *prefix;
528c33
 	char *home;
528c33
 	char *level;
528c33
+	char *login;
528c33
 	struct user_entry *next;
528c33
 } genhomedircon_user_entry_t;
528c33
 
528c33
 typedef struct {
528c33
+	const char *fcfilepath;
528c33
+	int usepasswd;
528c33
+	const char *homedir_template_path;
528c33
+	genhomedircon_user_entry_t *fallback;
528c33
+	semanage_handle_t *h_semanage;
528c33
+	sepol_policydb_t *policydb;
528c33
+} genhomedircon_settings_t;
528c33
+
528c33
+typedef struct {
528c33
 	const char *search_for;
528c33
 	const char *replace_with;
528c33
 } replacement_pair_t;
528c33
@@ -461,11 +471,29 @@ static int HOME_DIR_PRED(const char *string)
528c33
 	return semanage_is_prefix(string, TEMPLATE_HOME_DIR);
528c33
 }
528c33
 
528c33
+/* new names */
528c33
+static int USERNAME_CONTEXT_PRED(const char *string)
528c33
+{
528c33
+	return (int)(
528c33
+		(strstr(string, TEMPLATE_USERNAME) != NULL) ||
528c33
+		(strstr(string, TEMPLATE_USERID) != NULL)
528c33
+	);
528c33
+}
528c33
+
528c33
+/* This will never match USER if USERNAME or USERID are found. */
528c33
 static int USER_CONTEXT_PRED(const char *string)
528c33
 {
528c33
+	if (USERNAME_CONTEXT_PRED(string))
528c33
+		return 0;
528c33
+
528c33
 	return (int)(strstr(string, TEMPLATE_USER) != NULL);
528c33
 }
528c33
 
528c33
+static int STR_COMPARATOR(const void *a, const void *b)
528c33
+{
528c33
+	return strcmp((const char *) a, (const char *) b);
528c33
+}
528c33
+
528c33
 /* make_tempate
528c33
  * @param	s	  the settings holding the paths to various files
528c33
  * @param	pred	function pointer to function to use as filter for slurp
528c33
@@ -548,23 +576,12 @@ static int check_line(genhomedircon_settings_t * s, Ustr *line)
528c33
 	return result;
528c33
 }
528c33
 
528c33
-static int write_home_dir_context(genhomedircon_settings_t * s, FILE * out,
528c33
-				  semanage_list_t * tpl, const char *user,
528c33
-				  const char *seuser, const char *home,
528c33
-				  const char *role_prefix, const char *level)
528c33
+static int write_replacements(genhomedircon_settings_t * s, FILE * out,
528c33
+			      const semanage_list_t * tpl,
528c33
+			      const replacement_pair_t *repl)
528c33
 {
528c33
-	replacement_pair_t repl[] = {
528c33
-		{.search_for = TEMPLATE_SEUSER,.replace_with = seuser},
528c33
-		{.search_for = TEMPLATE_HOME_DIR,.replace_with = home},
528c33
-		{.search_for = TEMPLATE_ROLE,.replace_with = role_prefix},
528c33
-		{.search_for = TEMPLATE_LEVEL,.replace_with = level},
528c33
-		{NULL, NULL}
528c33
-	};
528c33
 	Ustr *line = USTR_NULL;
528c33
 
528c33
-	if (fprintf(out, COMMENT_USER_HOME_CONTEXT, user) < 0)
528c33
-		return STATUS_ERR;
528c33
-
528c33
 	for (; tpl; tpl = tpl->next) {
528c33
 		line = replace_all(tpl->data, repl);
528c33
 		if (!line)
528c33
@@ -582,6 +599,28 @@ static int write_home_dir_context(genhomedircon_settings_t * s, FILE * out,
528c33
 	return STATUS_ERR;
528c33
 }
528c33
 
528c33
+static int write_home_dir_context(genhomedircon_settings_t * s, FILE * out,
528c33
+				  semanage_list_t * tpl, const genhomedircon_user_entry_t *user)
528c33
+{
528c33
+	replacement_pair_t repl[] = {
528c33
+		{.search_for = TEMPLATE_SEUSER,.replace_with = user->sename},
528c33
+		{.search_for = TEMPLATE_HOME_DIR,.replace_with = user->home},
528c33
+		{.search_for = TEMPLATE_ROLE,.replace_with = user->prefix},
528c33
+		{.search_for = TEMPLATE_LEVEL,.replace_with = user->level},
528c33
+		{NULL, NULL}
528c33
+	};
528c33
+
528c33
+	if (strcmp(user->name, FALLBACK_NAME) == 0) {
528c33
+		if (fprintf(out, COMMENT_USER_HOME_CONTEXT, FALLBACK_SENAME) < 0)
528c33
+			return STATUS_ERR;
528c33
+	} else {
528c33
+		if (fprintf(out, COMMENT_USER_HOME_CONTEXT, user->name) < 0)
528c33
+			return STATUS_ERR;
528c33
+	}
528c33
+
528c33
+	return write_replacements(s, out, tpl, repl);
528c33
+}
528c33
+
528c33
 static int write_home_root_context(genhomedircon_settings_t * s, FILE * out,
528c33
 				   semanage_list_t * tpl, char *homedir)
528c33
 {
528c33
@@ -589,52 +628,54 @@ static int write_home_root_context(genhomedircon_settings_t * s, FILE * out,
528c33
 		{.search_for = TEMPLATE_HOME_ROOT,.replace_with = homedir},
528c33
 		{NULL, NULL}
528c33
 	};
528c33
-	Ustr *line = USTR_NULL;
528c33
 
528c33
-	for (; tpl; tpl = tpl->next) {
528c33
-		line = replace_all(tpl->data, repl);
528c33
-		if (!line)
528c33
-			goto fail;
528c33
-		if (check_line(s, line) == STATUS_SUCCESS) {
528c33
-			if (!ustr_io_putfileline(&line, out))
528c33
-				goto fail;
528c33
-		}
528c33
-		ustr_sc_free(&line);
528c33
-	}
528c33
-	return STATUS_SUCCESS;
528c33
+	return write_replacements(s, out, tpl, repl);
528c33
+}
528c33
 
528c33
-      fail:
528c33
-	ustr_sc_free(&line);
528c33
-	return STATUS_ERR;
528c33
+static int write_username_context(genhomedircon_settings_t * s, FILE * out,
528c33
+				  semanage_list_t * tpl,
528c33
+				  const genhomedircon_user_entry_t *user)
528c33
+{
528c33
+	replacement_pair_t repl[] = {
528c33
+		{.search_for = TEMPLATE_USERNAME,.replace_with = user->name},
528c33
+		{.search_for = TEMPLATE_USERID,.replace_with = user->uid},
528c33
+		{.search_for = TEMPLATE_ROLE,.replace_with = user->prefix},
528c33
+		{.search_for = TEMPLATE_SEUSER,.replace_with = user->sename},
528c33
+		{NULL, NULL}
528c33
+	};
528c33
+
528c33
+	return write_replacements(s, out, tpl, repl);
528c33
 }
528c33
 
528c33
 static int write_user_context(genhomedircon_settings_t * s, FILE * out,
528c33
-			      semanage_list_t * tpl, const char *user,
528c33
-			      const char *seuser, const char *role_prefix)
528c33
+			      semanage_list_t * tpl, const genhomedircon_user_entry_t *user)
528c33
 {
528c33
 	replacement_pair_t repl[] = {
528c33
-		{.search_for = TEMPLATE_USER,.replace_with = user},
528c33
-		{.search_for = TEMPLATE_ROLE,.replace_with = role_prefix},
528c33
-		{.search_for = TEMPLATE_SEUSER,.replace_with = seuser},
528c33
+		{.search_for = TEMPLATE_USER,.replace_with = user->name},
528c33
+		{.search_for = TEMPLATE_ROLE,.replace_with = user->prefix},
528c33
+		{.search_for = TEMPLATE_SEUSER,.replace_with = user->sename},
528c33
 		{NULL, NULL}
528c33
 	};
528c33
-	Ustr *line = USTR_NULL;
528c33
 
528c33
-	for (; tpl; tpl = tpl->next) {
528c33
-		line = replace_all(tpl->data, repl);
528c33
-		if (!line)
528c33
-			goto fail;
528c33
-		if (check_line(s, line) == STATUS_SUCCESS) {
528c33
-			if (!ustr_io_putfileline(&line, out))
528c33
-				goto fail;
528c33
-		}
528c33
-		ustr_sc_free(&line);
528c33
+	return write_replacements(s, out, tpl, repl);
528c33
+}
528c33
+
528c33
+static int seuser_sort_func(const void *arg1, const void *arg2)
528c33
+{
528c33
+	const semanage_seuser_t **u1 = (const semanage_seuser_t **) arg1;
528c33
+	const semanage_seuser_t **u2 = (const semanage_seuser_t **) arg2;;
528c33
+	const char *name1 = semanage_seuser_get_name(*u1);
528c33
+	const char *name2 = semanage_seuser_get_name(*u2);
528c33
+
528c33
+	if (name1[0] == '%' && name2[0] == '%') {
528c33
+		return 0;
528c33
+	} else if (name1[0] == '%') {
528c33
+		return 1;
528c33
+	} else if (name2[0] == '%') {
528c33
+		return -1;
528c33
 	}
528c33
-	return STATUS_SUCCESS;
528c33
 
528c33
-      fail:
528c33
-	ustr_sc_free(&line);
528c33
-	return STATUS_ERR;
528c33
+	return strcmp(name1, name2);
528c33
 }
528c33
 
528c33
 static int user_sort_func(semanage_user_t ** arg1, semanage_user_t ** arg2)
528c33
@@ -649,15 +690,19 @@ static int name_user_cmp(char *key, semanage_user_t ** val)
528c33
 }
528c33
 
528c33
 static int push_user_entry(genhomedircon_user_entry_t ** list, const char *n,
528c33
-			   const char *sen, const char *pre, const char *h,
528c33
-			   const char *l)
528c33
+			   const char *u, const char *g, const char *sen,
528c33
+			   const char *pre, const char *h, const char *l,
528c33
+			   const char *ln)
528c33
 {
528c33
 	genhomedircon_user_entry_t *temp = NULL;
528c33
 	char *name = NULL;
528c33
+	char *uid = NULL;
528c33
+	char *gid = NULL;
528c33
 	char *sename = NULL;
528c33
 	char *prefix = NULL;
528c33
 	char *home = NULL;
528c33
 	char *level = NULL;
528c33
+	char *lname = NULL;
528c33
 
528c33
 	temp = malloc(sizeof(genhomedircon_user_entry_t));
528c33
 	if (!temp)
528c33
@@ -665,6 +710,12 @@ static int push_user_entry(genhomedircon_user_entry_t ** list, const char *n,
528c33
 	name = strdup(n);
528c33
 	if (!name)
528c33
 		goto cleanup;
528c33
+	uid = strdup(u);
528c33
+	if (!uid)
528c33
+		goto cleanup;
528c33
+	gid = strdup(g);
528c33
+	if (!gid)
528c33
+		goto cleanup;
528c33
 	sename = strdup(sen);
528c33
 	if (!sename)
528c33
 		goto cleanup;
528c33
@@ -677,12 +728,18 @@ static int push_user_entry(genhomedircon_user_entry_t ** list, const char *n,
528c33
 	level = strdup(l);
528c33
 	if (!level)
528c33
 		goto cleanup;
528c33
+	lname = strdup(ln);
528c33
+	if (!lname)
528c33
+		goto cleanup;
528c33
 
528c33
 	temp->name = name;
528c33
+	temp->uid = uid;
528c33
+	temp->gid = gid;
528c33
 	temp->sename = sename;
528c33
 	temp->prefix = prefix;
528c33
 	temp->home = home;
528c33
 	temp->level = level;
528c33
+	temp->login = lname;
528c33
 	temp->next = (*list);
528c33
 	(*list) = temp;
528c33
 
528c33
@@ -690,10 +747,13 @@ static int push_user_entry(genhomedircon_user_entry_t ** list, const char *n,
528c33
 
528c33
       cleanup:
528c33
 	free(name);
528c33
+	free(uid);
528c33
+	free(gid);
528c33
 	free(sename);
528c33
 	free(prefix);
528c33
 	free(home);
528c33
 	free(level);
528c33
+	free(lname);
528c33
 	free(temp);
528c33
 	return STATUS_ERR;
528c33
 }
528c33
@@ -708,39 +768,16 @@ static void pop_user_entry(genhomedircon_user_entry_t ** list)
528c33
 	temp = *list;
528c33
 	*list = temp->next;
528c33
 	free(temp->name);
528c33
+	free(temp->uid);
528c33
+	free(temp->gid);
528c33
 	free(temp->sename);
528c33
 	free(temp->prefix);
528c33
 	free(temp->home);
528c33
 	free(temp->level);
528c33
+	free(temp->login);
528c33
 	free(temp);
528c33
 }
528c33
 
528c33
-static int set_fallback_user(genhomedircon_settings_t *s, const char *user,
528c33
-			     const char *prefix, const char *level)
528c33
-{
528c33
-	char *fallback_user = strdup(user);
528c33
-	char *fallback_user_prefix = strdup(prefix);
528c33
-	char *fallback_user_level = NULL;
528c33
-	if (level) 
528c33
-		fallback_user_level = strdup(level);
528c33
-
528c33
-	if (fallback_user == NULL || fallback_user_prefix == NULL ||
528c33
-	    (fallback_user_level == NULL && level != NULL)) {
528c33
-		free(fallback_user);
528c33
-		free(fallback_user_prefix);
528c33
-		free(fallback_user_level);
528c33
-		return STATUS_ERR;
528c33
-	}
528c33
-
528c33
-	free(s->fallback_user);
528c33
-	free(s->fallback_user_prefix);
528c33
-	free(s->fallback_user_level);
528c33
-	s->fallback_user = fallback_user;
528c33
-	s->fallback_user_prefix = fallback_user_prefix;
528c33
-	s->fallback_user_level = fallback_user_level;
528c33
-	return STATUS_SUCCESS;
528c33
-}
528c33
-
528c33
 static int setup_fallback_user(genhomedircon_settings_t * s)
528c33
 {
528c33
 	semanage_seuser_t **seuser_list = NULL;
528c33
@@ -775,17 +812,20 @@ static int setup_fallback_user(genhomedircon_settings_t * s)
528c33
 			if (semanage_user_query(s->h_semanage, key, &u) < 0)
528c33
 			{
528c33
 				prefix = name;
528c33
-				level = FALLBACK_USER_LEVEL;
528c33
+				level = FALLBACK_LEVEL;
528c33
 			}
528c33
 			else
528c33
 			{
528c33
 				prefix = semanage_user_get_prefix(u);
528c33
 				level = semanage_user_get_mlslevel(u);
528c33
 				if (!level)
528c33
-					level = FALLBACK_USER_LEVEL;
528c33
+					level = FALLBACK_LEVEL;
528c33
 			}
528c33
 
528c33
-			if (set_fallback_user(s, seuname, prefix, level) != 0)
528c33
+			if (push_user_entry(&(s->fallback), FALLBACK_NAME,
528c33
+					    FALLBACK_UIDGID, FALLBACK_UIDGID,
528c33
+					    seuname, prefix, "", level,
528c33
+					    FALLBACK_NAME) != 0)
528c33
 				errors = STATUS_ERR;
528c33
 			semanage_user_key_free(key);
528c33
 			if (u)
6f01c7
@@ -801,6 +841,212 @@ static int setup_fallback_user(genhomedircon_settings_t * s)
528c33
 	return errors;
528c33
 }
528c33
 
528c33
+static genhomedircon_user_entry_t *find_user(genhomedircon_user_entry_t *head,
528c33
+					     const char *name)
528c33
+{
528c33
+	for(; head; head = head->next) {
528c33
+		if (strcmp(head->name, name) == 0) {
528c33
+			return head;
528c33
+		}
528c33
+	}
528c33
+
528c33
+	return NULL;
528c33
+}
528c33
+
528c33
+static int add_user(genhomedircon_settings_t * s,
528c33
+		    genhomedircon_user_entry_t **head,
528c33
+		    semanage_user_t *user,
528c33
+		    const char *name,
528c33
+		    const char *sename,
528c33
+		    const char *selogin)
528c33
+{
528c33
+	if (selogin[0] == '%') {
528c33
+		genhomedircon_user_entry_t *orig = find_user(*head, name);
528c33
+		if (orig != NULL && orig->login[0] == '%') {
528c33
+			ERR(s->h_semanage, "User %s is already mapped to"
528c33
+			    " group %s, but also belongs to group %s. Add an"
528c33
+			    " explicit mapping for this user to"
528c33
+			    " override group mappings.",
528c33
+			    name, orig->login + 1, selogin + 1);
528c33
+			return STATUS_ERR;
528c33
+		} else if (orig != NULL) {
528c33
+			// user mappings take precedence
528c33
+			return STATUS_SUCCESS;
528c33
+		}
528c33
+	}
528c33
+
528c33
+	int retval = STATUS_ERR;
528c33
+
528c33
+	char *rbuf = NULL;
528c33
+	long rbuflen;
528c33
+	struct passwd pwstorage, *pwent = NULL;
528c33
+	const char *prefix = NULL;
528c33
+	const char *level = NULL;
528c33
+	char uid[11];
528c33
+	char gid[11];
528c33
+
528c33
+	/* Allocate space for the getpwnam_r buffer */
528c33
+	rbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
528c33
+	if (rbuflen <= 0)
528c33
+		goto cleanup;
528c33
+	rbuf = malloc(rbuflen);
528c33
+	if (rbuf == NULL)
528c33
+		goto cleanup;
528c33
+
528c33
+	if (user) {
528c33
+		prefix = semanage_user_get_prefix(user);
528c33
+		level = semanage_user_get_mlslevel(user);
528c33
+
528c33
+		if (!level) {
528c33
+			level = FALLBACK_LEVEL;
528c33
+		}
528c33
+	} else {
528c33
+		prefix = name;
528c33
+		level = FALLBACK_LEVEL;
528c33
+	}
528c33
+
528c33
+	retval = getpwnam_r(name, &pwstorage, rbuf, rbuflen, &pwent);
528c33
+	if (retval != 0 || pwent == NULL) {
528c33
+		if (retval != 0 && retval != ENOENT) {
528c33
+			goto cleanup;
528c33
+		}
528c33
+
528c33
+		WARN(s->h_semanage,
528c33
+		     "user %s not in password file", name);
528c33
+		retval = STATUS_SUCCESS;
528c33
+		goto cleanup;
528c33
+	}
528c33
+
528c33
+	int len = strlen(pwent->pw_dir) -1;
528c33
+	for(; len > 0 && pwent->pw_dir[len] == '/'; len--) {
528c33
+		pwent->pw_dir[len] = '\0';
528c33
+	}
528c33
+
528c33
+	if (strcmp(pwent->pw_dir, "/") == 0) {
528c33
+		/* don't relabel / genhomdircon checked to see if root
528c33
+		 * was the user and if so, set his home directory to
528c33
+		 * /root */
528c33
+		retval = STATUS_SUCCESS;
528c33
+		goto cleanup;
528c33
+	}
528c33
+
528c33
+	if (ignore(pwent->pw_dir)) {
528c33
+		retval = STATUS_SUCCESS;
528c33
+		goto cleanup;
528c33
+	}
528c33
+
528c33
+	len = snprintf(uid, sizeof(uid), "%u", pwent->pw_uid);
528c33
+	if (len < 0 || len >= (int)sizeof(uid)) {
528c33
+		goto cleanup;
528c33
+	}
528c33
+
528c33
+	len = snprintf(gid, sizeof(gid), "%u", pwent->pw_gid);
528c33
+	if (len < 0 || len >= (int)sizeof(gid)) {
528c33
+		goto cleanup;
528c33
+	}
528c33
+
528c33
+	retval = push_user_entry(head, name, uid, gid, sename, prefix,
528c33
+				pwent->pw_dir, level, selogin);
528c33
+cleanup:
528c33
+	free(rbuf);
528c33
+	return retval;
528c33
+}
528c33
+
528c33
+static int get_group_users(genhomedircon_settings_t * s,
528c33
+			  genhomedircon_user_entry_t **head,
528c33
+			  semanage_user_t *user,
528c33
+			  const char *sename,
528c33
+			  const char *selogin)
528c33
+{
528c33
+	int retval = STATUS_ERR;
528c33
+	unsigned int i;
528c33
+
528c33
+	long grbuflen;
528c33
+	char *grbuf = NULL;
528c33
+	struct group grstorage, *group = NULL;
528c33
+
528c33
+	long prbuflen;
528c33
+	char *pwbuf = NULL;
528c33
+	struct passwd pwstorage, *pw = NULL;
528c33
+
528c33
+	grbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
528c33
+	if (grbuflen <= 0)
528c33
+		goto cleanup;
528c33
+	grbuf = malloc(grbuflen);
528c33
+	if (grbuf == NULL)
528c33
+		goto cleanup;
528c33
+
528c33
+	const char *grname = selogin + 1;
528c33
+
6f01c7
+	errno = 0;
6f01c7
+	while (
6f01c7
+		(retval = getgrnam_r(grname, &grstorage, grbuf, (size_t) grbuflen, &group)) != 0 &&
6f01c7
+		errno == ERANGE
6f01c7
+	) {
6f01c7
+		char *new_grbuf;
6f01c7
+		grbuflen *= 2;
6f01c7
+		new_grbuf = realloc(grbuf, grbuflen);
6f01c7
+		if (new_grbuf == NULL)
6f01c7
+			goto cleanup;
6f01c7
+		grbuf = new_grbuf;
528c33
+	}
6f01c7
+	if (retval == -1)
6f01c7
+		goto cleanup;
528c33
+
528c33
+	if (group == NULL) {
528c33
+		ERR(s->h_semanage, "Can't find group named %s\n", grname);
528c33
+		goto cleanup;
528c33
+	}
528c33
+
528c33
+	size_t nmembers = 0;
528c33
+	char **members = group->gr_mem;
528c33
+
528c33
+	while (*members != NULL) {
528c33
+		nmembers++;
528c33
+		members++;
528c33
+	}
528c33
+
528c33
+	for (i = 0; i < nmembers; i++) {
528c33
+		const char *uname = group->gr_mem[i];
528c33
+
528c33
+		if (add_user(s, head, user, uname, sename, selogin) < 0) {
528c33
+			goto cleanup;
528c33
+		}
528c33
+	}
528c33
+
528c33
+	prbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
528c33
+	if (prbuflen <= 0)
528c33
+		goto cleanup;
528c33
+	pwbuf = malloc(prbuflen);
528c33
+	if (pwbuf == NULL)
528c33
+		goto cleanup;
528c33
+
528c33
+	setpwent();
528c33
+	while ((retval = getpwent_r(&pwstorage, pwbuf, prbuflen, &pw)) == 0) {
528c33
+		// skip users who also have this group as their
528c33
+		// primary group
528c33
+		if (lfind(pw->pw_name, group->gr_mem, &nmembers,
528c33
+			  sizeof(char *), &STR_COMPARATOR)) {
528c33
+			continue;
528c33
+		}
528c33
+
528c33
+		if (group->gr_gid == pw->pw_gid) {
528c33
+			if (add_user(s, head, user, pw->pw_name,
528c33
+				     sename, selogin) < 0) {
528c33
+				goto cleanup;
528c33
+			}
528c33
+		}
528c33
+	}
528c33
+
528c33
+	retval = STATUS_SUCCESS;
528c33
+cleanup:
528c33
+	endpwent();
528c33
+	free(pwbuf);
528c33
+	free(grbuf);
528c33
+
528c33
+	return retval;
528c33
+}
528c33
+
528c33
 static genhomedircon_user_entry_t *get_users(genhomedircon_settings_t * s,
528c33
 					     int *errors)
528c33
 {
6f01c7
@@ -812,12 +1058,7 @@ static genhomedircon_user_entry_t *get_users(genhomedircon_settings_t * s,
528c33
 	semanage_user_t **u = NULL;
528c33
 	const char *name = NULL;
528c33
 	const char *seuname = NULL;
528c33
-	const char *prefix = NULL;
528c33
-	const char *level = NULL;
528c33
-	struct passwd pwstorage, *pwent = NULL;
528c33
 	unsigned int i;
528c33
-	long rbuflen;
528c33
-	char *rbuf = NULL;
528c33
 	int retval;
528c33
 
528c33
 	*errors = 0;
6f01c7
@@ -831,82 +1072,42 @@ static genhomedircon_user_entry_t *get_users(genhomedircon_settings_t * s,
528c33
 		nusers = 0;
528c33
 	}
528c33
 
528c33
+	qsort(seuser_list, nseusers, sizeof(semanage_seuser_t *),
528c33
+	      &seuser_sort_func);
528c33
 	qsort(user_list, nusers, sizeof(semanage_user_t *),
528c33
 	      (int (*)(const void *, const void *))&user_sort_func);
528c33
 
528c33
-	/* Allocate space for the getpwnam_r buffer */
528c33
-	rbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
528c33
-	if (rbuflen <= 0)
528c33
-		goto cleanup;
528c33
-	rbuf = malloc(rbuflen);
528c33
-	if (rbuf == NULL)
528c33
-		goto cleanup;
528c33
-
528c33
 	for (i = 0; i < nseusers; i++) {
528c33
 		seuname = semanage_seuser_get_sename(seuser_list[i]);
528c33
 		name = semanage_seuser_get_name(seuser_list[i]);
528c33
 
528c33
-		if (strcmp(name,"root") && strcmp(seuname, s->fallback_user) == 0)
528c33
-			continue;
528c33
-
528c33
 		if (strcmp(name, DEFAULT_LOGIN) == 0)
528c33
 			continue;
528c33
 
528c33
 		if (strcmp(name, TEMPLATE_SEUSER) == 0)
528c33
 			continue;
528c33
 
528c33
-		/* %groupname syntax */
528c33
-		if (name[0] == '%')
528c33
-			continue;
528c33
-
528c33
 		/* find the user structure given the name */
528c33
 		u = bsearch(seuname, user_list, nusers, sizeof(semanage_user_t *),
528c33
 			    (int (*)(const void *, const void *))
528c33
 			    &name_user_cmp);
528c33
-		if (u) {
528c33
-			prefix = semanage_user_get_prefix(*u);
528c33
-			level = semanage_user_get_mlslevel(*u);
528c33
-			if (!level)
528c33
-				level = FALLBACK_USER_LEVEL;
528c33
-		} else {
528c33
-			prefix = name;
528c33
-			level = FALLBACK_USER_LEVEL;
528c33
-		}
528c33
-
528c33
-		retval = getpwnam_r(name, &pwstorage, rbuf, rbuflen, &pwent);
528c33
-		if (retval != 0 || pwent == NULL) {
528c33
-			if (retval != 0 && retval != ENOENT) {
528c33
-				*errors = STATUS_ERR;
528c33
-				goto cleanup;
528c33
-			}
528c33
-
528c33
-			WARN(s->h_semanage,
528c33
-			     "user %s not in password file", name);
528c33
-			continue;
528c33
-		}
528c33
 
528c33
-		int len = strlen(pwent->pw_dir) -1;
528c33
-		for(; len > 0 && pwent->pw_dir[len] == '/'; len--) {
528c33
-			pwent->pw_dir[len] = '\0';
528c33
+		/* %groupname syntax */
528c33
+		if (name[0] == '%') {
528c33
+			retval = get_group_users(s, &head, *u, seuname,
528c33
+						name);
528c33
+		} else {
528c33
+			retval = add_user(s, &head, *u, name,
528c33
+					  seuname, name);
528c33
 		}
528c33
 
528c33
-		if (strcmp(pwent->pw_dir, "/") == 0) {
528c33
-			/* don't relabel / genhomdircon checked to see if root
528c33
-			 * was the user and if so, set his home directory to
528c33
-			 * /root */
528c33
-			continue;
528c33
-		}
528c33
-		if (ignore(pwent->pw_dir))
528c33
-			continue;
528c33
-		if (push_user_entry(&head, name, seuname,
528c33
-				    prefix, pwent->pw_dir, level) != STATUS_SUCCESS) {
528c33
+		if (retval != 0) {
528c33
 			*errors = STATUS_ERR;
528c33
-			break;
528c33
+			goto cleanup;
528c33
 		}
528c33
 	}
528c33
 
528c33
       cleanup:
528c33
-	free(rbuf);
528c33
 	if (*errors) {
528c33
 		for (; head; pop_user_entry(&head)) {
528c33
 			/* the pop function takes care of all the cleanup
6f01c7
@@ -927,6 +1128,7 @@ static genhomedircon_user_entry_t *get_users(genhomedircon_settings_t * s,
528c33
 }
528c33
 
528c33
 static int write_gen_home_dir_context(genhomedircon_settings_t * s, FILE * out,
528c33
+				      semanage_list_t * username_context_tpl,
528c33
 				      semanage_list_t * user_context_tpl,
528c33
 				      semanage_list_t * homedir_context_tpl)
528c33
 {
6f01c7
@@ -939,13 +1141,11 @@ static int write_gen_home_dir_context(genhomedircon_settings_t * s, FILE * out,
528c33
 	}
528c33
 
528c33
 	for (; users; pop_user_entry(&users)) {
528c33
-		if (write_home_dir_context(s, out, homedir_context_tpl,
528c33
-					   users->name,
528c33
-					   users->sename, users->home,
528c33
-					   users->prefix, users->level))
528c33
+		if (write_home_dir_context(s, out, homedir_context_tpl, users))
528c33
 			goto err;
528c33
-		if (write_user_context(s, out, user_context_tpl, users->name,
528c33
-				       users->sename, users->prefix))
528c33
+		if (write_username_context(s, out, username_context_tpl, users))
528c33
+			goto err;
528c33
+		if (write_user_context(s, out, user_context_tpl, users))
528c33
 			goto err;
528c33
 	}
528c33
 
6f01c7
@@ -968,16 +1168,21 @@ static int write_context_file(genhomedircon_settings_t * s, FILE * out)
528c33
 {
528c33
 	semanage_list_t *homedirs = NULL;
528c33
 	semanage_list_t *h = NULL;
528c33
-	semanage_list_t *user_context_tpl = NULL;
528c33
 	semanage_list_t *homedir_context_tpl = NULL;
528c33
 	semanage_list_t *homeroot_context_tpl = NULL;
528c33
+	semanage_list_t *username_context_tpl = NULL;
528c33
+	semanage_list_t *user_context_tpl = NULL;
528c33
 	int retval = STATUS_SUCCESS;
528c33
 
528c33
 	homedir_context_tpl = make_template(s, &HOME_DIR_PRED);
528c33
 	homeroot_context_tpl = make_template(s, &HOME_ROOT_PRED);
528c33
+	username_context_tpl = make_template(s, &USERNAME_CONTEXT_PRED);
528c33
 	user_context_tpl = make_template(s, &USER_CONTEXT_PRED);
528c33
 
528c33
-	if (!homedir_context_tpl && !homeroot_context_tpl && !user_context_tpl)
528c33
+	if (!homedir_context_tpl
528c33
+	 && !homeroot_context_tpl
528c33
+	 && !username_context_tpl
528c33
+	 && !user_context_tpl)
528c33
 		goto done;
528c33
 
528c33
 	if (write_file_context_header(out) != STATUS_SUCCESS) {
6f01c7
@@ -1001,19 +1206,19 @@ static int write_context_file(genhomedircon_settings_t * s, FILE * out)
528c33
 		for (h = homedirs; h; h = h->next) {
528c33
 			Ustr *temp = ustr_dup_cstr(h->data);
528c33
 
528c33
-			if (!temp || !ustr_add_cstr(&temp, "/[^/]*")) {
528c33
+			if (!temp || !ustr_add_cstr(&temp, "/" FALLBACK_NAME)) {
528c33
 				ustr_sc_free(&temp);
528c33
 				retval = STATUS_ERR;
528c33
 				goto done;
528c33
 			}
528c33
 
528c33
-			if (write_home_dir_context(s, out,
528c33
-						   homedir_context_tpl,
528c33
-						   s->fallback_user, s->fallback_user,
528c33
-						   ustr_cstr(temp),
528c33
-						   s->fallback_user_prefix, s->fallback_user_level) !=
528c33
-			    STATUS_SUCCESS) {
528c33
+			free(s->fallback->home);
528c33
+			s->fallback->home = (char*) ustr_cstr(temp);
528c33
+
528c33
+			if (write_home_dir_context(s, out, homedir_context_tpl,
528c33
+						   s->fallback) != STATUS_SUCCESS) {
528c33
 				ustr_sc_free(&temp);
528c33
+				s->fallback->home = NULL;
528c33
 				retval = STATUS_ERR;
528c33
 				goto done;
528c33
 			}
6f01c7
@@ -1021,23 +1226,31 @@ static int write_context_file(genhomedircon_settings_t * s, FILE * out)
528c33
 						    homeroot_context_tpl,
528c33
 						    h->data) != STATUS_SUCCESS) {
528c33
 				ustr_sc_free(&temp);
528c33
+				s->fallback->home = NULL;
528c33
 				retval = STATUS_ERR;
528c33
 				goto done;
528c33
 			}
528c33
 
528c33
 			ustr_sc_free(&temp);
528c33
+			s->fallback->home = NULL;
528c33
 		}
528c33
 	}
528c33
-	if (user_context_tpl) {
528c33
+	if (user_context_tpl || username_context_tpl) {
528c33
+		if (write_username_context(s, out, username_context_tpl,
528c33
+					   s->fallback) != STATUS_SUCCESS) {
528c33
+			retval = STATUS_ERR;
528c33
+			goto done;
528c33
+		}
528c33
+
528c33
 		if (write_user_context(s, out, user_context_tpl,
528c33
-				       ".*", s->fallback_user,
528c33
-				       s->fallback_user_prefix) != STATUS_SUCCESS) {
528c33
+				       s->fallback) != STATUS_SUCCESS) {
528c33
 			retval = STATUS_ERR;
528c33
 			goto done;
528c33
 		}
528c33
 
528c33
-		if (write_gen_home_dir_context(s, out, user_context_tpl,
528c33
-					       homedir_context_tpl) != STATUS_SUCCESS) {
528c33
+		if (write_gen_home_dir_context(s, out, username_context_tpl,
528c33
+					       user_context_tpl, homedir_context_tpl)
528c33
+				!= STATUS_SUCCESS) {
528c33
 			retval = STATUS_ERR;
528c33
 		}
528c33
 	}
6f01c7
@@ -1045,6 +1258,7 @@ static int write_context_file(genhomedircon_settings_t * s, FILE * out)
528c33
 done:
528c33
 	/* Cleanup */
528c33
 	semanage_list_destroy(&homedirs);
528c33
+	semanage_list_destroy(&username_context_tpl);
528c33
 	semanage_list_destroy(&user_context_tpl);
528c33
 	semanage_list_destroy(&homedir_context_tpl);
528c33
 	semanage_list_destroy(&homeroot_context_tpl);
6f01c7
@@ -1068,10 +1282,20 @@ int semanage_genhomedircon(semanage_handle_t * sh,
528c33
 	s.fcfilepath = semanage_final_path(SEMANAGE_FINAL_TMP,
528c33
 					   SEMANAGE_FC_HOMEDIRS);
528c33
 
528c33
-	s.fallback_user = strdup(FALLBACK_USER);
528c33
-	s.fallback_user_prefix = strdup(FALLBACK_USER_PREFIX);
528c33
-	s.fallback_user_level = strdup(FALLBACK_USER_LEVEL);
528c33
-	if (s.fallback_user == NULL || s.fallback_user_prefix == NULL || s.fallback_user_level == NULL) {
528c33
+	s.fallback = calloc(1, sizeof(genhomedircon_user_entry_t));
528c33
+	if (s.fallback == NULL) {
528c33
+		retval = STATUS_ERR;
528c33
+		goto done;
528c33
+	}
528c33
+
528c33
+	s.fallback->name = strdup(FALLBACK_NAME);
528c33
+	s.fallback->sename = strdup(FALLBACK_SENAME);
528c33
+	s.fallback->prefix = strdup(FALLBACK_PREFIX);
528c33
+	s.fallback->level = strdup(FALLBACK_LEVEL);
528c33
+	if (s.fallback->name == NULL
528c33
+	 || s.fallback->sename == NULL
528c33
+	 || s.fallback->prefix == NULL
528c33
+	 || s.fallback->level == NULL) {
528c33
 		retval = STATUS_ERR;
528c33
 		goto done;
528c33
 	}
6f01c7
@@ -1095,9 +1319,7 @@ done:
528c33
 	if (out != NULL)
528c33
 		fclose(out);
528c33
 
528c33
-	free(s.fallback_user);
528c33
-	free(s.fallback_user_prefix);
528c33
-	free(s.fallback_user_level);
528c33
+	pop_user_entry(&(s.fallback));
528c33
 	ignore_free();
528c33
 
528c33
 	return retval;
e92a8c
diff --git libsemanage-2.5/src/libsemanage.map libsemanage-2.5/src/libsemanage.map
e92a8c
index 34b553d..41841e1 100644
e92a8c
--- libsemanage-2.5/src/libsemanage.map
e92a8c
+++ libsemanage-2.5/src/libsemanage.map
e92a8c
@@ -40,10 +40,12 @@ LIBSEMANAGE_1.1 {
e92a8c
 	  semanage_module_info_destroy;
e92a8c
 	  semanage_module_info_get_priority;
e92a8c
 	  semanage_module_info_get_name;
e92a8c
+	  semanage_module_info_get_version;
e92a8c
 	  semanage_module_info_get_lang_ext;
e92a8c
 	  semanage_module_info_get_enabled;
e92a8c
 	  semanage_module_info_set_priority;
e92a8c
 	  semanage_module_info_set_name;
e92a8c
+	  semanage_module_info_set_version;
e92a8c
 	  semanage_module_info_set_lang_ext;
e92a8c
 	  semanage_module_info_set_enabled;
e92a8c
 	  semanage_module_key_create;
e92a8c
diff --git libsemanage-2.5/src/module_internal.h libsemanage-2.5/src/module_internal.h
e92a8c
index c99f6c2..d62091a 100644
e92a8c
--- libsemanage-2.5/src/module_internal.h
e92a8c
+++ libsemanage-2.5/src/module_internal.h
e92a8c
@@ -11,10 +11,12 @@ hidden_proto(semanage_module_get_name)
e92a8c
     hidden_proto(semanage_module_info_destroy)
e92a8c
     hidden_proto(semanage_module_info_get_priority)
e92a8c
     hidden_proto(semanage_module_info_get_name)
e92a8c
+    hidden_proto(semanage_module_info_get_version)
e92a8c
     hidden_proto(semanage_module_info_get_lang_ext)
e92a8c
     hidden_proto(semanage_module_info_get_enabled)
e92a8c
     hidden_proto(semanage_module_info_set_priority)
e92a8c
     hidden_proto(semanage_module_info_set_name)
e92a8c
+    hidden_proto(semanage_module_info_set_version)
e92a8c
     hidden_proto(semanage_module_info_set_lang_ext)
e92a8c
     hidden_proto(semanage_module_info_set_enabled)
e92a8c
     hidden_proto(semanage_module_key_create)
e92a8c
diff --git libsemanage-2.5/src/modules.c libsemanage-2.5/src/modules.c
e92a8c
index 90c5e49..85285a1 100644
e92a8c
--- libsemanage-2.5/src/modules.c
e92a8c
+++ libsemanage-2.5/src/modules.c
e92a8c
@@ -291,6 +291,7 @@ int semanage_module_info_destroy(semanage_handle_t *sh,
e92a8c
 	}
e92a8c
 
e92a8c
 	free(modinfo->name);
e92a8c
+	free(modinfo->module_version);
e92a8c
 	free(modinfo->lang_ext);
e92a8c
 
e92a8c
 	return semanage_module_info_init(sh, modinfo);
e92a8c
@@ -306,6 +307,7 @@ int semanage_module_info_init(semanage_handle_t *sh,
e92a8c
 
e92a8c
 	modinfo->priority = 0;
e92a8c
 	modinfo->name = NULL;
e92a8c
+	modinfo->module_version = NULL;
e92a8c
 	modinfo->lang_ext = NULL;
e92a8c
 	modinfo->enabled = -1;
e92a8c
 
e92a8c
@@ -341,6 +343,14 @@ int semanage_module_info_clone(semanage_handle_t *sh,
e92a8c
 		goto cleanup;
e92a8c
 	}
e92a8c
 
e92a8c
+	if (source->module_version != NULL) {
e92a8c
+		ret = semanage_module_info_set_version(sh, target, source->module_version);
e92a8c
+		if (ret != 0) {
e92a8c
+			status = -1;
e92a8c
+			goto cleanup;
e92a8c
+		}
e92a8c
+	}
e92a8c
+
e92a8c
 	ret = semanage_module_info_set_lang_ext(sh, target, source->lang_ext);
e92a8c
 	if (ret != 0) {
e92a8c
 		status = -1;
e92a8c
@@ -388,6 +398,21 @@ int semanage_module_info_get_name(semanage_handle_t *sh,
e92a8c
 
e92a8c
 hidden_def(semanage_module_info_get_name)
e92a8c
 
e92a8c
+int semanage_module_info_get_version(semanage_handle_t *sh,
e92a8c
+				  semanage_module_info_t *modinfo,
e92a8c
+				  const char **version)
e92a8c
+{
e92a8c
+	assert(sh);
e92a8c
+	assert(modinfo);
e92a8c
+	assert(version);
e92a8c
+
e92a8c
+	*version = modinfo->module_version;
e92a8c
+
e92a8c
+	return 0;
e92a8c
+}
e92a8c
+
e92a8c
+hidden_def(semanage_module_info_get_version)
e92a8c
+
e92a8c
 int semanage_module_info_get_lang_ext(semanage_handle_t *sh,
e92a8c
 				      semanage_module_info_t *modinfo,
e92a8c
 				      const char **lang_ext)
e92a8c
@@ -470,6 +495,37 @@ int semanage_module_info_set_name(semanage_handle_t *sh,
e92a8c
 
e92a8c
 hidden_def(semanage_module_info_set_name)
e92a8c
 
e92a8c
+int semanage_module_info_set_version(semanage_handle_t *sh,
e92a8c
+                                    semanage_module_info_t *modinfo,
e92a8c
+                                    const char *version)
e92a8c
+{
e92a8c
+       assert(sh);
e92a8c
+       assert(modinfo);
e92a8c
+       assert(version);
e92a8c
+
e92a8c
+       char * tmp;
e92a8c
+
e92a8c
+       /* Verify version */
e92a8c
+
e92a8c
+       if (semanage_module_validate_version(version) < 0) {
e92a8c
+               errno = 0;
e92a8c
+               return -1;
e92a8c
+       }
e92a8c
+
e92a8c
+       tmp = strdup(version);
e92a8c
+       if (!tmp) {
e92a8c
+               return -1;
e92a8c
+       }
e92a8c
+
e92a8c
+       free(modinfo->module_version);
e92a8c
+       modinfo->module_version = tmp;
e92a8c
+
e92a8c
+       return 0;
e92a8c
+}
e92a8c
+
e92a8c
+hidden_def(semanage_module_info_set_version)
e92a8c
+
e92a8c
+
e92a8c
 int semanage_module_info_set_lang_ext(semanage_handle_t *sh,
e92a8c
 				      semanage_module_info_t *modinfo,
e92a8c
 				      const char *lang_ext)
e92a8c
@@ -1064,6 +1120,49 @@ exit:
e92a8c
 	return status;
e92a8c
 }
e92a8c
 
e92a8c
+/* Validate version.
e92a8c
+ *
e92a8c
+ * A version must match the following regular expression to be
e92a8c
+ * considered valid:
e92a8c
+ *
e92a8c
+ * ^[:print:]+$
e92a8c
+ *
e92a8c
+ * returns 0 if version is valid, returns -1 otherwise.
e92a8c
+ */
e92a8c
+int semanage_module_validate_version(const char *version)
e92a8c
+{
e92a8c
+	int status = 0;
e92a8c
+
e92a8c
+	if (version == NULL) {
e92a8c
+		status = -1;
e92a8c
+		goto exit;
e92a8c
+	}
e92a8c
+
e92a8c
+	/* must start with a printable char */
e92a8c
+	if (!isprint(*version)) {
e92a8c
+		status = -1;
e92a8c
+		goto exit;
e92a8c
+	}
e92a8c
+
e92a8c
+	/* everything else must be printable */
e92a8c
+#define ISVALIDCHAR(c) (isprint(c))
e92a8c
+
e92a8c
+	for (version++; *version; version++) {
e92a8c
+		if (ISVALIDCHAR(*version)) {
e92a8c
+			continue;
e92a8c
+		}
e92a8c
+		status = -1;
e92a8c
+		goto exit;
e92a8c
+	}
e92a8c
+
e92a8c
+#undef ISVALIDCHAR
e92a8c
+
e92a8c
+exit:
e92a8c
+	return status;
e92a8c
+}
e92a8c
+
e92a8c
+
e92a8c
+
e92a8c
 int semanage_module_get_module_info(semanage_handle_t *sh,
e92a8c
 				    const semanage_module_key_t *modkey,
e92a8c
 				    semanage_module_info_t **modinfo)
e92a8c
diff --git libsemanage-2.5/src/modules.h libsemanage-2.5/src/modules.h
e92a8c
index 8a5c01f..ee3d51d 100644
e92a8c
--- libsemanage-2.5/src/modules.h
e92a8c
+++ libsemanage-2.5/src/modules.h
e92a8c
@@ -44,6 +44,7 @@ struct semanage_module_info {
e92a8c
 	uint16_t priority;	/* key, module priority */
e92a8c
 	char *name;		/* key, module name */
e92a8c
 	char *lang_ext;		/* module source language extension */
e92a8c
+	char *module_version;	/* module version, applies only for pp modules  */
e92a8c
 	int enabled;		/* module enabled/disabled status */
e92a8c
 };
e92a8c
 
528c33
diff --git libsemanage-2.5/src/semanage_store.c libsemanage-2.5/src/semanage_store.c
6f01c7
index fa0876f..4b87d67 100644
528c33
--- libsemanage-2.5/src/semanage_store.c
528c33
+++ libsemanage-2.5/src/semanage_store.c
6f01c7
@@ -95,7 +95,7 @@ static const char *semanage_store_paths[SEMANAGE_NUM_STORES] = {
6f01c7
 static const char *semanage_sandbox_paths[SEMANAGE_STORE_NUM_PATHS] = {
6f01c7
 	"",
6f01c7
 	"/modules",
6f01c7
-	"/base.linked",
6f01c7
+	"/policy.linked",
6f01c7
 	"/homedir_template",
6f01c7
 	"/file_contexts.template",
6f01c7
 	"/commit_num",
6f01c7
@@ -104,8 +104,10 @@ static const char *semanage_sandbox_paths[SEMANAGE_STORE_NUM_PATHS] = {
6f01c7
 	"/nodes.local",
6f01c7
 	"/booleans.local",
6f01c7
 	"/seusers.local",
6f01c7
+	"/seusers.linked",
6f01c7
 	"/users.local",
6f01c7
 	"/users_extra.local",
6f01c7
+	"/users_extra.linked",
6f01c7
 	"/users_extra",
6f01c7
 	"/disable_dontaudit",
6f01c7
 	"/preserve_tunables",
6f01c7
@@ -292,6 +294,13 @@ static int semanage_init_final_suffix(semanage_handle_t *sh)
528c33
 		goto cleanup;
528c33
 	}
528c33
 
528c33
+	if (asprintf(&semanage_final_suffix[SEMANAGE_FC_BIN], "%s.bin",
528c33
+		     semanage_final_suffix[SEMANAGE_FC]) < 0) {
528c33
+		ERR(sh, "Unable to allocate space for file context path.");
528c33
+		status = -1;
528c33
+		goto cleanup;
528c33
+	}
528c33
+
528c33
 	semanage_final_suffix[SEMANAGE_FC_HOMEDIRS] =
528c33
 		strdup(selinux_file_context_homedir_path() + offset);
528c33
 	if (semanage_final_suffix[SEMANAGE_FC_HOMEDIRS] == NULL) {
6f01c7
@@ -300,6 +309,13 @@ static int semanage_init_final_suffix(semanage_handle_t *sh)
528c33
 		goto cleanup;
528c33
 	}
528c33
 
528c33
+	if (asprintf(&semanage_final_suffix[SEMANAGE_FC_HOMEDIRS_BIN], "%s.bin",
528c33
+		     semanage_final_suffix[SEMANAGE_FC_HOMEDIRS]) < 0) {
528c33
+		ERR(sh, "Unable to allocate space for file context home directory path.");
528c33
+		status = -1;
528c33
+		goto cleanup;
528c33
+	}
528c33
+
528c33
 	semanage_final_suffix[SEMANAGE_FC_LOCAL] =
528c33
 		strdup(selinux_file_context_local_path() + offset);
528c33
 	if (semanage_final_suffix[SEMANAGE_FC_LOCAL] == NULL) {
6f01c7
@@ -308,6 +324,13 @@ static int semanage_init_final_suffix(semanage_handle_t *sh)
528c33
 		goto cleanup;
528c33
 	}
528c33
 
528c33
+	if (asprintf(&semanage_final_suffix[SEMANAGE_FC_LOCAL_BIN], "%s.bin",
528c33
+		     semanage_final_suffix[SEMANAGE_FC_LOCAL]) < 0) {
528c33
+		ERR(sh, "Unable to allocate space for local file context path.");
528c33
+		status = -1;
528c33
+		goto cleanup;
528c33
+	}
528c33
+
528c33
 	semanage_final_suffix[SEMANAGE_NC] =
528c33
 		strdup(selinux_netfilter_context_path() + offset);
528c33
 	if (semanage_final_suffix[SEMANAGE_NC] == NULL) {
6f01c7
@@ -512,7 +535,6 @@ char *semanage_conf_path(void)
6f01c7
 int semanage_create_store(semanage_handle_t * sh, int create)
6f01c7
 {
6f01c7
 	struct stat sb;
6f01c7
-	int mode_mask = R_OK | W_OK | X_OK;
6f01c7
 	const char *path = semanage_files[SEMANAGE_ROOT];
6f01c7
 	int fd;
6f01c7
 
6f01c7
@@ -531,9 +553,9 @@ int semanage_create_store(semanage_handle_t * sh, int create)
6f01c7
 			return -1;
6f01c7
 		}
6f01c7
 	} else {
6f01c7
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
6f01c7
+		if (!S_ISDIR(sb.st_mode)) {
6f01c7
 			ERR(sh,
6f01c7
-			    "Could not access module store at %s, or it is not a directory.",
6f01c7
+			    "Module store at %s is not a directory.",
6f01c7
 			    path);
6f01c7
 			return -1;
6f01c7
 		}
6f01c7
@@ -554,9 +576,9 @@ int semanage_create_store(semanage_handle_t * sh, int create)
6f01c7
 			return -1;
6f01c7
 		}
6f01c7
 	} else {
6f01c7
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
6f01c7
+		if (!S_ISDIR(sb.st_mode)) {
6f01c7
 			ERR(sh,
6f01c7
-			    "Could not access module store active subdirectory at %s, or it is not a directory.",
6f01c7
+			    "Module store active subdirectory at %s is not a directory.",
6f01c7
 			    path);
6f01c7
 			return -1;
6f01c7
 		}
6f01c7
@@ -577,9 +599,9 @@ int semanage_create_store(semanage_handle_t * sh, int create)
6f01c7
 			return -1;
6f01c7
 		}
6f01c7
 	} else {
6f01c7
-		if (!S_ISDIR(sb.st_mode) || access(path, mode_mask) == -1) {
6f01c7
+		if (!S_ISDIR(sb.st_mode)) {
6f01c7
 			ERR(sh,
6f01c7
-			    "Could not access module store active modules subdirectory at %s, or it is not a directory.",
6f01c7
+			    "Module store active modules subdirectory at %s is not a directory.",
6f01c7
 			    path);
6f01c7
 			return -1;
6f01c7
 		}
6f01c7
@@ -598,8 +620,8 @@ int semanage_create_store(semanage_handle_t * sh, int create)
6f01c7
 			return -1;
6f01c7
 		}
6f01c7
 	} else {
6f01c7
-		if (!S_ISREG(sb.st_mode) || access(path, R_OK | W_OK) == -1) {
6f01c7
-			ERR(sh, "Could not access lock file at %s.", path);
6f01c7
+		if (!S_ISREG(sb.st_mode)) {
6f01c7
+			ERR(sh, "Lock file at %s missing.", path);
6f01c7
 			return -1;
6f01c7
 		}
6f01c7
 	}
6f01c7
@@ -1137,7 +1159,7 @@ cleanup:
6f01c7
 	free(all_modinfos);
6f01c7
 
6f01c7
 	if (status != 0) {
6f01c7
-		for (i = 0; i < j; j++) {
6f01c7
+		for (i = 0; i < j; i++) {
6f01c7
 			semanage_module_info_destroy(sh, &(*modinfo)[i]);
6f01c7
 		}
6f01c7
 		free(*modinfo);
6f01c7
@@ -1491,6 +1513,45 @@ static int sefcontext_compile(semanage_handle_t * sh, const char *path) {
528c33
 	return 0;
528c33
 }
528c33
 
528c33
+static int semanage_validate_and_compile_fcontexts(semanage_handle_t * sh)
528c33
+{
528c33
+	int status = -1;
528c33
+
528c33
+	if (sh->do_check_contexts) {
528c33
+		int ret;
528c33
+		ret = semanage_exec_prog(
528c33
+			sh,
528c33
+			sh->conf->setfiles,
528c33
+			semanage_final_path(SEMANAGE_FINAL_TMP,
528c33
+					    SEMANAGE_KERNEL),
528c33
+			semanage_final_path(SEMANAGE_FINAL_TMP,
528c33
+					    SEMANAGE_FC));
528c33
+		if (ret != 0) {
528c33
+			ERR(sh, "setfiles returned error code %d.", ret);
528c33
+			goto cleanup;
528c33
+		}
528c33
+	}
528c33
+
528c33
+	if (sefcontext_compile(sh,
528c33
+		    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC)) != 0) {
528c33
+		goto cleanup;
528c33
+	}
528c33
+
528c33
+	if (sefcontext_compile(sh,
528c33
+		    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL)) != 0) {
528c33
+		goto cleanup;
528c33
+	}
528c33
+
528c33
+	if (sefcontext_compile(sh,
528c33
+		    semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS)) != 0) {
528c33
+		goto cleanup;
528c33
+	}
528c33
+
528c33
+	status = 0;
528c33
+cleanup:
528c33
+	return status;
528c33
+}
528c33
+
528c33
 /* Load the contexts of the final tmp into the final selinux directory.
528c33
  * Return 0 on success, -3 on error.
528c33
  */
6f01c7
@@ -1566,35 +1627,6 @@ static int semanage_install_final_tmp(semanage_handle_t * sh)
528c33
 	}
528c33
 
528c33
 skip_reload:
528c33
-	if (sh->do_check_contexts) {
528c33
-		ret = semanage_exec_prog(
528c33
-			sh,
528c33
-			sh->conf->setfiles,
528c33
-			semanage_final_path(SEMANAGE_FINAL_SELINUX,
528c33
-					    SEMANAGE_KERNEL),
528c33
-			semanage_final_path(SEMANAGE_FINAL_SELINUX,
528c33
-					    SEMANAGE_FC));
528c33
-		if (ret != 0) {
528c33
-			ERR(sh, "setfiles returned error code %d.", ret);
528c33
-			goto cleanup;
528c33
-		}
528c33
-	}
528c33
-
528c33
-	if (sefcontext_compile(sh,
528c33
-		    semanage_final_path(SEMANAGE_FINAL_SELINUX, SEMANAGE_FC)) != 0) {
528c33
-		goto cleanup;
528c33
-	}
528c33
-
528c33
-	if (sefcontext_compile(sh,
528c33
-		    semanage_final_path(SEMANAGE_FINAL_SELINUX, SEMANAGE_FC_LOCAL)) != 0) {
528c33
-		goto cleanup;
528c33
-	}
528c33
-
528c33
-	if (sefcontext_compile(sh,
528c33
-		    semanage_final_path(SEMANAGE_FINAL_SELINUX, SEMANAGE_FC_HOMEDIRS)) != 0) {
528c33
-		goto cleanup;
528c33
-	}
528c33
-
528c33
 	status = 0;
528c33
 cleanup:
528c33
 	return status;
6f01c7
@@ -1737,6 +1769,9 @@ int semanage_install_sandbox(semanage_handle_t * sh)
528c33
 		goto cleanup;
528c33
 	}
528c33
 
528c33
+	if (semanage_validate_and_compile_fcontexts(sh) < 0)
528c33
+		goto cleanup;
528c33
+
528c33
 	if ((commit_num = semanage_commit_sandbox(sh)) < 0) {
528c33
 		retval = commit_num;
528c33
 		goto cleanup;
6f01c7
@@ -2003,9 +2038,10 @@ int semanage_load_files(semanage_handle_t * sh, cil_db_t *cildb, char **filename
6f01c7
  */
6f01c7
 
6f01c7
 /**
6f01c7
- * Read the policy from the sandbox (kernel)
6f01c7
+ * Read the policy from the sandbox (linked or kernel)
6f01c7
  */
6f01c7
-int semanage_read_policydb(semanage_handle_t * sh, sepol_policydb_t * in)
6f01c7
+int semanage_read_policydb(semanage_handle_t * sh, sepol_policydb_t * in,
6f01c7
+			   enum semanage_sandbox_defs file)
6f01c7
 {
6f01c7
 
6f01c7
 	int retval = STATUS_ERR;
6f01c7
@@ -2014,7 +2050,7 @@ int semanage_read_policydb(semanage_handle_t * sh, sepol_policydb_t * in)
6f01c7
 	FILE *infile = NULL;
6f01c7
 
6f01c7
 	if ((kernel_filename =
6f01c7
-	     semanage_path(SEMANAGE_ACTIVE, SEMANAGE_STORE_KERNEL)) == NULL) {
6f01c7
+	     semanage_path(SEMANAGE_ACTIVE, file)) == NULL) {
6f01c7
 		goto cleanup;
6f01c7
 	}
6f01c7
 	if ((infile = fopen(kernel_filename, "r")) == NULL) {
6f01c7
@@ -2044,9 +2080,10 @@ int semanage_read_policydb(semanage_handle_t * sh, sepol_policydb_t * in)
6f01c7
 	return retval;
6f01c7
 }
6f01c7
 /**
6f01c7
- * Writes the final policy to the sandbox (kernel)
6f01c7
+ * Writes the policy to the sandbox (linked or kernel)
6f01c7
  */
6f01c7
-int semanage_write_policydb(semanage_handle_t * sh, sepol_policydb_t * out)
6f01c7
+int semanage_write_policydb(semanage_handle_t * sh, sepol_policydb_t * out,
6f01c7
+			    enum semanage_sandbox_defs file)
6f01c7
 {
6f01c7
 
6f01c7
 	int retval = STATUS_ERR;
6f01c7
@@ -2055,7 +2092,7 @@ int semanage_write_policydb(semanage_handle_t * sh, sepol_policydb_t * out)
6f01c7
 	FILE *outfile = NULL;
6f01c7
 
6f01c7
 	if ((kernel_filename =
6f01c7
-	     semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_KERNEL)) == NULL) {
6f01c7
+	     semanage_path(SEMANAGE_TMP, file)) == NULL) {
6f01c7
 		goto cleanup;
6f01c7
 	}
6f01c7
 	if ((outfile = fopen(kernel_filename, "wb")) == NULL) {
528c33
diff --git libsemanage-2.5/src/semanage_store.h libsemanage-2.5/src/semanage_store.h
6f01c7
index acb6e3f..0b96fbe 100644
528c33
--- libsemanage-2.5/src/semanage_store.h
528c33
+++ libsemanage-2.5/src/semanage_store.h
6f01c7
@@ -49,8 +49,10 @@ enum semanage_sandbox_defs {
6f01c7
 	SEMANAGE_NODES_LOCAL,
6f01c7
 	SEMANAGE_BOOLEANS_LOCAL,
6f01c7
 	SEMANAGE_SEUSERS_LOCAL,
6f01c7
+	SEMANAGE_SEUSERS_LINKED,
6f01c7
 	SEMANAGE_USERS_BASE_LOCAL,
6f01c7
 	SEMANAGE_USERS_EXTRA_LOCAL,
6f01c7
+	SEMANAGE_USERS_EXTRA_LINKED,
6f01c7
 	SEMANAGE_USERS_EXTRA,
6f01c7
 	SEMANAGE_DISABLE_DONTAUDIT,
6f01c7
 	SEMANAGE_PRESERVE_TUNABLES,
6f01c7
@@ -71,8 +73,11 @@ enum semanage_final_defs {
528c33
 enum semanage_final_path_defs {
528c33
 	SEMANAGE_FINAL_TOPLEVEL,
528c33
 	SEMANAGE_FC,
528c33
+	SEMANAGE_FC_BIN,
528c33
 	SEMANAGE_FC_HOMEDIRS,
528c33
+	SEMANAGE_FC_HOMEDIRS_BIN,
528c33
 	SEMANAGE_FC_LOCAL,
528c33
+	SEMANAGE_FC_LOCAL_BIN,
528c33
 	SEMANAGE_KERNEL,
528c33
 	SEMANAGE_NC,
528c33
 	SEMANAGE_SEUSERS,
6f01c7
@@ -126,10 +131,12 @@ int semanage_load_files(semanage_handle_t * sh,
6f01c7
 			    cil_db_t *cildb, char **filenames, int num_modules);
6f01c7
 
6f01c7
 int semanage_read_policydb(semanage_handle_t * sh,
6f01c7
-			    sepol_policydb_t * policydb);
6f01c7
+			   sepol_policydb_t * policydb,
6f01c7
+			   enum semanage_sandbox_defs file);
6f01c7
 
6f01c7
 int semanage_write_policydb(semanage_handle_t * sh,
6f01c7
-			    sepol_policydb_t * policydb);
6f01c7
+			    sepol_policydb_t * policydb,
6f01c7
+			    enum semanage_sandbox_defs file);
6f01c7
 
6f01c7
 int semanage_install_sandbox(semanage_handle_t * sh);
6f01c7
 
528c33
diff --git libsemanage-2.5/tests/.gitignore libsemanage-2.5/tests/.gitignore
528c33
new file mode 100644
528c33
index 0000000..f07111d
528c33
--- /dev/null
528c33
+++ libsemanage-2.5/tests/.gitignore
528c33
@@ -0,0 +1 @@
528c33
+libsemanage-tests
528c33
diff --git libsemanage-2.5/utils/semanage_migrate_store libsemanage-2.5/utils/semanage_migrate_store
528c33
index 0ebd285..7965980 100755
528c33
--- libsemanage-2.5/utils/semanage_migrate_store
528c33
+++ libsemanage-2.5/utils/semanage_migrate_store
528c33
@@ -218,7 +218,7 @@ if __name__ == "__main__":
528c33
 	parser.add_option("-n", "--norebuild", dest="norebuild", action="store_true", default=False,
528c33
 			  help="Disable rebuilding policy after migration (default: no)")
528c33
 	parser.add_option("-P", "--path", dest="path",
528c33
-			  help="Set path for the policy store (default: /var/lib/selinux)")
528c33
+			  help="Set path for the policy store (default: /etc/selinux)")
528c33
 	parser.add_option("-r", "--root", dest="root",
528c33
 			  help="Set an alternative root for the migration (default: /)")
528c33
 
528c33
@@ -231,7 +231,7 @@ if __name__ == "__main__":
528c33
 	NOREBUILD = options.norebuild
528c33
 	PATH = options.path
528c33
 	if PATH is None:
528c33
-		PATH = "/var/lib/selinux"
528c33
+		PATH = "/etc/selinux"
528c33
 
528c33
 	ROOT = options.root
528c33
 	if ROOT is None: