v-add-letsencrypt-domain 5.1 KB

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