+# An ordinary user cannot change the UID of a file if that UID is
+# not their own, but chown can also be used to change the GID of a
+# file as well so it is feasible that an ordinary user could use
+# chown successfully.
+
+# preseve the command line as we're gonna mess with it.
+cmdline="$@"
+# strip off the options so that $1 becomes the UID:GID arg
+while [ -n "$1" ]; do
+ case "$1" in
+ (-*) shift ;;
+ (*) break ;;
+ esac
+done
+
+# Split USER:GROUP or USER.GROUP into USER and GROUP
+usrgrp="$1"
+usr=${usrgrp/[.:]*/}
+grp=${usrgrp/*[.:]/}
+
+report=0
+
+# Catch the case where USER is somebody else.
+if [ -n "$usr" ]; then
+ printf '%d' "$usr" &>/dev/null
+ if [ $? -eq 0 ]; then
+ if [ $usr -ne $(id -u) ]; then
+ report=1
+ fi
+ else
+ if [ "$usr" != "$(id -un)" ]; then
+ report=1
+ fi
+ fi
+fi
+
+# Catch the case where GROUP isn't in our groups.
+if [ -n "$grp" -a $report -eq 0 ]; then
+ GRP_CHAIN=""
+ printf '%d' "$grp" &>/dev/null
+ if [ $? -eq 0 ]; then
+ GRP_LIST=$(id -G)
+ else
+ GRP_LIST=$(id -Gn)
+ fi
+ for g in ${GRP_LIST}; do
+ if [ "$grp" == "$g" ]; then
+ GRP_CHAIN=$g
+ break
+ fi
+ done
+
+ if [ -z "$GRP_CHAIN" ]; then
+ report=1
+ fi