2022-01-26 19:36:09 +01:00
|
|
|
<?php declare(strict_types=1);
|
2022-01-18 19:14:24 +01:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2022-01-26 19:36:09 +01:00
|
|
|
error_reporting(error_level: E_ALL);
|
|
|
|
|
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
use PDO;
|
|
|
|
use PDOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class DomainController
|
|
|
|
{
|
2022-01-22 17:32:36 +01:00
|
|
|
private String $localZoneFile;
|
|
|
|
private String $localZonesDir;
|
|
|
|
private String $namedConfLocalFile;
|
|
|
|
private string $zoneCachePath;
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-01-25 20:34:13 +01:00
|
|
|
public function __construct(private DatabaseConnection $databaseConnection, private PanelController $panelController)
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-01-22 17:32:36 +01:00
|
|
|
$this->localZoneFile = '/etc/bind/local.zones';
|
|
|
|
$this->localZonesDir = '/etc/bind/zones/';
|
|
|
|
$this->namedConfLocalFile = '/etc/bind/named.conf.local';
|
|
|
|
$this->zoneCachePath = '/var/cache/bind/';
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
|
2022-01-22 17:32:36 +01:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
/**
|
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
public function findAll(): bool|array
|
|
|
|
{
|
2022-01-26 19:36:09 +01:00
|
|
|
$sql = "
|
2022-01-25 20:34:13 +01:00
|
|
|
SELECT id, name, panel_id, a, aaaa
|
|
|
|
FROM " . DatabaseConnection::TABLE_DOMAINS . "
|
|
|
|
ORDER BY name";
|
2022-01-18 19:14:24 +01:00
|
|
|
|
|
|
|
try {
|
2022-01-26 19:36:09 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->execute();
|
2022-01-22 17:32:36 +01:00
|
|
|
return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
|
2022-01-18 19:14:24 +01:00
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $name
|
|
|
|
*
|
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
public function findByName(String $name): bool|array
|
|
|
|
{
|
2022-01-22 17:32:36 +01:00
|
|
|
$sql = "
|
2022-01-25 20:34:13 +01:00
|
|
|
SELECT id, name, panel_id, a, aaaa
|
2022-01-22 18:25:18 +01:00
|
|
|
FROM " . DatabaseConnection::TABLE_DOMAINS . "
|
2022-01-18 19:14:24 +01:00
|
|
|
WHERE name = :name";
|
|
|
|
|
|
|
|
try {
|
2022-01-26 19:36:09 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
2022-01-22 17:32:36 +01:00
|
|
|
$statement->bindParam(param: ':name', var: $name);
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement->execute();
|
2022-01-26 19:36:09 +01:00
|
|
|
return $statement->fetch(mode: PDO::FETCH_ASSOC);
|
2022-01-22 17:32:36 +01:00
|
|
|
} catch (PDOException $e) {
|
2022-01-18 19:14:24 +01:00
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 17:32:36 +01:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
/**
|
2022-01-22 17:32:36 +01:00
|
|
|
* @param int $id
|
2022-01-18 19:14:24 +01:00
|
|
|
*
|
|
|
|
* @return array|false
|
|
|
|
*/
|
2022-01-22 17:32:36 +01:00
|
|
|
public function findByID(int $id): bool|array
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-01-22 17:32:36 +01:00
|
|
|
$sql = "
|
2022-01-25 20:34:13 +01:00
|
|
|
SELECT id, name, panel_id, a, aaaa
|
2022-01-22 18:25:18 +01:00
|
|
|
FROM . " . DatabaseConnection::TABLE_DOMAINS . "
|
2022-01-18 19:14:24 +01:00
|
|
|
WHERE id = :id";
|
|
|
|
|
|
|
|
try {
|
2022-01-26 19:36:09 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
2022-01-22 17:32:36 +01:00
|
|
|
$statement->bindParam(param:':id', var: $id);
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement->execute();
|
2022-01-26 19:36:09 +01:00
|
|
|
return $statement->fetch(mode: PDO::FETCH_ASSOC);
|
2022-01-22 17:32:36 +01:00
|
|
|
} catch (PDOException $e) {
|
2022-01-18 19:14:24 +01:00
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 20:34:13 +01:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $name
|
2022-01-25 20:34:13 +01:00
|
|
|
* @param int $panelID
|
2022-01-18 19:14:24 +01:00
|
|
|
* @param String $a
|
|
|
|
* @param String $aaaa
|
|
|
|
*
|
2022-01-26 19:36:09 +01:00
|
|
|
* @return string|false
|
2022-01-18 19:14:24 +01:00
|
|
|
*/
|
2022-01-26 19:36:09 +01:00
|
|
|
public function insert(String $name, int $panelID, String $a, String $aaaa): bool|string
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
|
|
|
// TODO create zone file and include
|
2022-01-22 17:32:36 +01:00
|
|
|
$sql = "
|
2022-01-25 20:34:13 +01:00
|
|
|
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel_id, a, aaaa)
|
|
|
|
VALUES (:name, :panel_id, :a, :aaaa)";
|
2022-01-18 19:14:24 +01:00
|
|
|
|
|
|
|
try {
|
2022-01-26 19:36:09 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
2022-01-22 17:32:36 +01:00
|
|
|
$statement->bindParam(param: ':name', var: $name);
|
2022-01-25 20:34:13 +01:00
|
|
|
$statement->bindParam(param: ':panel_d', var: $panelID);
|
2022-01-22 17:32:36 +01:00
|
|
|
$statement->bindParam(param: ':a', var: $a);
|
|
|
|
$statement->bindParam(param: ':aaaa', var: $aaaa);
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement->execute();
|
2022-01-22 17:32:36 +01:00
|
|
|
|
2022-01-25 20:34:13 +01:00
|
|
|
|
2022-01-26 19:36:09 +01:00
|
|
|
if ($panel = $this->panelController->findByID(id: $panelID)) {
|
2022-01-25 20:34:13 +01:00
|
|
|
$a = $panel['a'];
|
|
|
|
$aaaa = $panel['aaaa'];
|
|
|
|
}
|
2022-01-22 17:32:36 +01:00
|
|
|
$this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
|
|
|
|
$zoneFilename = $this->localZonesDir . $name;
|
|
|
|
echo $zoneFilename . PHP_EOL;
|
|
|
|
|
2022-01-26 19:36:09 +01:00
|
|
|
if ($localZones = fopen(filename: $this->localZoneFile, mode: 'a')) {
|
|
|
|
fputs(stream: $localZones, data: "include \"$zoneFilename\";" . PHP_EOL);
|
|
|
|
fclose(stream: $localZones);
|
2022-01-22 17:32:36 +01:00
|
|
|
} else {
|
|
|
|
echo "Error writing to $this->localZoneFile, check permissions";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-01-22 18:25:18 +01:00
|
|
|
return $this->databaseConnection->getConnection()->lastInsertId();
|
2022-01-22 17:32:36 +01:00
|
|
|
} catch (PDOException $e) {
|
2022-01-18 19:14:24 +01:00
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Int $id
|
|
|
|
* @param String $name
|
2022-01-25 20:34:13 +01:00
|
|
|
* @param int $panelID
|
2022-01-18 19:14:24 +01:00
|
|
|
* @param String $a
|
|
|
|
* @param String $aaaa
|
|
|
|
*
|
2022-01-22 18:25:18 +01:00
|
|
|
* @return false|int
|
2022-01-18 19:14:24 +01:00
|
|
|
*/
|
2022-01-25 20:34:13 +01:00
|
|
|
public function update(Int $id, String $name, int $panelID, String $a, String $aaaa): bool|int
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-01-26 19:36:09 +01:00
|
|
|
$current = $this->findByID(id: $id);
|
2022-01-22 17:32:36 +01:00
|
|
|
|
|
|
|
/* doesn't work
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement = "
|
2022-01-22 17:32:36 +01:00
|
|
|
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'];
|
|
|
|
}
|
2022-01-25 20:34:13 +01:00
|
|
|
if (empty($panelID)) {
|
|
|
|
$panelID = $current['panel_id'];
|
|
|
|
}
|
|
|
|
$panelID = intval(value: $panelID);
|
2022-01-22 17:32:36 +01:00
|
|
|
if (empty($a)) {
|
|
|
|
$a = $current['a'];
|
|
|
|
}
|
|
|
|
if (empty($aaaa)) {
|
|
|
|
$aaaa = $current['aaaa'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = "
|
2022-01-22 18:25:18 +01:00
|
|
|
UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET
|
2022-01-22 17:32:36 +01:00
|
|
|
name = :name,
|
2022-01-25 20:34:13 +01:00
|
|
|
panel_id = :panel_id,
|
2022-01-22 17:32:36 +01:00
|
|
|
a = :a,
|
|
|
|
aaaa = :aaaa
|
2022-01-18 19:14:24 +01:00
|
|
|
WHERE id = :id";
|
|
|
|
|
|
|
|
try {
|
2022-01-26 19:36:09 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
2022-01-22 17:32:36 +01:00
|
|
|
$statement->bindParam(param: 'id', var: $id);
|
|
|
|
$statement->bindParam(param: 'name', var: $name);
|
2022-01-25 20:34:13 +01:00
|
|
|
$statement->bindParam(param: 'panel_id', var: $panelID);
|
2022-01-22 17:32:36 +01:00
|
|
|
$statement->bindParam(param: 'a', var: $a);
|
|
|
|
$statement->bindParam(param: 'aaaa', var: $aaaa);
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement->execute();
|
2022-01-22 17:32:36 +01:00
|
|
|
|
|
|
|
// recreate zonefile
|
2022-01-26 19:36:09 +01:00
|
|
|
if ($panel = $this->panelController->findByID(id: intval(value: $panelID))) {
|
2022-01-25 20:34:13 +01:00
|
|
|
$a = $panel['a'];
|
|
|
|
$aaaa = $panel['aaaa'];
|
|
|
|
}
|
2022-01-22 17:32:36 +01:00
|
|
|
$this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
|
2022-01-22 18:25:18 +01:00
|
|
|
exec(command: '/usr/sbin/rndc reload');
|
2022-01-22 17:32:36 +01:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
return $statement->rowCount();
|
2022-01-22 17:32:36 +01:00
|
|
|
} catch (PDOException $e) {
|
|
|
|
print($e->getMessage());
|
|
|
|
return false;
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function delete($id): int
|
|
|
|
{
|
|
|
|
// TODO delete zone file and include
|
2022-01-26 19:36:09 +01:00
|
|
|
$sql = "
|
2022-01-22 18:25:18 +01:00
|
|
|
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
|
2022-01-18 19:14:24 +01:00
|
|
|
WHERE id = :id";
|
|
|
|
|
|
|
|
try {
|
2022-01-26 19:36:09 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
2022-01-22 17:32:36 +01:00
|
|
|
$statement->bindParam(param: 'id', var: $id);
|
2022-01-18 19:14:24 +01:00
|
|
|
$statement->execute();
|
|
|
|
return $statement->rowCount();
|
2022-01-22 17:32:36 +01:00
|
|
|
} catch (PDOException $e) {
|
2022-01-18 19:14:24 +01:00
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2022-01-22 17:32:36 +01:00
|
|
|
|
2022-01-25 20:34:13 +01:00
|
|
|
/**
|
|
|
|
* @param String $field
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getLongestEntry(String $field): int
|
|
|
|
{
|
2022-01-26 19:36:09 +01:00
|
|
|
$sql = "
|
2022-01-25 20:34:13 +01:00
|
|
|
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DOMAINS;
|
|
|
|
|
|
|
|
try {
|
2022-01-26 19:36:09 +01:00
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
2022-01-25 20:34:13 +01:00
|
|
|
$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);
|
|
|
|
|
2022-01-26 19:36:09 +01:00
|
|
|
$pwuid = posix_getpwuid(user_id: $uid);
|
2022-01-25 20:34:13 +01:00
|
|
|
$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;
|
2022-01-26 19:36:09 +01:00
|
|
|
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
|
|
|
|
if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) {
|
2022-01-25 20:34:13 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 17:32:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array|bool
|
|
|
|
*/
|
|
|
|
function checkDomains(): array|bool
|
|
|
|
{
|
2022-01-26 19:36:09 +01:00
|
|
|
return true;
|
|
|
|
/*
|
2022-01-22 17:32:36 +01:00
|
|
|
$domains = $this->findAll();
|
|
|
|
|
2022-01-26 19:36:09 +01:00
|
|
|
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
|
|
|
|
if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) {
|
2022-01-22 17:32:36 +01:00
|
|
|
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;
|
|
|
|
}
|
2022-01-26 19:36:09 +01:00
|
|
|
*/
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
|
|
|
|
2022-01-22 18:25:18 +01:00
|
|
|
|
2022-01-22 17:32:36 +01:00
|
|
|
/**
|
|
|
|
* @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')) {
|
2022-01-26 19:36:09 +01:00
|
|
|
fputs(stream: $zonefile, data: "zone \"$name\" IN {" . PHP_EOL);
|
|
|
|
fputs(stream: $zonefile, data: "\ttype slave;" . PHP_EOL);
|
|
|
|
fputs(stream: $zonefile, data: "\tfile \"" . $this->zoneCachePath . $name . '.db";' . PHP_EOL);
|
|
|
|
fputs(stream: $zonefile, data: "\tmasters {" . PHP_EOL);
|
2022-01-22 17:32:36 +01:00
|
|
|
if (!empty($a)) {
|
2022-01-26 19:36:09 +01:00
|
|
|
fputs(stream: $zonefile, data: "\t\t$a;" . PHP_EOL);
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
|
|
|
if (!empty($aaaa)) {
|
2022-01-26 19:36:09 +01:00
|
|
|
fputs(stream: $zonefile, data: "\t\t$aaaa;" . PHP_EOL);
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
2022-01-26 19:36:09 +01:00
|
|
|
fputs(stream: $zonefile, data: "\t};" . PHP_EOL);
|
|
|
|
fputs(stream: $zonefile, data: "};" . PHP_EOL);
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
2022-01-25 20:34:13 +01:00
|
|
|
|
|
|
|
// TODO check if ist exist in the include, else create
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|