Code

Updated tabs.
[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);
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       }
409     }
410   }
413   /* Save data to object */
414   function save_object()
415   {
417     if(isset($_POST['network_tpl_posted'])){
419       /* Save all posted vars */
420       plugin::save_object();
422       /* Handle DHCP Posts*/
423       if($this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
424         foreach($this->dhcpAttributes as $attr){
425           if(isset($_POST[$attr])){
426             $this->$attr = $_POST[$attr];
427           }
428         }
429         if(isset($_POST['dhcp_is_Account'])){
430           $this->dhcp_is_Account = TRUE;
431         }else{
432           $this->dhcp_is_Account = FALSE;
433         }
434       }
436       /* Ge all non dns attributes (IP/MAC)*/
437       foreach($this->attributes as $attr){
438         if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
439           $this->$attr = $_POST[$attr];
440         }
441       }
443       /* Check if DNS should be enabled / disabled */
444       if($this->DNS_is_account && $this->acl_is_removeable() && !isset($_POST['DNS_is_account'])){
445         $this->DNS_is_account = false;
446       }elseif(!$this->DNS_is_account && $this->acl_is_createable() && isset($_POST['DNS_is_account'])){
447         $this->DNS_is_account = true;
448       }
450       /* Get dns attributes */
451       if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
453         /* Check for posted record changes */
454         if(is_array($this->dnsEntry['RECORDS']) && $this->acl_is_writeable("Records")){
455           foreach($this->dnsEntry['RECORDS'] as $key => $value){
457             /* Check if type has changed */
458             if(isset($_POST['RecordTypeSelectedFor_'.$key])){
459               $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
460             }
461             /* Check if value has changed */
462             if(isset($_POST['RecordValue_'.$key])){
463               $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
464             }
465           }
466         }
467         /* Get all basic DNS attributes (TTL, Clas ..)*/
468         foreach($this->DNSattributes as $attr){
469           if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
470             $this->dnsEntry[$attr] = $_POST[$attr];
471           }
472         }
475       }
476       if($this->hide_dns_check_box){
477         $this->DNS_is_account = true;
478       }
479     }
480   }
483   /* Check supplied data */
484   function check()
485   {
486     /* Call common method to give check the hook */
487     $message= plugin::check();
489     if($this->dhcpEnabled && $this->dhcp_is_Account && $this->dhcpParentNode != "" && count($this->dhcpHostEntry) == 0){
490 #      $message[] =_("You have not configured your dhcp settings yet.");
491     }
492     
493     /* Check if mac and ip are already used */
494     if(!empty($this->ipHostNumber) && $this->DNS_is_account && 
495         $this->ipHostNumber != $this->orig_ipHostNumber && 
496         in_array("ip:".$this->ipHostNumber,$this->used_ip_mac)){
497       $message[] =_("The specified IP address is already in use.");
498     }
499     if(!empty($this->macAddress) && $this->dhcp_is_Account && 
500         $this->macAddress != $this->orig_macAddress && 
501         in_array("mac:".$this->macAddress,$this->used_ip_mac)){
502       print_red(sprintf(_("The specified MAC address '%s' for this system '%s' is already in use."),$this->macAddress,$this->cn));
503     }
505     /* Check if ip must be given
506      */  
507     if(($this->IPisMust)||($this->DNS_is_account)){
508       if (empty($this->ipHostNumber)){
509         $message[]= _("The required field 'IP-address' is not set.");
510       }
512       if (!tests::is_ip($this->ipHostNumber)){
513         $message[]= _("Wrong IP format in field IP-address.");
514       }
515     }
517     /* Check if mac is empty 
518      */
519     if ($this->macAddress == "" ){
520       $message[]= _("The required field 'MAC-address' is not set.");
521     }
522     if(!tests::is_mac($this->macAddress)){
523       $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
524     }
526     /* only perfrom this checks if this is a valid DNS account */
527     if($this->DNS_is_account){
529       $checkArray = array();
530       $onlyOnce   = array();
532       //  $onlyOnce['cNAMERecord'] = 0;
533        $tmp = array_flip($this->Zones);
534        $tmp2 = $tmp[$this->dnsEntry['zoneName']];
535        if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
536          $tmp2 = preg_replace("/^.*\//","",$tmp2);
537          $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
538        }
540       /* Walk through all entries and detect duplicates or mismatches
541        */  
542       foreach($this->dnsEntry['RECORDS'] as $name => $values){
544         /* Count record values, to detect duplicate entries for a specific record
545          */
546         if(!isset($checkArray[$values['type']][$values['value']])){
547           $checkArray[$values['type']][$values['value']] = 0;
548         }else{
549           $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
550         }
552         /* Check if given entries in $onlyOnce are used more than once
553          */
554         if(isset($onlyOnce[$values['type']])){
555           $onlyOnce[$values['type']] ++;
556           if($onlyOnce[$values['type']] > 1){
557             $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
558           }
559         }
561         /* Skip txt record ... 
562          */
563         if($values['type'] == "tXTRecord") continue;
565         /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress 
566          */
567         if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
568           $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."), 
569                $this->ipHostNumber);
570         }
572         /* only lower-case is allowed in record entries ... 
573          */
574         if($values['value'] != strtolower($values['value'])){
575           $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
576         }
577       }
578     }
579     return ($message);
580   }
583   /* Save to LDAP */
584   function save()
585   {
586     $ldap= $this->config->get_ldap_link();
587   
588     $dn = $this->parent->dn;
589  
590     /*******************/ 
591     /* IP-MAC HANDLING */
592     /*******************/ 
594     /* $dn was posted as parameter */
595     $this->dn = $dn;
596     
597     /* Save DNS setting & ip/Mac*/
598     plugin::save();
600     /* Write back to ldap */
601     $ldap->cd($this->dn);
602     $this->cleanup();
603     $ldap->modify ($this->attrs); 
605     /****************/ 
606     /* DHCP HANDLING */
607     /****************/ 
608   
609     /* New entry */
610     if($this->dhcpEnabled){
612       if(count($this->dhcpHostEntry) == 0){
613         $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
614         $this->dialog->cn = $this->cn;
615         $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
616         if(!empty($this->ipHostNumber)){
617           $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
618         }
619         $this->dialog->execute();
620         $this->dialog->save_object(); 
621         $this->dhcpHostEntry = $this->dialog->save();
622         if(count($this->dhcpHostEntry['dhcpOption']) == 0){
623           $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
624         }
625       }
627       if(count($this->dhcpHostEntry) == 0){
628         $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
629         $this->dialog->cn = $this->cn;
630         $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
631         if(!empty($this->ipHostNumber)){
632           $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
633         }
634         $this->dialog->execute();
635         $this->dialog->save_object(); 
636         $this->dhcpHostEntry = $this->dialog->save();
637         if(count($this->dhcpHostEntry['dhcpOption']) == 0){
638           $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
639         }
640       }
642       /* Write mac address to dhcp settings */
643       if($this->dhcp_is_Account){
644         if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) || 
645             !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
646           $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
647           $this->dhcpHostEntry['MODIFIED'] = TRUE;
648         }
649       }
650   
652       /* Unset dhcpStatements if this attribute is empty  */
653       if(isset($this->dhcpHostEntry['dhcpStatements']) && 
654           ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
655         unset($this->dhcpHostEntry['dhcpStatements']);
656       }
657   
658       /* DHCP removed */
659       if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
660         $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
661         show_ldap_error($ldap->get_error(),_("Removing dhcp entry for this object failed."));
663         $tmp = new servdhcp($this->config,$this->dhcpParentNode);
664         $tmp->handle_post_events("remove");
665       }
667       /* DHCP Added */
668       if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
669         $attrs = $this->dhcpHostEntry;
670         unset($attrs['MODIFIED']);
671         unset($attrs['dn']);
672         $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
673         $res = $ldap->add($attrs);
675         $tmp = new servdhcp($this->config,$this->dhcpParentNode);
676         $tmp->handle_post_events("add");
678         show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
679       }
681       /* DHCP still activated */
682       if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
684         /* DHCP node changed */
685         if(($this->initial_dhcpParentNode != $this->dhcpParentNode) || 
686            ($this->cn != $this->OrigCn)){
687           $attrs = $this->dhcpHostEntry;
688           $attrs['cn'] = $this->cn;
689           unset($attrs['dn']);
690           unset($attrs['MODIFIED']);
691           $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
692           $res = $ldap->add($attrs);
694           $tmp = new servdhcp($this->config,$this->dhcpParentNode);
695           $tmp->handle_post_events("modify");
697           show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
698           if($res){
699             $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
700             show_ldap_error($ldap->get_error(),_("Removing old dhcp entry failed."));
701           }
702         }
703          
704         /* SAME node but modified */ 
705         if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1  && 
706             $this->initial_dhcpParentNode == $this->dhcpParentNode){
707           $attrs = $this->dhcpHostEntry;
708           unset($attrs['dn']);
709           unset($attrs['MODIFIED']);
710           $ldap->cd($this->dhcpHostEntry['dn']);
711           $ldap->modify($attrs);
712           
713           $tmp = new servdhcp($this->config,$this->dhcpParentNode);
714           $tmp->handle_post_events("modify");
716           show_ldap_error($ldap->get_error(),_("Modifying dhcp entry failed."));
717         }    
718       }
719     }
720     $this->dialog = FALSE; 
722     /****************/ 
723     /* DNS HANDLING */
724     /****************/ 
726     /* If isn't DNS account but initially was DNS account 
727        remove all DNS entries 
728      */ 
729     if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
730       return;
731     }else{
733       /* Add ipHostNumber to aRecords
734        */
735       $backup_dnsEntry = $this->dnsEntry;
736       if(!empty($this->ipHostNumber)){
737         $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
738         $ptr = $this->get_pTRRecord();
739         if(!empty($ptr)){
740           $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
741         } 
742       }
744       /* Create diff and follow instructions 
745        * If Account was disabled, remove account by setting exists to false
746        */
747       if((!$this->DNS_is_account)&&($this->initially_was_account)){  
748         $this->dnsEntry['exists'] = false;
749         $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
750       }else{
751         $this->dnsEntry['exists'] = $this->DNS_is_account;
752         $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
753       }   
755       /* move follwoing entries 
756        */
757       foreach($tmp['move'] as $src => $dst){
758         $this->recursive_move($src,$dst);
759       }
761       /* Delete dns */
762       foreach($tmp['del'] as $dn => $del){
763         $ldap->cd($dn);
764         $ldap->rmdir_recursive($dn);
765         new log("modify","unknown/".get_class($this),$dn,array("*"),$ldap->get_error());
766       }
768       /* Add || Update new DNS entries 
769        */
770       foreach($tmp['add'] as $dn => $attrs){
771         $ldap->cd($dn);
772         $ldap->cat($dn, array('dn'));
773         if(count($ldap->fetch())){
774           $ldap->cd($dn);
775           $ldap->modify ($attrs); 
776         }else{
777           $ldap->cd($dn);
778           $ldap->add($attrs);
779         }
780         new log("modify","unknown/".get_class($this),$dn,array_keys($attrs),$ldap->get_error());
781       }
784       /* Display errors 
785        */
786       if($ldap->get_error() != "Success"){
787         show_ldap_error($ldap->get_error(), sprintf(_("Saving of terminal/dns account with dn '%s' failed."),$this->dn));
788       }
790       $tmp2 = new servdns($this->config,$this->dn);
791       $tmp2->handle_post_events("modify");
793       $this->dnsEntry =  $backup_dnsEntry;
794     }
795   }
797   /*  Create html table with all used record types
798    */
799   function generateRecordsList()
800   {
801     $changeStateForRecords = "";
802     
803     if(!$this->DNS_is_account) {
804       $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
805       return(array("str" => $str, "changeStateForRecords"=> ""));
806     }
807  
808     $str = "<table summary='' width='100%'>";
809     foreach($this->dnsEntry['RECORDS'] as $key => $entry){
811         $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
812         $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
813         $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
815         $str.=" <tr>".
816           "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
817           "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
818           "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
819           "</tr>";
820     }
822     $str.= "  <tr>".
823            "    <td colspan=2 width='50%'></td><td>".
824            "      <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
825            "    </td>".
826            "  </tr>".
827            "</table>";
828      $ret =  array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
829     return($ret);
830   }
833   /* Create a html select box which allows us to select different types of records 
834    */
835   function generateRecordListBox($selected,$name)
836   {
837     $str = "<select name='".$name."' id='".$name."'>";
838     foreach($this->RecordTypes as $type => $value){
839       $use = "";
840       if($type == $selected){
841         $use = " selected ";
842       }
843       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
844     }
845     $str.="</select>";
846     return($str); 
847   }
850   /* Return plugin informations for acl handling  */ 
851   static function plInfo()
852   {
853     $tmp =  array(
854         "plShortName"   => _("DNS"),
855         "plDescription" => _("DNS settings"),
856         "plSelfModify"  => FALSE,
857         "plDepends"     => array(),
858         "plPriority"    => 5,
859         "plSection"     => array("administration"),
860         "plCategory"    => array("workstation","terminal","phone","server","component","printer","winworkstation"),
862         "plProvidedAcls"=> array(
863           "ipHostNumber"  => _("IP address"),
864           "macAddress"    => _("MAC address"))
865         );
867     /* Hide all dns specific code, if dns is not available
868      */
869     $config = session::get('config');
870     foreach($config->data['TABS']['SERVERSERVICE'] as $tab){
871       if(preg_match("/^servdns$/",$tab['CLASS'])){
872         $tmp['plProvidedAcls']["Records"]        = _("DNS records");
873         $tmp['plProvidedAcls']["zoneName"]       = _("Zone name");
874         $tmp['plProvidedAcls']["dNSTTL"]         = _("TTL");
875         break;
876       }
877     }
878     return($tmp);
879   }
881   
882   function get_dhcp_host_entry()
883   {
884     $attrs = array();
885     $dn = $this->get_dhcp_host_entry_dn();
886     if($dn){
887       $ldap = $this->config->get_ldap_link();
888       $ldap->cd($this->config->current['BASE']);
889       $ldap->cat($dn,array("*"));
890       if($ldap->count()){
891         $attrs = $ldap->fetch();
892         foreach($attrs as $key => $value){
893           if(is_numeric($key) || ($key == "count")){
894             unset($attrs[$key]);
895           }
896           if(is_array($value) && isset($value['count'])){
897             unset($attrs[$key]['count']);
898           }
899         }
900       }
901     }
902     return($attrs);
903   }
906   function get_dhcp_host_entry_dn()
907   {
908     $ldap = $this->config->get_ldap_link();
909     $ldap->cd($this->config->current['BASE']);
910     $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
912     if($ldap->count()){
913       $attr = $ldap->fetch();
914       return($attr['dn']);
915     }else{
916       return("");
917     }
918   }  
921   function get_dhcp_parent_node()
922   {
923     return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
924   }
927   function get_dhcp_parent_nodes()
928   {
929     $ldap = $this->config->get_ldap_link();
930     $ldap->cd($this->config->current['BASE']);
931     $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
932     
933     $dhcp_dns = array();
934     while($attr = $ldap->fetch()){
935       $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
936     }
937  
938     foreach($dhcp_dns as $key => $pri_dns){
939       $ldap->cat($pri_dns,array("cn"));
940       $tmp = $ldap->fetch();
941       if(isset($tmp['cn'][0])){
942         $dhcp_dns[$key] = $tmp['cn'][0];
943       }else{
944         unset($dhcp_dns[$key]);
945       }
946     }
948     $tmp = $tmp2 = array();
949     foreach($dhcp_dns as $dn => $cn){
950       $ldap->cd($dn);
951       $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
952                     "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
953       while($attr = $ldap->fetch()){
954         $tmp[$attr['dn']] = $attr['cn'][0];
955       }
956       $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.")&nbsp;"));
957     }
958     return($tmp2);
959   }
961   
962   /* this function returns the default ptr record entry */
963   function get_pTRRecord()
964   {
965     if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
966       $ldap = $this->config->get_ldap_link();
967       $ldap->cat(DNS::getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
968       $attrs = $ldap->fetch();
969       $tmp = array_flip($this->Zones);
970       $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
971       $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
972       $ptr = preg_replace("/^".normalizePreg(DNS::FlipIp($tmp))."\./","",$this->ipHostNumber);
973       return($ptr);
974     }else{
975       return(FALSE);
976     }
977   }
979   
980   function generateRandomIP($net = "")
981   {
982     $str = $net;
983     $cnt = 4;
984     while(substr_count($str,".") < 3 && $cnt > 0){
985       $str .= ".".rand(0,255);
986       $str = preg_replace("/\.\.*/",".",$str);
987       $str = trim($str,". ");
988       $cnt --;
989     }
990     return($str);
991   }
993   
994   function create_tree($arr,$base,$current = "")
995   {
996     $ret = array();
997     foreach($arr as $r => $name){
998       $base_part = str_replace($base,"",$r);
999       if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
1000         $ret[$r] = $current.$name;
1001         $tmp = $this->create_tree($arr,$r,$current.".&nbsp;");
1002         foreach($tmp as $sub_key => $sub_name){
1003           $ret[$sub_key] = $sub_name;
1004         }
1005       } 
1006     }
1007     return($ret);
1008   }
1010   function force_dns()
1011   {
1012     if($this->DNSenabled){
1014       /* Only force DNS account, if we have at least on dns Zone */
1015       if(count($this->Zones)){
1016         $this->DNS_is_account  = TRUE;
1017         $this->hide_dns_check_box = TRUE;
1018       }
1019     }
1020   }
1023 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1024 ?>