6 while getopts ":kx" opt; do
16 if [ -z "$user" ]; then
17 echo >&2 "Usage: become [-c] [-k] [-x] <user>"
18 echo >&2 "Options: -c Stay in current directory even if target user is not root."
19 echo >&2 " -k Delegate Kerberos credentials even if target user is not root."
20 echo >&2 " -x Delegate X11 cookie even if target user is not root."
24 uid=$(PATH=/usr/xpg4/bin:/usr/bin id -u "$user" 2>/dev/null)
25 if [ -z "$uid" ]; then
26 echo >&2 "Who is $user?"
36 PRINCIPAL=$(klist 2>/dev/null | sed -n 's/^Default principal: //p')
37 if [ $x11 = 1 -a -n "$DISPLAY" -a "${DISPLAY##localhost:}" = "$DISPLAY" ]; then
38 COOKIE="$(xauth list $DISPLAY)"
41 for candidate in "$HOME" "$PROFILE_HOME"; do
42 [ -n "$candidate" ] || continue
43 BECOME="$candidate/.become"
44 [ -d "$BECOME" ] || continue
46 # Script to run when becoming any user.
47 [ -z "$allusersprofile" ] && allusersprofile="$BECOME/all"
48 [ -f "$allusersprofile" ] || allusersprofile=
50 # Set $HOME/all sticky to ignore $PROFILE_HOME/$user.
51 if [ "$candidate" = "$HOME" ]; then
52 [ -k "$BECOME/all" ] && ignore_profile_user=1
54 [ $ignore_profile_user = 1 ] && continue
57 # Script to run (after the one mentioned above) when becoming this user.
58 [ -z "$userprofile" ] && userprofile="$BECOME/$user"
59 [ -f "$userprofile" ] || userprofile=
62 file="${TMPDIR:-/tmp}/$USER.become.$user.$RANDOM.$$"
63 umask=$(builtin umask -p)
65 if exec 3>"$file" && exec <"$file" && rm "$file"; then
68 echo >&3 "PROFILE_HOME='${PROFILE_HOME:-$HOME}'"
69 if [ -n "$PRINCIPAL" ]; then
70 echo >&3 "PRINCIPAL='$PRINCIPAL'"
71 if [ $kerberos = 1 ]; then
72 ccname=$(klist 2>/dev/null | sed -n 's/^Ticket cache: [DF]I[LR][E:]://p')
73 if [ -f "$ccname" ]; then
74 echo >&3 "export KRB5CCNAME='$KRB5CCNAME'"
75 openssl=$(find_working openssl)
76 if [ -n "$openssl" ]; then
77 echo >&3 "KRB5OPENSSL='$openssl'"
78 echo >&3 "KRB5BASE64='$($openssl enc -a -in $ccname)'"
83 if [ -n "$DISPLAY" -a -n "$COOKIE" ]; then
84 echo >&3 "xauth add $COOKIE"
86 echo >&3 "unset DISPLAY"
88 if [ ! "$PROFILE_HOME" = "$HOME" ]; then
89 echo >&3 "export SCREENRC=$PROFILE_HOME/.screenrc"
95 echo >&3 ". ${PROFILE_HOME:-$HOME}/.bash_profile"
96 [ -f "$allusersprofile" ] && cat >&3 2>/dev/null "$allusersprofile"
97 [ -f "$userprofile" ] && cat >&3 2>/dev/null "$userprofile"
98 [ $chdir = 1 ] && echo >&3 2>/dev/null "cd - &>/dev/null"
102 [ "$dir" = "." ] && dir="$PWD"
103 exec sudo -H -u "$user" "$dir/became"