From 3ea540c7570a76bf72ae697c4040e77dad5bea6d Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Fri, 5 Jan 2007 18:23:54 +0100 Subject: ls: Add support for SELinux and a slightly modified -Z option. I started with the patches from Red Hat. The entries below tell how the code evolved. * src/ls.c (print_long_format, print_file_name_and_frills): When there is no security context (due to getfilecon/lgetfilecon failing with e.g. ENOTSUP), print it as "?", not "". * src/ls.c (print_file_name_and_frills): Make -Z work without -l. (length_of_file_name_and_frills): Likewise. * src/ls.c: Remove the --lcontext and --scontext options. Change the way -Z, --context work so that it no longer implies -l. Thus, -Z -l will work like -lcontext and -Z without -l will work like --scontext. Adjust tests to reflect new 'ls -l' syntax -- affects only systems with SELinux when operating on a file with no ACL. These tests assumed that everything before the first space on each line is the 10-byte mode string. But there may also be a "+" in the 11th column, just before the space. However, note that this is not new. The same thing would have happened even without the change below, when listing a file with an ACL. * tests/chmod/equals, tests/cp/cp-parents, tests/cp/fail-perm: * tests/cp/link-preserve, tests/install/basic-1, tests/misc/mknod: * tests/mkdir/parents, tests/mkdir/special-1, tests/mv/partition-perm: Don't make compilation depend on USE_ACL. An SELinux security context counts as an "alternate access control method", so ls must output a "+" for each file with a security context. * src/ls.c [struct fileinfo] (have_acl): Declare unconditionally. (FILE_HAS_ACL): Remove macro definition. Use f->have_acl directly. (gobble_file): Record whether a file has a security context, and update the condition used to determine whether to print the "+". (gobble_file): Call getfilecon/lgetfilecon also when format == long_format, so that we get the "+". * src/ls.c (gobble_file): Add a comment explaining why (with a security context option) ls doesn't exit nonzero due to e.g., getfilecon failing with errno == ENOTSUP. * src/ls.c (gobble_file): Ignore failure of getfilecon if it's due to ENOTSUP. * src/ls.c (gobble_file): Factor out three small blocks using getfilecon and lgetfilecon. Don't ignore return value from getfilecon and lgetfilecon. * src/ls.c (print_long_format): Don't use ?: (empty 2nd arg with C ternary operator). (print_scontext_format): Likewise. (print_scontext): Declare to be "bool", not int. Adjust uses. * src/Makefile.am (dir_LDADD, ls_LDADD, vdir_LDADD): Add $(LIB_SELINUX). * tests/misc/chcon: New file. * tests/misc/chcon-fail: New file. * tests/Makefile.am (check-root): Run new, root-only misc/chcon test. * tests/misc/Makefile.am (TESTS): Add chcon and chcon-fail. * tests/misc/Makefile.am (TESTS): Add selinux. * tests/misc/selinux: New file. * tests/help-version: Skip chcon. --- tests/cp/cp-parents | 6 +++--- tests/cp/fail-perm | 5 ++--- tests/cp/link-preserve | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) (limited to 'tests/cp') diff --git a/tests/cp/cp-parents b/tests/cp/cp-parents index 6c123d225..384f6c926 100755 --- a/tests/cp/cp-parents +++ b/tests/cp/cp-parents @@ -75,8 +75,8 @@ test -d d/f && fail=1 # Check that re_protect works. chmod go=w d/a cp -a --parents d/a/b/c e || fail=1 -p=`ls -ld e/d|sed 's/ .*//'`; case $p in drwxr-xr-x);; *) fail=1;; esac -p=`ls -ld e/d/a|sed 's/ .*//'`; case $p in drwx-w--w-);; *) fail=1;; esac -p=`ls -ld e/d/a/b/c|sed 's/ .*//'`; case $p in drwxr-xr-x);; *) fail=1;; esac +p=`ls -ld e/d|cut -b-10`; case $p in drwxr-xr-x);; *) fail=1;; esac +p=`ls -ld e/d/a|cut -b-10`; case $p in drwx-w--w-);; *) fail=1;; esac +p=`ls -ld e/d/a/b/c|cut -b-10`; case $p in drwxr-xr-x);; *) fail=1;; esac (exit $fail); exit $fail diff --git a/tests/cp/fail-perm b/tests/cp/fail-perm index 5fff090cd..a7486b6af 100755 --- a/tests/cp/fail-perm +++ b/tests/cp/fail-perm @@ -52,9 +52,8 @@ cp -pR D DD > /dev/null 2>&1 && fail=1 # Permissions on DD must be `dr-x------' -set X `ls -ld DD` -shift -test "$1" = dr-x------ || fail=1 +mode=`ls -ld DD|cut -b-10` +test "$mode" = dr-x------ || fail=1 chmod 0 D ln -s D/D symlink diff --git a/tests/cp/link-preserve b/tests/cp/link-preserve index 8fec65dea..e0138e4b6 100755 --- a/tests/cp/link-preserve +++ b/tests/cp/link-preserve @@ -98,7 +98,7 @@ rm -rf a b c d touch a; chmod 731 a umask 077 cp -a --no-preserve=mode a b -set _ `ls -l b`; shift; mode=$1 +mode=`ls -l b|cut -b-10` test "$mode" = "-rwx------" || fail=1 umask 022 # -------------------------------------- -- cgit v1.2.3-54-g00ecf