2021-02-17 21:48:49 +00:00
|
|
|
<?php
|
|
|
|
|
2022-06-18 02:22:05 +00:00
|
|
|
function knotcConfExec($cmds) {
|
|
|
|
exec(CONF['dns']['knotc_path'] . " conf-begin", $output['begin'], $code['begin']);
|
|
|
|
if ($code['begin'] !== 0)
|
|
|
|
serverError("knotcConfExec: <code>knotc</code> failed with exit code <samp>" . $code['begin'] . "</samp>: <samp>" . $output['begin'][0] . "</samp>.");
|
|
|
|
|
|
|
|
foreach ($cmds as $cmd) {
|
|
|
|
exec(CONF['dns']['knotc_path'] . " conf-" . $cmd, $output['op'], $code['op']);
|
|
|
|
if ($code['op'] !== 0) {
|
|
|
|
exec(CONF['dns']['knotc_path'] . " conf-abort");
|
|
|
|
serverError("knotcConfExec: <code>knotc</code> failed with exit code <samp>" . $code['op'] . "</samp>: <samp>" . $output['op'][0] . "</samp>.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exec(CONF['dns']['knotc_path'] . " conf-commit", $output['commit'], $code['commit']);
|
|
|
|
if ($code['commit'] !== 0) {
|
|
|
|
exec(CONF['dns']['knotc_path'] . " conf-abort");
|
|
|
|
serverError("knotcConfExec: <code>knotc</code> failed with exit code <samp>" . $code['commit'] . "</samp>: <samp>" . $output['commit'][0] . "</samp>.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function knotcZoneExec($zone, $cmd) {
|
2022-05-24 23:16:41 +00:00
|
|
|
$action = checkAction($_POST['action']);
|
|
|
|
|
2022-06-18 02:22:05 +00:00
|
|
|
exec(CONF['dns']['knotc_path'] . " zone-begin " . $zone, $output['begin'], $code['begin']);
|
2022-05-24 23:16:41 +00:00
|
|
|
if ($code['begin'] !== 0)
|
2022-06-18 02:22:05 +00:00
|
|
|
serverError("knotcZoneExec: <code>knotc</code> failed with exit code <samp>" . $code['begin'] . "</samp>: <samp>" . $output['begin'][0] . "</samp>.");
|
2022-05-24 23:16:41 +00:00
|
|
|
|
2022-06-18 02:22:05 +00:00
|
|
|
exec(CONF['dns']['knotc_path'] . " zone-" . $action . "set " . $zone . " " . implode(" ", $cmd), $output['op'], $code['op']);
|
2022-06-09 22:38:05 +00:00
|
|
|
if ($code['op'] !== 0) {
|
2022-06-18 02:22:05 +00:00
|
|
|
exec(CONF['dns']['knotc_path'] . " zone-abort " . $zone);
|
|
|
|
serverError("knotcZoneExec: <code>knotc</code> failed with exit code <samp>" . $code['op'] . "</samp>: <samp>" . $output['op'][0] . "</samp>.");
|
2022-06-09 22:38:05 +00:00
|
|
|
}
|
2022-05-24 23:16:41 +00:00
|
|
|
|
2022-06-18 02:22:05 +00:00
|
|
|
exec(CONF['dns']['knotc_path'] . " zone-commit " . $zone, $output['commit'], $code['commit']);
|
2022-06-09 22:38:05 +00:00
|
|
|
if ($code['commit'] !== 0) {
|
2022-06-18 02:22:05 +00:00
|
|
|
exec(CONF['dns']['knotc_path'] . " zone-abort " . $zone);
|
|
|
|
serverError("knotcZoneExec: <code>knotc</code> failed with exit code <samp>" . $code['commit'] . "</samp>: <samp>" . $output['commit'][0] . "</samp>.");
|
2022-06-09 22:38:05 +00:00
|
|
|
}
|
2022-05-24 23:16:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 21:48:49 +00:00
|
|
|
function checkIpFormat($ip) {
|
2022-05-20 00:19:45 +00:00
|
|
|
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 21:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkAbsoluteDomainFormat($domain) {
|
2022-04-18 14:05:00 +00: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))
|
2022-05-20 00:19:45 +00:00
|
|
|
userError("Domain malformed.");
|
2021-02-17 21:48:49 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 13:30:18 +00:00
|
|
|
function formatEndWithDot($str) {
|
|
|
|
if (!str_ends_with($str, '.'))
|
|
|
|
$str .= '.';
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatAbsoluteDomain($domain) {
|
|
|
|
$domain = formatEndWithDot(strtolower($domain));
|
|
|
|
checkAbsoluteDomainFormat($domain);
|
|
|
|
return $domain;
|
|
|
|
}
|
|
|
|
|
2022-04-22 23:57:43 +00:00
|
|
|
function checkAction($action) {
|
2022-06-11 23:31:16 +00:00
|
|
|
return match ($action) {
|
|
|
|
'add' => '',
|
|
|
|
'delete' => 'un',
|
|
|
|
default => userError("Wrong value for action."),
|
|
|
|
};
|
2021-02-17 21:48:49 +00:00
|
|
|
}
|