/
bundle
executable file
·335 lines (311 loc) · 12.5 KB
/
bundle
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/bin/bash
# my version of the `bundle' shell script (similar to GNU shar).
# create a self-extracting shell archive that can install any file(s).
# Usage: bundle [OPTIONS] [FILES] > install_whatever.sh
ver="george's bundle 0.9.4"
b="$(basename $0)"
presdirs=1
encode=base64
PASTETO="${PASTETO-http://paste.chakra-project.org/}"
crypt=0
main() {
getargs "$@"
check
bundle
}
getargs() {
while [[ "$1" != "" ]]; do
case "$1" in
"-u"|"--unzipwith") shift; uzipwith="$1";;
"-f"|"--filesonly"|"--basename") presdirs=0;;
"-h"|"--help") usage;;
"-v"|"--version") echo "$ver"; exit 0;;
"-n"|"--norecurse") recurse=0;;
"-s"|"--sha"|"-s1"|"--sha1") hashwith=sha1sum;;
"--sha"*) alg=$(echo "$1" | sed 's/--sha//g')
hashwith=$(echo "sha$alg sum" | sed 's/\s//g');;
"-s"*) alg=$(echo "$1" | sed 's/-s//g')
hashwith=$(echo "sha$alg sum" | sed 's/\s//g');;
"-c"|"--crypt") crypt=1; cryptwith=${CRYPT_WITH-gpg -c}; ucryptwith=${UCRYPT_WITH-gpg -d};;
"-z"|"--gz"|"--targz"|"--gzip") uzipwith="tar -xzf" zipwith="tar -czf";;
"-j"|"--bz"|"--tarbz"|"--bzip") uzipwith="tar -xjf" zipwith="tar -cjf";;
"-x"|"-J"|"--xz"|"--tarxz"|"--xzip") uzipwith="tar -xJf" xz=1 zipwith="tar -cf";;
"-i"|"--info") shift; info "$1";;
"-l"|"-t"|"--list") shift; list "$1";;
"-e"|"--uuencode") encode=uuencode;;
"-d"|"--delete") rmfiles=1;;
"-p"|"--paste") paste=1;;
"-o"|"--output") tofile=1; shift; saveas="$1";
[[ -e "$saveas" ]] && echo "$b: output file '$saveas' already exists, not overwriting." && exit 1;;
"-"*) echo "Unrecognized option '$1'"; echo "Use '$b --help' for more information"; exit 1;;
*) files="$(printf "$files\n$1")"
esac
shift
done
}
usage() {
echo "Usage: $b [OPTIONS] [FILES] > [SHELL_ARCHIVE]"
echo
echo "Compression: "
echo " -u, --uzipwith <prog> automatically unzip compressed files, with 'prog [OPT]'"
echo " The script will attempt to unzip all of its files, so"
echo " do not use this feature with any uncompressed files "
echo " -j, --tarbz compress files first with \`tar -cjf', then bundle"
echo " -x, --tarxz compress files first with \`tar -cf', then \`xz -ez9',"
echo " then bundle. This achieves maximum compression, but "
echo " takes longer. Files are extracted with \`tar -xJf'"
echo " -z, --targz compress files first with \`tar -czf', then bundle"
echo
echo "Integrity checking:"
echo " -sX, --shaX use shaXsum for file verification instead of $hashwith"
echo " The sha* option prefers shaXsum, but can use "
echo " \`shasum -a X' as well. Example usage: --sha512, -s256"
echo " All of -s, -s1, --sha, and --sha1 will use sha1."
echo
echo "Redirect output:"
echo " -p, --paste instead of sending to stdout, send to $PASTETO"
echo " -o, --output <file> instead of sending to stdout, save to <file>"
echo
echo "Other:"
echo " -b, --makebat * create Windoze batch files, instead of bash scripts"
echo " -c, --crypt encrypt files with gpg (symmetric only)"
echo " works only with single files, or with compression options"
echo " -d, --delete delete original files"
echo " -e, --uuencode use uuencode instead of base64 to create archives"
echo " -f, --filesonly don't preserve directory structure, just bundle files"
echo " cannot be used with compression options"
echo " -i, --info <bundle> * show info on files in a bundle (use '-' for stdin)"
echo " -l, --list <bundle> list files in a bundle, accepts '-' or file as arg"
echo " -n, --norecurse don't recurse subdirectories "
echo " cannot be used with other options"
echo " -h, --help print this help and exit"
echo " -v, --version print version information and exit"
echo
echo "Note: Options with '*' are not yet implemented."
echo "Options CANNOT be specified like '-rzs', they must be like '-r -z -s'"
echo "The default function (no options) is simplest, and best for using when you"
echo "don't know what programs the person executing the bundle has installed."
exit 0
}
# since this uses grep, it supports - as stdin, so you can use eg.
# `bundle file | bundle -l -' to test the script.
list() {
if [[ -r "$1" ]]; then
echo "Files in $1:" >&2
echo >&2
grep 'file=' "$1" | sed "s/.*file=//; s/;\sfi//; s/'//g; s/\"//g"
exit 0
fi
}
info() {
if [[ -r "$1" ]]; then
echo "Files in $1:" >&2
echo >&2
grep 'file=' "$1" | sed "s/.*file=//; s/;\sfi//; s/'//g; s/\"//g"
exit 0
fi
}
crypt() {
echo "$b: Encrypting file..." >&2
eval $cryptwith "$1"
filesarr=("$1.gpg")
IFS=' '
ucryptdo="echo decrypting \$file with \'$ucryptwith\'; owd=\"\$(pwd)\"; cd \"\$dir\"; $ucryptwith \"./\$file\" > \"./\${file/.gpg/}\"; echo removing \$file ...; rm \"./\$file\"; cd \"\$owd\""
IFS="$newline"
[[ $rmfiles == 1 ]] && rm "$1"
}
check() {
encode=${encode-base64}
if [[ ! -x "$(which $encode)" ]]; then
echo "Sorry, no $encode executable was found." >&2
echo "We need this in order to continue." >&2
exit 1
fi
hashwith=${hashwith-md5sum}
if [[ ! -x "$(which $hashwith)" ]]; then
if [[ ! -x "$(which shasum)" ]]; then
echo "$hashwith cannot be found, quitting." >&2
exit 1
elif [[ -n $alg ]]; then
if [[ $alg == 1 || $alg == 224 || $alg == 256 || $alg == 384 || $alg == 512 || $alg == 512224 || $alg == 512256 ]]; then
echo "$b: $hashwith cannot be found, using 'shasum -a $alg' instead" >&2
echo "$b: This may cause problems; be warned!" >&2
hashwith="shasum -a $alg"
else
echo "$b: Invalid algorithm $alg, quitting in disgust." >&2
exit 1
fi
else
echo "$b: unknown error, $alg empty!" >&2
echo "$b: is the md5sum executable missing?" >&2
exit 1
fi
fi
}
print() {
if [[ $paste == 1 ]]; then
echo "$@" >> "/tmp/pbundle-${BASHPID}${UID}.sh"
elif [[ $tofile == 1 ]]; then
[[ -e "$saveas" ]] || touch "$saveas" 2>/dev/null
if [[ -w "$saveas" ]]; then
echo "$@" >> "$saveas"
else
echo "$b: Output file '$saveas' is not writable." >&2
exit 1
fi
else
echo "$@"
fi
}
paste() {
echo "$b: Pasting bundle to $PASTETO..." >&2
curl -sS --data-urlencode "paste_data=$(cat "$1")" \
--data-urlencode "paste_lang=bash" \
--data-urlencode "api_submit=true" \
--data-urlencode "mode=json" \
--data-urlencode "paste_user=$ver" \
--data-urlencode "paste_expire=8640" \
"${PASTETO}" | grep '"id"' | sed "s,.*\".*\":\s*\"\(.*\)\".*,$b: $PASTETO\1,"
}
header() {
print '#!/bin/bash'
print "# $ver self-extracting archive."
print '# Usage: "sh thisscript", or "sh thisscript /install/to/this/dir"'
print "if ! test -x \"\`which $encode\`\"; then"
print " echo 'Sorry, no $encode executable was found.' >&2"
print " echo 'We need this in order to continue.' >&2"
print " exit 1"
print "fi"
print 'dir=${1-.}'
print 'if [[ -d "$dir" ]]; then'
print ' echo "directory $dir exists, starting extraction..."'
print 'elif [[ -r "$dir" ]]; then'
print ' echo "$dir is a file, but needs to be a folder."'
print ' exit 1'
print 'else'
print ' echo "creating directory $dir ..."'
print ' install -dm755 "$dir"'
print 'fi'
if [[ -n $uzipwith ]]; then
echo "$b: this script will extract files with '$uzipwith'" >&2
echo "$b: note that the person running the bundled script will need this program" >&2
print "# This script will use '$uzipwith' to extract files. You must have this program installed."
fi
if [[ $crypt == 1 ]]; then
echo "$b: this script will decrypt files with '$ucryptwith'" >&2
echo "$b: note that the person running the bundled script will need this program" >&2
print "# This script will use '$ucryptwith' to decrypt files. You must have this program installed."
fi
print "integerr=0"
}
bundle() {
if [[ -n $files ]]; then
if [[ -n $zipwith ]]; then
echo "$b: Compressing files first with '$zipwith'... " >&2
now=$(date +%s)
tempar="/tmp/bundle-${BASHPID-0}.$now"
$zipwith $tempar $files
if [[ $xz == 1 ]]; then
echo "$b: Compressing files with 'xz -ez9'... " >&2
xz -ez9 $tempar
tempar=$tempar.xz
fi
files="$tempar"
rmfiles=1
presdirs=0
recurse=0
fi
newline=$'\n'
IFS="$newline"
if [[ $recurse != 0 ]]; then
filesarr=($(find $files))
else
filesarr=($files)
fi
if [[ $crypt == 1 ]]; then
if [[ ${#filesarr[@]} != 1 ]]; then
echo "$b: Sorry, encryption only works on single files." >&2
echo "$b: Perhaps try again with a compression method enabled?" >&2
exit 1
fi
if [[ -f $filesarr ]]; then
crypt "$filesarr"
else
echo "$b: That doesn't seem to be a file." >&2
echo "$b: Cannot encrypt directories." >&2
exit 1
fi
fi
echo "$b: Creating bundle..." >&2
header
else
echo "$b: No files specified, nothing to do." >&2
exit 1
fi
for file in "${filesarr[@]}"; do
if [[ -z $uzipwith ]]; then
uzipdo="# "
else
IFS=' '
uzipdo="file=\"\${file/.gpg/}\"; echo unzipping \$file with \'$uzipwith\'; owd=\"\$(pwd)\"; cd \"\$dir\"; $uzipwith \"./\$file\"; echo removing \$file ...; rm \"./\$file\"; cd \"\$owd\""
IFS="$newline"
fi
if [[ -d $file ]]; then
print "echo creating directory \$dir/$file"
print "mkdir -p \"\$dir/$file\""
continue
elif [[ -r $file ]]; then
fileperm="$(stat -c %a "$file")" fileowner="$(stat -c %u "$file")"
fileatime="$(stat -c %x "$file")" filemtime="$(stat -c %y "$file")"
IFS=' ' filesum=$($hashwith -b $file | awk '{printf $1}')
IFS="$newline" filedir="$(dirname $file | sed 's@^/@@')"
ofiledir="$(dirname $file)"
[[ $encode == "base64" ]] && filecont="$(base64 -w 100 "$file")"
[[ $encode == "uuencode" ]] && filecont="$(uuencode "$file" -)"
file="$(basename $file)"
else
echo "$b: $file does not exist or is not readable." >&2
fi
print "# START FILE -- \`$filedir/$file\`"
if [[ $presdirs == 1 ]]; then
print "if test -d \"\$dir/$filedir\"; then echo \"\$dir/$filedir\" exists ...; file='$filedir/$file'; elif test -r \"\$dir/$filedir\"; then echo \"\$dir/$filedir\" is a file, but needs to be a folder; exit 1; else echo creating \$dir/$filedir; mkdir -p \$dir/$filedir; file='$filedir/$file'; fi"
else
print "file='$file'"
fi
print "echo extracting \$dir/\$file ..."
[[ $encode == "base64" ]] && print 'base64 -d << EOF > "$dir/$file"'
[[ $encode == "uuencode" ]] && print 'uudecode << EOF > "$dir/$file"'
print "$filecont"
print "EOF"
print "echo setting permissions on \$dir/\$file to $fileperm ..."
print "chmod $fileperm \"\$dir/\$file\""
print "echo -n checking \$dir/\$file file integrity \($hashwith\) ...\ "
print "filesum=\$($hashwith -b \"\$dir/\$file\" | awk '{printf \$1}')"
print "if [ \"\$filesum\" == \"$filesum\" ]; then echo looks like \$dir/\$file is ok.; else echo looks like \$dir/\$file is corrupted.; let integerr=\$integerr+1; fi"
print "${ucryptdo-#}"
print "$uzipdo"
print "# END FILE -- \`$filedir/$file\`"
print ""
[[ $rmfiles == 1 ]] && rm "$ofiledir/$file"
done
print "echo "
print "echo Extraction complete."
print "echo File verification errors: \$integerr "
if [[ $paste == 1 ]]; then
pbundle="/tmp/pbundle-${BASHPID}${UID}.sh"
pbsize=$(stat -c %s "$pbundle")
if [[ $pbsize -ge 131062 ]]; then
echo "$b: That bundle is too large, declining to paste." >&2
rm -f "$pbundle"
exit 1
fi
if paste "$pbundle"; then
rm -f "$pbundle"
else
echo "$b: Pasting failed." >&2
rm -f "$pbundle"
exit 1
fi
fi
}
main "$@"