06cb3ada7f8cd9242a017dd163edbc679aca2787
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 }
599 if (!tests::is_ip($this->ipHostNumber)){
600 $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.10");
601 }
602 }
604 /* Check if mac is empty
605 */
606 if ($this->macAddress == "" ){
607 $message[]= msgPool::required(_("IP address"));
608 }
609 if(!tests::is_mac($this->macAddress)){
610 $message[]= msgPool::invalid(_("MAC address"), "", "", "00:0C:7F:31:33:F1");
611 }
613 /* only perfrom this checks if this is a valid DNS account */
614 if($this->DNS_is_account){
616 $checkArray = array();
617 $onlyOnce = array();
619 // $onlyOnce['cNAMERecord'] = 0;
620 $tmp = array_flip($this->Zones);
621 $tmp2 = $tmp[$this->dnsEntry['zoneName']];
622 if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
623 $tmp2 = preg_replace("/^.*\//","",$tmp2);
624 $message[] =sprintf(_("The IP address '%s' is not part of the selected reverse zone '%s'!"),$this->ipHostNumber,$tmp2);
625 }
627 /* Walk through all entries and detect duplicates or mismatches
628 */
629 foreach($this->dnsEntry['RECORDS'] as $name => $values){
631 /* Count record values, to detect duplicate entries for a specific record
632 */
633 if(!isset($checkArray[$values['type']][$values['value']])){
634 $checkArray[$values['type']][$values['value']] = 0;
635 }else{
636 $message[] = sprintf(_("Record type '%s' is duplicated!"),$values['type']);
637 }
639 /* Check if given entries in $onlyOnce are used more than once
640 */
641 if(isset($onlyOnce[$values['type']])){
642 $onlyOnce[$values['type']] ++;
643 if($onlyOnce[$values['type']] > 1){
644 $message[] = sprintf(_("Uniq record type '%s' is duplicated!"),$values['type']);
645 }
646 }
648 /* Skip txt record ...
649 */
650 if($values['type'] == "tXTRecord") continue;
652 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
653 */
654 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
655 #TODO: Where's the problem here?
656 $message[]=sprintf(_("The IP address '%s' will be added as 'A Record', this will be done automatically, please remove the record."),
657 $this->ipHostNumber);
658 }
660 /* only lower-case is allowed in record entries ...
661 */
662 if($values['value'] != strtolower($values['value'])){
663 #TODO: What's in values['value']? Something for a propper message?
664 $message[] = sprintf(_("Only lowercase records are allowed, please check your '%ss'."),$values['type']);
665 }
666 }
667 }
668 return ($message);
669 }
672 /* Save to LDAP */
673 function save()
674 {
675 $ldap= $this->config->get_ldap_link();
677 $dn = $this->parent->dn;
679 /*******************/
680 /* IP-MAC HANDLING */
681 /*******************/
683 /* $dn was posted as parameter */
684 $this->dn = $dn;
686 /* Save DNS setting & ip/Mac*/
687 plugin::save();
689 /* Add all additional ipHostNumbers now
690 */
691 $this->attrs['ipHostNumber'] = array($this->ipHostNumber);
692 foreach($this->additionalHostNumbers as $value){
693 $this->attrs['ipHostNumber'][] = $value;
694 }
696 /* Write back to ldap */
697 $ldap->cd($this->dn);
698 $this->cleanup();
699 $ldap->modify ($this->attrs);
701 /****************/
702 /* DHCP HANDLING */
703 /****************/
705 /* New entry */
706 if($this->dhcpEnabled){
708 if(count($this->dhcpHostEntry) == 0){
709 $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
710 $this->dialog->cn = $this->cn;
711 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
712 if(!empty($this->ipHostNumber)){
713 $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
714 }
715 $this->dialog->execute();
716 $this->dialog->save_object();
717 $this->dhcpHostEntry = $this->dialog->save();
718 if(count($this->dhcpHostEntry['dhcpOption']) == 0){
719 $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
720 }
721 }
723 /* Write mac address to dhcp settings */
724 if($this->dhcp_is_Account){
725 if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) ||
726 !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
727 $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
728 $this->dhcpHostEntry['MODIFIED'] = TRUE;
729 }
730 }
732 /* Updated IP host number */
733 if($this->dhcp_is_Account){
734 foreach($this->dhcpHostEntry['dhcpStatements'] as $id => $value){
735 if(preg_match("/^fixed-address/",$value)){
736 $this->dhcpHostEntry['dhcpStatements'][$id] = "fixed-address ".$this->ipHostNumber;
737 $this->dhcpHostEntry['MODIFIED'] = TRUE;
738 }
739 }
740 }
742 /* Unset dhcpStatements if this attribute is empty */
743 if(isset($this->dhcpHostEntry['dhcpStatements']) &&
744 ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
745 unset($this->dhcpHostEntry['dhcpStatements']);
746 }
748 /* DHCP removed */
749 if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
750 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
751 if (!$ldap->success()){
752 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_DEL, get_class()));
753 }
755 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
756 $tmp->handle_post_events("remove");
757 }
759 /* DHCP Added */
760 if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
761 $attrs = $this->dhcpHostEntry;
762 unset($attrs['MODIFIED']);
763 unset($attrs['dn']);
764 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
765 $res = $ldap->add($attrs);
767 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
768 $tmp->handle_post_events("add");
770 if (!$ldap->success()){
771 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), "cn=".$this->cn.",".$this->dhcpParentNode, LDAP_ADD, get_class()));
772 }
773 }
775 /* DHCP still activated */
776 if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
778 /* DHCP node changed */
779 if(($this->initial_dhcpParentNode != $this->dhcpParentNode) ||
780 ($this->cn != $this->OrigCn)){
781 $attrs = $this->dhcpHostEntry;
782 $attrs['cn'] = $this->cn;
783 unset($attrs['dn']);
784 unset($attrs['MODIFIED']);
785 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
786 $res = $ldap->add($attrs);
788 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
789 $tmp->handle_post_events("modify");
791 if (!$ldap->success()){
792 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), "cn=".$this->cn.",".$this->dhcpParentNode, LDAP_ADD, get_class()));
793 }
794 if($res){
795 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
796 if (!$ldap->success()){
797 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_DEL, get_class()));
798 }
799 }
800 }
802 /* SAME node but modified */
803 if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1 &&
804 $this->initial_dhcpParentNode == $this->dhcpParentNode){
805 $attrs = $this->dhcpHostEntry;
806 unset($attrs['dn']);
807 unset($attrs['MODIFIED']);
808 $ldap->cd($this->dhcpHostEntry['dn']);
809 $ldap->modify($attrs);
811 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
812 $tmp->handle_post_events("modify");
814 if (!$ldap->success()){
815 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_MOD, get_class()));
816 }
817 }
818 }
819 }
820 $this->dialog = FALSE;
822 /****************/
823 /* DNS HANDLING */
824 /****************/
826 /* If isn't DNS account but initially was DNS account
827 remove all DNS entries
828 */
829 if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
830 return;
831 }else{
833 /* Add ipHostNumber to aRecords
834 */
835 $backup_dnsEntry = $this->dnsEntry;
836 if(!empty($this->ipHostNumber)){
837 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
838 $ptr = $this->get_pTRRecord();
839 if(!empty($ptr)){
840 $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
841 }
842 }
844 /* Create diff and follow instructions
845 * If Account was disabled, remove account by setting exists to false
846 */
847 if((!$this->DNS_is_account)&&($this->initially_was_account)){
848 $this->dnsEntry['exists'] = false;
849 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
850 }else{
851 $this->dnsEntry['exists'] = $this->DNS_is_account;
852 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
853 }
855 /* move follwoing entries
856 */
857 foreach($tmp['move'] as $src => $dst){
858 $this->recursive_move($src,$dst);
859 }
861 /* Delete dns */
862 foreach($tmp['del'] as $dn => $del){
863 $ldap->cd($dn);
864 $ldap->rmdir_recursive($dn);
865 new log("modify","unknown/".get_class($this),$dn,array("*"),$ldap->get_error());
866 }
868 /* Add || Update new DNS entries
869 */
870 foreach($tmp['add'] as $dn => $attrs){
871 $ldap->cd($dn);
872 $ldap->cat($dn, array('dn'));
873 if(count($ldap->fetch())){
874 $ldap->cd($dn);
875 $ldap->modify ($attrs);
876 }else{
877 $ldap->cd($dn);
878 $ldap->add($attrs);
879 }
880 new log("modify","unknown/".get_class($this),$dn,array_keys($attrs),$ldap->get_error());
881 }
884 /* Display errors
885 */
886 if (!$ldap->success()){
887 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, 0, get_class()));
888 }
890 $tmp2 = new servdns($this->config,$this->dn);
891 $tmp2->handle_post_events("modify");
893 $this->dnsEntry = $backup_dnsEntry;
894 }
895 }
897 /* Create html table with all used record types
898 */
899 function generateRecordsList()
900 {
901 $changeStateForRecords = "";
903 if(!$this->DNS_is_account) {
904 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
905 return(array("str" => $str, "changeStateForRecords"=> ""));
906 }
908 $str = "<table summary='' width='100%'>";
909 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
911 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
912 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
913 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
915 $str.=" <tr>".
916 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
917 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
918 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
919 "</tr>";
920 }
922 $str.= " <tr>".
923 " <td colspan=2 width='50%'></td><td>".
924 " <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
925 " </td>".
926 " </tr>".
927 "</table>";
928 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
929 return($ret);
930 }
933 /* Create a html select box which allows us to select different types of records
934 */
935 function generateRecordListBox($selected,$name)
936 {
937 $str = "<select name='".$name."' id='".$name."'>";
938 foreach($this->RecordTypes as $type => $value){
939 $use = "";
940 if($type == $selected){
941 $use = " selected ";
942 }
943 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
944 }
945 $str.="</select>";
946 return($str);
947 }
950 /* Return plugin informations for acl handling */
951 static function plInfo()
952 {
953 $tmp = array(
954 "plShortName" => _("DNS"),
955 "plDescription" => _("DNS settings"),
956 "plSelfModify" => FALSE,
957 "plDepends" => array(),
958 "plPriority" => 5,
959 "plSection" => array("administration"),
960 "plCategory" => array("workstation","terminal","phone","server","component","printer","winworkstation"),
962 "plProvidedAcls"=> array(
963 "ipHostNumber" => _("IP address"),
964 "macAddress" => _("MAC address"))
965 );
967 /* Hide all dns specific code, if dns is not available
968 */
969 $config = session::get('config');
970 foreach($config->data['TABS']['SERVERSERVICE'] as $tab){
971 if(preg_match("/^servdns$/",$tab['CLASS'])){
972 $tmp['plProvidedAcls']["Records"] = _("DNS records");
973 $tmp['plProvidedAcls']["zoneName"] = _("Zone name");
974 $tmp['plProvidedAcls']["dNSTTL"] = _("TTL");
975 break;
976 }
977 }
978 return($tmp);
979 }
982 function get_dhcp_host_entry()
983 {
984 $attrs = array();
985 $dn = $this->get_dhcp_host_entry_dn();
986 if($dn){
987 $ldap = $this->config->get_ldap_link();
988 $ldap->cd($this->config->current['BASE']);
989 $ldap->cat($dn,array("*"));
990 if($ldap->count()){
991 $attrs = $ldap->fetch();
992 foreach($attrs as $key => $value){
993 if(is_numeric($key) || ($key == "count")){
994 unset($attrs[$key]);
995 }
996 if(is_array($value) && isset($value['count'])){
997 unset($attrs[$key]['count']);
998 }
999 }
1000 }
1001 }
1002 return($attrs);
1003 }
1006 function get_dhcp_host_entry_dn()
1007 {
1008 $ldap = $this->config->get_ldap_link();
1009 $ldap->cd($this->config->current['BASE']);
1010 $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
1012 if($ldap->count()){
1013 $attr = $ldap->fetch();
1014 return($attr['dn']);
1015 }else{
1016 return("");
1017 }
1018 }
1021 function get_dhcp_parent_node()
1022 {
1023 return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
1024 }
1027 function get_dhcp_parent_nodes()
1028 {
1029 $ldap = $this->config->get_ldap_link();
1030 $ldap->cd($this->config->current['BASE']);
1031 $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
1033 $dhcp_dns = array();
1034 while($attr = $ldap->fetch()){
1035 $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
1036 }
1038 foreach($dhcp_dns as $key => $pri_dns){
1039 $ldap->cat($pri_dns,array("cn"));
1040 $tmp = $ldap->fetch();
1041 if(isset($tmp['cn'][0])){
1042 $dhcp_dns[$key] = $tmp['cn'][0];
1043 }else{
1044 unset($dhcp_dns[$key]);
1045 }
1046 }
1048 $tmp = $tmp2 = array();
1049 foreach($dhcp_dns as $dn => $cn){
1050 $ldap->cd($dn);
1051 $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
1052 "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
1053 while($attr = $ldap->fetch()){
1054 $tmp[$attr['dn']] = $attr['cn'][0];
1055 }
1056 $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.") "));
1057 }
1058 return($tmp2);
1059 }
1062 /* this function returns the default ptr record entry */
1063 function get_pTRRecord()
1064 {
1065 if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
1066 $ldap = $this->config->get_ldap_link();
1067 $ldap->cat(DNS::getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
1068 $attrs = $ldap->fetch();
1069 $tmp = array_flip($this->Zones);
1070 $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
1071 $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
1072 $ptr = preg_replace("/^".normalizePreg(DNS::FlipIp($tmp))."\./","",$this->ipHostNumber);
1073 return($ptr);
1074 }else{
1075 return(FALSE);
1076 }
1077 }
1080 function generateRandomIP($net = "")
1081 {
1082 $str = $net;
1083 $cnt = 4;
1084 while(substr_count($str,".") < 3 && $cnt > 0){
1085 $str .= ".".rand(0,255);
1086 $str = preg_replace("/\.\.*/",".",$str);
1087 $str = trim($str,". ");
1088 $cnt --;
1089 }
1090 return($str);
1091 }
1094 function create_tree($arr,$base,$current = "")
1095 {
1096 $ret = array();
1097 foreach($arr as $r => $name){
1098 $base_part = str_replace($base,"",$r);
1099 if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
1100 $ret[$r] = $current.$name;
1101 $tmp = $this->create_tree($arr,$r,$current.". ");
1102 foreach($tmp as $sub_key => $sub_name){
1103 $ret[$sub_key] = $sub_name;
1104 }
1105 }
1106 }
1107 return($ret);
1108 }
1110 function force_dns()
1111 {
1112 if($this->DNSenabled){
1114 /* Only force DNS account, if we have at least on dns Zone */
1115 if(count($this->Zones)){
1116 $this->DNS_is_account = TRUE;
1117 $this->hide_dns_check_box = TRUE;
1118 }
1119 }
1120 }
1121 }
1123 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1124 ?>