|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | /* |
4 | | -Copyright (c) 2007-2010, Till Brehm, projektfarm Gmbh |
| 4 | +Copyright (c) 2007-2018, Till Brehm, projektfarm Gmbh, Hj Ahmad Rasyid Hj Ismail |
5 | 5 | All rights reserved. |
6 | 6 |
|
7 | 7 | Redistribution and use in source and binary forms, with or without modification, |
@@ -938,6 +938,9 @@ public function configure_postfix($options = '') { |
938 | 938 | caselog($command." &> /dev/null", __FILE__, __LINE__, 'EXECUTED: '.$command, 'Failed to execute the command '.$command); |
939 | 939 | } |
940 | 940 |
|
| 941 | + //** We have to change the permissions of the courier authdaemon directory to make it accessible for maildrop. |
| 942 | + $command = 'chmod 755 /var/run/courier/authdaemon/'; |
| 943 | + if(is_file('/var/run/courier/authdaemon/')) caselog($command.' &> /dev/null', __FILE__, __LINE__, 'EXECUTED: '.$command, 'Failed to execute the command '.$command); |
941 | 944 | if(!stristr($options, 'dont-create-certs')) { |
942 | 945 | //* Create the SSL certificate |
943 | 946 | if(AUTOINSTALL){ |
@@ -2048,29 +2051,135 @@ public function configure_apps_vhost() { |
2048 | 2051 | } |
2049 | 2052 |
|
2050 | 2053 | public function make_ispconfig_ssl_cert() { |
2051 | | - global $conf,$autoinstall; |
| 2054 | + global $conf, $autoinstall; |
| 2055 | + |
| 2056 | + //* Get hostname from user entry or shell command */ |
| 2057 | + if($conf['hostname'] !== ('localhost' || '')) $hostname = $conf['hostname']; |
| 2058 | + else $hostname = exec('hostname -f'); |
| 2059 | + |
| 2060 | + // Check dns a record exist and its ip equal to server public ip |
| 2061 | + $svr_ip = file_get_contents('http://dynamicdns.park-your-domain.com/getip'); |
| 2062 | + if (checkdnsrr(idn_to_ascii($hostname, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46), 'A')) { |
| 2063 | + $dnsa=dns_get_record($hostname, DNS_A); |
| 2064 | + $dns_ips = array(); |
| 2065 | + foreach ($dnsa as $rec) { |
| 2066 | + $dns_ips[] = $rec['ip']; |
| 2067 | + } |
| 2068 | + } |
| 2069 | + |
| 2070 | + // Request for certs if no LE SSL folder for server fqdn exist |
| 2071 | + $le_live_dir = '/etc/letsencrypt/live/' . $hostname; |
| 2072 | + if (!@is_dir($le_live_dir) && in_array($svr_ip, $dns_ips)) { |
2052 | 2073 |
|
| 2074 | + // Get the default LE client name and version |
| 2075 | + $le_client = explode("\n", shell_exec('which letsencrypt certbot /root/.local/share/letsencrypt/bin/letsencrypt /opt/eff.org/certbot/venv/bin/certbot')); |
| 2076 | + $le_client = reset($le_client); |
| 2077 | + $le_info = exec($le_client . ' --version 2>&1', $ret, $val); |
| 2078 | + if(preg_match('/^(\S+|\w+)\s+(\d+(\.\d+)+)$/', $le_info, $matches)) { $le_name = $matches[1]; $le_version = $matches[2]; } |
| 2079 | + |
| 2080 | + // Define certbot commands |
| 2081 | + $acme_version = '--server https://acme-v0' . (($le_version >=0.22) ? '2' : '1') . '.api.letsencrypt.org/directory'; |
| 2082 | + $certonly = 'certonly --agree-tos --non-interactive --expand --rsa-key-size 4096'; |
| 2083 | + $webroot = '--authenticator webroot --webroot-path /var/www/html'; |
| 2084 | + $standalone = '--authenticator standalone'; |
| 2085 | + |
| 2086 | + // Only certbot is supported to prevent unknown failures |
| 2087 | + if($le_name == 'certbot' && is_executable($le_client)) { |
| 2088 | + // If this is a webserver, we use webroot |
| 2089 | + if(($conf['nginx']['installed'] || $conf['apache']['installed']) == true) { |
| 2090 | + $well_known = '/var/www/html/.well-known'; |
| 2091 | + $challenge = "$well_known/acme_challenge"; |
| 2092 | + $acme_challenge = '/usr/local/ispconfig/interface/acme/.well-known/acme-challenge'; |
| 2093 | + if (!is_dir($well_known)) mkdir($well_known, 0755, true); |
| 2094 | + if (!is_dir($challenge)) exec("ln -sf $acme_challenge $challenge"); |
| 2095 | + exec("$le_client $certonly $acme_version $webroot --email postmaster@$hostname -d $hostname"); |
| 2096 | + } |
| 2097 | + // Else, it is not webserver, so we use standalone |
| 2098 | + else |
| 2099 | + exec("$le_client $certonly $acme_version $standalone --email postmaster@$hostname -d $hostname"); |
| 2100 | + } |
| 2101 | + } |
| 2102 | + |
| 2103 | + //* Define and check ISPConfig SSL folder */ |
2053 | 2104 | $install_dir = $conf['ispconfig_install_dir']; |
2054 | 2105 |
|
2055 | 2106 | $ssl_crt_file = $install_dir.'/interface/ssl/ispserver.crt'; |
2056 | 2107 | $ssl_csr_file = $install_dir.'/interface/ssl/ispserver.csr'; |
2057 | 2108 | $ssl_key_file = $install_dir.'/interface/ssl/ispserver.key'; |
| 2109 | + $ssl_pem_file = $install_dir.'/interface/ssl/ispserver.pem'; |
2058 | 2110 |
|
2059 | 2111 | if(!@is_dir($install_dir.'/interface/ssl')) mkdir($install_dir.'/interface/ssl', 0755, true); |
2060 | 2112 |
|
2061 | | - $ssl_pw = substr(md5(mt_rand()), 0, 6); |
2062 | | - exec("openssl genrsa -des3 -passout pass:$ssl_pw -out $ssl_key_file 4096"); |
2063 | | - if(AUTOINSTALL){ |
2064 | | - 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"); |
| 2113 | + $date = new DateTime(); |
| 2114 | + |
| 2115 | + // If the LE SSL certs for this hostname exists |
| 2116 | + if (is_dir($le_live_dir) && in_array($svr_ip, $dns_ips)) { |
| 2117 | + |
| 2118 | + // Backup existing ispserver ssl files |
| 2119 | + if (file_exists($ssl_crt_file)) rename($ssl_crt_file, $ssl_crt_file . '-' .$date->format('YmdHis') . '.bak'); |
| 2120 | + if (file_exists($ssl_crt_file)) rename($ssl_key_file, $ssl_key_file . '-' .$date->format('YmdHis') . '.bak'); |
| 2121 | + if (file_exists($ssl_crt_file)) rename($ssl_pem_file, $ssl_pem_file . '-' .$date->format('YmdHis') . '.bak'); |
| 2122 | + |
| 2123 | + // Create symlink to LE fullchain and key for ISPConfig |
| 2124 | + symlink($le_live_dir.'/fullchain.pem', $ssl_crt_file); |
| 2125 | + symlink($le_live_dir.'/privkey.pem', $ssl_key_file); |
| 2126 | + |
2065 | 2127 | } else { |
2066 | | - exec("openssl req -new -passin pass:$ssl_pw -passout pass:$ssl_pw -key $ssl_key_file -out $ssl_csr_file"); |
2067 | | - } |
2068 | | - 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"); |
2069 | | - exec("openssl rsa -passin pass:$ssl_pw -in $ssl_key_file -out $ssl_key_file.insecure"); |
2070 | | - rename($ssl_key_file, $ssl_key_file.'.secure'); |
2071 | | - rename($ssl_key_file.'.insecure', $ssl_key_file); |
2072 | 2128 |
|
2073 | | - exec('chown -R root:root /usr/local/ispconfig/interface/ssl'); |
| 2129 | + // We can still use the old self-signed method |
| 2130 | + $ssl_pw = substr(md5(mt_rand()), 0, 6); |
| 2131 | + exec("openssl genrsa -des3 -passout pass:$ssl_pw -out $ssl_key_file 4096"); |
| 2132 | + if(AUTOINSTALL){ |
| 2133 | + 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"); |
| 2134 | + } else { |
| 2135 | + exec("openssl req -new -passin pass:$ssl_pw -passout pass:$ssl_pw -key $ssl_key_file -out $ssl_csr_file"); |
| 2136 | + } |
| 2137 | + 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"); |
| 2138 | + exec("openssl rsa -passin pass:$ssl_pw -in $ssl_key_file -out $ssl_key_file.insecure"); |
| 2139 | + rename($ssl_key_file, $ssl_key_file.'.secure'); |
| 2140 | + rename($ssl_key_file.'.insecure', $ssl_key_file); |
| 2141 | + } |
| 2142 | + |
| 2143 | + // Build ispserver.pem file and chmod it |
| 2144 | + exec("cat $ssl_key_file $ssl_crt_file > $ssl_pem_file; chmod 600 $ssl_pem_file"); |
| 2145 | + |
| 2146 | + // Extend LE SSL certs to postfix |
| 2147 | + if ($conf['postfix']['installed'] == true && strtolower($this->simple_query('Symlink ISPConfig LE SSL certs to postfix?', array('y', 'n'), 'y')) == 'y') { |
| 2148 | + |
| 2149 | + // Define folder, file(s) |
| 2150 | + $cf = $conf['postfix']; |
| 2151 | + $postfix_dir = $cf['config_dir']; |
| 2152 | + if(!is_dir($postfix_dir)) $this->error("The postfix configuration directory '$postfix_dir' does not exist."); |
| 2153 | + $smtpd_crt = $postfix_dir.'/smtpd.cert'; |
| 2154 | + $smtpd_key = $postfix_dir.'/smtpd.key'; |
| 2155 | + |
| 2156 | + // Backup existing postfix ssl files |
| 2157 | + if (file_exists($smtpd_crt)) rename($smtpd_crt, $smtpd_crt . '-' .$date->format('YmdHis') . '.bak'); |
| 2158 | + if (file_exists($smtpd_key)) rename($smtpd_key, $smtpd_key . '-' .$date->format('YmdHis') . '.bak'); |
| 2159 | + |
| 2160 | + // Create symlink to ISPConfig SSL files |
| 2161 | + symlink($ssl_crt_file, $smtpd_crt); |
| 2162 | + symlink($ssl_key_file, $smtpd_key); |
| 2163 | + } |
| 2164 | + |
| 2165 | + // Extend LE SSL certs to pureftpd |
| 2166 | + if ($conf['pureftpd']['installed'] == true && strtolower($this->simple_query('Symlink ISPConfig LE SSL certs to pureftpd? Creating dhparam file takes some times.', array('y', 'n'), 'y')) == 'y') { |
| 2167 | + |
| 2168 | + // Define folder, file(s) |
| 2169 | + $pureftpd_dir = '/etc/ssl/private'; |
| 2170 | + if(!is_dir($pureftpd_dir)) mkdir($pureftpd_dir, 0755, true); |
| 2171 | + $pureftpd_pem = $pureftpd_dir.'/pure-ftpd.pem'; |
| 2172 | + |
| 2173 | + // Backup existing pureftpd ssl files |
| 2174 | + if (file_exists($pureftpd_pem)) rename($pureftpd_pem, $pureftpd_pem . '-' .$date->format('YmdHis') . '.bak'); |
| 2175 | + |
| 2176 | + // Create symlink to ISPConfig SSL files |
| 2177 | + symlink($ssl_pem_file, $pureftpd_pem); |
| 2178 | + if (!file_exists("$pureftpd_dir/pure-ftpd-dhparams.pem")) |
| 2179 | + exec("cd $pureftpd_dir; openssl dhparam -out dhparam4096.pem 4096; ln -sf dhparam4096.pem pure-ftpd-dhparams.pem"); |
| 2180 | + } |
| 2181 | + |
| 2182 | + exec("chown -R root:root $install_dir/interface/ssl"); |
2074 | 2183 |
|
2075 | 2184 | } |
2076 | 2185 |
|
|
0 commit comments