Port tests/rmdir/ignore away from GNU/Linux.
[coreutils/ericb.git] / tests / priv-check
blob7d7b1fead1c0fec36d6998ba48f4e907543eea1c
1 # -*- sh -*-
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"\
24      1>&2; exit 1;;
25 esac
27 # Make sure id -u succeeds.
28 my_uid=`id -u`
29 test $? = 0 || {
30   echo "$0: cannot run \`id -u'" 1>&2
31   (exit 1); exit 1
34 # Make sure it gives valid output.
35 case $my_uid in
36   *[!0-9]*)
37     echo "$0: invalid output (\`$my_uid') from \`id -u'" 1>&2
38     (exit 1); exit 1
39     ;;
40   *) ;;
41 esac
43 test $my_uid = 0 && \
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`
51   test $? = 0 || \
52     {
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
56       (exit 77); exit 77
57     }
58   test "$coreutils_non_root_uid" = 0 && \
59     {
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
62       (exit 1); exit 1
63     }
66 give_msg=no
67 case $PRIV_CHECK_ARG:$my_uid in
68   require-root:0) ;;
69   require-root:*) give_msg=yes ;;
70   require-non-root:0)
71     # `.' must be writable by $NON_ROOT_USERNAME
72     setuidgid $NON_ROOT_USERNAME test -w . ||
73       {
74         echo "$0: `pwd`: not writable by user \`$NON_ROOT_USERNAME'" 1>&2
75         echo "$0: skipping this test" 1>&2
76         (exit 77); exit 77
77       }
78     exec setuidgid $NON_ROOT_USERNAME env PATH="$PATH" $0
79     ;;
80   require-non-root:*) ;;
81 esac
83 test $give_msg = yes && {
84   cat <<EOF
85 ***************************
86 NOTICE:
87 $0: This test is being skipped, since it works only
88 when run $who.
89 ***************************
90 EOF
91   (exit 77); exit 77