Skip to content

Commit e5d96e0

Browse files
committed
Update installer_base.lib.php as per the discussions / feedback in the thread at https://www.howtoforge.com/community/threads/wip-use-certbot-standalone-to-create-lets-encrypt-ssl-certs-for-ispconfig-servers.80449 by adding check whether the hostname fqdn A record has the same ip with the server before requesting for Let's Encrypt SSL standalone certs.
1 parent 7a60aea commit e5d96e0

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

install/lib/installer_base.lib.php

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,17 +2400,20 @@ public function configure_apps_vhost() {
24002400
public function make_ispconfig_ssl_cert() {
24012401
global $conf, $autoinstall;
24022402

2403-
// This hostname can be taken from user entry too
2404-
// But I don't find a way for it yet so...
2405-
// I use this for now ;D
2403+
// Get hostname from user entry or shell command
24062404
if($conf['hostname'] !== ('localhost' || '') ) $hostname = $conf['hostname'];
24072405
else $hostname = exec('hostname -f');
24082406

2407+
// Check dns a record exist and its ip equal to server public ip
2408+
$svr_ip = file_get_contents('http://dynamicdns.park-your-domain.com/getip');
2409+
if (checkdnsrr(idn_to_ascii($hostname), 'A')) {
2410+
$dns_A=dns_get_record($hostname, DNS_A); $dns_ip=$dns_A[0][ip];
2411+
}
24092412
// Check if LE SSL folder for the hostname existed
24102413
$le_live_dir = '/etc/letsencrypt/live/' . $hostname;
24112414

2412-
// We support certbot so create standalone LE SSL certs for this server
2413-
if (!@is_dir($le_live_dir)) {
2415+
// We support certbot so let's create standalone LE SSL certs for this server
2416+
if (!@is_dir($le_live_dir) && ($svr_ip = $dns_ip)) {
24142417
// If it is nginx webserver
24152418
if($conf['nginx']['installed'] == true)
24162419
exec("certbot certonly --authenticator standalone -d $hostname --pre-hook 'service nginx stop' --post-hook 'service nginx start'");
@@ -2421,20 +2424,21 @@ public function make_ispconfig_ssl_cert() {
24212424
else
24222425
exec("certbot certonly --authenticator standalone -d $hostname");
24232426
}
2427+
2428+
// Define and check ISPConfig SSL folder
2429+
$install_dir = $conf['ispconfig_install_dir'];
2430+
2431+
$ssl_crt_file = $install_dir.'/interface/ssl/ispserver.crt';
2432+
$ssl_csr_file = $install_dir.'/interface/ssl/ispserver.csr';
2433+
$ssl_key_file = $install_dir.'/interface/ssl/ispserver.key';
2434+
$ssl_pem_file = $install_dir.'/interface/ssl/ispserver.pem';
2435+
2436+
if(!@is_dir($install_dir.'/interface/ssl')) mkdir($install_dir.'/interface/ssl', 0755, true);
24242437

24252438
// If the LE SSL certs for this hostname exists
2426-
if (is_dir($le_live_dir)) {
2427-
2428-
// Define and check ISPConfig SSL folder
2429-
$install_dir = $conf['ispconfig_install_dir'];
2430-
if(!@is_dir($install_dir.'/interface/ssl')) mkdir($install_dir.'/interface/ssl', 0755, true);
2431-
2432-
$ssl_crt_file = $install_dir.'/interface/ssl/ispserver.crt';
2433-
$ssl_key_file = $install_dir.'/interface/ssl/ispserver.key';
2434-
$ssl_pem_file = $install_dir.'/interface/ssl/ispserver.pem';
2435-
$ssl_bak_file = $install_dir.'/interface/ssl/ispserver.*.bak';
2436-
2437-
// Delete old then backup existing ispserver ssl files
2439+
if (is_dir($le_live_dir) && ($svr_ip = $dns_ip)) {
2440+
2441+
// Backup existing ispserver ssl files
24382442
if (is_file($ssl_bak_file)) exec("rm $ssl_bak_file");
24392443
if (is_file($ssl_crt_file)) exec("mv $ssl_crt_file-\$(date +'%y%m%d%H%M%S).bak");
24402444
if (is_file($ssl_key_file)) exec("mv $ssl_key_file-\$(date +'%y%m%d%H%M%S).bak");
@@ -2448,19 +2452,22 @@ public function make_ispconfig_ssl_cert() {
24482452
exec("cat $ssl_key_file $ssl_crt_file > $ssl_pem_file");
24492453
exec("chmod 600 $ssl_pem_file");
24502454
}
2451-
/*
2452-
$ssl_pw = substr(md5(mt_rand()), 0, 6);
2453-
exec("openssl genrsa -des3 -passout pass:$ssl_pw -out $ssl_key_file 4096");
2454-
if(AUTOINSTALL){
2455-
exec("openssl req -new -passin pass:$ssl_pw -passout pass:$ssl_pw -subj '/C=".escapeshellcmd($autoinstall['ssl_cert_country'])."/ST=".escapeshellcmd($autoinstall['ssl_cert_state'])."/L=".escapeshellcmd($autoinstall['ssl_cert_locality'])."/O=".escapeshellcmd($autoinstall['ssl_cert_organisation'])."/OU=".escapeshellcmd($autoinstall['ssl_cert_organisation_unit'])."/CN=".escapeshellcmd($autoinstall['ssl_cert_common_name'])."' -key $ssl_key_file -out $ssl_csr_file");
2456-
} else {
2457-
exec("openssl req -new -passin pass:$ssl_pw -passout pass:$ssl_pw -key $ssl_key_file -out $ssl_csr_file");
2455+
2456+
if (!@is_dir($le_live_dir) && ($svr_ip != $dns_ip)) {
2457+
2458+
// We can still use the old self-signed method
2459+
$ssl_pw = substr(md5(mt_rand()), 0, 6);
2460+
exec("openssl genrsa -des3 -passout pass:$ssl_pw -out $ssl_key_file 4096");
2461+
if(AUTOINSTALL){
2462+
exec("openssl req -new -passin pass:$ssl_pw -passout pass:$ssl_pw -subj '/C=".escapeshellcmd($autoinstall['ssl_cert_country'])."/ST=".escapeshellcmd($autoinstall['ssl_cert_state'])."/L=".escapeshellcmd($autoinstall['ssl_cert_locality'])."/O=".escapeshellcmd($autoinstall['ssl_cert_organisation'])."/OU=".escapeshellcmd($autoinstall['ssl_cert_organisation_unit'])."/CN=".escapeshellcmd($autoinstall['ssl_cert_common_name'])."' -key $ssl_key_file -out $ssl_csr_file");
2463+
} else {
2464+
exec("openssl req -new -passin pass:$ssl_pw -passout pass:$ssl_pw -key $ssl_key_file -out $ssl_csr_file");
2465+
}
2466+
exec("openssl req -x509 -passin pass:$ssl_pw -passout pass:$ssl_pw -key $ssl_key_file -in $ssl_csr_file -out $ssl_crt_file -days 3650");
2467+
exec("openssl rsa -passin pass:$ssl_pw -in $ssl_key_file -out $ssl_key_file.insecure");
2468+
rename($ssl_key_file, $ssl_key_file.'.secure');
2469+
rename($ssl_key_file.'.insecure', $ssl_key_file);
24582470
}
2459-
exec("openssl req -x509 -passin pass:$ssl_pw -passout pass:$ssl_pw -key $ssl_key_file -in $ssl_csr_file -out $ssl_crt_file -days 3650");
2460-
exec("openssl rsa -passin pass:$ssl_pw -in $ssl_key_file -out $ssl_key_file.insecure");
2461-
rename($ssl_key_file, $ssl_key_file.'.secure');
2462-
rename($ssl_key_file.'.insecure', $ssl_key_file);
2463-
*/
24642471
exec("chown -R root:root $install_dir/interface/ssl");
24652472

24662473
}

0 commit comments

Comments
 (0)