-
Notifications
You must be signed in to change notification settings - Fork 0
/
master-zshrc
53 lines (47 loc) · 1.73 KB
/
master-zshrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
## 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")