added creation of zone files

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-01-22 17:32:36 +01:00
parent b3e8ff6ffb
commit 83709d06e0
2 changed files with 165 additions and 55 deletions

View File

@ -10,13 +10,20 @@ use PDOException;
*/ */
class DomainController class DomainController
{ {
private PDO $dbConnection; private String $localZoneFile;
private String $localZonesDir;
private String $namedConfLocalFile;
private string $zoneCachePath;
public function __construct(PDO $dbConnection) public function __construct(private PDO $dbConnection)
{ {
$this->dbConnection = $dbConnection; $this->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 * @return array|false
*/ */
@ -28,7 +35,7 @@ class DomainController
try { try {
$statement = $this->dbConnection->query($statement); $statement = $this->dbConnection->query($statement);
return $statement->fetchAll(PDO::FETCH_ASSOC); return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
@ -42,41 +49,40 @@ class DomainController
*/ */
public function findByName(String $name): bool|array public function findByName(String $name): bool|array
{ {
$statement = " $sql = "
SELECT id, name, a, aaaa SELECT id, name, a, aaaa
FROM domains FROM domains
WHERE name = :name"; WHERE name = :name";
try { try {
$statement = $this->dbConnection->prepare($statement); $statement = $this->dbConnection->prepare($sql);
$statement->bindParam(':name', $name); $statement->bindParam(param: ':name', var: $name);
$statement->execute(); $statement->execute();
return $statement->fetch(PDO::FETCH_ASSOC);
return $statement->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) {
} catch (\PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param Int $id * @param int $id
* *
* @return array|false * @return array|false
*/ */
public function findByID(Int $id): bool|array public function findByID(int $id): bool|array
{ {
$statement = " $sql = "
SELECT id, name, a, aaaa SELECT id, name, a, aaaa
FROM domains FROM domains
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->dbConnection->prepare($statement); $statement = $this->dbConnection->prepare($sql);
$statement->bindParam(':id', $id); $statement->bindParam(param:':id', var: $id);
$statement->execute(); $statement->execute();
return $statement->fetch(PDO::FETCH_ASSOC);
return $statement->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) {
} catch (\PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
@ -92,24 +98,34 @@ class DomainController
public function insert(String $name, String $a, String $aaaa): int public function insert(String $name, String $a, String $aaaa): int
{ {
// TODO create zone file and include // TODO create zone file and include
$statement = " $sql = "
INSERT INTO domains (name, a, aaaa) INSERT INTO domains (name, a, aaaa)
VALUES (:name, :a, :aaaa)"; VALUES (:name, :a, :aaaa)";
try { try {
$statement = $this->dbConnection->prepare($statement); $statement = $this->dbConnection->prepare($sql);
$statement->bindParam(':name', $name); $statement->bindParam(param: ':name', var: $name);
$statement->bindParam(':a', $a); $statement->bindParam(param: ':a', var: $a);
$statement->bindParam(':aaaa', $aaaa); $statement->bindParam(param: ':aaaa', var: $aaaa);
$statement->execute(); $statement->execute();
return $statement->rowCount();
} catch (\PDOException $e) { $this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
$zoneFilename = $this->localZonesDir . $name;
echo $zoneFilename . PHP_EOL;
if ($localZones = fopen($this->localZoneFile, 'a')) {
fputs($localZones, data: "include \"$zoneFilename\";" . PHP_EOL);
fclose($localZones);
} else {
echo "Error writing to $this->localZoneFile, check permissions";
exit(1);
}
return $this->dbConnection->lastInsertId();
} catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
// TODO
// create zone file
// add zone file to include file
} }
@ -123,28 +139,52 @@ class DomainController
*/ */
public function update(Int $id, String $name, String $a, String $aaaa) public function update(Int $id, String $name, String $a, String $aaaa)
{ {
// TODO UPDATE Zone file $current = $this->findByID($id);
/* doesn't work
$statement = " $statement = "
UPDATE domains SET INSERT INTO domains(id, name, a, aaaa)
name = :name, VALUES(:id, :name, :a, :aaaa)
a = :a, ON DUPLICATE KEY UPDATE
aaaa = :aaaa name=COALESCE(VALUES(name), :name),
a=COALESCE(:a, a),
aaaa=COALESCE(:aaaa, aaaa)";
*/
if (empty($name)) {
$name = $current['name'];
}
if (empty($a)) {
$a = $current['a'];
}
if (empty($aaaa)) {
$aaaa = $current['aaaa'];
}
$sql = "
UPDATE domains SET
name = :name,
a = :a,
aaaa = :aaaa
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->dbConnection->prepare($statement); $statement = $this->dbConnection->prepare($sql);
$statement->bindParam('id', $id); $statement->bindParam(param: 'id', var: $id);
$statement->bindParam('name', $name); $statement->bindParam(param: 'name', var: $name);
$statement->bindParam('a', $a); $statement->bindParam(param: 'a', var: $a);
$statement->bindParam('aaaa', $aaaa); $statement->bindParam(param: 'aaaa', var: $aaaa);
$statement->execute(); $statement->execute();
// recreate zonefile
$this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
exec('/usr/sbin/rndc reload');
return $statement->rowCount(); return $statement->rowCount();
} catch (\PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); print($e->getMessage());
return false;
} }
// TODO
// recreate zone file
} }
@ -162,11 +202,77 @@ class DomainController
try { try {
$statement = $this->dbConnection->prepare($statement); $statement = $this->dbConnection->prepare($statement);
$statement->bindParam('id', $id); $statement->bindParam(param: 'id', var: $id);
$statement->execute(); $statement->execute();
return $statement->rowCount(); return $statement->rowCount();
} catch (\PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/**
* @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);
}
}
} }

View File

@ -39,7 +39,7 @@ class RequestController
if ($this->checkPassword()) { if ($this->checkPassword()) {
try { try {
match ($this->requestMethod) { match ($this->requestMethod) {
'GET' => $this->result = $this->handleDomainGetRequest(), 'GET' => $this->handleDomainGetRequest(),
'POST' => $this->handleDomainPostRequest(), 'POST' => $this->handleDomainPostRequest(),
'PUT' => $this->handleDomainPutRequest(), 'PUT' => $this->handleDomainPutRequest(),
'DELETE' => $this->handleDomainDeleteRequest() 'DELETE' => $this->handleDomainDeleteRequest()
@ -92,17 +92,19 @@ class RequestController
/** /**
* @return array|bool * @return array|bool
*/ */
public function handleDomainGetRequest(): array|bool public function handleDomainGetRequest(): void
{ {
$result = '';
if (empty($this->uri[3])) { if (empty($this->uri[3])) {
$this->result = $this->domainController->findAll(); $this->result = $this->domainController->findAll();
} else { } else {
if (!$this->result = $this->domainController->findByName($this->uri[3])) { if ($result = $this->domainController->findByID(intval($this->uri[3]))) {
$this->result = $result;
} else {
$this->status = "404 Not Found "; $this->status = "404 Not Found ";
$this->message = "The specified domain was not found."; $this->message = "The specified domain was not found.";
} }
} }
return $this->result;
} }
/** /**
@ -110,9 +112,10 @@ class RequestController
*/ */
public function handleDomainPostRequest(): void public function handleDomainPostRequest(): void
{ {
$name = $_POST['name'] ?? ""; $name = $_POST['name'] ?? '';
$a = $_POST['a'] ?? ""; $a = $_POST['a'] ?? '';
$aaaa = $_POST['aaaa'] ?? ""; $aaaa = $_POST['aaaa'] ?? '';
$apikey = $_POST['apikey'] ?? '';
if (empty($name)) { if (empty($name)) {
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "A name is required"; $this->message = "A name is required";
@ -125,7 +128,7 @@ class RequestController
$this->status = "400 Bad request"; $this->status = "400 Bad request";
$this->message = "Domain: $name already exists."; $this->message = "Domain: $name already exists.";
} else { } else {
$result = $this->domainController->insert($name, $a, $aaaa); $result = $this->domainController->insert($name, $a, $aaaa, $apikey);
$this->status = "201 Created"; $this->status = "201 Created";
$this->message = $result; $this->message = $result;
} }
@ -151,6 +154,7 @@ class RequestController
$name = $put['name'] ?? ""; $name = $put['name'] ?? "";
$a = $put['a'] ?? ""; $a = $put['a'] ?? "";
$aaaa = $put['aaaa'] ?? ""; $aaaa = $put['aaaa'] ?? "";
$apikey = $put['apikey'] ?? "";
if ($id == 0) { if ($id == 0) {
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
@ -169,7 +173,7 @@ class RequestController
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "At least one IP address is required."; $this->message = "At least one IP address is required.";
} else { } else {
$dcResult = $this->domainController->update($id, $name, $a, $aaaa); $dcResult = $this->domainController->update($id, $name, $a, $aaaa, $apikey);
$this->status = "201 Updated"; $this->status = "201 Updated";
$this->message = $dcResult; $this->message = $dcResult;
} }
@ -196,7 +200,7 @@ class RequestController
if ($id == 0) { if ($id == 0) {
$this->status = "404 Bad Request"; $this->status = "404 Bad Request";
$this->message = "Domain with ID $id not found."; $this->message = "You need to supply an ID.";
} else { } else {
if (!$this->domainController->findByID($id)) { if (!$this->domainController->findByID($id)) {
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";