v-add-letsencrypt-user 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/bash
  2. # info: register letsencrypt user account
  3. # options: USER [EMAIL]
  4. #
  5. # The function creates and register LetsEncript account key
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. user=$1
  11. email=$2
  12. key_size=4096
  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. #----------------------------------------------------------#
  21. # Verifications #
  22. #----------------------------------------------------------#
  23. check_args '1' "$#" 'USER [EMAIL]'
  24. is_format_valid 'user'
  25. is_object_valid 'user' 'USER' "$user"
  26. if [ -e "$USER_DATA/ssl/le.conf" ]; then
  27. exit
  28. fi
  29. #----------------------------------------------------------#
  30. # Action #
  31. #----------------------------------------------------------#
  32. api='https://acme-v01.api.letsencrypt.org'
  33. if [ -z "$email" ]; then
  34. email=$(get_user_value '$CONTACT')
  35. fi
  36. agreement=$(curl -s -I "$api/terms" |grep Location |cut -f 2 -d \ |tr -d '\r\n')
  37. # Generating key
  38. key="$USER_DATA/ssl/user.key"
  39. if [ ! -e "$key" ]; then
  40. openssl genrsa -out $key $key_size >/dev/null 2>&1
  41. chmod 600 $key
  42. fi
  43. # Defining key exponent
  44. exponent=$(openssl pkey -inform perm -in "$key" -noout -text_pub |\
  45. grep Exponent: |cut -f 2 -d '(' |cut -f 1 -d ')' |sed -e 's/x//' |\
  46. xxd -r -p |encode_base64)
  47. # Defining key modulus
  48. modulus=$(openssl rsa -in "$key" -modulus -noout |\
  49. sed -e 's/^Modulus=//' |xxd -r -p |encode_base64)
  50. # Defining key thumb
  51. thumb='{"e":"'$exponent'","kty":"RSA","n":"'"$modulus"'"}'
  52. thumb="$(echo -n "$thumb" |openssl dgst -sha256 -binary |encode_base64)"
  53. # Defining JWK header
  54. header='{"e":"'$exponent'","kty":"RSA","n":"'"$modulus"'"}'
  55. header='{"alg":"RS256","jwk":'"$header"'}'
  56. # Requesting nonce
  57. nonce=$(curl -s -I "$api/directory" |grep Nonce |cut -f 2 -d \ |tr -d '\r\n')
  58. protected=$(echo -n '{"nonce":"'"$nonce"'"}' |encode_base64)
  59. # Defining registration query
  60. query='{"resource":"new-reg","contact":["mailto:'"$email"'"],'
  61. query=$query'"agreement":"'$agreement'"}'
  62. payload=$(echo -n "$query" |encode_base64)
  63. signature=$(printf "%s" "$protected.$payload" |\
  64. openssl dgst -sha256 -binary -sign "$key" |encode_base64)
  65. data='{"header":'"$header"',"protected":"'"$protected"'",'
  66. data=$data'"payload":"'"$payload"'","signature":"'"$signature"'"}'
  67. # Sending request to LetsEncrypt API
  68. answer=$(curl -s -i -d "$data" "$api/acme/new-reg")
  69. status=$(echo "$answer" |grep HTTP/1.1 |tail -n1 |cut -f2 -d ' ')
  70. # Checking http answer status
  71. if [[ "$status" -ne "201" ]] && [[ "$status" -ne "409" ]]; then
  72. check_result $E_CONNECT "LetsEncrypt account registration $status"
  73. fi
  74. #----------------------------------------------------------#
  75. # Vesta #
  76. #----------------------------------------------------------#
  77. # Adding le.conf
  78. echo "EMAIL='$email'" > $USER_DATA/ssl/le.conf
  79. echo "EXPONENT='$exponent'" >> $USER_DATA/ssl/le.conf
  80. echo "MODULUS='$modulus'" >> $USER_DATA/ssl/le.conf
  81. echo "THUMB='$thumb'" >> $USER_DATA/ssl/le.conf
  82. chmod 660 $USER_DATA/ssl/le.conf
  83. # Logging
  84. log_event "$OK" "$ARGUMENTS"
  85. exit