v-add-letsencrypt-user 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/bin/bash
  2. # info: register letsencrypt user account
  3. # options: USER
  4. #
  5. # The function creates and register LetsEncript account
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. user=$1
  11. # LE API
  12. API='https://acme-v02.api.letsencrypt.org'
  13. # Includes
  14. source $VESTA/func/main.sh
  15. source $VESTA/conf/vesta.conf
  16. # encode base64
  17. encode_base64() {
  18. cat |base64 |tr '+/' '-_' |tr -d '\r\n='
  19. }
  20. # Let's Encrypt v2 curl function
  21. query_le_v2() {
  22. protected='{"nonce": "'$3'",'
  23. protected=''$protected' "url": "'$1'",'
  24. protected=''$protected' "alg": "RS256", "jwk": '$jwk'}'
  25. content="Content-Type: application/jose+json"
  26. payload_=$(echo -n "$2" |encode_base64)
  27. protected_=$(echo -n "$protected" |encode_base64)
  28. signature_=$(printf "%s" "$protected_.$payload_" |\
  29. openssl dgst -sha256 -binary -sign $USER_DATA/ssl/user.key |\
  30. encode_base64)
  31. post_data='{"protected":"'"$protected_"'",'
  32. post_data=$post_data'"payload":"'"$payload_"'",'
  33. post_data=$post_data'"signature":"'"$signature_"'"}'
  34. curl -s -i -d "$post_data" "$1" -H "$content"
  35. }
  36. #----------------------------------------------------------#
  37. # Verifications #
  38. #----------------------------------------------------------#
  39. check_args '1' "$#" 'USER'
  40. is_format_valid 'user'
  41. is_object_valid 'user' 'USER' "$user"
  42. if [ -e "$USER_DATA/ssl/le.conf" ]; then
  43. source "$USER_DATA/ssl/le.conf"
  44. fi
  45. if [ ! -z "$KID" ]; then
  46. exit
  47. fi
  48. #----------------------------------------------------------#
  49. # Action #
  50. #----------------------------------------------------------#
  51. # Defining user email
  52. if [[ -z "$EMAIL" ]]; then
  53. EMAIL=$(get_user_value '$CONTACT')
  54. fi
  55. # Defining user agreement
  56. agreement=''
  57. # Generating user key
  58. KEY="$USER_DATA/ssl/user.key"
  59. if [ ! -e "$KEY" ]; then
  60. openssl genrsa -out $KEY 4096 >/dev/null 2>&1
  61. chmod 600 $KEY
  62. fi
  63. # Defining key exponent
  64. if [ -z "$EXPONENT" ]; then
  65. EXPONENT=$(openssl pkey -inform pem -in "$KEY" -noout -text_pub |\
  66. grep Exponent: |cut -f 2 -d '(' |cut -f 1 -d ')' |sed -e 's/x//' |\
  67. xxd -r -p |encode_base64)
  68. fi
  69. # Defining key modulus
  70. if [ -z "$MODULUS" ]; then
  71. MODULUS=$(openssl rsa -in "$KEY" -modulus -noout |\
  72. sed -e 's/^Modulus=//' |xxd -r -p |encode_base64)
  73. fi
  74. # Defining JWK
  75. jwk='{"e":"'$EXPONENT'","kty":"RSA","n":"'"$MODULUS"'"}'
  76. # Defining key thumbnail
  77. if [ -z "$THUMB" ]; then
  78. THUMB="$(echo -n "$jwk" |openssl dgst -sha256 -binary |encode_base64)"
  79. fi
  80. # Requesting ACME nonce
  81. nonce=$(curl -s -I "$API/directory" |grep -i nonce |cut -f2 -d\ |tr -d '\r\n')
  82. # Creating ACME account
  83. url="$API/acme/new-acct"
  84. payload='{"termsOfServiceAgreed": true}'
  85. answer=$(query_le_v2 "$url" "$payload" "$nonce")
  86. kid=$(echo "$answer" |grep -i location: |cut -f2 -d ' '|tr -d '\r')
  87. # Checking answer status
  88. status=$(echo "$answer" |grep HTTP/ |tail -n1 |cut -f2 -d ' ')
  89. if [[ "${status:0:2}" -ne "20" ]]; then
  90. check_result $E_CONNECT "Let's Encrypt acc registration failed $status"
  91. fi
  92. #----------------------------------------------------------#
  93. # Vesta #
  94. #----------------------------------------------------------#
  95. # Adding le.conf
  96. if [ ! -e "$USER_DATA/ssl/le.conf" ]; then
  97. echo "EXPONENT='$EXPONENT'" > $USER_DATA/ssl/le.conf
  98. echo "MODULUS='$MODULUS'" >> $USER_DATA/ssl/le.conf
  99. echo "THUMB='$THUMB'" >> $USER_DATA/ssl/le.conf
  100. echo "EMAIL='$EMAIL'" >> $USER_DATA/ssl/le.conf
  101. echo "KID='$kid'" >> $USER_DATA/ssl/le.conf
  102. chmod 660 $USER_DATA/ssl/le.conf
  103. else
  104. sed -i '/^KID=/d' $USER_DATA/ssl/le.conf
  105. echo "KID='$kid'" >> $USER_DATA/ssl/le.conf
  106. fi
  107. # Logging
  108. log_event "$OK" "$ARGUMENTS"
  109. exit