freedombone/src/freedombone-rmuser

151 lines
4.0 KiB
Plaintext
Raw Normal View History

#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2016-02-12 20:43:08 +01:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2016-02-12 20:43:08 +01:00
#
# Removes a user from the system
2018-04-08 14:30:21 +02:00
#
2016-02-12 20:43:08 +01:00
# License
# =======
#
2018-02-21 20:32:13 +01:00
# Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
2016-02-12 20:43:08 +01:00
#
# This program is free software: you can redistribute it and/or modify
2016-02-13 23:09:27 +01:00
# it under the terms of the GNU Affero General Public License as published by
2016-02-12 20:43:08 +01:00
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
2016-02-13 23:09:27 +01:00
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
2016-02-12 20:43:08 +01:00
#
2016-02-13 23:09:27 +01:00
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-11-27 12:42:16 +01:00
PROJECT_NAME='freedombone'
2015-11-27 17:52:23 +01:00
export TEXTDOMAIN=${PROJECT_NAME}-rmuser
2015-11-27 12:42:16 +01:00
export TEXTDOMAINDIR="/usr/share/locale"
2015-12-08 17:22:48 +01:00
COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
2018-03-02 20:17:02 +01:00
UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
2016-10-02 12:17:01 +02:00
for f in $UTILS_FILES
do
2018-03-02 20:17:02 +01:00
source "$f"
2016-10-02 12:17:01 +02:00
done
2018-03-02 20:17:02 +01:00
APP_FILES="/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*"
2016-10-02 12:17:01 +02:00
for f in $APP_FILES
do
2018-03-02 20:17:02 +01:00
source "$f"
2016-10-02 12:17:01 +02:00
done
2016-10-29 20:18:24 +02:00
read_config_param MY_USERNAME
2016-10-29 20:07:41 +02:00
2016-10-29 20:18:24 +02:00
REMOVE_USERNAME=$1
2016-11-20 11:43:43 +01:00
REMOVE_OPTIONS="$2"
2016-10-29 20:18:24 +02:00
2018-03-02 20:17:02 +01:00
if [ ! "$REMOVE_USERNAME" ]; then
2016-05-07 17:52:58 +02:00
echo $'Please specify a username to remove'
exit 1
fi
2016-10-29 20:18:24 +02:00
if [[ "$REMOVE_USERNAME" == "$MY_USERNAME" ]]; then
echo $'You cannot remove the administrator user'
2016-05-07 17:52:58 +02:00
exit 2
fi
2016-10-29 20:18:24 +02:00
if [[ $(is_valid_user "$REMOVE_USERNAME") == "0" ]]; then
echo $'Cannot remove reserved users'
2016-05-07 17:52:58 +02:00
exit 3
fi
2018-03-02 20:17:02 +01:00
if [ ! -d "/home/$REMOVE_USERNAME" ]; then
2016-10-29 20:18:24 +02:00
echo $"Home directory does not exist for $REMOVE_USERNAME"
exit 4
fi
2018-03-02 20:17:02 +01:00
if [ ! -f "$COMPLETION_FILE" ]; then
2016-05-07 17:52:58 +02:00
echo $"$COMPLETION_FILE not found"
2016-10-29 20:18:24 +02:00
exit 5
fi
2018-03-02 20:17:02 +01:00
if ! grep -q "Admin user" "$COMPLETION_FILE"; then
2016-05-07 17:52:58 +02:00
echo $"No admin user specified in $COMPLETION_FILE"
2016-10-29 20:18:24 +02:00
exit 6
fi
2016-10-16 20:50:56 +02:00
ADMIN_USERNAME=$(get_completion_param "Admin user")
2018-03-02 20:17:02 +01:00
if [ ! "$ADMIN_USERNAME" ]; then
2016-05-07 17:52:58 +02:00
echo $"No admin username specified in $COMPLETION_FILE"
2016-10-29 20:18:24 +02:00
exit 7
fi
2018-03-02 20:17:02 +01:00
if [[ "$REMOVE_USERNAME" == "$ADMIN_USERNAME" ]]; then
2016-05-07 17:52:58 +02:00
echo $"The administrator user cannot be removed"
2016-10-29 20:18:24 +02:00
exit 8
fi
2016-11-20 11:43:43 +01:00
if [[ "$REMOVE_OPTIONS" != '-f' && "$REMOVE_OPTIONS" != '-y' && "$REMOVE_OPTIONS" != '--force' ]]; then
echo $'>>> REMOVE USER <<<'
2018-03-02 20:17:02 +01:00
read -r -p $"Do you really wish to remove the user '$REMOVE_USERNAME' (y/n) ?" yn
2016-11-20 11:43:43 +01:00
if [[ $yn != 'y' && $yn != 'Y' && $yn != 'yes' && $yn != 'Yes' && $yn != 'YES' ]]; then
echo $"User $REMOVE_USERNAME was not removed"
exit 9
fi
else
echo $"Forced removal of user $REMOVE_USERNAME"
fi
2016-03-05 19:43:01 +01:00
if [ -f /etc/nginx/.htpasswd ]; then
2017-06-28 14:52:14 +02:00
if grep -q "${REMOVE_USERNAME}:" /etc/nginx/.htpasswd; then
2018-03-02 20:17:02 +01:00
htpasswd -D /etc/nginx/.htpasswd "$REMOVE_USERNAME"
2016-05-07 17:52:58 +02:00
fi
2016-03-05 19:43:01 +01:00
fi
2016-05-07 17:50:00 +02:00
# remove gpg keys
2018-03-02 20:17:02 +01:00
if [ -d "/home/$REMOVE_USERNAME/.gnupg" ]; then
rm "/home/$REMOVE_USERNAME/.gnupg/"*
2016-05-07 17:50:00 +02:00
fi
# remove ssh keys
2018-03-02 20:17:02 +01:00
if [ -d "/home/$REMOVE_USERNAME/.ssh" ]; then
rm "/home/$REMOVE_USERNAME/.ssh/"*
2016-05-07 17:52:58 +02:00
fi
2016-10-02 12:17:01 +02:00
echo $'Detecting installed apps...'
detect_apps
get_apps_installed_names
2018-03-08 15:38:15 +01:00
# shellcheck disable=SC2068
for app_name in ${APPS_INSTALLED_NAMES[@]}
2016-10-02 12:17:01 +02:00
do
2018-03-02 20:17:02 +01:00
if [[ $(function_exists "remove_user_${app_name}") == "1" ]]; then
2016-10-02 12:17:01 +02:00
echo $"Removing user from ${app_name}"
2018-03-02 20:17:02 +01:00
app_load_variables "${app_name}"
"remove_user_${app_name}" "$REMOVE_USERNAME"
if grep -q "${app_name}_${REMOVE_USERNAME}" "$APP_USERS_FILE"; then
sed -i "/${app_name}_${REMOVE_USERNAME}/d" "$APP_USERS_FILE"
fi
2016-05-07 17:52:58 +02:00
fi
2016-10-02 12:17:01 +02:00
done
2016-03-25 17:06:57 +01:00
chmod 600 /etc/shadow
chmod 600 /etc/gshadow
2018-03-02 20:17:02 +01:00
userdel -r "$REMOVE_USERNAME"
groupdel "$REMOVE_USERNAME"
chmod 0000 /etc/shadow
chmod 0000 /etc/gshadow
2018-03-02 20:17:02 +01:00
if [ -d "/home/$REMOVE_USERNAME" ]; then
rm -rf "/home/${REMOVE_USERNAME:?}"
fi
2016-10-29 20:18:24 +02:00
echo $"User $REMOVE_USERNAME was removed"
exit 0