zone-add.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php declare(strict_types=1);
  2. $domain = formatAbsoluteDomain($_POST['domain']);
  3. if (query('select', 'zones', ['zone' => $domain], ['zone']) !== [])
  4. output(403, _('This zone already exists on the service.'));
  5. $parent_domain = ltrim(strstr($domain, '.'), '.');
  6. $parent_authoritatives = array_column(kdig(name: $parent_domain, type: 'NS', server: (CONF['ns']['local_only_check'] ? CONF['reg']['address'] : NULL))['answerRRs'] ?? [], 'rdataNS');
  7. if ($parent_authoritatives === [])
  8. output(403, _('Parent zone\'s name servers not found.'));
  9. foreach ($parent_authoritatives as $parent_authoritative)
  10. checkAbsoluteDomainFormat($parent_authoritative);
  11. foreach ($parent_authoritatives as $i => $parent_authoritative) {
  12. if ($i === 3)
  13. output(403, sprintf(_('The %s first tried name servers failed to answer.'), $i));
  14. try {
  15. $results = kdig(name: $domain, type: 'NS', server: (CONF['ns']['local_only_check'] ? CONF['reg']['address'] : $parent_authoritative));
  16. } catch (KdigException) {
  17. continue;
  18. }
  19. break;
  20. }
  21. $ns_records = array_column($results['authorityRRs'] ?? [], 'rdataNS');
  22. if (preg_match('/^(?<salt>[0-9a-f]{8})-(?<hash>[0-9a-f]{32})\._domain-verification\.' . preg_quote(SERVER_NAME, '/') . '\.$/Dm', implode(LF, $ns_records), $matches) !== 1)
  23. output(403, _('NS authentication record not found.'));
  24. checkAuthToken($matches['salt'], $matches['hash']);
  25. rateLimit();
  26. insert('zones', [
  27. 'zone' => $domain,
  28. 'username' => $_SESSION['id'],
  29. ]);
  30. $zone_path = CONF['ns']['knot_zones_path'] . '/' . $domain . 'zone';
  31. $zone_content = implode(' ', [
  32. $domain,
  33. NS_SOA_VALUES['ttl'],
  34. 'SOA',
  35. CONF['ns']['servers'][0],
  36. NS_SOA_VALUES['email'],
  37. 1,
  38. NS_SOA_VALUES['refresh'],
  39. NS_SOA_VALUES['retry'],
  40. NS_SOA_VALUES['expire'],
  41. NS_SOA_VALUES['negative'],
  42. ]) . LF;
  43. foreach (CONF['ns']['servers'] as $server)
  44. $zone_content .= $domain . ' 86400 NS ' . $server . LF;
  45. $zone_content .= $domain . ' 86400 CSYNC 0 1 NS' . LF;
  46. if (file_put_contents($zone_path, $zone_content) === false)
  47. output(500, 'Failed to write new zone file.');
  48. if (chmod($zone_path, 0660) !== true)
  49. output(500, 'Failed to chmod new zone file.');
  50. knotcConfExec([
  51. ['conf-set', 'zone[' . $domain . ']'],
  52. ['conf-set', 'zone[' . $domain . '].template', 'servnest'],
  53. ]);
  54. output(200, _('Zone created.'));