/** * Export virtual hosts * @param DatabaseSqlite3 &$db Database object * @return boolean */ public function exportVirtualHosts(&$db) { // Smarty template if ($this->smarty === null) { $this->smarty = TemplateFactory::create(); if ($this->smarty === false) { return false; } } // Get all virtual hosts and generate SNI configurations $vhosts = VirtualHostFactory::populate($db); // Loop foreach ($vhosts as &$v) { // Extra check for a CA bundle if (is_file(Config::read('pkitls|directoryCerts') . '/' . $v->domainName . '.cabundle')) { $v->CABUNDLE = true; } else { $v->CABUNDLE = false; } } // Assign variables $this->smarty->assignByRef('VHOSTS', $vhosts); $rc = $this->saveConfigFile(Config::read('dovecot|directoryConfD') . '/10-ssl.conf', '10sslconf', 'dovecot.tpl', $this->smarty); if ($rc === false) { return false; } else { return true; } }
/** * Export virtual hosts * @param DatabaseSqlite3 &$db Database object * @return boolean */ public function exportVirtualHosts(&$db) { // Smarty template if ($this->smarty === null) { $this->smarty = TemplateFactory::create(); if ($this->smarty === false) { return false; } } // Get all virtual hosts and generate individual configurations $vhosts = VirtualHostFactory::populate($db); // Loop 1 (gather parked domains) foreach ($vhosts as &$v) { // Skip virtual hosts if ($v->parkedUnder == '') { continue; } // Add parked domain to its parent virtual host $vhosts[$v->parkedUnder]->parkedDomains[] = $v->domainName; } // Loop 2 (generate virtual hosts) foreach ($vhosts as &$v) { // Skip parked domains if ($v->parkedUnder != '') { continue; } // Assign variables $this->smarty->assignByRef('SERVERNAME', $v->domainName); $this->smarty->assignByRef('UNIXNAME', $v->unixName); $this->smarty->assignByRef('IPADDRESS', $v->ipAddress); $this->smarty->assignByRef('HOME', $v->home); if (is_file(Config::read('pkitls|directoryCerts') . '/' . $v->domainName . '.chain')) { $this->smarty->assign('CHAIN', true); } else { $this->smarty->assign('CHAIN', false); } if (is_file(Config::read('pkitls|directoryCerts') . '/' . $v->domainName . '.cabundle')) { $this->smarty->assign('CABUNDLE', true); } else { $this->smarty->assign('CABUNDLE', false); } if (isset($v->parkedDomains)) { $this->smarty->assignByRef('PARKEDDOMAINS', $v->parkedDomains); } else { $this->smarty->assign('PARKEDDOMAINS', array()); } // Save configuration file $rc = $this->saveConfigFile(Config::read('apache|directoryConfD') . '/' . $v->domainName . '.conf', '', 'virtualhostapache.tpl', $this->smarty); if ($rc === false) { return false; } } return true; }
/** * Add a new parked domain to the current virtual host * @param string $domainName Domain name to delete * @param array $ns Name servers * @param array $mx Mail exchange servers * @return boolean */ public function addParkedDomain($domainName, $ns = array(), $mx = array()) { // Input validation if ($domainName == '') { Log::error('Input validation failed'); return false; } Log::debug('Add parked domain: ' . $domainName); // Verify the validity of the domain name $domainName = $this->verifyDomainName($domainName); if ($domainName === false) { return false; } // Check to see if this domain name already exists in the database $rc = VirtualHostFactory::exists($this->db, $domainName); if ($rc === true) { Log::error('The domain name already exists in the database'); return false; } // Full home directory path $home = $this->home . '/' . $this->unixName; // Create required directories mkdir($home . '/etc/' . $domainName); mkdir($home . '/mail/' . $domainName); // Set ownership and permissions chown($home . '/etc/' . $domainName, $this->unixName); chgrp($home . '/etc/' . $domainName, 'dovecot'); chmod($home . '/etc/' . $domainName, 0751); chown($home . '/mail/' . $domainName, $this->unixName); chgrp($home . '/mail/' . $domainName, $this->unixName); chmod($home . '/mail/' . $domainName, 0751); // Email address authentication passwd/shadow files $tmp1 = $home . '/etc/' . $domainName . '/passwd'; $tmp2 = $home . '/etc/' . $domainName . '/shadow'; // Create empty authentication files touch($tmp1); chown($tmp1, $this->unixName); chgrp($tmp1, 'dovecot'); chmod($tmp1, 0640); touch($tmp2); chown($tmp2, $this->unixName); chgrp($tmp2, $this->unixName); chmod($tmp2, 0640); // Forwarder file variables $tmp1 = $home . '/etc/' . $domainName . '/forwarders'; $tmp2 = $home . '/etc/' . $domainName . '/forwarders.db'; // Create empty forwarder files touch($tmp1); chown($tmp1, $this->unixName); chgrp($tmp1, 'postfix'); chmod($tmp1, 0664); touch($tmp2); chown($tmp2, $this->unixName); chgrp($tmp2, 'postfix'); chmod($tmp2, 0664); // postmap exec('/usr/sbin/postmap ' . escapeshellarg($tmp1) . ' 2>/dev/null'); // SELinux postfix access to forward & forward.db exec('/usr/bin/chcon -R -t postfix_etc_t ' . escapeshellarg($tmp1)); exec('/usr/bin/chcon -R -t postfix_etc_t ' . escapeshellarg($tmp2)); // Create vhost directory symbolic link to user's home etc directory if (!is_link('/etc/dovecot/vhost/' . $domainName)) { symlink($home . '/etc/' . $domainName, '/etc/dovecot/vhost/' . $domainName); } // Add to database // Prepare statement $preped = $this->db->conn->prepare("INSERT INTO `virtualHost` (DomainName, UnixName, DbPrefix, IpAddress, Home, DomainZoneVersion, adminEmail, Quota, ParkedUnder) VALUES (:domainname, :unixname, :dbprefix, :ipaddress, :home, :domainzoneversion, :adminemail, :quota, :parkedunder)"); // Bind parameter $preped->bindParam(':domainname', $domainName); $preped->bindParam(':unixname', $this->unixName); $preped->bindParam(':dbprefix', $this->dbPrefix); $preped->bindParam(':ipaddress', $this->ipAddress); $preped->bindParam(':home', $this->home); $preped->bindParam(':domainzoneversion', $this->domainZoneVersion); $preped->bindParam(':adminemail', $this->adminEmail); $preped->bindParam(':quota', $this->quota); $preped->bindParam(':parkedunder', $this->id); // Execute prepared statement $rc = $preped->execute(); if ($rc === false) { Log::error('Error while inserting parked domain into the database table: virtualHost'); return false; } // Get ID of inserted virtual host $id = $this->db->conn->lastInsertId(); if ($id === false || !is_numeric($id)) { Log::error('Error while inserting parked domain into the database table: virtualHost'); return false; } // Add name server(s) // (1st try) If none given, use the DNS servers specified by the registrar if (sizeof($ns) === 0) { // Get NS $resolv = dns_get_record($domainName . ".", DNS_NS); // Loop foreach ($resolv as $r) { $ns[] = $r['target']; } } // (2nd try) If none given, use the parent DNS servers as defined in /etc/resolv.conf if (sizeof($ns) === 0) { // Read resolv.conf $resolv = file_get_contents('/etc/resolv.conf'); if ($resolv === false) { Log::error('File not found: /etc/resolv.conf'); return false; } // Parse nameserver(s) $rc = preg_match_all('/^nameserver\\s+(.*)$/im', $resolv, $resolv); if (isset($resolv[1][0]) && sizeof($resolv[1][0]) > 0) { $ns[] = $resolv[1][0]; } } // Prepare statement $preped = $this->db->conn->prepare("INSERT INTO `virtualHostNs` (VirtualHost_ID, DomainName) VALUES (:virtualhost_id, :domainname)"); foreach ($ns as $n) { // Bind parameter $preped->bindParam(':virtualhost_id', $id); $preped->bindParam(':domainname', $n); // Execute prepared statement $rc = $preped->execute(); if ($rc === false) { Log::error('Error while inserting name server into the database table: virtualHostNs'); return false; } } // Add mail exchange server(s) // If none given, use self domain if (sizeof($mx) === 0) { $mx[] = $domainName; } // Prepare statement $preped = $this->db->conn->prepare("INSERT INTO `virtualHostMx` (VirtualHost_ID, DomainName) VALUES (:virtualhost_id, :domainname)"); foreach ($mx as $m) { // Bind parameter $preped->bindParam(':virtualhost_id', $id); $preped->bindParam(':domainname', $m); // Execute prepared statement $rc = $preped->execute(); if ($rc === false) { Log::error('Error while inserting mail exchange server into the database table: virtualHostMx'); return false; } } return true; }
/** * Export virtual hosts * @param DatabaseSqlite3 &$db Database object * @return boolean */ public function exportVirtualHosts(&$db) { // Smarty template if ($this->smarty === null) { $this->smarty = TemplateFactory::create(); if ($this->smarty === false) { return false; } } // OpenDKIM support if (Config::read('opendkim') === 'enabled') { // Default public key file $signatureFile = '/etc/opendkim/keys/default.txt'; if (!is_file($signatureFile)) { Log::error('OpenDKIM public key file not found: ' . $signatureFile); return false; } // Get public key signature $signature = file_get_contents($signatureFile); if ($signature === false) { Log::error('Error while reading the OpenDKIM public key file: ' . signatureFile); return false; } // Filter input $signature = str_replace(array("\n", "\t", '"'), '', $signature); // Extract the TXT part of the signature $rc = preg_match('/\\((.*)\\)/U', $signature, $matches); if ($rc !== 1 || !isset($matches[1])) { Log::error('Error while extracting the signature from the OpenDKIM public key file: ' . signatureFile); return false; } // Remove extra white space within the string and trim $signature = trim(preg_replace('/\\s+/', ' ', $matches[1])); // Assign variable $this->smarty->assignByRef('OPENDKIM', $signature); } else { $this->smarty->assign('OPENDKIM', ''); } // Get all virtual hosts and generate zones $vhosts = VirtualHostFactory::populate($db); // Loop foreach ($vhosts as &$v) { // Assign variables $this->smarty->assignByRef('DOMAINNAME', $v->domainName); $this->smarty->assignByRef('IPADDRESS', $v->ipAddress); $this->smarty->assign('SERIAL', date('Ymd') . $v->domainZoneVersion); $this->smarty->assignByRef('NAMESERVERS', $v->ns); $this->smarty->assignByRef('MAILEXCHANGE', $v->mx); // Generate zone configuration file $rc = $this->saveConfigFile(Config::read('nsd|directoryConfD') . '/' . $v->domainName . '.conf', 'nsdzoneconf', 'nsd.tpl', $this->smarty); if ($rc === false) { return false; } // Generate zone file $rc = $this->saveConfigFile(Config::read('nsd|directoryConfD') . '/' . $v->domainName . '.zone', 'nsdzone', 'nsd.tpl', $this->smarty); if ($rc === false) { return false; } } return true; }