4 # Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; version 2 of the License.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 : ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}
20 test -e "$UTIL_VSERVER_VARS" || {
21 echo "Can not find util-vserver installation; aborting..."
24 . "$UTIL_VSERVER_VARS"
30 $"Usage: /usr/sbin/chcontext [--cap [!]<cap_name>] [--secure] [--ctx <num>] [--disconnect]
31 [--domainname <name>] [--hostname <name>] [--flag <flags>+]
32 [--silent] [--] command arguments ...
34 chcontext allocate a new security context and executes
35 a command in that context.
36 By default, a new/unused context is allocated
39 Add a capability from the command. This option may be
40 repeated several time.
41 See /usr/include/linux/capability.h
42 In general, this option is used with the --secure option
43 --secure removes most critical capabilities and --cap
47 Remove a capability from the command. This option may be
48 repeated several time.
49 See /usr/include/linux/capability.h
52 Select the context. On root in context 0 is allowed to
53 select a specific context.
54 Context number 1 is special. It can see all processes
55 in any contexts, but can't kill them though.
56 Option --ctx may be repeated several times to specify up to 16 contexts.
58 Start the command in background and make the process
60 --domainname new_domainname
61 Set the domainname (NIS) in the new security context.
62 Use "none" to unset the domain name.
64 Set one flag in the new or current security context. The following
65 flags are supported. The option may be used several time.
67 fakeinit: The new process will believe it is process number 1.
68 Useful to run a real /sbin/init in a vserver.
69 lock: The new process is trapped and can't use chcontext anymore.
70 sched: The new process and its children will share a common
72 nproc: Limit the number of process in the vserver according to
73 ulimit setting. Normally, ulimit is a per user thing.
74 With this flag, it becomes a per vserver thing.
75 private: No one can join this security context once created.
76 ulimit: Apply the current ulimit to the whole context
77 --hostname new_hostname
78 Set the hostname in the new security context
79 This is need because if you create a less privileged
80 security context, it may be unable to change its hostname
82 Remove all the capabilities to make a virtual server trustable
84 Do not print the allocated context number.
86 Report bugs to <$PACKAGE_BUGREPORT>."
90 function showVersion()
93 $"chcontext $PACKAGE_VERSION -- allocates/enters a security context
94 This program is part of $PACKAGE_STRING
96 Copyright (C) 2003 Enrico Scholz
97 This program is free software; you may redistribute it under the terms of
98 the GNU General Public License. This program has absolutely no warranty."
104 if test -d "$tmpdir"; then
105 local S="$tmpdir"/socket
106 test ! -S "$S" || $_KEEP_CTX_ALIVE --quit "$S" &>/dev/null
112 $_VSERVER_INFO - FEATURE migrate || exec $_CHCONTEXT_COMPAT "$@"
114 tmp=$(getopt -o + --long cap:,ctx:,disconnect,domainname:,flag:,hostname:,secure,silent,help,version -n "$0" -- "$@") || exit 1
126 --help) showHelp $0 ;;
127 --version) showVersion ;;
128 --cap) OPT_CAPS=( "${OPT_CAPS[@]}" "$1" ); shift;;
129 --ctx) OPT_CTX=$1; shift;;
130 --disconnect) OPT_DISCONNECT=1;
131 --domainname) OPT_DOMAINNAME=$1; shift;;
132 --hostname) OPT_HOSTNAME=$1; shift;;
133 --flag) OPT_FLAGS=( "${OPT_FLAGS[@]}" "$1" ); shift;;
134 --secure) OPT_SECURE=1;;
135 --silent) OPT_SILENT=1;;
137 *) echo $"chcontext: internal error; arg=='$1'"; exit 1;;
143 tmpdir=$(mktemp -d /tmp/chcontext.XXXXXX)
146 xid=$($_VCONTEXT --create ${OPT_SILENT:+--silent} ${OPT_CTX:+--xid "$OPT_CTX"} -- $_CTX_KEEP_ALIVE --socket "$tmpdir"/socket --timeout 60)
148 test "$rc" -ne 255 ||
149 echo $"chcontext: Failed to create the context; aborting..."
153 test "$rc" -ne 254 || xid=$OPT_CTX
155 echo $"chcontext: Failed to determine context xid; aborting..."
160 test "$rc" -ne 0 || {
165 if test "$rc" -ne 254; then
166 $_VATTRIBUTE --xid "$xid" -s \
167 ${OPT_CAPS:+--cap "${OPT_CAPS[@]}"}
168 ${OPT_FLAGS:+--flag "${OPT_FLAGS[@]}"} \
169 ${OPT_SECURE:+--secure}
171 $_VUNAME --xid "$xid" -s \
172 ${OPT_DOMAINNAME:+--domainname "$OPT_DOMAINNAME"} \
173 ${OPT_HOSTNAME:+--nodename "$OPT_HOSTNAME"}
176 exec $_VCONTEXT --migrate --xid "$xid" \
177 ${OPT_SILENT:+--silent} \
178 ${OPT_DISCONNECT:+--disconnect} \
179 ${rc:--syncsock "$tmpdir"/socket} \