summaryrefslogtreecommitdiffstats
path: root/base/all/root/scripts/cluster_configure/mkchroot.sh
blob: 1f2bb0a7be52c4a8619a32d26fd4f7d9865fd58b (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/sh

set -e

# Make a simple chroot for vsftpd and scp.

# Note that for the base chroot this attempts to use read-only bind
# mounts.  These will be silently readable on older kernels.  If
# you're using this for anything except simple testing then PLEASE
# TEST the chroot's security!

usage ()
{
    cat <<EOF
usage: $0 { create | destroy } chroot_dir
       $0 mount src_dir dst_dir
       $0 umount dst_dir
EOF
    exit 1
}

[ $# -ge 1 ] || usage

action="$1"
shift
case "$action" in
    create|destroy|umount)
	:
	;;
    mount)
	[ $# -ge 2 ] || usage
	;;
    *)
	usage
esac

do_mount ()
{
    _src="$1"
    _dst="$2"

    if [ -f "$_dst" ] ; then
	return 0
    fi

    if [ -f "$_src" ] ; then
	mkdir -vp $(dirname "$_dst")
	touch "$_dst"
    else
	mkdir -vp "$_dst"
    fi

    mount -v --bind "$_src" "$_dst"
}

do_umount ()
{
    _dst="$1"

    _out=$(awk "\$2 == \"$_dst\" {print \$2}" /proc/mounts)    
    if [ -n "$_out" ] ; then
	_is_file=false
	if [ -f "$_dst" ] ; then
	    _is_file=true
	fi
	umount -v "$_dst"
	if $_is_file ; then
	    rm -vf "$_dst"
	    rmdir --ignore-fail-on-non-empty -vp $(dirname "$_dst")
	else
	    rmdir --ignore-fail-on-non-empty -vp "$_dst"
	fi
    fi
}

first_ro_mount=true
can_remount_ro=true

do_mount_ro ()
{
    _src="$1"
    _dst="$2"

    if [ -f "$_dst" ] ; then
	return 0
    fi

    do_mount "$_src" "$_dst"

    if $first_ro_mount ; then
	if ! mount -v -o remount,ro "$_dst"; then
	    cat <<EOF
Unable to remount $_dst read-only.  Won't try this again.

WARNING: Your chroot may be less secure than you think!

EOF
	    can_remount_ro=false
	else
	    if f=$(mktemp -p "$_dst" >/dev/null 2>&1) ; then
		rm -f "$f"
		cat <<EOF
WARNING: Bind mounts don't really look to be read-only!
EOF
	    fi
	fi
    else
	if $can_remount_ro ; then
	    mount -v -o remount,ro "$_dst"
	fi
    fi

    first_ro_mount=false
}

sftp_server=""
for d in /usr/libexec/openssh /usr/lib/openssh ; do
    f="$d/sftp-server"
    if [ -x "$f" ] ; then
	sftp_server="$f"
	break
    fi
done
if [ -z "$sftp_server" ] ; then
    echo "$0: error - could not find location of sftp_server"
    exit 2
fi

case $(uname -m) in
    x86_64)
	lib="lib64"
	;;
    *)
	lib="lib"
esac

mounts="\
/usr/$lib \
/$lib \
/etc/nsswitch.conf \
/etc/resolv.conf \
$sftp_server \
/usr/bin/scp \
"

do_create ()
{
    chroot_dir="$1"

    mkdir -p "$chroot_dir"

    fake="${chroot_dir}/.mkchroot"
    touch "$fake"
    ls -l "$fake"

    d="$chroot_dir/dev"
    mkdir -vp "$d"
    cp -va /dev/null /dev/zero "$d/"

    for s in $mounts ; do
	f="$chroot_dir$s"
	do_mount_ro "$s" "$f"
    done

    d="$chroot_dir/etc/passwd"
    touch "$d"
    ls -l "$d"
}

do_destroy ()
{
    chroot_dir="$1"

    rm -vf "$chroot_dir/etc/passwd"

    for i in "null" "zero" ; do
	rm -vf "$chroot_dir/dev/$i"
    done
    rmdir -v "$chroot_dir/dev"

    # Reverse the list 1st.
    rmounts=
    for s in $mounts ; do
	rmounts="$s${rmounts:+ }${rmounts}"
    done

    for s in $rmounts ; do
	do_umount "${chroot_dir}$s"
    done

    fake="${chroot_dir}/.mkchroot"
    rm -vf "$fake"
    rmdir -v "$chroot_dir"
}

"do_$action" "$@"