From ca4ddb866b288689082b5f2b7bf738c2c3fc4846 Mon Sep 17 00:00:00 2001 From: hickert Date: Wed, 1 Feb 2006 09:32:25 +0000 Subject: [PATCH] Added network device dns git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@2610 594d385d-05f5-0310-b6e9-bd551577e9d8 --- plugins/admin/systems/class_termDNS.inc | 392 +++++++++++++++++++++++- plugins/admin/systems/network.tpl | 93 ++++-- plugins/admin/systems/termdns.tpl | 3 - 3 files changed, 456 insertions(+), 32 deletions(-) delete mode 100644 plugins/admin/systems/termdns.tpl diff --git a/plugins/admin/systems/class_termDNS.inc b/plugins/admin/systems/class_termDNS.inc index f3cfc4ed6..156cafee9 100644 --- a/plugins/admin/systems/class_termDNS.inc +++ b/plugins/admin/systems/class_termDNS.inc @@ -9,26 +9,166 @@ class termDNS extends plugin /* attribute list for save action */ var $ignore_account= TRUE; + var $DNSattributes = array("dNSClass","zoneName","dNSTTL"); var $attributes= array("ipHostNumber","macAddress"); var $objectclasses= array("whatever"); - var $ipHostNumber =""; - var $macAddress =""; + var $ipHostNumber =""; // IP address + var $macAddress =""; // Mac address + var $cn =""; // CN of currently edited device + + var $Zones = array(); // All Available Zones like array("3.2.1.in-addr.arpa"=>"MyServer.de") + var $RecordTypes= array(); // Possible record types + + var $dNSClass = "IN"; // dNSClass name + var $zoneName = ""; // Used ZoneName + var $dNSTTL = 7200; // TTL settings for the created entries + + /* Used records */ + var $types = array(); + var $DNSinitially_was_account = false; function termDNS ($config, $dn,$objectClasses) { + /* We need to know which objectClasses are used, to store the ip/mac*/ $this->objectclasses= $objectClasses; plugin::plugin ($config, $dn); + + /* All types with required attrs */ + $this->RecordTypes['aRecord'] ="aRecord"; + $this->RecordTypes['MDRecord'] =""; + $this->RecordTypes['mXRecord'] ="mXRecord"; + $this->RecordTypes['nSRecord'] ="nSRecord"; + $this->RecordTypes['pTRRecord'] ="relativeDomainName"; + $this->RecordTypes['HINFORecord'] =""; + $this->RecordTypes['MINFORecord'] =""; + $this->RecordTypes['cNAMERecord'] ="relativeDomainName"; + $this->RecordTypes['tXTRecord'] ="tXTRecord"; + $this->RecordTypes['AFSDBRecord'] =""; + $this->RecordTypes['SIGRecord'] =""; + $this->RecordTypes['KEYRecord'] =""; + $this->RecordTypes['AAAARecord'] =""; + $this->RecordTypes['LOCRecord'] =""; + $this->RecordTypes['NXTRecord'] =""; + $this->RecordTypes['SRVRecord'] =""; + $this->RecordTypes['NAPTRRecord'] =""; + $this->RecordTypes['KXRecord'] =""; + $this->RecordTypes['CERTRecord'] =""; + $this->RecordTypes['A6Record'] =""; + $this->RecordTypes['DSRecord'] =""; + $this->RecordTypes['SSHFPRecord'] =""; + $this->RecordTypes['RRSIGRecord'] =""; + $this->RecordTypes['NSECRecord'] =""; + + /* Get all available zones */ + $this->cn = $this->attrs['cn'][0]; + $this->Zones = $this->get_Zones(); + $types = array(); + + /* Get all records */ + $ldap = $this->config->get_ldap_link(); + $ldap->cd($this->dn); + $ldap->search("(&(objectClass=dNSZone)(zoneName=*))",array("*")); + + while($attrs = $ldap->fetch()){ + /* If relative domainname == cn + * Try to read dnsclass / TTl / zone + */ + if($attrs['relativeDomainName'][0] == $this->cn){ + /* Get class */ + if(isset($attrs['dNSClass'][0])){ + $this->dNSClass = $attrs['dNSClass'][0]; + } + /* Get Zone*/ + if(isset($attrs['zoneName'][0])){ + $this->zoneName = $attrs['zoneName'][0]; + } + /* Get ttl */ + if(isset($attrs['dNSTTL'][0])){ + $this->dNSTTL = $attrs['dNSTTL'][0]; + } + } + + /* Create list with all used records */ + foreach($this->RecordTypes as $name => $value){ + + /* If there is a record attribute */ + if(isset($attrs[$name])){ + + /* get all entries */ + for($i = 0 ; $i < $attrs[$value]['count']; $i ++){ + $types[] =array("type"=>$name,"value"=>$attrs[$value][$i],"status"=>"edited","dn"=>$attrs['dn']); + } + } + } + } + + /* If there is at least one entry in this -> types, we have DNS enabled */ + $this->types = $types; + if(count($this->types) == 0){ + $this->DNS_is_account = false; + }else{ + $this->DNS_is_account = true; + } + + /* Store initally account settings */ + $this->DNSinitially_was_account = $this->DNS_is_account; } function execute() { - /* Call parent execute */ + /* Call parent execute */ $smarty= get_smarty(); $display= ""; + + /* Add new empty array with status new, to our record list */ + if(isset($_POST['AddNewRecord'])){ + $this->types[] =array("type"=>"aRecord","value"=>"","status"=>"new"); + } + + /* Handle all posts */ + $only_once =true; + foreach($_POST as $name => $value){ + + /* Check if we have to delete a record entry */ + if((preg_match("/RemoveRecord_/",$name))&&($only_once)) { + + /* Avoid performing this once again */ + $only_once = false; + + /* Extract id for specified entry */ + $id = preg_replace("/RemoveRecord_/","",$name); + $id = preg_replace("/_.*$/","",$id); + + /* Delete this record, mark edited entries to be able to delete them */ + if(isset($this->types[$id])){ + if($this->types[$id]['status'] == "edited"){ + $this->types[$id]['status'] = "deleted"; + }else{ + unset($this->types[$id]); + } + } + } + } + + /* Assign smarty all non DNs attributes */ foreach($this->attributes as $attr){ $smarty->assign($attr,$this->$attr); } + + /* Assign smarty all DNS attributes */ + foreach($this->DNSattributes as $attr){ + $smarty->assign($attr,$this->$attr); + } + + /* Assign all needed vars */ + $smarty->assign("DNSAccount",$this->DNS_is_account); + $smarty->assign("Zones",$this->Zones); + $smarty->assign("ZoneKeys",($this->Zones)); + $changeStateForRecords =""; + $smarty->assign("records",$this->generateRecordsList(&$changeStateForRecords)); + $smarty->assign("changeStateForRecords",$changeStateForRecords); + $smarty->assign("dNSClasses",array("IN"=>"IN")); $smarty->assign("staticAddress","*"); $display.= $smarty->fetch(get_template_path('network.tpl', TRUE)); return($display); @@ -36,19 +176,52 @@ class termDNS extends plugin function remove_from_parent() { - /* This cannot be removed... */ + /* This cannot be removed... */ } - /* Save data to object */ function save_object() { + /* Save all posted vars */ plugin::save_object(); + + /* Ge all non dns attributes (IP/MAC)*/ foreach($this->attributes as $attr){ if(isset($_POST[$attr])){ $this->$attr = $_POST[$attr]; } } + + /* Get dns attributes */ + if(isset($_POST['network_tpl_posted'])){ + + /* Check for posted record changes */ + foreach($this->types as $key => $value){ + + /* Check if type has changed */ + if(isset($_POST['RecordTypeSelectedFor_'.$key])){ + $this->types[$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key]; + } + /* Check if value has changed */ + if(isset($_POST['RecordValue_'.$key])){ + $this->types[$key]['value'] = $_POST['RecordValue_'.$key]; + } + } + + /* Get all basic DNS attributes (TTL, Clas ..)*/ + foreach($this->DNSattributes as $attr){ + if(isset($_POST[$attr])){ + $this->$attr = $_POST[$attr]; + } + } + + /* Enable diable DNS */ + if(isset($_POST['enableDNS'])){ + $this->DNS_is_account = true; + }else{ + $this->DNS_is_account = false; + } + } } @@ -56,7 +229,6 @@ class termDNS extends plugin function check() { $message= array(); - return ($message); } @@ -64,14 +236,218 @@ class termDNS extends plugin /* Save to LDAP */ function save($dn) { - $org_dn = $dn; + $ldap= $this->config->get_ldap_link(); + + /*******************/ + /* IP-MAC HANDLING */ + /*******************/ + + /* $dn was posted as parameter */ $this->dn = $dn; + + /* Save DNS setting & ip/Mac*/ plugin::save(); /* Write back to ldap */ - $ldap= $this->config->get_ldap_link(); $ldap->cd($this->dn); $ldap->modify($this->attrs); + + /****************/ + /* DNS HANDLING */ + /****************/ + + /* If isn't DNS account but initially was DNS account + remove all DNS entries + */ + if(!$this->DNS_is_account){ + if($this->DNSinitially_was_account){ + foreach($this->types as $type){ + $dn = $type['dn']; + $ldap->cd($dn); + $ldap->rmDir($dn); + } + } + }else{ + + /* DNS is enabled, check what we have to do */ + $delete = array(); + + /* Generate a list of new ldap entries, + & $delete contains all dns which should be deleted + */ + $entries = $this->generate_LDAP_entries(&$delete); + + /* Delete dns */ + foreach($delete as $dn => $del){ + $ldap->cd($dn); + $ldap->rmDir($dn); + } + + /* Add || Update new DNS entries */ + foreach($entries as $dn => $attrs){ + $ldap->cd($dn); + $ldap->cat($dn); + + if(count($ldap->fetch())){ + $ldap->cd($dn); + $ldap->modify($attrs); + }else{ + $ldap->cd($dn); + $ldap->add($attrs); + } + } + } + if($ldap->get_error() != "Success"){ + print_red($ldap->get_error()); + } + + } + + + function generateRecordsList($changeStateForRecords) + { + $changeStateForRecords = ""; + + if(!$this->DNS_is_account) { + $str = ""; + return $str; + } + + $str = ""; + foreach($this->types as $key => $entry){ + if($entry['status'] == "deleted") continue; + + $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n"; + $changeStateForRecords.= "changeState('RecordValue_".$key."');\n"; + $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n"; + + $str.=" ". + " ". + " ". + " ". + ""; + } + + $str.= " ". + " ". + " ". + "
".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."
". + " ". + "
"; + return($str); + } + + function generateRecordListBox($selected,$name) + { + $str = ""; + return($str); + } + + function get_Zones() + { + $ret = array(); + $ldap = $this->config->get_ldap_link(); + $ldap-> cd ($this->config->current['BASE']); + $ldap->search("(&(objectClass=dNSZone)(sOARecord=*))",array("*")); + + while($at = $ldap->fetch()){ + if(preg_match("/\.in\-addr\.arpa/",$at['zoneName'][0])){ + $ret[$at['relativeDomainName'][0]]['addr']= $at['zoneName'][0]; + }else{ + $ret[$at['relativeDomainName'][0]]['name']= $at['zoneName'][0]; + } + } + + $tmp =array(); + foreach($ret as $name => $entry){ + $tmp[$entry['addr']]=$entry['name']; + } + $ret = $tmp; + return($ret); + } + + function generate_LDAP_entries($delete) + { + + $entries = array(); + + $delete = array(); + + /* Generate Main Entry */ + $dn = "relativeDomainName=".$this->cn.",".$this->dn; + $entries[$dn]['dNSClass'] = $this->dNSClass; + $entries[$dn]['zoneName'] = $this->zoneName; + $entries[$dn]['dNSTTL'] = $this->dNSTTL; + $entries[$dn]['relativeDomainName'] = $this->cn; + + /* Generate cNAMERecord */ + $aRecords = array(); + foreach($this->types as $type){ + if($type['type'] == "cNAMERecord"){ + + $Cdn = "relativeDomainName=".$type['value'].",".$this->dn; + if($type['status']=="deleted"){ + $delete [$type['dn']] = $Cdn; + }else{ + $entries[$Cdn] = $entries[$dn]; + $entries[$Cdn]['relativeDomainName'] = $type['value']; + $entries[$Cdn]['cNAMERecord'] = $this->cn.".".$this->zoneName; + } + } + } + + /* Generate tXTRecord */ + $aRecords = array(); + foreach($this->types as $type){ + if(($type['type'] == "tXTRecord")&&($type['status']!="deleted")){ + $entries[$dn]['tXTRecord'][] = $type['value']; + } + } + + /* Generate A Records (IP Address relation) */ + $aRecords = array(); + foreach($this->types as $type){ + if(($type['type'] == "aRecord")&&($type['status']!="deleted")){ + $aRecords[] = $type['value']; + } + } + if(count($aRecords)){ + $dn = "relativeDomainName=".$this->cn.",".$this->dn; + foreach($aRecords as $rec){ + $entries[$dn]['aRecord'][] = $rec; + } + } + + /* Generate pTRRecord Records */ + foreach($this->types as $type){ + if($type['type'] == "pTRRecord"){ + $PTRdn= "relativeDomainName=".$type['value'].",".$this->dn; + if($type['status']=="deleted"){ + $delete [$type['dn']] = $PTRdn; + }else{ + $zones = array_flip($this->Zones); + $zone = $zones[$this->zoneName]; + $entries[$PTRdn]['relativeDomainName'] = $type['value']; + $entries[$PTRdn]['pTRRecord'] = $this->cn.".".$this->zoneName; + $entries[$PTRdn]['zoneName'] = $zone; + } + } + } + + foreach($entries as $key => $entry ){ + $entries[$key]['objectClass']=array("top","dNSZone"); + $entries[$key] = array_reverse($entries[$key]); + } + + return($entries); } } diff --git a/plugins/admin/systems/network.tpl b/plugins/admin/systems/network.tpl index 89c776ea3..93b7fbc25 100644 --- a/plugins/admin/systems/network.tpl +++ b/plugins/admin/systems/network.tpl @@ -1,23 +1,74 @@ -

{t}Network settings{/t}

+

{t}Network settings{/t}

- - - - - +
- - - - - -
-
- - - - - -
{$staticAddress}
-
+ + + +
+ + + + + +
+
+ + + + + +
{$staticAddress}
+
-
+

 

+
+ + + + + +
+

{t}Domain name service{/t}

+ + {t}Enable DNS for this device.{/t} + + + + + + + + + + + + + + +
+ +
{t}DNS-TTL{/t} +
{t}DNS Class{/t} + + +
+
+

{t}Records{/t}

+ {$records} +
+ + diff --git a/plugins/admin/systems/termdns.tpl b/plugins/admin/systems/termdns.tpl deleted file mode 100644 index cc03d4e26..000000000 --- a/plugins/admin/systems/termdns.tpl +++ /dev/null @@ -1,3 +0,0 @@ - - -

-- 2.30.2