Debian Codegraph @

templates/lxc-sshd.in

raw
1 #!/bin/bash
2
3 #
4 # lxc: linux Container library
5
6 # Authors:
7 # Daniel Lezcano <[email protected]>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
23 # Detect use under userns (unsupported)
24 for arg in "$@"; do
25 [ "$arg" = "--" ] && break
26 if [ "$arg" = "--mapped-uid" -o "$arg" = "--mapped-gid" ]; then
27 echo "This template can't be used for unprivileged containers." 1>&2
28 echo "You may want to try the \"download\" template instead." 1>&2
29 exit 1
30 fi
31 done
32
33 # Make sure the usual locations are in PATH
34 export PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
35
36 install_sshd()
37 {
38 rootfs=$1
39
40 tree="\
41 $rootfs/var/empty/sshd \
42 $rootfs/var/lib/empty/sshd \
43 $rootfs/etc/init.d \
44 $rootfs/etc/rc.d \
45 $rootfs/etc/ssh \
46 $rootfs/etc/sysconfig/network-scripts \
47 $rootfs/dev/shm \
48 $rootfs/run/sshd \
49 $rootfs/proc \
50 $rootfs/sys \
51 $rootfs/bin \
52 $rootfs/sbin \
53 $rootfs/usr \
54 $rootfs/tmp \
55 $rootfs/home \
56 $rootfs/root \
57 $rootfs/lib \
58 $rootfs/lib64"
59
60 mkdir -p $tree
61 if [ $? -ne 0 ]; then
62 return 1
63 fi
64
65 ln -s /run $rootfs/var/run
66 if [ $? -ne 0 ]; then
67 return 1
68 fi
69
70 return 0
71 }
72
73 configure_sshd()
74 {
75 rootfs=$1
76
77 cat <<EOF > $rootfs/etc/passwd
78 root:x:0:0:root:/root:/bin/bash
79 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
80 EOF
81
82 cat <<EOF > $rootfs/etc/group
83 root:x:0:root
84 sshd:x:74:
85 EOF
86
87 ssh-keygen -t rsa -N "" -f $rootfs/etc/ssh/ssh_host_rsa_key
88 ssh-keygen -t dsa -N "" -f $rootfs/etc/ssh/ssh_host_dsa_key
89
90 # by default setup root password with no password
91 cat <<EOF > $rootfs/etc/ssh/sshd_config
92 Port 22
93 Protocol 2
94 HostKey /etc/ssh/ssh_host_rsa_key
95 HostKey /etc/ssh/ssh_host_dsa_key
96 UsePrivilegeSeparation yes
97 SyslogFacility AUTH
98 LogLevel INFO
99 LoginGraceTime 120
100 PermitRootLogin yes
101 StrictModes yes
102 PubkeyAuthentication yes
103 IgnoreRhosts yes
104 HostbasedAuthentication no
105 PermitEmptyPasswords yes
106 ChallengeResponseAuthentication no
107 EOF
108
109 if [ -n "$auth_key" -a -f "$auth_key" ]; then
110 u_path="/root/.ssh"
111 root_u_path="$rootfs/$u_path"
112 mkdir -p $root_u_path
113 cp $auth_key "$root_u_path/authorized_keys"
114 chown -R 0:0 "$rootfs/$u_path"
115 chmod 700 "$rootfs/$u_path"
116 echo "Inserted SSH public key from $auth_key into $rootfs/$u_path"
117 fi
118
119 return 0
120 }
121
122 copy_configuration()
123 {
124 path=$1
125 rootfs=$2
126 name=$3
127
128 init_path=$(realpath --relative-to=/ $(readlink -f /sbin/init))
129
130 grep -q "^lxc.rootfs.path" $path/config 2>/dev/null || echo "lxc.rootfs.path = $rootfs" >> $path/config
131 cat <<EOF >> $path/config
132 lxc.uts.name = $name
133 lxc.pty.max = 1024
134 lxc.cap.drop = sys_module mac_admin mac_override sys_time
135
136 # When using LXC with apparmor, uncomment the next line to run unconfined:
137 #lxc.apparmor.profile = unconfined
138
139 lxc.autodev = 1
140
141 lxc.mount.entry = /lib lib none ro,bind 0 0
142 lxc.mount.entry = /bin bin none ro,bind 0 0
143 lxc.mount.entry = /usr usr none ro,bind 0 0
144 lxc.mount.entry = /sbin sbin none ro,bind 0 0
145 lxc.mount.entry = tmpfs run/sshd tmpfs mode=0644 0 0
146 lxc.mount.entry = @LXCTEMPLATEDIR@/lxc-sshd $init_path none ro,bind 0 0
147 lxc.mount.entry = /etc/init.d etc/init.d none ro,bind 0 0
148
149 lxc.mount.auto = cgroup:mixed proc:mixed sys:mixed
150 EOF
151
152 # Oracle Linux and Fedora need the following two bind mounted
153 if [ -d /etc/sysconfig/network-scripts ]; then
154 cat <<EOF >> $path/config
155 lxc.mount.entry = /etc/sysconfig/network-scripts etc/sysconfig/network-scripts none ro,bind 0 0
156 EOF
157 fi
158
159 if [ -d /etc/rc.d ]; then
160 cat <<EOF >> $path/config
161 lxc.mount.entry = /etc/rc.d etc/rc.d none ro,bind 0 0
162 EOF
163 fi
164
165 # if no .ipv4 section in config, then have the container run dhcp
166 grep -q "^lxc.net.0.ipv4.address" $path/config || touch $rootfs/run-dhcp
167
168 if [ "$(uname -m)" = "x86_64" ]; then
169 cat <<EOF >> $path/config
170 lxc.mount.entry = /lib64 lib64 none ro,bind 0 0
171 EOF
172 fi
173 }
174
175 usage()
176 {
177 cat <<EOF
178 $1 -h|--help -p|--path=<path> [--rootfs=<path>]
179 EOF
180 return 0
181 }
182
183 check_for_cmd()
184 {
185 cmd_path=`type $1`
186 if [ $? -ne 0 ]; then
187 echo "The command '$1' $cmd_path is not accessible on the system"
188 exit 1
189 fi
190 # we use cut instead of awk because awk is alternatives symlink on ubuntu
191 # and /etc/alternatives isn't bind mounted
192 cmd_path=`echo $cmd_path |cut -d ' ' -f 3`
193 }
194
195 options=$(getopt -o hp:n:S: -l help,rootfs:,path:,name:,auth-key: -- "$@")
196 if [ $? -ne 0 ]; then
197 usage $(basename $0)
198 exit 1
199 fi
200 eval set -- "$options"
201
202 while true
203 do
204 case "$1" in
205 -h|--help) usage $0 && exit 0;;
206 -p|--path) path=$2; shift 2;;
207 --rootfs) rootfs=$2; shift 2;;
208 -n|--name) name=$2; shift 2;;
209 -S|--auth-key) auth_key=$2; shift 2;;
210 --) shift 1; break ;;
211 *) break ;;
212 esac
213 done
214
215 if [ "$(id -u)" != "0" ]; then
216 echo "This script should be run as 'root'"
217 exit 1
218 fi
219
220 if [ $0 = "/sbin/init" ]; then
221
222 PATH="$PATH:/bin:/sbin:/usr/sbin"
223 check_for_cmd @SBINDIR@/init.lxc
224 check_for_cmd sshd
225 sshd_path=$cmd_path
226
227 # run dhcp?
228 if [ -f /run-dhcp ]; then
229 check_for_cmd dhclient
230 check_for_cmd ifconfig
231 touch /etc/fstab
232 rm -f /dhclient.conf
233 cat > /dhclient.conf << EOF
234 send host-name = gethostname();
235 EOF
236 ifconfig eth0 up
237 dhclient eth0 -cf /dhclient.conf
238 echo "Container IP address:"
239 ifconfig eth0 |grep inet
240 fi
241
242 exec @SBINDIR@/init.lxc --name $name -- $sshd_path
243 exit 1
244 fi
245
246 if [ -z "$path" ]; then
247 echo "'path' parameter is required"
248 exit 1
249 fi
250
251 # detect rootfs
252 config="$path/config"
253 if [ -z "$rootfs" ]; then
254 if grep -q '^lxc.rootfs.path' $config 2>/dev/null ; then
255 rootfs=$(awk -F= '/^lxc.rootfs.path =/{ print $2 }' $config)
256 else
257 rootfs=$path/rootfs
258 fi
259 fi
260
261 install_sshd $rootfs
262 if [ $? -ne 0 ]; then
263 echo "failed to install sshd's rootfs"
264 exit 1
265 fi
266
267 configure_sshd $rootfs
268 if [ $? -ne 0 ]; then
269 echo "failed to configure sshd template"
270 exit 1
271 fi
272
273 copy_configuration $path $rootfs $name
274 if [ $? -ne 0 ]; then
275 echo "failed to write configuration file"
276 exit 1
277 fi