2022-04-05 04:46:48 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2022-05-03 03:09:41 +08:00
|
|
|
SCRIPT_NAME="$(basename "${APPIMAGE}")"
|
2022-04-08 19:53:18 +08:00
|
|
|
SYSTEMD_UNIT_FILENAME="infection-monkey.service"
|
2022-05-05 21:53:31 +08:00
|
|
|
TMP_SYSTEMD_UNIT="${PWD}/${SYSTEMD_UNIT_FILENAME}"
|
2022-04-05 04:46:48 +08:00
|
|
|
SYSTEMD_DIR="/lib/systemd/system"
|
|
|
|
MONKEY_BIN="/opt/infection-monkey/bin"
|
2022-04-11 02:57:13 +08:00
|
|
|
APPIMAGE_NAME="InfectionMonkey.AppImage"
|
2022-04-05 04:46:48 +08:00
|
|
|
|
2022-05-05 22:43:22 +08:00
|
|
|
die() {
|
|
|
|
echo "$1" >&2
|
|
|
|
echo ""
|
|
|
|
echo_help
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2022-04-05 04:46:48 +08:00
|
|
|
echo_help() {
|
2022-04-10 21:58:50 +08:00
|
|
|
echo "Installs the Infection Monkey service to run on boot."
|
2022-04-05 04:46:48 +08:00
|
|
|
echo ""
|
2022-04-08 19:53:18 +08:00
|
|
|
echo "Usage:"
|
2022-05-05 21:31:29 +08:00
|
|
|
echo " ${SCRIPT_NAME} service --install --user <USERNAME>"
|
2022-05-03 03:09:41 +08:00
|
|
|
echo " ${SCRIPT_NAME} service --uninstall"
|
|
|
|
echo " ${SCRIPT_NAME} service -h|--help"
|
2022-04-08 19:53:18 +08:00
|
|
|
echo ""
|
|
|
|
echo "Options:"
|
2022-05-05 21:31:29 +08:00
|
|
|
echo " --install Install the Infection Monkey service"
|
|
|
|
echo " --user Configure the Infection Monkey service to run as a specific user"
|
2022-05-03 03:09:41 +08:00
|
|
|
echo " --uninstall Uninstall Infection Monkey service"
|
2022-04-05 04:46:48 +08:00
|
|
|
}
|
|
|
|
|
2022-04-08 19:53:18 +08:00
|
|
|
install_service() {
|
2022-05-05 22:02:22 +08:00
|
|
|
copy_appimage
|
2022-05-05 21:59:45 +08:00
|
|
|
install_systemd_unit "$1"
|
2022-04-08 19:53:18 +08:00
|
|
|
|
2022-05-05 22:05:45 +08:00
|
|
|
echo "The Infection Monkey service has been installed and will start on boot."
|
|
|
|
echo "Run 'systemctl start infection-monkey' to start the service now."
|
2022-05-05 21:59:45 +08:00
|
|
|
}
|
|
|
|
|
2022-05-05 22:02:22 +08:00
|
|
|
copy_appimage() {
|
2022-05-05 22:01:53 +08:00
|
|
|
sudo mkdir --mode=0755 -p "${MONKEY_BIN}"
|
|
|
|
|
|
|
|
if [ "${APPIMAGE}" != "${MONKEY_BIN}/${APPIMAGE_NAME}" ] ; then
|
|
|
|
umask 022
|
|
|
|
sudo cp "${APPIMAGE}" "${MONKEY_BIN}/${APPIMAGE_NAME}"
|
|
|
|
sudo chmod 755 "${MONKEY_BIN}/${APPIMAGE_NAME}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-05-05 21:59:45 +08:00
|
|
|
install_systemd_unit() {
|
2022-05-05 21:53:31 +08:00
|
|
|
umask 077
|
|
|
|
cat > "${TMP_SYSTEMD_UNIT}" << EOF
|
2022-04-05 04:46:48 +08:00
|
|
|
[Unit]
|
2022-04-08 19:53:18 +08:00
|
|
|
Description=Infection Monkey Runner
|
2022-04-05 04:46:48 +08:00
|
|
|
After=network.target
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
User=$1
|
|
|
|
Type=simple
|
|
|
|
ExecStart="${MONKEY_BIN}/${APPIMAGE_NAME}"
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
EOF
|
|
|
|
|
2022-05-05 21:53:31 +08:00
|
|
|
sudo mv "${TMP_SYSTEMD_UNIT}" "${SYSTEMD_DIR}/${SYSTEMD_UNIT_FILENAME}"
|
2022-04-05 04:46:48 +08:00
|
|
|
sudo systemctl enable "${SYSTEMD_UNIT_FILENAME}" &>/dev/null
|
2022-04-08 19:53:18 +08:00
|
|
|
}
|
2022-04-05 04:46:48 +08:00
|
|
|
|
2022-04-08 19:53:18 +08:00
|
|
|
uninstall_service() {
|
2022-04-05 04:46:48 +08:00
|
|
|
if [ -f "${MONKEY_BIN}/${APPIMAGE_NAME}" ] ; then
|
|
|
|
sudo rm -f "${MONKEY_BIN}/${APPIMAGE_NAME}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -f "${SYSTEMD_DIR}/${SYSTEMD_UNIT_FILENAME}" ] ; then
|
|
|
|
sudo systemctl stop "${SYSTEMD_UNIT_FILENAME}" 2>/dev/null
|
|
|
|
sudo systemctl disable "${SYSTEMD_UNIT_FILENAME}" &>/dev/null
|
|
|
|
sudo rm "${SYSTEMD_DIR}/${SYSTEMD_UNIT_FILENAME}"
|
|
|
|
sudo systemctl daemon-reload
|
|
|
|
fi
|
|
|
|
|
2022-04-08 19:53:18 +08:00
|
|
|
echo "The Infection Monkey service has been uninstalled"
|
|
|
|
}
|
|
|
|
|
2022-05-05 21:42:16 +08:00
|
|
|
exit_if_user_doesnt_exist() {
|
|
|
|
if ! user_exists "$1" ; then
|
2022-05-05 22:43:22 +08:00
|
|
|
die "Error: User '$1' does not exist."
|
2022-05-05 21:42:16 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-05-05 22:44:07 +08:00
|
|
|
user_exists() {
|
|
|
|
id -u "$1" &>/dev/null
|
|
|
|
}
|
|
|
|
|
2022-04-05 04:46:48 +08:00
|
|
|
has_sudo() {
|
|
|
|
# 0 true, 1 false
|
|
|
|
sudo -nv > /dev/null 2>&1
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
exit_if_missing_argument() {
|
|
|
|
if [ -z "$2" ] || [ "${2:0:1}" == "-" ]; then
|
2022-05-05 22:43:22 +08:00
|
|
|
die "Error: Argument for parameter '$1' is missing."
|
2022-04-05 04:46:48 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-04-08 19:53:18 +08:00
|
|
|
do_uninstall=false
|
2022-05-05 21:29:46 +08:00
|
|
|
do_install=false
|
2022-05-05 21:21:19 +08:00
|
|
|
username=""
|
2022-04-05 04:46:48 +08:00
|
|
|
|
|
|
|
while (( "$#" )); do
|
|
|
|
case "$1" in
|
|
|
|
--user)
|
|
|
|
exit_if_missing_argument "$1" "$2"
|
2022-05-05 21:42:16 +08:00
|
|
|
exit_if_user_doesnt_exist "$2"
|
2022-05-05 21:21:19 +08:00
|
|
|
username=$2
|
2022-04-05 04:46:48 +08:00
|
|
|
shift 2
|
|
|
|
;;
|
2022-05-03 03:09:41 +08:00
|
|
|
--install)
|
2022-05-05 21:29:46 +08:00
|
|
|
do_install=true
|
2022-05-03 03:09:41 +08:00
|
|
|
shift
|
2022-04-05 04:46:48 +08:00
|
|
|
;;
|
|
|
|
--uninstall)
|
2022-04-08 19:53:18 +08:00
|
|
|
do_uninstall=true
|
|
|
|
shift
|
2022-04-05 04:46:48 +08:00
|
|
|
;;
|
|
|
|
-h|--help)
|
|
|
|
echo_help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
2022-05-05 22:43:22 +08:00
|
|
|
die "Error: Unsupported parameter $1."
|
2022-04-05 04:46:48 +08:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if ! has_sudo; then
|
2022-05-05 22:43:22 +08:00
|
|
|
die "Error: You need root permissions for some of this script operations. \
|
2022-04-05 04:46:48 +08:00
|
|
|
Run \`sudo -v\`, enter your password, and then re-run this script."
|
|
|
|
fi
|
|
|
|
|
2022-05-03 03:09:41 +08:00
|
|
|
if [ -z "${APPIMAGE}" ] ; then
|
2022-05-05 22:43:22 +08:00
|
|
|
die "Error: Missing 'APPIMAGE' environment variable. Try installing the Infection Monkey service through the AppImage"
|
2022-05-03 03:09:41 +08:00
|
|
|
fi
|
|
|
|
|
2022-05-05 21:29:46 +08:00
|
|
|
if $do_install && $do_uninstall ; then
|
2022-05-05 22:43:22 +08:00
|
|
|
die "Error: The --install and --uninstall flags are mutually exclusive."
|
2022-05-05 21:29:46 +08:00
|
|
|
fi
|
|
|
|
|
2022-04-08 19:53:18 +08:00
|
|
|
if $do_uninstall ; then
|
|
|
|
uninstall_service
|
|
|
|
exit 0
|
2022-04-05 04:46:48 +08:00
|
|
|
fi
|
|
|
|
|
2022-05-05 21:31:29 +08:00
|
|
|
if $do_install ; then
|
2022-05-05 22:14:04 +08:00
|
|
|
if [ -z "$username" ] ; then
|
2022-05-05 22:43:22 +08:00
|
|
|
die "Error: You must supply a username."
|
2022-05-05 22:14:04 +08:00
|
|
|
fi
|
|
|
|
|
2022-05-05 21:31:29 +08:00
|
|
|
install_service "$username"
|
|
|
|
exit 0
|
2022-04-05 04:46:48 +08:00
|
|
|
fi
|
|
|
|
|
2022-05-05 22:43:22 +08:00
|
|
|
die "Error:You must specify either the --install or --uninstall flag."
|