Browse Source

Improve letsencrypt certs

Bob Mottram 8 years ago
parent
commit
1000297af0
2 changed files with 52 additions and 18 deletions
  1. 7
    0
      src/freedombone-app-xmpp
  2. 45
    18
      src/freedombone-utils-web

+ 7
- 0
src/freedombone-app-xmpp View File

@@ -308,6 +308,13 @@ function install_xmpp_main {
308 308
     if [[ $(app_is_installed xmpp_main) == "1" ]]; then
309 309
         return
310 310
     fi
311
+
312
+    # obtain a cert for the default domain
313
+    if [ ! -f /etc/ssl/certs/${DEFAULT_DOMAIN_NAME}.pem ]; then
314
+        echo $'Obtaining certificate for the main domain'
315
+        create_site_certificate ${DEFAULT_DOMAIN_NAME} 'yes'
316
+    fi
317
+
311 318
     apt-get -yq install lua-sec lua-bitop
312 319
     apt-get -yq install prosody prosody-modules mercurial
313 320
 

+ 45
- 18
src/freedombone-utils-web View File

@@ -187,6 +187,43 @@ function check_certificates {
187 187
     fi
188 188
 }
189 189
 
190
+function cert_exists {
191
+    cert_type='dhparam'
192
+    if [ $2 ]; then
193
+        cert_type="$2"
194
+    fi
195
+    if [ -f /etc/ssl/certs/${1}.${cert_type} ]; then
196
+        echo "1"
197
+    else
198
+        echo "0"
199
+    fi
200
+}
201
+
202
+function create_self_signed_cert {
203
+    ${PROJECT_NAME}-addcert -h ${SITE_DOMAIN_NAME} --dhkey ${DH_KEYLENGTH}
204
+    function_check check_certificates
205
+    check_certificates ${SITE_DOMAIN_NAME}
206
+}
207
+
208
+function create_letsencrypt_cert {
209
+    ${PROJECT_NAME}-addcert -e ${SITE_DOMAIN_NAME} -s ${LETSENCRYPT_SERVER} --dhkey ${DH_KEYLENGTH} --email ${MY_EMAIL_ADDRESS}
210
+    if [ ! "$?" = "0" ]; then
211
+        if [[ ${NO_SELF_SIGNED} == 'no' ]]; then
212
+            echo $"Lets Encrypt failed for ${SITE_DOMAIN_NAME}, so try making a self-signed cert"
213
+            ${PROJECT_NAME}-addcert -h ${SITE_DOMAIN_NAME} --dhkey ${DH_KEYLENGTH}
214
+            function_check check_certificates
215
+            check_certificates ${SITE_DOMAIN_NAME}
216
+        else
217
+            echo $"Lets Encrypt failed for $SITE_DOMAIN_NAME"
218
+            exit 682529
219
+        fi
220
+        return
221
+    fi
222
+
223
+    function_check check_certificates
224
+    check_certificates ${SITE_DOMAIN_NAME} 'yes'
225
+}
226
+
190 227
 function create_site_certificate {
191 228
     SITE_DOMAIN_NAME="$1"
192 229
 
@@ -197,26 +234,16 @@ function create_site_certificate {
197 234
     fi
198 235
 
199 236
     if [[ $ONION_ONLY == "no" ]]; then
200
-        if [ ! -f /etc/ssl/certs/${SITE_DOMAIN_NAME}.dhparam ]; then
237
+        if [[ $(cert_exists) == "0" ]]; then
201 238
             if [[ $LETSENCRYPT_ENABLED != "yes" ]]; then
202
-                ${PROJECT_NAME}-addcert -h ${SITE_DOMAIN_NAME} --dhkey ${DH_KEYLENGTH}
203
-                function_check check_certificates
204
-                check_certificates ${SITE_DOMAIN_NAME}
239
+                create_self_signed_cert
205 240
             else
206
-                ${PROJECT_NAME}-addcert -e ${SITE_DOMAIN_NAME} -s ${LETSENCRYPT_SERVER} --dhkey ${DH_KEYLENGTH} --email ${MY_EMAIL_ADDRESS}
207
-                if [ ! "$?" = "0" ]; then
208
-                    if [[ ${NO_SELF_SIGNED} == 'no' ]]; then
209
-                        echo $"Lets Encrypt failed for ${SITE_DOMAIN_NAME}, so try making a self-signed cert"
210
-                        ${PROJECT_NAME}-addcert -h ${SITE_DOMAIN_NAME} --dhkey ${DH_KEYLENGTH}
211
-                        function_check check_certificates
212
-                        check_certificates ${SITE_DOMAIN_NAME}
213
-                    else
214
-                        echo $"Lets Encrypt failed for $SITE_DOMAIN_NAME"
215
-                        exit 682529
216
-                    fi
217
-                else
218
-                    function_check check_certificates
219
-                    check_certificates ${SITE_DOMAIN_NAME} 'yes'
241
+                create_letsencrypt_cert
242
+            fi
243
+        else
244
+            if [[ $LETSENCRYPT_ENABLED == "yes" ]]; then
245
+                if [[ $(cert_exists pem) == "0" ]]; then
246
+                    create_letsencrypt_cert
220 247
                 fi
221 248
             fi
222 249
         fi