revoke_client.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/bash
  2. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  3. #SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
  4. source "$SCRIPT_DIR/functions.sh"
  5. if [ "$#" -ne 3 ]; then
  6. log "Usage: $0 <service_name> <rsa_dir> <username>"
  7. exit 1
  8. fi
  9. check_permissions
  10. SRV_NAME="${1}"
  11. RSA_DIR="${2}"
  12. USERNAME="${3}"
  13. log "Starting certificate revocation for $USERNAME by user $ORIGINAL_USER"
  14. # Check that the RSA directory exists
  15. if [ ! -d "$RSA_DIR" ]; then
  16. log "Error: RSA directory not found: $RSA_DIR"
  17. exit 1
  18. fi
  19. # Change to the RSA directory
  20. cd "$RSA_DIR" || exit 1
  21. # Check that easyrsa exists
  22. if [ ! -f "./easyrsa" ]; then
  23. log "Error: easyrsa not found in $RSA_DIR"
  24. exit 1
  25. fi
  26. # Check that the certificate exists
  27. if [ ! -f "./pki/issued/${USERNAME}.crt" ]; then
  28. log "Error: Certificate for user $USERNAME not found"
  29. exit 1
  30. fi
  31. # Check whether the certificate is already revoked
  32. if grep -q "/CN=${USERNAME}" ./pki/index.txt | grep -q "R"; then
  33. log "Error: Certificate for $USERNAME is already revoked"
  34. exit 1
  35. fi
  36. # Revoke the certificate
  37. log "Revoking certificate for user: $USERNAME"
  38. ./easyrsa --batch revoke "$USERNAME"
  39. if [ $? -eq 0 ]; then
  40. log "Successfully revoked certificate for $USERNAME"
  41. # Generate CRL (Certificate Revocation List)
  42. log "Generating CRL..."
  43. ./easyrsa --batch gen-crl
  44. if [ $? -eq 0 ]; then
  45. log "CRL generated successfully"
  46. chown ${owner_user}:${owner_group} -R "$RSA_DIR/pki/issued/"
  47. chown ${owner_user}:${owner_group} "$RSA_DIR/pki/crl.pem"
  48. chmod 660 "${RSA_DIR}/pki/issued/"*.crt
  49. # Restart the service
  50. log "Restarting service: $SRV_NAME"
  51. systemctl restart "${SRV_NAME}"
  52. if [ $? -eq 0 ]; then
  53. log "Service $SRV_NAME restarted successfully"
  54. log "Certificate revocation completed for $USERNAME"
  55. exit 0
  56. else
  57. log "Error: Failed to restart service $SRV_NAME"
  58. exit 1
  59. fi
  60. else
  61. log "Error: Failed to generate CRL"
  62. exit 1
  63. fi
  64. else
  65. log "Error: Failed to revoke certificate for $USERNAME"
  66. exit 1
  67. fi
  68. exit 0