Code

Added multi actions
[gosa.git] / gosa-plugins / systems / admin / systems / class_termDNS.inc
1 <?php
3 class termDNS extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account = true;
8   /* Basic informations 
9    */
10   var $attributes     = array("ipHostNumber","macAddress");
11   var $objectclasses  = array("whatever");
13   var $ipHostNumber   = "";    // IP address 
14   var $macAddress     = "";    // Mac address 
16   var $orig_ipHostNumber   = "";    // IP address 
17   var $orig_macAddress     = "";    // Mac address 
19   var $cn             = "";    // CN of currently edited device 
20   var $OrigCn         = "";    // Initial cn
21   var $IPisMust       = false;
22   var $MACisMust      = false;
23   var $dialog         = false;
25   /* DCHP Attributes 
26    */
27   var $dhcpAttributes           = array("dhcpParentNode");
28   var $dhcpEnabled              = FALSE;
29   var $dhcp_is_Account          = FALSE;
30   var $dhcpParentNodes          = array();
31   var $dhcpParentNode           = "";
32   var $dhcpHostEntry            = array();
33   var $initial_dhcpParentNode   = "";
34   var $initial_dhcpHostEntry    = array();
35   var $initial_dhcp_is_Account  = FALSE;
37   var $used_ip_mac              = array();  
39   /* DNS attributes  
40    */
41   var $DNSattributes            = array("dNSClass","zoneName","dNSTTL");
42   var $DNS_is_account           = false;
43   var $initially_was_account = false;
44   var $dnsEntry                 = array();
45   var $DNSenabled               = false;
46   var $hide_dns_check_box       = FALSE;
48   /*  Terminal dns 
49    */
50   function termDNS (&$config, $parent,$objectClasses,$IPisMust = false)
51   {
52     /* We need to know which objectClasses are used, to store the ip/mac
53      * Because of different type of devices   
54      */ 
55     $this->parent         = $parent;
56     $this->objectclasses  =  $objectClasses;
57     $this->IPisMust       = $IPisMust;
59     plugin::plugin ($config, $parent->dn);
61     if(isset($this->attrs['cn'][0])){
62       $this->OrigCn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
63       $this->cn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
64     }
66  
67     /************
68      * DHCP 
69      ************/
71     /* Hide all dhcp specific code, if dhcp plugin is not present in config */
72     $dhcpEnabled = FALSE;
73     if($this->config->search("servdhcp","class",array("tabs"))){
74       $this->dhcpEnabled = TRUE;
75     }
76     if($this->dhcpEnabled){
77       $this->dhcpParentNodes = $this->get_dhcp_parent_nodes();
78       $this->dhcpParentNode  = $this->get_dhcp_parent_node();
79       if($this->dhcpParentNode){
80         $this->dhcp_is_Account = TRUE;
81         $this->initial_dhcp_is_Account = TRUE;
82         $this->dhcpHostEntry  = $this->get_dhcp_host_entry();    
83       }
84       $this->initial_dhcpHostEntry = $this->dhcpHostEntry;
85       $this->initial_dhcpParentNode= $this->dhcpParentNode;
86     }
89     /************
90      * DNS
91      ************/
92  
93     /* Hide all dns specific code, if dns is not available 
94      */
95     $DNSenabled = false;
96     foreach($this->config->data['TABS']['SERVERSERVICE'] as $tab){
97       if(preg_match("/^servdns$/",$tab['CLASS'])){
98         $this->DNSenabled = true;
99       }
100     }
101     if(!$this->DNSenabled){
102       $this->DNS_is_account = false;
103       return;
104     }
106     if($this->DNSenabled){
108       /* Get Zones  
109        */
110       $this->Zones        = DNS::getAvailableZones($config);
112       /* Get Entry 
113        */
114       $this->dnsEntry     = DNS::getDNSHostEntries($config,$this->OrigCn);
116       /* Remove A record which equals $this->ipHostNumber
117        */
118       $ptr = $this->get_pTRRecord();
119       foreach($this->dnsEntry['RECORDS'] as $key => $rec){
120         if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
121           unset($this->dnsEntry['RECORDS'][$key]);
122         }
123         if(($rec['type'] == "pTRRecord") && ($rec['value'] == $ptr)){
124           unset($this->dnsEntry['RECORDS'][$key]);
125         }
126       }
128       /* Get Record types 
129        */
130       $this->RecordTypes  = DNS::getDnsRecordTypes();
132       /* If there is at least one entry in this -> types, we have DNS enabled 
133        */
134       if($this->dnsEntry['exists']){
135         $this->DNS_is_account = true;
136       }else{
137         $this->DNS_is_account = false;
138       }
139     }
141     /* Create a list of used mac and ip addresses.
143        ! We use this optically huge amount of code to fetch all 
144        Mac and IP addresses, because a simple search for mac and IP
145        over the whole ldap server was 10 to 20 times slower.
146      */
147     $deps  = array();
148     $ou = preg_replace("/,.*$/","",get_ou("systemsou"));
149     $a_ous = array(get_ou("serverou"),
150                   get_ou("terminalou"),
151                   get_ou("workstationou"),
152                   get_ou("printerou"),
153                   get_ou("phoneou"),
154                   get_ou("componentou"));
155   
156     $ldap = $this->config->get_ldap_link();
157     $ldap->cd($this->config->current['BASE']);
158     $ldap->search("(&(objectClass=organizationalUnit)(".$ou."))",array("dn"));
159     while($attrs = $ldap->fetch()){
160       foreach($a_ous as $allowed){
161         $deps[] = $allowed.$attrs['dn'];
162       }
163     }
165     foreach($deps as $dep){
166       $ldap->cd($dep);
167       $ldap->search("(|(macAddress=*)(ipHostNumber=*))",array("macAddress","ipHostNumber"));
168       while($attrs = $ldap->fetch()){
169         if(isset($attrs['ipHostNumber'][0])){
170           $this->used_ip_mac["ip:".$attrs['ipHostNumber'][0]] = "ip:".$attrs['ipHostNumber'][0];
171         }
172         if(isset($attrs['macAddress'][0])){
173           $this->used_ip_mac["mac:".$attrs['macAddress'][0]] = "mac:".$attrs['macAddress'][0];
174         }
175       } 
176     }
178     /* Save initial ip and mac values, to be able 
179         check if the used values are already in use */ 
180     $this->orig_ipHostNumber   = $this->ipHostNumber;  
181     $this->orig_macAddress     = $this->macAddress;
182  
183     /* Store initally account settings 
184      */
185     $this->initially_was_account = $this->DNS_is_account;
187     if($this->DNS_is_account){
188       new log("view","unknown/".get_class($this),$this->dn);
189     }
190   }
193   function netmaskIsCoherent($idZone) 
194   {
195     $netmask = DNS::FlipIp(str_replace(".in-addr.arpa","",DNS::getNameFromMix($idZone)));
196     if(!strstr($this->ipHostNumber, $netmask)){
197       return false;
198     }else{
199       return true;
200     }
201   }
204   function getVarsForSaving($attrs) 
205   {
206     foreach($this->attributes as $attr){
207       if(!empty($this->$attr)){
208         $attrs[$attr] = $this->$attr;
209       }
210     }
211     return($attrs); 
212   }
214   function execute()
215   {
216           /* Call parent execute */
217     $smarty= get_smarty();
219     $tmp = $this->plInfo();
220     foreach($tmp['plProvidedAcls'] as $name => $translation){
221       $smarty->assign($name."ACL",$this->getacl($name));
222     }
224     $display= "";
226     $smarty->assign("staticAddress", ""); 
227  
228     /* Check for autonet button */
229     if (isset($_POST['autonet'])){
230         $d= new gosaSupportDaemon(TRUE, 0.5);
231         $res= $d->_send("<xml><header>gosa_network_completition</header><source>GOSA</source><target>GOSA</target><hostname>".$this->cn."</hostname></xml>", TRUE);
232         if (isset($res['XML']['IP'])){
233                 $this->ipHostNumber= $res['XML']['IP'];
234         }
235         if (isset($res['XML']['MAC'])){
236                 $this->macAddress= $res['XML']['MAC'];
237         }
238     }
239     
240   
241     /**********
242      * DHCP Handling
243      **********/
244  
245     if(isset($_POST['dhcpEditOptions'])){
247       if(count($this->dhcpHostEntry) == 0){
248         $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
249       }else{
250         $this->dialog = new dhcpHost($this->dhcpHostEntry,TRUE);
251       }
252       $this->dialog->cn = $this->cn; 
253       $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress; 
254       if(!empty($this->ipHostNumber)){
255         $this->dialog->statements['fixed-address'] = $this->ipHostNumber; 
256       }
257     }
259     if(isset($_POST['cancel_dhcp'])){
260       $this->dialog = FALSE; 
261     }
263     if(isset($_POST['save_dhcp'])){
264       $this->dialog->save_object();
265       
266       $msgs = $this->dialog->check(array());
267       if(count($msgs)){
268         foreach($msgs as $msg){
269           print_red($msg);
270         }
271       }else{
272         $this->dhcpHostEntry = $this->dialog->save();
273         $this->dialog = FALSE; 
274       }
275     }
277     if(is_object($this->dialog)){
278       $this->dialog->save_object();
279       return($this->dialog->execute());
280     }
281  
282     $smarty->assign("dhcpEnabled",    $this->dhcpEnabled);
283     $smarty->assign("dhcp_is_Account",$this->dhcp_is_Account);
284     $smarty->assign("dhcpParentNode", $this->dhcpParentNode);
285     $smarty->assign("dhcpParentNodes",$this->dhcpParentNodes);
286     $smarty->assign("dhcpParentNodeCnt",count($this->dhcpParentNodes));
289     /**********
290      * DNS Handling
291      **********/
293     /* There is no dns available
294      */
295     $smarty->assign("DNS_is_account",$this->DNS_is_account);
296     $smarty->assign("DNSenabled",$this->DNSenabled);
297     if($this->DNSenabled == false){
299       /* Is IP address must ? */
300 #      $smarty->assign("DNS_is_account",false);
301       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
303       /* Assign smarty all non DNs attributes */
304       foreach($this->attributes as $attr){
305         $smarty->assign($attr,$this->$attr);
306       }
307       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
309       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
310     }else{
311  #     $smarty->assign("DNS_is_account",true);
313       /* Add new empty array to our record list */
314       if(isset($_POST['AddNewRecord'])){
315         $this->dnsEntry['RECORDS'][]  =array("type"=>"aRecord","value"=>"");
316       }
318       /* propose_ip */
319       if(isset($_POST['propose_ip'])){
320         foreach($this->Zones as $key => $name){
321           if($name == $this->dnsEntry['zoneName']){
322             $net = DNS::FlipIp(str_replace(".in-addr.arpa","",DNS::getNameFromMix($key)));
323             $this->ipHostNumber = $this->generateRandomIp($net);
324           }
325         }
326       }
328       /* Handle all posts */
329       $only_once =true;
330       foreach($_POST as $name => $value){
332         /* Check if we have to delete a record entry */
333         if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
335           /* Avoid performing this once again */
336           $only_once = false;
338           /* Extract id for specified entry */
339           $id = preg_replace("/RemoveRecord_/","",$name);
340           $id = preg_replace("/_.*$/","",$id);
342           /* Delete this record, mark edited entries to be able to delete them */
343           if(isset($this->dnsEntry['RECORDS'][$id])){
344             unset($this->dnsEntry['RECORDS'][$id]);
345           }
346         }
347       }
348       /* Assign smarty all non DNs attributes */
349       foreach($this->attributes as $attr){
350         $smarty->assign($attr,$this->$attr);
351       }
353       /* Assign smarty all DNS attributes */
354       foreach($this->DNSattributes as $attr){
355         $smarty->assign($attr,$this->dnsEntry[$attr]);
356       }
358       /* Assign all needed vars */
359  #     $smarty->assign("DNSAccount",$this->DNS_is_account);
360       $smarty->assign("hide_dns_check_box",$this->hide_dns_check_box);
361   
362       $smarty->assign("Zones",$this->Zones);
363       $smarty->assign("ZoneCnt",count($this->Zones));
364       $smarty->assign("ZoneKeys",($this->Zones));
365       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
367       /* Create zone array */
368       $idZones = array();
369       foreach($this->Zones as $id => $zone){
370         if($this->netmaskIsCoherent($id)) {
371           $idZones[$id] = $zone;
372         }else{
373           $idZones[$id] = $zone."&nbsp;("._("Not matching").")";
374         }
375       }
376       $smarty->assign("Zones",$idZones);
377       $smarty->assign("ZoneKeys", $this->Zones);
379       $tmp = $this->generateRecordsList();
381       $changeStateForRecords = $tmp['changeStateForRecords'];
383       $smarty->assign("records",$tmp['str']);
384       $smarty->assign("changeStateForRecords",$changeStateForRecords);
385       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
387       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
388     }
389     return($display);
390   }
393   function remove_from_parent()
394   {
395     if($this->initially_was_account){
397       $ldap = $this->config->get_ldap_link();
399       $tmp = array();
400       $this->dnsEntry['exists'] = false;
401       $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
402   
403       /* Delete dns */
404       foreach($tmp['del'] as $dn => $del){
405         $ldap->cd($dn);
406         $ldap->rmdir_recursive($dn);
407         new log("remove","unknown/".get_class($this),$dn);
408         show_ldap_error($ldap->get_error(), sprintf(_("Could not remove dns extension for '%s'."),$dn));
409       }
410     }
411   }
414   /* Save data to object */
415   function save_object()
416   {
418     if(isset($_POST['network_tpl_posted'])){
420       /* Save all posted vars */
421       plugin::save_object();
423       /* Handle DHCP Posts*/
424       if($this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
425         foreach($this->dhcpAttributes as $attr){
426           if(isset($_POST[$attr])){
427             $this->$attr = $_POST[$attr];
428           }
429         }
430         if(isset($_POST['dhcp_is_Account'])){
431           $this->dhcp_is_Account = TRUE;
432         }else{
433           $this->dhcp_is_Account = FALSE;
434         }
435       }
437       /* Ge all non dns attributes (IP/MAC)*/
438       foreach($this->attributes as $attr){
439         if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
440           $this->$attr = $_POST[$attr];
441         }
442       }
444       /* Check if DNS should be enabled / disabled */
445       if($this->DNS_is_account && $this->acl_is_removeable() && !isset($_POST['DNS_is_account'])){
446         $this->DNS_is_account = false;
447       }elseif(!$this->DNS_is_account && $this->acl_is_createable() && isset($_POST['DNS_is_account'])){
448         $this->DNS_is_account = true;
449       }
451       /* Get dns attributes */
452       if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
454         /* Check for posted record changes */
455         if(is_array($this->dnsEntry['RECORDS']) && $this->acl_is_writeable("Records")){
456           foreach($this->dnsEntry['RECORDS'] as $key => $value){
458             /* Check if type has changed */
459             if(isset($_POST['RecordTypeSelectedFor_'.$key])){
460               $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
461             }
462             /* Check if value has changed */
463             if(isset($_POST['RecordValue_'.$key])){
464               $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
465             }
466           }
467         }
468         /* Get all basic DNS attributes (TTL, Clas ..)*/
469         foreach($this->DNSattributes as $attr){
470           if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
471             $this->dnsEntry[$attr] = $_POST[$attr];
472           }
473         }
476       }
477       if($this->hide_dns_check_box){
478         $this->DNS_is_account = true;
479       }
480     }
481   }
484   /* Check supplied data */
485   function check()
486   {
487     /* Call common method to give check the hook */
488     $message= plugin::check();
490     if($this->dhcpEnabled && $this->dhcp_is_Account && $this->dhcpParentNode != "" && count($this->dhcpHostEntry) == 0){
491 #      $message[] =_("You have not configured your dhcp settings yet.");
492     }
493     
494     /* Check if mac and ip are already used */
495     if(!empty($this->ipHostNumber) && $this->DNS_is_account && 
496         $this->ipHostNumber != $this->orig_ipHostNumber && 
497         in_array("ip:".$this->ipHostNumber,$this->used_ip_mac)){
498       $message[] =_("The specified IP address is already in use.");
499     }
500     if(!empty($this->macAddress) && $this->dhcp_is_Account && 
501         $this->macAddress != $this->orig_macAddress && 
502         in_array("mac:".$this->macAddress,$this->used_ip_mac)){
503       print_red(sprintf(_("The specified MAC address '%s' for this system '%s' is already in use."),$this->macAddress,$this->cn));
504     }
506     /* Check if ip must be given
507      */  
508     if(($this->IPisMust)||($this->DNS_is_account)){
509       if (empty($this->ipHostNumber)){
510         $message[]= _("The required field 'IP-address' is not set.");
511       }
513       if (!tests::is_ip($this->ipHostNumber)){
514         $message[]= _("Wrong IP format in field IP-address.");
515       }
516     }
518     /* Check if mac is empty 
519      */
520     if ($this->macAddress == "" ){
521       $message[]= _("The required field 'MAC-address' is not set.");
522     }
523     if(!tests::is_mac($this->macAddress)){
524       $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
525     }
527     /* only perfrom this checks if this is a valid DNS account */
528     if($this->DNS_is_account){
530       $checkArray = array();
531       $onlyOnce   = array();
533       //  $onlyOnce['cNAMERecord'] = 0;
534        $tmp = array_flip($this->Zones);
535        $tmp2 = $tmp[$this->dnsEntry['zoneName']];
536        if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
537          $tmp2 = preg_replace("/^.*\//","",$tmp2);
538          $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
539        }
541       /* Walk through all entries and detect duplicates or mismatches
542        */  
543       foreach($this->dnsEntry['RECORDS'] as $name => $values){
545         /* Count record values, to detect duplicate entries for a specific record
546          */
547         if(!isset($checkArray[$values['type']][$values['value']])){
548           $checkArray[$values['type']][$values['value']] = 0;
549         }else{
550           $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
551         }
553         /* Check if given entries in $onlyOnce are used more than once
554          */
555         if(isset($onlyOnce[$values['type']])){
556           $onlyOnce[$values['type']] ++;
557           if($onlyOnce[$values['type']] > 1){
558             $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
559           }
560         }
562         /* Skip txt record ... 
563          */
564         if($values['type'] == "tXTRecord") continue;
566         /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress 
567          */
568         if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
569           $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."), 
570                $this->ipHostNumber);
571         }
573         /* only lower-case is allowed in record entries ... 
574          */
575         if($values['value'] != strtolower($values['value'])){
576           $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
577         }
578       }
579     }
580     return ($message);
581   }
584   /* Save to LDAP */
585   function save()
586   {
587     $ldap= $this->config->get_ldap_link();
588   
589     $dn = $this->parent->dn;
590  
591     /*******************/ 
592     /* IP-MAC HANDLING */
593     /*******************/ 
595     /* $dn was posted as parameter */
596     $this->dn = $dn;
597     
598     /* Save DNS setting & ip/Mac*/
599     plugin::save();
601     /* Write back to ldap */
602     $ldap->cd($this->dn);
603     $this->cleanup();
604     $ldap->modify ($this->attrs); 
606     /****************/ 
607     /* DHCP HANDLING */
608     /****************/ 
609   
610     /* New entry */
611     if($this->dhcpEnabled){
613       if(count($this->dhcpHostEntry) == 0){
614         $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
615         $this->dialog->cn = $this->cn;
616         $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
617         if(!empty($this->ipHostNumber)){
618           $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
619         }
620         $this->dialog->execute();
621         $this->dialog->save_object(); 
622         $this->dhcpHostEntry = $this->dialog->save();
623         if(count($this->dhcpHostEntry['dhcpOption']) == 0){
624           $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
625         }
626       }
628       if(count($this->dhcpHostEntry) == 0){
629         $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
630         $this->dialog->cn = $this->cn;
631         $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
632         if(!empty($this->ipHostNumber)){
633           $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
634         }
635         $this->dialog->execute();
636         $this->dialog->save_object(); 
637         $this->dhcpHostEntry = $this->dialog->save();
638         if(count($this->dhcpHostEntry['dhcpOption']) == 0){
639           $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
640         }
641       }
643       /* Write mac address to dhcp settings */
644       if($this->dhcp_is_Account){
645         if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) || 
646             !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
647           $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
648           $this->dhcpHostEntry['MODIFIED'] = TRUE;
649         }
650       }
651   
653       /* Unset dhcpStatements if this attribute is empty  */
654       if(isset($this->dhcpHostEntry['dhcpStatements']) && 
655           ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
656         unset($this->dhcpHostEntry['dhcpStatements']);
657       }
658   
659       /* DHCP removed */
660       if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
661         $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
662         show_ldap_error($ldap->get_error(),_("Removing dhcp entry for this object failed."));
664         $tmp = new servdhcp($this->config,$this->dhcpParentNode);
665         $tmp->handle_post_events("remove");
666       }
668       /* DHCP Added */
669       if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
670         $attrs = $this->dhcpHostEntry;
671         unset($attrs['MODIFIED']);
672         unset($attrs['dn']);
673         $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
674         $res = $ldap->add($attrs);
676         $tmp = new servdhcp($this->config,$this->dhcpParentNode);
677         $tmp->handle_post_events("add");
679         show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
680       }
682       /* DHCP still activated */
683       if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
685         /* DHCP node changed */
686         if(($this->initial_dhcpParentNode != $this->dhcpParentNode) || 
687            ($this->cn != $this->OrigCn)){
688           $attrs = $this->dhcpHostEntry;
689           $attrs['cn'] = $this->cn;
690           unset($attrs['dn']);
691           unset($attrs['MODIFIED']);
692           $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
693           $res = $ldap->add($attrs);
695           $tmp = new servdhcp($this->config,$this->dhcpParentNode);
696           $tmp->handle_post_events("modify");
698           show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
699           if($res){
700             $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
701             show_ldap_error($ldap->get_error(),_("Removing old dhcp entry failed."));
702           }
703         }
704          
705         /* SAME node but modified */ 
706         if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1  && 
707             $this->initial_dhcpParentNode == $this->dhcpParentNode){
708           $attrs = $this->dhcpHostEntry;
709           unset($attrs['dn']);
710           unset($attrs['MODIFIED']);
711           $ldap->cd($this->dhcpHostEntry['dn']);
712           $ldap->modify($attrs);
713           
714           $tmp = new servdhcp($this->config,$this->dhcpParentNode);
715           $tmp->handle_post_events("modify");
717           show_ldap_error($ldap->get_error(),_("Modifying dhcp entry failed."));
718         }    
719       }
720     }
721     $this->dialog = FALSE; 
723     /****************/ 
724     /* DNS HANDLING */
725     /****************/ 
727     /* If isn't DNS account but initially was DNS account 
728        remove all DNS entries 
729      */ 
730     if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
731       return;
732     }else{
734       /* Add ipHostNumber to aRecords
735        */
736       $backup_dnsEntry = $this->dnsEntry;
737       if(!empty($this->ipHostNumber)){
738         $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
739         $ptr = $this->get_pTRRecord();
740         if(!empty($ptr)){
741           $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
742         } 
743       }
745       /* Create diff and follow instructions 
746        * If Account was disabled, remove account by setting exists to false
747        */
748       if((!$this->DNS_is_account)&&($this->initially_was_account)){  
749         $this->dnsEntry['exists'] = false;
750         $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
751       }else{
752         $this->dnsEntry['exists'] = $this->DNS_is_account;
753         $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
754       }   
756       /* move follwoing entries 
757        */
758       foreach($tmp['move'] as $src => $dst){
759         $this->recursive_move($src,$dst);
760       }
762       /* Delete dns */
763       foreach($tmp['del'] as $dn => $del){
764         $ldap->cd($dn);
765         $ldap->rmdir_recursive($dn);
766         new log("modify","unknown/".get_class($this),$dn,array("*"),$ldap->get_error());
767       }
769       /* Add || Update new DNS entries 
770        */
771       foreach($tmp['add'] as $dn => $attrs){
772         $ldap->cd($dn);
773         $ldap->cat($dn, array('dn'));
774         if(count($ldap->fetch())){
775           $ldap->cd($dn);
776           $ldap->modify ($attrs); 
777         }else{
778           $ldap->cd($dn);
779           $ldap->add($attrs);
780         }
781         new log("modify","unknown/".get_class($this),$dn,array_keys($attrs),$ldap->get_error());
782       }
785       /* Display errors 
786        */
787       if($ldap->get_error() != "Success"){
788         show_ldap_error($ldap->get_error(), sprintf(_("Saving of terminal/dns account with dn '%s' failed."),$this->dn));
789       }
791       $tmp2 = new servdns($this->config,$this->dn);
792       $tmp2->handle_post_events("modify");
794       $this->dnsEntry =  $backup_dnsEntry;
795     }
796   }
798   /*  Create html table with all used record types
799    */
800   function generateRecordsList()
801   {
802     $changeStateForRecords = "";
803     
804     if(!$this->DNS_is_account) {
805       $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
806       return(array("str" => $str, "changeStateForRecords"=> ""));
807     }
808  
809     $str = "<table summary='' width='100%'>";
810     foreach($this->dnsEntry['RECORDS'] as $key => $entry){
812         $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
813         $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
814         $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
816         $str.=" <tr>".
817           "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
818           "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
819           "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
820           "</tr>";
821     }
823     $str.= "  <tr>".
824            "    <td colspan=2 width='50%'></td><td>".
825            "      <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
826            "    </td>".
827            "  </tr>".
828            "</table>";
829      $ret =  array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
830     return($ret);
831   }
834   /* Create a html select box which allows us to select different types of records 
835    */
836   function generateRecordListBox($selected,$name)
837   {
838     $str = "<select name='".$name."' id='".$name."'>";
839     foreach($this->RecordTypes as $type => $value){
840       $use = "";
841       if($type == $selected){
842         $use = " selected ";
843       }
844       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
845     }
846     $str.="</select>";
847     return($str); 
848   }
851   /* Return plugin informations for acl handling  */ 
852   static function plInfo()
853   {
854     $tmp =  array(
855         "plShortName"   => _("DNS"),
856         "plDescription" => _("DNS settings"),
857         "plSelfModify"  => FALSE,
858         "plDepends"     => array(),
859         "plPriority"    => 5,
860         "plSection"     => array("administration"),
861         "plCategory"    => array("workstation","terminal","phone","server","component","printer","winworkstation"),
863         "plProvidedAcls"=> array(
864           "ipHostNumber"  => _("IP address"),
865           "macAddress"    => _("MAC address"))
866         );
868     /* Hide all dns specific code, if dns is not available
869      */
870     $config = session::get('config');
871     foreach($config->data['TABS']['SERVERSERVICE'] as $tab){
872       if(preg_match("/^servdns$/",$tab['CLASS'])){
873         $tmp['plProvidedAcls']["Records"]        = _("DNS records");
874         $tmp['plProvidedAcls']["zoneName"]       = _("Zone name");
875         $tmp['plProvidedAcls']["dNSTTL"]         = _("TTL");
876         break;
877       }
878     }
879     return($tmp);
880   }
882   
883   function get_dhcp_host_entry()
884   {
885     $attrs = array();
886     $dn = $this->get_dhcp_host_entry_dn();
887     if($dn){
888       $ldap = $this->config->get_ldap_link();
889       $ldap->cd($this->config->current['BASE']);
890       $ldap->cat($dn,array("*"));
891       if($ldap->count()){
892         $attrs = $ldap->fetch();
893         foreach($attrs as $key => $value){
894           if(is_numeric($key) || ($key == "count")){
895             unset($attrs[$key]);
896           }
897           if(is_array($value) && isset($value['count'])){
898             unset($attrs[$key]['count']);
899           }
900         }
901       }
902     }
903     return($attrs);
904   }
907   function get_dhcp_host_entry_dn()
908   {
909     $ldap = $this->config->get_ldap_link();
910     $ldap->cd($this->config->current['BASE']);
911     $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
913     if($ldap->count()){
914       $attr = $ldap->fetch();
915       return($attr['dn']);
916     }else{
917       return("");
918     }
919   }  
922   function get_dhcp_parent_node()
923   {
924     return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
925   }
928   function get_dhcp_parent_nodes()
929   {
930     $ldap = $this->config->get_ldap_link();
931     $ldap->cd($this->config->current['BASE']);
932     $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
933     
934     $dhcp_dns = array();
935     while($attr = $ldap->fetch()){
936       $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
937     }
938  
939     foreach($dhcp_dns as $key => $pri_dns){
940       $ldap->cat($pri_dns,array("cn"));
941       $tmp = $ldap->fetch();
942       if(isset($tmp['cn'][0])){
943         $dhcp_dns[$key] = $tmp['cn'][0];
944       }else{
945         unset($dhcp_dns[$key]);
946       }
947     }
949     $tmp = $tmp2 = array();
950     foreach($dhcp_dns as $dn => $cn){
951       $ldap->cd($dn);
952       $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
953                     "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
954       while($attr = $ldap->fetch()){
955         $tmp[$attr['dn']] = $attr['cn'][0];
956       }
957       $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.")&nbsp;"));
958     }
959     return($tmp2);
960   }
962   
963   /* this function returns the default ptr record entry */
964   function get_pTRRecord()
965   {
966     if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
967       $ldap = $this->config->get_ldap_link();
968       $ldap->cat(DNS::getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
969       $attrs = $ldap->fetch();
970       $tmp = array_flip($this->Zones);
971       $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
972       $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
973       $ptr = preg_replace("/^".normalizePreg(DNS::FlipIp($tmp))."\./","",$this->ipHostNumber);
974       return($ptr);
975     }else{
976       return(FALSE);
977     }
978   }
980   
981   function generateRandomIP($net = "")
982   {
983     $str = $net;
984     $cnt = 4;
985     while(substr_count($str,".") < 3 && $cnt > 0){
986       $str .= ".".rand(0,255);
987       $str = preg_replace("/\.\.*/",".",$str);
988       $str = trim($str,". ");
989       $cnt --;
990     }
991     return($str);
992   }
994   
995   function create_tree($arr,$base,$current = "")
996   {
997     $ret = array();
998     foreach($arr as $r => $name){
999       $base_part = str_replace($base,"",$r);
1000       if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
1001         $ret[$r] = $current.$name;
1002         $tmp = $this->create_tree($arr,$r,$current.".&nbsp;");
1003         foreach($tmp as $sub_key => $sub_name){
1004           $ret[$sub_key] = $sub_name;
1005         }
1006       } 
1007     }
1008     return($ret);
1009   }
1011   function force_dns()
1012   {
1013     if($this->DNSenabled){
1015       /* Only force DNS account, if we have at least on dns Zone */
1016       if(count($this->Zones)){
1017         $this->DNS_is_account  = TRUE;
1018         $this->hide_dns_check_box = TRUE;
1019       }
1020     }
1021   }
1024 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1025 ?>