81dcc4eccc95e1b86fdea2e681813a218bf4b934
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 }
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 ************/
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 */
142 $ldap = $this->config->get_ldap_link();
143 $ldap->cd($this->config->current['BASE']);
144 $ldap->search("(|(macAddress=*)(ipHostNumber=*))",array("macAddress","ipHostNumber"));
145 while($attrs = $ldap->fetch()){
146 if(isset($attrs['ipHostNumber'][0])){
147 $this->used_ip_mac["ip:".$attrs['ipHostNumber'][0]] = "ip:".$attrs['ipHostNumber'][0];
148 }
149 if(isset($attrs['macAddress'][0])){
150 $this->used_ip_mac["mac:".$attrs['macAddress'][0]] = "mac:".$attrs['macAddress'][0];
151 }
152 }
154 /* Save initial ip and mac values, to be able
155 check if the used values are already in use */
156 $this->orig_ipHostNumber = $this->ipHostNumber;
157 $this->orig_macAddress = $this->macAddress;
159 /* Store initally account settings
160 */
161 $this->initially_was_account = $this->DNS_is_account;
163 if($this->DNS_is_account){
164 new log("view","unknown/".get_class($this),$this->dn);
165 }
166 }
169 function netmaskIsCoherent($idZone)
170 {
171 $netmask = DNS::FlipIp(str_replace(".in-addr.arpa","",DNS::getNameFromMix($idZone)));
172 if(!strstr($this->ipHostNumber, $netmask)){
173 return false;
174 }else{
175 return true;
176 }
177 }
180 function getVarsForSaving($attrs)
181 {
182 foreach($this->attributes as $attr){
183 if(!empty($this->$attr)){
184 $attrs[$attr] = $this->$attr;
185 }
186 }
187 return($attrs);
188 }
190 function execute()
191 {
192 /* Call parent execute */
193 $smarty= get_smarty();
195 $tmp = $this->plInfo();
196 foreach($tmp['plProvidedAcls'] as $name => $translation){
197 $smarty->assign($name."ACL",$this->getacl($name));
198 }
200 $display= "";
202 $smarty->assign("staticAddress", "");
204 /* Check for autonet button */
205 if (isset($_POST['autonet'])){
206 $d= new gosaSupportDaemon(TRUE, 0.5);
207 $res= $d->_send("<xml><header>gosa_network_completition</header><source>GOSA</source><target>GOSA</target><hostname>".$this->cn."</hostname></xml>", TRUE);
208 if (isset($res['XML']['IP'])){
209 $this->ipHostNumber= $res['XML']['IP'];
210 }
211 if (isset($res['XML']['MAC'])){
212 $this->macAddress= $res['XML']['MAC'];
213 }
214 }
217 /**********
218 * DHCP Handling
219 **********/
221 if(isset($_POST['dhcpEditOptions'])){
223 if(count($this->dhcpHostEntry) == 0){
224 $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
225 }else{
226 $this->dialog = new dhcpHost($this->dhcpHostEntry,TRUE);
227 }
228 $this->dialog->cn = $this->cn;
229 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
230 if(!empty($this->ipHostNumber)){
231 $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
232 }
233 }
235 if(isset($_POST['cancel_dhcp'])){
236 $this->dialog = FALSE;
237 }
239 if(isset($_POST['save_dhcp'])){
240 $this->dialog->save_object();
242 $msgs = $this->dialog->check(array());
243 if(count($msgs)){
244 foreach($msgs as $msg){
245 print_red($msg);
246 }
247 }else{
248 $this->dhcpHostEntry = $this->dialog->save();
249 $this->dialog = FALSE;
250 }
251 }
253 if(is_object($this->dialog)){
254 $this->dialog->save_object();
255 return($this->dialog->execute());
256 }
258 $smarty->assign("dhcpEnabled", $this->dhcpEnabled);
259 $smarty->assign("dhcp_is_Account",$this->dhcp_is_Account);
260 $smarty->assign("dhcpParentNode", $this->dhcpParentNode);
261 $smarty->assign("dhcpParentNodes",$this->dhcpParentNodes);
262 $smarty->assign("dhcpParentNodeCnt",count($this->dhcpParentNodes));
265 /**********
266 * DNS Handling
267 **********/
269 /* There is no dns available
270 */
271 $smarty->assign("DNS_is_account",$this->DNS_is_account);
272 $smarty->assign("DNSenabled",$this->DNSenabled);
273 if($this->DNSenabled == false){
275 /* Is IP address must ? */
276 # $smarty->assign("DNS_is_account",false);
277 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
279 /* Assign smarty all non DNs attributes */
280 foreach($this->attributes as $attr){
281 $smarty->assign($attr,$this->$attr);
282 }
283 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
285 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
286 }else{
287 # $smarty->assign("DNS_is_account",true);
289 /* Add new empty array to our record list */
290 if(isset($_POST['AddNewRecord'])){
291 $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>"");
292 }
294 /* propose_ip */
295 if(isset($_POST['propose_ip'])){
296 foreach($this->Zones as $key => $name){
297 if($name == $this->dnsEntry['zoneName']){
298 $net = DNS::FlipIp(str_replace(".in-addr.arpa","",DNS::getNameFromMix($key)));
299 $this->ipHostNumber = $this->generateRandomIp($net);
300 }
301 }
302 }
304 /* Handle all posts */
305 $only_once =true;
306 foreach($_POST as $name => $value){
308 /* Check if we have to delete a record entry */
309 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
311 /* Avoid performing this once again */
312 $only_once = false;
314 /* Extract id for specified entry */
315 $id = preg_replace("/RemoveRecord_/","",$name);
316 $id = preg_replace("/_.*$/","",$id);
318 /* Delete this record, mark edited entries to be able to delete them */
319 if(isset($this->dnsEntry['RECORDS'][$id])){
320 unset($this->dnsEntry['RECORDS'][$id]);
321 }
322 }
323 }
324 /* Assign smarty all non DNs attributes */
325 foreach($this->attributes as $attr){
326 $smarty->assign($attr,$this->$attr);
327 }
329 /* Assign smarty all DNS attributes */
330 foreach($this->DNSattributes as $attr){
331 $smarty->assign($attr,$this->dnsEntry[$attr]);
332 }
334 /* Assign all needed vars */
335 # $smarty->assign("DNSAccount",$this->DNS_is_account);
336 $smarty->assign("hide_dns_check_box",$this->hide_dns_check_box);
338 $smarty->assign("Zones",$this->Zones);
339 $smarty->assign("ZoneCnt",count($this->Zones));
340 $smarty->assign("ZoneKeys",($this->Zones));
341 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
343 /* Create zone array */
344 $idZones = array();
345 foreach($this->Zones as $id => $zone){
346 if($this->netmaskIsCoherent($id)) {
347 $idZones[$id] = $zone;
348 }else{
349 $idZones[$id] = $zone." ("._("Not matching").")";
350 }
351 }
352 $smarty->assign("Zones",$idZones);
353 $smarty->assign("ZoneKeys", $this->Zones);
355 $tmp = $this->generateRecordsList();
357 $changeStateForRecords = $tmp['changeStateForRecords'];
359 $smarty->assign("records",$tmp['str']);
360 $smarty->assign("changeStateForRecords",$changeStateForRecords);
361 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
363 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
364 }
365 return($display);
366 }
369 function remove_from_parent()
370 {
371 if($this->initially_was_account){
373 $ldap = $this->config->get_ldap_link();
375 $tmp = array();
376 $this->dnsEntry['exists'] = false;
377 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
379 /* Delete dns */
380 foreach($tmp['del'] as $dn => $del){
381 $ldap->cd($dn);
382 $ldap->rmdir_recursive($dn);
383 new log("remove","unknown/".get_class($this),$dn);
384 }
385 }
386 }
389 /* Save data to object */
390 function save_object()
391 {
393 if(isset($_POST['network_tpl_posted'])){
395 /* Save all posted vars */
396 plugin::save_object();
398 /* Handle DHCP Posts*/
399 if($this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
400 foreach($this->dhcpAttributes as $attr){
401 if(isset($_POST[$attr])){
402 $this->$attr = $_POST[$attr];
403 }
404 }
405 if(isset($_POST['dhcp_is_Account'])){
406 $this->dhcp_is_Account = TRUE;
407 }else{
408 $this->dhcp_is_Account = FALSE;
409 }
410 }
412 /* Ge all non dns attributes (IP/MAC)*/
413 foreach($this->attributes as $attr){
414 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
415 $this->$attr = $_POST[$attr];
416 }
417 }
419 /* Check if DNS should be enabled / disabled */
420 if($this->DNS_is_account && $this->acl_is_removeable() && !isset($_POST['DNS_is_account'])){
421 $this->DNS_is_account = false;
422 }elseif(!$this->DNS_is_account && $this->acl_is_createable() && isset($_POST['DNS_is_account'])){
423 $this->DNS_is_account = true;
424 }
426 /* Get dns attributes */
427 if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
429 /* Check for posted record changes */
430 if(is_array($this->dnsEntry['RECORDS']) && $this->acl_is_writeable("Records")){
431 foreach($this->dnsEntry['RECORDS'] as $key => $value){
433 /* Check if type has changed */
434 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
435 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
436 }
437 /* Check if value has changed */
438 if(isset($_POST['RecordValue_'.$key])){
439 $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
440 }
441 }
442 }
443 /* Get all basic DNS attributes (TTL, Clas ..)*/
444 foreach($this->DNSattributes as $attr){
445 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
446 $this->dnsEntry[$attr] = $_POST[$attr];
447 }
448 }
451 }
452 if($this->hide_dns_check_box){
453 $this->DNS_is_account = true;
454 }
455 }
456 }
459 /* Check supplied data */
460 function check()
461 {
462 /* Call common method to give check the hook */
463 $message= plugin::check();
465 if($this->dhcpEnabled && $this->dhcp_is_Account && $this->dhcpParentNode != "" && count($this->dhcpHostEntry) == 0){
466 # $message[] =_("You have not configured your dhcp settings yet.");
467 }
469 /* Check if mac and ip are already used */
470 if(!empty($this->ipHostNumber) && $this->DNS_is_account &&
471 $this->ipHostNumber != $this->orig_ipHostNumber &&
472 in_array("ip:".$this->ipHostNumber,$this->used_ip_mac)){
473 $message[] =_("The specified IP address is already in use.");
474 }
475 if(!empty($this->macAddress) && $this->dhcp_is_Account &&
476 $this->macAddress != $this->orig_macAddress &&
477 in_array("mac:".$this->macAddress,$this->used_ip_mac)){
478 print_red(sprintf(_("The specified MAC address '%s' for this system '%s' is already in use."),$this->macAddress,$this->cn));
479 }
481 /* Check if ip must be given
482 */
483 if(($this->IPisMust)||($this->DNS_is_account)){
484 if (empty($this->ipHostNumber)){
485 $message[]= _("The required field 'IP-address' is not set.");
486 }
488 if (!tests::is_ip($this->ipHostNumber)){
489 $message[]= _("Wrong IP format in field IP-address.");
490 }
491 }
493 /* Check if mac is empty
494 */
495 if ($this->macAddress == "" ){
496 $message[]= _("The required field 'MAC-address' is not set.");
497 }
498 if(!tests::is_mac($this->macAddress)){
499 $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
500 }
502 /* only perfrom this checks if this is a valid DNS account */
503 if($this->DNS_is_account){
505 $checkArray = array();
506 $onlyOnce = array();
508 // $onlyOnce['cNAMERecord'] = 0;
509 $tmp = array_flip($this->Zones);
510 $tmp2 = $tmp[$this->dnsEntry['zoneName']];
511 if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
512 $tmp2 = preg_replace("/^.*\//","",$tmp2);
513 $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
514 }
516 /* Walk through all entries and detect duplicates or mismatches
517 */
518 foreach($this->dnsEntry['RECORDS'] as $name => $values){
520 /* Count record values, to detect duplicate entries for a specific record
521 */
522 if(!isset($checkArray[$values['type']][$values['value']])){
523 $checkArray[$values['type']][$values['value']] = 0;
524 }else{
525 $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
526 }
528 /* Check if given entries in $onlyOnce are used more than once
529 */
530 if(isset($onlyOnce[$values['type']])){
531 $onlyOnce[$values['type']] ++;
532 if($onlyOnce[$values['type']] > 1){
533 $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
534 }
535 }
537 /* Skip txt record ...
538 */
539 if($values['type'] == "tXTRecord") continue;
541 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
542 */
543 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
544 $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."),
545 $this->ipHostNumber);
546 }
548 /* only lower-case is allowed in record entries ...
549 */
550 if($values['value'] != strtolower($values['value'])){
551 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
552 }
553 }
554 }
555 return ($message);
556 }
559 /* Save to LDAP */
560 function save()
561 {
562 $ldap= $this->config->get_ldap_link();
564 $dn = $this->parent->dn;
566 /*******************/
567 /* IP-MAC HANDLING */
568 /*******************/
570 /* $dn was posted as parameter */
571 $this->dn = $dn;
573 /* Save DNS setting & ip/Mac*/
574 plugin::save();
576 /* Write back to ldap */
577 $ldap->cd($this->dn);
578 $this->cleanup();
579 $ldap->modify ($this->attrs);
581 /****************/
582 /* DHCP HANDLING */
583 /****************/
585 /* New entry */
586 if($this->dhcpEnabled){
588 if(count($this->dhcpHostEntry) == 0){
589 $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
590 $this->dialog->cn = $this->cn;
591 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
592 if(!empty($this->ipHostNumber)){
593 $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
594 }
595 $this->dialog->execute();
596 $this->dialog->save_object();
597 $this->dhcpHostEntry = $this->dialog->save();
598 if(count($this->dhcpHostEntry['dhcpOption']) == 0){
599 $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
600 }
601 }
603 if(count($this->dhcpHostEntry) == 0){
604 $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
605 $this->dialog->cn = $this->cn;
606 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
607 if(!empty($this->ipHostNumber)){
608 $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
609 }
610 $this->dialog->execute();
611 $this->dialog->save_object();
612 $this->dhcpHostEntry = $this->dialog->save();
613 if(count($this->dhcpHostEntry['dhcpOption']) == 0){
614 $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
615 }
616 }
618 /* Write mac address to dhcp settings */
619 if($this->dhcp_is_Account){
620 if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) ||
621 !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
622 $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
623 $this->dhcpHostEntry['MODIFIED'] = TRUE;
624 }
625 }
628 /* Unset dhcpStatements if this attribute is empty */
629 if(isset($this->dhcpHostEntry['dhcpStatements']) &&
630 ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
631 unset($this->dhcpHostEntry['dhcpStatements']);
632 }
634 /* DHCP removed */
635 if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
636 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
637 show_ldap_error($ldap->get_error(),_("Removing dhcp entry for this object failed."));
639 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
640 $tmp->handle_post_events("remove");
641 }
643 /* DHCP Added */
644 if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
645 $attrs = $this->dhcpHostEntry;
646 unset($attrs['MODIFIED']);
647 unset($attrs['dn']);
648 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
649 $res = $ldap->add($attrs);
651 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
652 $tmp->handle_post_events("add");
654 show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
655 }
657 /* DHCP still activated */
658 if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
660 /* DHCP node changed */
661 if(($this->initial_dhcpParentNode != $this->dhcpParentNode) ||
662 ($this->cn != $this->OrigCn)){
663 $attrs = $this->dhcpHostEntry;
664 $attrs['cn'] = $this->cn;
665 unset($attrs['dn']);
666 unset($attrs['MODIFIED']);
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("modify");
673 show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
674 if($res){
675 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
676 show_ldap_error($ldap->get_error(),_("Removing old dhcp entry failed."));
677 }
678 }
680 /* SAME node but modified */
681 if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1 &&
682 $this->initial_dhcpParentNode == $this->dhcpParentNode){
683 $attrs = $this->dhcpHostEntry;
684 unset($attrs['dn']);
685 unset($attrs['MODIFIED']);
686 $ldap->cd($this->dhcpHostEntry['dn']);
687 $ldap->modify($attrs);
689 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
690 $tmp->handle_post_events("modify");
692 show_ldap_error($ldap->get_error(),_("Modifying dhcp entry failed."));
693 }
694 }
695 }
696 $this->dialog = FALSE;
698 /****************/
699 /* DNS HANDLING */
700 /****************/
702 /* If isn't DNS account but initially was DNS account
703 remove all DNS entries
704 */
705 if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
706 return;
707 }else{
709 /* Add ipHostNumber to aRecords
710 */
711 $backup_dnsEntry = $this->dnsEntry;
712 if(!empty($this->ipHostNumber)){
713 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
714 $ptr = $this->get_pTRRecord();
715 if(!empty($ptr)){
716 $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
717 }
718 }
720 /* Create diff and follow instructions
721 * If Account was disabled, remove account by setting exists to false
722 */
723 if((!$this->DNS_is_account)&&($this->initially_was_account)){
724 $this->dnsEntry['exists'] = false;
725 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
726 }else{
727 $this->dnsEntry['exists'] = $this->DNS_is_account;
728 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
729 }
731 /* move follwoing entries
732 */
733 foreach($tmp['move'] as $src => $dst){
734 $this->recursive_move($src,$dst);
735 }
737 /* Delete dns */
738 foreach($tmp['del'] as $dn => $del){
739 $ldap->cd($dn);
740 $ldap->rmdir_recursive($dn);
741 new log("modify","unknown/".get_class($this),$dn,array("*"),$ldap->get_error());
742 }
744 /* Add || Update new DNS entries
745 */
746 foreach($tmp['add'] as $dn => $attrs){
747 $ldap->cd($dn);
748 $ldap->cat($dn, array('dn'));
749 if(count($ldap->fetch())){
750 $ldap->cd($dn);
751 $ldap->modify ($attrs);
752 }else{
753 $ldap->cd($dn);
754 $ldap->add($attrs);
755 }
756 new log("modify","unknown/".get_class($this),$dn,array_keys($attrs),$ldap->get_error());
757 }
760 /* Display errors
761 */
762 if($ldap->get_error() != "Success"){
763 show_ldap_error($ldap->get_error(), sprintf(_("Saving of terminal/dns account with dn '%s' failed."),$this->dn));
764 }
766 $tmp2 = new servdns($this->config,$this->dn);
767 $tmp2->handle_post_events("modify");
769 $this->dnsEntry = $backup_dnsEntry;
770 }
771 }
773 /* Create html table with all used record types
774 */
775 function generateRecordsList()
776 {
777 $changeStateForRecords = "";
779 if(!$this->DNS_is_account) {
780 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
781 return(array("str" => $str, "changeStateForRecords"=> ""));
782 }
784 $str = "<table summary='' width='100%'>";
785 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
787 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
788 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
789 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
791 $str.=" <tr>".
792 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
793 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
794 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
795 "</tr>";
796 }
798 $str.= " <tr>".
799 " <td colspan=2 width='50%'></td><td>".
800 " <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
801 " </td>".
802 " </tr>".
803 "</table>";
804 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
805 return($ret);
806 }
809 /* Create a html select box which allows us to select different types of records
810 */
811 function generateRecordListBox($selected,$name)
812 {
813 $str = "<select name='".$name."' id='".$name."'>";
814 foreach($this->RecordTypes as $type => $value){
815 $use = "";
816 if($type == $selected){
817 $use = " selected ";
818 }
819 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
820 }
821 $str.="</select>";
822 return($str);
823 }
826 /* Return plugin informations for acl handling */
827 static function plInfo()
828 {
829 $tmp = array(
830 "plShortName" => _("DNS"),
831 "plDescription" => _("DNS settings"),
832 "plSelfModify" => FALSE,
833 "plDepends" => array(),
834 "plPriority" => 5,
835 "plSection" => array("administration"),
836 "plCategory" => array("workstation","terminal","phone","server","component","printer","winworkstation"),
838 "plProvidedAcls"=> array(
839 "ipHostNumber" => _("IP address"),
840 "macAddress" => _("MAC address"))
841 );
843 /* Hide all dns specific code, if dns is not available
844 */
845 $config = session::get('config');
846 foreach($config->data['TABS']['SERVERSERVICE'] as $tab){
847 if(preg_match("/^servdns$/",$tab['CLASS'])){
848 $tmp['plProvidedAcls']["Records"] = _("DNS records");
849 $tmp['plProvidedAcls']["zoneName"] = _("Zone name");
850 $tmp['plProvidedAcls']["dNSTTL"] = _("TTL");
851 break;
852 }
853 }
854 return($tmp);
855 }
858 function get_dhcp_host_entry()
859 {
860 $attrs = array();
861 $dn = $this->get_dhcp_host_entry_dn();
862 if($dn){
863 $ldap = $this->config->get_ldap_link();
864 $ldap->cd($this->config->current['BASE']);
865 $ldap->cat($dn,array("*"));
866 if($ldap->count()){
867 $attrs = $ldap->fetch();
868 foreach($attrs as $key => $value){
869 if(is_numeric($key) || ($key == "count")){
870 unset($attrs[$key]);
871 }
872 if(is_array($value) && isset($value['count'])){
873 unset($attrs[$key]['count']);
874 }
875 }
876 }
877 }
878 return($attrs);
879 }
882 function get_dhcp_host_entry_dn()
883 {
884 $ldap = $this->config->get_ldap_link();
885 $ldap->cd($this->config->current['BASE']);
886 $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
888 if($ldap->count()){
889 $attr = $ldap->fetch();
890 return($attr['dn']);
891 }else{
892 return("");
893 }
894 }
897 function get_dhcp_parent_node()
898 {
899 return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
900 }
903 function get_dhcp_parent_nodes()
904 {
905 $ldap = $this->config->get_ldap_link();
906 $ldap->cd($this->config->current['BASE']);
907 $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
909 $dhcp_dns = array();
910 while($attr = $ldap->fetch()){
911 $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
912 }
914 foreach($dhcp_dns as $key => $pri_dns){
915 $ldap->cat($pri_dns,array("cn"));
916 $tmp = $ldap->fetch();
917 if(isset($tmp['cn'][0])){
918 $dhcp_dns[$key] = $tmp['cn'][0];
919 }else{
920 unset($dhcp_dns[$key]);
921 }
922 }
924 $tmp = $tmp2 = array();
925 foreach($dhcp_dns as $dn => $cn){
926 $ldap->cd($dn);
927 $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
928 "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
929 while($attr = $ldap->fetch()){
930 $tmp[$attr['dn']] = $attr['cn'][0];
931 }
932 $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.") "));
933 }
934 return($tmp2);
935 }
938 /* this function returns the default ptr record entry */
939 function get_pTRRecord()
940 {
941 if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
942 $ldap = $this->config->get_ldap_link();
943 $ldap->cat(DNS::getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
944 $attrs = $ldap->fetch();
945 $tmp = array_flip($this->Zones);
946 $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
947 $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
948 $ptr = preg_replace("/^".normalizePreg(DNS::FlipIp($tmp))."\./","",$this->ipHostNumber);
949 return($ptr);
950 }else{
951 return(FALSE);
952 }
953 }
956 function generateRandomIP($net = "")
957 {
958 $str = $net;
959 $cnt = 4;
960 while(substr_count($str,".") < 3 && $cnt > 0){
961 $str .= ".".rand(0,255);
962 $str = preg_replace("/\.\.*/",".",$str);
963 $str = trim($str,". ");
964 $cnt --;
965 }
966 return($str);
967 }
970 function create_tree($arr,$base,$current = "")
971 {
972 $ret = array();
973 foreach($arr as $r => $name){
974 $base_part = str_replace($base,"",$r);
975 if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
976 $ret[$r] = $current.$name;
977 $tmp = $this->create_tree($arr,$r,$current.". ");
978 foreach($tmp as $sub_key => $sub_name){
979 $ret[$sub_key] = $sub_name;
980 }
981 }
982 }
983 return($ret);
984 }
986 function force_dns()
987 {
988 if($this->DNSenabled){
990 /* Only force DNS account, if we have at least on dns Zone */
991 if(count($this->Zones)){
992 $this->DNS_is_account = TRUE;
993 $this->hide_dns_check_box = TRUE;
994 }
995 }
996 }
997 }
999 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1000 ?>