v-delete-user-ssh-key 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. # info: add ssh key
  3. # options: USER KEY
  4. # labels: hestia
  5. #
  6. # example: v-delete-user-ssh-key user unique_id
  7. #
  8. # Delete user ssh key from authorized_keys
  9. #----------------------------------------------------------#
  10. # Variable&Function #
  11. #----------------------------------------------------------#
  12. # Argument definition
  13. user=$1
  14. keyid=$2
  15. # Includes
  16. # shellcheck source=/usr/local/hestia/func/main.sh
  17. source $HESTIA/func/main.sh
  18. # shellcheck source=/usr/local/hestia/conf/hestia.conf
  19. source $HESTIA/conf/hestia.conf
  20. # Additional argument formatting
  21. #----------------------------------------------------------#
  22. # Verifications #
  23. #----------------------------------------------------------#
  24. check_args '2' "$#" 'USER KEYID'
  25. is_format_valid 'user'
  26. is_object_valid 'user' 'USER' "$user"
  27. source $USER_DATA/user.conf
  28. AUTHKEY_FILE="$HOMEDIR/$user/.ssh/authorized_keys"
  29. [ -z "$(readlink -f "$AUTHKEY_FILE" | egrep "^$HOMEDIR/$user/.ssh/")" ] && check_result $E_FORBIDEN "Invalid authorized keys path"
  30. if [ ! -f "$AUTHKEY_FILE" ]; then
  31. exit
  32. fi
  33. # Perform verification if read-only mode is enabled
  34. check_hestia_demo_mode
  35. #----------------------------------------------------------#
  36. # Action #
  37. #----------------------------------------------------------#
  38. sed -i "/${keyid}/d" "$AUTHKEY_FILE"
  39. #----------------------------------------------------------#
  40. # Hestia #
  41. #----------------------------------------------------------#
  42. # Logging
  43. log_history "Deleted ssh-key $user"
  44. log_event "$OK" "$ARGUMENTS"
  45. exit