More checks in become.
[profile.git] / opt / bin / become
index 9be0b9b..cfc498e 100755 (executable)
@@ -1,22 +1,36 @@
 #!/bin/bash
 
 kerberos=0
-while getopts ":k" opt; do
+x11=0
+while getopts ":kx" opt; do
   case $opt in
-    k) kerberos=1
+    k) kerberos=1;;
+    x) x11=1;;
   esac
 done
 shift $((OPTIND-1))
 
 user="$1"; shift
 if [ -z "$user" ]; then
-  echo >&2 "Usage: become [-k] <user>"
+  echo >&2 "Usage: become [-k] [-x] <user>"
   echo >&2 "Options: -k   Delegate Kerberos credentials even if target user is not root."
+  echo >&2 "         -x   Delegate X11 cookie even if target user is not root."
   exit 1
 fi
 
+uid=$(PATH=/usr/xpg4/bin:/usr/bin id -u "$user" 2>/dev/null)
+if [ -z "$uid" ]; then
+  echo >&2 "Who is $user?"
+  exit 2
+fi
+
+if [ $uid = 0 ]; then
+  kerberos=1
+  x11=1
+fi
+
 PRINCIPAL=$(klist 2>/dev/null | sed -n 's/^Default principal: //p')
-if [ -n "$DISPLAY" -a "${DISPLAY##localhost:}" = "$DISPLAY" ]; then
+if [ $x11 = 1 -a -n "$DISPLAY" -a "${DISPLAY##localhost:}" = "$DISPLAY" ]; then
   COOKIE="$(xauth list $DISPLAY)"
 fi
 BECOME="$HOME/.become"