ban_client.sh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/bin/bash
  2. set -o errexit
  3. set -o nounset
  4. set -o pipefail
  5. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  6. #SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
  7. source "$SCRIPT_DIR/functions.sh"
  8. show_usage() {
  9. echo "Usage: $0 <ccd_file> <ban|unban>"
  10. echo "Example: $0 /etc/openvpn/server/server/ccd/login ban"
  11. exit 1
  12. }
  13. main() {
  14. # Check permissions
  15. check_permissions
  16. # Process arguments
  17. [[ $# -lt 2 ]] && show_usage
  18. local ccd_file=$1
  19. local action=$2
  20. # Validate CCD file path
  21. check_ccd_path "$ccd_file"
  22. local username
  23. username=$(basename "${ccd_file}")
  24. touch "${ccd_file}"
  25. chmod 660 "${ccd_file}"
  26. chown ${owner_user}:${owner_group} "${ccd_file}"
  27. local is_banned=""
  28. if grep -q "^disable$" "$ccd_file"; then
  29. is_banned="disable"
  30. fi
  31. case "$action" in
  32. ban)
  33. if [[ -z "$is_banned" ]]; then
  34. log "Ban user: ${username}"
  35. sed -i '1i\disable' "${ccd_file}"
  36. log "User ${username} banned successfully"
  37. else
  38. log "User ${username} is already banned"
  39. fi
  40. ;;
  41. unban)
  42. if [[ -n "$is_banned" ]]; then
  43. log "Unban user: ${username}"
  44. sed -i '/^disable$/d' "${ccd_file}"
  45. log "User ${username} unbanned successfully"
  46. else
  47. log "User ${username} is not banned"
  48. fi
  49. ;;
  50. *)
  51. log "Error: Invalid action. Use 'ban' or 'unban'"
  52. show_usage
  53. ;;
  54. esac
  55. exit 0
  56. }
  57. main "$@"