freedombone-rmuser 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Removes a user from the system
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. PROJECT_NAME='freedombone'
  29. export TEXTDOMAIN=${PROJECT_NAME}-rmuser
  30. export TEXTDOMAINDIR="/usr/share/locale"
  31. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  32. UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
  33. for f in $UTILS_FILES
  34. do
  35. source "$f"
  36. done
  37. APP_FILES="/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*"
  38. for f in $APP_FILES
  39. do
  40. source "$f"
  41. done
  42. read_config_param MY_USERNAME
  43. REMOVE_USERNAME=$1
  44. REMOVE_OPTIONS="$2"
  45. if [ ! "$REMOVE_USERNAME" ]; then
  46. echo $'Please specify a username to remove'
  47. exit 1
  48. fi
  49. if [[ "$REMOVE_USERNAME" == "$MY_USERNAME" ]]; then
  50. echo $'You cannot remove the administrator user'
  51. exit 2
  52. fi
  53. if [[ $(is_valid_user "$REMOVE_USERNAME") == "0" ]]; then
  54. echo $'Cannot remove reserved users'
  55. exit 3
  56. fi
  57. if [ ! -d "/home/$REMOVE_USERNAME" ]; then
  58. echo $"Home directory does not exist for $REMOVE_USERNAME"
  59. exit 4
  60. fi
  61. if [ ! -f "$COMPLETION_FILE" ]; then
  62. echo $"$COMPLETION_FILE not found"
  63. exit 5
  64. fi
  65. if ! grep -q "Admin user" "$COMPLETION_FILE"; then
  66. echo $"No admin user specified in $COMPLETION_FILE"
  67. exit 6
  68. fi
  69. ADMIN_USERNAME=$(get_completion_param "Admin user")
  70. if [ ! "$ADMIN_USERNAME" ]; then
  71. echo $"No admin username specified in $COMPLETION_FILE"
  72. exit 7
  73. fi
  74. if [[ "$REMOVE_USERNAME" == "$ADMIN_USERNAME" ]]; then
  75. echo $"The administrator user cannot be removed"
  76. exit 8
  77. fi
  78. if [[ "$REMOVE_OPTIONS" != '-f' && "$REMOVE_OPTIONS" != '-y' && "$REMOVE_OPTIONS" != '--force' ]]; then
  79. echo $'>>> REMOVE USER <<<'
  80. read -r -p $"Do you really wish to remove the user '$REMOVE_USERNAME' (y/n) ?" yn
  81. if [[ $yn != 'y' && $yn != 'Y' && $yn != 'yes' && $yn != 'Yes' && $yn != 'YES' ]]; then
  82. echo $"User $REMOVE_USERNAME was not removed"
  83. exit 9
  84. fi
  85. else
  86. echo $"Forced removal of user $REMOVE_USERNAME"
  87. fi
  88. if [ -f /etc/nginx/.htpasswd ]; then
  89. if grep -q "${REMOVE_USERNAME}:" /etc/nginx/.htpasswd; then
  90. htpasswd -D /etc/nginx/.htpasswd "$REMOVE_USERNAME"
  91. fi
  92. fi
  93. # remove gpg keys
  94. if [ -d "/home/$REMOVE_USERNAME/.gnupg" ]; then
  95. shred -zu "/home/$REMOVE_USERNAME/.gnupg/"*
  96. fi
  97. # remove ssh keys
  98. if [ -d "/home/$REMOVE_USERNAME/.ssh" ]; then
  99. shred -zu "/home/$REMOVE_USERNAME/.ssh/"*
  100. fi
  101. echo $'Detecting installed apps...'
  102. detect_apps
  103. get_apps_installed_names
  104. # shellcheck disable=SC2068
  105. for app_name in ${APPS_INSTALLED_NAMES[@]}
  106. do
  107. if [[ $(function_exists "remove_user_${app_name}") == "1" ]]; then
  108. echo $"Removing user from ${app_name}"
  109. app_load_variables "${app_name}"
  110. "remove_user_${app_name}" "$REMOVE_USERNAME"
  111. if grep -q "${app_name}_${REMOVE_USERNAME}" "$APP_USERS_FILE"; then
  112. sed -i "/${app_name}_${REMOVE_USERNAME}/d" "$APP_USERS_FILE"
  113. fi
  114. fi
  115. done
  116. chmod 600 /etc/shadow
  117. chmod 600 /etc/gshadow
  118. userdel -r "$REMOVE_USERNAME"
  119. groupdel "$REMOVE_USERNAME"
  120. chmod 0000 /etc/shadow
  121. chmod 0000 /etc/gshadow
  122. if [ -d "/home/$REMOVE_USERNAME" ]; then
  123. rm -rf "/home/${REMOVE_USERNAME:?}"
  124. fi
  125. echo $"User $REMOVE_USERNAME was removed"
  126. exit 0