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("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 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 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
458 }
460 return($display);
461 }
464 function remove_from_parent()
465 {
466 if($this->DNS_is_account){
468 $ldap = $this->config->get_ldap_link();
470 $tmp = array();
471 $this->dnsEntry['exists'] = false;
472 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
474 /* Delete dns */
475 foreach($tmp['del'] as $dn => $del){
476 $ldap->cd($dn);
477 $ldap->rmdir_recursive($dn);
478 new log("remove","unknown/".get_class($this),$dn);
479 if (!$ldap->success()){
480 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_DEL, get_class()));
481 }
482 }
483 }
484 }
487 /* Save data to object */
488 function save_object()
489 {
491 if(isset($_POST['network_tpl_posted'])){
493 /* Save all posted vars */
494 plugin::save_object();
496 /******
497 Additional IP Host Numbers
498 ******/
500 /* Get posts for all additionally added ipHostNumbers */
501 if($this->acl_is_writeable("ipHostNumber")){
502 foreach($this->additionalHostNumbers as $id => $value){
503 if(isset($_POST['additionalHostNumbers_'.$id])){
504 $this->additionalHostNumbers[$id] = get_post('additionalHostNumbers_'.$id);
505 }
506 }
507 }
510 /******
511 DHCP posts
512 ******/
514 if($this->acl_is_writeable("dhcpSetup") && $this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
515 foreach($this->dhcpAttributes as $attr){
516 if(isset($_POST[$attr])){
517 $this->$attr = $_POST[$attr];
518 }
519 }
520 if(isset($_POST['dhcp_is_Account'])){
521 $this->dhcp_is_Account = TRUE;
522 }else{
523 $this->dhcp_is_Account = FALSE;
524 }
525 }
527 /* Ge all non dns attributes (IP/MAC)*/
528 foreach($this->attributes as $attr){
529 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
530 $this->$attr = $_POST[$attr];
531 }
532 }
535 /******
536 DNS posts
537 ******/
539 /* Check if DNS should be enabled / disabled */
540 if($this->DNS_is_account && $this->acl_is_writeable("dnsSetup") && !isset($_POST['DNS_is_account'])){
541 $this->DNS_is_account = false;
542 }elseif(!$this->DNS_is_account && $this->acl_is_writeable("dnsSetup") && isset($_POST['DNS_is_account'])){
543 $this->DNS_is_account = true;
544 }
546 /* Get dns attributes */
547 if(($this->DNSenabled) && (isset($_POST['network_tpl_posted'])) && $this->acl_is_writeable("dnsSetup")){
549 /* Check for posted record changes */
550 if(is_array($this->dnsEntry['RECORDS'])){
551 foreach($this->dnsEntry['RECORDS'] as $key => $value){
553 /* Check if type has changed */
554 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
555 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
556 }
557 /* Check if value has changed */
558 if(isset($_POST['RecordValue_'.$key])){
559 $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
560 }
561 }
562 }
564 /* Get all basic DNS attributes (TTL, Clas ..)*/
565 foreach($this->DNSattributes as $attr){
566 if(isset($_POST[$attr])){
567 $this->dnsEntry[$attr] = $_POST[$attr];
568 }
569 }
570 }
571 if($this->hide_dns_check_box){
572 $this->DNS_is_account = true;
573 }
574 }
575 }
578 /* Check supplied data */
579 function check()
580 {
581 /* Call common method to give check the hook */
582 $message= plugin::check();
584 /******
585 check additional IP Host Numbers
586 ******/
588 foreach($this->additionalHostNumbers as $id => $value){
589 if(!tests::is_ip($value)){
590 $message[]= msgPool::invalid(sprintf(_("IP address #%s"),($id +2)), "", "", "192.168.1.10");
591 }
592 }
595 /* Check if mac and ip are already used */
596 if(!empty($this->ipHostNumber) && $this->DNS_is_account &&
597 $this->ipHostNumber != $this->orig_ipHostNumber &&
598 in_array("ip:".$this->ipHostNumber,$this->used_ip_mac)){
599 $message[]= msgPool::duplicated(_("IP address"));
600 }
601 if(!empty($this->macAddress) && $this->dhcp_is_Account &&
602 $this->macAddress != $this->orig_macAddress &&
603 in_array("mac:".$this->macAddress,$this->used_ip_mac)){
604 $message[]= msgPool::duplicated(_("MAC address"));
605 }
607 /* Check if ip must be given
608 */
609 if(($this->IPisMust)||($this->DNS_is_account)){
610 if (empty($this->ipHostNumber)){
611 $message[]= msgPool::required(_("IP address"));
612 }elseif (!tests::is_ip($this->ipHostNumber)){
613 $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.10");
614 }
615 }
617 /* Check if mac is empty
618 */
619 if($this->MACisMust || $this->dhcp_is_Account){
620 if ($this->macAddress == "" ){
621 $message[]= msgPool::required(_("MAC address"));
622 }elseif(!tests::is_mac($this->macAddress)){
623 $message[]= msgPool::invalid(_("MAC address"), "", "", "00:0C:7F:31:33:F1");
624 }
625 }
627 /* only perfrom this checks if this is a valid DNS account */
628 if($this->DNS_is_account){
630 $checkArray = array();
631 $onlyOnce = array();
633 // $onlyOnce['cNAMERecord'] = 0;
634 $tmp = array_flip($this->Zones);
635 $tmp2 = $tmp[$this->dnsEntry['zoneName']];
636 if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
637 $tmp2 = preg_replace("/^.*\//","",$tmp2);
638 $message[] =sprintf(_("The IP address '%s' is not part of the selected reverse zone '%s'!"),$this->ipHostNumber,$tmp2);
639 }
641 /* Walk through all entries and detect duplicates or mismatches
642 */
643 foreach($this->dnsEntry['RECORDS'] as $name => $values){
645 /* Count record values, to detect duplicate entries for a specific record
646 */
647 if(!isset($checkArray[$values['type']][$values['value']])){
648 $checkArray[$values['type']][$values['value']] = 0;
649 }else{
650 $message[] = sprintf(_("Record type '%s' is duplicated!"),$values['type']);
651 }
653 /* Check if given entries in $onlyOnce are used more than once
654 */
655 if(isset($onlyOnce[$values['type']])){
656 $onlyOnce[$values['type']] ++;
657 if($onlyOnce[$values['type']] > 1){
658 $message[] = sprintf(_("Uniq record type '%s' is duplicated!"),$values['type']);
659 }
660 }
662 /* Skip txt record ...
663 */
664 if($values['type'] == "tXTRecord") continue;
666 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
667 */
668 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
669 #TODO: Where's the problem here?
670 $message[]=sprintf(_("The IP address '%s' will be added as 'A Record', this will be done automatically, please remove the record."),
671 $this->ipHostNumber);
672 }
674 /* only lower-case is allowed in record entries ...
675 */
676 if($values['value'] != strtolower($values['value'])){
677 #TODO: What's in values['value']? Something for a propper message?
678 $message[] = sprintf(_("Only lowercase records are allowed, please check your '%ss'."),$values['type']);
679 }
680 }
681 }
682 return ($message);
683 }
686 /* Save to LDAP */
687 function save()
688 {
689 $ldap= $this->config->get_ldap_link();
691 $dn = $this->parent->dn;
693 /*******************/
694 /* IP-MAC HANDLING */
695 /*******************/
697 /* $dn was posted as parameter */
698 $this->dn = $dn;
700 /* Save DNS setting & ip/Mac*/
701 plugin::save();
703 /* Add all additional ipHostNumbers now
704 */
705 if(!empty($this->ipHostNumber)){
706 $this->attrs['ipHostNumber'] = array($this->ipHostNumber);
707 }
708 foreach($this->additionalHostNumbers as $value){
709 $this->attrs['ipHostNumber'][] = $value;
710 }
712 /* Write back to ldap */
713 $ldap->cd($this->dn);
714 $this->cleanup();
715 $ldap->modify ($this->attrs);
717 /****************/
718 /* DHCP HANDLING */
719 /****************/
721 /* New entry */
722 if($this->dhcpEnabled && $this->acl_is_writeable("dhcpSetup")) {
724 if(count($this->dhcpHostEntry) == 0){
725 $this->dialog = new dhcpHost($this->parent,$this->dhcpParentNode,TRUE);
726 $this->dialog->cn = $this->cn;
727 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
728 if(!empty($this->ipHostNumber)){
729 $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
730 }
731 $this->dialog->execute();
732 $this->dialog->save_object();
733 $this->dhcpHostEntry = $this->dialog->save();
734 if(count($this->dhcpHostEntry['dhcpOption']) == 0){
735 $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
736 }
737 }
739 /* Write mac address to dhcp settings */
740 if($this->dhcp_is_Account){
741 if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) ||
742 !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
743 $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
744 $this->dhcpHostEntry['MODIFIED'] = TRUE;
745 }
746 }
748 /* Updated IP host number */
749 if($this->dhcp_is_Account){
750 foreach($this->dhcpHostEntry['dhcpStatements'] as $id => $value){
751 if(preg_match("/^fixed-address/",$value)){
752 $this->dhcpHostEntry['dhcpStatements'][$id] = "fixed-address ".$this->ipHostNumber;
753 $this->dhcpHostEntry['MODIFIED'] = TRUE;
754 }
755 }
756 }
758 /* Unset dhcpStatements if this attribute is empty */
759 if(isset($this->dhcpHostEntry['dhcpStatements']) &&
760 ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
761 unset($this->dhcpHostEntry['dhcpStatements']);
762 }
764 /* DHCP removed */
765 if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
766 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
767 if (!$ldap->success()){
768 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_DEL, get_class()));
769 }
771 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
772 $tmp->handle_post_events("remove");
773 }
775 /* DHCP Added */
776 if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
777 $attrs = $this->dhcpHostEntry;
778 unset($attrs['MODIFIED']);
779 unset($attrs['dn']);
780 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
781 $res = $ldap->add($attrs);
783 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
784 $tmp->handle_post_events("add");
786 if (!$ldap->success()){
787 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), "cn=".$this->cn.",".$this->dhcpParentNode, LDAP_ADD, get_class()));
788 }
789 }
791 /* DHCP still activated */
792 if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
794 /* DHCP node changed */
795 if(($this->initial_dhcpParentNode != $this->dhcpParentNode) ||
796 ($this->cn != $this->OrigCn)){
797 $attrs = $this->dhcpHostEntry;
798 $attrs['cn'] = $this->cn;
799 unset($attrs['dn']);
800 unset($attrs['MODIFIED']);
801 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
802 $res = $ldap->add($attrs);
804 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
805 $tmp->handle_post_events("modify");
807 if (!$ldap->success()){
808 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), "cn=".$this->cn.",".$this->dhcpParentNode, LDAP_ADD, get_class()));
809 }
810 if($res){
811 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
812 if (!$ldap->success()){
813 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_DEL, get_class()));
814 }
815 }
816 }
818 /* SAME node but modified */
819 if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1 &&
820 $this->initial_dhcpParentNode == $this->dhcpParentNode){
821 $attrs = $this->dhcpHostEntry;
822 unset($attrs['dn']);
823 unset($attrs['MODIFIED']);
824 $ldap->cd($this->dhcpHostEntry['dn']);
825 $ldap->modify($attrs);
827 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
828 $tmp->handle_post_events("modify");
830 if (!$ldap->success()){
831 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_MOD, get_class()));
832 }
833 }
834 }
835 }
836 $this->dialog = FALSE;
838 /****************/
839 /* DNS HANDLING */
840 /****************/
842 /* If isn't DNS account but initially was DNS account
843 remove all DNS entries
844 */
845 if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
846 return;
847 }elseif($this->acl_is_writeable("dnsSetup")){
849 /* Add ipHostNumber to aRecords
850 */
851 $backup_dnsEntry = $this->dnsEntry;
852 if(!empty($this->ipHostNumber)){
853 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
854 $ptr = $this->get_pTRRecord();
855 if(!empty($ptr)){
856 $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
857 }
858 }
860 /* Create diff and follow instructions
861 * If Account was disabled, remove account by setting exists to false
862 */
863 if((!$this->DNS_is_account)&&($this->initially_was_account)){
864 $this->dnsEntry['exists'] = false;
865 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
866 }else{
867 $this->dnsEntry['exists'] = $this->DNS_is_account;
868 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
869 }
871 /* move follwoing entries
872 */
873 foreach($tmp['move'] as $src => $dst){
874 $this->recursive_move($src,$dst);
875 }
877 /* Delete dns */
878 foreach($tmp['del'] as $dn => $del){
879 $ldap->cd($dn);
880 $ldap->rmdir_recursive($dn);
881 new log("modify","unknown/".get_class($this),$dn,array("*"),$ldap->get_error());
882 }
884 /* Add || Update new DNS entries
885 */
886 foreach($tmp['add'] as $dn => $attrs){
887 $ldap->cd($dn);
888 $ldap->cat($dn, array('dn'));
889 if(count($ldap->fetch())){
890 $ldap->cd($dn);
891 $ldap->modify ($attrs);
892 }else{
893 $ldap->cd($dn);
894 $ldap->add($attrs);
895 }
896 new log("modify","unknown/".get_class($this),$dn,array_keys($attrs),$ldap->get_error());
897 }
900 /* Display errors
901 */
902 if (!$ldap->success()){
903 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, 0, get_class()));
904 }
906 $tmp2 = new servdns($this->config,$this->dn);
907 $tmp2->handle_post_events("modify");
909 $this->dnsEntry = $backup_dnsEntry;
910 }
911 }
913 /* Create html table with all used record types
914 */
915 function generateRecordsList()
916 {
917 $changeStateForRecords = "";
919 if(!$this->DNS_is_account) {
920 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
921 return(array("str" => $str, "changeStateForRecords"=> ""));
922 }
924 $str = "<table summary='' width='100%'>";
925 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
927 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
928 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
929 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
931 $str.=" <tr>".
932 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
933 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
934 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
935 "</tr>";
936 }
938 $str.= " <tr>".
939 " <td colspan=2 width='50%'></td><td>".
940 " <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
941 " </td>".
942 " </tr>".
943 "</table>";
944 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
945 return($ret);
946 }
949 /* Create a html select box which allows us to select different types of records
950 */
951 function generateRecordListBox($selected,$name)
952 {
953 $str = "<select name='".$name."' id='".$name."'>";
954 foreach($this->RecordTypes as $type => $value){
955 $use = "";
956 if($type == $selected){
957 $use = " selected ";
958 }
959 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
960 }
961 $str.="</select>";
962 return($str);
963 }
966 /* Return plugin informations for acl handling */
967 static function plInfo()
968 {
969 $tmp = array(
970 "plShortName" => _("DNS"),
971 "plDescription" => _("DNS settings"),
972 "plSelfModify" => FALSE,
973 "plDepends" => array(),
974 "plPriority" => 5,
975 "plSection" => array("administration"),
976 "plCategory" => array("workstation","terminal","phone","server","component","printer","winworkstation"),
978 "plProvidedAcls"=> array(
979 "ipHostNumber" => _("IP address"),
980 "macAddress" => _("MAC address"))
981 );
983 /* Hide all dns/dhcp configurations if not available
984 */
985 if(class_available("servdns")){
986 $tmp['plProvidedAcls']["dnsSetup"] = _("DNS configuration");
987 }
988 if(class_available("servdhcp")){
989 $tmp['plProvidedAcls']["dhcpSetup"] = _("DHCP configuration");
990 }
991 return($tmp);
992 }
995 function get_dhcp_host_entry()
996 {
997 $attrs = array();
998 $dn = $this->get_dhcp_host_entry_dn();
999 if($dn){
1000 $ldap = $this->config->get_ldap_link();
1001 $ldap->cd($this->config->current['BASE']);
1002 $ldap->cat($dn,array("*"));
1003 if($ldap->count()){
1004 $attrs = $ldap->fetch();
1005 foreach($attrs as $key => $value){
1006 if(is_numeric($key) || ($key == "count")){
1007 unset($attrs[$key]);
1008 }
1009 if(is_array($value) && isset($value['count'])){
1010 unset($attrs[$key]['count']);
1011 }
1012 }
1013 }
1014 }
1015 return($attrs);
1016 }
1019 function get_dhcp_host_entry_dn()
1020 {
1021 $ldap = $this->config->get_ldap_link();
1022 $ldap->cd($this->config->current['BASE']);
1023 $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
1025 if($ldap->count()){
1026 $attr = $ldap->fetch();
1027 return($attr['dn']);
1028 }else{
1029 return("");
1030 }
1031 }
1034 function get_dhcp_parent_node()
1035 {
1036 return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
1037 }
1040 function get_dhcp_parent_nodes()
1041 {
1042 $ldap = $this->config->get_ldap_link();
1043 $ldap->cd($this->config->current['BASE']);
1044 $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
1046 $dhcp_dns = array();
1047 while($attr = $ldap->fetch()){
1048 $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
1049 }
1051 foreach($dhcp_dns as $key => $pri_dns){
1052 $ldap->cat($pri_dns,array("cn"));
1053 $tmp = $ldap->fetch();
1054 if(isset($tmp['cn'][0])){
1055 $dhcp_dns[$key] = $tmp['cn'][0];
1056 }else{
1057 unset($dhcp_dns[$key]);
1058 }
1059 }
1061 $tmp = $tmp2 = array();
1062 foreach($dhcp_dns as $dn => $cn){
1063 $ldap->cd($dn);
1064 $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
1065 "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
1066 while($attr = $ldap->fetch()){
1067 $tmp[$attr['dn']] = $attr['cn'][0];
1068 }
1069 $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.") "));
1070 }
1071 return($tmp2);
1072 }
1075 /* this function returns the default ptr record entry */
1076 function get_pTRRecord()
1077 {
1078 if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
1079 $ldap = $this->config->get_ldap_link();
1080 $ldap->cat(DNS::getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
1081 $attrs = $ldap->fetch();
1082 $tmp = array_flip($this->Zones);
1083 $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
1084 $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
1085 $ptr = preg_replace("/^".normalizePreg(DNS::FlipIp($tmp))."\./","",$this->ipHostNumber);
1086 return($ptr);
1087 }else{
1088 return(FALSE);
1089 }
1090 }
1093 function generateRandomIP($net = "")
1094 {
1095 $str = $net;
1096 $cnt = 4;
1097 while(substr_count($str,".") < 3 && $cnt > 0){
1098 $str .= ".".rand(0,255);
1099 $str = preg_replace("/\.\.*/",".",$str);
1100 $str = trim($str,". ");
1101 $cnt --;
1102 }
1103 return($str);
1104 }
1107 function create_tree($arr,$base,$current = "")
1108 {
1109 $ret = array();
1110 foreach($arr as $r => $name){
1111 $base_part = str_replace($base,"",$r);
1112 if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
1113 $ret[$r] = $current.$name;
1114 $tmp = $this->create_tree($arr,$r,$current.". ");
1115 foreach($tmp as $sub_key => $sub_name){
1116 $ret[$sub_key] = $sub_name;
1117 }
1118 }
1119 }
1120 return($ret);
1121 }
1123 function force_dns()
1124 {
1125 if($this->DNSenabled){
1127 /* Only force DNS account, if we have at least on dns Zone */
1128 if(count($this->Zones)){
1129 $this->DNS_is_account = TRUE;
1130 $this->hide_dns_check_box = TRUE;
1131 }
1132 }
1133 }
1134 }
1136 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1137 ?>