]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc-setcap.in
lxc: add capabilities for C/R
[mirror_lxc.git] / src / lxc / lxc-setcap.in
1 #!/bin/bash
2
3 #
4 # lxc: linux Container library
5
6 # Authors:
7 # Daniel Lezcano <daniel.lezcano@free.fr>
8
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
18
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23 #
24 # This script allows to set or remove the capabilities on the lxc tools.
25 # When the capabilities are set, a non root user can manage the containers.
26 #
27
28 LXC_CREATE_CAPS="cap_sys_admin"
29 LXC_NETSTAT_CAPS="cap_sys_admin"
30 LXC_INIT_CAPS="cap_sys_admin,cap_dac_override"
31 LXC_COMMON_CAPS="cap_net_admin,cap_net_raw,cap_sys_admin,cap_dac_override"
32 LXC_UNSHARE_CAPS=$LXC_COMMON_CAPS
33 LXC_START_CAPS="$LXC_COMMON_CAPS,cap_fowner,cap_sys_chroot,cap_setpcap"
34 LXC_EXECUTE_CAPS=$LXC_START_CAPS
35 LXC_RESTART_CAPS="$LXC_START_CAPS,cap_mknod"
36 LXC_CHECKPOINT_CAPS="$LXC_COMMON_CAPS,cap_sys_ptrace,cap_mknod"
37
38 LXC_DROP_CAPS=""
39
40 usage()
41 {
42 echo "lxc-setcap [-d] : set or remove capabilities on the lxc tools"
43 }
44
45 lxc_setcaps()
46 {
47 setcap $LXC_CREATE_CAPS=ep @BINDIR@/lxc-create
48 setcap $LXC_EXECUTE_CAPS=ep @BINDIR@/lxc-execute
49 setcap $LXC_START_CAPS=ep @BINDIR@/lxc-start
50 setcap $LXC_RESTART_CAPS=ep @BINDIR@/lxc-restart
51 setcap $LXC_UNSHARE_CAPS=ep @BINDIR@/lxc-unshare
52 setcap $LXC_NETSTAT_CAPS=ep @BINDIR@/lxc-netstat
53 setcap $LXC_CHECKPOINT_CAPS=ep @BINDIR@/lxc-checkpoint
54 setcap $LXC_INIT_CAPS=ep @LIBEXECDIR@/lxc-init
55
56 test -e @LXCPATH@ || mkdir -p @LXCPATH@
57 chmod 0777 @LXCPATH@
58 }
59
60 lxc_dropcaps()
61 {
62 setcap -r @BINDIR@/lxc-create
63 setcap -r @BINDIR@/lxc-execute
64 setcap -r @BINDIR@/lxc-start
65 setcap -r @BINDIR@/lxc-restart
66 setcap -r @BINDIR@/lxc-unshare
67 setcap -r @BINDIR@/lxc-netstat
68 setcap -r @BINDIR@/lxc-checkpoint
69 setcap -r @LIBEXECDIR@/lxc-init
70 chmod 0755 @LXCPATH@
71 }
72
73 if [ "$(id -u)" != "0" ]; then
74 echo "You have to be root to run this script"
75 exit 1
76 fi
77
78
79 if [ $? != 0 ]; then
80 usage
81 exit 1
82 fi
83
84 set -- $(getopt dh $*)
85
86 for i in $*; do
87 case "$1" in
88 -d)
89 LXC_DROP_CAPS="yes"
90 shift
91 ;;
92 -h)
93 usage
94 exit 0
95 ;;
96 --)
97 shift
98 break
99 ;;
100 *)
101 usage
102 exit 1
103 ;;
104 esac
105 done;
106
107 if [ -z "$LXC_DROP_CAPS" ]; then
108 lxc_setcaps
109 else
110 lxc_dropcaps
111 fi