Hi SELinux people,

Sorry, late reply :((

On Monday 02 July 2007 06:29, Yuichi Nakamura wrote:
> I would like to submit setfiles/restorecon applet.
> setfiles and restorecon are SELinux commands that  
> label files according to configuration file
> (in configuration file, relationship between file and label is
> described).
> 
> These applets are very important for SELinux enabled system.
> Please review and consider merging this patch.

Took a look:

* Trimmed help text a bit
* Removed \n at the end of bb_[p]error
* Removed progname (using applet_name)
* Removed restorecon.c - USE_RESTORECON(APPLET_ODDNAME...) handles that
* int -> smallint for many flag variables
  This needs improvement - many of those flags are already accessible
  as (option_mask32 & BIT_MASK), you don't need separate variables

Please take a look at:
* Do you really have to have fork()?
* Maybe use xstrdup instead of strdupa
* nftw() is not used in bbox. We have recursive_action().
  It will be better if we won't pull in nftw() into busybox
  just for this applet.
* Do not do "if (applet_is_restorecon && option_x) bb_show_usage()"
  Just remove 'x' flag from optarg32 instead.

If some of the above is not feasible, explain that.

Please see attached updated patch.
--
vda
diff -d -urpN busybox.3/include/applets.h busybox.4/include/applets.h
--- busybox.3/include/applets.h	2007-07-15 02:09:10.000000000 +0100
+++ busybox.4/include/applets.h	2007-07-17 22:25:20.000000000 +0100
@@ -269,6 +269,7 @@ USE_HALT(APPLET_ODDNAME(reboot, halt, _B
 USE_RENICE(APPLET(renice, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
 USE_RESET(APPLET(reset, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
 USE_RESIZE(APPLET(resize, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
+USE_RESTORECON(APPLET_ODDNAME(restorecon, setfiles, _BB_DIR_SBIN, _BB_SUID_NEVER, restorecon))
 USE_RM(APPLET_NOFORK(rm, rm, _BB_DIR_BIN, _BB_SUID_NEVER, rm))
 USE_RMDIR(APPLET_NOFORK(rmdir, rmdir, _BB_DIR_BIN, _BB_SUID_NEVER, rmdir))
 USE_RMMOD(APPLET(rmmod, _BB_DIR_SBIN, _BB_SUID_NEVER))
@@ -287,6 +288,7 @@ USE_SEQ(APPLET_NOFORK(seq, seq, _BB_DIR_
 USE_SETARCH(APPLET(setarch, _BB_DIR_BIN, _BB_SUID_NEVER))
 USE_SETCONSOLE(APPLET(setconsole, _BB_DIR_SBIN, _BB_SUID_NEVER))
 USE_SETENFORCE(APPLET(setenforce, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
+USE_SETFILES(APPLET(setfiles, _BB_DIR_SBIN, _BB_SUID_NEVER))
 USE_SETKEYCODES(APPLET(setkeycodes, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
 USE_SETLOGCONS(APPLET(setlogcons, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
 USE_SETSID(APPLET(setsid, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
diff -d -urpN busybox.3/include/usage.h busybox.4/include/usage.h
--- busybox.3/include/usage.h	2007-07-15 13:23:11.000000000 +0100
+++ busybox.4/include/usage.h	2007-07-17 22:33:32.000000000 +0100
@@ -2775,6 +2775,22 @@
 #define resize_full_usage \
        "Resize the screen"
 
+#define restorecon_trivial_usage \
+       "[-iFnrRv] [-e excludedir]... [-o filename] [-f filename | pathname]"
+#define restorecon_full_usage \
+       "Reset security contexts of files in pathname\n" \
+       "\n	-i		Ignore files that do not exist" \
+       "\n	-f filename	File with list of files to process. Use - for stdin" \
+       "\n	-e directory	Directory to exclude" \
+       "\n	-R,-r		Recurse directories" \
+       "\n	-n		Don't change any file labels" \
+       "\n	-o filename	Save list of files with incorrect context" \
+       "\n	-v		Verbose" \
+       "\n	-vv		Show changed labels" \
+       "\n	-F		Force reset of context to match file_context" \
+       "\n			for customizable files, or the user section," \
+       "\n			if it has changed"
+
 #define rm_trivial_usage \
        "[OPTION]... FILE..."
 #define rm_full_usage \
@@ -2939,6 +2955,25 @@ USE_FEATURE_RUN_PARTS_FANCY("\n	-l	Print
        "[ Enforcing | Permissive | 1 | 0 ]"
 #define setenforce_full_usage
 
+#define setfiles_trivial_usage \
+       "[-dnpqsvW] [-e dir]... [-o filename] [-r alt_root_path] [-c policyfile] spec_file pathname"
+
+#define setfiles_full_usage \
+       "Reset file contexts under pathname according to spec_file" \
+       "\n	-c file	Check the validity of the contexts against the specified binary policy" \
+       "\n	-d	Show which specification matched each file" \
+       "\n	-l	Log changes in file labels to syslog" \
+       "\n	-n	Don't change any file labels" \
+       "\n	-q	Suppress no-error output" \
+       "\n	-r dir	Use an altenate root path" \
+       "\n	-e dir	Exclude directory" \
+       "\n	-F	Force reset of context to match file_context for customizable files" \
+       "\n	-o file	Save list of files with incorrect context" \
+       "\n	-s	Take a list of files from standard input (instead of command line)" \
+       "\n	-v	Show changes in file labels, if type or role are changing" \
+	"\n	-vv	Show changes in file labels, if type, role, or user are changing" \
+	"\n	-W	Display warnings about entries that had no matching files"
+
 #define setkeycodes_trivial_usage \
        "SCANCODE KEYCODE ..."
 #define setkeycodes_full_usage \
diff -d -urpN busybox.3/selinux/Config.in busybox.4/selinux/Config.in
--- busybox.3/selinux/Config.in	2007-07-15 02:09:10.000000000 +0100
+++ busybox.4/selinux/Config.in	2007-07-17 22:27:10.000000000 +0100
@@ -49,6 +49,14 @@ config MATCHPATHCON
 	  Enable support to get default security context of the
 	  specified path from the file contexts configuration.
 
+config RESTORECON
+	bool "restorecon"
+	default n
+	depends on SELINUX
+	help
+	  Enable support to relabel files. The feature is almost
+          the same as setfiles, but usage is a little different.
+
 config RUNCON
 	bool "runcon"
 	default n
@@ -78,5 +86,20 @@ config SETENFORCE
 	help
 	  Enable support to modify the mode SELinux is running in.
 
+config SETFILES
+	bool "setfiles"
+	default n
+	depends on SELINUX
+	help
+	  Enable support to modify to relabel files.
+
+config FEATURE_SETFILES_CHECK_OPTION
+	bool "Enable check option"
+	default n
+	depends on SETFILES
+	help
+	  Support "-c" option (check the validity of the contexts against
+	  the specified binary policy) for setfiles. Requires libsepol.
+
 endmenu
 
diff -d -urpN busybox.3/selinux/Kbuild busybox.4/selinux/Kbuild
--- busybox.3/selinux/Kbuild	2007-07-15 02:09:10.000000000 +0100
+++ busybox.4/selinux/Kbuild	2007-07-17 22:25:40.000000000 +0100
@@ -14,3 +14,5 @@ lib-$(CONFIG_MATCHPATHCON)	+= matchpathc
 lib-$(CONFIG_RUNCON)		+= runcon.o
 lib-$(CONFIG_SELINUXENABLED)	+= selinuxenabled.o
 lib-$(CONFIG_SETENFORCE)	+= setenforce.o
+lib-$(CONFIG_SETFILES)		+= setfiles.o
+lib-$(CONFIG_RESTORECON)	+= setfiles.o
diff -d -urpN busybox.3/selinux/setfiles.c busybox.4/selinux/setfiles.c
--- busybox.3/selinux/setfiles.c	1970-01-01 01:00:00.000000000 +0100
+++ busybox.4/selinux/setfiles.c	2007-07-17 22:38:28.000000000 +0100
@@ -0,0 +1,704 @@
+/*
+  setfiles: Based on policycoreutils 2.0.19
+  policycoreutils was released under GPL 2.
+  Port to BusyBox by 2007 Yuichi Nakamura <[EMAIL PROTECTED]>
+*/
+
+#include "libbb.h"
+#define __USE_XOPEN_EXTENDED 1	/* nftw */
+#include <ftw.h>
+#if ENABLE_FEATURE_SETFILES_CHECK_OPTION
+#include <sepol/sepol.h>
+#endif
+
+static FILE *outfile = NULL;
+static smallint force = 0;
+static smallint progress = 0;
+#define STAT_BLOCK_SIZE 1
+static int pipe_fds[2] = { -1, -1 };
+static unsigned long long count = 0;
+
+#define MAX_EXCLUDES 50
+static int excludeCtr = 0;
+struct edir {
+	char *directory;
+	size_t size;
+};
+static struct edir excludeArray[MAX_EXCLUDES];
+
+/*
+ * Command-line options.
+ */
+static char *policyfile = NULL;
+static int verbose = 0; /* getopt32 uses it, has to be int */
+static char *rootpath = NULL;
+static int rootpathlen = 0;
+static int errors;
+static smallint debug = 0;
+static smallint change = 1;
+/*static smallint quiet = 0;*/ //?? never used??
+static smallint ignore_enoent;
+static smallint take_log = 0;
+static smallint warn_no_match = 0;
+static smallint recurse; /* Recursive descent. */
+
+/* Behavior flags determined based on setfiles vs. restorecon */
+static smallint expand_realpath;  /* Expand paths via realpath. */
+static smallint abort_on_error; /* Abort the file tree walk upon an error. */
+static int add_assoc; /* Track inode associations for conflict detection. */
+static int nftw_flags; /* Flags to nftw, e.g. follow links, follow mounts */
+static int matchpathcon_flags; /* Flags to matchpathcon */
+
+#if 0 /* UNUSED */
+static void qprintf(const char *fmt, ...)
+{
+	/* quiet, do nothing */
+}
+#endif
+
+static int nerr = 0;
+static void inc_err(void)
+{
+	nerr++;
+	if (nerr > 9 && !debug) {
+		bb_error_msg_and_die("exiting after 10 errors");
+	}
+}
+
+static int add_exclude(const char *directory)
+{
+	struct stat sb;
+	size_t len = 0;
+	if (directory == NULL || directory[0] != '/') {
+		bb_error_msg("full path required for exclude: %s", directory);
+		return 1;
+	}
+	if (lstat(directory, &sb)) {
+		bb_error_msg("directory \"%s\" not found, ignoring",	directory);
+		return 0;
+	}
+	if ((sb.st_mode & S_IFDIR) == 0) {
+		bb_error_msg("\"%s\" is not a directory: mode %o, ignoring",
+			directory, sb.st_mode);
+		return 0;
+	}
+
+	if (excludeCtr == MAX_EXCLUDES) {
+		bb_error_msg("maximum excludes %d exceeded", MAX_EXCLUDES);
+		return 1;
+	}
+
+	len = strlen(directory);
+	while (len > 1 && directory[len - 1] == '/') {
+		len--;
+	}
+	excludeArray[excludeCtr].directory = xstrndup(directory, len);
+	excludeArray[excludeCtr++].size = len;
+
+	return 0;
+}
+
+static int exclude(const char *file)
+{
+	int i = 0;
+	for (i = 0; i < excludeCtr; i++) {
+		if (strncmp(file, excludeArray[i].directory,
+					excludeArray[i].size) == 0) {
+			if (file[excludeArray[i].size] == 0
+			 || file[excludeArray[i].size] == '/') {
+				return 1;
+			}
+		}
+	}
+	return 0;
+}
+
+static int match(const char *name, struct stat *sb, char **con)
+{
+	int ret;
+	char path[PATH_MAX + 1];
+
+	if (excludeCtr > 0) {
+		if (exclude(name)) {
+			return -1;
+		}
+	}
+	ret = lstat(name, sb);
+	if (ret) {
+		if (ignore_enoent && errno == ENOENT)
+			return 0;
+		bb_error_msg("stat(%s)", name);
+		return -1;
+	}
+
+	if (expand_realpath) {
+		if (S_ISLNK(sb->st_mode)) {
+			char *p = NULL;
+			char *file_sep;
+			// what about using xstrdup (and free when done)?
+			char *tmp_path = strdupa(name);
+			size_t len = 0;
+
+			if (verbose > 1)
+				bb_error_msg("warning! %s refers to a symbolic link, not following last component", name);
+		
+			if (!tmp_path) { //won't be needed
+				bb_perror_msg("strdupa(%s)", name);
+				return -1;
+			}
+			file_sep = strrchr(tmp_path, '/');
+			if (file_sep == tmp_path) {
+				file_sep++;
+				p = strcpy(path, "");
+			} else if (file_sep) {
+				*file_sep = 0;
+				file_sep++;
+				p = realpath(tmp_path, path);
+			} else {
+				file_sep = tmp_path;
+				p = realpath("./", path);
+			}
+			if (p)
+				len = strlen(p);
+			if (!p || len + strlen(file_sep) + 2 > PATH_MAX) {
+				bb_perror_msg("realpath(%s) failed", name);
+				// free(tmp_path);
+				return -1;
+			}
+			p += len;
+			/* ensure trailing slash of directory name */
+			if (len == 0 || *(p - 1) != '/') {
+				*p = '/';
+				p++;
+			}
+			strcpy(p, file_sep);
+			name = path;
+			// free(tmp_path);
+			if (excludeCtr > 0 && exclude(name))
+				return -1;
+		} else {
+			char *p;
+			p = realpath(name, path);
+			if (!p) {
+				bb_perror_msg("realpath(%s)", name);
+				return -1;
+			}
+			name = p;
+			if (excludeCtr > 0 && exclude(name))
+				return -1;
+		}
+	}
+
+	/* name will be what is matched in the policy */
+	if (NULL != rootpath) {
+		if (0 != strncmp(rootpath, name, rootpathlen)) {
+			bb_error_msg("%s is not located in %s",
+				name, rootpath);
+			return -1;
+		}
+		name += rootpathlen;
+	}
+
+	if (rootpath != NULL && name[0] == '\0')
+		/* this is actually the root dir of the alt root */
+		return matchpathcon_index("/", sb->st_mode, con);
+	return matchpathcon_index(name, sb->st_mode, con);
+}
+
+/* Compare two contexts to see if their differences are "significant",
+ * or whether the only difference is in the user. */
+static int only_changed_user(const char *a, const char *b)
+{
+	char *rest_a, *rest_b;	/* Rest of the context after the user */
+	if (force)
+		return 0;
+	if (!a || !b)
+		return 0;
+	rest_a = strchr(a, ':');
+	rest_b = strchr(b, ':');
+	if (!rest_a || !rest_b)
+		return 0;
+	return (strcmp(rest_a, rest_b) == 0);
+}
+
+static int restore(const char *file)
+{
+	char *my_file = strdupa(file);
+	struct stat my_sb;
+	int i, j, ret;
+	char *context, *newcon;
+	int user_only_changed = 0;
+	size_t len = strlen(my_file);
+
+	/* Skip the extra slashes at the beginning and end, if present. */
+	if (file[0] == '/' && file[1] == '/')
+		my_file++;
+	if (len > 1 && my_file[len - 1] == '/')
+		my_file[len - 1] = 0;
+
+	i = match(my_file, &my_sb, &newcon);
+
+	if (i < 0)
+		/* No matching specification. */
+		return 0;
+
+	if (progress) {
+		count++;
+		if (count % 1000 == 0) {
+			if (count % 80000 == 0)
+				fputc('\n', stdout);
+			fputc('*', stdout);
+			fflush(stdout);
+		}
+	}
+
+	/*
+	 * Try to add an association between this inode and
+	 * this specification.  If there is already an association
+	 * for this inode and it conflicts with this specification,
+	 * then use the last matching specification.
+	 */
+	if (add_assoc) {
+		j = matchpathcon_filespec_add(my_sb.st_ino, i, my_file);
+		if (j < 0)
+			goto err;
+
+		if (j != i) {
+			/* There was already an association and it took precedence. */
+			goto out;
+		}
+	}
+
+	if (debug)
+		printf("%s: %s matched by %s\n", applet_name, my_file, newcon);	
+
+	/* Get the current context of the file. */
+	ret = lgetfilecon_raw(my_file, &context);
+	if (ret < 0) {
+		if (errno == ENODATA) {
+			context = NULL;
+		} else {
+			bb_perror_msg("lgetfilecon_raw on %s", my_file);
+			goto err;
+		}
+		user_only_changed = 0;
+	} else
+		user_only_changed = only_changed_user(context, newcon);
+
+	/*
+	 * Do not relabel the file if the matching specification is
+	 * <<none>> or the file is already labeled according to the
+	 * specification.
+	 */
+	if ((strcmp(newcon, "<<none>>") == 0)
+	 || (context && (strcmp(context, newcon) == 0) && !force)) {
+		freecon(context);
+		goto out;
+	}
+
+	if (!force && context && (is_context_customizable(context) > 0)) {
+		if (verbose > 1) {
+			bb_error_msg("%s not reset customized by admin to %s",
+				my_file, context);
+		}
+		freecon(context);
+		goto out;
+	}
+
+	if (verbose) {
+		/* If we're just doing "-v", trim out any relabels where
+		 * the user has changed but the role and type are the
+		 * same.  For "-vv", emit everything. */
+		if (verbose > 1 || !user_only_changed) {
+			bb_info_msg("%s: reset %s context %s->%s",
+			       applet_name, my_file, context ?: "", newcon);
+		}
+	}
+
+	if (take_log && !user_only_changed) {
+		if (context)
+			bb_info_msg("relabeling %s from %s to %s", my_file, context, newcon);
+		else
+			bb_info_msg("labeling %s to %s", my_file, newcon);
+	}
+
+	if (outfile && !user_only_changed)
+		fprintf(outfile, "%s\n", my_file);
+
+	if (context)
+		freecon(context);
+
+	/*
+	 * Do not relabel the file if -n was used.
+	 */
+	if (!change || user_only_changed)
+		goto out;
+
+	/*
+	 * Relabel the file to the specified context.
+	 */
+	ret = lsetfilecon(my_file, newcon);
+	if (ret) {
+		bb_perror_msg("lsetfileconon(%s,%s)", my_file, newcon);
+		goto out;
+	}
+ out:
+	freecon(newcon);
+	return 0;
+ err:
+	freecon(newcon);
+	return -1;
+}
+
+/*
+ * Apply the last matching specification to a file.
+ * This function is called by nftw on each file during
+ * the directory traversal.
+ */
+static int apply_spec(const char *file,
+		      const struct stat *sb_unused __attribute__ ((unused)),
+		      int flag, struct FTW *s_unused __attribute__ ((unused)))
+{
+	char buf[STAT_BLOCK_SIZE];
+	if (pipe_fds[0] != -1
+	    && read(pipe_fds[0], buf, STAT_BLOCK_SIZE) != STAT_BLOCK_SIZE) {
+		bb_error_msg("read error on pipe");
+		pipe_fds[0] = -1;
+	}
+
+	if (flag == FTW_DNR) {
+		bb_error_msg("cannot read directory %s", file);
+		return 0;
+	}
+
+	errors |= restore(file);
+	if (abort_on_error && errors)
+		return -1;
+	return 0;
+}
+
+
+static int canoncon(const char *path, unsigned lineno, char **contextp)
+{
+	char *context = *contextp, *tmpcon;
+	int valid = 1;
+	char err_msg[]="%s:  line %u has invalid context %s";
+
+#if ENABLE_FEATURE_SETFILES_CHECK_OPTION
+	if (policyfile) {
+		valid = (sepol_check_context(context) >= 0);
+		if (!valid) {		
+			/* Exit immediately if we're in checking mode. */
+			bb_error_msg_and_die(err_msg, path, lineno, context);
+		} else {
+			return !valid;
+		}
+	}
+#endif
+
+	if (security_canonicalize_context_raw(context, &tmpcon) < 0) {
+		if (errno != ENOENT) {
+			valid = 0;
+			inc_err();
+		}
+	} else {
+		free(context);
+		*contextp = tmpcon;
+	}
+
+	if (!valid) {
+		bb_error_msg(err_msg, path, lineno, context);
+	}
+
+	return !valid;
+}
+
+static int pre_stat(const char *file_unused __attribute__ ((unused)),
+		    const struct stat *sb_unused __attribute__ ((unused)),
+		    int flag_unused __attribute__ ((unused)),
+		    struct FTW *s_unused __attribute__ ((unused)))
+{
+	char buf[STAT_BLOCK_SIZE];
+	if (write(pipe_fds[1], buf, STAT_BLOCK_SIZE) != STAT_BLOCK_SIZE) {
+		bb_error_msg_and_die("error writing to stat pipe, child exiting");
+	}
+	return 0;
+}
+
+static int process_one(char *name)
+{
+	struct stat sb;
+	int rc;
+
+	rc = lstat(name, &sb);
+	if (rc < 0) {
+		if (ignore_enoent && errno == ENOENT)
+			return 0;
+		bb_perror_msg("stat(%s)", name);
+		goto err;
+	}
+
+	if (S_ISDIR(sb.st_mode) && recurse) {
+		if (pipe(pipe_fds) < 0) {
+			bb_perror_msg("pipe on %s", name);
+			goto err;
+		}
+		// Huh? what's going on here?
+		rc = fork();
+		if (rc < 0) {
+			bb_error_msg("fork on %s", name);
+			goto err;
+		}
+		if (rc == 0) {
+			/* Child: pre-stat the files. */
+			close(pipe_fds[0]);
+			nftw(name, pre_stat, 1024, nftw_flags);
+			exit(0);
+		}
+		/* Parent: Check and label the files. */
+		rc = 0;
+		close(pipe_fds[1]);
+		if (nftw(name, apply_spec, 1024, nftw_flags)) {
+			bb_error_msg("error while labeling %s", name);
+			goto err;
+		}
+	} else {
+		rc = restore(name);
+		if (rc)
+			goto err;
+	}
+
+ out:
+	if (add_assoc) {
+		/* if (quiet) // always 0
+			set_matchpathcon_printf(&qprintf); */
+		matchpathcon_filespec_eval();
+		set_matchpathcon_printf(NULL);
+		matchpathcon_filespec_destroy();
+	}
+
+	return rc;
+
+ err:
+	rc = -1;
+	goto out;
+}
+
+#define OPT_c		(1<<0)
+#define OPT_d		(1<<1)
+#define OPT_e		(1<<2)
+#define OPT_f		(1<<3)
+#define OPT_i		(1<<4)
+#define OPT_l		(1<<5)
+#define OPT_n		(1<<6)
+#define OPT_p		(1<<7)
+#define OPT_q		(1<<8)
+#define OPT_r		(1<<9)
+#define OPT_s		(1<<10)
+#define OPT_v		(1<<11)
+#define OPT_o		(1<<12)
+#define OPT_F		(1<<13)
+#define OPT_R		(1<<14)
+#define OPT_W		(1<<15)
+
+int setfiles_main(int argc, char **argv);
+int setfiles_main(int argc, char **argv)
+{
+	struct stat sb;
+	int rc, i = 0;
+	char *input_filename = NULL;
+	int use_input_file = 0;
+	char *buf = NULL;
+	size_t buf_len;
+	int flags;
+	llist_t *exclude_dir = NULL;
+	char *out_filename = NULL;
+
+	memset(excludeArray, 0, sizeof(excludeArray));
+
+	if (applet_name[0] == 's') /* "setfiles" */
+		/*
+		 * setfiles:
+		 * Recursive descent,
+		 * Does not expand paths via realpath,
+		 * Aborts on errors during the file tree walk,
+		 * Try to track inode associations for conflict detection,
+		 * Does not follow mounts,
+		 * Validates all file contexts at init time.
+		 */
+		recurse = 1;
+		/* expand_realpath = 0; - already is */
+		abort_on_error = 1;
+		add_assoc = 1;
+		nftw_flags = FTW_PHYS | FTW_MOUNT;
+		matchpathcon_flags = MATCHPATHCON_VALIDATE | MATCHPATHCON_NOTRANS;
+	} else {
+		/*
+		 * restorecon:
+		 * No recursive descent unless -r/-R,
+		 * Expands paths via realpath,
+		 * Do not abort on errors during the file tree walk,
+		 * Do not try to track inode associations for conflict detection,
+		 * Follows mounts,
+		 * Does lazy validation of contexts upon use.
+		 */
+		/* recurse = 0; - already is */
+		expand_realpath = 1;
+		/* abort_on_error = 0; - already is */
+		/* add_assoc = 0; - already is */
+		nftw_flags = FTW_PHYS;
+		matchpathcon_flags = MATCHPATHCON_NOTRANS;
+		/* restorecon only */
+		selinux_or_die();	
+	}
+
+	set_matchpathcon_flags(matchpathcon_flags);
+
+	opt_complementary = "e::vvv--p:p--v";
+	if (applet_name[0] == 'r') { /* restorecon */
+		flags = getopt32(argc, argv, "c:de:f:ilnpqrsvo:FRW", &policyfile, &exclude_dir, &input_filename, &out_filename, &verbose);
+	} else { /* setfiles */
+		flags = getopt32(argc, argv, "c:de:f:ilnpqr:svo:FRW", &policyfile, &exclude_dir, &input_filename, &rootpath, &out_filename, &verbose);
+	}
+
+	if (flags & OPT_c) {
+#if ENABLE_FEATURE_SETFILES_CHECK_OPTION
+		FILE *policystream;
+		if (applet_name[0] == 'r') /* restorecon */
+			bb_show_usage(); //?? why -c is even in getopt32 then?
+		policystream = xfopen(policyfile, "r");
+		
+		if (sepol_set_policydb_from_file(policystream) < 0) {
+			bb_error_msg_and_die("sepol_set_policydb_from_file on %s", policyfile);
+		}
+		fclose(policystream);
+		
+		/* Only process the specified file_contexts file, not
+		   any .homedirs or .local files, and do not perform
+		   context translations. */
+		set_matchpathcon_flags(MATCHPATHCON_BASEONLY |
+				       MATCHPATHCON_NOTRANS |
+				       MATCHPATHCON_VALIDATE);	
+		
+#else
+		bb_error_msg_and_die("-c is not supported");
+#endif		
+	}
+	
+	if (flags & OPT_d) {
+		debug = 1;
+	}
+
+	if (flags & OPT_e) {
+		if (exclude_dir == NULL){
+			bb_show_usage();
+		}
+		
+		while (exclude_dir) {
+			if (add_exclude(llist_pop(&exclude_dir)))
+				exit(1);
+		}
+	}
+
+	if (flags & OPT_i) {
+		ignore_enoent = 1;
+	}
+
+	if (flags & OPT_l) {
+		take_log = 1;
+	}
+
+	if (flags & OPT_F) {
+		force = 1;
+	}
+
+	if (flags & OPT_n) {
+		change = 0;
+	}
+
+	if (flags & OPT_o) {
+		if (LONE_CHAR(out_filename, '-')) {
+			outfile = stdout;
+		} else {
+			outfile = xfopen(out_filename, "w");
+		}
+	}
+
+	if (applet_name[0] == 'r') /* restorecon */
+		if (flags & (OPT_r | OPT_R))
+			recurse = 1;
+	} else {		
+		if (flags & OPT_R)
+			bb_show_usage(); // why -R is in getopt32 then?
+		if (flags & OPT_r)
+			rootpathlen = strlen(rootpath);
+	}
+
+	if (flags & OPT_s) {
+		use_input_file = 1;
+		input_filename = xstrdup("-");
+		add_assoc = 0;
+	}
+
+	if (flags & OPT_p) {
+		progress = 1;
+	}
+
+	if (flags & OPT_W) {
+		warn_no_match = 1;
+	}
+
+	if (applet_name[0] != 'r') /* !restorecon */
+		/* Use our own invalid context checking function so that
+		   we can support either checking against the active policy or
+		   checking against a binary policy file. */
+		set_matchpathcon_canoncon(&canoncon);
+		if (argc == 1)
+			bb_show_usage();
+		if (stat(argv[optind], &sb) < 0) {
+			bb_perror_msg_and_die("%s", argv[optind]);
+		}
+		if (!S_ISREG(sb.st_mode)) {
+			bb_error_msg_and_die("spec file %s is not a regular file", argv[optind]);
+		}
+
+		/* Load the file contexts configuration and check it. */
+		rc = matchpathcon_init(argv[optind]);
+		if (rc < 0) {	
+			bb_perror_msg_and_die("%s", argv[optind]);
+		}
+
+		optind++;
+
+		if (nerr)
+			exit(1);
+	}
+
+	if (use_input_file) {
+		FILE *f = stdin;
+		ssize_t len;
+		if (NOT_LONE_CHAR(input_filename, '-'))
+			f = xfopen(input_filename, "r");
+
+		while ((len = getline(&buf, &buf_len, f)) > 0) {
+			buf[len - 1] = 0;
+			errors |= process_one(buf);
+		}
+		if (ENABLE_FEATURE_CLEAN_UP && NOT_LONE_CHAR(input_filename, '-'))
+			fclose(f);
+	} else {
+		if (optind >= argc)
+			bb_show_usage();
+		for (i = optind; i < argc; i++) {
+			errors |= process_one(argv[i]);
+		}
+	}
+
+	if (warn_no_match)
+		matchpathcon_checkmatches(argv[0]);
+
+	if (ENABLE_FEATURE_CLEAN_UP && outfile)
+		fclose(outfile);
+
+	return errors;
+}
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to