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("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("systemRDN"));
188 $a_ous = array(get_ou("serverRDN"),
189 get_ou("terminalRDN"),
190 get_ou("workstationRDN"),
191 get_ou("printerRDN"),
192 get_ou("phoneRDN"),
193 get_ou("componentRDN"));
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 if($this->acl_is_writeable("ipHostNumber")){
271 foreach($_POST as $name => $value){
272 if(preg_match("/^additionalHostNumbers_add/",$name)){
273 $this->additionalHostNumbers[] = "";
274 break;
275 }
277 /* Delete given entry */
278 if(preg_match("/^additionalHostNumbers_del_/",$name)){
279 $id = preg_replace("/^^additionalHostNumbers_del_([0-9]*)_.*/","\\1",$name);
280 if(isset($this->additionalHostNumbers[$id])){
281 unset($this->additionalHostNumbers[$id]);
282 $this->additionalHostNumbers = array_values($this->additionalHostNumbers);
283 }
284 break;
285 }
286 }
287 }
289 $smarty->assign("additionalHostNumbers",$this->additionalHostNumbers);
290 $smarty->assign("staticAddress", "");
292 /**********
293 * Autonet completion
294 **********/
296 /* Check for autonet button */
297 if (isset($_POST['autonet']) && ($this->acl_is_writeable("ipHostNumber") || $this->acl_is_writeable("macAddress"))){
298 $d= new gosaSupportDaemon(TRUE, 0.5);
299 $res= $d->_send("<xml><header>gosa_network_completition</header>".
300 "<source>GOSA</source><target>GOSA</target><hostname>".$this->cn."</hostname></xml>", TRUE);
301 if (isset($res['XML']['IP']) && $this->acl_is_writeable("ipHostNumber")){
302 $this->ipHostNumber= $res['XML']['IP'];
303 }
304 if (isset($res['XML']['MAC']) && $this->acl_is_writeable("macAddress")){
305 $this->macAddress= $res['XML']['MAC'];
306 }
307 }
310 /**********
311 * DHCP Handling
312 **********/
314 if(isset($_POST['dhcpEditOptions']) && $this->acl_is_readable("dhcpSetup")){
315 if(count($this->dhcpHostEntry) == 0){
316 $this->dialog = new dhcpHost($this->parent,$this->dhcpParentNode,TRUE);
317 }else{
318 $this->dialog = new dhcpHost($this->parent,$this->dhcpHostEntry,TRUE);
319 }
320 $this->dialog->cn = $this->cn;
321 $this->dialog->read_only = !$this->acl_is_writeable("dhcpSetup");
322 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
323 if(!empty($this->ipHostNumber)){
324 $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
325 }
326 }
328 if(isset($_POST['cancel_dhcp'])){
329 $this->dialog = FALSE;
330 }
332 if(isset($_POST['save_dhcp']) && $this->acl_is_writeable("dhcpSetup")){
333 $this->dialog->save_object();
334 $msgs = $this->dialog->check(array());
335 if(count($msgs)){
336 foreach($msgs as $msg){
337 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
338 }
339 }else{
340 $this->dhcpHostEntry = $this->dialog->save();
341 $this->dialog = FALSE;
342 }
343 }
345 if(is_object($this->dialog)){
346 $this->dialog->save_object();
347 return($this->dialog->execute());
348 }
350 $smarty->assign("dhcpEnabled", $this->dhcpEnabled && $this->acl_is_readable("dhcpSetup"));
351 $smarty->assign("dhcp_is_Account",$this->dhcp_is_Account);
352 $smarty->assign("dhcpParentNode", $this->dhcpParentNode);
353 $smarty->assign("dhcpParentNodes",$this->dhcpParentNodes);
354 $smarty->assign("dhcpParentNodeCnt",count($this->dhcpParentNodes));
356 /**********
357 * DNS Handling
358 **********/
360 /* There is no dns available
361 */
362 $smarty->assign("DNS_is_account",$this->DNS_is_account);
363 $smarty->assign("DNSenabled",$this->DNSenabled && $this->acl_is_readable("dnsSetup"));
365 if($this->DNSenabled == false){
367 /* Is IP address must ? */
368 # $smarty->assign("DNS_is_account",false);
369 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
371 /* Assign smarty all non DNs attributes */
372 foreach($this->attributes as $attr){
373 $smarty->assign($attr,$this->$attr);
374 }
375 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
377 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
378 }else{
379 # $smarty->assign("DNS_is_account",true);
381 /* Add new empty array to our record list */
382 if(isset($_POST['AddNewRecord']) && $this->acl_is_writeable("dnsSetup")){
383 $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>"");
384 }
386 /* propose_ip */
387 if(isset($_POST['propose_ip']) && $this->acl_is_writeable("ipHostNumber")){
388 foreach($this->Zones as $key => $name){
389 if($name == $this->dnsEntry['zoneName']){
390 $net = DNS::FlipIp(str_replace(".in-addr.arpa","",DNS::getNameFromMix($key)));
391 $this->ipHostNumber = $this->generateRandomIp($net);
392 }
393 }
394 }
396 /* Handle all posts */
397 $only_once =true;
398 if($this->acl_is_writeable("dnsSetup")){
399 foreach($_POST as $name => $value){
401 /* Check if we have to delete a record entry */
402 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
404 /* Avoid performing this once again */
405 $only_once = false;
407 /* Extract id for specified entry */
408 $id = preg_replace("/RemoveRecord_/","",$name);
409 $id = preg_replace("/_.*$/","",$id);
411 /* Delete this record, mark edited entries to be able to delete them */
412 if(isset($this->dnsEntry['RECORDS'][$id])){
413 unset($this->dnsEntry['RECORDS'][$id]);
414 }
415 }
416 }
417 }
418 /* Assign smarty all non DNs attributes */
419 foreach($this->attributes as $attr){
420 $smarty->assign($attr,$this->$attr);
421 }
423 /* Assign smarty all DNS attributes */
424 foreach($this->DNSattributes as $attr){
425 $smarty->assign($attr,$this->dnsEntry[$attr]);
426 }
428 /* Assign all needed vars */
429 # $smarty->assign("DNSAccount",$this->DNS_is_account);
430 $smarty->assign("hide_dns_check_box",$this->hide_dns_check_box);
432 $smarty->assign("Zones",$this->Zones);
433 $smarty->assign("ZoneCnt",count($this->Zones));
434 $smarty->assign("ZoneKeys",($this->Zones));
435 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
437 /* Create zone array */
438 $idZones = array();
439 foreach($this->Zones as $id => $zone){
440 if($this->netmaskIsCoherent($id)) {
441 $idZones[$id] = $zone;
442 }else{
443 $idZones[$id] = $zone." ("._("Not matching").")";
444 }
445 }
446 $smarty->assign("Zones",$idZones);
447 $smarty->assign("ZoneKeys", $this->Zones);
449 $tmp = $this->generateRecordsList();
451 $changeStateForRecords = $tmp['changeStateForRecords'];
453 $smarty->assign("records",$tmp['str']);
454 $smarty->assign("changeStateForRecords",$changeStateForRecords);
455 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
457 $smarty->assign("autonetACL",$this->acl_is_writeable("macAddress").$this->acl_is_writeable("ipHostNumber"));
459 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
460 }
462 return($display);
463 }
466 function remove_from_parent()
467 {
468 if($this->DNS_is_account){
470 $ldap = $this->config->get_ldap_link();
472 $tmp = array();
473 $this->dnsEntry['exists'] = false;
474 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
476 /* Delete dns */
477 foreach($tmp['del'] as $dn => $del){
478 $ldap->cd($dn);
479 $ldap->rmdir_recursive($dn);
480 new log("remove","unknown/".get_class($this),$dn);
481 if (!$ldap->success()){
482 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_DEL, get_class()));
483 }
484 }
485 }
486 }
489 /* Save data to object */
490 function save_object()
491 {
493 if(isset($_POST['network_tpl_posted'])){
495 /* Save all posted vars */
496 plugin::save_object();
498 /******
499 Additional IP Host Numbers
500 ******/
502 /* Get posts for all additionally added ipHostNumbers */
503 if($this->acl_is_writeable("ipHostNumber")){
504 foreach($this->additionalHostNumbers as $id => $value){
505 if(isset($_POST['additionalHostNumbers_'.$id])){
506 $this->additionalHostNumbers[$id] = get_post('additionalHostNumbers_'.$id);
507 }
508 }
509 }
512 /******
513 DHCP posts
514 ******/
516 if($this->acl_is_writeable("dhcpSetup") && $this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
517 foreach($this->dhcpAttributes as $attr){
518 if(isset($_POST[$attr])){
519 $this->$attr = $_POST[$attr];
520 }
521 }
522 if(isset($_POST['dhcp_is_Account'])){
523 $this->dhcp_is_Account = TRUE;
524 }else{
525 $this->dhcp_is_Account = FALSE;
526 }
527 }
529 /* Ge all non dns attributes (IP/MAC)*/
530 foreach($this->attributes as $attr){
531 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
532 $this->$attr = $_POST[$attr];
533 }
534 }
537 /******
538 DNS posts
539 ******/
541 /* Check if DNS should be enabled / disabled */
542 if($this->DNS_is_account && $this->acl_is_writeable("dnsSetup") && !isset($_POST['DNS_is_account'])){
543 $this->DNS_is_account = false;
544 }elseif(!$this->DNS_is_account && $this->acl_is_writeable("dnsSetup") && isset($_POST['DNS_is_account'])){
545 $this->DNS_is_account = true;
546 }
548 /* Get dns attributes */
549 if(($this->DNSenabled) && (isset($_POST['network_tpl_posted'])) && $this->acl_is_writeable("dnsSetup")){
551 /* Check for posted record changes */
552 if(is_array($this->dnsEntry['RECORDS'])){
553 foreach($this->dnsEntry['RECORDS'] as $key => $value){
555 /* Check if type has changed */
556 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
557 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
558 }
559 /* Check if value has changed */
560 if(isset($_POST['RecordValue_'.$key])){
561 $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
562 }
563 }
564 }
566 /* Get all basic DNS attributes (TTL, Clas ..)*/
567 foreach($this->DNSattributes as $attr){
568 if(isset($_POST[$attr])){
569 $this->dnsEntry[$attr] = $_POST[$attr];
570 }
571 }
572 }
573 if($this->hide_dns_check_box){
574 $this->DNS_is_account = true;
575 }
576 }
577 }
580 /* Check supplied data */
581 function check()
582 {
583 /* Call common method to give check the hook */
584 $message= plugin::check();
586 /******
587 check additional IP Host Numbers
588 ******/
590 foreach($this->additionalHostNumbers as $id => $value){
591 if(!tests::is_ip($value)){
592 $message[]= msgPool::invalid(sprintf(_("IP address %s"),($id +2)), "", "", "192.168.1.10");
593 }
594 }
597 /* Check if mac and ip are already used */
598 if(!empty($this->ipHostNumber) && $this->DNS_is_account &&
599 $this->ipHostNumber != $this->orig_ipHostNumber &&
600 in_array("ip:".$this->ipHostNumber,$this->used_ip_mac)){
601 $message[]= msgPool::duplicated(_("IP address"));
602 }
603 if(!empty($this->macAddress) && $this->dhcp_is_Account &&
604 $this->macAddress != $this->orig_macAddress &&
605 in_array("mac:".$this->macAddress,$this->used_ip_mac)){
606 $message[]= msgPool::duplicated(_("MAC address"));
607 }
609 /* Check if ip must be given
610 */
611 if(($this->IPisMust)||($this->DNS_is_account)){
612 if (empty($this->ipHostNumber)){
613 $message[]= msgPool::required(_("IP address"));
614 }elseif (!tests::is_ip($this->ipHostNumber)){
615 $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.10");
616 }
617 }
619 /* Check if mac is empty
620 */
621 if($this->MACisMust || $this->dhcp_is_Account){
622 if ($this->macAddress == "" ){
623 $message[]= msgPool::required(_("MAC address"));
624 }elseif(!tests::is_mac($this->macAddress)){
625 $message[]= msgPool::invalid(_("MAC address"), "", "", "00:0C:7F:31:33:F1");
626 }
627 }
629 /* only perfrom this checks if this is a valid DNS account */
630 if($this->DNS_is_account){
632 $checkArray = array();
633 $onlyOnce = array();
635 // $onlyOnce['cNAMERecord'] = 0;
636 $tmp = array_flip($this->Zones);
637 $tmp2 = $tmp[$this->dnsEntry['zoneName']];
638 if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
639 $tmp2 = preg_replace("/^.*\//","",$tmp2);
640 $message[] =sprintf(_("The IP address '%s' is not part of the selected reverse zone '%s'!"),$this->ipHostNumber,$tmp2);
641 }
643 /* Walk through all entries and detect duplicates or mismatches
644 */
645 foreach($this->dnsEntry['RECORDS'] as $name => $values){
647 /* Count record values, to detect duplicate entries for a specific record
648 */
649 if(!isset($checkArray[$values['type']][$values['value']])){
650 $checkArray[$values['type']][$values['value']] = 0;
651 }else{
652 $message[] = sprintf(_("Record type '%s' is duplicated!"),$values['type']);
653 }
655 /* Check if given entries in $onlyOnce are used more than once
656 */
657 if(isset($onlyOnce[$values['type']])){
658 $onlyOnce[$values['type']] ++;
659 if($onlyOnce[$values['type']] > 1){
660 $message[] = sprintf(_("Uniq record type '%s' is duplicated!"),$values['type']);
661 }
662 }
664 /* Skip txt record ...
665 */
666 if($values['type'] == "tXTRecord") continue;
668 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
669 */
670 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
671 #TODO: Where's the problem here?
672 $message[]=sprintf(_("The IP address '%s' will be added as 'A Record', this will be done automatically, please remove the record."),
673 $this->ipHostNumber);
674 }
676 /* only lower-case is allowed in record entries ...
677 */
678 if($values['value'] != strtolower($values['value'])){
679 #TODO: What's in values['value']? Something for a propper message?
680 $message[] = sprintf(_("Only lowercase records are allowed, please check your '%ss'."),$values['type']);
681 }
682 }
683 }
684 return ($message);
685 }
688 /* Save to LDAP */
689 function save()
690 {
691 $ldap= $this->config->get_ldap_link();
693 $dn = $this->parent->dn;
695 /*******************/
696 /* IP-MAC HANDLING */
697 /*******************/
699 /* $dn was posted as parameter */
700 $this->dn = $dn;
702 /* Save DNS setting & ip/Mac*/
703 plugin::save();
705 /* Add all additional ipHostNumbers now
706 */
707 if(!empty($this->ipHostNumber)){
708 $this->attrs['ipHostNumber'] = array($this->ipHostNumber);
709 }
710 foreach($this->additionalHostNumbers as $value){
711 $this->attrs['ipHostNumber'][] = $value;
712 }
714 /* Write back to ldap */
715 $ldap->cd($this->dn);
716 $this->cleanup();
717 $ldap->modify ($this->attrs);
719 /****************/
720 /* DHCP HANDLING */
721 /****************/
723 /* New entry */
724 if($this->dhcpEnabled && $this->acl_is_writeable("dhcpSetup")) {
726 if(count($this->dhcpHostEntry) == 0){
727 $this->dialog = new dhcpHost($this->parent,$this->dhcpParentNode,TRUE);
728 $this->dialog->cn = $this->cn;
729 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
730 if(!empty($this->ipHostNumber)){
731 $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
732 }
733 $this->dialog->execute();
734 $this->dialog->save_object();
735 $this->dhcpHostEntry = $this->dialog->save();
736 if(count($this->dhcpHostEntry['dhcpOption']) == 0){
737 $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
738 }
739 }
741 /* Write mac address to dhcp settings */
742 if($this->dhcp_is_Account){
743 if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) ||
744 !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
745 $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
746 $this->dhcpHostEntry['MODIFIED'] = TRUE;
747 }
748 }
750 /* Updated IP host number */
751 if($this->dhcp_is_Account){
752 foreach($this->dhcpHostEntry['dhcpStatements'] as $id => $value){
753 if(preg_match("/^fixed-address/",$value)){
754 $this->dhcpHostEntry['dhcpStatements'][$id] = "fixed-address ".$this->ipHostNumber;
755 $this->dhcpHostEntry['MODIFIED'] = TRUE;
756 }
757 }
758 }
760 /* Unset dhcpStatements if this attribute is empty */
761 if(isset($this->dhcpHostEntry['dhcpStatements']) &&
762 ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
763 unset($this->dhcpHostEntry['dhcpStatements']);
764 }
766 /* DHCP removed */
767 if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
768 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
769 if (!$ldap->success()){
770 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_DEL, get_class()));
771 }
773 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
774 $tmp->handle_post_events("remove");
775 }
777 /* DHCP Added */
778 if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
779 $attrs = $this->dhcpHostEntry;
780 unset($attrs['MODIFIED']);
781 unset($attrs['dn']);
782 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
783 $res = $ldap->add($attrs);
785 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
786 $tmp->handle_post_events("add");
788 if (!$ldap->success()){
789 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), "cn=".$this->cn.",".$this->dhcpParentNode, LDAP_ADD, get_class()));
790 }
791 }
793 /* DHCP still activated */
794 if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
796 /* DHCP node changed */
797 if(($this->initial_dhcpParentNode != $this->dhcpParentNode) ||
798 ($this->cn != $this->OrigCn)){
799 $attrs = $this->dhcpHostEntry;
800 $attrs['cn'] = $this->cn;
801 unset($attrs['dn']);
802 unset($attrs['MODIFIED']);
803 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
804 $res = $ldap->add($attrs);
806 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
807 $tmp->handle_post_events("modify");
809 if (!$ldap->success()){
810 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), "cn=".$this->cn.",".$this->dhcpParentNode, LDAP_ADD, get_class()));
811 }
812 if($res){
813 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
814 if (!$ldap->success()){
815 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_DEL, get_class()));
816 }
817 }
818 }
820 /* SAME node but modified */
821 if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1 &&
822 $this->initial_dhcpParentNode == $this->dhcpParentNode){
823 $attrs = $this->dhcpHostEntry;
824 unset($attrs['dn']);
825 unset($attrs['MODIFIED']);
826 $ldap->cd($this->dhcpHostEntry['dn']);
827 $ldap->modify($attrs);
829 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
830 $tmp->handle_post_events("modify");
832 if (!$ldap->success()){
833 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_MOD, get_class()));
834 }
835 }
836 }
837 }
838 $this->dialog = FALSE;
840 /****************/
841 /* DNS HANDLING */
842 /****************/
844 /* If isn't DNS account but initially was DNS account
845 remove all DNS entries
846 */
847 if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
848 return;
849 }elseif($this->acl_is_writeable("dnsSetup")){
851 /* Add ipHostNumber to aRecords
852 */
853 $backup_dnsEntry = $this->dnsEntry;
854 if(!empty($this->ipHostNumber)){
855 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
856 $ptr = $this->get_pTRRecord();
857 if(!empty($ptr)){
858 $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
859 }
860 }
862 /* Create diff and follow instructions
863 * If Account was disabled, remove account by setting exists to false
864 */
865 if((!$this->DNS_is_account)&&($this->initially_was_account)){
866 $this->dnsEntry['exists'] = false;
867 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
868 }else{
869 $this->dnsEntry['exists'] = $this->DNS_is_account;
870 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
871 }
873 /* move follwoing entries
874 */
875 foreach($tmp['move'] as $src => $dst){
876 $this->recursive_move($src,$dst);
877 }
879 /* Delete dns */
880 foreach($tmp['del'] as $dn => $del){
881 $ldap->cd($dn);
882 $ldap->rmdir_recursive($dn);
883 new log("modify","unknown/".get_class($this),$dn,array("*"),$ldap->get_error());
884 }
886 /* Add || Update new DNS entries
887 */
888 foreach($tmp['add'] as $dn => $attrs){
889 $ldap->cd($dn);
890 $ldap->cat($dn, array('dn'));
891 if(count($ldap->fetch())){
892 $ldap->cd($dn);
893 $ldap->modify ($attrs);
894 }else{
895 $ldap->cd($dn);
896 $ldap->add($attrs);
897 }
898 new log("modify","unknown/".get_class($this),$dn,array_keys($attrs),$ldap->get_error());
899 }
902 /* Display errors
903 */
904 if (!$ldap->success()){
905 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, 0, get_class()));
906 }
908 $tmp2 = new servdns($this->config,$this->dn);
909 $tmp2->handle_post_events("modify");
911 $this->dnsEntry = $backup_dnsEntry;
912 }
913 }
915 /* Create html table with all used record types
916 */
917 function generateRecordsList()
918 {
919 $changeStateForRecords = "";
921 if(!$this->DNS_is_account) {
922 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
923 return(array("str" => $str, "changeStateForRecords"=> ""));
924 }
926 $str = "<table summary='' width='100%'>";
927 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
929 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
930 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
931 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
933 $str.=" <tr>".
934 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
935 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
936 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
937 "</tr>";
938 }
940 $str.= " <tr>".
941 " <td colspan=2 width='50%'></td><td>".
942 " <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
943 " </td>".
944 " </tr>".
945 "</table>";
946 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
947 return($ret);
948 }
951 /* Create a html select box which allows us to select different types of records
952 */
953 function generateRecordListBox($selected,$name)
954 {
955 $str = "<select name='".$name."' id='".$name."'>";
956 foreach($this->RecordTypes as $type => $value){
957 $use = "";
958 if($type == $selected){
959 $use = " selected ";
960 }
961 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
962 }
963 $str.="</select>";
964 return($str);
965 }
968 /* Return plugin informations for acl handling */
969 static function plInfo()
970 {
971 $tmp = array(
972 "plShortName" => _("DNS"),
973 "plDescription" => _("DNS settings"),
974 "plSelfModify" => FALSE,
975 "plDepends" => array(),
976 "plPriority" => 5,
977 "plSection" => array("administration"),
978 "plCategory" => array("workstation","terminal","phone","server","component","printer","winworkstation"),
980 "plProvidedAcls"=> array(
981 "ipHostNumber" => _("IP address"),
982 "macAddress" => _("MAC address"))
983 );
985 /* Hide all dns/dhcp configurations if not available
986 */
987 if(class_available("servdns")){
988 $tmp['plProvidedAcls']["dnsSetup"] = _("DNS configuration");
989 }
990 if(class_available("servdhcp")){
991 $tmp['plProvidedAcls']["dhcpSetup"] = _("DHCP configuration");
992 }
993 return($tmp);
994 }
997 function get_dhcp_host_entry()
998 {
999 $attrs = array();
1000 $dn = $this->get_dhcp_host_entry_dn();
1001 if($dn){
1002 $ldap = $this->config->get_ldap_link();
1003 $ldap->cd($this->config->current['BASE']);
1004 $ldap->cat($dn,array("*"));
1005 if($ldap->count()){
1006 $attrs = $ldap->fetch();
1007 foreach($attrs as $key => $value){
1008 if(is_numeric($key) || ($key == "count")){
1009 unset($attrs[$key]);
1010 }
1011 if(is_array($value) && isset($value['count'])){
1012 unset($attrs[$key]['count']);
1013 }
1014 }
1015 }
1016 }
1017 return($attrs);
1018 }
1021 function get_dhcp_host_entry_dn()
1022 {
1023 $ldap = $this->config->get_ldap_link();
1024 $ldap->cd($this->config->current['BASE']);
1025 $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
1027 if($ldap->count()){
1028 $attr = $ldap->fetch();
1029 return($attr['dn']);
1030 }else{
1031 return("");
1032 }
1033 }
1036 function get_dhcp_parent_node()
1037 {
1038 return(preg_replace("/^cn=".preg_quote($this->cn, '/').",/","",$this->get_dhcp_host_entry_dn()));
1039 }
1042 function get_dhcp_parent_nodes()
1043 {
1044 $ldap = $this->config->get_ldap_link();
1045 $ldap->cd($this->config->current['BASE']);
1046 $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
1048 $dhcp_dns = array();
1049 while($attr = $ldap->fetch()){
1050 $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
1051 }
1053 foreach($dhcp_dns as $key => $pri_dns){
1054 $ldap->cat($pri_dns,array("cn"));
1055 $tmp = $ldap->fetch();
1056 if(isset($tmp['cn'][0])){
1057 $dhcp_dns[$key] = $tmp['cn'][0];
1058 }else{
1059 unset($dhcp_dns[$key]);
1060 }
1061 }
1063 $tmp = $tmp2 = array();
1064 foreach($dhcp_dns as $dn => $cn){
1065 $ldap->cd($dn);
1066 $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
1067 "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
1068 while($attr = $ldap->fetch()){
1069 $tmp[$attr['dn']] = $attr['cn'][0];
1070 }
1071 $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.") "));
1072 }
1073 return($tmp2);
1074 }
1077 /* this function returns the default ptr record entry */
1078 function get_pTRRecord()
1079 {
1080 if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
1081 $ldap = $this->config->get_ldap_link();
1082 $ldap->cat(DNS::getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
1083 $attrs = $ldap->fetch();
1084 $tmp = array_flip($this->Zones);
1085 $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
1086 $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
1087 $ptr = preg_replace("/^".preg_quote(DNS::FlipIp($tmp), '/')."\./","",$this->ipHostNumber);
1088 return($ptr);
1089 }else{
1090 return(FALSE);
1091 }
1092 }
1095 function generateRandomIP($net = "")
1096 {
1097 $str = $net;
1098 $cnt = 4;
1099 while(substr_count($str,".") < 3 && $cnt > 0){
1100 $str .= ".".rand(0,255);
1101 $str = preg_replace("/\.\.*/",".",$str);
1102 $str = trim($str,". ");
1103 $cnt --;
1104 }
1105 return($str);
1106 }
1109 function create_tree($arr,$base,$current = "")
1110 {
1111 $ret = array();
1112 foreach($arr as $r => $name){
1113 $base_part = str_replace($base,"",$r);
1114 if(preg_match("/^[a-z]*=".preg_quote($name, '/')."(|,)$/i",$base_part)){
1115 $ret[$r] = $current.$name;
1116 $tmp = $this->create_tree($arr,$r,$current.". ");
1117 foreach($tmp as $sub_key => $sub_name){
1118 $ret[$sub_key] = $sub_name;
1119 }
1120 }
1121 }
1122 return($ret);
1123 }
1125 function force_dns()
1126 {
1127 if($this->DNSenabled){
1129 /* Only force DNS account, if we have at least on dns Zone */
1130 if(count($this->Zones)){
1131 $this->DNS_is_account = TRUE;
1132 $this->hide_dns_check_box = TRUE;
1133 }
1134 }
1135 }
1136 }
1138 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1139 ?>