2 # Source this file at the beginning of a test that works
3 # only when run as root or as non-root.
5 # Copyright (C) 2001, 2003, 2004 Free Software Foundation, Inc.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 case "$PRIV_CHECK_ARG" in
21 require-root) who='as root';;
22 require-non-root) who='by an unprivileged user';;
23 *) echo "Usage: PRIV_CHECK_ARG={require-root|require-non-root} . priv-check"\
27 # Make sure id -u succeeds.
30 echo "$0: cannot run \`id -u'" 1>&2
34 # Make sure it gives valid output.
37 echo "$0: invalid output (\`$my_uid') from \`id -u'" 1>&2
45 # When running as root, always ensure that we have a valid non-root username.
46 # As non-root, don't do anything, since we won't be running setuidgid.
47 : ${NON_ROOT_USERNAME=nobody}
49 # Ensure that the supplied username is valid and with UID != 0.
50 coreutils_non_root_uid=`id -u $NON_ROOT_USERNAME`
53 echo "$0: This command failed: \`id -u $NON_ROOT_USERNAME'" 1>&2
54 echo "$0: Skipping this test. To enable it, set the envvar" 1>&2
55 echo "$0: NON_ROOT_USERNAME to a non-root user name." 1>&2
58 test "$coreutils_non_root_uid" = 0 && \
60 echo "$0: The specified NON_ROOT_USERNAME ($NON_ROOT_USERNAME)" 1>&2
61 echo "$0: is invalid because its UID is 0." 1>&2
67 case $PRIV_CHECK_ARG:$my_uid in
69 require-root:*) give_msg=yes ;;
71 # `.' must be writable by $NON_ROOT_USERNAME
72 setuidgid $NON_ROOT_USERNAME test -w . ||
74 echo "$0: `pwd`: not writable by user \`$NON_ROOT_USERNAME'" 1>&2
75 echo "$0: skipping this test" 1>&2
78 exec setuidgid $NON_ROOT_USERNAME env PATH="$PATH" $0
80 require-non-root:*) ;;
83 test $give_msg = yes && {
85 ***************************
87 $0: This test is being skipped, since it works only
89 ***************************