You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
7.7 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. #!/usr/bin/env sh
  2. # TESTING!!! #
  3. #Here is a script to deploy cert to apache server.
  4. #returns 0 means success, otherwise error.
  5. #acme.sh --install-cert -d example.com \
  6. #--cert-file /path/to/certfile/in/apache/cert.pem \
  7. #--key-file /path/to/keyfile/in/apache/key.pem \
  8. #--fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem \
  9. #--reloadcmd "service apache2 force-reload"
  10. ######## Public functions #####################
  11. set -x
  12. # get rid of _APACHECTL, and _exec after testing
  13. _APACHECTL='httpd'
  14. _exec() {
  15. eval "$@"
  16. }
  17. ## $1 : new cert location $2: cp to location
  18. _cpCert() {
  19. #return 0
  20. if cp -f ${1} ${2} && chmod 600 ${2}; then
  21. return 0
  22. fi
  23. return 1
  24. }
  25. _vhostBackupConf() {
  26. #return 0
  27. if cp -f "${1}" "${1}.bak"; then
  28. return 0
  29. fi
  30. return 1
  31. }
  32. _vhostRestoreConf() {
  33. #return 0
  34. if cp -f "${1}.bak" "${1}"; then
  35. return 0
  36. fi
  37. return 1
  38. }
  39. _testConf() {
  40. if ! _exec $_APACHECTL -t; then
  41. return 1
  42. fi
  43. return 0
  44. }
  45. ## $1 : vhost config file to check and edit. $2: domain $3: port
  46. _vhostConf() {
  47. if ! _vhostBackupConf "$1"; then
  48. # do something
  49. testvar=''
  50. fi
  51. serverName=$(awk '/ServerName/,/$/' "$1")
  52. serverName=$(awk -F ' ' '{print $2}' <<< ${serverName})
  53. serverAlias=$(awk '/ServerAlias/,/$/' "$1")
  54. serverAlias=$(awk -F ' ' '{print $2}' <<< ${serverAlias})
  55. docRoot=$(awk '/DocumentRoot/,/$/' "$1")
  56. docRoot=$(awk -F ' ' '{print $2}' <<< ${docRoot})
  57. rootParent=$(dirname ${docRoot})
  58. pri=$rootParent/ssl/private
  59. pub=$rootParent/ssl/public
  60. mkdir -m 700 -p ${pri:1}
  61. mkdir -m 700 -p ${pub:1}
  62. sslEng=$(awk '/SSLEngine/,/$/' "$1")
  63. sslEng=$(awk -F ' ' '{print $2}' <<< ${sslEng})
  64. sslPro=$(awk '/SSLProtocol/,/$/' "$1")
  65. sslPro=$(awk -F ' ' '{print $2}' <<< ${sslPro})
  66. sslCiph=$(awk '/SSLCipherSuite/,/$/' "$1")
  67. sslCiph=$(awk -F ' ' '{print $2}' <<< ${sslCiph})
  68. ciphOrd=$(awk '/SSLHonorCipherOrder/,/$/' "$1")
  69. ciphOrd=$(awk -F ' ' '{print $2}' <<< ${ciphOrd})
  70. crtFile=$(awk '/SSLCertificateFile/,/$/' "$1")
  71. crtFile=$(awk -F ' ' '{print $2}' <<< ${crtFile})
  72. keyFile=$(awk '/SSLCertificateKeyFile/,/$/' "$1")
  73. keyFile=$(awk -F ' ' '{print $2}' <<< ${keyFile})
  74. chainFile=$(awk '/SSLCertificateChainFile/,/$/' "$1")
  75. chainFile=$(awk -F ' ' '{print $2}' <<< ${chainFile})
  76. locSec1='<location '
  77. locSec2='>'
  78. locSec=$locSec1$docRoot$locSec2
  79. dirSlash=$(awk '/DirectorySlash/,/$/' "$1")
  80. dirSlash=$(awk -F ' ' '{print $2}' <<< ${dirSlash})
  81. rewriteEng=$(awk '/RewriteEngine/,/$/' "$1")
  82. rewriteEng=$(awk -F ' ' '{print $2}' <<< ${rewriteEng})
  83. rwCond1=$(awk '/RewriteCond %{HTTPS}/,/$/' "$1")
  84. rwCond1=$(awk -F ' ' '{print $2}' <<< ${rwCond1})
  85. rwCond2=$(awk '/RewriteCond %{HTTP_HOST}/,/$/' "$1")
  86. rwCond2=$(awk -F ' ' '{print $2}' <<< ${rwCond2})
  87. rwCond3=$(awk '/RewriteCond %{REQUEST_URI}/,/$/' "$1")
  88. rwCond3=$(awk -F ' ' '{print $2}' <<< ${rwCond3})
  89. rwRuleSsl=$(awk '/RewriteRule .*/,/$/' "$1")
  90. rwRuleSsl=$(awk -F ' ' '{print $2}' <<< ${rwRuleSsl})
  91. newRwRuleSsl1='RewriteRule .* https://'
  92. newRwRuleSsl2='/%{REQUEST_URI}/ [R=301,L,QSA]'
  93. newRwRuleSsl=$newRwRuleSsl1$serverName$newRwRuleSsl2
  94. if [ ! -z "${serverName}" ]; then
  95. # it is probably an alias on a wildcard port 80
  96. # so we will find where docroot matches and redirect there
  97. confRoot=$(dirname "$1")
  98. #confMatch=$(grep "$docRoot" "$configRoot/*.conf" /dev/null | head -n 1)
  99. confMatch="$(grep "${docRoot}" "${confRoot}"/*.conf /dev/null | head -n 1 | awk -F ':' '{print $1}')"
  100. if [ ! -z "${confMatch}" ]; then
  101. #confMatch="$(awk -F ':' '{print $1}' <<< ${confMatch})"
  102. matchServerName=$(awk '/ServerName/,/$/' "${confMatch}")
  103. matchServerName=$(awk -F ' ' '{print $2}' <<< "${matchServerName}")
  104. reWriteBlock=$(cat <<EOF
  105. <IfModule mod_rewrite.c>
  106. RewriteEngine On
  107. RewriteRule .* https://${matchServerName}/%{REQUEST_URI}/ [R=301,L,QSA]
  108. </IfModule>
  109. EOF
  110. )
  111. sed -i '/"${reWriteBlock}"/i </virtualhost>' "${confMatch}"
  112. return 0
  113. fi
  114. return 1
  115. fi
  116. if grep -q 'SSLEngine' "$1"; then
  117. sed -i '/SSLEngine /c\SSLEngine On' "$1"
  118. sed -i '/SSLProtocol /c\SSLProtocol -all +TLSv1.2' "$1"
  119. sed -i '/SSLCipherSuite /c\SSLCipherSuite ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS' "$1"
  120. sed -i '/SSLHonorCipherOrder /c\SSLHonorCipherOrder on' "$1"
  121. sed -i '/SSLCertificateFile /c\SSLCertificateFile ${rootParent}/ssl/public/${serverName}.crt' "$1"
  122. sed -i '/SSLCertificateChainFile /c\SSLCertificateChainFile ${rootParent}/ssl/public/${serverName}.chain.crt' "$1"
  123. sed -i '/SSLCertificateKeyFile /c\SSLCertificateKeyFile ${rootParent}/ssl/private/${serverName}.key' "$1"
  124. testvar=''
  125. else
  126. sslBlock=$(cat <<EOF
  127. <virtualhost *:443>
  128. ServerName ${serverName}
  129. DocumentRoot ${docRoot}
  130. SSLEngine On
  131. SSLProtocol -all +TLSv1.2
  132. SSLCipherSuite ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS
  133. SSLHonorCipherOrder on
  134. SSLCertificateFile ${rootParent}/ssl/public/${serverName}.crt
  135. SSLCertificateChainFile ${rootParent}/ssl/public/${serverName}.chain.crt
  136. SSLCertificateKeyFile ${rootParent}/ssl/private/${serverName}.key
  137. ${locSec}
  138. DirectorySlash On
  139. </location>
  140. </virtualhost>
  141. EOF
  142. )
  143. echo "${sslBlock}" >> "$1"
  144. fi
  145. #look for a location section eg. <location /var/www/html>
  146. if grep -q ${locSec} "$1"; then
  147. if grep -q ${dirSlash} "$1"; then
  148. #set dir slash on
  149. sed -i '/DirectorySlash /c\DirectorySlash On' "$1"
  150. testvar=''
  151. else
  152. #append dir slash here
  153. sed -i '/${locSec}/a DirectorySlash On' "$1"
  154. testvar=''
  155. fi
  156. else
  157. locBlock=$(cat <<EOF
  158. ${locSec}
  159. DirectorySlash On
  160. </location>
  161. EOF
  162. )
  163. # insert the new block here...
  164. sed -i '/<\/virtualhost>/i ${locBlock}' "$1"
  165. fi
  166. #look for mod_rewrite section
  167. modReWrite='<IfModule mod_rewrite.c>'
  168. if grep -q ${modReWrite} "$1"; then
  169. if grep -q "RewriteEngine On" "$1"; then
  170. #set rewrite rules for ssl
  171. # too many ways to redirect ssl for me to check....
  172. testvar=''
  173. else
  174. #append rewrite rules for ssl
  175. sed -i '/${modReWrite}/a RewriteEngine On' "$1"
  176. sed -i '/RewriteEngine On/a RewriteCond %{HTTPS} !on [OR]' "$1"
  177. sed -i '/RewriteCond %{HTTPS} !on [OR]/a RewriteCond %{HTTP_HOST} ^www\. [NC] [OR]' "$1"
  178. sed -i '/RewriteCond %{HTTP_HOST} ^www\. [NC] [OR]/a RewriteCond %{REQUEST_URI} !(.*)/$' "$1"
  179. sed -i '/RewriteCond %{REQUEST_URI} !(.*)/$/a ${newRwRuleSsl}' "$1"
  180. testvar=''
  181. fi
  182. else
  183. reWriteBlock=$(cat <<EOF
  184. <IfModule mod_rewrite.c>
  185. RewriteEngine On
  186. RewriteCond %{HTTPS} !on [OR]
  187. RewriteCond %{HTTP_HOST} ^www\. [NC] [OR]
  188. RewriteCond %{REQUEST_URI} !(.*)/$
  189. ${newRwRuleSsl}
  190. </IfModule>
  191. EOF
  192. )
  193. # insert the new block here...
  194. sed -i '/<\/virtualhost>/i ${reWriteBlock}' "$1"
  195. fi
  196. return
  197. }
  198. apache_deploy() {
  199. _cdomain="$1"
  200. _ckey="$2"
  201. _ccert="$3"
  202. _cca="$4"
  203. _cfullchain="$5"
  204. all_hosts=$(eval "$_APACHECTL -S" | awk '/namevhost/,/\)/')
  205. #echo "$all_hosts"
  206. oldIFS=$IFS
  207. IFS='
  208. '
  209. loopLog=''
  210. for h in $all_hosts; do
  211. d=$(awk -F ' ' '{print $4}' <<< "${h}")
  212. c=$(awk -F ' ' '{print $5}' <<< "${h}")
  213. c=$(echo "$c" | awk -v FS="(\\\\(|\\\\:)" '{print $2}')
  214. p=$(awk -F ' ' '{print $2}' <<< "${h}")
  215. #echo "$d $p $c"
  216. if echo ${d} | grep -q ${_cdomain}; then
  217. if _vhostConf "$c" "$d" "$p"; then
  218. c1='/ssl/public/'
  219. c2='/ssl/private/'
  220. k='.key'
  221. k1=$rootParent$c2$d$k
  222. c3='.crt'
  223. c4='.chain.crt'
  224. c5=$rootParent$c1$d$c3
  225. c6=$rootParent$c1$d$c4
  226. cp -f $_ckey ${k1:1}
  227. cp -f $_ccert ${c5:1}
  228. cp -f $_cfullchain ${c6:1}
  229. fi
  230. fi
  231. done
  232. IFS=$oldIFS
  233. }
  234. apache_deploy idragonfly.net /path/to/test.key /path/to/test.crt /path/to/test.cacert.crt /path/to/test.chain.crt
  235. #echo "$testLog" >> test.log
  236. set +x