add-http-dns.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. if (isset($_SESSION['username']))
  3. $dirsStatuses = dirsStatuses($_SESSION['username'], 'dns', 'http');
  4. else
  5. $dirsStatuses = [];
  6. if (processForm()) {
  7. $_POST['domain'] = formatDomain($_POST['domain']);
  8. if ($dirsStatuses[$_POST['dir']] !== false)
  9. output(403, 'Wrong value for <code>dir</code>.');
  10. if (query('select', 'sites', ['domain' => $_POST['domain']], 'domain') !== [])
  11. output(403, 'Ce domaine existe déjà sur ce service.');
  12. $remoteAaaaRecords = dns_get_record($_POST['domain'], DNS_AAAA);
  13. if (is_array($remoteAaaaRecords) !== true)
  14. output(500, 'Erreur lors de la récupération de l\'enregistrement AAAA.');
  15. if (equalArrays([CONF['ht']['ipv6_address']], array_column($remoteAaaaRecords, 'ipv6')) !== true)
  16. output(403, 'Ce domaine doit avoir pour unique enregistrement AAAA <code>' . CONF['ht']['ipv6_address'] . '</code>.');
  17. $remoteARecords = dns_get_record($_POST['domain'], DNS_A);
  18. if (is_array($remoteARecords) !== true)
  19. output(500, 'Erreur lors de la récupération de l\'enregistrement A.');
  20. if (equalArrays([CONF['ht']['ipv4_address']], array_column($remoteARecords, 'ip')) !== true)
  21. output(403, 'Ce domaine doit avoir pour unique enregistrement A <code>' . CONF['ht']['ipv4_address'] . '</code>.');
  22. $remoteTXTRecords = dns_get_record($_POST['domain'], DNS_TXT);
  23. if (is_array($remoteTXTRecords) !== true)
  24. output(500, 'Erreur lors de la récupération de l\'enregistrement TXT.');
  25. if (preg_match('/^auth-owner=([0-9a-f]{8})-([0-9a-f]{32})$/m', implode(LF, array_column($remoteTXTRecords, 'txt')), $matches) !== 1)
  26. output(403, 'Aucun enregistrement TXT au format correct trouvé.');
  27. checkAuthToken($matches[1], $matches[2]);
  28. rateLimit();
  29. addSite($_SESSION['username'], $_POST['dir'], $_POST['domain'], 'dns', 'http');
  30. exec('2>&1 ' . CONF['ht']['sudo_path'] . ' ' . CONF['ht']['certbot_path'] . ' certonly' . (($_SESSION['type'] === 'trusted') ? '' : ' --test-cert') . ' --key-type rsa --rsa-key-size 3072 --webroot --webroot-path /srv/niver/acme --domain ' . $_POST['domain'], $output, $returnCode);
  31. if ($returnCode !== 0)
  32. output(500, 'Certbot failed to get a Let\'s Encrypt certificate.', $output);
  33. $nginxConf = 'server {
  34. listen [' . CONF['ht']['ipv6_listen_address'] . ']:' . CONF['ht']['https_port'] . ' ssl http2;
  35. listen ' . CONF['ht']['ipv4_listen_address'] . ':' . CONF['ht']['https_port'] . ' ssl http2;
  36. server_name ' . $_POST['domain'] . ';
  37. root ' . CONF['ht']['ht_path'] . '/' . $_SESSION['username'] . '/' . $_POST['dir'] . ';
  38. ssl_certificate /etc/letsencrypt/live/' . $_POST['domain'] . '/fullchain.pem;
  39. ssl_certificate_key /etc/letsencrypt/live/' . $_POST['domain'] . '/privkey.pem;
  40. include inc/ht-tls.conf;
  41. }
  42. ';
  43. if (file_put_contents(CONF['ht']['nginx_config_path'] . '/' . $_POST['domain'] . '.conf', $nginxConf) === false)
  44. output(500, 'Failed to write Nginx configuration.');
  45. // Reload Nginx
  46. exec(CONF['ht']['sudo_path'] . ' ' . CONF['ht']['systemctl_path'] . ' reload nginx', result_code: $code);
  47. if ($code !== 0)
  48. output(500, 'Failed to reload Nginx.');
  49. output(200, 'Accès HTTP par domaine ajouté sur ce dossier !');
  50. }
  51. $proof = getAuthToken();
  52. ?>
  53. <p>
  54. Ajouter sur un dossier de site un accès <?= linkToDocs('http', 'HTTP') ?> par <?= linkToDocs('dns', 'DNS') ?> et <?= linkToDocs('tls', 'TLS') ?> <?= linkToDocs('ca', 'authentifié par <em>Let\'s Encrypt</em>') ?>.
  55. </p>
  56. <p>
  57. La présence des enregistrements ci-après sera vérifiée lors du traitement de ce formulaire.
  58. </p>
  59. <dl>
  60. <dt><code>AAAA</code></dt>
  61. <dd>
  62. <code><?= CONF['ht']['ipv6_address'] ?></code>
  63. </dd>
  64. <dt><code>A</code></dt>
  65. <dd>
  66. <code><?= CONF['ht']['ipv4_address'] ?></code>
  67. </dd>
  68. <dt><code>TXT</code></dt>
  69. <dd>
  70. <code>auth-owner=<?= $proof ?></code>
  71. </dd>
  72. </dl>
  73. <form method="post">
  74. <label for="domain">Domaine sur lequel répondre</label><br>
  75. <input required="" placeholder="site.<?= PLACEHOLDER_DOMAIN ?>" id="domain" name="domain" type="text"><br>
  76. <label for="dir">Dossier ciblé</label><br>
  77. <select required="" name="dir" id="dir">
  78. <option value="" disabled="" selected="">---</option>
  79. <?php
  80. foreach ($dirsStatuses as $dir => $alreadyEnabled)
  81. echo ' <option' . ($alreadyEnabled ? ' disabled=""' : '') . ' value="' . $dir . '">' . $dir . '</option>' . LF;
  82. ?>
  83. </select>
  84. <br>
  85. <input value="Valider" type="submit">
  86. </form>