ns.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php declare(strict_types=1);
  2. const NS_SOA_VALUES = [
  3. 'ttl' => 10800,
  4. 'email' => CONF['ns']['public_soa_email'],
  5. 'refresh' => 10800,
  6. 'retry' => 3600,
  7. 'expire' => 3628800,
  8. 'negative' => 10800,
  9. ];
  10. const NS_MIN_TTL = 300;
  11. const NS_DEFAULT_TTL = 10800;
  12. const NS_MAX_TTL = 1728000;
  13. const NS_ALLOWED_TYPES = ['AAAA', 'A', 'TXT', 'SRV', 'MX', 'SSHFP', 'TLSA', 'NS', 'DS', 'CSYNC', 'CAA', 'CNAME', 'DNAME', 'SVCB', 'HTTPS', 'LOC'];
  14. const NS_TEXTAREA_MAX_CHARACTERS = 10000;
  15. const NS_SYNC_TTL = 10800;
  16. function nsParseCommonRequirements(): array {
  17. nsCheckZonePossession($_POST['zone']);
  18. if (($_POST['subdomain'] === '') OR ($_POST['subdomain'] === '@'))
  19. $values['domain'] = $_POST['zone'];
  20. else
  21. $values['domain'] = formatAbsoluteDomain(formatEndWithDot($_POST['subdomain']) . $_POST['zone']);
  22. $values['ttl'] = intval($_POST['ttl-value'] * $_POST['ttl-multiplier']);
  23. if ($values['ttl'] < NS_MIN_TTL)
  24. output(403, sprintf(_('TTLs shorter than %s seconds are forbidden.'), NS_MIN_TTL));
  25. if ($values['ttl'] > NS_MAX_TTL)
  26. output(403, sprintf(_('TTLs longer than %s seconds are forbidden.'), NS_MAX_TTL));
  27. return $values;
  28. }
  29. function nsListUserZones(): array {
  30. if (isset($_SESSION['id']))
  31. return query('select', 'zones', ['username' => $_SESSION['id']], ['zone']);
  32. return [];
  33. }
  34. function nsCheckZonePossession(string $zone): void {
  35. checkAbsoluteDomainFormat($zone);
  36. if (!in_array($zone, nsListUserZones(), true))
  37. output(403, 'You don\'t own this zone on the name server.');
  38. }
  39. function nsDeleteZone(string $zone, string $user_id): void {
  40. // Remove from Knot configuration
  41. knotcConfExec([['conf-unset', 'zone[' . $zone . ']']]);
  42. // Remove Knot zone file
  43. if (unlink(CONF['ns']['knot_zones_path'] . '/' . $zone . 'zone') !== true)
  44. output(500, 'Failed to remove Knot zone file.');
  45. // Remove Knot related data
  46. exescape([
  47. CONF['dns']['knotc_path'],
  48. '--blocking',
  49. '--timeout',
  50. '3',
  51. '--force',
  52. '--',
  53. 'zone-purge',
  54. $zone,
  55. '+orphan',
  56. ], result_code: $code);
  57. if ($code !== 0)
  58. output(500, 'Failed to purge zone data.');
  59. query('delete', 'ns-syncs', ['destination' => $zone]);
  60. // Remove from database
  61. query('delete', 'zones', [
  62. 'zone' => $zone,
  63. 'username' => $user_id,
  64. ]);
  65. }
  66. function nsSync(string $source, string $destination): void {
  67. $zone_raw = file_get_contents(CONF['ns']['knot_zones_path'] . '/' . $destination . 'zone');
  68. if ($zone_raw === false)
  69. output(403, 'Unable to read zone file.');
  70. foreach (['AAAA', 'A', 'CAA'] as $type) {
  71. // Get source/distant records
  72. $results = kdig(name: $source, type: $type);
  73. if ($results['AD'] !== 1)
  74. throw new NoDnssecException($source . ' not DNSSEC-signed.');
  75. $source_records = array_column($results['answerRRs'] ?? [], 'rdata' . $type);
  76. // Get destination/local records
  77. $dest_records = array_column(parseZoneFile($zone_raw, [$type], $destination, false), 3);
  78. // Add source records that are not yet in destination
  79. foreach (array_diff($source_records, $dest_records) as $value_to_add)
  80. knotcZoneExec($destination, [
  81. $destination,
  82. NS_SYNC_TTL,
  83. $type,
  84. $value_to_add,
  85. ], 'add');
  86. // Delete destination records that are not part of source anymore
  87. foreach (array_diff($dest_records, $source_records) as $value_to_delete)
  88. knotcZoneExec($destination, [
  89. $destination,
  90. $type,
  91. $value_to_delete,
  92. ], 'delete');
  93. }
  94. }