-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchrooter
More file actions
executable file
·230 lines (194 loc) · 8.47 KB
/
chrooter
File metadata and controls
executable file
·230 lines (194 loc) · 8.47 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/sh
# Abstract: CHange ROOT into desired directory (chrooter).
# Created by github.com/kreyren under GNUv3 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html)
### START OF KREYPI INIT ###
# https://github.com/RXT067/Scripts/tree/kreyren/kreypi
# Do not make additional functions here since we are going to source a library
# Check for root
if [ "$(id -u)" != 0 ]; then
printf 'FATAL: %s\n' "This script is using KREYPI library which needs to be exported in /lib/shell using root permission"
exit 3
elif [ "$(id -u)" = 0 ]; then
# shellcheck disable=SC2154
[ -n "$debug" ] && printf 'DEBUG: %s\n' "Script executed from an user with ID $(id -u)"
else
printf 'FATAL: %s\n' "Unexpected happend in KREYPI_INIT for checking root"
exit 255
fi
# Create a new directory for shell libraries if not present already
if [ ! -e /lib/shell ]; then
mkdir /lib/shell || { printf 'FATAL: %s\n' "Unable to make a new directory in '/lib/shell', is this non-standard file hierarchy?" ; exit 1 ;}
elif [ -f /lib/shell ]; then
printf 'FATAL: %s\n' "File '/lib/shell' is a file which is unexpected, expecting directory to export kreypi library"
exit 1
elif [ -d /lib/shell ]; then
# shellcheck disable=SC2154
[ -n "$debug" ] && printf 'DEBUG: %s\n' "Directory '/lib/shell' already exists, no need to make it"
else
printf 'FATAL: %s\n' "Unexpected result in KREYPI_INIT checking for /lib/shell"
exit 255
fi
# Fetch the library
if [ -e /lib/shell/kreypi.sh ]; then
# shellcheck disable=SC2154
[ -n "$debug" ] && printf 'DEBUG: %s\n' "Directory in '/lib/shell' already exists, skipping fetch"
elif command -v wget >/dev/null; then
wget https://raw.githubusercontent.com/RXT067/Scripts/kreyren/kreypi/kreypi.sh -O /lib/shell/kreypi.sh || { printf 'FATAL: %s\n' "Unable to fetch kreypi.sh from https://raw.githubusercontent.com/RXT067/Scripts/kreyren/kreypi/kreypi.sh in /lib/shell/kreypi.sh using wget" ; exit 1;}
elif command -v curl >/dev/null; then
curl https://raw.githubusercontent.com/RXT067/Scripts/kreyren/kreypi/kreypi.sh -o /lib/shell/kreypi.sh || { printf 'FATAL: %s\n' "Unable to fetch kreypi.sh from https://raw.githubusercontent.com/RXT067/Scripts/kreyren/kreypi/kreypi.sh in /lib/shell/kreypi.sh using curl" ; exit 1 ;}
else
printf 'FATAL: %s\n' "Unable to download kreypi library from 'https://raw.githubusercontent.com/RXT067/Scripts/kreyren/kreypi/kreypi.sh' in '/lib/shell/kreypi.sh'"
exit 255
fi
# Sanitycheck for /lib/shell
if [ -e /lib/shell ]; then
# shellcheck disable=SC2154
[ -n "$debug" ] && printf 'DEBUG: %s\n' "Directory in '/lib/shell' already exists, passing sanity check"
elif [ ! -e /lib/shell ]; then
printf 'FATAL: %s\n' "Sanitycheck for /lib/shell failed"
exit 1
else
printf 'FATAL: %s\n' "Unexpected happend in sanitycheck for /lib/shell"
exit 255
fi
# Source KREYPI
if [ -e "/lib/shell/kreypi.sh" ]; then
# 'source' can not be used on POSIX sh
# shellcheck source="/lib/shell/kreypi.sh"
. "/lib/shell/kreypi.sh" || { printf 'FATAL: %s\n' "Unable to source '/lib/shell/kreypi.sh'" ; exit 1 ;}
# shellcheck disable=SC2154
[ -n "$debug" ] && printf 'DEBUG: %s\n' "Kreypi in '/lib/shell/kreypi.sh' has been successfully sourced"
elif [ ! -e "/lib/shell/kreypi.sh" ]; then
printf 'FATAL: %s\n' "Unable to source '/lib/shell/kreypi.sh' since path does not exists"
exit 1
else
printf 'FATAL: %s\n' "Unexpected happend in sourcing KREYPI_INIT"
exit 255
fi
### END OF KREYPI INIT ###
# Sanitization for Mount Directory variable
sanitize_mount_directory() {
destdir_smd="$1"
# Sanity check for destdir_smd
if [ -z "$destdir_smd" ]; then
die 2 "Function 'sanitize_mount_directory' expects argument with path to mount directory which is not provided"
elif [ -n "$destdir_smd" ]; then
debug "Variable destdir_smd in function 'sanitize_mount_directory' is set on '$destdir_smd'"
# Check if destdir_smd is valid directory
if [ ! -d "$destdir_smd" ]; then
die 1 "First argument '$destdir_smd' in function 'sanitize_mount_directory' is not a valid directory"
elif [ -d "$destdir_smd" ]; then
debug "In function 'sanitize_mount_directory' variable destdir_smd stores a value of valid directory"
else
die 255 "sanitize_mount_directory, destdir_smd directory check"
fi
# Enforce FSH-3.0
case "$destdir_smd" in
/mnt/*) debug "Function 'sanitize_mount_directory' - destdir_smd '$destdir_smd' is valid" ;;
*) warn "Mount directories should be mounted in /mnt directory not $destdir_smd"
esac
if [ ! -e "$destdir_smd/etc" ] && [ -z "$forced" ]; then
die 1 "Unable to find /etc directory in $destdir_smd directory, assuming invalid mount directory, use --force to override if needed."
elif [ ! -e "$destdir_smd/etc" ] && [ -n "$forced" ]; then
warn "Directory '$destdir_smd/etc' is not found, but forced is used"
elif [ -e "$destdir_smd/etc" ]; then
debug "Directory '$destdir_smd' is valid directory for chroot assuming checking for '$destdir_smd/etc'"
else
die 255 "sanitize_mount_directory, destdir_smd/etc check"
fi
else
die 255 "sanitize_mount_directory, destdir_smd"
fi
unset destdir_smd
}
# Mount required filesystems for chrooting
## Reference: https://wiki.gentoo.org/wiki/Chroot#Configuration
mount_required_fs() {
destdir_mrf="$1"
# Sanitization - Expecting $destdir_mrf="/mnt/something"
sanitize_mount_directory "$destdir_mrf"
# Mount /dev
if ! grep -qF "$destdir_mrf/dev" /proc/mounts; then
mount --rbind /dev "$destdir_mrf/dev/" || die 1 "Unable to mount '/dev' as rbind on '$destdir_mrf/dev'"
mount --make-rslave "$destdir_mrf/dev" || die 1 "Unable to mount '/dev' as '-make-rslave' on '$destdir_mrf/dev'"
elif grep -qF "$destdir_mrf/dev" /proc/mounts; then
# FIXME: This may output if --rbind above fails
debug "Directory '$destdir_mrf/dev' is already mounted"
else
die 255 "mount_reguired_fs, dev"
fi
# Mount /sys
if ! grep -qF "$destdir_mrf/sys" /proc/mounts; then
mount --bind /sys "$destdir_mrf/sys/" || die 1 "Unable to mount '/sys' to '$destdir_mrf/sys' as bind which is required"
elif grep -qF "$destdir_mrf/sys" /proc/mounts; then
debug "Directory '$destdir_mrf/sys' is already mounted"
else
die 255 "mount_required_fs, sys"
fi
# Mount /tmp
if ! grep -qF "$destdir_mrf/tmp" /proc/mounts; then
mount --rbind /tmp "$destdir_mrf/tmp" || die 1 "Unable to mount '$destdir_mrf/tmp' as '--bind' on '/tmp'"
mount --make-rslave "$destdir_mrf/tmp" || die 1 "Unable to mount '/tmp' to '$destdir_mrf/tmp' as '--make-rbind' which is required"
elif grep -qF "$destdir_mrf/tmp" /proc/mounts; then
# FIXME: This may trigger if --rbind above failed
debug "Directory '$destdir_mrf/tmp' is already mounted"
else
die 255 "mount_required_fs, tmp"
fi
# Mount /proc
#if ! grep -qF "$destdir_mrf/proc" /proc/mounts; then { mount --types proc none "$destdir_mrf/proc/" || die 0 "Unable to mount '/proc' on 'none' which is required";}; fi
# HOTFIX, fuck everything
# FIXME!
mount --types proc none "$destdir_mrf/proc/" 2>/dev/null
unset destdir_mrf
}
# Perform chroot
chrooter() {
destdir_crt="$1"
command="$2"
# Sanitization - Expecting $destdir_crt="/mnt/something"
sanitize_mount_directory "$destdir_crt"
# Mount everything in chroot
# FIXME: Sanitize
{ chroot "$destdir_crt" mount -a && info "filesystems has been mounted on $destdir_crt" ;} || warn "Unable to mount all filesystems on $destdir_crt"
# Perform chroot
if [ -n "$command" ]; then
chroot "$destdir_crt" "$command" || die 1 "Unable to Change Root from '$destdir_crt' with command '$command'"
elif [ -z "$command" ]; then
chroot "$destdir_crt" /bin/bash || die 1 "Unable to Change Root from '$destdir_crt' to /bin/bash"
fi
unset destdir_crt command
}
# Take down chroot
kill_chrooter() {
destdir_kc="$1"
# Sanitization - Expecting $destdir_kc="/mnt/something"
sanitize_mount_directory "$destdir_kc"
if grep -qF $destdir_kc /proc/mounts; then
fixme "Unable to kill chroot on runtime (/dev preventing umount)"
umount -R "$destdir_kc" || die 1 "Unable to unmount $destdir_kc"
elif ! grep -qF "$destdir_kc" /proc/mounts; then
info "Directory $destdir_kc is already not mounted, skipping.."
return 0
else
die 255 "kill_chrooter, checking for destdir_kc"
fi
}
# CODE
checkroot "$@"
while [ "$#" -ge 1 ]; do case "$1" in
-f|--force) export forced="KREYREN"; shift 1 ;;
-K|--kill) kill_chrooter "$2" ; exit 0 ;;
/*) # Allow full path only
mount_required_fs "$1"
chrooter "$1" "$2"
if [ -n "$2" ]; then
shift 2
elif [ -n "$1" ] && [ -z "$2" ]; then
shift 1
else
die 255 "Arguments, shifting of full unix path"
fi
;;
*) die 2 "Unrecognized argument '$1' was parsed"
esac; done