Skip to content

Commit

Permalink
Add record disabled property/functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
BotoX committed Sep 5, 2022
1 parent 5f4cc71 commit 0aee7b7
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 37 deletions.
9 changes: 5 additions & 4 deletions backend/src/controllers/Records.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function postNew(Request $req, Response $res, array $args)

// Single PTR record exists, update it
if (isset($record)) {
$rresult = $records->updateRecord($record['id'], $reverse['arpa'], 'PTR', $body['name'], $body['priority'], $body['ttl']);
$rresult = $records->updateRecord($record['id'], $reverse['arpa'], 'PTR', $body['name'], $body['priority'], $body['ttl'], null);
$line = '';
$check = array('name', 'type', 'content', 'priority', 'ttl');
foreach ($check as $item) {
Expand Down Expand Up @@ -265,6 +265,7 @@ public function put(Request $req, Response $res, array $args)
$priority = array_key_exists('priority', $body) ? $body['priority'] : null;
$ttl = array_key_exists('ttl', $body) ? $body['ttl'] : null;
$ptr = array_key_exists('ptr', $body) ? $body['ptr'] : null;
$disabled = array_key_exists('disabled', $body) ? $body['disabled'] : null;

$records = new \Operations\Records($this->c);

Expand Down Expand Up @@ -311,7 +312,7 @@ public function put(Request $req, Response $res, array $args)
}

try {
$result = $records->updateRecord($recordId, $name, $type, $content, $priority, $ttl);
$result = $records->updateRecord($recordId, $name, $type, $content, $priority, $ttl, $disabled);
} catch (\Exceptions\NotFoundException $e) {
$this->logger->debug('User tries to update not existing record.');
return $res->withJson(['error' => 'The record does not exist.'], 404);
Expand Down Expand Up @@ -381,7 +382,7 @@ public function put(Request $req, Response $res, array $args)
}
} else {
// Reverse zone stayed the same, update existing PTR record
$rresult = $records->updateRecord($record['id'], $reverse['arpa'], 'PTR', $result['new']['name'], $result['new']['priority'], $result['new']['ttl']);
$rresult = $records->updateRecord($record['id'], $reverse['arpa'], 'PTR', $result['new']['name'], $result['new']['priority'], $result['new']['ttl'], null);
$line = '';
$check = array('name', 'type', 'content', 'priority', 'ttl');
foreach ($check as $item) {
Expand Down Expand Up @@ -422,7 +423,7 @@ public function put(Request $req, Response $res, array $args)

// Found PTR record in new zone, update it
if (isset($record)) {
$rresult = $records->updateRecord($record['id'], $reverse['arpa'], 'PTR', $result['new']['name'], $result['new']['priority'], $result['new']['ttl']);
$rresult = $records->updateRecord($record['id'], $reverse['arpa'], 'PTR', $result['new']['name'], $result['new']['priority'], $result['new']['ttl'], null);
$line = '';
$check = array('name', 'type', 'content', 'priority', 'ttl');
foreach ($check as $item) {
Expand Down
8 changes: 6 additions & 2 deletions backend/src/controllers/Remote.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function updatePassword(Request $req, Response $res, array $args)
{
$record = $req->getParam('record');
$content = $req->getParam('content');
$disabled = $req->getParam('disabled');
$password = $req->getParam('password');

if ($record === null || $content === null || $password === null) {
Expand All @@ -41,7 +42,7 @@ public function updatePassword(Request $req, Response $res, array $args)
$remote = new \Operations\Remote($this->c);

try {
$remote->updatePassword(intval($record), $content, $password);
$remote->updatePassword(intval($record), $content, $disabled, $password);
} catch (\Exceptions\NotFoundException $e) {
$this->logger->debug('User tried to update non existent record via changepw api.');
return $res->withJson(['error' => 'The given record does not exist.'], 404);
Expand All @@ -58,17 +59,20 @@ public function updateKey(Request $req, Response $res, array $args)
{
$record = $req->getParsedBodyParam('record');
$content = $req->getParsedBodyParam('content');
$disabled = $req->getParsedBodyParam('disabled');
$time = $req->getParsedBodyParam('time');
$signature = $req->getParsedBodyParam('signature');

$disabled = $disabled === "" ? null : $disabled;

if ($record === null || $content === null || $time === null || $signature === null) {
return $res->withJson(['error' => 'One of the required fields is missing.'], 422);
}

$remote = new \Operations\Remote($this->c);

try {
$remote->updateKey($record, $content, $time, $signature);
$remote->updateKey($record, $content, $disabled, $time, $signature);
} catch (\Exceptions\NotFoundException $e) {
$this->logger->debug('User tried to update non existent record via changekey api.');
return $res->withJson(['error' => 'The given record does not exist.'], 404);
Expand Down
12 changes: 8 additions & 4 deletions backend/src/operations/Records.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function getRecords(
$pageStr = \Services\Database::makePagingString($pi);

$query = $this->db->prepare('
SELECT R.id,R.name,R.type,R.content,R.prio as priority,R.ttl,R.domain_id as domain FROM records R
SELECT R.id,R.name,R.type,R.content,R.prio as priority,R.ttl,R.disabled,R.domain_id as domain FROM records R
LEFT OUTER JOIN domains D ON R.domain_id = D.id
LEFT OUTER JOIN permissions P ON P.domain_id = R.domain_id
WHERE (P.user_id=:userId OR :userIsAdmin) AND
Expand Down Expand Up @@ -230,7 +230,7 @@ public function deleteRecord(int $id) : array
*/
public function getRecord(int $recordId) : array
{
$query = $this->db->prepare('SELECT id,name,type,content,prio AS priority,ttl,domain_id AS domain FROM records
$query = $this->db->prepare('SELECT id,name,type,content,prio AS priority,ttl,domain_id AS domain,disabled FROM records
WHERE id=:recordId');
$query->bindValue(':recordId', $recordId, \PDO::PARAM_INT);
$query->execute();
Expand All @@ -245,6 +245,7 @@ public function getRecord(int $recordId) : array
$record['priority'] = intval($record['priority']);
$record['ttl'] = intval($record['ttl']);
$record['domain'] = intval($record['domain']);
$record['disabled'] = boolval($record['disabled']);

return $record;
}
Expand Down Expand Up @@ -337,13 +338,14 @@ public function findRecord(? string $name, ? string $type, ? string $content, ?
* @param $content New content
* @param $priority New priority
* @param $ttl New ttl
* @param $disabled New disabled
*
* @return array Record entry
*
* @throws NotFoundException The given record does not exist
* @throws SemanticException The given record type is invalid
*/
public function updateRecord(int $recordId, ? string $name, ? string $type, ? string $content, ? int $priority, ? int $ttl) : array
public function updateRecord(int $recordId, ? string $name, ? string $type, ? string $content, ? int $priority, ? int $ttl, ? bool $disabled) : array
{
if ($type !== null && !in_array($type, $this->c['config']['records']['allowedTypes'])) {
throw new \Exceptions\SemanticException();
Expand All @@ -356,17 +358,19 @@ public function updateRecord(int $recordId, ? string $name, ? string $type, ? st
$content = $content === null ? $record['content'] : $content;
$priority = $priority === null ? intval($record['priority']) : $priority;
$ttl = $ttl === null ? intval($record['ttl']) : $ttl;
$disabled = $disabled === null ? boolval($record['disabled']) : $disabled;

$this->db->beginTransaction();

$query = $this->db->prepare('UPDATE records SET name=:name,type=:type,content=:content,ttl=:ttl,prio=:prio
$query = $this->db->prepare('UPDATE records SET name=:name,type=:type,content=:content,ttl=:ttl,prio=:prio,disabled=:disabled
WHERE id=:recordId');
$query->bindValue(':recordId', $recordId, \PDO::PARAM_INT);
$query->bindValue(':name', $name, \PDO::PARAM_STR);
$query->bindValue(':type', $type, \PDO::PARAM_STR);
$query->bindValue(':content', $content, \PDO::PARAM_STR);
$query->bindValue(':ttl', $ttl, \PDO::PARAM_INT);
$query->bindValue(':prio', $priority, \PDO::PARAM_INT);
$query->bindValue(':disabled', $disabled, \PDO::PARAM_BOOL);
$query->execute();

$soa = new \Operations\Soa($this->c);
Expand Down
35 changes: 21 additions & 14 deletions backend/src/operations/Remote.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ public function __construct(\Slim\Container $c)

/**
* Update given record with password
*
* @param $record Name of the new record
* @param $content Type of the new record
* @param $password Content of the new record
*
*
* @param $record Record to update
* @param $content New content
* @param $disabled New disabled
* @param $password Password to authenticate
*
* @throws NotFoundException if the record does not exist
* @throws ForbiddenException if the password is not valid for the record
*/
public function updatePassword(int $record, string $content, string $password) : void
public function updatePassword(int $record, string $content, ? bool $disabled, string $password) : void
{
$query = $this->db->prepare('SELECT id FROM records WHERE id=:record');
$query->bindValue(':record', $record, \PDO::PARAM_INT);
Expand Down Expand Up @@ -63,21 +64,22 @@ public function updatePassword(int $record, string $content, string $password) :
}

$records = new \Operations\Records($this->c);
$records->updateRecord($record, null, null, $content, null, null);
$records->updateRecord($record, null, null, $content, null, null, $disabled);
}

/**
* Update given record with password
*
* @param $record Name of the new record
* @param $content Type of the new record
* Update given record with signature
*
* @param $record Record to update
* @param $content New content
* @param $disabled New disabled
* @param $time Timestamp of the signature
* @param $signature Signature
*
*
* @throws NotFoundException if the record does not exist
* @throws ForbiddenException if the signature is not valid for the record
*/
public function updateKey(int $record, string $content, int $time, string $signature) : void
public function updateKey(int $record, string $content, ? bool $disabled, int $time, string $signature) : void
{
$timestampWindow = $this->c['config']['remote']['timestampWindow'];

Expand All @@ -100,6 +102,11 @@ public function updateKey(int $record, string $content, int $time, string $signa
$validKeyFound = false;

$verifyString = $record . $content . $time;
if ($disabled !== null) {
$verifyString = $record . $content . intval($disabled) . $time;
}

$this->logger->info($verifyString);

while ($row = $query->fetch()) {
if (openssl_verify($verifyString, base64_decode($signature), $row['security'], OPENSSL_ALGO_SHA512)) {
Expand All @@ -113,6 +120,6 @@ public function updateKey(int $record, string $content, int $time, string $signa
}

$records = new \Operations\Records($this->c);
$records->updateRecord($record, null, null, $content, null, null);
$records->updateRecord($record, null, null, $content, null, null, $disabled);
}
}
30 changes: 20 additions & 10 deletions backend/test/tests/records-crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ test.run(async function () {
content: '1.2.3.4',
priority: 0,
ttl: 86400,
domain: 1
domain: 1,
disabled: false
}, 'Adding record return data fail.');

//Get not existing record
Expand All @@ -113,7 +114,8 @@ test.run(async function () {
content: '1.2.3.4',
priority: 0,
ttl: 86400,
domain: 1
domain: 1,
disabled: false
}, 'Record data should be the same it was created with.');

//Get created PTR record
Expand All @@ -130,7 +132,8 @@ test.run(async function () {
content: 'dns.example.com',
priority: 0,
ttl: 86400,
domain: 6
domain: 6,
disabled: false
}, 'PTR record has wrong data.');


Expand Down Expand Up @@ -160,7 +163,8 @@ test.run(async function () {
content: '1.2.3.4',
priority: 0,
ttl: 86400,
domain: 1
domain: 1,
disabled: false
}, 'Updated record has wrong data.');

//Get updated PTR record
Expand All @@ -177,7 +181,8 @@ test.run(async function () {
content: 'foo.example.com',
priority: 0,
ttl: 86400,
domain: 6
domain: 6,
disabled: false
}, 'PTR record has wrong data.');

//Delete not existing record
Expand Down Expand Up @@ -267,7 +272,8 @@ test.run(async function () {
content: '1.2.3.4',
priority: 0,
ttl: 86400,
domain: 1
domain: 1,
disabled: false
}, 'Adding record return data fail.');

//Get created record
Expand All @@ -284,7 +290,8 @@ test.run(async function () {
content: '1.2.3.4',
priority: 0,
ttl: 86400,
domain: 1
domain: 1,
disabled: false
}, 'Record data should be the same it was created with.');

//Get created PTR record
Expand All @@ -301,7 +308,8 @@ test.run(async function () {
content: 'dns.example.com',
priority: 0,
ttl: 86400,
domain: 6
domain: 6,
disabled: false
}, 'PTR record has wrong data.');


Expand Down Expand Up @@ -332,7 +340,8 @@ test.run(async function () {
content: '1.2.3.4',
priority: 0,
ttl: 60,
domain: 1
domain: 1,
disabled: false
}, 'Updated record has wrong data.');

//Get updated PTR record
Expand All @@ -349,7 +358,8 @@ test.run(async function () {
content: 'foo.example.com',
priority: 0,
ttl: 60,
domain: 6
domain: 6,
disabled: false
}, 'PTR record has wrong data.');

//Delete existing record
Expand Down
9 changes: 9 additions & 0 deletions backend/test/tests/records-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ test.run(async function () {
content: '::1',
priority: 0,
ttl: 86400,
disabled: 0,
domain: 1
},
{
Expand All @@ -92,6 +93,7 @@ test.run(async function () {
content: '9.8.7.6',
priority: 0,
ttl: 86400,
disabled: 0,
domain: 3
},
], 'Result fail for ' + res.config.url);
Expand All @@ -110,6 +112,7 @@ test.run(async function () {
content: 'foo bar baz',
priority: 10,
ttl: 60,
disabled: 0,
domain: 1
},
{
Expand All @@ -119,6 +122,7 @@ test.run(async function () {
content: '::1',
priority: 0,
ttl: 86400,
disabled: 0,
domain: 1
}], 'Result fail for ' + res.config.url);

Expand All @@ -137,6 +141,7 @@ test.run(async function () {
content: '12.34.56.78',
priority: 0,
ttl: 86400,
disabled: 0,
domain: 1
},
{
Expand All @@ -146,6 +151,7 @@ test.run(async function () {
content: '9.8.7.6',
priority: 0,
ttl: 86400,
disabled: 0,
domain: 3
}
], 'Result fail for ' + res.config.url);
Expand All @@ -167,6 +173,7 @@ test.run(async function () {
content: '12.34.56.78',
priority: 0,
ttl: 86400,
disabled: 0,
domain: 1
},
{
Expand All @@ -176,6 +183,7 @@ test.run(async function () {
content: 'foo bar baz',
priority: 10,
ttl: 60,
disabled: 0,
domain: 1
},
{
Expand All @@ -185,6 +193,7 @@ test.run(async function () {
content: '::1',
priority: 0,
ttl: 86400,
disabled: 0,
domain: 1
}
], 'Result fail for user on ' + res.config.url);
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/app/apitypes/Record.apitype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class RecordApitype {

public ttl = 0;

public disabled = false;

public domain = 0;

public new = false;
Expand Down
Loading

0 comments on commit 0aee7b7

Please sign in to comment.