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 2 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, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 case "$PRIV_CHECK_ARG" in
23 require-root) who='as root';;
24 require-non-root) who='by an unprivileged user';;
25 *) echo "Usage: PRIV_CHECK_ARG={require-root|require-non-root} . priv-check"\
29 # Make sure id -u succeeds.
32 echo "$0: cannot run \`id -u'" 1>&2
36 # Make sure it gives valid output.
39 echo "$0: invalid output (\`$my_uid') from \`id -u'" 1>&2
47 # When running as root, always ensure that we have a valid non-root username.
48 # As non-root, don't do anything, since we won't be running setuidgid.
49 : ${NON_ROOT_USERNAME=nobody}
51 # Ensure that the supplied username is valid and with UID != 0.
52 coreutils_non_root_uid=`id -u $NON_ROOT_USERNAME`
55 echo "$0: This command failed: \`id -u $NON_ROOT_USERNAME'" 1>&2
56 echo "$0: Skipping this test. To enable it, set the envvar" 1>&2
57 echo "$0: NON_ROOT_USERNAME to a non-root user name." 1>&2
60 test "$coreutils_non_root_uid" = 0 && \
62 echo "$0: The specified NON_ROOT_USERNAME ($NON_ROOT_USERNAME)" 1>&2
63 echo "$0: is invalid because its UID is 0." 1>&2
69 case $PRIV_CHECK_ARG:$my_uid in
71 require-root:*) give_msg=yes ;;
73 # `.' must be writable by $NON_ROOT_USERNAME
74 setuidgid $NON_ROOT_USERNAME test -w . ||
76 echo "$0: `pwd`: not writable by user \`$NON_ROOT_USERNAME'" 1>&2
77 echo "$0: skipping this test" 1>&2
80 exec setuidgid $NON_ROOT_USERNAME env PATH="$PATH" $0
82 require-non-root:*) ;;
85 test $give_msg = yes && {
87 ***************************
89 $0: This test is being skipped, since it works only
91 ***************************