Code

Udpated gotomasses
[gosa.git] / plugins / admin / systems / class_termDNS.inc
1 <?php
3 class termDNS extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account = true;
7   var $autonet        = false;
9   /* Basic informations 
10    */
11   var $attributes     = array("ipHostNumber","macAddress");
12   var $objectclasses  = array("whatever");
14   var $ipHostNumber   = "";    // IP address 
15   var $macAddress     = "";    // Mac address 
17   var $orig_ipHostNumber   = "";    // IP address 
18   var $orig_macAddress     = "";    // Mac address 
20   var $cn             = "";    // CN of currently edited device 
21   var $OrigCn         = "";    // Initial cn
22   var $IPisMust       = false;
23   var $MACisMust      = false;
24   var $dialog;
26   /* DCHP Attributes 
27    */
28   var $dhcpAttributes           = array("dhcpParentNode");
29   var $dhcpEnabled              = FALSE;
30   var $dhcp_is_Account          = FALSE;
31   var $dhcpParentNodes          = array();
32   var $dhcpParentNode           = "";
33   var $dhcpHostEntry            = array();
34   var $initial_dhcpParentNode   = "";
35   var $initial_dhcpHostEntry    = array();
36   var $initial_dhcp_is_Account  = FALSE;
38   var $used_ip_mac              = array();  
40   /* DNS attributes  
41    */
42   var $DNSattributes            = array("dNSClass","zoneName","dNSTTL");
43   var $DNS_is_account           = false;
44   var $initially_was_account = false;
45   var $dnsEntry                 = array();
46   var $DNSenabled               = false;
47   var $hide_dns_check_box       = FALSE;
49   /*  Terminal dns 
50    */
51   function termDNS (&$config, $parent,$objectClasses,$IPisMust = false)
52   {
53     /* We need to know which objectClasses are used, to store the ip/mac
54      * Because of different type of devices   
55      */ 
56     $this->parent         = $parent;
57     $this->objectclasses  =  $objectClasses;
58     $this->IPisMust       = $IPisMust;
60     plugin::plugin ($config, $parent->dn);
62     if(isset($this->attrs['cn'][0])){
63       $this->OrigCn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
64       $this->cn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
65     }
67  
68     /************
69      * DHCP 
70      ************/
72     /* Hide all dhcp specific code, if dhcp plugin is not present in config */
73     $dhcpEnabled = FALSE;
74     if($this->config->search("servdhcp","class",array("tabs"))){
75       $this->dhcpEnabled = TRUE;
76     }
77     if($this->dhcpEnabled){
78       $this->dhcpParentNodes = $this->get_dhcp_parent_nodes();
79       $this->dhcpParentNode  = $this->get_dhcp_parent_node();
80       if($this->dhcpParentNode){
81         $this->dhcp_is_Account = TRUE;
82         $this->initial_dhcp_is_Account = TRUE;
83         $this->dhcpHostEntry  = $this->get_dhcp_host_entry();    
84       }
85       $this->initial_dhcpHostEntry = $this->dhcpHostEntry;
86       $this->initial_dhcpParentNode= $this->dhcpParentNode;
87     }
90     /************
91      * Autonetwork hook 
92      ************/
93  
94     /* Do we have autonet support? */
95     if (isset($this->config->data['MAIN']['AUTO_NETWORK_HOOK'])){
96       $this->autonet= true;
97     }
100     /************
101      * DNS
102      ************/
103  
104     /* Hide all dns specific code, if dns is not available 
105      */
106     $DNSenabled = false;
107     foreach($this->config->data['TABS']['SERVERSERVICE'] as $tab){
108       if(preg_match("/^servdns$/",$tab['CLASS'])){
109         $this->DNSenabled = true;
110       }
111     }
112     if(!$this->DNSenabled){
113       $this->DNS_is_account = false;
114       return;
115     }
117     if($this->DNSenabled){
119       /* Get Zones  
120        */
121       $this->Zones        = getAvailableZones($config);
123       /* Get Entry 
124        */
125       $this->dnsEntry     = getDNSHostEntries($config,$this->OrigCn);
127       /* Remove A record which equals $this->ipHostNumber
128        */
129       $ptr = $this->get_pTRRecord();
130       foreach($this->dnsEntry['RECORDS'] as $key => $rec){
131         if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
132           unset($this->dnsEntry['RECORDS'][$key]);
133         }
134         if(($rec['type'] == "pTRRecord") && ($rec['value'] == $ptr)){
135           unset($this->dnsEntry['RECORDS'][$key]);
136         }
137       }
139       /* Get Record types 
140        */
141       $this->RecordTypes  = getDnsRecordTypes();
143       /* If there is at least one entry in this -> types, we have DNS enabled 
144        */
145       if($this->dnsEntry['exists']){
146         $this->DNS_is_account = true;
147       }else{
148         $this->DNS_is_account = false;
149       }
150     }
152     /* Create a list of used mac and ip addresses */
153     $ldap = $this->config->get_ldap_link();
154     $ldap->cd($this->config->current['BASE']);
155     $ldap->search("(|(macAddress=*)(ipHostNumber=*))",array("macAddress","ipHostNumber"));
156     while($attrs = $ldap->fetch()){
157       if(isset($attrs['ipHostNumber'][0])){
158         $this->used_ip_mac["ip:".$attrs['ipHostNumber'][0]] = "ip:".$attrs['ipHostNumber'][0];
159       }
160       if(isset($attrs['macAddress'][0])){
161         $this->used_ip_mac["mac:".$attrs['macAddress'][0]] = "mac:".$attrs['macAddress'][0];
162       }
163     } 
165     /* Save initial ip and mac values, to be able 
166         check if the used values are already in use */ 
167     $this->orig_ipHostNumber   = $this->ipHostNumber;  
168     $this->orig_macAddress     = $this->macAddress;
169  
170     /* Store initally account settings 
171      */
172     $this->initially_was_account = $this->DNS_is_account;
174     if($this->DNS_is_account){
175       new log("view","unknown/".get_class($this),$this->dn);
176     }
177   }
180   function netmaskIsCoherent($idZone) 
181   {
182     $netmask = FlipIp(str_replace(".in-addr.arpa","",getNameFromMix($idZone)));
183     if(!strstr($this->ipHostNumber, $netmask)){
184       return false;
185     }else{
186       return true;
187     }
188   }
191   function getVarsForSaving($attrs) 
192   {
193     foreach($this->attributes as $attr){
194       if(!empty($this->$attr)){
195         $attrs[$attr] = $this->$attr;
196       }
197     }
198     return($attrs); 
199   }
201   function execute()
202   {
203           /* Call parent execute */
204     $smarty= get_smarty();
206     $tmp = $this->plInfo();
207     foreach($tmp['plProvidedAcls'] as $name => $translation){
208       $smarty->assign($name."ACL",$this->getacl($name));
209     }
211     $display= "";
213     $smarty->assign("staticAddress", ""); 
214     $smarty->assign("autonet", "");
215  
216     /* Check for autonet button */
217     if ($this->autonet && isset($_POST['autonet'])){
218       $cmd= $this->config->data['MAIN']['AUTO_NETWORK_HOOK'];
219       if(!empty($cmd) && $this->cn != ""){
220         $res = shell_exec($cmd." ".$this->cn);
221         if(!$res){
222           print_red(sprintf(_("Can't execute specified AUTO_NETWORK_HOOK '%s'. Please check your gosa.conf."),$cmd));
223         } else {
224           $res= split(';', trim($res));
225           if (isset($res[0]) && $res[0] != ""){
226             $this->ipHostNumber= $res[0];
227           }
228           if (isset($res[1]) && $res[1] != ""){
229             $this->macAddress= $res[1];
230           }
231         }
232       }
233     }
235     
236   
237     /**********
238      * DHCP Handling
239      **********/
240  
241     if(isset($_POST['dhcpEditOptions'])){
243       if(count($this->dhcpHostEntry) == 0){
244         $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
245       }else{
246         $this->dialog = new dhcpHost($this->dhcpHostEntry,TRUE);
247       }
248       $this->dialog->cn = $this->cn; 
249       $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress; 
250       if(!empty($this->ipHostNumber)){
251         $this->dialog->statements['fixed-address'] = $this->ipHostNumber; 
252       }
253     }
255     if(isset($_POST['cancel_dhcp'])){
256       $this->dialog = FALSE; 
257     }
259     if(isset($_POST['save_dhcp'])){
260       $this->dialog->save_object();
261       
262       $msgs = $this->dialog->check(array());
263       if(count($msgs)){
264         foreach($msgs as $msg){
265           print_red($msg);
266         }
267       }else{
268         $this->dhcpHostEntry = $this->dialog->save();
269         $this->dialog = FALSE; 
270       }
271     }
273     if(is_object($this->dialog)){
274       $this->dialog->save_object();
275       return($this->dialog->execute());
276     }
277  
278     $smarty->assign("dhcpEnabled",    $this->dhcpEnabled);
279     $smarty->assign("dhcp_is_Account",$this->dhcp_is_Account);
280     $smarty->assign("dhcpParentNode", $this->dhcpParentNode);
281     $smarty->assign("dhcpParentNodes",$this->dhcpParentNodes);
282     $smarty->assign("dhcpParentNodeCnt",count($this->dhcpParentNodes));
285     /* There is no dns available
286      */
287     if($this->DNSenabled == false){
289       /* Is IP address must ? */
290       $smarty->assign("DNS_is_account",false);
291       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
293       /* Assign smarty all non DNs attributes */
294       foreach($this->attributes as $attr){
295         $smarty->assign($attr,$this->$attr);
296       }
297       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
299       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
300     }else{
301       $smarty->assign("DNS_is_account",true);
303       /* Add new empty array to our record list */
304       if(isset($_POST['AddNewRecord'])){
305         $this->dnsEntry['RECORDS'][]  =array("type"=>"aRecord","value"=>"");
306       }
308       /* Handle all posts */
309       $only_once =true;
310       foreach($_POST as $name => $value){
312         /* Check if we have to delete a record entry */
313         if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
315           /* Avoid performing this once again */
316           $only_once = false;
318           /* Extract id for specified entry */
319           $id = preg_replace("/RemoveRecord_/","",$name);
320           $id = preg_replace("/_.*$/","",$id);
322           /* Delete this record, mark edited entries to be able to delete them */
323           if(isset($this->dnsEntry['RECORDS'][$id])){
324             unset($this->dnsEntry['RECORDS'][$id]);
325           }
326         }
327       }
328       /* Assign smarty all non DNs attributes */
329       foreach($this->attributes as $attr){
330         $smarty->assign($attr,$this->$attr);
331       }
333       /* Assign smarty all DNS attributes */
334       foreach($this->DNSattributes as $attr){
335         $smarty->assign($attr,$this->dnsEntry[$attr]);
336       }
338       /* Assign all needed vars */
339       $smarty->assign("DNSAccount",$this->DNS_is_account);
340       $smarty->assign("hide_dns_check_box",$this->hide_dns_check_box);
341   
342       $smarty->assign("Zones",$this->Zones);
343       $smarty->assign("ZoneCnt",count($this->Zones));
344       $smarty->assign("ZoneKeys",($this->Zones));
345       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
347       /* Create zone array */
348       $idZones = array();
349       foreach($this->Zones as $id => $zone){
350         if($this->netmaskIsCoherent($id)) {
351           $idZones[$id] = $zone;
352         }else{
353           $idZones[$id] = $zone."&nbsp;("._("Not matching").")";
354         }
355       }
356       $smarty->assign("Zones",$idZones);
357       $smarty->assign("ZoneKeys", $this->Zones);
359       $tmp = $this->generateRecordsList();
361       $changeStateForRecords = $tmp['changeStateForRecords'];
363       $smarty->assign("records",$tmp['str']);
364       $smarty->assign("changeStateForRecords",$changeStateForRecords);
365       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
367       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
368     }
369     return($display);
370   }
373   function remove_from_parent()
374   {
375     if($this->initially_was_account){
377       $ldap = $this->config->get_ldap_link();
379       $tmp = array();
380       $this->dnsEntry['exists'] = false;
381       $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
383       /* Delete dns */
384       foreach($tmp['del'] as $dn => $del){
385         $ldap->cd($dn);
386         $ldap->rmdir_recursive($dn);
387         new log("remove","unknown/".get_class($this),$dn);
388       }
389     }
390   }
393   /* Save data to object */
394   function save_object()
395   {
397     if(isset($_POST['network_tpl_posted'])){
399       /* Save all posted vars */
400       plugin::save_object();
402       /* Handle DHCP Posts*/
403       if($this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
404         foreach($this->dhcpAttributes as $attr){
405           if(isset($_POST[$attr])){
406             $this->$attr = $_POST[$attr];
407           }
408         }
409         if(isset($_POST['dhcp_is_Account'])){
410           $this->dhcp_is_Account = TRUE;
411         }else{
412           $this->dhcp_is_Account = FALSE;
413         }
414       }
416       /* Ge all non dns attributes (IP/MAC)*/
417       foreach($this->attributes as $attr){
418         if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
419           $this->$attr = $_POST[$attr];
420         }
421       }
423       /* Check if DNS should be enabled / disabled */
424       if($this->DNS_is_account && $this->acl_is_removeable() && !isset($_POST['DNS_is_account'])){
425         $this->DNS_is_account = false;
426       }elseif(!$this->DNS_is_account && $this->acl_is_createable() && isset($_POST['DNS_is_account'])){
427         $this->DNS_is_account = true;
428       }
430       /* Get dns attributes */
431       if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
433         /* Check for posted record changes */
434         if(is_array($this->dnsEntry['RECORDS']) && $this->acl_is_writeable("Records")){
435           foreach($this->dnsEntry['RECORDS'] as $key => $value){
437             /* Check if type has changed */
438             if(isset($_POST['RecordTypeSelectedFor_'.$key])){
439               $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
440             }
441             /* Check if value has changed */
442             if(isset($_POST['RecordValue_'.$key])){
443               $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
444             }
445           }
446         }
447         /* Get all basic DNS attributes (TTL, Clas ..)*/
448         foreach($this->DNSattributes as $attr){
449           if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
450             $this->dnsEntry[$attr] = $_POST[$attr];
451           }
452         }
455       }
456       if($this->hide_dns_check_box){
457         $this->DNS_is_account = true;
458       }
459     }
460   }
463   /* Check supplied data */
464   function check()
465   {
466     /* Call common method to give check the hook */
467     $message= plugin::check();
469     if($this->dhcpEnabled && $this->dhcp_is_Account && $this->dhcpParentNode != "" && count($this->dhcpHostEntry) == 0){
470 #      $message[] =_("You have not configured your dhcp settings yet.");
471     }
472     
473     /* Check if mac and ip are already used */
474     if(!empty($this->ipHostNumber) && $this->DNS_is_account && 
475         $this->ipHostNumber != $this->orig_ipHostNumber && 
476         in_array("ip:".$this->ipHostNumber,$this->used_ip_mac)){
477       $message[] =_("The specified IP address is already in use.");
478     }
479     if(!empty($this->macAddress) && $this->dhcp_is_Account && 
480         $this->macAddress != $this->orig_macAddress && 
481         in_array("mac:".$this->macAddress,$this->used_ip_mac)){
482       $message[] =_("The specified MAC address is already in use.");
483     }
485     /* Check if ip must be given
486      */  
487     if(($this->IPisMust)||($this->DNS_is_account)){
488       if (empty($this->ipHostNumber)){
489         $message[]= _("The required field 'IP-address' is not set.");
490       }
492       if (!is_ip($this->ipHostNumber)){
493         $message[]= _("Wrong IP format in field IP-address.");
494       }
495     }
497     /* Check if mac is empty 
498      */
499     if ($this->macAddress == "" ){
500       $message[]= _("The required field 'MAC-address' is not set.");
501     }
502     if(!is_mac($this->macAddress)){
503       $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
504     }
506     /* only perfrom this checks if this is a valid DNS account */
507     if($this->DNS_is_account){
509       $checkArray = array();
510       $onlyOnce   = array();
512       //  $onlyOnce['cNAMERecord'] = 0;
513        $tmp = array_flip($this->Zones);
514        $tmp2 = $tmp[$this->dnsEntry['zoneName']];
515        if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
516          $tmp2 = preg_replace("/^.*\//","",$tmp2);
517          $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
518        }
520       /* Walk through all entries and detect duplicates or mismatches
521        */  
522       foreach($this->dnsEntry['RECORDS'] as $name => $values){
524         /* Count record values, to detect duplicate entries for a specific record
525          */
526         if(!isset($checkArray[$values['type']][$values['value']])){
527           $checkArray[$values['type']][$values['value']] = 0;
528         }else{
529           $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
530         }
532         /* Check if given entries in $onlyOnce are used more than once
533          */
534         if(isset($onlyOnce[$values['type']])){
535           $onlyOnce[$values['type']] ++;
536           if($onlyOnce[$values['type']] > 1){
537             $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
538           }
539         }
541         /* Skip txt record ... 
542          */
543         if($values['type'] == "tXTRecord") continue;
545         /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress 
546          */
547         if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
548           $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."), 
549                $this->ipHostNumber);
550         }
552         /* only lower-case is allowed in record entries ... 
553          */
554         if($values['value'] != strtolower($values['value'])){
555           $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
556         }
557       }
558     }
559     return ($message);
560   }
563   /* Save to LDAP */
564   function save()
565   {
566     $ldap= $this->config->get_ldap_link();
567   
568     $dn = $this->parent->dn;
569  
570     /*******************/ 
571     /* IP-MAC HANDLING */
572     /*******************/ 
574     /* $dn was posted as parameter */
575     $this->dn = $dn;
576     
577     /* Save DNS setting & ip/Mac*/
578     plugin::save();
580     /* Write back to ldap */
581     $ldap->cd($this->dn);
582     $this->cleanup();
583     $ldap->modify ($this->attrs); 
585     /****************/ 
586     /* DHCP HANDLING */
587     /****************/ 
588   
589     /* New entry */
590     if($this->dhcpEnabled){
592       if(count($this->dhcpHostEntry) == 0){
593         $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
594         $this->dialog->cn = $this->cn;
595         $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
596         if(!empty($this->ipHostNumber)){
597           $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
598         }
599         $this->dialog->execute();
600         $this->dialog->save_object(); 
601         $this->dhcpHostEntry = $this->dialog->save();
602         if(count($this->dhcpHostEntry['dhcpOption']) == 0){
603           $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
604         }
605       }
607       if(count($this->dhcpHostEntry) == 0){
608         $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
609         $this->dialog->cn = $this->cn;
610         $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
611         if(!empty($this->ipHostNumber)){
612           $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
613         }
614         $this->dialog->execute();
615         $this->dialog->save_object(); 
616         $this->dhcpHostEntry = $this->dialog->save();
617         if(count($this->dhcpHostEntry['dhcpOption']) == 0){
618           $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
619         }
620       }
622       /* Write mac address to dhcp settings */
623       if($this->dhcp_is_Account){
624         if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) || 
625             !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
626           $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
627           $this->dhcpHostEntry['MODIFIED'] = TRUE;
628         }
629       }
630   
632       /* Unset dhcpStatements if this attribute is empty  */
633       if(isset($this->dhcpHostEntry['dhcpStatements']) && 
634           ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
635         unset($this->dhcpHostEntry['dhcpStatements']);
636       }
637   
638       /* DHCP removed */
639       if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
640         $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
641         show_ldap_error($ldap->get_error(),_("Removing dhcp entry for this object failed."));
643         $tmp = new servdhcp($this->config,$this->dhcpParentNode);
644         $tmp->handle_post_events("remove");
645       }
647       /* DHCP Added */
648       if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
649         $attrs = $this->dhcpHostEntry;
650         unset($attrs['MODIFIED']);
651         unset($attrs['dn']);
652         $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
653         $res = $ldap->add($attrs);
655         $tmp = new servdhcp($this->config,$this->dhcpParentNode);
656         $tmp->handle_post_events("add");
658         show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
659       }
661       /* DHCP still activated */
662       if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
664         /* DHCP node changed */
665         if(($this->initial_dhcpParentNode != $this->dhcpParentNode) || 
666            ($this->cn != $this->OrigCn)){
667           $attrs = $this->dhcpHostEntry;
668           $attrs['cn'] = $this->cn;
669           unset($attrs['dn']);
670           unset($attrs['MODIFIED']);
671           $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
672           $res = $ldap->add($attrs);
674           $tmp = new servdhcp($this->config,$this->dhcpParentNode);
675           $tmp->handle_post_events("modify");
677           show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
678           if($res){
679             $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
680             show_ldap_error($ldap->get_error(),_("Removing old dhcp entry failed."));
681           }
682         }
683          
684         /* SAME node but modified */ 
685         if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1  && 
686             $this->initial_dhcpParentNode == $this->dhcpParentNode){
687           $attrs = $this->dhcpHostEntry;
688           unset($attrs['dn']);
689           unset($attrs['MODIFIED']);
690           $ldap->cd($this->dhcpHostEntry['dn']);
691           $ldap->modify($attrs);
692           
693           $tmp = new servdhcp($this->config,$this->dhcpParentNode);
694           $tmp->handle_post_events("modify");
696           show_ldap_error($ldap->get_error(),_("Modifying dhcp entry failed."));
697         }    
698       }
699     }
700       
702     /****************/ 
703     /* DNS HANDLING */
704     /****************/ 
706     /* If isn't DNS account but initially was DNS account 
707        remove all DNS entries 
708      */ 
709     if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
710       return;
711     }else{
713       /* Add ipHostNumber to aRecords
714        */
715       $backup_dnsEntry = $this->dnsEntry;
716       if(!empty($this->ipHostNumber)){
717         $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
718         $ptr = $this->get_pTRRecord();
719         if(!empty($ptr)){
720           $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
721         } 
722       }
724       /* Create diff and follow instructions 
725        * If Account was disabled, remove account by setting exists to false
726        */
727       if((!$this->DNS_is_account)&&($this->initially_was_account)){  
728         $this->dnsEntry['exists'] = false;
729         $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
730       }else{
731         $this->dnsEntry['exists'] = $this->DNS_is_account;
732         $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
733       }   
735       /* move follwoing entries 
736        */
737       foreach($tmp['move'] as $src => $dst){
738         $this->recursive_move($src,$dst);
739       }
741       /* Delete dns */
742       foreach($tmp['del'] as $dn => $del){
743         $ldap->cd($dn);
744         $ldap->rmdir_recursive($dn);
745         new log("modify","unknown/".get_class($this),$dn,array("*"),$ldap->get_error());
746       }
748       /* Add || Update new DNS entries 
749        */
750       foreach($tmp['add'] as $dn => $attrs){
751         $ldap->cd($dn);
752         $ldap->cat($dn, array('dn'));
753         if(count($ldap->fetch())){
754           $ldap->cd($dn);
755           $ldap->modify ($attrs); 
756         }else{
757           $ldap->cd($dn);
758           $ldap->add($attrs);
759         }
760         new log("modify","unknown/".get_class($this),$dn,array_keys($attrs),$ldap->get_error());
761       }
764       /* Display errors 
765        */
766       if($ldap->get_error() != "Success"){
767         show_ldap_error($ldap->get_error(), sprintf(_("Saving of terminal/dns account with dn '%s' failed."),$this->dn));
768       }
770       $tmp2 = new servdns($this->config,$this->dn);
771       $tmp2->handle_post_events("modify");
773       $this->dnsEntry =  $backup_dnsEntry;
774     }
775   }
777   /*  Create html table with all used record types
778    */
779   function generateRecordsList()
780   {
781     $changeStateForRecords = "";
782     
783     if(!$this->DNS_is_account) {
784       $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
785       return(array("str" => $str, "changeStateForRecords"=> ""));
786     }
787  
788     $str = "<table summary='' width='100%'>";
789     foreach($this->dnsEntry['RECORDS'] as $key => $entry){
791         $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
792         $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
793         $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
795         $str.=" <tr>".
796           "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
797           "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
798           "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
799           "</tr>";
800     }
802     $str.= "  <tr>".
803            "    <td colspan=2 width='50%'></td><td>".
804            "      <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
805            "    </td>".
806            "  </tr>".
807            "</table>";
808      $ret =  array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
809     return($ret);
810   }
813   /* Create a html select box which allows us to select different types of records 
814    */
815   function generateRecordListBox($selected,$name)
816   {
817     $str = "<select name='".$name."' id='".$name."'>";
818     foreach($this->RecordTypes as $type => $value){
819       $use = "";
820       if($type == $selected){
821         $use = " selected ";
822       }
823       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
824     }
825     $str.="</select>";
826     return($str); 
827   }
830   /* Return plugin informations for acl handling  */ 
831   function plInfo()
832   {
833     $tmp =  array(
834         "plShortName"   => _("DNS"),
835         "plDescription" => _("DNS settings"),
836         "plSelfModify"  => FALSE,
837         "plDepends"     => array(),
838         "plPriority"    => 5,
839         "plSection"     => array("administration"),
840         "plCategory"    => array("workstation","terminal","phone","server","component","printer","winworkstation"),
842         "plProvidedAcls"=> array(
843           "ipHostNumber"  => _("IP address"),
844           "macAddress"    => _("MAC address"))
845         );
847     /* Hide all dns specific code, if dns is not available
848      */
849     foreach($_SESSION['config']->data['TABS']['SERVERSERVICE'] as $tab){
850       if(preg_match("/^servdns$/",$tab['CLASS'])){
851         $tmp['plProvidedAcls']["Records"]        = _("DNS records");
852         $tmp['plProvidedAcls']["zoneName"]       = _("Zone name");
853         $tmp['plProvidedAcls']["dNSTTL"]         = _("TTL");
854         break;
855       }
856     }
857     return($tmp);
858   }
860   
861   function get_dhcp_host_entry()
862   {
863     $attrs = array();
864     $dn = $this->get_dhcp_host_entry_dn();
865     if($dn){
866       $ldap = $this->config->get_ldap_link();
867       $ldap->cd($this->config->current['BASE']);
868       $ldap->cat($dn,array("*"));
869       if($ldap->count()){
870         $attrs = $ldap->fetch();
871         foreach($attrs as $key => $value){
872           if(is_numeric($key) || ($key == "count")){
873             unset($attrs[$key]);
874           }
875           if(is_array($value) && isset($value['count'])){
876             unset($attrs[$key]['count']);
877           }
878         }
879       }
880     }
881     return($attrs);
882   }
885   function get_dhcp_host_entry_dn()
886   {
887     $ldap = $this->config->get_ldap_link();
888     $ldap->cd($this->config->current['BASE']);
889     $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
891     if($ldap->count()){
892       $attr = $ldap->fetch();
893       return($attr['dn']);
894     }else{
895       return("");
896     }
897   }  
900   function get_dhcp_parent_node()
901   {
902     return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
903   }
906   function get_dhcp_parent_nodes()
907   {
908     $ldap = $this->config->get_ldap_link();
909     $ldap->cd($this->config->current['BASE']);
910     $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
911     
912     $dhcp_dns = array();
913     while($attr = $ldap->fetch()){
914       $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
915     }
916  
917     foreach($dhcp_dns as $key => $pri_dns){
918       $ldap->cat($pri_dns,array("cn"));
919       $tmp = $ldap->fetch();
920       if(isset($tmp['cn'][0])){
921         $dhcp_dns[$key] = $tmp['cn'][0];
922       }else{
923         unset($dhcp_dns[$key]);
924       }
925     }
927     $tmp = $tmp2 = array();
928     foreach($dhcp_dns as $dn => $cn){
929       $ldap->cd($dn);
930       $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
931                     "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
932       while($attr = $ldap->fetch()){
933         $tmp[$attr['dn']] = $attr['cn'][0];
934       }
935       $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.")&nbsp;"));
936     }
937     return($tmp2);
938   }
940   
941   /* this function returns the default ptr record entry */
942   function get_pTRRecord()
943   {
944     if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
945       $ldap = $this->config->get_ldap_link();
946       $ldap->cat(getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
947       $attrs = $ldap->fetch();
948       $tmp = array_flip($this->Zones);
949       $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
950       $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
951       $ptr = preg_replace("/^".normalizePreg(FlipIp($tmp))."\./","",$this->ipHostNumber);
952       return($ptr);
953     }else{
954       return(FALSE);
955     }
956   }
958   
959   function create_tree($arr,$base,$current = "")
960   {
961     $ret = array();
962     foreach($arr as $r => $name){
963       $base_part = str_replace($base,"",$r);
964       if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
965         $ret[$r] = $current.$name;
966         $tmp = $this->create_tree($arr,$r,$current.".&nbsp;");
967         foreach($tmp as $sub_key => $sub_name){
968           $ret[$sub_key] = $sub_name;
969         }
970       } 
971     }
972     return($ret);
973   }
975   function force_dns()
976   {
977     if($this->DNSenabled){
978       $this->DNS_is_account  = TRUE;
979       $this->hide_dns_check_box = TRUE;
980     }
981   }
984 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
985 ?>