freedombone-rmuser 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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@freedombone.net>
  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. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  33. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  34. for f in $UTILS_FILES
  35. do
  36. source $f
  37. done
  38. APP_FILES=/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-*
  39. for f in $APP_FILES
  40. do
  41. source $f
  42. done
  43. read_config_param MY_USERNAME
  44. REMOVE_USERNAME=$1
  45. REMOVE_OPTIONS="$2"
  46. if [ ! $REMOVE_USERNAME ]; then
  47. echo $'Please specify a username to remove'
  48. exit 1
  49. fi
  50. if [[ "$REMOVE_USERNAME" == "$MY_USERNAME" ]]; then
  51. echo $'You cannot remove the administrator user'
  52. exit 2
  53. fi
  54. if [[ $(is_valid_user "$REMOVE_USERNAME") == "0" ]]; then
  55. echo $'Cannot remove reserved users'
  56. exit 3
  57. fi
  58. if [ ! -d /home/$REMOVE_USERNAME ]; then
  59. echo $"Home directory does not exist for $REMOVE_USERNAME"
  60. exit 4
  61. fi
  62. if [ ! -f $COMPLETION_FILE ]; then
  63. echo $"$COMPLETION_FILE not found"
  64. exit 5
  65. fi
  66. if ! grep -q "Admin user" $COMPLETION_FILE; then
  67. echo $"No admin user specified in $COMPLETION_FILE"
  68. exit 6
  69. fi
  70. ADMIN_USERNAME=$(get_completion_param "Admin user")
  71. if [ ! $ADMIN_USERNAME ]; then
  72. echo $"No admin username specified in $COMPLETION_FILE"
  73. exit 7
  74. fi
  75. if [[ $REMOVE_USERNAME == $ADMIN_USERNAME ]]; then
  76. echo $"The administrator user cannot be removed"
  77. exit 8
  78. fi
  79. if [[ "$REMOVE_OPTIONS" != '-f' && "$REMOVE_OPTIONS" != '-y' && "$REMOVE_OPTIONS" != '--force' ]]; then
  80. echo $'>>> REMOVE USER <<<'
  81. read -p $"Do you really wish to remove the user '$REMOVE_USERNAME' (y/n) ?" yn
  82. if [[ $yn != 'y' && $yn != 'Y' && $yn != 'yes' && $yn != 'Yes' && $yn != 'YES' ]]; then
  83. echo $"User $REMOVE_USERNAME was not removed"
  84. exit 9
  85. fi
  86. else
  87. echo $"Forced removal of user $REMOVE_USERNAME"
  88. fi
  89. if [ -f /etc/nginx/.htpasswd ]; then
  90. if grep "${REMOVE_USERNAME}:" /etc/nginx/.htpasswd; then
  91. htpasswd -D /etc/nginx/.htpasswd $REMOVE_USERNAME
  92. fi
  93. fi
  94. # remove gpg keys
  95. if [ -d /home/$REMOVE_USERNAME/.gnupg ]; then
  96. shred -zu /home/$REMOVE_USERNAME/.gnupg/*
  97. fi
  98. # remove ssh keys
  99. if [ -d /home/$REMOVE_USERNAME/.ssh ]; then
  100. shred -zu /home/$REMOVE_USERNAME/.ssh/*
  101. fi
  102. echo $'Detecting installed apps...'
  103. detect_apps
  104. get_apps_installed_names
  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