Index: include/libbb.h
===================================================================
--- include/libbb.h	(revision 17803)
+++ include/libbb.h	(working copy)
@@ -735,7 +735,7 @@
 uint32_t *crc32_filltable(int endian);
 
 
-enum {	/* DO NOT CHANGE THESE VALUES!  cp.c depends on them. */
+enum {	/* DO NOT CHANGE THESE VALUES!  cp.c, mv.c, install.c depends on them. */
 	FILEUTILS_PRESERVE_STATUS = 1,
 	FILEUTILS_DEREFERENCE = 2,
 	FILEUTILS_RECUR = 4,
@@ -743,9 +743,14 @@
 	FILEUTILS_INTERACTIVE = 0x10,
 	FILEUTILS_MAKE_HARDLINK = 0x20,
 	FILEUTILS_MAKE_SOFTLINK = 0x40,
+#if ENABLE_SELINUX
+	FILEUTILS_PRESERVE_SECURITY_CONTEXT = 0x80,
+	FILEUTILS_SET_SECURITY_CONTEXT = 0x100
+#endif
 };
-#define FILEUTILS_CP_OPTSTR "pdRfils"
 
+#define FILEUTILS_CP_OPTSTR "pdRfils" USE_SELINUX("c\b")
+
 extern const char *applet_name;
 extern const char BB_BANNER[];
 
Index: coreutils/cp.c
===================================================================
--- coreutils/cp.c	(revision 17803)
+++ coreutils/cp.c	(working copy)
@@ -3,6 +3,7 @@
  * Mini cp implementation for busybox
  *
  * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
+ * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
  *
  * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
  */
@@ -50,6 +51,12 @@
 	if (flags & OPT_H) ... // deref command-line params only
 	*/
 
+#if ENABLE_SELINUX 
+	if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
+		selinux_or_die();
+	}
+#endif
+
 	flags ^= FILEUTILS_DEREFERENCE;		/* The sense of this flag was reversed. */
 
 	if (optind + 2 > argc) {
Index: coreutils/mv.c
===================================================================
--- coreutils/mv.c	(revision 17803)
+++ coreutils/mv.c	(working copy)
@@ -3,6 +3,7 @@
  * Mini mv implementation for busybox
  *
  * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
+ * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
  *
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
@@ -44,6 +45,7 @@
 	unsigned long flags;
 	int dest_exists;
 	int status = 0;
+	int copy_flag = 0;
 
 #if ENABLE_FEATURE_MV_LONG_OPTIONS
 	applet_long_options = mv_long_options;
@@ -113,8 +115,11 @@
 						goto RET_1;
 					}
 				}
-				if ((copy_file(*argv, dest,
-					FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) >= 0) &&
+				copy_flag = FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS;
+#if ENABLE_SELINUX
+				copy_flag |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
+#endif				
+				if ((copy_file(*argv, dest,	copy_flag) >= 0) &&
 					(remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) {
 					goto RET_0;
 				}
Index: coreutils/install.c
===================================================================
--- coreutils/install.c	(revision 17803)
+++ coreutils/install.c	(working copy)
@@ -21,10 +21,51 @@
 	{ "group",               0, NULL, 'g' },
 	{ "mode",                0, NULL, 'm' },
 	{ "owner",               0, NULL, 'o' },
+#if ENABLE_SELINUX
+	{ "context",             1, NULL, 'Z' },
+	{ "preserve_context",    0, NULL, '\b'},
+	{ "preserve-context",    0, NULL, '\b'},
+
+#endif
 	{ 0, 0, 0, 0 }
 };
 #endif
 
+
+#if ENABLE_SELINUX
+static int use_default_selinux_context = 1;
+
+static void setdefaultfilecon(const char *path) {
+	struct stat s;
+	security_context_t scontext = NULL;
+
+	if (!is_selinux_enabled()){
+		return;
+	}	
+	if (lstat(path, &s) != 0){
+		return;
+	}
+
+	if (matchpathcon(path, s.st_mode, &scontext) < 0){
+		return;
+	}
+	if (strcmp(scontext, "<<none>>") == 0){
+		freecon(scontext);
+		return;
+	}
+
+	if (lsetfilecon(path, scontext) < 0) {
+		if (errno != ENOTSUP) {
+			bb_perror_msg("warning: failed to change context of %s to %s", path, scontext);
+		}
+	}
+
+	freecon(scontext);
+	return;
+}
+
+#endif
+
 int install_main(int argc, char **argv);
 int install_main(int argc, char **argv)
 {
@@ -37,7 +78,9 @@
 	const char *mode_str;
 	int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
 	int ret = EXIT_SUCCESS, flags, i, isdir;
-
+#if ENABLE_SELINUX
+	security_context_t scontext;
+#endif
 	enum {
 		OPT_CMD           =  0x1,
 		OPT_DIRECTORY     =  0x2,
@@ -46,15 +89,42 @@
 		OPT_GROUP         = 0x10,
 		OPT_MODE          = 0x20,
 		OPT_OWNER         = 0x40,
+#if ENABLE_SELINUX
+		OPT_SET_SECURITY_CONTEXT = 0x80,
+		OPT_PRESERVE_SECURITY_CONTEXT = 0x100,
+#endif
 	};
 
 #if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
 	applet_long_options = install_long_options;
 #endif
-	opt_complementary = "?:s--d:d--s";
+	opt_complementary = "?:s--d:d--s" USE_SELINUX(":Z--\b:\b--Z");
 	/* -c exists for backwards compatibility, its needed */
-	flags = getopt32(argc, argv, "cdpsg:m:o:", &gid_str, &mode_str, &uid_str);
+	flags = getopt32(argc, argv, "cdpsg:m:o:" USE_SELINUX("Z:\b"), &gid_str, &mode_str, &uid_str USE_SELINUX(, &scontext));
 
+#if ENABLE_SELINUX
+	if (flags & OPT_PRESERVE_SECURITY_CONTEXT) {
+		use_default_selinux_context = 0;
+		copy_flags |= FILEUTILS_PRESERVE_SECURITY_CONTEXT;
+		if(!is_selinux_enabled()) {
+			bb_error_msg("warning: ignoring --preserve-context. "
+					 "The kernel is not SELinux-enabled.\n" );
+		}
+	}
+	if (flags & OPT_SET_SECURITY_CONTEXT) {
+		if(!is_selinux_enabled()) {
+			bb_error_msg("warning: ignoring --context (-Z). "
+					 "The kernel is not SELinux-enabled.\n" );
+		} else {
+			if (setfscreatecon(scontext) < 0) {
+				bb_error_msg_and_die("cannot set default security context %s\n", scontext);
+			}
+		}
+		use_default_selinux_context = 0;
+		copy_flags |= FILEUTILS_SET_SECURITY_CONTEXT;
+	}
+#endif
+
 	/* preserve access and modification time, this is GNU behaviour, BSD only preserves modification time */
 	if (flags & OPT_PRESERVE_TIME) {
 		copy_flags |= FILEUTILS_PRESERVE_STATUS;
@@ -117,7 +187,10 @@
 			bb_perror_msg("cannot change permissions of %s", dest);
 			ret = EXIT_FAILURE;
 		}
-
+#if ENABLE_SELINUX
+		if (use_default_selinux_context)
+			setdefaultfilecon(dest);
+#endif
 		/* Set the user and group id */
 		if ((flags & (OPT_OWNER|OPT_GROUP))
 		 && lchown(dest, uid, gid) == -1
Index: libbb/copy_file.c
===================================================================
--- libbb/copy_file.c	(revision 17803)
+++ libbb/copy_file.c	(working copy)
@@ -3,6 +3,7 @@
  * Mini copy_file implementation for busybox
  *
  * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
+ * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
  *
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  *
@@ -62,6 +63,26 @@
 		dest_exists = 1;
 	}
 
+#if ENABLE_SELINUX
+	if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0){
+		security_context_t con;
+		if (lgetfilecon (source, &con) >= 0){
+			if (setfscreatecon(con) < 0) {
+				bb_perror_msg ("cannot set setfscreatecon %s", con);
+				freecon(con);
+				return -1;
+			}	
+		}else{
+			if( errno == ENOTSUP || errno == ENODATA ) {
+				setfscreatecon(NULL);
+			} else {
+				bb_perror_msg ("cannot  lgetfilecon %s", source);
+				return -1;
+			}
+		}
+	}
+#endif
+
 	if (S_ISDIR(source_stat.st_mode)) {
 		DIR *dp;
 		struct dirent *d;
@@ -202,8 +223,27 @@
 				close(src_fd);
 				return -1;
 			}
+		}				
+
+#if ENABLE_SELINUX
+		if ( ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT)
+			  ||(flags & FILEUTILS_SET_SECURITY_CONTEXT))
+			 && is_selinux_enabled() > 0 ){
+			security_context_t con;  
+			if(getfscreatecon(&con) == -1){
+				bb_perror_msg ("cannot getfscreatecon");
+				return -1;
+			}				
+			if (con){
+				if(setfilecon(dest, con) == -1){
+					bb_perror_msg ("cannot setfilecon:%s,%s",dest,con);
+					freecon(con);
+					return -1;
+				}
+				freecon(con);
+			}
 		}
-
+#endif
 		if (bb_copyfd_eof(src_fd, dst_fd) == -1)
 			status = -1;
 		if (close(dst_fd) < 0) {
