localZoneFile = '/etc/bind/local.zones'; $this->localZonesDir = '/etc/bind/zones/'; $this->namedConfLocalFile = '/etc/bind/named.conf.local'; $this->zoneCachePath = '/var/cache/bind/'; } /** * @return array|false */ public function findAll(): bool|array { $statement = " SELECT id, name, panel_id, a, aaaa FROM " . DatabaseConnection::TABLE_DOMAINS . " ORDER BY name"; try { $statement = $this->databaseConnection->getConnection()->query($statement); return $statement->fetchAll(mode: PDO::FETCH_ASSOC); } catch (PDOException $e) { exit($e->getMessage()); } } /** * @param String $name * * @return array|false */ public function findByName(String $name): bool|array { $sql = " SELECT id, name, panel_id, a, aaaa FROM " . DatabaseConnection::TABLE_DOMAINS . " WHERE name = :name"; try { $statement = $this->databaseConnection->getConnection()->prepare($sql); $statement->bindParam(param: ':name', var: $name); $statement->execute(); return $statement->fetch(PDO::FETCH_ASSOC); } catch (PDOException $e) { exit($e->getMessage()); } } /** * @param int $id * * @return array|false */ public function findByID(int $id): bool|array { $sql = " SELECT id, name, panel_id, a, aaaa FROM . " . DatabaseConnection::TABLE_DOMAINS . " WHERE id = :id"; try { $statement = $this->databaseConnection->getConnection()->prepare($sql); $statement->bindParam(param:':id', var: $id); $statement->execute(); return $statement->fetch(PDO::FETCH_ASSOC); } catch (PDOException $e) { exit($e->getMessage()); } } /** * @param String $name * @param int $panelID * @param String $a * @param String $aaaa * * @return int */ public function insert(String $name, int $panelID, String $a, String $aaaa): int { // TODO create zone file and include $sql = " INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel_id, a, aaaa) VALUES (:name, :panel_id, :a, :aaaa)"; try { $statement = $this->databaseConnection->getConnection()->prepare($sql); $statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':panel_d', var: $panelID); $statement->bindParam(param: ':a', var: $a); $statement->bindParam(param: ':aaaa', var: $aaaa); $statement->execute(); if ($panel = $this->panelController->findByID($panelID)) { $a = $panel['a']; $aaaa = $panel['aaaa']; } $this->createZoneFile(name: $name, a: $a, aaaa: $aaaa); $zoneFilename = $this->localZonesDir . $name; echo $zoneFilename . PHP_EOL; if ($localZones = fopen($this->localZoneFile, mode: 'a')) { fputs($localZones, data: "include \"$zoneFilename\";" . PHP_EOL); fclose($localZones); } else { echo "Error writing to $this->localZoneFile, check permissions"; exit(1); } return $this->databaseConnection->getConnection()->lastInsertId(); } catch (PDOException $e) { exit($e->getMessage()); } } /** * @param Int $id * @param String $name * @param int $panelID * @param String $a * @param String $aaaa * * @return false|int */ public function update(Int $id, String $name, int $panelID, String $a, String $aaaa): bool|int { $current = $this->findByID($id); /* doesn't work $statement = " INSERT INTO domains(id, name, a, aaaa) VALUES(:id, :name, :a, :aaaa) ON DUPLICATE KEY UPDATE name=COALESCE(VALUES(name), :name), a=COALESCE(:a, a), aaaa=COALESCE(:aaaa, aaaa)"; */ if (empty($name)) { $name = $current['name']; } if (empty($panelID)) { $panelID = $current['panel_id']; } $panelID = intval(value: $panelID); if (empty($a)) { $a = $current['a']; } if (empty($aaaa)) { $aaaa = $current['aaaa']; } $sql = " UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET name = :name, panel_id = :panel_id, a = :a, aaaa = :aaaa WHERE id = :id"; try { $statement = $this->databaseConnection->getConnection()->prepare($sql); $statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'name', var: $name); $statement->bindParam(param: 'panel_id', var: $panelID); $statement->bindParam(param: 'a', var: $a); $statement->bindParam(param: 'aaaa', var: $aaaa); $statement->execute(); // recreate zonefile if ($panel = $this->panelController->findByID($panelID)) { $a = $panel['a']; $aaaa = $panel['aaaa']; } $this->createZoneFile(name: $name, a: $a, aaaa: $aaaa); exec(command: '/usr/sbin/rndc reload'); return $statement->rowCount(); } catch (PDOException $e) { print($e->getMessage()); return false; } } /** * @param $id * * @return int */ public function delete($id): int { // TODO delete zone file and include $statement = " DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . " WHERE id = :id"; try { $statement = $this->databaseConnection->getConnection()->prepare($statement); $statement->bindParam(param: 'id', var: $id); $statement->execute(); return $statement->rowCount(); } catch (PDOException $e) { exit($e->getMessage()); } } /** * @param String $field * * @return int */ public function getLongestEntry(String $field): int { $statement = " SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DOMAINS; try { $statement = $this->databaseConnection->getConnection()->prepare($statement); $statement->execute(); $result = $statement->fetch(); return $result['length']; } catch (PDOException $e) { exit($e->getMessage()); } } /** * @return void */ function checkPermissions(): void { echo 'Checking permission:' . PHP_EOL . PHP_EOL; $uid = posix_geteuid(); print("UID:\t$uid" . PHP_EOL); $pwuid = posix_getpwuid($uid); $name = $pwuid['name']; print("Name:\t$name" . PHP_EOL); $bindGroup = posix_getgrnam(name: 'bind'); $members = $bindGroup['members']; if (in_array(needle: $name, haystack: $members)) { echo "\t✅ is in group 'bind" . PHP_EOL; } echo 'Checking file: ' .$this->localZoneFile . PHP_EOL; $localZoneFilePermissions = fileperms(filename: $this->localZoneFile); if ($localZoneFilePermissions & 0x0010) { echo "\t✅ Group has write access." . PHP_EOL; } else { echo "\t❌Group needs write permission!" . PHP_EOL; } echo "Checking $this->namedConfLocalFile" . PHP_EOL; if ($namedConfLocal = file_get_contents($this->namedConfLocalFile)) { if (!str_contains($namedConfLocal, $this->localZoneFile)) { echo "\t❌ $this->localZoneFile needs to be included in $this->namedConfLocalFile." . PHP_EOL; } else { echo "\t✅ $this->localZoneFile is included in $this->namedConfLocalFile" . PHP_EOL; } } else { echo "\t❌ No access to '$this->namedConfLocalFile'. Please check permissions" . PHP_EOL; } echo 'Checking directory: ' . $this->localZonesDir . PHP_EOL; $localZoneDirPermissions = fileperms(filename: $this->localZonesDir); if ($localZoneDirPermissions & 0x0010) { echo "\t✅ Group has write access." . PHP_EOL; } else { echo "\t❌Group needs write permission!" . PHP_EOL; } } /** * @return array|bool */ function checkDomains(): array|bool { $domains = $this->findAll(); if ($namedConfLocal = file_get_contents($this->namedConfLocalFile)) { if (!str_contains($namedConfLocal, $this->localZoneFile)) { return "$this->localZoneFile needs to be included in $this->namedConfLocalFile."; } } else { return "No access to '$this->namedConfLocalFile'. Please check permissions"; } if (!fileperms($this->localZoneFile)) { return "No access to $this->localZoneFile. Please check permissions."; } $localZones = file_get_contents($this->localZoneFile); foreach($domains as $domain) { if(!str_contains($localZones, $domain['name'])) { $errors[] = $domain['name'] . " is missing in '$this->localZoneFile'"; } $zoneFile = $this->localZonesDir . $domain['name']; if (!file_exists($zoneFile)) { $errors[] = "Missing zone file for $zoneFile. Update zone to create it"; } } if (empty($errors)) { return true; } else { return $errors; } } /** * @param mixed $name * @param mixed $a * @param mixed $aaaa * * @return void */ public function createZoneFile(String $name, String $a, String $aaaa): void { if ($zonefile = fopen(filename: $this->localZonesDir . $name, mode: 'w')) { fputs($zonefile, data: "zone \"$name\" IN {" . PHP_EOL); fputs($zonefile, data: "\ttype slave;" . PHP_EOL); fputs($zonefile, data: "\tfile \"" . $this->zoneCachePath . $name . '.db";' . PHP_EOL); fputs($zonefile, data: "\tmasters {" . PHP_EOL); if (!empty($a)) { fputs($zonefile, data: "\t\t$a;" . PHP_EOL); } if (!empty($aaaa)) { fputs($zonefile, data: "\t\t$aaaa;" . PHP_EOL); } fputs($zonefile, data: "\t};" . PHP_EOL); fputs($zonefile, data: "};" . PHP_EOL); } // TODO check if ist exist in the include, else create } }