prepare script passes shellcheck

master
Alessandro Mauri 4 years ago
parent d80a695d06
commit 35d955683e
  1. 129
      posix/prepare

@ -3,11 +3,13 @@
# NEEDED: lz4 readelf tar cat awk cut sed sh sort # NEEDED: lz4 readelf tar cat awk cut sed sh sort
WORKDIR="$(basename "$1")" WORKDIR="$(basename "$1")"
SYS_LIBDIR='/lib' if [ -z "$LIBPATH" ]; then
LIBPATH='/lib'
fi
PRELOAD_SCRIPT='preload' PRELOAD_SCRIPT='preload'
LIBLIST=$WORKDIR/liblist LIBLIST="$WORKDIR"/liblist
SUMFILE=$WORKDIR/checksum SUMFILE="$WORKDIR"/checksum
tmpfile=$WORKDIR/tt tmpfile="$WORKDIR"/tt
die () die ()
{ {
@ -17,67 +19,71 @@ die ()
getlibs () getlibs ()
{ {
tmplist=$WORKDIR/tmplist tmplist="$WORKDIR"/tmplist
touch $LIBLIST touch "$LIBLIST"
# Take the executable and get the first level dependencies # Take the executable and get the first level dependencies
readelf -d "$1" | readelf -d "$1" |
grep NEEDED | grep NEEDED |
awk '{print $5}' | awk '{print $5}' |
sed s/\\[/\ / | sed s/\\]/\ / > $tmplist sed s/\\[/\ / | sed s/\\]/\ / > "$tmplist"
# While there are more dependencies # While there are more dependencies
while [ "$(cat $tmplist)" ]; do while [ "$(cat "$tmplist")" ]; do
# Add them to the master file # Add them to the master file
cat $LIBLIST $tmplist > $tmpfile cat "$LIBLIST" "$tmplist" > "$tmpfile"
mv -f $tmpfile $LIBLIST mv -f "$tmpfile" "$LIBLIST"
# Copy them to lib/ # Copy them to lib/
while read l; do while read -r l; do
find -L $SYS_LIBDIR -maxdepth 2 -name "$l" -exec cp -n {} $WORKDIR/lib/ \; ( IFS=:
done < $tmplist for p in $LIBPATH; do
find -L "$p" -maxdepth 2 -name "$l" -exec cp -n {} "$WORKDIR"/lib/ \;
done
)
done < "$tmplist"
# Extract their dependencies # Extract their dependencies
touch $tmpfile touch "$tmpfile"
for f in $WORKDIR/lib/*; do for f in "$WORKDIR"/lib/*; do
if [ "$(grep -F "$(basename "$f")" "$tmplist")" ]; then if grep -Fq "$(basename "$f")" "$tmplist"; then
readelf -d "$f" | readelf -d "$f" |
grep NEEDED | grep NEEDED |
awk '{print $5}' | awk '{print $5}' |
sed s/\\[/\ / | sed s/\\]/\ / >> $tmpfile sed s/\\[/\ / | sed s/\\]/\ / >> "$tmpfile"
fi fi
done done
sort -u $tmpfile > $tmplist sort -u "$tmpfile" > "$tmplist"
done done
sort -u $LIBLIST > $tmpfile sort -u "$LIBLIST" > "$tmpfile"
mv -f $tmpfile $LIBLIST mv -f "$tmpfile" "$LIBLIST"
rm -f $tmplist rm -f "$tmplist"
} }
if ! [ $WORKDIR ]; then if ! [ "$WORKDIR" ]; then
die 'Not enough arguments' die 'Not enough arguments'
fi fi
if ! test -d $WORKDIR ; then if ! [ -d "$WORKDIR" ]; then
die 'Argument is not a directory' die 'Argument is not a directory'
fi fi
if test -e $WORKDIR/bin; then if [ -e "$WORKDIR"/bin ]; then
if ! test -d $WORKDIR/bin; then if ! [ -d "$WORKDIR"/bin ]; then
die 'bin/ is not a directory' die 'bin/ is not a directory'
fi fi
else else
die 'Directory does not contain bin/' die 'Directory does not contain bin/'
fi fi
if ! test -e $WORKDIR/id; then if ! [ -e "$WORKDIR"/id ]; then
die 'id file not present' die 'id file not present'
fi fi
if ls -1 $WORKDIR/bin; then if ls -1 "$WORKDIR"/bin; then
for f in $WORKDIR/bin/*; do for f in "$WORKDIR"/bin/*; do
if ! test -x $f; then if ! [ -x "$f" ]; then
die "$f is not executable" die "$f is not executable"
fi fi
done done
@ -86,60 +92,65 @@ else
fi fi
# Create necessary directories and files # Create necessary directories and files
mkdir -p $WORKDIR/lib mkdir -p "$WORKDIR"/lib
mkdir -p $WORKDIR/man mkdir -p "$WORKDIR"/ext
mkdir -p $WORKDIR/ext mkdir -p "$WORKDIR"/man
touch $WORKDIR/env touch "$WORKDIR"/env
# TODO: add usage and error checking # TODO: add usage and error checking
DIRNAME="$WORKDIR"/"$(head -1 $WORKDIR/id | awk '{print $1}')" DIRNAME="$WORKDIR"/"$(head -1 "$WORKDIR"/id | awk '{print $1}')"
if test -s $LIBLIST; then echo "Fetching binary dependencies..."
rm -f $LIBLIST if [ -s "$WORKDIR"/deps ]; then
while read -r d; do
( IFS=:
for p in $PATH; do
find -L "$p" -maxdepth 2 -name "$d" -exec cp -n {} "$WORKDIR"/bin/ \;
done
)
done < "$tmplist"
fi fi
echo "Fetching dependencies and stripping binaries..." echo "Fetching dependencies and stripping binaries..."
# TODO: get al binary dependencies from deps file # TODO: get al binary dependencies from deps file
# Get all dependencies and strip them # Get all dependencies and strip them
for b in $WORKDIR/bin/*; do for b in "$WORKDIR"/bin/*; do
getlibs $b getlibs "$b"
strip $b strip "$b"
done done
echo "Stripping libraries..." echo "Stripping libraries..."
for l in $WORKDIR/lib/*; do for l in "$WORKDIR"/lib/*; do
strip $l strip "$l"
done done
echo "Compressing destination directory..." echo "Compressing destination directory..."
if test -d $DIRNAME; then if [ -d "$DIRNAME" ]; then
rm -rf $DIRNAME rm -rf "$DIRNAME"
fi fi
mkdir -p $DIRNAME mkdir -p "$DIRNAME"
cp -r $WORKDIR/bin $DIRNAME/ cp -r "$WORKDIR"/bin "$DIRNAME"/
cp -r $WORKDIR/lib $DIRNAME/ cp -r "$WORKDIR"/lib "$DIRNAME"/
cp -r $WORKDIR/man $DIRNAME/ cp -r "$WORKDIR"/man "$DIRNAME"/
cp -r $WORKDIR/ext $DIRNAME/ cp -r "$WORKDIR"/ext "$DIRNAME"/
cp $WORKDIR/env $DIRNAME/ cp "$WORKDIR"/env "$DIRNAME"/
cp $WORKDIR/id $DIRNAME/ cp "$WORKDIR"/id "$DIRNAME"/
tar -c -f $DIRNAME.tar -C $WORKDIR "$(basename $DIRNAME)" tar -c -f "$DIRNAME".tar -C "$WORKDIR" "$(basename "$DIRNAME")"
md5sum -b $DIRNAME.tar | cut -d " " -f1 > $SUMFILE md5sum -b "$DIRNAME".tar | cut -d " " -f1 > "$SUMFILE"
lz4 --rm -9 -c $DIRNAME.tar > "$DIRNAME".tar.lz4 lz4 --rm -9 -c "$DIRNAME.tar" > "$DIRNAME".tar.lz4
echo "Injecting payload..." echo "Injecting payload..."
cat "$PRELOAD_SCRIPT" | sed s/SUM/"$(cat "$SUMFILE")"/ "$PRELOAD_SCRIPT" |
sed s/SUM/"$(cat $SUMFILE)"/ |
sed s/ID/"$(basename "$DIRNAME")"/ | sed s/ID/"$(basename "$DIRNAME")"/ |
awk '!/^ *#/ && NF' > tpp awk '!/^ *#/ && NF' > tpp
size="$(wc -c tpp | cut -d " " -f1)" size="$(wc -c tpp | cut -d " " -f1)"
strsize="$(echo "BYTES" | wc -c)" strsize="$(echo "BYTES" | wc -c)"
numsize="$(echo "$size" | wc -c)" numsize="$(echo "$size" | wc -c)"
normsize="$(expr "$size" - "$(expr "$strsize" - "$numsize")")" normsize="$((size - (strsize - numsize)))"
cat tpp | sed s/BYTES/"$normsize"/ tpp > tpr
sed s/BYTES/"$normsize"/ > tpr
cat tpr "$DIRNAME".tar.lz4 > "$DIRNAME".ti cat tpr "$DIRNAME".tar.lz4 > "$DIRNAME".ti
echo "Cleaning up..." echo "Cleaning up..."

Loading…
Cancel
Save