Skip to content
Permalink
master
Go to file
 
 
Cannot retrieve contributors at this time
53 lines (47 sloc) 1.73 KB
## Copyright 2017 Clemens Fruhwirth <clemens@endorphin.org>
##
## Usage: source master-zshrc from your .zshrc
##
#FIXME incorrect user selection when:
# pwdjail-mkdir /somedir/foo
# pwdjail-mkdir /somedir/foobar
# cd foobar
# This will select the foo user as /somedir/foo is a prefix of /somedir/foobar.
# FIXME any modification to this function with pwdjail-rm-Ir
function pwdjail_get_user_for_directory() {
getent passwd | awk -F: '"'"$1"/'" ~ $6"/" && $5 ~ "Sandbox (X11 )?user" { print $1 }' | head -1
}
function pwdjail_enter() {
# Grab through /etc/passwd for sandbox users that have a home dir above $PWD.
SANDBOX_USER=$(pwdjail_get_user_for_directory "$PWD")
[ -z "$SANDBOX_USER" ] && return
SANDBOX_PWDLINE_RAW=$(getent passwd $SANDBOX_USER)
SANDBOX_PWDLINE=("${(@s/:/)SANDBOX_PWDLINE_RAW}")
if [ "$SANDBOX_PWDLINE[5]" = "Sandbox user" ]; then
X11=false
else
if [ "$SANDBOX_PWDLINE[5]" = "Sandbox X11 user" ]; then
X11=true
else
return
fi
fi
TMP=$(mktemp)
# FIXME Don't make that world-write readable.
chmod a+w $TMP
SANDBOX_ROOT=$SANDBOX_PWDLINE[6]
# FIXME only extract the cookie of the current $DISPLAY. Not all!
$X11 && cat ${XAUTHORITY:-$HOME/.Xauthority} | sudo -u $SANDBOX_USER tee $SANDBOX_ROOT/.Xauthority > /dev/null
export XAUTHORITY=$SANDBOX_ROOT/.Xauthority
sudo -u $SANDBOX_USER /usr/bin/env PWDJAIL_EXIT_DIR=$TMP zsh -i
EXIT_DIR=$(cat $TMP)
rm $TMP
if [ -z "$EXIT_DIR" ]; then
echo 'Warning: Sandbox shell exited without changing directory. Use "cd" to exit.'
echo "Returning you to $OLDPWD"
cd $OLDPWD
else
cd $EXIT_DIR
fi
}
chpwd_functions=(${chpwd_functions[@]} "pwdjail_enter")
You can’t perform that action at this time.