-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxcage
executable file
·132 lines (112 loc) · 3.7 KB
/
xcage
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/sh
# Copyright (C)2011 Laurence Tratt <http://tratt.net/laurie/>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# http://tratt.net/laurie/src/xcage/
#
# This utility runs an Xnest (i.e. X Windows-inside-X Windows), and inside
# that executes a command as another user. This allows one to conveniently
# run an X Windows "jail" or "sandbox" where programs can execute without
# impacting upon one's normal user account.
#
# This utility requires sudo to work appropriately.
#
# This utility was inspired by xsandbox:
# http://shoestringfoundation.org/cgi-bin/blosxom.cgi/2005/11/16
which sudo 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
sudo="sudo"
else
which doas 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
sudo="doas"
else
sudo=""
fi
fi
user=""
cmd=""
xnest_args=""
if [ -f ~/.xcagerc ]; then
. ~/.xcagerc
fi
ae=0
while getopts ":hc:u:" f
do
case "$f" in
c) cmd="$OPTARG";;
u) user="$OPTARG";;
h) ae=1; break;;
s) sudo="$OPTARG";;
[?]) ae=2; break;;
esac
done
if [ "$sudo" == "" ]; then
echo "No sudo-esque command specified." > /dev/stderr
exit 1
fi
if [[ "X$user" = "X" || "X$cmd" = "X" ]]; then
ae=2
fi
if [ $ae -gt 0 ]; then
echo "Usage: xcage [-c <cmd>] [-u <user>] [-- <arguments for Xnest>]" >&2
if [ $ae -eq 1 ]; then
exit 0
else
exit 1
fi
fi
shift $((OPTIND-1))
function die {
echo $1
exit $2
}
# Setup the appropriate authorisation - we use two files to ensure that only
# the current user and $user can access the X authorisation files.
which jot > /dev/null 2> /dev/null
if [ $? -eq 0 ]; then
hexkey=`jot -p 8 -s "" -r -w "%.2x" 16 0 255`
else
hexkey=`head -c 16 < /dev/urandom | hexdump -v -e '/1 "%02x"'` ||
die "Can't create hex key" $?
fi
umask 022
xauthmef=`mktemp` ||
die "Can't create temporary file" $?
trap "rm -f $xauthmef" 0 1 2 3 13 15
xauthof=`$sudo su -l $user -c mktemp` ||
die "Can't create temporary file as user $user" $?
trap "rm -f $xauthmef; $sudo -u $user rm -f $xauthof" 0 1 2 3 13 15
xauth -i -f $xauthmef add :1.0 . $hexkey ||
exit $?
xauth -i -f $xauthmef nextract - :1.0 |
$sudo -u $user xauth -f $xauthof nmerge - ||
exit $?
# Run Xnest using the override args or not, as appropriate.
if [ $# -eq 0 ]; then
Xnest :1 -auth $xauthmef -sss $xnest_args &
else
Xnest :1 -auth $xauthmef -sss $* &
fi
# Run the specified command $cmd as $user.
$sudo su -l $user -c "XAUTHORITY=$xauthof DISPLAY=:1.0 $cmd"
# Sometimes Xnest exits unexpectedly, and there may be other Xnests running
# in the system, so we're careful only to kill an Xnest process (should
# there be any running) if it was started by this script.
pkill -P $$ Xnest