v-add-letsencrypt-domain 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. # Additional argument formatting
  25. format_domain_idn
  26. #----------------------------------------------------------#
  27. # Verifications #
  28. #----------------------------------------------------------#
  29. check_args '2' "$#" 'USER DOMAIN [ALIASES] [RESTART] [NOTIFY]'
  30. is_format_valid 'user' 'domain'
  31. is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
  32. is_system_enabled "$WEB_SSL" 'SSL_SUPPORT'
  33. is_object_valid 'user' 'USER' "$user"
  34. is_object_unsuspended 'user' 'USER' "$user"
  35. is_object_valid 'web' 'DOMAIN' "$domain"
  36. is_object_unsuspended 'web' 'DOMAIN' "$domain"
  37. #----------------------------------------------------------#
  38. # Action #
  39. #----------------------------------------------------------#
  40. # Parsing domain data
  41. get_domain_values 'web'
  42. # Registering LetsEncrypt user account
  43. $BIN/v-add-letsencrypt-user $user
  44. if [ "$?" -ne 0 ]; then
  45. touch $VESTA/data/queue/letsencrypt.pipe
  46. sed -i "/ $domain /d" $VESTA/data/queue/letsencrypt.pipe
  47. send_notice "LETSENCRYPT" "Account registration failed"
  48. check_result $E_CONNECT "LE account registration" >/dev/null
  49. fi
  50. # Parsing LetsEncrypt account data
  51. source $USER_DATA/ssl/le.conf
  52. email=$EMAIL
  53. # Validating domain and aliases
  54. i=1
  55. for alias in $(echo $domain,$aliases |tr ',' '\n' |sort -u); do
  56. $BIN/v-check-letsencrypt-domain $user $alias
  57. if [ "$?" -ne 0 ]; then
  58. touch $VESTA/data/queue/letsencrypt.pipe
  59. sed -i "/ $domain /d" $VESTA/data/queue/letsencrypt.pipe
  60. send_notice "LETSENCRYPT" "$alias validation failed"
  61. check_result $E_INVALID "LE domain validation" >/dev/null
  62. fi
  63. # Checking LE limits per account
  64. if [ "$i" -gt 100 ]; then
  65. touch $VESTA/data/queue/letsencrypt.pipe
  66. sed -i "/ $domain /d" $VESTA/data/queue/letsencrypt.pipe
  67. send_notice 'LETSENCRYPT' 'Limit of domains per account is reached'
  68. check_result $E_LIMIT "LE can't sign more than 100 domains"
  69. fi
  70. i=$((i++))
  71. done
  72. # Generating CSR
  73. ssl_dir=$($BIN/v-generate-ssl-cert "$domain" "$email" "US" "California" \
  74. "San Francisco" "Vesta" "IT" "$aliases" |tail -n1 |awk '{print $2}')
  75. # Signing CSR
  76. crt=$($BIN/v-sign-letsencrypt-csr $user $domain $ssl_dir)
  77. if [ "$?" -ne 0 ]; then
  78. touch $VESTA/data/queue/letsencrypt.pipe
  79. sed -i "/ $domain /d" $VESTA/data/queue/letsencrypt.pipe
  80. send_notice "LETSENCRYPT" "$alias validation failed"
  81. check_result "$E_INVALID" "LE $domain validation"
  82. fi
  83. echo "$crt" > $ssl_dir/$domain.crt
  84. # Dowloading CA certificate
  85. le_certs='https://letsencrypt.org/certs'
  86. x1='lets-encrypt-x1-cross-signed.pem.txt'
  87. x3='lets-encrypt-x3-cross-signed.pem.txt'
  88. issuer=$(openssl x509 -text -in $ssl_dir/$domain.crt |grep "Issuer:")
  89. if [ -z "$(echo $issuer|grep X3)" ]; then
  90. curl -s $le_certs/$x1 > $ssl_dir/$domain.ca
  91. else
  92. curl -s $le_certs/$x3 > $ssl_dir/$domain.ca
  93. fi
  94. # Adding SSL
  95. $BIN/v-delete-web-domain-ssl $user $domain >/dev/null 2>&1
  96. $BIN/v-add-web-domain-ssl $user $domain $ssl_dir
  97. if [ "$?" -ne '0' ]; then
  98. touch $VESTA/data/queue/letsencrypt.pipe
  99. sed -i "/ $domain /d" $VESTA/data/queue/letsencrypt.pipe
  100. send_notice 'LETSENCRYPT' "$domain certificate installation failed"
  101. check_result $? "SSL install" >/dev/null
  102. fi
  103. # Adding LE autorenew cronjob
  104. if [ -z "$(grep v-update-lets $VESTA/data/users/admin/cron.conf)" ]; then
  105. min=$(generate_password '012345' '2')
  106. hour=$(generate_password '1234567' '1')
  107. cmd="sudo $BIN/v-update-letsencrypt-ssl"
  108. $BIN/v-add-cron-job admin "$min" "$hour" '*' '*' '*' "$cmd" > /dev/null
  109. fi
  110. # Updating letsencrypt key
  111. if [ -z "$LETSENCRYPT" ]; then
  112. add_object_key "web" 'DOMAIN' "$domain" 'LETSENCRYPT' 'FTP_USER'
  113. fi
  114. update_object_value 'web' 'DOMAIN' "$domain" '$LETSENCRYPT' 'yes'
  115. #----------------------------------------------------------#
  116. # Vesta #
  117. #----------------------------------------------------------#
  118. # Restarting web
  119. $BIN/v-restart-web $restart
  120. if [ "$?" -ne 0 ]; then
  121. send_notice 'LETSENCRYPT' "web server needs to be restarted manually"
  122. fi
  123. # Notifying user
  124. send_notice 'LETSENCRYPT' "$domain SSL has been installed successfully"
  125. # Deleteing task from queue
  126. touch $VESTA/data/queue/letsencrypt.pipe
  127. sed -i "/ $domain /d" $VESTA/data/queue/letsencrypt.pipe
  128. # Logging
  129. log_event "$OK" "$ARGUMENTS"
  130. exit