
1: # -*- sh -*- 2: # Source this file at the beginning of a test that works 3: # only when run as root or as non-root. 4: 5: # Copyright (C) 2001, 2003, 2004 Free Software Foundation, Inc. 6: 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. 11: 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. 16: 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 20: # 02110-1301, USA. 21: 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"\ 26: 1>&2; exit 1;; 27: esac 28: 29: # Make sure id -u succeeds. 30: my_uid=`id -u` 31: test $? = 0 || { 32: echo "$0: cannot run \`id -u'" 1>&2 33: (exit 1); exit 1 34: } 35: 36: # Make sure it gives valid output. 37: case $my_uid in 38: *[!0-9]*) 39: echo "$0: invalid output (\`$my_uid') from \`id -u'" 1>&2 40: (exit 1); exit 1 41: ;; 42: *) ;; 43: esac 44: 45: test $my_uid = 0 && \ 46: { 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} 50: 51: # Ensure that the supplied username is valid and with UID != 0. 52: coreutils_non_root_uid=`id -u $NON_ROOT_USERNAME` 53: test $? = 0 || \ 54: { 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 58: (exit 77); exit 77 59: } 60: test "$coreutils_non_root_uid" = 0 && \ 61: { 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 64: (exit 1); exit 1 65: } 66: } 67: 68: give_msg=no 69: case $PRIV_CHECK_ARG:$my_uid in 70: require-root:0) ;; 71: require-root:*) give_msg=yes ;; 72: require-non-root:0) 73: # `.' must be writable by $NON_ROOT_USERNAME 74: setuidgid $NON_ROOT_USERNAME test -w . || 75: { 76: echo "$0: `pwd`: not writable by user \`$NON_ROOT_USERNAME'" 1>&2 77: echo "$0: skipping this test" 1>&2 78: (exit 77); exit 77 79: } 80: exec setuidgid $NON_ROOT_USERNAME env PATH="$PATH" $0 81: ;; 82: require-non-root:*) ;; 83: esac 84: 85: test $give_msg = yes && { 86: cat <<EOF 87: *************************** 88: NOTICE: 89: $0: This test is being skipped, since it works only 90: when run $who. 91: *************************** 92: EOF 93: (exit 77); exit 77 94: }