3 * @author Jan Altensen (Stricted)
4 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
5 * @copyright 2014-2015 Jan Altensen (Stricted)
7 $data = file_get_contents("https://dns-control.eu/API/?key=xxx");
8 $data = json_decode($data, true);
9 if (is_array($data) && !isset($data['error'])) {
10 shell_exec("rm -rf /srv/bind/*");
12 foreach ($data as $zone) {
13 $out = $zone['soa']['origin']."\t".$zone['soa']['minimum']."\tIN\tSOA\t".$zone['soa']['ns']." ".$zone['soa']['mbox']." (\n";
14 $out .= "\t\t\t\t".$zone['soa']['serial']."\t; Serial\n";
15 $out .= "\t\t\t\t".$zone['soa']['refresh']."\t\t; Refresh\n";
16 $out .= "\t\t\t\t".$zone['soa']['retry']."\t\t; Retry\n";
17 $out .= "\t\t\t\t".$zone['soa']['expire']."\t\t; Expire\n";
18 $out .= "\t\t\t\t180 )\t\t; Negative Cache TTL\n";
24 foreach ($zone['rr'] as $record) {
25 if ($record['type'] == "DNSKEY") {
26 if ($record['aux'] == 256) {
29 else if ($record['aux'] == 257) {
33 $out .= $record['name']."\t".$record['ttl']."\tIN\t".$record['type']."\t".$record['aux']."\t".$record['data']."\n";
35 else if ($record['type'] == "MX" ||
$record['type'] == "SRV" ||
$record['type'] == "TLSA" ||
$record['type'] == "DS") {
36 $out .= $record['name']."\t".$record['ttl']."\tIN\t".$record['type']."\t".$record['aux']."\t".$record['data']."\n";
38 else if ($record['type'] == "TXT") {
39 $txt = $record['data'];
41 if (strpos($txt, " ") !== false) {
42 if (substr($txt, -1) != '"' && substr($txt, 0, 1) != '"') {
43 $record['data'] = '"'.$txt.'"';
47 if (strpos($record['data'], "v=spf1") !== false) {
48 $out .= $record['name']."\t".$record['ttl']."\tIN\tSPF\t" . $record['data']."\n";
51 $out .= $record['name']."\t".$record['ttl']."\tIN\t".$record['type']."\t" . $record['data']."\n";
54 $out .= $record['name']."\t".$record['ttl']."\tIN\t".$record['type']."\t\t" . $record['data']."\n";
60 foreach ($zone['sec'] as $sec) {
61 $dir = "/srv/bind/dnssec/".$zone['soa']['origin']."/";
62 if (!file_exists($dir)) {
63 shell_exec("mkdir -p ".$dir);
66 if ($sec['type'] == "ZSK" ||
$sec['type'] == "KSK") {
67 if (!empty($sec['public']) && !empty($sec['private'])) {
68 preg_match("/; This is a (key|zone)-signing key, keyid ([0-9]+), for ".$zone['soa']['origin']."/i", $sec['public'], $match);
69 $filename1 = getFileName ($zone['soa']['origin'], $sec['algo'], $match[2], "pub");
70 $filename2 = getFileName ($zone['soa']['origin'], $sec['algo'], $match[2], "priv");
72 if (file_exists($dir.$filename1)) {
73 unlink($dir.$filename1);
76 if (file_exists($dir.$filename2)) {
77 unlink($dir.$filename2);
80 $handler = fOpen($dir.$filename1, "a+");
81 fWrite($handler, $sec['public']);
84 $handler = fOpen($dir.$filename2, "a+");
85 fWrite($handler, $sec['private']);
88 if (file_exists($dir.$filename1) && file_exists($dir.$filename2)) {
89 /* fallback for missing DNSKEY record */
90 if ($zsk === false ||
$ksk === false) {
91 preg_match("/".$zone['soa']['origin']." IN DNSKEY ([0-9]+) ([0-9]+) ([0-9]+) ([\s\S]+)/i", $sec['public'], $match);
92 $out .= $zone['soa']['origin']."\t60\tIN\tDNSKEY\t".$match[1]."\t".$match[2]." ".$match[3]." ".$match[4]."\n";
93 if ($sec['type'] == "ZSK") {
96 else if ($sec['type'] == "KSK") {
101 if ($sec['type'] == "ZSK") {
104 else if ($sec['type'] == "KSK") {
113 if ($zsk === true && $ksk === true && $zskkey === true && $kskkey === true) {
117 $handler = fOpen("/srv/bind/".$zone['soa']['origin']."db", "a+");
118 fWrite($handler, $out);
122 if ($sign === true) {
123 shell_exec("cd /srv/bind/ && /usr/sbin/dnssec-signzone -r /dev/urandom -A -N INCREMENT -K /srv/bind/dnssec/".$zone['soa']['origin']."/ -o ".$zone['soa']['origin']." -t ".$zone['soa']['origin']."db");
124 if (file_exists("/srv/bind/".$zone['soa']['origin']."db.signed")) {
129 $cout = "zone \"" . $zone['soa']['origin'] . "\" {\n";
130 $cout .= "\ttype master;\n";
131 $cout .= "\tnotify no;\n";
132 $cout .= "\tfile \"/srv/bind/".$zone['soa']['origin']."db".($signed === true ?
".signed" : "")."\";\n";
135 $handler = fOpen("/srv/bind/domains.cfg", "a+");
136 fWrite($handler, $cout);
140 shell_exec("/etc/init.d/bind9 reload");
143 function getFileName ($zone, $algo, $id, $type) {
148 else if ($len == "2") {
151 else if ($len == "3") {
154 else if ($len == "4") {
157 if ($type == "pub") {
160 else if ($type == "priv") {
167 else if ($algo == "10") {
171 return "K".$zone."+".$algo."+".$id.".".$type;