1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class termDNS extends plugin
24 {
25 /* attribute list for save action */
26 var $ignore_account = true;
28 /* Basic informations
29 */
30 var $attributes = array("ipHostNumber","macAddress");
31 var $objectclasses = array("whatever");
33 var $ipHostNumber = ""; // IP address
34 var $additionalHostNumbers = array();
35 var $macAddress = ""; // Mac address
37 var $orig_ipHostNumber = ""; // IP address
38 var $orig_macAddress = ""; // Mac address
40 var $cn = ""; // CN of currently edited device
41 var $OrigCn = ""; // Initial cn
42 var $IPisMust = false;
43 var $MACisMust = false;
44 var $dialog = false;
46 /* DCHP Attributes
47 */
48 var $dhcpAttributes = array("dhcpParentNode");
49 var $dhcpEnabled = FALSE;
50 var $dhcp_is_Account = FALSE;
51 var $dhcpParentNodes = array();
52 var $dhcpParentNode = "";
53 var $dhcpHostEntry = array();
54 var $initial_dhcpParentNode = "";
55 var $initial_dhcpHostEntry = array();
56 var $initial_dhcp_is_Account = FALSE;
58 var $used_ip_mac = array();
60 /* DNS attributes
61 */
62 var $DNSattributes = array("dNSClass","zoneName","dNSTTL");
63 var $DNS_is_account = false;
64 var $initially_was_account = false;
65 var $dnsEntry = array();
66 var $DNSenabled = false;
67 var $hide_dns_check_box = FALSE;
69 /* Terminal dns
70 */
71 function termDNS (&$config, $parent,$objectClasses,$IPisMust = false)
72 {
73 /* We need to know which objectClasses are used, to store the ip/mac
74 * Because of different type of devices
75 */
76 $this->parent = $parent;
77 $this->objectclasses = $objectClasses;
78 $this->IPisMust = $IPisMust;
80 plugin::plugin ($config, $parent->dn);
82 if(isset($this->attrs['cn'][0])){
83 $this->OrigCn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
84 $this->cn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
85 }
87 /* Create list of additional ipHostNumber.
88 */
89 $this->additionalHostNumbers = array();
90 if(isset($this->attrs['ipHostNumber']) && $this->attrs['ipHostNumber']['count'] > 1){
91 for($i = 1 ; $i < $this->attrs['ipHostNumber']['count']; $i ++){
92 $this->additionalHostNumbers[] = $this->attrs['ipHostNumber'][$i];
93 }
94 }
96 /************
97 * DHCP
98 ************/
100 /* Hide all dhcp specific code, if dhcp plugin is not present in config */
101 $dhcpEnabled = FALSE;
102 if($this->config->search("servdhcp","class",array("tabs"))){
103 $this->dhcpEnabled = TRUE;
104 }
106 if(!class_available("dhcpHost")){
107 $this->dhcpEnabled = FALSE;
108 }
110 if($this->dhcpEnabled){
111 $this->dhcpParentNodes = $this->get_dhcp_parent_nodes();
112 $this->dhcpParentNode = $this->get_dhcp_parent_node();
113 if($this->dhcpParentNode){
114 $this->dhcp_is_Account = TRUE;
115 $this->initial_dhcp_is_Account = TRUE;
116 $this->dhcpHostEntry = $this->get_dhcp_host_entry();
117 }
118 $this->initial_dhcpHostEntry = $this->dhcpHostEntry;
119 $this->initial_dhcpParentNode= $this->dhcpParentNode;
120 }
123 /************
124 * DNS
125 ************/
127 /* Hide all dns specific code, if dns is not available
128 */
129 $DNSenabled = false;
130 foreach($this->config->data['TABS']['SERVERSERVICE'] as $tab){
131 if(preg_match("/^servdns$/",$tab['CLASS'])){
132 $this->DNSenabled = true;
133 }
134 }
136 if(!class_available("DNS")){
137 $this->DNSenabled = FALSE;
138 }
140 if(!$this->DNSenabled){
141 $this->DNS_is_account = false;
142 return;
143 }
145 if($this->DNSenabled){
147 /* Get Zones
148 */
149 $this->Zones = DNS::getAvailableZones($config);
151 /* Get Entry
152 */
153 $this->dnsEntry = DNS::getDNSHostEntries($config,$this->OrigCn);
155 /* Remove A record which equals $this->ipHostNumber
156 */
157 $ptr = $this->get_pTRRecord();
158 foreach($this->dnsEntry['RECORDS'] as $key => $rec){
159 if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
160 unset($this->dnsEntry['RECORDS'][$key]);
161 }
162 if(($rec['type'] == "pTRRecord") && ($rec['value'] == $ptr)){
163 unset($this->dnsEntry['RECORDS'][$key]);
164 }
165 }
167 /* Get Record types
168 */
169 $this->RecordTypes = DNS::getDnsRecordTypes();
171 /* If there is at least one entry in this -> types, we have DNS enabled
172 */
173 if($this->dnsEntry['exists']){
174 $this->DNS_is_account = true;
175 }else{
176 $this->DNS_is_account = false;
177 }
178 }
180 /* Create a list of used mac and ip addresses.
182 ! We use this optically huge amount of code to fetch all
183 Mac and IP addresses, because a simple search for mac and IP
184 over the whole ldap server was 10 to 20 times slower.
185 */
186 $deps = array();
187 $ou = preg_replace("/,.*$/","",get_ou("systemsou"));
188 $a_ous = array(get_ou("serverou"),
189 get_ou("terminalou"),
190 get_ou("workstationou"),
191 get_ou("printerou"),
192 get_ou("phoneou"),
193 get_ou("componentou"));
195 $ldap = $this->config->get_ldap_link();
196 $ldap->cd($this->config->current['BASE']);
197 $ldap->search("(&(objectClass=organizationalUnit)(".$ou."))",array("dn"));
198 while($attrs = $ldap->fetch()){
199 foreach($a_ous as $allowed){
200 $deps[] = $allowed.$attrs['dn'];
201 }
202 }
204 foreach($deps as $dep){
205 $ldap->cd($dep);
206 $ldap->search("(|(macAddress=*)(ipHostNumber=*))",array("macAddress","ipHostNumber"));
207 while($attrs = $ldap->fetch()){
208 if(isset($attrs['ipHostNumber'][0])){
209 $this->used_ip_mac["ip:".$attrs['ipHostNumber'][0]] = "ip:".$attrs['ipHostNumber'][0];
210 }
211 if(isset($attrs['macAddress'][0])){
212 $this->used_ip_mac["mac:".$attrs['macAddress'][0]] = "mac:".$attrs['macAddress'][0];
213 }
214 }
215 }
217 /* Save initial ip and mac values, to be able
218 check if the used values are already in use */
219 $this->orig_ipHostNumber = $this->ipHostNumber;
220 $this->orig_macAddress = $this->macAddress;
222 /* Store initally account settings
223 */
224 $this->initially_was_account = $this->DNS_is_account;
226 if($this->DNS_is_account){
227 new log("view","unknown/".get_class($this),$this->dn);
228 }
229 }
232 function netmaskIsCoherent($idZone)
233 {
234 $netmask = DNS::FlipIp(str_replace(".in-addr.arpa","",DNS::getNameFromMix($idZone)));
235 if(!strstr($this->ipHostNumber, $netmask)){
236 return false;
237 }else{
238 return true;
239 }
240 }
243 function getVarsForSaving($attrs)
244 {
245 foreach($this->attributes as $attr){
246 if(!empty($this->$attr)){
247 $attrs[$attr] = $this->$attr;
248 }
249 }
250 return($attrs);
251 }
253 function execute()
254 {
255 /* Call parent execute */
256 $smarty= get_smarty();
258 $tmp = $this->plInfo();
259 foreach($tmp['plProvidedAcls'] as $name => $translation){
260 $smarty->assign($name."ACL",$this->getacl($name));
261 }
263 $display= "";
265 /**********
266 * Additional ipHostNumber handling
267 **********/
269 /* Add a new one */
270 foreach($_POST as $name => $value){
271 if(preg_match("/^additionalHostNumbers_add/",$name)){
272 $this->additionalHostNumbers[] = "";
273 break;
274 }
276 /* Delete given entry */
277 if(preg_match("/^additionalHostNumbers_del_/",$name)){
278 $id = preg_replace("/^^additionalHostNumbers_del_([0-9]*)_.*/","\\1",$name);
279 if(isset($this->additionalHostNumbers[$id])){
280 unset($this->additionalHostNumbers[$id]);
281 $this->additionalHostNumbers = array_values($this->additionalHostNumbers);
282 }
283 break;
284 }
285 }
287 $smarty->assign("additionalHostNumbers",$this->additionalHostNumbers);
288 $smarty->assign("staticAddress", "");
290 /**********
291 * Autonet completion
292 **********/
294 /* Check for autonet button */
295 if (isset($_POST['autonet'])){
296 $d= new gosaSupportDaemon(TRUE, 0.5);
297 $res= $d->_send("<xml><header>gosa_network_completition</header><source>GOSA</source><target>GOSA</target><hostname>".$this->cn."</hostname></xml>", TRUE);
298 if (isset($res['XML']['IP'])){
299 $this->ipHostNumber= $res['XML']['IP'];
300 }
301 if (isset($res['XML']['MAC'])){
302 $this->macAddress= $res['XML']['MAC'];
303 }
304 }
307 /**********
308 * DHCP Handling
309 **********/
311 if(isset($_POST['dhcpEditOptions'])){
313 if(count($this->dhcpHostEntry) == 0){
314 $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
315 }else{
316 $this->dialog = new dhcpHost($this->dhcpHostEntry,TRUE);
317 }
318 $this->dialog->cn = $this->cn;
319 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
320 if(!empty($this->ipHostNumber)){
321 $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
322 }
323 }
325 if(isset($_POST['cancel_dhcp'])){
326 $this->dialog = FALSE;
327 }
329 if(isset($_POST['save_dhcp'])){
330 $this->dialog->save_object();
332 $msgs = $this->dialog->check(array());
333 if(count($msgs)){
334 foreach($msgs as $msg){
335 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
336 }
337 }else{
338 $this->dhcpHostEntry = $this->dialog->save();
339 $this->dialog = FALSE;
340 }
341 }
343 if(is_object($this->dialog)){
344 $this->dialog->save_object();
345 return($this->dialog->execute());
346 }
348 $smarty->assign("dhcpEnabled", $this->dhcpEnabled);
349 $smarty->assign("dhcp_is_Account",$this->dhcp_is_Account);
350 $smarty->assign("dhcpParentNode", $this->dhcpParentNode);
351 $smarty->assign("dhcpParentNodes",$this->dhcpParentNodes);
352 $smarty->assign("dhcpParentNodeCnt",count($this->dhcpParentNodes));
355 /**********
356 * DNS Handling
357 **********/
359 /* There is no dns available
360 */
361 $smarty->assign("DNS_is_account",$this->DNS_is_account);
362 $smarty->assign("DNSenabled",$this->DNSenabled);
363 if($this->DNSenabled == false){
365 /* Is IP address must ? */
366 # $smarty->assign("DNS_is_account",false);
367 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
369 /* Assign smarty all non DNs attributes */
370 foreach($this->attributes as $attr){
371 $smarty->assign($attr,$this->$attr);
372 }
373 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
375 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
376 }else{
377 # $smarty->assign("DNS_is_account",true);
379 /* Add new empty array to our record list */
380 if(isset($_POST['AddNewRecord'])){
381 $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>"");
382 }
384 /* propose_ip */
385 if(isset($_POST['propose_ip'])){
386 foreach($this->Zones as $key => $name){
387 if($name == $this->dnsEntry['zoneName']){
388 $net = DNS::FlipIp(str_replace(".in-addr.arpa","",DNS::getNameFromMix($key)));
389 $this->ipHostNumber = $this->generateRandomIp($net);
390 }
391 }
392 }
394 /* Handle all posts */
395 $only_once =true;
396 foreach($_POST as $name => $value){
398 /* Check if we have to delete a record entry */
399 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
401 /* Avoid performing this once again */
402 $only_once = false;
404 /* Extract id for specified entry */
405 $id = preg_replace("/RemoveRecord_/","",$name);
406 $id = preg_replace("/_.*$/","",$id);
408 /* Delete this record, mark edited entries to be able to delete them */
409 if(isset($this->dnsEntry['RECORDS'][$id])){
410 unset($this->dnsEntry['RECORDS'][$id]);
411 }
412 }
413 }
414 /* Assign smarty all non DNs attributes */
415 foreach($this->attributes as $attr){
416 $smarty->assign($attr,$this->$attr);
417 }
419 /* Assign smarty all DNS attributes */
420 foreach($this->DNSattributes as $attr){
421 $smarty->assign($attr,$this->dnsEntry[$attr]);
422 }
424 /* Assign all needed vars */
425 # $smarty->assign("DNSAccount",$this->DNS_is_account);
426 $smarty->assign("hide_dns_check_box",$this->hide_dns_check_box);
428 $smarty->assign("Zones",$this->Zones);
429 $smarty->assign("ZoneCnt",count($this->Zones));
430 $smarty->assign("ZoneKeys",($this->Zones));
431 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
433 /* Create zone array */
434 $idZones = array();
435 foreach($this->Zones as $id => $zone){
436 if($this->netmaskIsCoherent($id)) {
437 $idZones[$id] = $zone;
438 }else{
439 $idZones[$id] = $zone." ("._("Not matching").")";
440 }
441 }
442 $smarty->assign("Zones",$idZones);
443 $smarty->assign("ZoneKeys", $this->Zones);
445 $tmp = $this->generateRecordsList();
447 $changeStateForRecords = $tmp['changeStateForRecords'];
449 $smarty->assign("records",$tmp['str']);
450 $smarty->assign("changeStateForRecords",$changeStateForRecords);
451 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
453 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
454 }
456 return($display);
457 }
460 function remove_from_parent()
461 {
462 if($this->DNS_is_account){
464 $ldap = $this->config->get_ldap_link();
466 $tmp = array();
467 $this->dnsEntry['exists'] = false;
468 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
470 /* Delete dns */
471 foreach($tmp['del'] as $dn => $del){
472 $ldap->cd($dn);
473 $ldap->rmdir_recursive($dn);
474 new log("remove","unknown/".get_class($this),$dn);
475 if (!$ldap->success()){
476 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_DEL, get_class()));
477 }
478 }
479 }
480 }
483 /* Save data to object */
484 function save_object()
485 {
487 if(isset($_POST['network_tpl_posted'])){
489 /* Save all posted vars */
490 plugin::save_object();
492 /******
493 Additional IP Host Numbers
494 ******/
496 /* Get posts for all additionally added ipHostNumbers */
497 foreach($this->additionalHostNumbers as $id => $value){
498 if(isset($_POST['additionalHostNumbers_'.$id])){
499 $this->additionalHostNumbers[$id] = get_post('additionalHostNumbers_'.$id);
500 }
501 }
504 /******
505 DHCP posts
506 ******/
508 if($this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
509 foreach($this->dhcpAttributes as $attr){
510 if(isset($_POST[$attr])){
511 $this->$attr = $_POST[$attr];
512 }
513 }
514 if(isset($_POST['dhcp_is_Account'])){
515 $this->dhcp_is_Account = TRUE;
516 }else{
517 $this->dhcp_is_Account = FALSE;
518 }
519 }
521 /* Ge all non dns attributes (IP/MAC)*/
522 foreach($this->attributes as $attr){
523 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
524 $this->$attr = $_POST[$attr];
525 }
526 }
529 /******
530 DNS posts
531 ******/
533 /* Check if DNS should be enabled / disabled */
534 if($this->DNS_is_account && $this->acl_is_removeable() && !isset($_POST['DNS_is_account'])){
535 $this->DNS_is_account = false;
536 }elseif(!$this->DNS_is_account && $this->acl_is_createable() && isset($_POST['DNS_is_account'])){
537 $this->DNS_is_account = true;
538 }
540 /* Get dns attributes */
541 if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
543 /* Check for posted record changes */
544 if(is_array($this->dnsEntry['RECORDS']) && $this->acl_is_writeable("Records")){
545 foreach($this->dnsEntry['RECORDS'] as $key => $value){
547 /* Check if type has changed */
548 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
549 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
550 }
551 /* Check if value has changed */
552 if(isset($_POST['RecordValue_'.$key])){
553 $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
554 }
555 }
556 }
557 /* Get all basic DNS attributes (TTL, Clas ..)*/
558 foreach($this->DNSattributes as $attr){
559 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
560 $this->dnsEntry[$attr] = $_POST[$attr];
561 }
562 }
565 }
566 if($this->hide_dns_check_box){
567 $this->DNS_is_account = true;
568 }
569 }
570 }
573 /* Check supplied data */
574 function check()
575 {
576 /* Call common method to give check the hook */
577 $message= plugin::check();
579 /******
580 check additional IP Host Numbers
581 ******/
583 foreach($this->additionalHostNumbers as $id => $value){
584 if(!tests::is_ip($value)){
585 $message[]= msgPool::invalid(sprintf(_("IP address #%s"),($id +2)), "", "", "192.168.1.10");
586 }
587 }
590 /* Check if mac and ip are already used */
591 if(!empty($this->ipHostNumber) && $this->DNS_is_account &&
592 $this->ipHostNumber != $this->orig_ipHostNumber &&
593 in_array("ip:".$this->ipHostNumber,$this->used_ip_mac)){
594 $message[]= msgPool::duplicated(_("IP address"));
595 }
596 if(!empty($this->macAddress) && $this->dhcp_is_Account &&
597 $this->macAddress != $this->orig_macAddress &&
598 in_array("mac:".$this->macAddress,$this->used_ip_mac)){
599 $message[]= msgPool::duplicated(_("MAC address"));
600 }
602 /* Check if ip must be given
603 */
604 if(($this->IPisMust)||($this->DNS_is_account)){
605 if (empty($this->ipHostNumber)){
606 $message[]= msgPool::required(_("IP address"));
607 }elseif (!tests::is_ip($this->ipHostNumber)){
608 $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.10");
609 }
610 }
612 /* Check if mac is empty
613 */
614 if ($this->macAddress == "" ){
615 $message[]= msgPool::required(_("MAC address"));
616 }elseif(!tests::is_mac($this->macAddress)){
617 $message[]= msgPool::invalid(_("MAC address"), "", "", "00:0C:7F:31:33:F1");
618 }
620 /* only perfrom this checks if this is a valid DNS account */
621 if($this->DNS_is_account){
623 $checkArray = array();
624 $onlyOnce = array();
626 // $onlyOnce['cNAMERecord'] = 0;
627 $tmp = array_flip($this->Zones);
628 $tmp2 = $tmp[$this->dnsEntry['zoneName']];
629 if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
630 $tmp2 = preg_replace("/^.*\//","",$tmp2);
631 $message[] =sprintf(_("The IP address '%s' is not part of the selected reverse zone '%s'!"),$this->ipHostNumber,$tmp2);
632 }
634 /* Walk through all entries and detect duplicates or mismatches
635 */
636 foreach($this->dnsEntry['RECORDS'] as $name => $values){
638 /* Count record values, to detect duplicate entries for a specific record
639 */
640 if(!isset($checkArray[$values['type']][$values['value']])){
641 $checkArray[$values['type']][$values['value']] = 0;
642 }else{
643 $message[] = sprintf(_("Record type '%s' is duplicated!"),$values['type']);
644 }
646 /* Check if given entries in $onlyOnce are used more than once
647 */
648 if(isset($onlyOnce[$values['type']])){
649 $onlyOnce[$values['type']] ++;
650 if($onlyOnce[$values['type']] > 1){
651 $message[] = sprintf(_("Uniq record type '%s' is duplicated!"),$values['type']);
652 }
653 }
655 /* Skip txt record ...
656 */
657 if($values['type'] == "tXTRecord") continue;
659 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
660 */
661 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
662 #TODO: Where's the problem here?
663 $message[]=sprintf(_("The IP address '%s' will be added as 'A Record', this will be done automatically, please remove the record."),
664 $this->ipHostNumber);
665 }
667 /* only lower-case is allowed in record entries ...
668 */
669 if($values['value'] != strtolower($values['value'])){
670 #TODO: What's in values['value']? Something for a propper message?
671 $message[] = sprintf(_("Only lowercase records are allowed, please check your '%ss'."),$values['type']);
672 }
673 }
674 }
675 return ($message);
676 }
679 /* Save to LDAP */
680 function save()
681 {
682 $ldap= $this->config->get_ldap_link();
684 $dn = $this->parent->dn;
686 /*******************/
687 /* IP-MAC HANDLING */
688 /*******************/
690 /* $dn was posted as parameter */
691 $this->dn = $dn;
693 /* Save DNS setting & ip/Mac*/
694 plugin::save();
696 /* Add all additional ipHostNumbers now
697 */
698 if(!empty($this->ipHostNumber)){
699 $this->attrs['ipHostNumber'] = array($this->ipHostNumber);
700 }
701 foreach($this->additionalHostNumbers as $value){
702 $this->attrs['ipHostNumber'][] = $value;
703 }
705 /* Write back to ldap */
706 $ldap->cd($this->dn);
707 $this->cleanup();
708 $ldap->modify ($this->attrs);
710 /****************/
711 /* DHCP HANDLING */
712 /****************/
714 /* New entry */
715 if($this->dhcpEnabled){
717 if(count($this->dhcpHostEntry) == 0){
718 $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
719 $this->dialog->cn = $this->cn;
720 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
721 if(!empty($this->ipHostNumber)){
722 $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
723 }
724 $this->dialog->execute();
725 $this->dialog->save_object();
726 $this->dhcpHostEntry = $this->dialog->save();
727 if(count($this->dhcpHostEntry['dhcpOption']) == 0){
728 $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
729 }
730 }
732 /* Write mac address to dhcp settings */
733 if($this->dhcp_is_Account){
734 if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) ||
735 !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
736 $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
737 $this->dhcpHostEntry['MODIFIED'] = TRUE;
738 }
739 }
741 /* Updated IP host number */
742 if($this->dhcp_is_Account){
743 foreach($this->dhcpHostEntry['dhcpStatements'] as $id => $value){
744 if(preg_match("/^fixed-address/",$value)){
745 $this->dhcpHostEntry['dhcpStatements'][$id] = "fixed-address ".$this->ipHostNumber;
746 $this->dhcpHostEntry['MODIFIED'] = TRUE;
747 }
748 }
749 }
751 /* Unset dhcpStatements if this attribute is empty */
752 if(isset($this->dhcpHostEntry['dhcpStatements']) &&
753 ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
754 unset($this->dhcpHostEntry['dhcpStatements']);
755 }
757 /* DHCP removed */
758 if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
759 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
760 if (!$ldap->success()){
761 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_DEL, get_class()));
762 }
764 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
765 $tmp->handle_post_events("remove");
766 }
768 /* DHCP Added */
769 if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
770 $attrs = $this->dhcpHostEntry;
771 unset($attrs['MODIFIED']);
772 unset($attrs['dn']);
773 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
774 $res = $ldap->add($attrs);
776 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
777 $tmp->handle_post_events("add");
779 if (!$ldap->success()){
780 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), "cn=".$this->cn.",".$this->dhcpParentNode, LDAP_ADD, get_class()));
781 }
782 }
784 /* DHCP still activated */
785 if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
787 /* DHCP node changed */
788 if(($this->initial_dhcpParentNode != $this->dhcpParentNode) ||
789 ($this->cn != $this->OrigCn)){
790 $attrs = $this->dhcpHostEntry;
791 $attrs['cn'] = $this->cn;
792 unset($attrs['dn']);
793 unset($attrs['MODIFIED']);
794 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
795 $res = $ldap->add($attrs);
797 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
798 $tmp->handle_post_events("modify");
800 if (!$ldap->success()){
801 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), "cn=".$this->cn.",".$this->dhcpParentNode, LDAP_ADD, get_class()));
802 }
803 if($res){
804 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
805 if (!$ldap->success()){
806 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_DEL, get_class()));
807 }
808 }
809 }
811 /* SAME node but modified */
812 if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1 &&
813 $this->initial_dhcpParentNode == $this->dhcpParentNode){
814 $attrs = $this->dhcpHostEntry;
815 unset($attrs['dn']);
816 unset($attrs['MODIFIED']);
817 $ldap->cd($this->dhcpHostEntry['dn']);
818 $ldap->modify($attrs);
820 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
821 $tmp->handle_post_events("modify");
823 if (!$ldap->success()){
824 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_MOD, get_class()));
825 }
826 }
827 }
828 }
829 $this->dialog = FALSE;
831 /****************/
832 /* DNS HANDLING */
833 /****************/
835 /* If isn't DNS account but initially was DNS account
836 remove all DNS entries
837 */
838 if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
839 return;
840 }else{
842 /* Add ipHostNumber to aRecords
843 */
844 $backup_dnsEntry = $this->dnsEntry;
845 if(!empty($this->ipHostNumber)){
846 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
847 $ptr = $this->get_pTRRecord();
848 if(!empty($ptr)){
849 $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
850 }
851 }
853 /* Create diff and follow instructions
854 * If Account was disabled, remove account by setting exists to false
855 */
856 if((!$this->DNS_is_account)&&($this->initially_was_account)){
857 $this->dnsEntry['exists'] = false;
858 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
859 }else{
860 $this->dnsEntry['exists'] = $this->DNS_is_account;
861 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
862 }
864 /* move follwoing entries
865 */
866 foreach($tmp['move'] as $src => $dst){
867 $this->recursive_move($src,$dst);
868 }
870 /* Delete dns */
871 foreach($tmp['del'] as $dn => $del){
872 $ldap->cd($dn);
873 $ldap->rmdir_recursive($dn);
874 new log("modify","unknown/".get_class($this),$dn,array("*"),$ldap->get_error());
875 }
877 /* Add || Update new DNS entries
878 */
879 foreach($tmp['add'] as $dn => $attrs){
880 $ldap->cd($dn);
881 $ldap->cat($dn, array('dn'));
882 if(count($ldap->fetch())){
883 $ldap->cd($dn);
884 $ldap->modify ($attrs);
885 }else{
886 $ldap->cd($dn);
887 $ldap->add($attrs);
888 }
889 new log("modify","unknown/".get_class($this),$dn,array_keys($attrs),$ldap->get_error());
890 }
893 /* Display errors
894 */
895 if (!$ldap->success()){
896 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, 0, get_class()));
897 }
899 $tmp2 = new servdns($this->config,$this->dn);
900 $tmp2->handle_post_events("modify");
902 $this->dnsEntry = $backup_dnsEntry;
903 }
904 }
906 /* Create html table with all used record types
907 */
908 function generateRecordsList()
909 {
910 $changeStateForRecords = "";
912 if(!$this->DNS_is_account) {
913 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
914 return(array("str" => $str, "changeStateForRecords"=> ""));
915 }
917 $str = "<table summary='' width='100%'>";
918 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
920 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
921 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
922 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
924 $str.=" <tr>".
925 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
926 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
927 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
928 "</tr>";
929 }
931 $str.= " <tr>".
932 " <td colspan=2 width='50%'></td><td>".
933 " <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
934 " </td>".
935 " </tr>".
936 "</table>";
937 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
938 return($ret);
939 }
942 /* Create a html select box which allows us to select different types of records
943 */
944 function generateRecordListBox($selected,$name)
945 {
946 $str = "<select name='".$name."' id='".$name."'>";
947 foreach($this->RecordTypes as $type => $value){
948 $use = "";
949 if($type == $selected){
950 $use = " selected ";
951 }
952 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
953 }
954 $str.="</select>";
955 return($str);
956 }
959 /* Return plugin informations for acl handling */
960 static function plInfo()
961 {
962 $tmp = array(
963 "plShortName" => _("DNS"),
964 "plDescription" => _("DNS settings"),
965 "plSelfModify" => FALSE,
966 "plDepends" => array(),
967 "plPriority" => 5,
968 "plSection" => array("administration"),
969 "plCategory" => array("workstation","terminal","phone","server","component","printer","winworkstation"),
971 "plProvidedAcls"=> array(
972 "ipHostNumber" => _("IP address"),
973 "macAddress" => _("MAC address"))
974 );
976 /* Hide all dns specific code, if dns is not available
977 */
978 $config = session::get('config');
979 foreach($config->data['TABS']['SERVERSERVICE'] as $tab){
980 if(preg_match("/^servdns$/",$tab['CLASS'])){
981 $tmp['plProvidedAcls']["Records"] = _("DNS records");
982 $tmp['plProvidedAcls']["zoneName"] = _("Zone name");
983 $tmp['plProvidedAcls']["dNSTTL"] = _("TTL");
984 break;
985 }
986 }
987 return($tmp);
988 }
991 function get_dhcp_host_entry()
992 {
993 $attrs = array();
994 $dn = $this->get_dhcp_host_entry_dn();
995 if($dn){
996 $ldap = $this->config->get_ldap_link();
997 $ldap->cd($this->config->current['BASE']);
998 $ldap->cat($dn,array("*"));
999 if($ldap->count()){
1000 $attrs = $ldap->fetch();
1001 foreach($attrs as $key => $value){
1002 if(is_numeric($key) || ($key == "count")){
1003 unset($attrs[$key]);
1004 }
1005 if(is_array($value) && isset($value['count'])){
1006 unset($attrs[$key]['count']);
1007 }
1008 }
1009 }
1010 }
1011 return($attrs);
1012 }
1015 function get_dhcp_host_entry_dn()
1016 {
1017 $ldap = $this->config->get_ldap_link();
1018 $ldap->cd($this->config->current['BASE']);
1019 $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
1021 if($ldap->count()){
1022 $attr = $ldap->fetch();
1023 return($attr['dn']);
1024 }else{
1025 return("");
1026 }
1027 }
1030 function get_dhcp_parent_node()
1031 {
1032 return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
1033 }
1036 function get_dhcp_parent_nodes()
1037 {
1038 $ldap = $this->config->get_ldap_link();
1039 $ldap->cd($this->config->current['BASE']);
1040 $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
1042 $dhcp_dns = array();
1043 while($attr = $ldap->fetch()){
1044 $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
1045 }
1047 foreach($dhcp_dns as $key => $pri_dns){
1048 $ldap->cat($pri_dns,array("cn"));
1049 $tmp = $ldap->fetch();
1050 if(isset($tmp['cn'][0])){
1051 $dhcp_dns[$key] = $tmp['cn'][0];
1052 }else{
1053 unset($dhcp_dns[$key]);
1054 }
1055 }
1057 $tmp = $tmp2 = array();
1058 foreach($dhcp_dns as $dn => $cn){
1059 $ldap->cd($dn);
1060 $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
1061 "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
1062 while($attr = $ldap->fetch()){
1063 $tmp[$attr['dn']] = $attr['cn'][0];
1064 }
1065 $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.") "));
1066 }
1067 return($tmp2);
1068 }
1071 /* this function returns the default ptr record entry */
1072 function get_pTRRecord()
1073 {
1074 if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
1075 $ldap = $this->config->get_ldap_link();
1076 $ldap->cat(DNS::getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
1077 $attrs = $ldap->fetch();
1078 $tmp = array_flip($this->Zones);
1079 $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
1080 $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
1081 $ptr = preg_replace("/^".normalizePreg(DNS::FlipIp($tmp))."\./","",$this->ipHostNumber);
1082 return($ptr);
1083 }else{
1084 return(FALSE);
1085 }
1086 }
1089 function generateRandomIP($net = "")
1090 {
1091 $str = $net;
1092 $cnt = 4;
1093 while(substr_count($str,".") < 3 && $cnt > 0){
1094 $str .= ".".rand(0,255);
1095 $str = preg_replace("/\.\.*/",".",$str);
1096 $str = trim($str,". ");
1097 $cnt --;
1098 }
1099 return($str);
1100 }
1103 function create_tree($arr,$base,$current = "")
1104 {
1105 $ret = array();
1106 foreach($arr as $r => $name){
1107 $base_part = str_replace($base,"",$r);
1108 if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
1109 $ret[$r] = $current.$name;
1110 $tmp = $this->create_tree($arr,$r,$current.". ");
1111 foreach($tmp as $sub_key => $sub_name){
1112 $ret[$sub_key] = $sub_name;
1113 }
1114 }
1115 }
1116 return($ret);
1117 }
1119 function force_dns()
1120 {
1121 if($this->DNSenabled){
1123 /* Only force DNS account, if we have at least on dns Zone */
1124 if(count($this->Zones)){
1125 $this->DNS_is_account = TRUE;
1126 $this->hide_dns_check_box = TRUE;
1127 }
1128 }
1129 }
1130 }
1132 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1133 ?>