Esempio n. 1
0
 /**
  * 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;
 }