410b34b3d4fafa40ac0275acc4ac9f480e706278
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);
84 if(isset($this->attrs[$namingAttr][0])){
85 $this->OrigCn = preg_replace("/\\\$\$/","",$this->attrs[$namingAttr][0]);
86 $this->cn = preg_replace("/\\\$\$/","",$this->attrs[$namingAttr][0]);
87 }
89 /* Create list of additional ipHostNumber.
90 */
91 $this->additionalHostNumbers = array();
92 if(isset($this->attrs['ipHostNumber']) && $this->attrs['ipHostNumber']['count'] > 1){
93 for($i = 1 ; $i < $this->attrs['ipHostNumber']['count']; $i ++){
94 $this->additionalHostNumbers[] = $this->attrs['ipHostNumber'][$i];
95 }
96 }
98 /************
99 * DHCP
100 ************/
102 /* Hide all dhcp specific code, if dhcp plugin is not present in config */
103 $this->dhcpEnabled = FALSE;
104 if($this->config->search("servdhcp","class",array("tabs"))){
105 $this->dhcpEnabled = TRUE;
106 }
108 if(!class_available("dhcpHost")){
109 $this->dhcpEnabled = FALSE;
110 }
112 if($this->dhcpEnabled){
113 $this->dhcpParentNodes = $this->get_dhcp_parent_nodes();
114 $this->dhcpParentNode = $this->get_dhcp_parent_node();
115 if($this->dhcpParentNode){
116 $this->dhcp_is_Account = TRUE;
117 $this->initial_dhcp_is_Account = TRUE;
118 $this->dhcpHostEntry = $this->get_dhcp_host_entry();
119 }
120 $this->initial_dhcpHostEntry = $this->dhcpHostEntry;
121 $this->initial_dhcpParentNode= $this->dhcpParentNode;
122 }
125 /************
126 * DNS
127 ************/
129 /* Hide all dns specific code, if dns is not available
130 */
131 $DNSenabled = false;
132 foreach($this->config->data['TABS']['SERVERSERVICE'] as $tab){
133 if(preg_match("/^servdns$/",$tab['CLASS'])){
134 $this->DNSenabled = true;
135 }
136 }
138 if(!class_available("DNS")){
139 $this->DNSenabled = FALSE;
140 }
142 if(!$this->DNSenabled){
143 $this->DNS_is_account = false;
144 return;
145 }
147 if($this->DNSenabled){
149 /* Get Zones
150 */
151 $this->Zones = DNS::getAvailableZones($config);
153 /* Get Entry
154 */
155 $this->dnsEntry = DNS::getDNSHostEntries($config,$this->OrigCn);
157 /* Remove A record which equals $this->ipHostNumber
158 */
159 $ptr = $this->get_pTRRecord();
160 foreach($this->dnsEntry['RECORDS'] as $key => $rec){
161 if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
162 unset($this->dnsEntry['RECORDS'][$key]);
163 }
164 if(($rec['type'] == "pTRRecord") && ($rec['value'] == $ptr)){
165 unset($this->dnsEntry['RECORDS'][$key]);
166 }
167 }
169 /* Get Record types
170 */
171 $this->RecordTypes = DNS::getDnsRecordTypes();
173 /* If there is at least one entry in this -> types, we have DNS enabled
174 */
175 if($this->dnsEntry['exists']){
176 $this->DNS_is_account = true;
177 }else{
178 $this->DNS_is_account = false;
179 }
180 }
182 /* Create a list of used mac and ip addresses.
184 ! We use this optically huge amount of code to fetch all
185 Mac and IP addresses, because a simple search for mac and IP
186 over the whole ldap server was 10 to 20 times slower.
187 */
188 $deps = array();
189 $ou = preg_replace("/,.*$/","",get_ou("systemRDN"));
190 $a_ous = array(get_ou("serverRDN"),
191 get_ou("terminalRDN"),
192 get_ou("workstationRDN"),
193 get_ou("printerRDN"),
194 get_ou("phoneRDN"),
195 get_ou("componentRDN"));
197 $ldap = $this->config->get_ldap_link();
198 $ldap->cd($this->config->current['BASE']);
199 $ldap->search("(&(objectClass=organizationalUnit)(".$ou."))",array("dn"));
200 while($attrs = $ldap->fetch()){
201 foreach($a_ous as $allowed){
202 $deps[] = $allowed.$attrs['dn'];
203 }
204 }
206 foreach($deps as $dep){
207 $ldap->cd($dep);
208 $ldap->search("(|(macAddress=*)(ipHostNumber=*))",array("macAddress","ipHostNumber"));
209 while($attrs = $ldap->fetch()){
210 if(isset($attrs['ipHostNumber'][0])){
211 $this->used_ip_mac["ip:".$attrs['ipHostNumber'][0]] = "ip:".$attrs['ipHostNumber'][0];
212 }
213 if(isset($attrs['macAddress'][0])){
214 $this->used_ip_mac["mac:".$attrs['macAddress'][0]] = "mac:".$attrs['macAddress'][0];
215 }
216 }
217 }
219 /* Save initial ip and mac values, to be able
220 check if the used values are already in use */
221 $this->orig_ipHostNumber = $this->ipHostNumber;
222 $this->orig_macAddress = $this->macAddress;
224 /* Store initally account settings
225 */
226 $this->initially_was_account = $this->DNS_is_account;
228 if($this->DNS_is_account){
229 new log("view","unknown/".get_class($this),$this->dn);
230 }
231 }
234 function netmaskIsCoherent($idZone)
235 {
236 $netmask = DNS::FlipIp(str_replace(".in-addr.arpa","",DNS::getNameFromMix($idZone)));
237 if(!strstr($this->ipHostNumber, $netmask)){
238 return false;
239 }else{
240 return true;
241 }
242 }
245 function getVarsForSaving($attrs)
246 {
247 foreach($this->attributes as $attr){
248 if(!empty($this->$attr)){
249 $attrs[$attr] = $this->$attr;
250 }
251 }
252 return($attrs);
253 }
255 function execute()
256 {
257 /* Call parent execute */
258 $smarty= get_smarty();
259 $smarty->assign("autonetACL",$this->acl_is_writeable("macAddress").$this->acl_is_writeable("ipHostNumber"));
261 $tmp = $this->plInfo();
262 foreach($tmp['plProvidedAcls'] as $name => $translation){
263 $smarty->assign($name."ACL",$this->getacl($name));
264 }
266 $display= "";
268 /**********
269 * Additional ipHostNumber handling
270 **********/
272 /* Add a new one */
273 if($this->acl_is_writeable("ipHostNumber")){
274 foreach($_POST as $name => $value){
275 if(preg_match("/^additionalHostNumbers_add/",$name)){
276 $this->additionalHostNumbers[] = "";
277 break;
278 }
280 /* Delete given entry */
281 if(preg_match("/^additionalHostNumbers_del_/",$name)){
282 $id = preg_replace("/^^additionalHostNumbers_del_([0-9]*)_.*/","\\1",$name);
283 if(isset($this->additionalHostNumbers[$id])){
284 unset($this->additionalHostNumbers[$id]);
285 $this->additionalHostNumbers = array_values($this->additionalHostNumbers);
286 }
287 break;
288 }
289 }
290 }
292 $smarty->assign("additionalHostNumbers",$this->additionalHostNumbers);
293 $smarty->assign("staticAddress", "");
295 /**********
296 * Autonet completion
297 **********/
299 /* Check for autonet button */
300 if (isset($_POST['autonet']) && ($this->acl_is_writeable("ipHostNumber") || $this->acl_is_writeable("macAddress"))){
301 $d= new gosaSupportDaemon(TRUE, 0.5);
302 $res= $d->_send("<xml><header>gosa_network_completition</header>".
303 "<source>GOSA</source><target>GOSA</target><hostname>".$this->cn."</hostname></xml>", TRUE);
304 if (isset($res['XML']['IP']) && $this->acl_is_writeable("ipHostNumber")){
305 $this->ipHostNumber= $res['XML']['IP'];
306 }
307 if (isset($res['XML']['MAC']) && $this->acl_is_writeable("macAddress")){
308 $this->macAddress= $res['XML']['MAC'];
309 }
310 }
313 /**********
314 * DHCP Handling
315 **********/
317 if(isset($_POST['dhcpEditOptions']) && $this->acl_is_readable("dhcpSetup")){
318 if(count($this->dhcpHostEntry) == 0){
319 $this->dialog = new dhcpHost($this->parent,$this->dhcpParentNode,TRUE);
320 }else{
321 $this->dialog = new dhcpHost($this->parent,$this->dhcpHostEntry,TRUE);
322 }
323 $this->dialog->cn = $this->cn;
324 $this->dialog->read_only = !$this->acl_is_writeable("dhcpSetup");
325 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
326 if(!empty($this->ipHostNumber)){
327 $this->dialog->statements->set('fixed-address', $this->ipHostNumber);
328 }
329 }
331 if(isset($_POST['cancel_dhcp'])){
332 $this->dialog = FALSE;
333 }
335 if(isset($_POST['save_dhcp']) && $this->acl_is_writeable("dhcpSetup") && is_object($this->dialog)){
336 $this->dialog->save_object();
337 $msgs = $this->dialog->check(array());
338 if(count($msgs)){
339 foreach($msgs as $msg){
340 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
341 }
342 }else{
343 $this->dhcpHostEntry = $this->dialog->save();
344 $this->dialog = FALSE;
345 }
346 }
348 if(is_object($this->dialog)){
349 $this->dialog->save_object();
350 return($this->dialog->execute());
351 }
353 $smarty->assign("dhcpEnabled", $this->dhcpEnabled && $this->acl_is_readable("dhcpSetup"));
354 $smarty->assign("dhcp_is_Account",$this->dhcp_is_Account);
355 $smarty->assign("dhcpParentNode", $this->dhcpParentNode);
356 $smarty->assign("dhcpParentNodes",$this->dhcpParentNodes);
357 $smarty->assign("dhcpParentNodeCnt",count($this->dhcpParentNodes));
359 /**********
360 * DNS Handling
361 **********/
363 /* There is no dns available
364 */
365 $smarty->assign("DNS_is_account",$this->DNS_is_account);
366 $smarty->assign("DNSenabled",$this->DNSenabled && $this->acl_is_readable("dnsSetup"));
368 if($this->DNSenabled == false){
370 /* Is IP address must ? */
371 # $smarty->assign("DNS_is_account",false);
372 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
374 /* Assign smarty all non DNs attributes */
375 foreach($this->attributes as $attr){
376 $smarty->assign($attr,$this->$attr);
377 }
378 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
380 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
381 }else{
382 # $smarty->assign("DNS_is_account",true);
384 /* Add new empty array to our record list */
385 if(isset($_POST['AddNewRecord']) && $this->acl_is_writeable("dnsSetup")){
386 $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>"");
387 }
389 /* propose_ip */
390 if(isset($_POST['propose_ip']) && $this->acl_is_writeable("ipHostNumber")){
391 foreach($this->Zones as $key => $name){
392 if($name == $this->dnsEntry['zoneName']){
393 $net = DNS::FlipIp(str_replace(".in-addr.arpa","",DNS::getNameFromMix($key)));
394 $this->ipHostNumber = $this->generateRandomIP($net);
395 }
396 }
397 }
399 /* Handle all posts */
400 $only_once =true;
401 if($this->acl_is_writeable("dnsSetup")){
402 foreach($_POST as $name => $value){
404 /* Check if we have to delete a record entry */
405 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
407 /* Avoid performing this once again */
408 $only_once = false;
410 /* Extract id for specified entry */
411 $id = preg_replace("/RemoveRecord_/","",$name);
412 $id = preg_replace("/_.*$/","",$id);
414 /* Delete this record, mark edited entries to be able to delete them */
415 if(isset($this->dnsEntry['RECORDS'][$id])){
416 unset($this->dnsEntry['RECORDS'][$id]);
417 }
418 }
419 }
420 }
421 /* Assign smarty all non DNs attributes */
422 foreach($this->attributes as $attr){
423 $smarty->assign($attr,$this->$attr);
424 }
426 /* Assign smarty all DNS attributes */
427 foreach($this->DNSattributes as $attr){
428 $smarty->assign($attr,htmlentities($this->dnsEntry[$attr]));
429 }
431 /* Assign all needed vars */
432 # $smarty->assign("DNSAccount",$this->DNS_is_account);
433 $smarty->assign("hide_dns_check_box",$this->hide_dns_check_box);
435 $smarty->assign("Zones",$this->Zones);
436 $smarty->assign("ZoneCnt",count($this->Zones));
437 $smarty->assign("ZoneKeys",($this->Zones));
438 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
440 /* Create zone array */
441 $idZones = array();
442 foreach($this->Zones as $id => $zone){
443 if($this->netmaskIsCoherent($id)) {
444 $idZones[$id] = $zone;
445 }else{
446 $idZones[$id] = $zone." ("._("Not matching").")";
447 }
448 }
449 $smarty->assign("Zones",$idZones);
450 $smarty->assign("ZoneKeys", $this->Zones);
452 $tmp = $this->generateRecordsList();
454 $changeStateForRecords = $tmp['changeStateForRecords'];
456 $smarty->assign("records",$tmp['str']);
457 $smarty->assign("changeStateForRecords",$changeStateForRecords);
458 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
460 $smarty->assign("autonetACL",$this->acl_is_writeable("macAddress").$this->acl_is_writeable("ipHostNumber"));
462 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
463 }
465 return($display);
466 }
469 function remove_from_parent()
470 {
471 if($this->DNS_is_account){
473 $ldap = $this->config->get_ldap_link();
475 $tmp = array();
476 $this->dnsEntry['exists'] = false;
477 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
479 /* Delete dns */
480 foreach($tmp['del'] as $dn => $del){
481 $ldap->cd($dn);
482 $ldap->rmdir_recursive($dn);
483 new log("remove","unknown/".get_class($this),$dn);
484 if (!$ldap->success()){
485 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_DEL, get_class()));
486 }
487 }
488 }
489 }
492 /* Save data to object */
493 function save_object()
494 {
496 if(isset($_POST['network_tpl_posted'])){
498 /* Save all posted vars */
499 plugin::save_object();
501 /******
502 Additional IP Host Numbers
503 ******/
505 /* Get posts for all additionally added ipHostNumbers */
506 if($this->acl_is_writeable("ipHostNumber")){
507 foreach($this->additionalHostNumbers as $id => $value){
508 if(isset($_POST['additionalHostNumbers_'.$id])){
509 $this->additionalHostNumbers[$id] = get_post('additionalHostNumbers_'.$id);
510 }
511 }
512 }
515 /******
516 DHCP posts
517 ******/
519 if($this->acl_is_writeable("dhcpSetup") && $this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
520 foreach($this->dhcpAttributes as $attr){
521 if(isset($_POST[$attr])){
522 $this->$attr = $_POST[$attr];
523 }
524 }
525 if(isset($_POST['dhcp_is_Account'])){
526 $this->dhcp_is_Account = TRUE;
527 }else{
528 $this->dhcp_is_Account = FALSE;
529 }
530 }
532 /* Ge all non dns attributes (IP/MAC)*/
533 foreach($this->attributes as $attr){
534 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
535 $this->$attr = $_POST[$attr];
536 }
537 }
540 /******
541 DNS posts
542 ******/
544 /* Check if DNS should be enabled / disabled
545 * -skip this, if the dns account is enforced.
546 */
547 if(!$this->hide_dns_check_box){
548 if($this->DNS_is_account && $this->acl_is_writeable("dnsSetup") && !isset($_POST['DNS_is_account'])){
549 $this->DNS_is_account = false;
550 }elseif(!$this->DNS_is_account && $this->acl_is_writeable("dnsSetup") && isset($_POST['DNS_is_account'])){
551 $this->DNS_is_account = true;
552 }
553 }
555 /* Get dns attributes */
556 if(($this->DNSenabled) && (isset($_POST['network_tpl_posted'])) && $this->acl_is_writeable("dnsSetup")){
558 /* Check for posted record changes */
559 if(is_array($this->dnsEntry['RECORDS'])){
560 foreach($this->dnsEntry['RECORDS'] as $key => $value){
562 /* Check if type has changed */
563 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
564 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
565 }
566 /* Check if value has changed */
567 if(isset($_POST['RecordValue_'.$key])){
568 $this->dnsEntry['RECORDS'][$key]['value'] = get_post('RecordValue_'.$key);
569 }
570 }
571 }
573 /* Get all basic DNS attributes (TTL, Clas ..)*/
574 foreach($this->DNSattributes as $attr){
575 if(isset($_POST[$attr])){
576 $this->dnsEntry[$attr] = get_post($attr);
577 }
578 }
579 }
580 if($this->hide_dns_check_box){
581 $this->DNS_is_account = true;
582 }
583 }
584 }
587 /* Check supplied data */
588 function check()
589 {
590 /* Call common method to give check the hook */
591 $message= plugin::check();
593 /******
594 check additional IP Host Numbers
595 ******/
597 foreach($this->additionalHostNumbers as $id => $value){
598 if(!tests::is_ip($value)){
599 $message[]= msgPool::invalid(sprintf(_("IP address %s"),($id +2)), "", "", "192.168.1.10");
600 }
601 }
604 /* Check if mac and ip are already used */
605 if(!empty($this->ipHostNumber) && $this->DNS_is_account &&
606 $this->ipHostNumber != $this->orig_ipHostNumber &&
607 in_array("ip:".$this->ipHostNumber,$this->used_ip_mac)){
608 $message[]= msgPool::duplicated(_("IP address"));
609 }
610 if(!empty($this->macAddress) && $this->dhcp_is_Account &&
611 $this->macAddress != $this->orig_macAddress &&
612 in_array("mac:".$this->macAddress,$this->used_ip_mac)){
613 $message[]= msgPool::duplicated(_("MAC address"));
614 }
616 /* Check if ip must be given
617 */
618 if(($this->IPisMust)||($this->DNS_is_account)){
619 if (empty($this->ipHostNumber)){
620 $message[]= msgPool::required(_("IP address"));
621 }elseif (!tests::is_ip($this->ipHostNumber)){
622 $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.10");
623 }
624 }
626 /* Check if mac is empty
627 */
628 if($this->MACisMust || $this->dhcp_is_Account){
629 if ($this->macAddress == "" ){
630 $message[]= msgPool::required(_("MAC address"));
631 }elseif(!tests::is_mac($this->macAddress)){
632 $message[]= msgPool::invalid(_("MAC address"), "", "", "00:0C:7F:31:33:F1");
633 }
634 }
636 /* only perfrom this checks if this is a valid DNS account */
637 if($this->DNS_is_account){
639 $checkArray = array();
640 $onlyOnce = array();
642 // $onlyOnce['cNAMERecord'] = 0;
643 $tmp = array_flip($this->Zones);
644 $tmp2 = $tmp[$this->dnsEntry['zoneName']];
645 if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
646 $tmp2 = preg_replace("/^.*\//","",$tmp2);
647 $message[] =sprintf(_("The IP address '%s' is not part of the selected reverse zone '%s'!"),$this->ipHostNumber,$tmp2);
648 }
650 /* Walk through all entries and detect duplicates or mismatches
651 */
652 foreach($this->dnsEntry['RECORDS'] as $name => $values){
654 /* Count record values, to detect duplicate entries for a specific record
655 */
656 if(!isset($checkArray[$values['type']][$values['value']])){
657 $checkArray[$values['type']][$values['value']] = 0;
658 }else{
659 $message[] = sprintf(_("Record type '%s' is duplicated!"),$values['type']);
660 }
662 /* Check if given entries in $onlyOnce are used more than once
663 */
664 if(isset($onlyOnce[$values['type']])){
665 $onlyOnce[$values['type']] ++;
666 if($onlyOnce[$values['type']] > 1){
667 $message[] = sprintf(_("Uniq record type '%s' is duplicated!"),$values['type']);
668 }
669 }
671 /* Skip txt record ...
672 */
673 if($values['type'] == "tXTRecord") continue;
675 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
676 */
677 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
678 #TODO: Where's the problem here?
679 $message[]=sprintf(_("The IP address '%s' will be added as 'A Record', this will be done automatically, please remove the record."),
680 $this->ipHostNumber);
681 }
683 /* only lower-case is allowed in record entries ...
684 */
685 if($values['value'] != strtolower($values['value'])){
686 #TODO: What's in values['value']? Something for a propper message?
687 $message[] = sprintf(_("Only lowercase records are allowed, please check your '%ss'."),$values['type']);
688 }
689 }
690 }
691 return ($message);
692 }
695 /* Save to LDAP */
696 function save()
697 {
698 $ldap= $this->config->get_ldap_link();
700 $dn = $this->parent->dn;
702 /*******************/
703 /* IP-MAC HANDLING */
704 /*******************/
706 /* $dn was posted as parameter */
707 $this->dn = $dn;
709 /* Save DNS setting & ip/Mac*/
710 plugin::save();
712 /* Add all additional ipHostNumbers now
713 */
714 if(!empty($this->ipHostNumber)){
715 $this->attrs['ipHostNumber'] = array($this->ipHostNumber);
716 }
717 foreach($this->additionalHostNumbers as $value){
718 $this->attrs['ipHostNumber'][] = $value;
719 }
721 /* Do not add the objectClass ipHost if no ip address is given */
722 if(!$this->attrs['ipHostNumber']){
723 $this->attrs['objectClass'] = array_remove_entries(array("ipHost"),$this->attrs['objectClass']);
724 }
726 /* Write back to ldap */
727 $ldap->cd($this->dn);
728 $this->cleanup();
729 $ldap->modify ($this->attrs);
731 /****************/
732 /* DHCP HANDLING */
733 /****************/
735 /* New entry */
736 if($this->dhcpEnabled && $this->acl_is_writeable("dhcpSetup")) {
738 if(count($this->dhcpHostEntry) == 0){
739 $this->dialog = new dhcpHost($this->parent,$this->dhcpParentNode,TRUE);
740 $this->dialog->cn = $this->cn;
741 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
742 if(!empty($this->ipHostNumber)){
743 $this->dialog->statements->set('fixed-address', $this->ipHostNumber);
744 }
745 $this->dialog->execute();
746 $this->dialog->save_object();
747 $this->dhcpHostEntry = $this->dialog->save();
748 if(count($this->dhcpHostEntry['dhcpOption']) == 0){
749 $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
750 }
751 }
753 /* Write mac address to dhcp settings */
754 if($this->dhcp_is_Account){
755 if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) ||
756 !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
757 $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
758 $this->dhcpHostEntry['MODIFIED'] = TRUE;
759 }
760 }
762 /* Updated IP host number */
763 if($this->dhcp_is_Account && $this->dhcpHostEntry['dhcpStatements']){
764 foreach($this->dhcpHostEntry['dhcpStatements'] as $id => $value){
765 if(preg_match("/^fixed-address/",$value)){
766 $this->dhcpHostEntry['dhcpStatements'][$id] = "fixed-address ".$this->ipHostNumber;
767 $this->dhcpHostEntry['MODIFIED'] = TRUE;
768 }
769 }
770 }
772 /* Unset dhcpStatements if this attribute is empty */
773 if(isset($this->dhcpHostEntry['dhcpStatements']) &&
774 ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
775 unset($this->dhcpHostEntry['dhcpStatements']);
776 }
778 /* DHCP removed */
779 if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
780 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
781 if (!$ldap->success()){
782 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_DEL, get_class()));
783 }
785 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
786 $tmp->handle_post_events("remove");
787 }
789 /* DHCP Added */
790 if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
791 $attrs = $this->dhcpHostEntry;
792 unset($attrs['MODIFIED']);
793 unset($attrs['dn']);
794 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
795 $res = $ldap->add($attrs);
797 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
798 $tmp->handle_post_events("add");
800 if (!$ldap->success()){
801 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), "cn=".$this->cn.",".$this->dhcpParentNode, LDAP_ADD, get_class()));
802 }
803 }
805 /* DHCP still activated */
806 if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
808 /* DHCP node changed */
809 if(($this->initial_dhcpParentNode != $this->dhcpParentNode) ||
810 ($this->cn != $this->OrigCn)){
811 $attrs = $this->dhcpHostEntry;
812 $attrs[$this->namingAttr] = $this->cn;
813 unset($attrs['dn']);
814 unset($attrs['MODIFIED']);
815 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
816 $res = $ldap->add($attrs);
818 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
819 $tmp->handle_post_events("modify");
821 if (!$ldap->success()){
822 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), "cn=".$this->cn.",".$this->dhcpParentNode, LDAP_ADD, get_class()));
823 }
824 if($res){
825 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
826 if (!$ldap->success()){
827 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_DEL, get_class()));
828 }
829 }
830 }
832 /* SAME node but modified */
833 if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1 &&
834 $this->initial_dhcpParentNode == $this->dhcpParentNode){
835 $attrs = $this->dhcpHostEntry;
836 unset($attrs['dn']);
837 unset($attrs['MODIFIED']);
838 $ldap->cd($this->dhcpHostEntry['dn']);
839 $ldap->modify($attrs);
841 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
842 $tmp->handle_post_events("modify");
844 if (!$ldap->success()){
845 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_MOD, get_class()));
846 }
847 }
848 }
849 }
850 $this->dialog = FALSE;
852 /****************/
853 /* DNS HANDLING */
854 /****************/
856 /* If isn't DNS account but initially was DNS account
857 remove all DNS entries
858 */
859 if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
860 return;
861 }elseif($this->acl_is_writeable("dnsSetup")){
863 /* Add ipHostNumber to aRecords
864 */
865 $backup_dnsEntry = $this->dnsEntry;
866 if(!empty($this->ipHostNumber)){
867 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
868 $ptr = $this->get_pTRRecord();
869 if(!empty($ptr)){
870 $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
871 }
872 }
874 /* Create diff and follow instructions
875 * If Account was disabled, remove account by setting exists to false
876 */
877 if((!$this->DNS_is_account)&&($this->initially_was_account)){
878 $this->dnsEntry['exists'] = false;
879 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
880 }else{
881 $this->dnsEntry['exists'] = $this->DNS_is_account;
882 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
883 }
885 /* move follwoing entries
886 */
887 foreach($tmp['move'] as $src => $dst){
888 $this->recursive_move($src,$dst);
889 }
891 /* Delete dns */
892 foreach($tmp['del'] as $dn => $del){
893 $ldap->cd($dn);
894 $ldap->rmdir_recursive($dn);
895 new log("modify","unknown/".get_class($this),$dn,array("*"),$ldap->get_error());
896 }
898 /* Add || Update new DNS entries
899 */
900 foreach($tmp['add'] as $dn => $attrs){
901 $ldap->cd($dn);
902 $ldap->cat($dn, array('dn'));
903 if(count($ldap->fetch())){
904 $ldap->cd($dn);
905 $ldap->modify ($attrs);
906 }else{
907 $ldap->cd($dn);
908 $ldap->add($attrs);
909 }
910 new log("modify","unknown/".get_class($this),$dn,array_keys($attrs),$ldap->get_error());
911 }
914 /* Display errors
915 */
916 if (!$ldap->success()){
917 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, 0, get_class()));
918 }
920 $tmp2 = new servdns($this->config,$this->dn);
921 $tmp2->handle_post_events("modify");
923 $this->dnsEntry = $backup_dnsEntry;
924 }
925 }
927 /* Create html table with all used record types
928 */
929 function generateRecordsList()
930 {
931 $changeStateForRecords = "";
933 if(!$this->DNS_is_account) {
934 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
935 return(array("str" => $str, "changeStateForRecords"=> ""));
936 }
938 $str = "<table summary='' width='100%'>";
939 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
941 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
942 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
943 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
945 $str.=" <tr>".
946 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
947 " <td><input type='text' value=\"".htmlentities($entry['value'])."\" name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
948 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
949 "</tr>";
950 }
952 $str.= " <tr>".
953 " <td colspan=2 width='50%'></td><td>".
954 " <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
955 " </td>".
956 " </tr>".
957 "</table>";
958 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
960 return($ret);
961 }
964 /* Create a html select box which allows us to select different types of records
965 */
966 function generateRecordListBox($selected,$name)
967 {
968 $str = "<select name='".$name."' id='".$name."'>";
969 foreach($this->RecordTypes as $type => $value){
970 $use = "";
971 if($type == $selected){
972 $use = " selected ";
973 }
974 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
975 }
976 $str.="</select>";
977 return($str);
978 }
981 /* Return plugin informations for acl handling */
982 static function plInfo()
983 {
984 $tmp = array(
985 "plShortName" => _("DNS"),
986 "plDescription" => _("DNS settings"),
987 "plSelfModify" => FALSE,
988 "plDepends" => array(),
989 "plPriority" => 5,
990 "plSection" => array("administration"),
991 "plCategory" => array("workstation","terminal","phone","server","component","printer","winworkstation"),
993 "plProvidedAcls"=> array(
994 "ipHostNumber" => _("IP address"),
995 "macAddress" => _("MAC address"))
996 );
998 /* Hide all dns/dhcp configurations if not available
999 */
1000 if(class_available("servdns")){
1001 $tmp['plProvidedAcls']["dnsSetup"] = _("DNS configuration");
1002 }
1003 if(class_available("servdhcp")){
1004 $tmp['plProvidedAcls']["dhcpSetup"] = _("DHCP configuration");
1005 }
1006 return($tmp);
1007 }
1010 function get_dhcp_host_entry()
1011 {
1012 $attrs = array();
1013 $dn = $this->get_dhcp_host_entry_dn();
1014 if($dn){
1015 $ldap = $this->config->get_ldap_link();
1016 $ldap->cd($this->config->current['BASE']);
1017 $ldap->cat($dn,array("*"));
1018 if($ldap->count()){
1019 $attrs = $ldap->fetch();
1020 foreach($attrs as $key => $value){
1021 if(is_numeric($key) || ($key == "count")){
1022 unset($attrs[$key]);
1023 }
1024 if(is_array($value) && isset($value['count'])){
1025 unset($attrs[$key]['count']);
1026 }
1027 }
1028 }
1029 }
1030 return($attrs);
1031 }
1034 function get_dhcp_host_entry_dn()
1035 {
1036 $ldap = $this->config->get_ldap_link();
1037 $ldap->cd($this->config->current['BASE']);
1038 $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("uid","cn","dn"));
1040 if($ldap->count()){
1041 $attr = $ldap->fetch();
1042 return($attr['dn']);
1043 }else{
1044 return("");
1045 }
1046 }
1049 function get_dhcp_parent_node()
1050 {
1051 return(preg_replace("/^cn=".preg_quote($this->cn, '/').",/","",$this->get_dhcp_host_entry_dn()));
1052 }
1055 function get_dhcp_parent_nodes()
1056 {
1057 $ldap = $this->config->get_ldap_link();
1058 $ldap->cd($this->config->current['BASE']);
1059 $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
1061 $dhcp_dns = array();
1062 while($attr = $ldap->fetch()){
1063 $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
1064 }
1066 foreach($dhcp_dns as $key => $pri_dns){
1067 $ldap->cat($pri_dns,array("cn"));
1068 $tmp = $ldap->fetch();
1069 if(isset($tmp['cn'][0])){
1070 $dhcp_dns[$key] = $tmp['cn'][0];
1071 }else{
1072 unset($dhcp_dns[$key]);
1073 }
1074 }
1076 $tmp = $tmp2 = array();
1077 foreach($dhcp_dns as $dn => $cn){
1078 $ldap->cd($dn);
1079 $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
1080 "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
1081 while($attr = $ldap->fetch()){
1082 $tmp[$attr['dn']] = $attr['cn'][0];
1083 }
1084 $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.") "));
1085 }
1086 return($tmp2);
1087 }
1090 /* this function returns the default ptr record entry */
1091 function get_pTRRecord()
1092 {
1093 if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
1094 $ldap = $this->config->get_ldap_link();
1095 $ldap->cat(DNS::getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
1096 $attrs = $ldap->fetch();
1097 $tmp = array_flip($this->Zones);
1098 $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
1099 $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
1100 $ptr = preg_replace("/^".preg_quote(DNS::FlipIp($tmp), '/')."\./","",$this->ipHostNumber);
1101 return($ptr);
1102 }else{
1103 return(FALSE);
1104 }
1105 }
1108 function generateRandomIP($net = "")
1109 {
1110 $str = $net;
1111 $cnt = 4;
1113 // first gather all IPs
1114 $ldap = $this->config->get_ldap_link();
1115 $ocs =
1116 "(objectClass=goFonHardware)".
1117 "(objectClass=goServer)".
1118 "(objectClass=GOhard)".
1119 "(objectClass=gotoTerminal)".
1120 "(objectClass=gotoWorkstation)".
1121 "(objectClass=gotoPrinter)".
1122 "(objectClass=ipHost)";
1123 $list = array();
1124 $ldap->search("(&(|{$ocs})(ipHostNumber={$net}*))",array("ipHostNumber"));
1125 while($attrs = $ldap->fetch()){
1126 $list[] = $attrs['ipHostNumber'][0];
1127 }
1129 // Set starting ip.
1130 $ip_data = preg_split("/\./",$net);
1131 for($i=0;$i<4;$i++){
1132 if(!isset($ip_data[$i])) $ip_data[$i] = 0;
1133 }
1135 // Search the next free and valid ip.
1136 while(in_array(implode(".",$ip_data),$list) || $ip_data[3] <= 1){
1137 $ip_data[3] ++ ;
1138 if($ip_data[3] > 255){
1139 $ip_data[3] = 1 ;
1140 $ip_data[2] ++ ;
1141 }
1142 if($ip_data[2] > 255){
1143 $ip_data[2] = 1 ;
1144 $ip_data[1] ++ ;
1145 }
1146 if($ip_data[1] > 255){
1147 $ip_data[1] = 1 ;
1148 $ip_data[0] ++ ;
1149 }
1150 if($ip_data[0] > 255) break;
1151 }
1153 return(implode(".",$ip_data));
1154 }
1157 function create_tree($arr,$base,$current = "")
1158 {
1159 $ret = array();
1160 foreach($arr as $r => $name){
1161 $base_part = str_replace($base,"",$r);
1162 if(preg_match("/^[a-z]*=".preg_quote($name, '/')."(|,)$/i",$base_part)){
1163 $ret[$r] = $current.$name;
1164 $tmp = $this->create_tree($arr,$r,$current.". ");
1165 foreach($tmp as $sub_key => $sub_name){
1166 $ret[$sub_key] = $sub_name;
1167 }
1168 }
1169 }
1170 return($ret);
1171 }
1173 function force_dns()
1174 {
1175 if($this->DNSenabled){
1177 /* Only force DNS account, if we have at least on dns Zone */
1178 if(count($this->Zones)){
1179 $this->DNS_is_account = TRUE;
1180 $this->hide_dns_check_box = TRUE;
1181 }
1182 }
1183 }
1184 }
1186 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1187 ?>