X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=plugins%2Fadmin%2Fsystems%2Fclass_termDNS.inc;h=fc55c61d73bbc67e2f7765b787c92accb2f5b258;hb=afecd435ef601ea27a281cc6667f94d7273b40e2;hp=59facba570f96070772da4a3bf31086d9ea801d8;hpb=bcc0bddbe9b7200c8de19e47ce6383cdcaad44ec;p=gosa.git diff --git a/plugins/admin/systems/class_termDNS.inc b/plugins/admin/systems/class_termDNS.inc index 59facba57..fc55c61d7 100644 --- a/plugins/admin/systems/class_termDNS.inc +++ b/plugins/admin/systems/class_termDNS.inc @@ -3,131 +3,115 @@ class termDNS extends plugin { /* CLI vars */ - var $cli_summary= "Manage server basic objects"; - var $cli_description= "Some longer text\nfor help"; - var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser"); + var $cli_summary = "Manage server basic objects"; + var $cli_description = "Some longer text\nfor help"; + var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser"); /* 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 $ignore_account = true; + var $autonet = false; - var $ipHostNumber =""; // IP address - var $macAddress =""; // Mac address - var $cn =""; // CN of currently edited device + /* Basic informations + */ + var $attributes = array("ipHostNumber","macAddress"); + var $objectclasses = array("whatever"); - var $Zones = array(); // All Available Zones like array("3.2.1"=>"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 + var $ipHostNumber = ""; // IP address + var $macAddress = ""; // Mac address + var $cn = ""; // CN of currently edited device + var $OrigCn = ""; // Initial cn + var $IPisMust = false; + var $MACisMust = false; - /* Used records */ - var $types = array(); + /* DNS attributes + */ + var $DNSattributes = array("dNSClass","zoneName","dNSTTL"); + var $DNS_is_Account = false; var $DNSinitially_was_account = false; + var $dnsEntry = array(); + var $DNSenabled = false; - var $orig_dn =""; - - var $IPisMust = false; - var $MACisMust= false; - + /* Terminal dns + */ function termDNS ($config, $dn,$objectClasses,$IPisMust = false) { - /* We need to know which objectClasses are used, to store the ip/mac*/ - $this->objectclasses= $objectClasses; + /* We need to know which objectClasses are used, to store the ip/mac + * Because of different type of devices + */ + $this->objectclasses = $objectClasses; + $this->IPisMust = $IPisMust; + plugin::plugin ($config, $dn); - $this->orig_dn= $dn; - - $this->IPisMust = $IPisMust; - - /* All types with required attrs */ - $this->RecordTypes['aRecord'] = "aRecord"; // ok - $this->RecordTypes['mDRecord'] = "mDRecord"; // ok - $this->RecordTypes['mXRecord'] = "mXRecord"; // ok - $this->RecordTypes['nSRecord'] = "nSRecord"; // ok - $this->RecordTypes['pTRRecord'] = "relativeDomainName";// ok - $this->RecordTypes['hInfoRecord'] = "hInfoRecord"; // ok - $this->RecordTypes['mInfoRecord'] = "mInfoRecord"; // ok - $this->RecordTypes['cNAMERecord'] = "relativeDomainName";// ok - $this->RecordTypes['tXTRecord'] = "tXTRecord"; // ok - $this->RecordTypes['aFSDBRecord'] = "aFSDBRecord"; // ok - $this->RecordTypes['SigRecord'] = "SigRecord"; // ok - $this->RecordTypes['KeyRecord'] = "KeyRecord"; // ok - $this->RecordTypes['aAAARecord'] = "aAAARecord"; // ok - $this->RecordTypes['LocRecord'] = "LocRecord"; // ok - $this->RecordTypes['nXTRecord'] = "nXTRecord"; // ok - $this->RecordTypes['sRVRecord'] = "sRVRecord"; // ok - $this->RecordTypes['nAPTRRecord'] = "nAPTRRecord"; // ok - $this->RecordTypes['kXRecord'] = "kXRecord"; // ok - $this->RecordTypes['certRecord'] = "certRecord"; // ok - $this->RecordTypes['a6Record'] = "a6Record"; // ok - $this->RecordTypes['dSRecord'] = "dSRecord"; // ok - $this->RecordTypes['sSHFPRecord'] = "sSHFPRecord"; // ok - $this->RecordTypes['rRSIGRecord'] = "rRSIGRecord"; // ok - $this->RecordTypes['nSECRecord'] = "nSECRecord"; // ok - - /* Get all available zones */ - if(empty($this->cn)&&(isset($this->attrs['cn'][0]))){ + if(isset($this->attrs['cn'][0])){ + $this->OrigCn = $this->attrs['cn'][0]; $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=*)(!(relativeDomainName=@)))",array("*")); + /* Do we have autonet support? */ + if (isset($this->config->data['MAIN']['AUTO_NETWORK_HOOK'])){ + $this->autonet= true; + } - while($attrs = $ldap->fetch()){ - /* If relative domainname == cn - * Try to read dnsclass / TTl / zone + /* Hide all dns specific code, if dns is not available + */ + $DNSenabled = false; + foreach($this->config->data['TABS']['SERVTABS'] as $tab){ + if(preg_match("/^servdns$/",$tab['CLASS'])){ + $this->DNSenabled = true; + } + } + if(!$this->DNSenabled){ + $this->DNS_is_account = false; + return; + } + + if($this->DNSenabled){ + + /* Get Zones */ - 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]; + $this->Zones = getAvailableZones($config); + + /* Get Entry + */ + $this->dnsEntry = getDNSHostEntries($config,$this->OrigCn); + + /* Remove A record which equals $this->ipHostNumber + */ + foreach($this->dnsEntry['RECORDS'] as $key => $rec){ + if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){ + unset($this->dnsEntry['RECORDS'][$key]); } } - /* 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 ++){ - if(($value == "aRecord")&&($this->ipHostNumber==$attrs[$value][$i])){ - continue; - } - $types[] =array("type"=>$name,"inittype"=>$name,"value"=>$attrs[$value][$i],"status"=>"edited","dn"=>$attrs['dn']); - } - } + /* Get Record types + */ + $this->RecordTypes = getDnsRecordTypes(); + + /* If there is at least one entry in this -> types, we have DNS enabled + */ + if($this->dnsEntry['exists']){ + $this->DNS_is_account = true; + }else{ + $this->DNS_is_account = false; } + } + + /* Store initally account settings + */ + $this->DNSinitially_was_account = $this->DNS_is_account; + } - /* 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; + + function getVarsForSaving($attrs) + { + foreach($this->attributes as $attr){ + if(!empty($this->$attr)){ + $attrs[$attr] = $this->$attr; + } } - - /* Store initally account settings */ - $this->DNSinitially_was_account = $this->DNS_is_account; + return($attrs); } function execute() @@ -136,9 +120,56 @@ class termDNS extends plugin $smarty= get_smarty(); $display= ""; - /* Add new empty array with status new, to our record list */ + $smarty->assign("staticAddress", ""); + $smarty->assign("autonet", ""); + + /* Check for autonet button */ + if ($this->autonet && isset($_POST['autonet'])){ + $cmd= $this->config->data['MAIN']['AUTO_NETWORK_HOOK']; + if(!empty($cmd) && $this->cn != ""){ + $res = shell_exec($cmd." ".$this->cn); + if(!$res){ + print_red(sprintf(_("Can't execute specified AUTO_NETWORK_HOOK '%s'. Please check your gosa.conf."),$cmd)); + } else { + $res= split(';', trim($res)); + if (isset($res[0]) && $res[0] != ""){ + $this->ipHostNumber= $res[0]; + } + if (isset($res[1]) && $res[1] != ""){ + $this->macAddress= $res[1]; + } + } + } + } + + /* There is no dns available + */ + if($this->DNSenabled == false){ + + /* Is IP address must ? */ + $smarty->assign("DNS_is_account",false); + $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account))); + + /* Assign smarty all non DNs attributes */ + foreach($this->attributes as $attr){ + $smarty->assign($attr,$this->$attr); + } + $smarty->assign("staticAddress","*"); + + if ($this->autonet){ + $smarty->assign("autonet", "true"); + } else { + $smarty->assign("autonet", ""); + } + $display.= $smarty->fetch(get_template_path('network.tpl', TRUE)); + return($display); + }else{ + $smarty->assign("DNS_is_account",true); + } + + /* Add new empty array to our record list */ if(isset($_POST['AddNewRecord'])){ - $this->types[] =array("type"=>"aRecord","value"=>"","status"=>"new"); + $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>""); } /* Handle all posts */ @@ -156,12 +187,8 @@ class termDNS extends plugin $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]); - } + if(isset($this->dnsEntry['RECORDS'][$id])){ + unset($this->dnsEntry['RECORDS'][$id]); } } } @@ -173,7 +200,7 @@ class termDNS extends plugin /* Assign smarty all DNS attributes */ foreach($this->DNSattributes as $attr){ - $smarty->assign($attr,$this->$attr); + $smarty->assign($attr,$this->dnsEntry[$attr]); } /* Assign all needed vars */ @@ -181,24 +208,31 @@ class termDNS extends plugin $smarty->assign("Zones",$this->Zones); $smarty->assign("ZoneKeys",($this->Zones)); $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account))); - $changeStateForRecords =""; - $smarty->assign("records",$this->generateRecordsList(&$changeStateForRecords)); + + $tmp = $this->generateRecordsList(); + + $changeStateForRecords = $tmp['changeStateForRecords']; + + $smarty->assign("records",$tmp['str']); $smarty->assign("changeStateForRecords",$changeStateForRecords); -// $smarty->assign("dNSClasses",array("IN"=>"IN")); $smarty->assign("staticAddress","*"); + $display.= $smarty->fetch(get_template_path('network.tpl', TRUE)); return($display); } function remove_from_parent() { + /* $ldap = $this->config->get_ldap_link(); $ldap->cd($this->orig_dn); $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("relativeDomainName","zoneName")); while($attr = $ldap->fetch()){ $ldap->cd($attr['dn']); $ldap->rmDir($attr['dn']); + show_ldap_error("Record:".$ldap->get_error(), _("Removing terminal from DNS object failed")); } + */ } /* Save data to object */ @@ -215,25 +249,26 @@ class termDNS extends plugin } /* Get dns attributes */ - if(isset($_POST['network_tpl_posted'])){ + if(($this->DNSenabled) && (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]; + if(is_array($this->dnsEntry['RECORDS'])){ + foreach($this->dnsEntry['RECORDS'] as $key => $value){ + + /* Check if type has changed */ + if(isset($_POST['RecordTypeSelectedFor_'.$key])){ + $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key]; + } + /* Check if value has changed */ + if(isset($_POST['RecordValue_'.$key])){ + $this->dnsEntry['RECORDS'][$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]; + $this->dnsEntry[$attr] = $_POST[$attr]; } } @@ -250,50 +285,85 @@ class termDNS extends plugin /* Check supplied data */ function check() { - $message= array(); + /* Call common method to give check the hook */ + $message= plugin::check(); - + /* Check if ip must be given + */ if(($this->IPisMust)||($this->DNS_is_account)){ - /* Check if ip is empty */ + + /* Check if ip is empty + */ if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){ $message[]= _("The required field 'IP-address' is not set."); } - /* check if given ip is valid ip*/ - $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])"; - if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){ - $message[]= _("Wrong IP format in field IP-address."); - } } - /* Check if mac is empty */ + /* check if given ip is valid ip */ + if ($this->ipHostNumber != "" && !is_ip($this->ipHostNumber)){ + $message[]= _("Wrong IP format in field IP-address."); + } + + /* Check if mac is empty + */ if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){ $message[]= _("The required field 'MAC-address' is not set."); } - /* Check if given mac is valid mac */ - $tr = count(split(":",$this->macAddress)); - if($tr!=6){ - $message[]=(_("The given macaddress is invalid. There must be 6 1byte segments seperated by ':'.")); + /* Check if given mac is valid mac + */ + if(!is_mac($this->macAddress)){ + $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'.")); } /* only perfrom this checks if this is a valid DNS account */ if($this->DNS_is_account){ - foreach($this->types as $name => $values){ - /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress */ - if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)&&($values['status']!="deleted")){ + $checkArray = array(); + $onlyOnce = array(); + + $onlyOnce['cNAMERecord'] = 0; + + /* Walk through all entries and detect duplicates or mismatches + */ + foreach($this->dnsEntry['RECORDS'] as $name => $values){ + + /* Count record values, to detect duplicate entries for a specific record + */ + if(!isset($checkArray[$values['type']][$values['value']])){ + $checkArray[$values['type']][$values['value']] = 0; + }else{ + $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']); + } + + /* Check if given entries in $onlyOnce are used more than once + */ + if(isset($onlyOnce[$values['type']])){ + $onlyOnce[$values['type']] ++; + if($onlyOnce[$values['type']] > 1){ + $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']); + } + } + + /* Skip txt record ... + */ + if($values['type'] == "tXTRecord") continue; + + /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress + */ + if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){ $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."), $this->ipHostNumber); } - /* only lower-case is allowed in record entries ... */ + /* only lower-case is allowed in record entries ... + */ if($values['value'] != strtolower($values['value'])){ $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']); } } } - return ($message); } @@ -316,8 +386,7 @@ class termDNS extends plugin /* Write back to ldap */ $ldap->cd($this->dn); $this->cleanup(); -$ldap->modify ($this->attrs); - + $ldap->modify ($this->attrs); /****************/ /* DNS HANDLING */ @@ -326,70 +395,72 @@ $ldap->modify ($this->attrs); /* If isn't DNS account but initially was DNS account remove all DNS entries */ - if(!$this->DNS_is_account){ - if($this->DNSinitially_was_account){ - $tmp = array(); - foreach($this->types as $type){ - $dn = $type['dn']; - if(!isset($tmp[$dn])) { - $ldap->cd($dn); - $ldap->rmDir($dn); - } - $tmp[$dn]=$dn; - } - } + if(!$this->DNSenabled){ + return; }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 + + /* Add ipHostNumber to aRecords */ - $entries = $this->generate_LDAP_entries(&$delete); + $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber); + + /* Create diff and follow instructions + * If Account was disabled, remove account by setting exists to false + */ + if((!$this->DNS_is_account)&&($this->DNSinitially_was_account)){ + $this->dnsEntry['exists'] = false; + $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn); + }else{ + $this->dnsEntry['exists'] = $this->DNS_is_account; + $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn); + } + + /* move follwoing entries + */ + foreach($tmp['move'] as $src => $dst){ + $this->recursive_move($src,$dst); + } /* Delete dns */ - foreach($delete as $dn => $del){ + foreach($tmp['del'] as $dn => $del){ $ldap->cd($dn); - $ldap->rmDir($dn); + $ldap->rmdir_recursive($dn); } - /* Add || Update new DNS entries */ - foreach($entries as $dn => $attrs){ + /* Add || Update new DNS entries + */ + foreach($tmp['add'] as $dn => $attrs){ $ldap->cd($dn); - $ldap->cat($dn); - + $ldap->cat($dn, array('dn')); if(count($ldap->fetch())){ $ldap->cd($dn); -// $this->cleanup(); $ldap->modify ($attrs); - }else{ $ldap->cd($dn); $ldap->add($attrs); } } + + /* Display errors + */ + if($ldap->get_error() != "Success"){ + show_ldap_error("Record:".$ldap->get_error(), _("Saving terminal to DNS object failed")); + } } - if($ldap->get_error() != "Success"){ - show_ldap_error($ldap->get_error()); - } - } /* Create html table with all used record types */ - function generateRecordsList($changeStateForRecords) + function generateRecordsList() { $changeStateForRecords = ""; if(!$this->DNS_is_account) { - $str = ""; + $str = ""; return $str; } $str = "