servnest/fn/ns.php

84 lines
2.1 KiB
PHP
Raw Normal View History

2023-07-17 21:15:18 +02:00
<?php declare(strict_types=1);
2021-02-19 13:23:26 +01:00
2023-05-06 02:39:19 +02:00
const SOA_VALUES = [
'ttl' => 10800,
'email' => CONF['ns']['public_soa_email'],
'refresh' => 10800,
'retry' => 3600,
'expire' => 3628800,
'negative' => 10800,
2023-05-06 02:39:19 +02:00
];
2023-05-06 02:39:19 +02:00
const MIN_TTL = 300;
const DEFAULT_TTL = 10800;
const MAX_TTL = 1728000;
2023-07-05 19:53:39 +02:00
const ALLOWED_TYPES = ['AAAA', 'A', 'TXT', 'SRV', 'MX', 'SSHFP', 'TLSA', 'NS', 'DS', 'CSYNC', 'CAA', 'CNAME', 'DNAME', 'SVCB', 'HTTPS', 'LOC'];
2023-05-06 02:39:19 +02:00
const ZONE_MAX_CHARACTERS = 10000;
2021-05-16 16:55:39 +02:00
2023-06-26 04:13:52 +02:00
const SYNC_TTL = 10800;
2023-06-24 16:54:36 +02:00
2023-06-20 00:36:58 +02:00
function nsParseCommonRequirements(): array {
2022-04-18 16:05:00 +02:00
nsCheckZonePossession($_POST['zone']);
if (($_POST['subdomain'] === '') OR ($_POST['subdomain'] === '@'))
2022-04-18 16:05:00 +02:00
$values['domain'] = $_POST['zone'];
else
$values['domain'] = formatAbsoluteDomain(formatEndWithDot($_POST['subdomain']) . $_POST['zone']);
2021-05-16 16:55:39 +02:00
2023-05-06 02:39:19 +02:00
$values['ttl'] = intval($_POST['ttl-value'] * $_POST['ttl-multiplier']);
2021-03-02 22:56:38 +01:00
if ($values['ttl'] < MIN_TTL)
output(403, sprintf(_('TTLs shorter than %s seconds are forbidden.'), MIN_TTL));
if ($values['ttl'] > MAX_TTL)
output(403, sprintf(_('TTLs longer than %s seconds are forbidden.'), MAX_TTL));
2021-03-02 22:56:38 +01:00
2022-04-18 16:05:00 +02:00
return $values;
2021-03-02 22:56:38 +01:00
}
2023-06-20 00:36:58 +02:00
function nsListUserZones(): array {
if (isset($_SESSION['id']))
return query('select', 'zones', ['username' => $_SESSION['id']], 'zone');
return [];
}
2023-06-20 00:36:58 +02:00
function nsCheckZonePossession(string $zone): void {
checkAbsoluteDomainFormat($zone);
2021-02-19 13:23:26 +01:00
if (!in_array($zone, nsListUserZones(), true))
output(403, 'You don\'t own this zone on the name server.');
2021-02-19 13:23:26 +01:00
}
2023-06-20 00:36:58 +02:00
function nsDeleteZone(string $zone, string $user_id): void {
// Remove from Knot configuration
knotcConfExec([['conf-unset', 'zone[' . $zone . ']']]);
2023-05-06 02:39:19 +02:00
// Remove Knot zone file
if (unlink(CONF['ns']['knot_zones_path'] . '/' . $zone . 'zone') !== true)
output(500, 'Failed to remove Knot zone file.');
// Remove Knot related data
exescape([
CONF['dns']['knotc_path'],
'--blocking',
'--timeout',
'3',
'--force',
'--',
'zone-purge',
$zone,
'+orphan',
], result_code: $code);
2023-05-06 02:39:19 +02:00
if ($code !== 0)
output(500, 'Failed to purge zone data.');
2023-06-26 04:13:52 +02:00
query('delete', 'ns-syncs', ['destination' => $zone]);
// Remove from database
query('delete', 'zones', [
'zone' => $zone,
'username' => $user_id,
]);
}