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