Code

Updated Network settings.
[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;
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         = false;
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        = DNS::getAvailableZones($config);
123       /* Get Entry 
124        */
125       $this->dnsEntry     = DNS::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  = DNS::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 = DNS::FlipIp(str_replace(".in-addr.arpa","",DNS::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", $this->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     }
234     
235   
236     /**********
237      * DHCP Handling
238      **********/
239  
240     if(isset($_POST['dhcpEditOptions'])){
242       if(count($this->dhcpHostEntry) == 0){
243         $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
244       }else{
245         $this->dialog = new dhcpHost($this->dhcpHostEntry,TRUE);
246       }
247       $this->dialog->cn = $this->cn; 
248       $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress; 
249       if(!empty($this->ipHostNumber)){
250         $this->dialog->statements['fixed-address'] = $this->ipHostNumber; 
251       }
252     }
254     if(isset($_POST['cancel_dhcp'])){
255       $this->dialog = FALSE; 
256     }
258     if(isset($_POST['save_dhcp'])){
259       $this->dialog->save_object();
260       
261       $msgs = $this->dialog->check(array());
262       if(count($msgs)){
263         foreach($msgs as $msg){
264           print_red($msg);
265         }
266       }else{
267         $this->dhcpHostEntry = $this->dialog->save();
268         $this->dialog = FALSE; 
269       }
270     }
272     if(is_object($this->dialog)){
273       $this->dialog->save_object();
274       return($this->dialog->execute());
275     }
276  
277     $smarty->assign("dhcpEnabled",    $this->dhcpEnabled);
278     $smarty->assign("dhcp_is_Account",$this->dhcp_is_Account);
279     $smarty->assign("dhcpParentNode", $this->dhcpParentNode);
280     $smarty->assign("dhcpParentNodes",$this->dhcpParentNodes);
281     $smarty->assign("dhcpParentNodeCnt",count($this->dhcpParentNodes));
284     /**********
285      * DNS Handling
286      **********/
288     /* There is no dns available
289      */
290     $smarty->assign("DNS_is_account",$this->DNS_is_account);
291     $smarty->assign("DNSenabled",$this->DNSenabled);
292     if($this->DNSenabled == false){
294       /* Is IP address must ? */
295 #      $smarty->assign("DNS_is_account",false);
296       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
298       /* Assign smarty all non DNs attributes */
299       foreach($this->attributes as $attr){
300         $smarty->assign($attr,$this->$attr);
301       }
302       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
304       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
305     }else{
306  #     $smarty->assign("DNS_is_account",true);
308       /* Add new empty array to our record list */
309       if(isset($_POST['AddNewRecord'])){
310         $this->dnsEntry['RECORDS'][]  =array("type"=>"aRecord","value"=>"");
311       }
313       /* propose_ip */
314       if(isset($_POST['propose_ip'])){
315         foreach($this->Zones as $key => $name){
316           if($name == $this->dnsEntry['zoneName']){
317             $net = DNS::FlipIp(str_replace(".in-addr.arpa","",DNS::getNameFromMix($key)));
318             $this->ipHostNumber = $this->generateRandomIp($net);
319           }
320         }
321       }
323       /* Handle all posts */
324       $only_once =true;
325       foreach($_POST as $name => $value){
327         /* Check if we have to delete a record entry */
328         if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
330           /* Avoid performing this once again */
331           $only_once = false;
333           /* Extract id for specified entry */
334           $id = preg_replace("/RemoveRecord_/","",$name);
335           $id = preg_replace("/_.*$/","",$id);
337           /* Delete this record, mark edited entries to be able to delete them */
338           if(isset($this->dnsEntry['RECORDS'][$id])){
339             unset($this->dnsEntry['RECORDS'][$id]);
340           }
341         }
342       }
343       /* Assign smarty all non DNs attributes */
344       foreach($this->attributes as $attr){
345         $smarty->assign($attr,$this->$attr);
346       }
348       /* Assign smarty all DNS attributes */
349       foreach($this->DNSattributes as $attr){
350         $smarty->assign($attr,$this->dnsEntry[$attr]);
351       }
353       /* Assign all needed vars */
354  #     $smarty->assign("DNSAccount",$this->DNS_is_account);
355       $smarty->assign("hide_dns_check_box",$this->hide_dns_check_box);
356   
357       $smarty->assign("Zones",$this->Zones);
358       $smarty->assign("ZoneCnt",count($this->Zones));
359       $smarty->assign("ZoneKeys",($this->Zones));
360       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
362       /* Create zone array */
363       $idZones = array();
364       foreach($this->Zones as $id => $zone){
365         if($this->netmaskIsCoherent($id)) {
366           $idZones[$id] = $zone;
367         }else{
368           $idZones[$id] = $zone."&nbsp;("._("Not matching").")";
369         }
370       }
371       $smarty->assign("Zones",$idZones);
372       $smarty->assign("ZoneKeys", $this->Zones);
374       $tmp = $this->generateRecordsList();
376       $changeStateForRecords = $tmp['changeStateForRecords'];
378       $smarty->assign("records",$tmp['str']);
379       $smarty->assign("changeStateForRecords",$changeStateForRecords);
380       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
382       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
383     }
384     return($display);
385   }
388   function remove_from_parent()
389   {
390     if($this->initially_was_account){
392       $ldap = $this->config->get_ldap_link();
394       $tmp = array();
395       $this->dnsEntry['exists'] = false;
396       $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
398       /* Delete dns */
399       foreach($tmp['del'] as $dn => $del){
400         $ldap->cd($dn);
401         $ldap->rmdir_recursive($dn);
402         new log("remove","unknown/".get_class($this),$dn);
403       }
404     }
405   }
408   /* Save data to object */
409   function save_object()
410   {
412     if(isset($_POST['network_tpl_posted'])){
414       /* Save all posted vars */
415       plugin::save_object();
417       /* Handle DHCP Posts*/
418       if($this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
419         foreach($this->dhcpAttributes as $attr){
420           if(isset($_POST[$attr])){
421             $this->$attr = $_POST[$attr];
422           }
423         }
424         if(isset($_POST['dhcp_is_Account'])){
425           $this->dhcp_is_Account = TRUE;
426         }else{
427           $this->dhcp_is_Account = FALSE;
428         }
429       }
431       /* Ge all non dns attributes (IP/MAC)*/
432       foreach($this->attributes as $attr){
433         if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
434           $this->$attr = $_POST[$attr];
435         }
436       }
438       /* Check if DNS should be enabled / disabled */
439       if($this->DNS_is_account && $this->acl_is_removeable() && !isset($_POST['DNS_is_account'])){
440         $this->DNS_is_account = false;
441       }elseif(!$this->DNS_is_account && $this->acl_is_createable() && isset($_POST['DNS_is_account'])){
442         $this->DNS_is_account = true;
443       }
445       /* Get dns attributes */
446       if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
448         /* Check for posted record changes */
449         if(is_array($this->dnsEntry['RECORDS']) && $this->acl_is_writeable("Records")){
450           foreach($this->dnsEntry['RECORDS'] as $key => $value){
452             /* Check if type has changed */
453             if(isset($_POST['RecordTypeSelectedFor_'.$key])){
454               $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
455             }
456             /* Check if value has changed */
457             if(isset($_POST['RecordValue_'.$key])){
458               $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
459             }
460           }
461         }
462         /* Get all basic DNS attributes (TTL, Clas ..)*/
463         foreach($this->DNSattributes as $attr){
464           if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
465             $this->dnsEntry[$attr] = $_POST[$attr];
466           }
467         }
470       }
471       if($this->hide_dns_check_box){
472         $this->DNS_is_account = true;
473       }
474     }
475   }
478   /* Check supplied data */
479   function check()
480   {
481     /* Call common method to give check the hook */
482     $message= plugin::check();
484     if($this->dhcpEnabled && $this->dhcp_is_Account && $this->dhcpParentNode != "" && count($this->dhcpHostEntry) == 0){
485 #      $message[] =_("You have not configured your dhcp settings yet.");
486     }
487     
488     /* Check if mac and ip are already used */
489     if(!empty($this->ipHostNumber) && $this->DNS_is_account && 
490         $this->ipHostNumber != $this->orig_ipHostNumber && 
491         in_array("ip:".$this->ipHostNumber,$this->used_ip_mac)){
492       $message[] =_("The specified IP address is already in use.");
493     }
494     if(!empty($this->macAddress) && $this->dhcp_is_Account && 
495         $this->macAddress != $this->orig_macAddress && 
496         in_array("mac:".$this->macAddress,$this->used_ip_mac)){
497       print_red(sprintf(_("The specified MAC address '%s' for this system '%s' is already in use."),$this->macAddress,$this->cn));
498     }
500     /* Check if ip must be given
501      */  
502     if(($this->IPisMust)||($this->DNS_is_account)){
503       if (empty($this->ipHostNumber)){
504         $message[]= _("The required field 'IP-address' is not set.");
505       }
507       if (!tests::is_ip($this->ipHostNumber)){
508         $message[]= _("Wrong IP format in field IP-address.");
509       }
510     }
512     /* Check if mac is empty 
513      */
514     if ($this->macAddress == "" ){
515       $message[]= _("The required field 'MAC-address' is not set.");
516     }
517     if(!tests::is_mac($this->macAddress)){
518       $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
519     }
521     /* only perfrom this checks if this is a valid DNS account */
522     if($this->DNS_is_account){
524       $checkArray = array();
525       $onlyOnce   = array();
527       //  $onlyOnce['cNAMERecord'] = 0;
528        $tmp = array_flip($this->Zones);
529        $tmp2 = $tmp[$this->dnsEntry['zoneName']];
530        if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
531          $tmp2 = preg_replace("/^.*\//","",$tmp2);
532          $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
533        }
535       /* Walk through all entries and detect duplicates or mismatches
536        */  
537       foreach($this->dnsEntry['RECORDS'] as $name => $values){
539         /* Count record values, to detect duplicate entries for a specific record
540          */
541         if(!isset($checkArray[$values['type']][$values['value']])){
542           $checkArray[$values['type']][$values['value']] = 0;
543         }else{
544           $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
545         }
547         /* Check if given entries in $onlyOnce are used more than once
548          */
549         if(isset($onlyOnce[$values['type']])){
550           $onlyOnce[$values['type']] ++;
551           if($onlyOnce[$values['type']] > 1){
552             $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
553           }
554         }
556         /* Skip txt record ... 
557          */
558         if($values['type'] == "tXTRecord") continue;
560         /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress 
561          */
562         if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
563           $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."), 
564                $this->ipHostNumber);
565         }
567         /* only lower-case is allowed in record entries ... 
568          */
569         if($values['value'] != strtolower($values['value'])){
570           $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
571         }
572       }
573     }
574     return ($message);
575   }
578   /* Save to LDAP */
579   function save()
580   {
581     $ldap= $this->config->get_ldap_link();
582   
583     $dn = $this->parent->dn;
584  
585     /*******************/ 
586     /* IP-MAC HANDLING */
587     /*******************/ 
589     /* $dn was posted as parameter */
590     $this->dn = $dn;
591     
592     /* Save DNS setting & ip/Mac*/
593     plugin::save();
595     /* Write back to ldap */
596     $ldap->cd($this->dn);
597     $this->cleanup();
598     $ldap->modify ($this->attrs); 
600     /****************/ 
601     /* DHCP HANDLING */
602     /****************/ 
603   
604     /* New entry */
605     if($this->dhcpEnabled){
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       if(count($this->dhcpHostEntry) == 0){
623         $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
624         $this->dialog->cn = $this->cn;
625         $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
626         if(!empty($this->ipHostNumber)){
627           $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
628         }
629         $this->dialog->execute();
630         $this->dialog->save_object(); 
631         $this->dhcpHostEntry = $this->dialog->save();
632         if(count($this->dhcpHostEntry['dhcpOption']) == 0){
633           $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
634         }
635       }
637       /* Write mac address to dhcp settings */
638       if($this->dhcp_is_Account){
639         if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) || 
640             !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
641           $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
642           $this->dhcpHostEntry['MODIFIED'] = TRUE;
643         }
644       }
645   
647       /* Unset dhcpStatements if this attribute is empty  */
648       if(isset($this->dhcpHostEntry['dhcpStatements']) && 
649           ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
650         unset($this->dhcpHostEntry['dhcpStatements']);
651       }
652   
653       /* DHCP removed */
654       if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
655         $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
656         show_ldap_error($ldap->get_error(),_("Removing dhcp entry for this object failed."));
658         $tmp = new servdhcp($this->config,$this->dhcpParentNode);
659         $tmp->handle_post_events("remove");
660       }
662       /* DHCP Added */
663       if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
664         $attrs = $this->dhcpHostEntry;
665         unset($attrs['MODIFIED']);
666         unset($attrs['dn']);
667         $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
668         $res = $ldap->add($attrs);
670         $tmp = new servdhcp($this->config,$this->dhcpParentNode);
671         $tmp->handle_post_events("add");
673         show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
674       }
676       /* DHCP still activated */
677       if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
679         /* DHCP node changed */
680         if(($this->initial_dhcpParentNode != $this->dhcpParentNode) || 
681            ($this->cn != $this->OrigCn)){
682           $attrs = $this->dhcpHostEntry;
683           $attrs['cn'] = $this->cn;
684           unset($attrs['dn']);
685           unset($attrs['MODIFIED']);
686           $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
687           $res = $ldap->add($attrs);
689           $tmp = new servdhcp($this->config,$this->dhcpParentNode);
690           $tmp->handle_post_events("modify");
692           show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
693           if($res){
694             $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
695             show_ldap_error($ldap->get_error(),_("Removing old dhcp entry failed."));
696           }
697         }
698          
699         /* SAME node but modified */ 
700         if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1  && 
701             $this->initial_dhcpParentNode == $this->dhcpParentNode){
702           $attrs = $this->dhcpHostEntry;
703           unset($attrs['dn']);
704           unset($attrs['MODIFIED']);
705           $ldap->cd($this->dhcpHostEntry['dn']);
706           $ldap->modify($attrs);
707           
708           $tmp = new servdhcp($this->config,$this->dhcpParentNode);
709           $tmp->handle_post_events("modify");
711           show_ldap_error($ldap->get_error(),_("Modifying dhcp entry failed."));
712         }    
713       }
714     }
715     $this->dialog = FALSE; 
717     /****************/ 
718     /* DNS HANDLING */
719     /****************/ 
721     /* If isn't DNS account but initially was DNS account 
722        remove all DNS entries 
723      */ 
724     if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
725       return;
726     }else{
728       /* Add ipHostNumber to aRecords
729        */
730       $backup_dnsEntry = $this->dnsEntry;
731       if(!empty($this->ipHostNumber)){
732         $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
733         $ptr = $this->get_pTRRecord();
734         if(!empty($ptr)){
735           $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
736         } 
737       }
739       /* Create diff and follow instructions 
740        * If Account was disabled, remove account by setting exists to false
741        */
742       if((!$this->DNS_is_account)&&($this->initially_was_account)){  
743         $this->dnsEntry['exists'] = false;
744         $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
745       }else{
746         $this->dnsEntry['exists'] = $this->DNS_is_account;
747         $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
748       }   
750       /* move follwoing entries 
751        */
752       foreach($tmp['move'] as $src => $dst){
753         $this->recursive_move($src,$dst);
754       }
756       /* Delete dns */
757       foreach($tmp['del'] as $dn => $del){
758         $ldap->cd($dn);
759         $ldap->rmdir_recursive($dn);
760         new log("modify","unknown/".get_class($this),$dn,array("*"),$ldap->get_error());
761       }
763       /* Add || Update new DNS entries 
764        */
765       foreach($tmp['add'] as $dn => $attrs){
766         $ldap->cd($dn);
767         $ldap->cat($dn, array('dn'));
768         if(count($ldap->fetch())){
769           $ldap->cd($dn);
770           $ldap->modify ($attrs); 
771         }else{
772           $ldap->cd($dn);
773           $ldap->add($attrs);
774         }
775         new log("modify","unknown/".get_class($this),$dn,array_keys($attrs),$ldap->get_error());
776       }
779       /* Display errors 
780        */
781       if($ldap->get_error() != "Success"){
782         show_ldap_error($ldap->get_error(), sprintf(_("Saving of terminal/dns account with dn '%s' failed."),$this->dn));
783       }
785       $tmp2 = new servdns($this->config,$this->dn);
786       $tmp2->handle_post_events("modify");
788       $this->dnsEntry =  $backup_dnsEntry;
789     }
790   }
792   /*  Create html table with all used record types
793    */
794   function generateRecordsList()
795   {
796     $changeStateForRecords = "";
797     
798     if(!$this->DNS_is_account) {
799       $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
800       return(array("str" => $str, "changeStateForRecords"=> ""));
801     }
802  
803     $str = "<table summary='' width='100%'>";
804     foreach($this->dnsEntry['RECORDS'] as $key => $entry){
806         $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
807         $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
808         $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
810         $str.=" <tr>".
811           "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
812           "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
813           "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
814           "</tr>";
815     }
817     $str.= "  <tr>".
818            "    <td colspan=2 width='50%'></td><td>".
819            "      <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
820            "    </td>".
821            "  </tr>".
822            "</table>";
823      $ret =  array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
824     return($ret);
825   }
828   /* Create a html select box which allows us to select different types of records 
829    */
830   function generateRecordListBox($selected,$name)
831   {
832     $str = "<select name='".$name."' id='".$name."'>";
833     foreach($this->RecordTypes as $type => $value){
834       $use = "";
835       if($type == $selected){
836         $use = " selected ";
837       }
838       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
839     }
840     $str.="</select>";
841     return($str); 
842   }
845   /* Return plugin informations for acl handling  */ 
846   static function plInfo()
847   {
848     $tmp =  array(
849         "plShortName"   => _("DNS"),
850         "plDescription" => _("DNS settings"),
851         "plSelfModify"  => FALSE,
852         "plDepends"     => array(),
853         "plPriority"    => 5,
854         "plSection"     => array("administration"),
855         "plCategory"    => array("workstation","terminal","phone","server","component","printer","winworkstation"),
857         "plProvidedAcls"=> array(
858           "ipHostNumber"  => _("IP address"),
859           "macAddress"    => _("MAC address"))
860         );
862     /* Hide all dns specific code, if dns is not available
863      */
864     $config = session::get('config');
865     foreach($config->data['TABS']['SERVERSERVICE'] as $tab){
866       if(preg_match("/^servdns$/",$tab['CLASS'])){
867         $tmp['plProvidedAcls']["Records"]        = _("DNS records");
868         $tmp['plProvidedAcls']["zoneName"]       = _("Zone name");
869         $tmp['plProvidedAcls']["dNSTTL"]         = _("TTL");
870         break;
871       }
872     }
873     return($tmp);
874   }
876   
877   function get_dhcp_host_entry()
878   {
879     $attrs = array();
880     $dn = $this->get_dhcp_host_entry_dn();
881     if($dn){
882       $ldap = $this->config->get_ldap_link();
883       $ldap->cd($this->config->current['BASE']);
884       $ldap->cat($dn,array("*"));
885       if($ldap->count()){
886         $attrs = $ldap->fetch();
887         foreach($attrs as $key => $value){
888           if(is_numeric($key) || ($key == "count")){
889             unset($attrs[$key]);
890           }
891           if(is_array($value) && isset($value['count'])){
892             unset($attrs[$key]['count']);
893           }
894         }
895       }
896     }
897     return($attrs);
898   }
901   function get_dhcp_host_entry_dn()
902   {
903     $ldap = $this->config->get_ldap_link();
904     $ldap->cd($this->config->current['BASE']);
905     $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
907     if($ldap->count()){
908       $attr = $ldap->fetch();
909       return($attr['dn']);
910     }else{
911       return("");
912     }
913   }  
916   function get_dhcp_parent_node()
917   {
918     return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
919   }
922   function get_dhcp_parent_nodes()
923   {
924     $ldap = $this->config->get_ldap_link();
925     $ldap->cd($this->config->current['BASE']);
926     $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
927     
928     $dhcp_dns = array();
929     while($attr = $ldap->fetch()){
930       $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
931     }
932  
933     foreach($dhcp_dns as $key => $pri_dns){
934       $ldap->cat($pri_dns,array("cn"));
935       $tmp = $ldap->fetch();
936       if(isset($tmp['cn'][0])){
937         $dhcp_dns[$key] = $tmp['cn'][0];
938       }else{
939         unset($dhcp_dns[$key]);
940       }
941     }
943     $tmp = $tmp2 = array();
944     foreach($dhcp_dns as $dn => $cn){
945       $ldap->cd($dn);
946       $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
947                     "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
948       while($attr = $ldap->fetch()){
949         $tmp[$attr['dn']] = $attr['cn'][0];
950       }
951       $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.")&nbsp;"));
952     }
953     return($tmp2);
954   }
956   
957   /* this function returns the default ptr record entry */
958   function get_pTRRecord()
959   {
960     if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
961       $ldap = $this->config->get_ldap_link();
962       $ldap->cat(DNS::getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
963       $attrs = $ldap->fetch();
964       $tmp = array_flip($this->Zones);
965       $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
966       $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
967       $ptr = preg_replace("/^".normalizePreg(DNS::FlipIp($tmp))."\./","",$this->ipHostNumber);
968       return($ptr);
969     }else{
970       return(FALSE);
971     }
972   }
974   
975   function generateRandomIP($net = "")
976   {
977     $str = $net;
978     $cnt = 4;
979     while(substr_count($str,".") < 3 && $cnt > 0){
980       $str .= ".".rand(0,255);
981       $str = preg_replace("/\.\.*/",".",$str);
982       $str = trim($str,". ");
983       $cnt --;
984     }
985     return($str);
986   }
988   
989   function create_tree($arr,$base,$current = "")
990   {
991     $ret = array();
992     foreach($arr as $r => $name){
993       $base_part = str_replace($base,"",$r);
994       if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
995         $ret[$r] = $current.$name;
996         $tmp = $this->create_tree($arr,$r,$current.".&nbsp;");
997         foreach($tmp as $sub_key => $sub_name){
998           $ret[$sub_key] = $sub_name;
999         }
1000       } 
1001     }
1002     return($ret);
1003   }
1005   function force_dns()
1006   {
1007     if($this->DNSenabled){
1009       /* Only force DNS account, if we have at least on dns Zone */
1010       if(count($this->Zones)){
1011         $this->DNS_is_account  = TRUE;
1012         $this->hide_dns_check_box = TRUE;
1013       }
1014     }
1015   }
1018 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1019 ?>