Skip to content

Instantly share code, notes, and snippets.

@yurikoles
Created July 21, 2017 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurikoles/29718438e288c4a2042328f68c71ea91 to your computer and use it in GitHub Desktop.
Save yurikoles/29718438e288c4a2042328f68c71ea91 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Copyright (C) 2015 by PrivatBank <acsk@privatbank.ua>
# All rights reserved.
#
# Don't edit this file!
log_file="$HOME/.cryptoplugin/installer.log"
destdir="$HOME/.mozilla/plugins/"
lib_name="libnpcryptoplugin.so"
bin_name="nmcryptoplugin"
manifest_file="com.privatbank.cryptoplugin.json"
manifestdir="$HOME/.config/google-chrome/NativeMessagingHosts"
manifest_mozilla_dir="$HOME/.mozilla/native-messaging-hosts"
x32_dir=linux_32
x64_dir=linux_64
half_of_skip_regex="EOFEOFEOF"
log() {
echo $(date "+%Y-%m-%d %H.%M.%S ") $1 >> $log_file
}
error_exit() {
log " Error, $1"
exit $2
}
run_command() {
log " run: $1"
res=$(eval $1 2>&1)
if [ "${res}" != "" ]; then
log " Error run command: ${res}"
exit $2
fi
}
check_file() {
if [ ! -f $1 ]; then
echo Installer was damaged \($1 not found\). Exiting...
clean
exit 1
fi
}
help() {
echo 'Crypto Plugin installer.'
echo 'Copyright (C) 2015 by PrivatBank <acsk@privatbank.ua>.'
echo
echo 'Usage:'
echo
echo ' sh cryptoplugin.run ACTION'
echo
echo ' -i | --install Install user local if run by regular user.'
echo ' Default action.'
echo ' -u | --uninstall uninstall user local.'
echo ' -h | --help Print this help.'
}
clean() {
run_command "rm -rf $TMP_DIR"
}
install() {
echo "Install Crypto Plugin"
# Create log.
run_command "mkdir -p \"$HOME/.cryptoplugin\""
log "Install Crypto Plugin."
if [[ $0 == /* ]] ;
then
script_file="$0"
else
script_file="$PWD/$0"
fi
log "Script file: $script_file"
log " Create tmp dir"
TMP_DIR=$(mktemp -d)
echo 'Extracting'
log "Calculate skip line."
skip=$(sed "/$half_of_skip_regex$half_of_skip_regex/q" <"$script_file" | wc -l)
skip=$(expr $skip + 1)
log "Skip line: $skip"
log "Extracting"
run_command "tail -n +$skip \"$script_file\" | tar zxf - -C $TMP_DIR"
echo "Extracting complete"
host_cpu=$(uname -m)
log "Host cpu: $host_cpu"
case $host_cpu in
i[3456]86) dir_arch="$x32_dir" ;;
x86_64) dir_arch="$x64_dir" ;;
esac
lib="$TMP_DIR/$dir_arch/$lib_name"
nmbin="$TMP_DIR/$dir_arch/$bin_name"
log "Lib file: $lib"
log "Bin file: $nmbin"
check_file $lib
check_file $nmbin
echo "Check requires"
requires_libs=$(objdump -p $lib | grep NEEDED | sed -e 's/ *NEEDED *//')
log "Requires libs: $requires_libs"
requires_libs=$(echo $requires_libs | sed 's/ /:/g')
IFS=:
array=($requires_libs)
for i in "${!array[@]}"
do
if [ "$(/sbin/ldconfig -p | grep "${array[i]}")" == "" ]; then
absent_libs="${array[i]}, $absent"
fi
done
if [ "$absent_libs" != "" ]; then
log "Absent libs: $absent_libs"
echo "Requires libs ($absent_libs) not installed. Exiting..."
clean
exit 1
fi
echo "Check requires complete"
echo "Copy files"
log "mkdir -p $destdir"
mkdir -p $destdir
log "Copy libs"
run_command "command install -m 555 $lib $destdir"
run_command "command install -m 777 $nmbin $destdir"
run_command "mkdir -p $manifestdir"
run_command "mkdir -p $manifest_mozilla_dir"
log "Copy manifest"
run_command "mkdir $TMP_DIR/tmp"
run_command "mkdir $TMP_DIR/tmp/mozilla"
run_command "sed \"s|PLUGIN_PATH|$destdir$bin_name|\" $TMP_DIR/$manifest_file > $TMP_DIR/tmp/$manifest_file"
run_command "command install -m 555 $TMP_DIR/tmp/$manifest_file $manifestdir"
run_command "sed \"s|PLUGIN_PATH|$destdir$bin_name|\" $TMP_DIR/mozilla/$manifest_file > $TMP_DIR/tmp/mozilla/$manifest_file"
run_command "command install -m 555 $TMP_DIR/tmp/mozilla/$manifest_file $manifest_mozilla_dir"
find "$HOME/.mozilla/firefox/" -name "*.default" | while read mozilla_profile_path; do
run_command "mkdir -p $mozilla_profile_path/extensions"
run_command "cp $TMP_DIR/mozilla/cryptoplugin_ext_id@privatbank.ua.xpi $mozilla_profile_path/extensions"
done
echo "Copy files complete"
clean
pid_nm=$(pidof $bin_name)
log "Find pid $pid_nm"
if [ "$pid_nm" != "" ]; then
run_command "kill $pid_nm"
fi
echo 'Local user installing complete.'
}
remove() {
echo "Uninstall Crypto Plugin."
log "Uninstall Crypto Plugin."
run_command "rm -f $destdir/$lib_name"
run_command "rm -f $destdir/$bin_name"
run_command "rm -f $manifestdir/$manifest_file"
run_command "rm -f $manifest_mozilla_dir/$manifest_file"
find "$HOME/.mozilla/firefox/" -name "*.default" | while read mozilla_profile_path; do
run_command "rm -f $mozilla_profile_path/extensions/cryptoplugin_ext_id@privatbank.ua.xpi"
done
echo 'Local user uninstalling complete.'
}
[ -z "$1" ] && set -- --install
case $1 in
-h|--help) help; exit 0 ;;
-i|--install) install; exit 0 ;;
-u|--uninstall) remove; exit 0 ;;
*) help; exit 0 ;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment