1#!/bin/sh
2# A cross-partition move of a file in a sticky tmpdir and owned by
3# someone else would evoke an invalid diagnostic:
4# mv: cannot remove 'x': Operation not permitted
5# Affects coreutils-6.0-6.9.
6
7# Copyright (C) 2007-2023 Free Software Foundation, Inc.
8
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
22. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
23print_ver_ mv
24require_root_
25
26cleanup_() { rm -rf "$other_partition_tmpdir"; }
27. "$abs_srcdir/tests/other-fs-tmpdir"
28
29# Set up to run a test where non-root user tries to move a root-owned
30# file from a sticky tmpdir to a directory owned by that user on
31# a different partition.
32
33mkdir t || framework_failure_
34chmod a=rwx,o+t t || framework_failure_
35echo > t/root-owned || framework_failure_
36chmod a+r t/root-owned || framework_failure_
37chown "$NON_ROOT_USERNAME" "$other_partition_tmpdir" || framework_failure_
38
39# We have to allow $NON_ROOT_USERNAME access to ".".
40chmod go+x . || framework_failure_
41
42
43# Ensure that $NON_ROOT_USERNAME can access the required version of mv.
44version=$(
45  chroot --skip-chdir --user=$NON_ROOT_USERNAME / env PATH="$PATH" \
46    mv --version |
47  sed -n '1s/.* //p'
48)
49case $version in
50  $PACKAGE_VERSION) ;;
51  *) skip_ "cannot access just-built mv as user $NON_ROOT_USERNAME";;
52esac
53
54chroot --skip-chdir --user=$NON_ROOT_USERNAME / env PATH="$PATH" \
55  mv t/root-owned "$other_partition_tmpdir" 2> out-t && fail=1
56
57# On some systems, we get 'Not owner'.  Convert it.
58# On other systems (HPUX), we get 'Permission denied'.  Convert it, too.
59onp='Operation not permitted'
60sed "s/Not owner/$onp/;s/Permission denied/$onp/" out-t > out
61
62cat <<\EOF > exp
63mv: cannot remove 't/root-owned': Operation not permitted
64EOF
65
66compare exp out || fail=1
67
68Exit $fail
69