freedombone-rmuser 3.3KB

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