5fcdd76e05a563f4344055ae917d5fc037614eb9
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;
68 var $namingAttr = "cn";
70 /* Terminal dns
71 */
72 function termDNS (&$config, $parent,$objectClasses,$IPisMust = false,$namingAttr = "cn")
73 {
74 /* We need to know which objectClasses are used, to store the ip/mac
75 * Because of different type of devices
76 */
77 $this->parent = $parent;
78 $this->objectclasses = $objectClasses;
79 $this->IPisMust = $IPisMust;
80 $this->namingAttr = $namingAttr;
82 plugin::plugin ($config, $parent->dn, $this->parent);
84 $this->attrs = &$this->parent->attrs;
86 if(isset($this->attrs[$namingAttr][0])){
87 $this->OrigCn = preg_replace("/\\\$\$/","",$this->attrs[$namingAttr][0]);
88 $this->cn = preg_replace("/\\\$\$/","",$this->attrs[$namingAttr][0]);
89 }
91 /* Create list of additional ipHostNumber.
92 */
93 $this->additionalHostNumbers = array();
94 if(isset($this->attrs['ipHostNumber']) && $this->attrs['ipHostNumber']['count'] > 1){
95 for($i = 1 ; $i < $this->attrs['ipHostNumber']['count']; $i ++){
96 $this->additionalHostNumbers[] = $this->attrs['ipHostNumber'][$i];
97 }
98 }
100 /************
101 * DHCP
102 ************/
104 /* Hide all dhcp specific code, if dhcp plugin is not present in config */
105 $this->dhcpEnabled = FALSE;
106 if($this->config->search("servdhcp","class",array("tabs"))){
107 $this->dhcpEnabled = TRUE;
108 }
110 if(!class_available("dhcpHost")){
111 $this->dhcpEnabled = FALSE;
112 }
114 if($this->dhcpEnabled){
115 $this->dhcpParentNodes = $this->get_dhcp_parent_nodes();
116 $this->dhcpParentNode = $this->get_dhcp_parent_node();
117 if($this->dhcpParentNode){
118 $this->dhcp_is_Account = TRUE;
119 $this->initial_dhcp_is_Account = TRUE;
120 $this->dhcpHostEntry = $this->get_dhcp_host_entry();
121 }
122 $this->initial_dhcpHostEntry = $this->dhcpHostEntry;
123 $this->initial_dhcpParentNode= $this->dhcpParentNode;
124 }
127 /************
128 * DNS
129 ************/
131 /* Hide all dns specific code, if dns is not available
132 */
133 $DNSenabled = false;
134 foreach($this->config->data['TABS']['SERVERSERVICE'] as $tab){
135 if(preg_match("/^servdns$/",$tab['CLASS'])){
136 $this->DNSenabled = true;
137 }
138 }
140 if(!class_available("DNS")){
141 $this->DNSenabled = FALSE;
142 }
144 if(!$this->DNSenabled){
145 $this->DNS_is_account = false;
146 return;
147 }
149 if($this->DNSenabled){
151 /* Get Zones
152 */
153 $this->Zones = DNS::getAvailableZones($config);
155 /* Get Entry
156 */
157 $this->dnsEntry = DNS::getDNSHostEntries($config,$this->OrigCn);
159 $ptr = DNS::FlipIp($this->get_pTRRecord());
161 /* Remove A record which equals $this->ipHostNumber
162 */
163 foreach($this->dnsEntry['RECORDS'] as $key => $rec){
164 if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
165 unset($this->dnsEntry['RECORDS'][$key]);
166 }
167 if(($rec['type'] == "pTRRecord") && ($rec['value'] == $ptr)){
168 unset($this->dnsEntry['RECORDS'][$key]);
169 }
170 }
172 /* Get Record types
173 */
174 $this->RecordTypes = DNS::getDnsRecordTypes();
176 /* If there is at least one entry in this -> types, we have DNS enabled
177 */
178 if($this->dnsEntry['exists']){
179 $this->DNS_is_account = true;
180 }else{
181 $this->DNS_is_account = false;
182 }
183 }
185 /* Create a list of used mac and ip addresses.
187 ! We use this optically huge amount of code to fetch all
188 Mac and IP addresses, because a simple search for mac and IP
189 over the whole ldap server was 10 to 20 times slower.
190 */
191 $deps = array();
192 $ou = preg_replace("/,.*$/","",get_ou("systemRDN"));
193 $a_ous = array(get_ou("serverRDN"),
194 get_ou("terminalRDN"),
195 get_ou("workstationRDN"),
196 get_ou("printerRDN"),
197 get_ou("phoneRDN"),
198 get_ou("componentRDN"));
200 $ldap = $this->config->get_ldap_link();
201 $ldap->cd($this->config->current['BASE']);
202 $ldap->search("(&(objectClass=organizationalUnit)(".$ou."))",array("dn"));
203 while($attrs = $ldap->fetch()){
204 foreach($a_ous as $allowed){
205 $deps[] = $allowed.$attrs['dn'];
206 }
207 }
209 foreach($deps as $dep){
210 $ldap->cd($dep);
211 $ldap->search("(|(macAddress=*)(ipHostNumber=*))",array("macAddress","ipHostNumber"));
212 while($attrs = $ldap->fetch()){
213 if(isset($attrs['ipHostNumber'][0])){
214 $this->used_ip_mac["ip:".$attrs['ipHostNumber'][0]] = "ip:".$attrs['ipHostNumber'][0];
215 }
216 if(isset($attrs['macAddress'][0])){
217 $this->used_ip_mac["mac:".$attrs['macAddress'][0]] = "mac:".$attrs['macAddress'][0];
218 }
219 }
220 }
222 /* Save initial ip and mac values, to be able
223 check if the used values are already in use */
224 $this->orig_ipHostNumber = $this->ipHostNumber;
225 $this->orig_macAddress = $this->macAddress;
227 /* Store initally account settings
228 */
229 $this->initially_was_account = $this->DNS_is_account;
231 if($this->DNS_is_account){
232 new log("view","unknown/".get_class($this),$this->dn);
233 }
234 }
237 function netmaskIsCoherent($idZone)
238 {
239 $netmask = DNS::FlipIp(str_replace(".in-addr.arpa.","",DNS::getNameFromMix($idZone)));
240 if(!strstr($this->ipHostNumber, $netmask)){
241 return false;
242 }else{
243 return true;
244 }
245 }
248 function getVarsForSaving($attrs)
249 {
250 foreach($this->attributes as $attr){
251 if(!empty($this->$attr)){
252 $attrs[$attr] = $this->$attr;
253 }
254 }
255 return($attrs);
256 }
258 function execute()
259 {
260 /* Call parent execute */
261 $smarty= get_smarty();
262 $smarty->assign("autonetACL",$this->acl_is_writeable("macAddress") && $this->acl_is_writeable("ipHostNumber")?"rw":"");
263 $smarty->assign("MACisMust",$this->MACisMust);
265 $tmp = $this->plInfo();
266 foreach($tmp['plProvidedAcls'] as $name => $translation){
267 $smarty->assign($name."ACL",$this->getacl($name));
268 }
270 $display= "";
272 /**********
273 * Additional ipHostNumber handling
274 **********/
276 /* Add a new one */
277 if($this->acl_is_writeable("ipHostNumber")){
278 foreach($_POST as $name => $value){
279 if(preg_match("/^additionalHostNumbers_add/",$name)){
280 $this->additionalHostNumbers[] = "";
281 break;
282 }
284 /* Delete given entry */
285 if(preg_match("/^additionalHostNumbers_del_/",$name)){
286 $id = preg_replace("/^^additionalHostNumbers_del_([0-9]*)_.*/","\\1",$name);
287 if(isset($this->additionalHostNumbers[$id])){
288 unset($this->additionalHostNumbers[$id]);
289 $this->additionalHostNumbers = array_values($this->additionalHostNumbers);
290 }
291 break;
292 }
293 }
294 }
296 $smarty->assign("additionalHostNumbers",$this->additionalHostNumbers);
298 /**********
299 * Autonet completion
300 **********/
302 /* Check for autonet button */
303 if (isset($_POST['autonet']) && ($this->acl_is_writeable("ipHostNumber") || $this->acl_is_writeable("macAddress"))){
304 if(class_available("gosaSupportDaemon")){
305 $d= new gosaSupportDaemon(TRUE, 0.5);
306 $res= $d->_send("<xml><header>gosa_network_completition</header>".
307 "<source>GOSA</source><target>GOSA</target><hostname>".$this->cn."</hostname></xml>", TRUE);
308 if (isset($res['XML']['IP']) && $this->acl_is_writeable("ipHostNumber")){
309 $this->ipHostNumber= $res['XML']['IP'];
310 }
311 if (isset($res['XML']['MAC']) && $this->acl_is_writeable("macAddress")){
312 $this->macAddress= $res['XML']['MAC'];
313 }
314 }
315 }
318 /**********
319 * DHCP Handling
320 **********/
322 if(isset($_POST['dhcpEditOptions']) && $this->acl_is_readable("dhcpSetup")){
323 if(count($this->dhcpHostEntry) == 0){
324 $this->dialog = new dhcpHost($this->parent,$this->dhcpParentNode,TRUE);
325 }else{
326 $this->dialog = new dhcpHost($this->parent,$this->dhcpHostEntry,TRUE);
327 }
328 $this->dialog->cn = $this->cn;
329 $this->dialog->read_only = !$this->acl_is_writeable("dhcpSetup");
330 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
331 if(!empty($this->ipHostNumber)){
332 $this->dialog->statements->set('fixed-address', $this->ipHostNumber);
333 }
334 }
336 if(isset($_POST['cancel_dhcp'])){
337 $this->dialog = FALSE;
338 }
340 if(isset($_POST['save_dhcp']) && $this->acl_is_writeable("dhcpSetup") && is_object($this->dialog)){
341 $this->dialog->save_object();
342 $msgs = $this->dialog->check(array());
343 if(count($msgs)){
344 foreach($msgs as $msg){
345 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
346 }
347 }else{
348 $this->dhcpHostEntry = $this->dialog->save();
349 $this->dialog = FALSE;
350 }
351 }
353 if(is_object($this->dialog)){
354 $this->dialog->save_object();
355 return($this->dialog->execute());
356 }
358 $smarty->assign("dhcpEnabled", $this->dhcpEnabled && $this->acl_is_readable("dhcpSetup"));
359 $smarty->assign("dhcp_is_Account",$this->dhcp_is_Account);
360 $smarty->assign("dhcpParentNode", $this->dhcpParentNode);
361 $smarty->assign("dhcpParentNodes",$this->dhcpParentNodes);
362 $smarty->assign("dhcpParentNodeCnt",count($this->dhcpParentNodes));
364 /**********
365 * DNS Handling
366 **********/
368 /* There is no dns available
369 */
370 $smarty->assign("DNS_is_account",$this->DNS_is_account);
371 $smarty->assign("DNSenabled",$this->DNSenabled && $this->acl_is_readable("dnsSetup"));
373 if($this->DNSenabled == false){
375 /* Is IP address must ? */
376 # $smarty->assign("DNS_is_account",false);
377 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
379 /* Assign smarty all non DNs attributes */
380 foreach($this->attributes as $attr){
381 $smarty->assign($attr,$this->$attr);
382 }
384 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
385 }else{
386 # $smarty->assign("DNS_is_account",true);
388 /* Add new empty array to our record list */
389 if(isset($_POST['AddNewRecord']) && $this->acl_is_writeable("dnsSetup")){
390 $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>"");
391 }
393 /* propose_ip */
394 if(isset($_POST['propose_ip']) && $this->acl_is_writeable("ipHostNumber")){
395 foreach($this->Zones as $key => $name){
396 if($name == $this->dnsEntry['zoneName']){
397 $net = DNS::FlipIp(str_replace(".in-addr.arpa.","",DNS::getNameFromMix($key)));
398 $this->ipHostNumber = $this->generateRandomIP($net);
399 }
400 }
401 }
403 /* Handle all posts */
404 $only_once =true;
405 if($this->acl_is_writeable("dnsSetup")){
406 foreach($_POST as $name => $value){
408 /* Check if we have to delete a record entry */
409 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
411 /* Avoid performing this once again */
412 $only_once = false;
414 /* Extract id for specified entry */
415 $id = preg_replace("/RemoveRecord_/","",$name);
416 $id = preg_replace("/_.*$/","",$id);
418 /* Delete this record, mark edited entries to be able to delete them */
419 if(isset($this->dnsEntry['RECORDS'][$id])){
420 unset($this->dnsEntry['RECORDS'][$id]);
421 }
422 }
423 }
424 }
425 /* Assign smarty all non DNs attributes */
426 foreach($this->attributes as $attr){
427 $smarty->assign($attr,$this->$attr);
428 }
430 /* Assign smarty all DNS attributes */
431 foreach($this->DNSattributes as $attr){
432 $smarty->assign($attr,htmlentities($this->dnsEntry[$attr]));
433 }
435 /* Assign all needed vars */
436 # $smarty->assign("DNSAccount",$this->DNS_is_account);
437 $smarty->assign("hide_dns_check_box",$this->hide_dns_check_box);
439 $smarty->assign("Zones",$this->Zones);
440 $smarty->assign("ZoneCnt",count($this->Zones));
441 $smarty->assign("ZoneKeys",($this->Zones));
442 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
444 /* Create zone array */
445 $idZones = array();
446 foreach($this->Zones as $id => $zone){
447 if($this->netmaskIsCoherent($id)) {
448 $idZones[$id] = $zone;
449 }else{
450 $idZones[$id] = $zone." ("._("Not matching").")";
451 }
452 }
453 $smarty->assign("Zones",$idZones);
454 $smarty->assign("ZoneKeys", $this->Zones);
456 $tmp = $this->generateRecordsList();
458 $changeStateForRecords = $tmp['changeStateForRecords'];
460 $smarty->assign("records",$tmp['str']);
461 $smarty->assign("changeStateForRecords",$changeStateForRecords);
463 $smarty->assign("autonetACL",$this->acl_is_writeable("macAddress") && $this->acl_is_writeable("ipHostNumber")?"rw":"");
465 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
466 }
468 return($display);
469 }
472 function remove_from_parent()
473 {
475 $ldap = $this->config->get_ldap_link();
477 /************
478 * DNS Handling
479 ************/
481 if($this->DNS_is_account){
484 $tmp = array();
485 $this->dnsEntry['exists'] = false;
486 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
488 /* Delete dns */
489 foreach($tmp['del'] as $dn => $del){
490 $ldap->cd($dn);
491 $ldap->rmdir_recursive($dn);
492 new log("remove","unknown/".get_class($this),$dn);
493 if (!$ldap->success()){
494 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_DEL, get_class()));
495 }
496 }
497 }
500 /************
501 * DHCP Handling
502 ************/
504 /* DHCP removed */
505 if($this->dhcp_is_Account){
506 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
507 if (!$ldap->success()){
508 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_DEL, get_class()));
509 }
510 }
511 }
514 /* Save data to object */
515 function save_object()
516 {
518 if(isset($_POST['network_tpl_posted'])){
520 /* Save all posted vars */
521 plugin::save_object();
523 /******
524 Additional IP Host Numbers
525 ******/
527 /* Get posts for all additionally added ipHostNumbers */
528 if($this->acl_is_writeable("ipHostNumber")){
529 foreach($this->additionalHostNumbers as $id => $value){
530 if(isset($_POST['additionalHostNumbers_'.$id])){
531 $this->additionalHostNumbers[$id] = get_post('additionalHostNumbers_'.$id);
532 }
533 }
534 }
537 /******
538 DHCP posts
539 ******/
541 if($this->acl_is_writeable("dhcpSetup") && $this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
542 foreach($this->dhcpAttributes as $attr){
543 if(isset($_POST[$attr])){
544 $this->$attr = $_POST[$attr];
545 }
546 }
547 if(isset($_POST['dhcp_is_Account'])){
548 $this->dhcp_is_Account = TRUE;
549 }else{
550 $this->dhcp_is_Account = FALSE;
551 }
552 }
554 /* Ge all non dns attributes (IP/MAC)*/
555 foreach($this->attributes as $attr){
556 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
557 $this->$attr = $_POST[$attr];
558 }
559 }
562 /******
563 DNS posts
564 ******/
566 /* Check if DNS should be enabled / disabled
567 * -skip this, if the dns account is enforced.
568 */
569 if(!$this->hide_dns_check_box){
570 if($this->DNS_is_account && $this->acl_is_writeable("dnsSetup") && !isset($_POST['DNS_is_account'])){
571 $this->DNS_is_account = false;
572 }elseif(!$this->DNS_is_account && $this->acl_is_writeable("dnsSetup") && isset($_POST['DNS_is_account'])){
573 $this->DNS_is_account = true;
574 }
575 }
577 /* Get dns attributes */
578 if(($this->DNSenabled) && (isset($_POST['network_tpl_posted'])) && $this->acl_is_writeable("dnsSetup")){
580 /* Check for posted record changes */
581 if(is_array($this->dnsEntry['RECORDS'])){
582 foreach($this->dnsEntry['RECORDS'] as $key => $value){
584 /* Check if type has changed */
585 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
586 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
587 }
588 /* Check if value has changed */
589 if(isset($_POST['RecordValue_'.$key])){
590 $this->dnsEntry['RECORDS'][$key]['value'] = get_post('RecordValue_'.$key);
591 }
592 }
593 }
595 /* Get all basic DNS attributes (TTL, Clas ..)*/
596 foreach($this->DNSattributes as $attr){
597 if(isset($_POST[$attr])){
598 $this->dnsEntry[$attr] = get_post($attr);
599 }
600 }
601 }
602 if($this->hide_dns_check_box){
603 $this->DNS_is_account = true;
604 }
605 }
606 }
609 /* Check supplied data */
610 function check()
611 {
612 /* Call common method to give check the hook */
613 $message= plugin::check();
615 /******
616 check additional IP Host Numbers
617 ******/
619 foreach($this->additionalHostNumbers as $id => $value){
620 if(!tests::is_ip($value)){
621 $message[]= msgPool::invalid(sprintf(_("IP address %s"),($id +2)), "", "", "192.168.1.10");
622 }
623 }
626 /* Check if mac and ip are already used */
627 if(!empty($this->ipHostNumber) && $this->DNS_is_account &&
628 $this->ipHostNumber != $this->orig_ipHostNumber &&
629 in_array_strict("ip:".$this->ipHostNumber,$this->used_ip_mac)){
630 $message[]= msgPool::duplicated(_("IP address"));
631 }
632 if(!empty($this->macAddress) && $this->dhcp_is_Account &&
633 $this->macAddress != $this->orig_macAddress &&
634 in_array_strict("mac:".$this->macAddress,$this->used_ip_mac)){
635 $message[]= msgPool::duplicated(_("MAC address"));
636 }
638 /* Check if ip must be given
639 */
640 if(($this->IPisMust)||($this->DNS_is_account)){
641 if (empty($this->ipHostNumber)){
642 $message[]= msgPool::required(_("IP address"));
643 }elseif (!tests::is_ip($this->ipHostNumber)){
644 $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.10");
645 }
646 }
648 /* Check if mac is empty
649 */
650 if($this->MACisMust || $this->dhcp_is_Account){
651 if ($this->macAddress == "" ){
652 $message[]= msgPool::required(_("MAC address"));
653 }elseif(!tests::is_mac($this->macAddress)){
654 $message[]= msgPool::invalid(_("MAC address"), "", "", "00:0C:7F:31:33:F1");
655 }
656 }
658 /* only perfrom this checks if this is a valid DNS account */
659 if($this->DNS_is_account){
661 $checkArray = array();
662 $onlyOnce = array();
664 // $onlyOnce['cNAMERecord'] = 0;
665 $tmp = array_flip($this->Zones);
666 $tmp2 = $tmp[$this->dnsEntry['zoneName']];
667 if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
668 $tmp2 = preg_replace("/^.*\//","",$tmp2);
669 $message[] =sprintf(_("The IP address '%s' is not part of the selected reverse zone '%s'!"),$this->ipHostNumber,$tmp2);
670 }
672 /* Walk through all entries and detect duplicates or mismatches
673 */
674 foreach($this->dnsEntry['RECORDS'] as $name => $values){
676 /* Count record values, to detect duplicate entries for a specific record
677 */
678 if(!isset($checkArray[$values['type']][$values['value']])){
679 $checkArray[$values['type']][$values['value']] = 0;
680 }else{
681 $message[] = sprintf(_("Record type '%s' is duplicated!"),$values['type']);
682 }
684 /* Check if given entries in $onlyOnce are used more than once
685 */
686 if(isset($onlyOnce[$values['type']])){
687 $onlyOnce[$values['type']] ++;
688 if($onlyOnce[$values['type']] > 1){
689 $message[] = sprintf(_("Uniq record type '%s' is duplicated!"),$values['type']);
690 }
691 }
693 /* Skip txt record ...
694 */
695 if($values['type'] == "tXTRecord") continue;
697 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
698 */
699 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
700 #TODO: Where's the problem here?
701 $message[]=sprintf(_("The IP address '%s' will be added as 'A Record', this will be done automatically, please remove the record."),
702 $this->ipHostNumber);
703 }
705 /* only lower-case is allowed in record entries ...
706 */
707 if($values['value'] != strtolower($values['value'])){
708 #TODO: What's in values['value']? Something for a propper message?
709 $message[] = sprintf(_("Only lowercase records are allowed, please check your '%ss'."),$values['type']);
710 }
711 }
712 }
713 return ($message);
714 }
717 /* Save to LDAP */
718 function save()
719 {
720 $ldap= $this->config->get_ldap_link();
722 $dn = $this->parent->dn;
724 /*******************/
725 /* IP-MAC HANDLING */
726 /*******************/
728 /* $dn was posted as parameter */
729 $this->dn = $dn;
731 /* Save DNS setting & ip/Mac*/
732 plugin::save();
734 /* Add all additional ipHostNumbers now
735 */
736 if(!empty($this->ipHostNumber)){
737 $this->attrs['ipHostNumber'] = array($this->ipHostNumber);
738 }
739 foreach($this->additionalHostNumbers as $value){
740 $this->attrs['ipHostNumber'][] = $value;
741 }
743 /* Do not add the objectClass ipHost if no ip address is given */
744 if(!isset($this->attrs['ipHostNumber'])){
745 $this->attrs['objectClass'] = array_remove_entries(array("ipHost"),$this->attrs['objectClass']);
746 }
748 /* Write back to ldap */
749 $ldap->cd($this->dn);
750 $this->cleanup();
751 $ldap->modify ($this->attrs);
753 /****************/
754 /* DHCP HANDLING */
755 /****************/
757 /* New entry */
758 if($this->dhcpEnabled && $this->acl_is_writeable("dhcpSetup")) {
760 if(count($this->dhcpHostEntry) == 0){
761 $this->dialog = new dhcpHost($this->parent,$this->dhcpParentNode,TRUE);
762 $this->dialog->cn = $this->cn;
763 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
764 if(!empty($this->ipHostNumber)){
765 $this->dialog->statements->set('fixed-address', $this->ipHostNumber);
766 }
767 $this->dialog->execute();
768 $this->dialog->save_object();
769 $this->dhcpHostEntry = $this->dialog->save();
770 if(count($this->dhcpHostEntry['dhcpOption']) == 0){
771 $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
772 }
773 }else{
775 // Updated Host-Name entry
776 foreach($this->dhcpHostEntry['dhcpOption'] as $key => $entry){
777 if(preg_match("/^host-name/", $entry)){
778 $this->dhcpHostEntry['dhcpOption'][$key] = "host-name ".$this->cn;
779 break;
780 }
781 }
782 }
784 /* Write mac address to dhcp settings */
785 if($this->dhcp_is_Account){
786 if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) ||
787 !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
788 $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
789 $this->dhcpHostEntry['MODIFIED'] = TRUE;
790 }
791 }
793 /* Updated IP host number */
794 if($this->dhcp_is_Account && $this->dhcpHostEntry['dhcpStatements']){
795 foreach($this->dhcpHostEntry['dhcpStatements'] as $id => $value){
796 if(preg_match("/^fixed-address/",$value)){
797 $this->dhcpHostEntry['dhcpStatements'][$id] = "fixed-address ".$this->ipHostNumber;
798 $this->dhcpHostEntry['MODIFIED'] = TRUE;
799 }
800 }
801 }
803 /* Unset dhcpStatements if this attribute is empty */
804 if(isset($this->dhcpHostEntry['dhcpStatements']) &&
805 ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
806 unset($this->dhcpHostEntry['dhcpStatements']);
807 }
809 /* DHCP removed */
810 if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
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 }
816 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
817 $tmp->handle_post_events("remove");
818 }
820 /* DHCP Added */
821 if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
822 $attrs = $this->dhcpHostEntry;
823 unset($attrs['MODIFIED']);
824 unset($attrs['dn']);
825 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
826 $res = $ldap->add($attrs);
828 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
829 $tmp->handle_post_events("add");
831 if (!$ldap->success()){
832 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), "cn=".$this->cn.",".$this->dhcpParentNode, LDAP_ADD, get_class()));
833 }
834 }
836 /* DHCP still activated */
837 if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
839 /* DHCP node changed */
840 if(($this->initial_dhcpParentNode != $this->dhcpParentNode) ||
841 ($this->cn != $this->OrigCn)){
842 $attrs = $this->dhcpHostEntry;
843 $attrs[$this->namingAttr] = $this->cn;
844 unset($attrs['dn']);
845 unset($attrs['MODIFIED']);
846 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
847 $res = $ldap->add($attrs);
849 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
850 $tmp->handle_post_events("modify");
852 if (!$ldap->success()){
853 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), "cn=".$this->cn.",".$this->dhcpParentNode, LDAP_ADD, get_class()));
854 }
855 if($res){
856 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
857 if (!$ldap->success()){
858 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_DEL, get_class()));
859 }
860 }
861 }
863 /* SAME node but modified */
864 if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1 &&
865 $this->initial_dhcpParentNode == $this->dhcpParentNode){
866 $attrs = $this->dhcpHostEntry;
867 unset($attrs['dn']);
868 unset($attrs['MODIFIED']);
869 $ldap->cd($this->dhcpHostEntry['dn']);
870 $ldap->modify($attrs);
872 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
873 $tmp->handle_post_events("modify");
875 if (!$ldap->success()){
876 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_MOD, get_class()));
877 }
878 }
879 }
880 }
881 $this->dialog = FALSE;
883 /****************/
884 /* DNS HANDLING */
885 /****************/
887 /* If isn't DNS account but initially was DNS account
888 remove all DNS entries
889 */
890 if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
891 return;
892 }elseif($this->acl_is_writeable("dnsSetup")){
894 /* Add ipHostNumber to aRecords
895 */
896 $backup_dnsEntry = $this->dnsEntry;
897 if(!empty($this->ipHostNumber)){
898 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
899 $ptr = $this->get_pTRRecord();
900 if(!empty($ptr)){
901 $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
902 }
903 }
905 /* Create diff and follow instructions
906 * If Account was disabled, remove account by setting exists to false
907 */
908 if((!$this->DNS_is_account)&&($this->initially_was_account)){
909 $this->dnsEntry['exists'] = false;
910 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
911 }else{
912 $this->dnsEntry['exists'] = $this->DNS_is_account;
913 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
914 }
916 /* Update SOA record if needed
917 */
918 $this->updateSOARecord();
920 /* move follwoing entries
921 */
922 foreach($tmp['move'] as $src => $dst){
923 $this->recursive_move($src,$dst);
924 }
926 /* Delete dns */
927 foreach($tmp['del'] as $dn => $del){
928 $ldap->cd($dn);
929 $ldap->rmdir_recursive($dn);
930 new log("modify","unknown/".get_class($this),$dn,array("*"),$ldap->get_error());
931 }
933 /* Add || Update new DNS entries
934 */
935 foreach($tmp['add'] as $dn => $attrs){
936 $ldap->cd($dn);
937 $ldap->cat($dn, array('dn'));
938 if(count($ldap->fetch())){
939 $ldap->cd($dn);
940 $ldap->modify ($attrs);
941 }else{
942 $ldap->cd($dn);
943 $ldap->add($attrs);
944 }
945 new log("modify","unknown/".get_class($this),$dn,array_keys($attrs),$ldap->get_error());
946 }
949 /* Display errors
950 */
951 if (!$ldap->success()){
952 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, 0, get_class()));
953 }
955 $tmp2 = new servdns($this->config,$this->dn);
956 $tmp2->handle_post_events("modify");
958 $this->dnsEntry = $backup_dnsEntry;
959 }
960 }
962 /* Create html table with all used record types
963 */
964 function generateRecordsList()
965 {
966 $changeStateForRecords = "";
967 $str = "<table summary='' width='100%'>";
968 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
970 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
971 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
972 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
974 $str.=" <tr>".
975 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
976 " <td><input type='text' value=\"".htmlentities($entry['value'])."\" name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
977 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
978 "</tr>";
979 }
981 $str.= " <tr>".
982 " <td colspan=2 width='50%'></td><td>".
983 " <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
984 " </td>".
985 " </tr>".
986 "</table>";
987 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
989 return($ret);
990 }
993 /* Create a html select box which allows us to select different types of records
994 */
995 function generateRecordListBox($selected,$name)
996 {
997 $str = "<select name='".$name."' id='".$name."'>";
998 foreach($this->RecordTypes as $type => $value){
999 $use = "";
1000 if($type == $selected){
1001 $use = " selected ";
1002 }
1003 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
1004 }
1005 $str.="</select>";
1006 return($str);
1007 }
1010 /* Return plugin informations for acl handling */
1011 static function plInfo()
1012 {
1013 $tmp = array(
1014 "plShortName" => _("DNS"),
1015 "plDescription" => _("DNS settings"),
1016 "plSelfModify" => FALSE,
1017 "plDepends" => array(),
1018 "plPriority" => 5,
1019 "plSection" => array("administration"),
1020 "plCategory" => array("workstation","terminal","phone","server","component","printer","winworkstation"),
1022 "plProvidedAcls"=> array(
1023 "ipHostNumber" => _("IP address"),
1024 "macAddress" => _("MAC address"))
1025 );
1027 /* Hide all dns/dhcp configurations if not available
1028 */
1029 if(class_available("servdns")){
1030 $tmp['plProvidedAcls']["dnsSetup"] = _("DNS configuration");
1031 }
1032 if(class_available("servdhcp")){
1033 $tmp['plProvidedAcls']["dhcpSetup"] = _("DHCP configuration");
1034 }
1035 return($tmp);
1036 }
1039 function get_dhcp_host_entry()
1040 {
1041 $attrs = array();
1042 $dn = $this->get_dhcp_host_entry_dn();
1043 if($dn){
1044 $ldap = $this->config->get_ldap_link();
1045 $ldap->cd($this->config->current['BASE']);
1046 $ldap->cat($dn,array("*"));
1047 if($ldap->count()){
1048 $attrs = $ldap->fetch();
1049 foreach($attrs as $key => $value){
1050 if(is_numeric($key) || ($key == "count")){
1051 unset($attrs[$key]);
1052 }
1053 if(is_array($value) && isset($value['count'])){
1054 unset($attrs[$key]['count']);
1055 }
1056 }
1057 }
1058 }
1059 return($attrs);
1060 }
1063 function get_dhcp_host_entry_dn()
1064 {
1065 $ldap = $this->config->get_ldap_link();
1066 $ldap->cd($this->config->current['BASE']);
1067 $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("uid","cn","dn"));
1069 if($ldap->count()){
1070 $attr = $ldap->fetch();
1071 return($attr['dn']);
1072 }else{
1073 return("");
1074 }
1075 }
1078 function get_dhcp_parent_node()
1079 {
1080 return(preg_replace("/^cn=".preg_quote($this->cn, '/').",/","",$this->get_dhcp_host_entry_dn()));
1081 }
1084 function get_dhcp_parent_nodes()
1085 {
1086 $ldap = $this->config->get_ldap_link();
1087 $ldap->cd($this->config->current['BASE']);
1088 $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
1090 $dhcp_dns = array();
1091 while($attr = $ldap->fetch()){
1092 $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
1093 }
1095 foreach($dhcp_dns as $key => $pri_dns){
1096 $ldap->cat($pri_dns,array("cn"));
1097 $tmp = $ldap->fetch();
1098 if(isset($tmp['cn'][0])){
1099 $dhcp_dns[$key] = $tmp['cn'][0];
1100 }else{
1101 unset($dhcp_dns[$key]);
1102 }
1103 }
1105 $tmp = $tmp2 = array();
1106 foreach($dhcp_dns as $dn => $cn){
1107 $ldap->cd($dn);
1108 $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
1109 "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
1110 while($attr = $ldap->fetch()){
1111 $tmp[$attr['dn']] = $attr['cn'][0];
1112 }
1113 $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.") "));
1114 }
1115 return($tmp2);
1116 }
1119 /* this function returns the default ptr record entry */
1120 function get_pTRRecord()
1121 {
1122 if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
1123 $ldap = $this->config->get_ldap_link();
1124 $ldap->cat(DNS::getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
1125 $attrs = $ldap->fetch();
1126 $tmp = array_flip($this->Zones);
1127 $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
1128 $tmp = trim(preg_replace("/\.in-addr.arpa\.$/","",$tmp));
1129 $ptr = preg_replace("/^".preg_quote(DNS::FlipIp($tmp), '/')."\./","",$this->ipHostNumber);
1130 return($ptr);
1131 }else{
1132 return(FALSE);
1133 }
1134 }
1137 function generateRandomIP($net = "")
1138 {
1139 $str = $net;
1140 $cnt = 4;
1142 // first gather all IPs
1143 $ldap = $this->config->get_ldap_link();
1144 $ocs =
1145 "(objectClass=goFonHardware)".
1146 "(objectClass=goServer)".
1147 "(objectClass=GOhard)".
1148 "(objectClass=gotoTerminal)".
1149 "(objectClass=gotoWorkstation)".
1150 "(objectClass=gotoPrinter)".
1151 "(objectClass=ipHost)";
1152 $list = array();
1153 $ldap->search("(&(|{$ocs})(ipHostNumber=*))",array("ipHostNumber"));
1154 while($attrs = $ldap->fetch()){
1155 if (preg_match("/^$net\./", $attrs['ipHostNumber'][0])) {
1156 $list[] = $attrs['ipHostNumber'][0];
1157 }
1158 }
1160 // Set starting ip.
1161 $ip_data = preg_split("/\./",$net);
1162 for($i=0;$i<4;$i++){
1163 if(!isset($ip_data[$i])) $ip_data[$i] = 0;
1164 }
1166 // Search the next free and valid ip.
1167 while(in_array_strict(implode(".",$ip_data),$list) || $ip_data[3] <= 1){
1168 $ip_data[3] ++ ;
1169 if($ip_data[3] > 255){
1170 $ip_data[3] = 1 ;
1171 $ip_data[2] ++ ;
1172 }
1173 if($ip_data[2] > 255){
1174 $ip_data[2] = 1 ;
1175 $ip_data[1] ++ ;
1176 }
1177 if($ip_data[1] > 255){
1178 $ip_data[1] = 1 ;
1179 $ip_data[0] ++ ;
1180 }
1181 if($ip_data[0] > 255) break;
1182 }
1184 return(implode(".",$ip_data));
1185 }
1188 function create_tree($arr,$base,$current = "")
1189 {
1190 $ret = array();
1191 foreach($arr as $r => $name){
1192 $base_part = str_replace($base,"",$r);
1193 if(preg_match("/^[a-z]*=".preg_quote($name, '/')."(|,)$/i",$base_part)){
1194 $ret[$r] = $current.$name;
1195 $tmp = $this->create_tree($arr,$r,$current.". ");
1196 foreach($tmp as $sub_key => $sub_name){
1197 $ret[$sub_key] = $sub_name;
1198 }
1199 }
1200 }
1201 return($ret);
1202 }
1204 function force_dns()
1205 {
1206 if($this->DNSenabled){
1208 /* Only force DNS account, if we have at least on dns Zone */
1209 if(count($this->Zones)){
1210 $this->DNS_is_account = TRUE;
1211 $this->hide_dns_check_box = TRUE;
1212 }
1213 }
1214 }
1216 function updateSOARecord()
1217 {
1218 $oldEntries = DNS::getDNSHostEntries($this->config,$this->OrigCn);
1219 $newEntries = $this->dnsEntry;
1220 if(array_differs($newEntries['RECORDS'],$oldEntries['RECORDS'])){
1221 DNS::touchDNSZone($this->config,$this->dnsEntry['zoneName']);
1222 }
1223 }
1224 }
1226 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1227 ?>