1#!/bin/sh
2# Verify that --preserve-root works.
3
4# Copyright (C) 2006-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ chown
21skip_if_root_
22
23mkdir d && ln -s / d/slink-to-root
24
25
26# Even if --preserve-root were to malfunction, allowing the chown or
27# chgrp to traverse through "/", since we're running as non-root,
28# they would be very unlikely to cause any changes.
29chown -R --preserve-root 0 / >  out 2>&1 && fail=1
30chgrp -R --preserve-root 0 / >> out 2>&1 && fail=1
31
32# Here, if --preserve-root were to malfunction, chmod could make changes,
33# but only to files owned and unreadable by the user running this test,
34# and then, only to make them readable by owner.
35chmod -R --preserve-root u+r / >> out 2>&1 && fail=1
36
37# With -RHh, --preserve-root should trigger nothing,
38# since the symlink in question is not a command line argument.
39# Contrary to the above commands, these two should succeed.
40echo '==== test -RHh' >> out || framework_failure_
41chown -RHh --preserve-root $(id -u) d >> out 2>&1 || fail=1
42chgrp -RHh --preserve-root $(id -g) d >> out 2>&1 || fail=1
43
44# These must fail.
45echo '==== test -RLh' >> out || framework_failure_
46chown -RLh --preserve-root $(id -u) d >> out 2>&1 && fail=1
47chgrp -RLh --preserve-root $(id -g) d >> out 2>&1 && fail=1
48
49cat <<\EOF > exp || framework_failure_
50chown: it is dangerous to operate recursively on '/'
51chown: use --no-preserve-root to override this failsafe
52chgrp: it is dangerous to operate recursively on '/'
53chgrp: use --no-preserve-root to override this failsafe
54chmod: it is dangerous to operate recursively on '/'
55chmod: use --no-preserve-root to override this failsafe
56==== test -RHh
57==== test -RLh
58chown: it is dangerous to operate recursively on 'd/slink-to-root' (same as '/')
59chown: use --no-preserve-root to override this failsafe
60chgrp: it is dangerous to operate recursively on 'd/slink-to-root' (same as '/')
61chgrp: use --no-preserve-root to override this failsafe
62EOF
63
64compare exp out || fail=1
65
66Exit $fail
67