-
Notifications
You must be signed in to change notification settings - Fork 1
/
embedJT.sh
183 lines (148 loc) · 3.14 KB
/
embedJT.sh
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
#! /bin/sh
# this script prepares the 'jt' superscript before it is embedded into busybox.
# It encodes all the scripts of the jailTools project into itself and adds
# means to '--show' and '--run' them.
# this is an internal script, please don't use it
case "$(readlink -f /proc/$$/exe)" in
*busybox)
sh="$(readlink -f /proc/$$/exe) sh"
echo "using shell : $sh"
;;
*)
sh="$(readlink -f /proc/$$/exe)"
echo "using shell : $sh"
;;
esac
ownPath=$(dirname $0)
# convert the path of this script to an absolute path
if [ "$ownPath" = "." ]; then
ownPath=$PWD
else
if echo $ownPath | grep -q '^\/'; then
# absolute path, we do nothing
:
else
# relative path
ownPath=$PWD/$ownPath
fi
fi
toEmbed() {
embedTable=$($bb sed -e "s/^/scripts\//" << EOF
config.sh jt_config
readElf.sh jt_readElf
cpDep.sh jt_cpDep
jailUpgrade.sh jt_upgrade
newJail.sh jt_new
utils.sh jt_utils
jailLib.template.sh jt_jailLib_template
startRoot.template.sh jt_startRoot_template
filesystem.template.sh jt_filesystem_template
firewall.template.sh jt_firewall
rootCustomConfig.template.sh jt_rootCustomConfig_template
rootDefaultConfig.template.sh jt_rootDefaultConfig_template
EOF
)
filesToEmbed=$(printf "%s" "$embedTable" | $bb cut -d " " -f 1)
sed -e 's/^@EOF$/EOF/' << EOF
$(cat $ownPath/scripts/jailtools.template.sh | sed -ne "s/\(JT_VERSION=\).*$/\1\"$(sh getVersion.sh)\"/" -e '/^@EMBEDDEDFILES_LOCATION@/ q; /^@EMBEDDEDFILES_LOCATION@/ ! p')
embedTable=\$(\$bb cat << EOF
$embedTable
@EOF
)
embeddedFiles=\$(\$bb cat << EOF
$(cd $ownPath; $bb tar -jcf - $filesToEmbed | $bb base64)
@EOF
)
existsFile() {
local file=\$1
local oldIFS=\$IFS
IFS="
"
for st in \$embedTable; do
IFS=" "
set -- \$st
if [ "\$file" = "\$2" ]; then
IFS=\$oldIFS
return 0
fi
done
IFS=\$oldIFS
return 1
}
runFile() {
local file=\$1
local path=""
shift
local oldIFS=\$IFS
IFS="
"
for st in \$embedTable; do
local one=\${st%% *}
local two=\${st##* }
if [ "\$file" = "\$two" ]; then
path=\$one
break
fi
done
IFS=\$oldIFS
if [ "\$path" = "" ]; then
return 1
fi
echo "\$embeddedFiles" | \$bb base64 -d | \$bb tar -jxOf - \$path | \$bb env IS_RUNNING=1 \$bb sh -s -- "\$@"
return \$?
}
showFile() {
local file=\$1
local path=""
local oldIFS=\$IFS
IFS="
"
for st in \$embedTable; do
IFS=" "
set -- \$st
if [ "\$file" = "\$2" ]; then
path=\$1
break
fi
done
IFS=\$oldIFS
if [ "\$path" = "" ]; then
return 1
fi
echo "\$embeddedFiles" | \$bb base64 -d | \$bb tar -jxOf - \$path
return 0
}
case \$cmd in
"--show")
file="\$1"
path=""
if ! existsFile \$file; then
echo "No such file." >&2
exit 1
fi
showFile \$file
exit 0
;;
--run)
file="\$1"
path=""
shift
if ! existsFile \$file; then
echo "No such file." >&2
exit 1
fi
runFile "\$file" "\$@"
exit \$?
;;
esac
$(cat $ownPath/scripts/utils.sh)
$(cat $ownPath/scripts/jailtools.template.sh | sed -ne '/^@EMBEDDEDFILES_LOCATION@/ { s/.*// ; :e ; $ { p; q }; N; be }')
EOF
}
if [ "$1" != "" ] && [ "$2" != "" ]; then
toEmbed | $bb sed -e "s%@SCRIPT_PATH@%.%" > $1/$2
exit 0
else
echo "This is an internal only script, please don't use it."
exit 1
fi