servnest/fn/dns.php

48 lines
1.8 KiB
PHP
Raw Normal View History

2021-02-17 22:48:49 +01:00
<?php
2022-05-25 01:16:41 +02:00
function knotcExec($suffix, $cmd) {
$action = checkAction($_POST['action']);
exec(CONF['dns']['knotc_path'] . " zone-begin " . $suffix, $output['begin'], $code['begin']);
if ($code['begin'] !== 0)
serverError("<code>knotc</code> failed with exit code <samp>" . $code['begin'] . "</samp>: <samp>" . $output['begin'][0] . "</samp>.");
exec(CONF['dns']['knotc_path'] . " zone-" . $action . "set " . $suffix . " " . implode(" ", $cmd), $output['op'], $code['op']);
2022-06-10 00:38:05 +02:00
if ($code['op'] !== 0) {
exec(CONF['dns']['knotc_path'] . " zone-abort " . $suffix);
2022-05-25 01:16:41 +02:00
serverError("<code>knotc</code> failed with exit code <samp>" . $code['op'] . "</samp>: <samp>" . $output['op'][0] . "</samp>.");
2022-06-10 00:38:05 +02:00
}
2022-05-25 01:16:41 +02:00
exec(CONF['dns']['knotc_path'] . " zone-commit " . $suffix, $output['commit'], $code['commit']);
2022-06-10 00:38:05 +02:00
if ($code['commit'] !== 0) {
exec(CONF['dns']['knotc_path'] . " zone-abort " . $suffix);
2022-05-25 01:16:41 +02:00
serverError("<code>knotc</code> failed with exit code <samp>" . $code['commit'] . "</samp>: <samp>" . $output['commit'][0] . "</samp>.");
2022-06-10 00:38:05 +02:00
}
2022-05-25 01:16:41 +02:00
}
2021-02-17 22:48:49 +01:00
function checkIpFormat($ip) {
2022-04-18 16:05:00 +02:00
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE))
userError("IP address is on the private range.");
2022-04-18 16:05:00 +02:00
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE))
userError("IP address is on the reserved range.");
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
return "A";
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
return "AAAA";
userError("IP address malformed.");
2021-02-17 22:48:49 +01:00
}
function checkAbsoluteDomainFormat($domain) {
2022-04-18 16:05:00 +02:00
// If the domain must end with a dot
if (!filter_var($domain, FILTER_VALIDATE_DOMAIN) OR !preg_match("/^([a-z0-9_-]{1,63}\.){2,127}$/", $domain))
userError("Domain malformed.");
2021-02-17 22:48:49 +01:00
}
2022-04-23 01:57:43 +02:00
function checkAction($action) {
2022-06-12 01:31:16 +02:00
return match ($action) {
'add' => '',
'delete' => 'un',
default => userError("Wrong value for action."),
};
2021-02-17 22:48:49 +01:00
}