v-add-letsencrypt-domain 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/bin/bash
  2. # info: adding letsencrypt ssl cetificate for domain
  3. # options: USER DOMAIN [ALIASES] [RESTART] [NOTIFY]
  4. #
  5. # The function turns on SSL support for a domain. Parameter ssl_dir is a path
  6. # to directory where 2 or 3 ssl files can be found. Certificate file
  7. # domain.tld.crt and its key domain.tld.key are mandatory. Certificate
  8. # authority domain.tld.ca file is optional. If home directory parameter
  9. # (ssl_home) is not set, https domain uses public_shtml as separate
  10. # documentroot directory.
  11. #----------------------------------------------------------#
  12. # Variable&Function #
  13. #----------------------------------------------------------#
  14. # Argument definition
  15. user=$1
  16. domain=$2
  17. aliases=$3
  18. restart=$4
  19. notify=$5
  20. # Includes
  21. source $VESTA/func/main.sh
  22. source $VESTA/func/domain.sh
  23. source $VESTA/conf/vesta.conf
  24. #----------------------------------------------------------#
  25. # Verifications #
  26. #----------------------------------------------------------#
  27. check_args '2' "$#" 'USER DOMAIN [ALIASES] [RESTART] [NOTIFY]'
  28. is_format_valid 'user' 'domain'
  29. is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
  30. is_system_enabled "$WEB_SSL" 'SSL_SUPPORT'
  31. is_object_valid 'user' 'USER' "$user"
  32. is_object_unsuspended 'user' 'USER' "$user"
  33. is_object_valid 'web' 'DOMAIN' "$domain"
  34. is_object_unsuspended 'web' 'DOMAIN' "$domain"
  35. #----------------------------------------------------------#
  36. # Action #
  37. #----------------------------------------------------------#
  38. # Parsing domain data
  39. get_domain_values 'web'
  40. # Registering LetsEncrypt user account
  41. $BIN/v-add-letsencrypt-user $user
  42. if [ "$?" -ne 0 ]; then
  43. sed -i "/ $domain /d" $VESTA/data/queue/letsencrypt.pipe
  44. send_notice "LETSENCRYPT" "Account registration failed"
  45. check_result $E_CONNECT "LE account registration" >/dev/null
  46. fi
  47. # Parsing LetsEncrypt account data
  48. source $USER_DATA/ssl/le.conf
  49. email=$EMAIL
  50. # Validating domain and aliases
  51. i=1
  52. for alias in $(echo $domain,$aliases |tr ',' '\n' |sort -u); do
  53. $BIN/v-check-letsencrypt-domain $user $alias
  54. if [ "$?" -ne 0 ]; then
  55. sed -i "/ $domain /d" $VESTA/data/queue/letsencrypt.pipe
  56. send_notice "LETSENCRYPT" "$alias validation failed"
  57. check_result $E_INVALID "LE domain validation" >/dev/null
  58. fi
  59. # Checking LE limits per account
  60. if [ "$i" -gt 100 ]; then
  61. sed -i "/ $domain /d" $VESTA/data/queue/letsencrypt.pipe
  62. send_notice 'LETSENCRYPT' 'Limit of domains per account is reached'
  63. check_result $E_LIMIT "LE can't sign more than 100 domains"
  64. fi
  65. i=$((i++))
  66. done
  67. # Generating CSR
  68. ssl_dir=$($BIN/v-generate-ssl-cert "$domain" "$email" "US" "California" \
  69. "San Francisco" "Vesta" "IT" "$aliases" |tail -n1 |awk '{print $2}')
  70. # Signing CSR
  71. crt=$($BIN/v-sign-letsencrypt-csr $user $domain $ssl_dir)
  72. if [ "$?" -ne 0 ]; then
  73. sed -i "/ $domain /d" $VESTA/data/queue/letsencrypt.pipe
  74. send_notice "LETSENCRYPT" "$alias validation failed"
  75. check_result "$E_INVALID" "LE $domain validation"
  76. fi
  77. echo "$crt" > $ssl_dir/$domain.crt
  78. # Dowloading CA certificate
  79. le_certs='https://letsencrypt.org/certs'
  80. x1='lets-encrypt-x1-cross-signed.pem.txt'
  81. x3='lets-encrypt-x3-cross-signed.pem.txt'
  82. issuer=$(openssl x509 -text -in $ssl_dir/$domain.crt |grep "Issuer:")
  83. if [ -z "$(echo $issuer|grep X3)" ]; then
  84. curl -s $le_certs/$x1 > $ssl_dir/$domain.ca
  85. else
  86. curl -s $le_certs/$x3 > $ssl_dir/$domain.ca
  87. fi
  88. # Adding SSL
  89. $BIN/v-delete-web-domain-ssl $user $domain >/dev/null 2>&1
  90. $BIN/v-add-web-domain-ssl $user $domain $ssl_dir
  91. if [ "$?" -ne '0' ]; then
  92. sed -i "/ $domain /d" $VESTA/data/queue/letsencrypt.pipe
  93. send_notice 'LETSENCRYPT' "$domain certificate installation failed"
  94. check_result $? "SSL install" >/dev/null
  95. fi
  96. # Adding LE autorenew cronjob
  97. if [ -z "$(grep v-update-lets $VESTA/data/users/admin/cron.conf)" ]; then
  98. min=$(generate_password '012345' '2')
  99. hour=$(generate_password '1234567' '1')
  100. cmd="sudo $BIN/v-update-letsencrypt-ssl"
  101. $BIN/v-add-cron-job admin "$min" "$hour" '*' '*' '*' "$cmd" > /dev/null
  102. fi
  103. # Updating letsencrypt key
  104. if [ -z "$LETSENCRYPT" ]; then
  105. add_object_key "web" 'DOMAIN' "$domain" 'LETSENCRYPT' 'FTP_USER'
  106. fi
  107. update_object_value 'web' 'DOMAIN' "$domain" '$LETSENCRYPT' 'yes'
  108. #----------------------------------------------------------#
  109. # Vesta #
  110. #----------------------------------------------------------#
  111. # Restarting web
  112. $BIN/v-restart-web $restart
  113. if [ "$?" -ne 0 ]; then
  114. send_notice 'LETSENCRYPT' "web server needs to be restarted manually"
  115. fi
  116. # Notifying user
  117. send_notice 'LETSENCRYPT' "$domain SSL has been installed successfully"
  118. # Deleteing task from queue
  119. sed -i "/ $domain /d" $VESTA/data/queue/letsencrypt.pipe
  120. # Logging
  121. log_event "$OK" "$ARGUMENTS"
  122. exit