$ipaddress) { // Load domain and the equivalent IP address if($debug==true) echo "\t[DEBUG] Converting certificates for domain: ".$domain." with IP address: ".$ipaddress."\n"; // Load existing private key $rc = exec('/bin/grep "^sslcertificatekeyfile:" /var/cpanel/userdata/'. $users[$domain] . '/' . $domain . '_SSL'); if(strpos($rc, "sslcertificatekeyfile:")===false) { echo "[ERROR] User SSL key not found\n"; exit(2); } $userkey = substr($rc, strpos($rc, "sslcertificatekeyfile: ")+23); $userkeypath = dirname($userkey)."/"; if($debug==true) { echo "[DEBUG] User SSL key: ".$userkey."\n"; echo "[DEBUG] User SSL key path: ".$userkeypath."\n"; } $rc = exec('/bin/grep "^sslcertificatefile:" /var/cpanel/userdata/'. $users[$domain] . '/' . $domain . '_SSL'); if(strpos($rc, "sslcertificatefile:")===false) { echo "[ERROR] User SSL config not found\n"; exit(2); } $usercert = substr($rc, strpos($rc, "sslcertificatefile: ")+20); $usercertpath = dirname($usercert)."/"; if($debug==true) { echo "[DEBUG] User SSL certificate: ".$usercert."\n"; echo "[DEBUG] User SSL certificate path: ".$usercertpath."\n"; } $rc = exec('/bin/grep "^sslcacertificatefile:" /var/cpanel/userdata/'. $users[$domain] . '/' . $domain . '_SSL'); if(strpos($rc, "sslcacertificatefile:")===false) { echo "[ERROR] User SSL config not found\n"; exit(2); } $usercacert = substr($rc, strpos($rc, "sslcacertificatefile: ")+22); $usercacertpath = dirname($usercacert)."/"; if($debug==true) { echo "[DEBUG] User SSL CA certificate: ".$usercacert."\n"; echo "[DEBUG] User SSL CA certificate path: ".$usercacertpath."\n"; } //add some links //check and remove if($nochanges==false){ $pem = $certdir . $domain . '.pem'; exec("touch ".$pem); exec("chown root:wheel ".$pem); exec("chmod u+rw-x,g+wr-x,o-wrx ".$pem); exec("cat ".$userkey." > ".$pem); exec('echo "" >> '.$pem); exec("cat ".$usercert." >> ".$pem); exec('echo "" >> '.$pem); exec("cat ".$usercacert." >> ".$pem); } if($courier){ // Generate the final PEM files (courier IMAP/POP3) $pemimap = $certimap.".".$ipaddress; if($debug==true) echo "\t[DEBUG] IMAP PEM file for domain ".$domain.": ".$pemimap."\n"; $pempop3 = $certpop3.".".$ipaddress; if($debug==true) echo "\t[DEBUG] POP3 PEM file for domain ".$domain.": ".$pempop3."\n"; if($nochanges==false) { // Generate IMAP PEM file exec("rm -rf ".$pemimap); exec("touch ".$pemimap); exec("chown root:wheel ".$pemimap); exec("chmod u+rw-x,g+wr-x,o-wrx ".$pemimap); exec("cat ".$key." > ".$pemimap); exec("cat ".$cert." >> ".$pemimap); // Generate POP PEM file exec("rm -rf ".$pempop3); exec("touch ".$pempop3); exec("chown root:wheel ".$pempop3); exec("chmod u+rw-x,g+wr-x,o-wrx ".$pempop3); exec("cat ".$key." > ".$pempop3); exec("cat ".$cert." >> ".$pempop3); } if($debug==true) echo "\t[DEBUG] Combine ".$key." and ".$cert." into ".$pemimap." and ".$pempop3."\n"; } if($dovecot){ exec('echo "local '.$ipaddress.' {" >> '.$dovecotlocal); exec('echo " protocol imap {" >> '.$dovecotlocal); exec('echo " ssl_cert = <'.$pem.'" >> '.$dovecotlocal); exec('echo " ssl_key = <'.$pem.'" >> '.$dovecotlocal); exec('echo " }" >> '.$dovecotlocal); exec('echo " protocol pop3 {" >> '.$dovecotlocal); exec('echo " ssl_cert = <'.$pem.'" >> '.$dovecotlocal); exec('echo " ssl_key = <'.$pem.'" >> '.$dovecotlocal); exec('echo " }" >> '.$dovecotlocal); exec('echo "}" >> '.$dovecotlocal); } // Store the processed IP address $convertedips[] = $ipaddress; echo "\t\n"; } if($debug==true) echo "[DEBUG] Converted a total of ".sizeof($convertedips)." domains\n"; /* // Scanning for stale PEM files if($handle = opendir($certimappath)) { while(false!==($file = readdir($handle))) { $pos = strpos($file, "imapd.pem."); if($pos!==false && $pos===0) { $ip = substr($file, 10); if(strlen($ip)>0 && in_array($ip, $convertedips)==false) { if($nochanges==false) exec("rm -rf ".$certimappath.$file); if($debug==true) echo "[DEBUG] Found stale IMAP PEM file: ".$certimappath.$file."\n"; } } } closedir($handle); } // Scanning for stale POP3 PEM files if($handle = opendir($certpop3path)) { while(false!==($file = readdir($handle))) { $pos = strpos($file, "pop3d.pem."); if($pos!==false && $pos===0) { $ip = substr($file, 10); if(strlen($ip)>0 && in_array($ip, $convertedips)==false) { if($nochanges==false) exec("rm -rf ".$certpop3path.$file); if($debug==true) echo "[DEBUG] Found stale POP3 PEM file: ".$certpop3path.$file."\n"; } } } closedir($handle); } */ //restart services exec('/usr/local/cpanel/scripts/builddovecotconf'); exec('/usr/local/cpanel/scripts/restartsrv_dovecot'); ?>