freedombone-addcert 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Create self-signed or Let's Encrypt certificates on Debian
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. export TEXTDOMAIN=${PROJECT_NAME}-addcert
  31. export TEXTDOMAINDIR="/usr/share/locale"
  32. CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
  33. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  34. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  35. for f in $UTILS_FILES
  36. do
  37. source $f
  38. done
  39. # Don't pin certs by default
  40. PIN_CERTS=
  41. HOSTNAME=
  42. LETSENCRYPT_HOSTNAME=
  43. COUNTRY_CODE="US"
  44. AREA="Free Speech Zone"
  45. LOCATION="Freedomville"
  46. ORGANISATION="Freedombone"
  47. UNIT="Freedombone Unit"
  48. EXTENSIONS=""
  49. NODH=
  50. DH_KEYLENGTH=2048
  51. INSTALL_DIR=/root/build
  52. LETSENCRYPT_SERVER='https://acme-v01.api.letsencrypt.org/directory'
  53. MY_EMAIL_ADDRESS=
  54. FRIENDS_MIRRORS_SERVER=
  55. FRIENDS_MIRRORS_PASSWORD=
  56. FRIENDS_MIRRORS_SSH_PORT=
  57. MY_MIRRORS_PASSWORD=
  58. function show_help {
  59. echo ''
  60. echo $"${PROJECT_NAME}-addcert -h [hostname] -c [country code] -a [area] -l [location]"
  61. echo $' -o [organisation] -u [unit] --ca "" --nodh ""'
  62. echo ''
  63. echo $'Creates a self-signed certificate for the given hostname'
  64. echo ''
  65. echo $' --help Show help'
  66. echo $' -h --hostname [name] Hostname'
  67. echo $' -e --letsencrypt [hostname] Hostname to use with Lets Encrypt'
  68. echo $' -s --server [url] Lets Encrypt server URL'
  69. echo $' -c --country [code] Optional country code (eg. US, GB, etc)'
  70. echo $' -a --area [description] Optional area description'
  71. echo $' -l --location [locn] Optional location name'
  72. echo $' -o --organisation [name] Optional organisation name'
  73. echo $' -u --unit [name] Optional unit name'
  74. echo $' --email [address] Email address for letsencrypt'
  75. echo $' --dhkey [bits] DH key length in bits'
  76. echo $' --nodh "" Do not calculate DH params'
  77. echo $' --ca "" Certificate authority cert'
  78. echo ''
  79. exit 0
  80. }
  81. while [[ $# > 1 ]]
  82. do
  83. key="$1"
  84. case $key in
  85. --help)
  86. show_help
  87. ;;
  88. -h|--hostname)
  89. shift
  90. HOSTNAME="$1"
  91. ;;
  92. -e|--letsencrypt)
  93. shift
  94. LETSENCRYPT_HOSTNAME="$1"
  95. ;;
  96. --email)
  97. shift
  98. MY_EMAIL_ADDRESS="$1"
  99. ;;
  100. -s|--server)
  101. shift
  102. LETSENCRYPT_SERVER="$1"
  103. ;;
  104. -c|--country)
  105. shift
  106. COUNTRY_CODE="$1"
  107. ;;
  108. -a|--area)
  109. shift
  110. AREA="$1"
  111. ;;
  112. -l|--location)
  113. shift
  114. LOCATION="$1"
  115. ;;
  116. -o|--organisation)
  117. shift
  118. ORGANISATION="$1"
  119. ;;
  120. -u|--unit)
  121. shift
  122. UNIT="$1"
  123. ;;
  124. --ca)
  125. shift
  126. EXTENSIONS="-extensions v3_ca"
  127. ORGANISATION="Freedombone-CA"
  128. ;;
  129. --nodh)
  130. shift
  131. NODH="true"
  132. ;;
  133. --dhkey)
  134. shift
  135. DH_KEYLENGTH=${1}
  136. ;;
  137. --pin)
  138. shift
  139. PIN_CERTS=${1}
  140. ;;
  141. *)
  142. # unknown option
  143. ;;
  144. esac
  145. shift
  146. done
  147. if [ ! $HOSTNAME ]; then
  148. if [ ! $LETSENCRYPT_HOSTNAME ]; then
  149. echo $'No hostname specified'
  150. exit 5748
  151. fi
  152. fi
  153. if ! which openssl > /dev/null ;then
  154. echo $"$0: openssl is not installed, exiting" 1>&2
  155. exit 5689
  156. fi
  157. if [ ! -d /etc/ssl/mycerts ]; then
  158. mkdir /etc/ssl/mycerts
  159. fi
  160. CERTFILE=$HOSTNAME
  161. function add_cert_letsencrypt {
  162. CERTFILE=$LETSENCRYPT_HOSTNAME
  163. # obtain the email address for the admin user
  164. if [ ! $MY_EMAIL_ADDRESS ]; then
  165. if [ -f $CONFIGURATION_FILE ]; then
  166. read_config_param MY_EMAIL_ADDRESS
  167. fi
  168. fi
  169. if [ ! $MY_EMAIL_ADDRESS ]; then
  170. if [ -f $COMPLETION_FILE ]; then
  171. if grep -q "Admin user:" $COMPLETION_FILE; then
  172. function_check get_completion_param
  173. ADMIN_USER=$(get_completion_param "Admin user")
  174. if [ ${#ADMIN_USER} -eq 0 ]; then
  175. exit 463732
  176. fi
  177. MY_EMAIL_ADDRESS=$ADMIN_USER@$HOSTNAME
  178. fi
  179. fi
  180. fi
  181. if [ ! -f /usr/bin/certbot ]; then
  182. apt-get -yq install certbot -t jessie-backports
  183. if [ ! -f /usr/bin/certbot ]; then
  184. echo $'LetsEncrypt certbot failed to install'
  185. exit 762830
  186. fi
  187. fi
  188. # stop the web server
  189. systemctl stop nginx
  190. certbot certonly -n --server $LETSENCRYPT_SERVER --standalone -d $LETSENCRYPT_HOSTNAME --renew-by-default --agree-tos --email $MY_EMAIL_ADDRESS
  191. if [ ! "$?" = "0" ]; then
  192. echo $"Failed to install letsencrypt for domain $LETSENCRYPT_HOSTNAME"
  193. systemctl start nginx
  194. exit 63216
  195. fi
  196. # replace some legacy filenames
  197. if [ -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.bundle.crt ]; then
  198. mv /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.bundle.crt /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem
  199. fi
  200. if [ -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.crt ]; then
  201. mv /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.crt /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem
  202. fi
  203. sed -i "s|ssl_certificate /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.bundle.crt|ssl_certificate /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem|g" /etc/nginx/sites-available/$LETSENCRYPT_HOSTNAME
  204. sed -i "s|ssl_certificate /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.crt|ssl_certificate /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem|g" /etc/nginx/sites-available/$LETSENCRYPT_HOSTNAME
  205. # link the private key
  206. if [ -f /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key ]; then
  207. if [ ! -f /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key.old ]; then
  208. mv /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key.old
  209. else
  210. rm -f /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key
  211. fi
  212. fi
  213. if [ -L /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key ]; then
  214. rm /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key
  215. fi
  216. ln -s /etc/letsencrypt/live/${LETSENCRYPT_HOSTNAME}/privkey.pem /etc/ssl/private/${LETSENCRYPT_HOSTNAME}.key
  217. # link the public key
  218. if [ -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem ]; then
  219. if [ ! -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem.old ]; then
  220. mv /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem.old
  221. else
  222. rm -f /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem
  223. fi
  224. fi
  225. if [ -L /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem ]; then
  226. rm /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem
  227. fi
  228. ln -s /etc/letsencrypt/live/${LETSENCRYPT_HOSTNAME}/fullchain.pem /etc/ssl/certs/${LETSENCRYPT_HOSTNAME}.pem
  229. cp /etc/letsencrypt/live/${LETSENCRYPT_HOSTNAME}/fullchain.pem /etc/ssl/mycerts/${LETSENCRYPT_HOSTNAME}.pem
  230. update_default_domain
  231. systemctl start nginx
  232. if [ $PIN_CERTS ]; then
  233. ${PROJECT_NAME}-pin-cert $LETSENCRYPT_HOSTNAME
  234. if [ ! "$?" = "0" ]; then
  235. echo $"Certificate for $LETSENCRYPT_HOSTNAME could not be pinned"
  236. exit 62878
  237. fi
  238. fi
  239. }
  240. function add_cert_selfsigned {
  241. if [[ $ORGANISATION == "Freedombone-CA" ]]; then
  242. CERTFILE="ca-$HOSTNAME"
  243. fi
  244. openssl req -x509 ${EXTENSIONS} -nodes -days 3650 -sha256 \
  245. -subj "/O=$ORGANISATION/OU=$UNIT/C=$COUNTRY_CODE/ST=$AREA/L=$LOCATION/CN=$HOSTNAME" \
  246. -newkey rsa:4096 -keyout /etc/ssl/private/${CERTFILE}.key \
  247. -out /etc/ssl/certs/${CERTFILE}.crt
  248. chmod 400 /etc/ssl/private/${CERTFILE}.key
  249. chmod 640 /etc/ssl/certs/${CERTFILE}.crt
  250. cp /etc/ssl/certs/${CERTFILE}.crt /etc/ssl/mycerts
  251. if [ $PIN_CERTS ]; then
  252. ${PROJECT_NAME}-pin-cert $CERTFILE
  253. if [ ! "$?" = "0" ]; then
  254. echo $"Certificate for $CERTFILE could not be pinned"
  255. exit 62879
  256. fi
  257. fi
  258. }
  259. function generate_dh_params {
  260. if [ ! $NODH ]; then
  261. if [ ! -f /etc/ssl/certs/${CERTFILE}.dhparam ]; then
  262. ${PROJECT_NAME}-dhparam -h ${CERTFILE} --fast yes
  263. fi
  264. fi
  265. }
  266. function restart_web_server {
  267. if [ -f /etc/init.d/nginx ]; then
  268. /etc/init.d/nginx reload
  269. fi
  270. }
  271. function make_cert_bundle {
  272. # Create a bundle of your certificates
  273. cat /etc/ssl/mycerts/*.crt /etc/ssl/mycerts/*.pem > /etc/ssl/${PROJECT_NAME}-bundle.crt
  274. tar -czvf /etc/ssl/${PROJECT_NAME}-certs.tar.gz /etc/ssl/mycerts/*.crt /etc/ssl/mycerts/*.pem
  275. }
  276. function create_cert {
  277. if [ $LETSENCRYPT_HOSTNAME ]; then
  278. add_cert_letsencrypt
  279. else
  280. add_cert_selfsigned
  281. fi
  282. }
  283. read_repo_servers
  284. create_cert
  285. generate_dh_params
  286. restart_web_server
  287. make_cert_bundle
  288. exit 0