diff --git a/server/plugins-available/powerdns_plugin.inc.php b/server/plugins-available/powerdns_plugin.inc.php new file mode 100644 index 0000000000000000000000000000000000000000..7575554e193a020f1227e2dda9c2eea3433e9f75 --- /dev/null +++ b/server/plugins-available/powerdns_plugin.inc.php @@ -0,0 +1,349 @@ +plugins->registerEvent('dns_soa_insert',$this->plugin_name,'soa_insert'); + $app->plugins->registerEvent('dns_soa_update',$this->plugin_name,'soa_update'); + $app->plugins->registerEvent('dns_soa_delete',$this->plugin_name,'soa_delete'); + + //* RR + $app->plugins->registerEvent('dns_rr_insert',$this->plugin_name,'rr_insert'); + $app->plugins->registerEvent('dns_rr_update',$this->plugin_name,'rr_update'); + $app->plugins->registerEvent('dns_rr_delete',$this->plugin_name,'rr_delete'); + + } + + + function soa_insert($event_name,$data) { + global $app, $conf; + + if($data["new"]["active"] != 'Y') return; + + $origin = substr($data["new"]["origin"], 0, -1); + $ispconfig_id = $data["new"]["id"]; + $app->db->query("INSERT INTO powerdns.domains (name, type, ispconfig_id) VALUES ('$origin', 'NATIVE', $ispconfig_id)"); + $zone_id = mysql_insert_id(); + if(substr($data["new"]["ns"], -1) == '.'){ + $ns = substr($data["new"]["ns"], 0, -1); + } else { + $ns = $data["new"]["ns"].'.'.$origin; + } + if($ns == '') $ns = $origin; + + $hostmaster = substr($data["new"]["mbox"], 0, -1); + $content = $ns.' '.$hostmaster.' 0'; + $ttl = $data["new"]["ttl"]; + + $app->db->query("INSERT INTO powerdns.records (domain_id, name, type, content, ttl, prio, change_date, ispconfig_id) VALUES ($zone_id, '$origin', 'SOA', '$content', $ttl, 0, ".time().", $ispconfig_id)"); + + } + + function soa_update($event_name,$data) { + global $app, $conf; + + if($data["new"]["active"] != 'Y'){ + if($data["old"]["active"] != 'Y') return; + $this->soa_delete($event_name,$data); + } else { + if($data["old"]["active"] == 'Y'){ + $origin = substr($data["new"]["origin"], 0, -1); + $ispconfig_id = $data["new"]["id"]; + $app->db->query("UPDATE powerdns.domains SET name = '$origin' WHERE ispconfig_id = $ispconfig_id"); + + if(substr($data["new"]["ns"], -1) == '.'){ + $ns = substr($data["new"]["ns"], 0, -1); + } else { + $ns = $data["new"]["ns"].'.'.$origin; + } + if($ns == '') $ns = $origin; + + $hostmaster = substr($data["new"]["mbox"], 0, -1); + $content = $ns.' '.$hostmaster.' 0'; + $ttl = $data["new"]["ttl"]; + $app->db->query("UPDATE powerdns.records SET name = '$origin', content = '$content', ttl = $ttl, change_date = ".time()." WHERE ispconfig_id = ".$data["new"]["id"]." AND type = 'SOA'"); + } else { + $this->soa_insert($event_name,$data); + $ispconfig_id = $data["new"]["id"]; + if($records = $app->db->queryAllRecords("SELECT * FROM dns_rr WHERE zone = $ispconfig_id AND active = 'Y'")){ + foreach($records as $record){ + foreach($record as $key => $val){ + $data["new"][$key] = $val; + } + $this->rr_insert("dns_rr_insert", $data); + } + } + + } + } + + } + + function soa_delete($event_name,$data) { + global $app, $conf; + + $zone = $app->db->queryOneRecord("SELECT * FROM powerdns.domains WHERE ispconfig_id = ".$data["old"]["id"]); + $zone_id = $zone["id"]; + $app->db->query("DELETE FROM powerdns.records WHERE domain_id = $zone_id"); + $app->db->query("DELETE FROM powerdns.domains WHERE id = $zone_id"); + + } + + function rr_insert($event_name,$data) { + global $app, $conf; + if($data["new"]["active"] != 'Y') return; + + $zone = $app->db->queryOneRecord("SELECT * FROM dns_soa WHERE id = ".$data["new"]["zone"]); + $origin = substr($zone["origin"], 0, -1); + $powerdns_zone = $app->db->queryOneRecord("SELECT * FROM powerdns.domains WHERE ispconfig_id = ".$data["new"]["zone"]); + $zone_id = $powerdns_zone["id"]; + + $type = $data["new"]["type"]; + + switch ($type) { + case "PTR": + $name = $data["new"]["name"]; + break; + default: + if(substr($data["new"]["name"], -1) == '.'){ + $name = substr($data["new"]["name"], 0, -1); + } else { + if($data["new"]["name"] == ""){ + $name = $origin; + } else { + $name = $data["new"]["name"].'.'.$origin; + } + } + if($name == '') $name = $origin; + } + + switch ($type) { + case "CNAME": + case "MX": + case "NS": + case "ALIAS": + case "PTR": + case "SRV": + if(substr($data["new"]["data"], -1) == '.'){ + $content = substr($data["new"]["data"], 0, -1); + } else { + $content = $data["new"]["data"].'.'.$origin; + } + break; + case "HINFO": + $content = $data["new"]["data"]; + $quote1 = strpos($content, '"'); + if($quote1 !== FALSE){ + $quote2 = strpos(substr($content, ($quote1 + 1)), '"'); + } + if($quote1 !== FALSE && $quote2 !== FALSE){ + $text_between_quotes = str_replace(' ', '_', substr($content, ($quote1 + 1), (($quote2 - $quote1)))); + $content = $text_between_quotes.substr($content, ($quote2 + 2)); + } + break; + default: + $content = $data["new"]["data"]; + } + + $ttl = $data["new"]["ttl"]; + $prio = $data["new"]["aux"]; + $change_date = time(); + $ispconfig_id = $data["new"]["id"]; + + $app->db->query("INSERT INTO powerdns.records (domain_id, name, type, content, ttl, prio, change_date, ispconfig_id) VALUES ($zone_id, '$name', '$type', '$content', $ttl, $prio, $change_date, $ispconfig_id)"); + + } + + function rr_update($event_name,$data) { + global $app, $conf; + + if($data["new"]["active"] != 'Y'){ + if($data["old"]["active"] != 'Y') return; + $this->rr_delete($event_name,$data); + } else { + if($data["old"]["active"] == 'Y'){ + $zone = $app->db->queryOneRecord("SELECT * FROM dns_soa WHERE id = ".$data["new"]["zone"]); + $origin = substr($zone["origin"], 0, -1); + $powerdns_zone = $app->db->queryOneRecord("SELECT * FROM powerdns.domains WHERE ispconfig_id = ".$data["new"]["zone"]); + $zone_id = $powerdns_zone["id"]; + + $type = $data["new"]["type"]; + + switch ($type) { + case "PTR": + $name = $data["new"]["name"]; + break; + default: + if(substr($data["new"]["name"], -1) == '.'){ + $name = substr($data["new"]["name"], 0, -1); + } else { + if($data["new"]["name"] == ""){ + $name = $origin; + } else { + $name = $data["new"]["name"].'.'.$origin; + } + } + if($name == '') $name = $origin; + } + + switch ($type) { + case "CNAME": + case "MX": + case "NS": + case "ALIAS": + case "PTR": + case "SRV": + if(substr($data["new"]["data"], -1) == '.'){ + $content = substr($data["new"]["data"], 0, -1); + } else { + $content = $data["new"]["data"].'.'.$origin; + } + break; + case "HINFO": + $content = $data["new"]["data"]; + $quote1 = strpos($content, '"'); + if($quote1 !== FALSE){ + $quote2 = strpos(substr($content, ($quote1 + 1)), '"'); + } + if($quote1 !== FALSE && $quote2 !== FALSE){ + $text_between_quotes = str_replace(' ', '_', substr($content, ($quote1 + 1), (($quote2 - $quote1)))); + $content = $text_between_quotes.substr($content, ($quote2 + 2)); + } + break; + default: + $content = $data["new"]["data"]; + } + + $ttl = $data["new"]["ttl"]; + $prio = $data["new"]["aux"]; + $change_date = time(); + $ispconfig_id = $data["new"]["id"]; + //echo "UPDATE powerdns.records SET name = '$name', type = '$type', content = '$content', ttl = $ttl, prio = $prio, change_date = ".time()." WHERE id = $record_id"; + $app->db->query("UPDATE powerdns.records SET name = '$name', type = '$type', content = '$content', ttl = $ttl, prio = $prio, change_date = ".time()." WHERE ispconfig_id = $ispconfig_id AND type != 'SOA'"); + + } else { + $this->rr_insert($event_name,$data); + } + } + + } + + function rr_delete($event_name,$data) { + global $app, $conf; + + $ispconfig_id = $data["old"]["id"]; + $app->db->query("DELETE FROM powerdns.records WHERE ispconfig_id = $ispconfig_id AND type != 'SOA'"); + + } + + + + +} // end class + +?> \ No newline at end of file