view packaging/linux-installer.inc @ 1268:2efdf2faf4e5

Remove dead variable and fix possible null pointer dereference Problems found by clang static-analyzer
author Andre Heinecke <andre.heinecke@intevation.de>
date Fri, 26 Sep 2014 19:31:53 +0200
parents 469c1a04b678
children 5e0c338552b4
line wrap: on
line source
#!/bin/bash
# Um TrustBridge zu installieren:
# 1. Prüfen Sie, ob Sie dieser Datei genügend vertrauen, um ihr die
#    Kontrolle über diesen Rechner zu übergeben. Beispielsweise
#    durch Vergleich mit einer starken Prüfsumme aus einer zweiten,
#    unabhängigen Quelle.
# 2. Öffnen Sie eine Kommandozeile (Anwendung "Terminal").
# 3. Wechseln Sie in das Verzeichnis, in dem diese Datei gespeichert
#    ist (z.B. per Kommandozeile mit: "cd ~/Schreibtisch").
# 4. Starten Sie die Anwendung auf der Kommandozeile, beispielsweise
#    als Installation nur für den aktuellen Nutzer, indem Sie
#    eingeben:
#    	bash TrustBridge-1.0.0-amd64.sh
#    Tipp: Die Tab-Taste nach dem "Tr" ergänzt oft den ganzen Namen.
#
# Übrigens, wir konnten kein übliches .deb-Paket verwenden, da
# TrustBridge Ihnen auch die Installation als reiner Nutzer ohne
# Admin-Rechte ermöglichen soll.
#
#
# To install TrustBridge:
# 1. Verify that you trust this specific file far enough, that you
#    are willing to hand over the control of your computer to it. For
#    example compare a strong checksum of the file to one from a
#    second, independent source.
# 2. Open a command line (application "Terminal").
# 3. Change your working directory to where this file is stored.
#    (e.g. type "cd ~/Desktop" on your command line).
# 4. Start the installation on the command line, e.g. for the current
#    user only by typing something like:
#       bash TrustBridge-1.0.0-amd64.sh
#    Hint: If you press the tab-key after "Tr" it may complete the
#    filename.
#
# By the way: We could not have used a .deb package, because the
# installation must also work without without adminstrator
# priviledges.
#
#
# Copyright (C) 2014
# by Bundesamt für Sicherheit in der Informationstechnik (BSI).
# Software engineering by Intevation GmbH.
#
# This file is Free Software under the GNU GPL (v>=2)
# and comes with ABSOLUTELY NO WARRANTY!
# For details and the license of TrustBridge
# see the folder "licenses" after installation
# check the corresponding revision of it online at
# https://wald.intevation.org/hg/trustbridge/file/tip/licenses

set -u

###INCLUDE:linux-installer-common.inc
###INCLUDE:linux-installer.l10n-de

###INCLUDE:linux-installer-uninstall.inc

usage()
{
  getxt "Usage: %s [OPTION]...\n" "$ME"
  getxt "Install TrustBridge.\n\n"
  getxt "Options:\n"
  getxt "  -p, --prefix=PATH  install files in PATH\n"
  getxt "  -f, --force        install to given prefix, even when a current\n"
  getxt "                     installation with different prefix exists.\n"
  getxt "  -u, --uninstall    uninstall files from current installation\n"
  getxt "  -s, --system       create a system wide (de)installation\n"
  getxt "      --help         display this help and exit\n"
  getxt "      --version      output version information and exit\n"
  exit $1
}

parse_args()
{
  OPTS=`getopt \
      -l uninstall,update,force,help,prefix:,system,version \
      -o d,f,p:,s -n "$ME" -- "$@"`
  [ $? -eq 0 ] || usage 23

  eval set -- "$OPTS"

  while true ; do
    case "$1" in
      --prefix|-p)
        instcfg[PREFIX]="$2"
        shift 2
        ;;
      --system|-s)
        SYSINST=1
        shift 1
        ;;
      --force|-f)
        FORCE=1
        shift 1
        ;;
      --uninstall|-u)
        DEINSTALL=1
        shift 1
        ;;
      --update)
# Update is an internal option that is used when the application calls
# the installer to install an update.
        UPDATE=1
        shift 1
        ;;
      --help)
        usage 0
        ;;
      --version)
        version
        ;;
      --)
        shift
        break
        ;;
    esac
  done
}

finished()
{
  echo
  echo "################################################################################"
  if [ $SYSINST -eq 1 ]; then
    getxt "System wide installation successful.\n"
    UN_RUNCMD="sudo "
  else
    getxt "Single user installation successful.\n"
    UN_RUNCMD=""
  fi
  getxt "TrustBridge has been installed to: '%s'\n\n" "${instcfg[PREFIX]}"

  getxt "To remove the application and the root certificates it has inserted,\n"
  getxt "call the uninstall command:\n"
  echo "  $UN_RUNCMD ${instcfg[PREFIX]}/bin/trustbridge-uninstall.sh"

  echo "################################################################################"
  if ! [ $(id -u) -eq 0 ]; then
    getxt "Press enter to launch '%s'\n" "${instcfg[PREFIX]}/bin/trustbridge"
    getxt "or press Control-C to quit the installer.\n"
    read
    "${instcfg[PREFIX]}/bin/trustbridge" &
  else
    getxt "You can now launch '%s'\n" "${instcfg[PREFIX]}/bin/trustbridge"
  fi
}

cleanup()
{
  getxt "Cleaning up temporary stuff ...\n"

  # remove temporary directories:
  local -a temp_dirs
  if [ "${lock_dir:-}" ]; then
      # $lock_dir is generate by the shar
      temp_dirs+=("${instcfg[PREFIX]}/$lock_dir")
  fi
  if [ "${TMPEXTRACT_DIR:-}" ]; then
      temp_dirs+=("$TMPEXTRACT_DIR")
  fi

  if [ "${lock_dir:-}" ]; then
      temp_dirs+=("$extra_bin_path")
  fi

  for dir in "${temp_dirs[@]-}" ; do
    [ -d "$dir" ] &&
        rm -rf "$dir"
  done
}

write_instcfg()
{
  install -d `dirname "$instcfg_file"`
  echo "# Created by TrustBridge-Installer, don't touch!" >"$instcfg_file"
  for key in "${!instcfg[@]}" ; do
    echo "${key}=${instcfg[$key]}" >>"$instcfg_file"
  done
}

write_autostart()
{
  cat > "$1" << EOF
[Desktop Entry]
Type=Application
Name=TrustBridge
Exec="${instcfg[PREFIX]}/bin/trustbridge" --tray
EOF
  chown "${SUDO_USER:-${USER}}" "$1"
  chmod 700 "$1"
}

write_startmenu()
{
  cat > "$1" << EOF
[Desktop Entry]
Type=Application
Name=TrustBridge
Comment=Install and update trusted root certificates
Comment[de]=Vertrauenswürdige Wurzelzertifikate installieren und aktualisieren
Exec=${instcfg[PREFIX]}/bin/trustbridge
Icon=${instcfg[PREFIX]}/share/pixmaps/trustbridge/trustbridge.png
Terminal=false
Categories=Network;Qt;
StartupNotify=false
EOF
}

setup_startmenu()
{
  # Supported desktop environments: Unity, GNOME, XFCE, LXDE, KDE
  # System wide installation with a nonstandard XDG_DATA_HOME is not
  # respected with regards to autostart.
  if [ ! -d "${startmenu_path}" ]; then
    install -d "${startmenu_path}" || \
      fatal "Failed to create startmenu directory: '%s'\n" "$startmenu_path"
  fi

  write_startmenu "${startmenu_path}/trustbridge.desktop"
  update-desktop-database 2>&1 || true
}

setup_autostart()
{
  # Supported desktop environments: Unity, GNOME, XFCE, LXDE, KDE
  # System wide installation with a nonstandard XDG_CONFIG_HOME or KDEHOME is not
  # respected with regards to autostart.
  if [ ! -d "${autostart_path}" ]; then
    install -d "${autostart_path}" || \
      fatal "Failed to create autostart directory: '%s'\n" "$autostart_path"
  fi

  write_autostart "${autostart_path}/trustbridge.desktop"
}

provide_uudecode_maybe()
{
  # The shar needs uudecode, which might not be installed.  If its not
  # available we will provide our own python based implementation.
  if which uudecode >/dev/null 2>&1 ; then
      getxt "Found system uudecode.\n"
  else
    local myuudecode="$extra_bin_path/uudecode"
    cat >"$myuudecode" <<EOF
#!/usr/bin/python2
import os
import sys
import uu
os.path.chmod = os.chmod
def rm_if_exists(file):
    try:
        os.remove(file)
    except OSError:
        pass
os.path.exists = rm_if_exists
if len(sys.argv) > 1:
    f = open(sys.argv[1], 'r')
else:
    f = sys.stdin
uu.decode(f, None, None, 1)
EOF
    chmod 755 "$myuudecode"
    PATH="${extra_bin_path}:$PATH"
    getxt "Using python uudecode provided by installer.\n"
  fi
}

#======================================================================
# main()
trap cleanup EXIT

parse_args "$@"
check_priv
init_vars
read_oldinstcfg

cat <<EOF
------------------------------------------------------------------------

   TrustBridge - Installer
   Version ${instcfg[VERSION]} - ${ARCH} (Testversion)

------------------------------------------------------------------------
EOF

if [ "$ARCH" == "x86_64" -a "$ARCH" != "$HOST_ARCH" ]; then
    getxt "It appears your system architecture is %s.\n" "$HOST_ARCH"
    getxt "This installer is for 64 bit systems.\n"
    getxt "Really install TrustBridge for '%s' systems? [y/n]\n" "${ARCH}"
    yorn || exit 0
fi

if [ "$ARCH" == "i386" ]; then
    if [[ "$HOST_ARCH" != *86 ]]; then
        getxt "It appears your system architecture is %s.\n" "$HOST_ARCH"
        getxt "This installer is for 32 bit systems.\n"
        getxt "Really install TrustBridge for '%s' systems? [y/n]\n" "${ARCH}"
        yorn || exit 0
    fi
fi

if [ $DEINSTALL -eq 1 ] ; then
    deinstall
    # Stop after deinstallation:
    exit 0
fi

if [ -z "${instcfg[PREFIX]}" ] ; then
  instcfg[PREFIX]="${inst_default_prefix}"
else
  # Prefix was given on invocation:
  if [ "${oldinstcfg[PREFIX]}" -a \
       "${instcfg[PREFIX]}" != "${oldinstcfg[PREFIX]}" -a \
       $FORCE -ne 1 ] ; then
      fatal "Prefix differs from current installation (%s).  Aborting!\n" "${oldinstcfg[PREFIX]}"
  fi
fi

getxt "Installing to '%s':\n" "${instcfg[PREFIX]}"

if [ ! -d "${instcfg[PREFIX]}" ] ; then
    getxt "creating installation directory ...\n"
    install -d "${instcfg[PREFIX]}" || fatal "Could not create '%s'!\n" "${instcfg[PREFIX]}"
fi

getxt "checking for uudecode ...\n"
provide_uudecode_maybe

getxt "unpacking files ...\n"
OLDWD="$PWD"

if [ $UPDATE -eq 1 ]; then
    # Trustbridge might be running. Install into temporary dir
    # and move the directory into the installation prefix afterwards
    TMPEXTRACT_DIR=$(mktemp -d)
    cd "$TMPEXTRACT_DIR"
else
    cd "${instcfg[PREFIX]}"
fi

set +u
set -- '-c'
# ----------------------------------------------------------------------
# regular shar archive inserted here:
###SHAR###
# ----------------------------------------------------------------------

if [ $UPDATE -eq 1 ]; then
    cp -fr "$TMPEXTRACT_DIR/bin" "${instcfg[PREFIX]}"
    cp -fr "$TMPEXTRACT_DIR/share" "${instcfg[PREFIX]}"
fi

cd "$OLDWD"

getxt "Preparing trustbridge-tray-starter ...\n"
sed -i "/^PREFIX=/c\PREFIX='${instcfg[PREFIX]}'" \
    "${instcfg[PREFIX]}/bin/trustbridge-tray-starter.sh"

getxt "Setting up cronjob ...\n"
setup_cronjob

getxt "Setting up autostart ...\n"
setup_autostart

getxt "Setting up start menu entries ...\n"
setup_startmenu

getxt "Writing installation configuration to: %s ...\n" "$instcfg_file"
write_instcfg

finished

# cleanup
# is called implicitly at exit via trap...
exit 0

http://wald.intevation.org/projects/trustbridge/