Index: include/libbb.h
===================================================================
--- include/libbb.h	(revision 17803)
+++ include/libbb.h	(working copy)
@@ -743,9 +743,15 @@
 	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("cZ:")
+
 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.
  */
@@ -28,6 +29,9 @@
 	int d_flags;
 	int flags;
 	int status = 0;
+#if ENABLE_SELINUX
+ 	char *context_str = NULL;
+#endif
 	enum {
 		OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1),
 		OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)),
@@ -40,8 +44,8 @@
 	// -P and -d are the same (-P is POSIX, -d is GNU)
 	// -r and -R are the same
 	// -a = -pdR
-	opt_complementary = "?:l--s:s--l:Pd:rR:apdR";
-	flags = getopt32(argc, argv, FILEUTILS_CP_OPTSTR "arPHL");
+	opt_complementary = "?:l--s:s--l:Pd:rR:apdR" USE_SELINUX(":c--Z:Z--c");
+	flags = getopt32(argc, argv, FILEUTILS_CP_OPTSTR "arPHL" USE_SELINUX(,&context_str));
 	/* Default behavior of cp is to dereference, so we don't have to do
 	 * anything special when we are given -L.
 	 * The behavior of -H is *almost* like -L, but not quite, so let's
@@ -50,6 +54,19 @@
 	if (flags & OPT_H) ... // deref command-line params only
 	*/
 
+#if ENABLE_SELINUX 
+	if (flags & FILEUTILS_SET_SECURITY_CONTEXT) {
+		if(is_selinux_enabled() == 0) {
+			fprintf( stderr, "Warning:  ignoring --context (-Z). "
+					 "It requires a SELinux enabled kernel.\n" );
+		}else{
+			if ( setfscreatecon(context_str) < 0 ) {
+				bb_error_msg_and_die("cannot set default security context %s\n", context_str);
+			}
+		}
+	}
+#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)
@@ -25,6 +25,41 @@
 };
 #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)
 {
@@ -117,7 +152,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) {
