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 $macAddress = ""; // Mac address
36 var $orig_ipHostNumber = ""; // IP address
37 var $orig_macAddress = ""; // Mac address
39 var $cn = ""; // CN of currently edited device
40 var $OrigCn = ""; // Initial cn
41 var $IPisMust = false;
42 var $MACisMust = false;
43 var $dialog = false;
45 /* DCHP Attributes
46 */
47 var $dhcpAttributes = array("dhcpParentNode");
48 var $dhcpEnabled = FALSE;
49 var $dhcp_is_Account = FALSE;
50 var $dhcpParentNodes = array();
51 var $dhcpParentNode = "";
52 var $dhcpHostEntry = array();
53 var $initial_dhcpParentNode = "";
54 var $initial_dhcpHostEntry = array();
55 var $initial_dhcp_is_Account = FALSE;
57 var $used_ip_mac = array();
59 /* DNS attributes
60 */
61 var $DNSattributes = array("dNSClass","zoneName","dNSTTL");
62 var $DNS_is_account = false;
63 var $initially_was_account = false;
64 var $dnsEntry = array();
65 var $DNSenabled = false;
66 var $hide_dns_check_box = FALSE;
68 /* Terminal dns
69 */
70 function termDNS (&$config, $parent,$objectClasses,$IPisMust = false)
71 {
72 /* We need to know which objectClasses are used, to store the ip/mac
73 * Because of different type of devices
74 */
75 $this->parent = $parent;
76 $this->objectclasses = $objectClasses;
77 $this->IPisMust = $IPisMust;
79 plugin::plugin ($config, $parent->dn);
81 if(isset($this->attrs['cn'][0])){
82 $this->OrigCn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
83 $this->cn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
84 }
87 /************
88 * DHCP
89 ************/
91 /* Hide all dhcp specific code, if dhcp plugin is not present in config */
92 $dhcpEnabled = FALSE;
93 if($this->config->search("servdhcp","class",array("tabs"))){
94 $this->dhcpEnabled = TRUE;
95 }
96 if($this->dhcpEnabled){
97 $this->dhcpParentNodes = $this->get_dhcp_parent_nodes();
98 $this->dhcpParentNode = $this->get_dhcp_parent_node();
99 if($this->dhcpParentNode){
100 $this->dhcp_is_Account = TRUE;
101 $this->initial_dhcp_is_Account = TRUE;
102 $this->dhcpHostEntry = $this->get_dhcp_host_entry();
103 }
104 $this->initial_dhcpHostEntry = $this->dhcpHostEntry;
105 $this->initial_dhcpParentNode= $this->dhcpParentNode;
106 }
109 /************
110 * DNS
111 ************/
113 /* Hide all dns specific code, if dns is not available
114 */
115 $DNSenabled = false;
116 foreach($this->config->data['TABS']['SERVERSERVICE'] as $tab){
117 if(preg_match("/^servdns$/",$tab['CLASS'])){
118 $this->DNSenabled = true;
119 }
120 }
121 if(!$this->DNSenabled){
122 $this->DNS_is_account = false;
123 return;
124 }
126 if($this->DNSenabled){
128 /* Get Zones
129 */
130 $this->Zones = DNS::getAvailableZones($config);
132 /* Get Entry
133 */
134 $this->dnsEntry = DNS::getDNSHostEntries($config,$this->OrigCn);
136 /* Remove A record which equals $this->ipHostNumber
137 */
138 $ptr = $this->get_pTRRecord();
139 foreach($this->dnsEntry['RECORDS'] as $key => $rec){
140 if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
141 unset($this->dnsEntry['RECORDS'][$key]);
142 }
143 if(($rec['type'] == "pTRRecord") && ($rec['value'] == $ptr)){
144 unset($this->dnsEntry['RECORDS'][$key]);
145 }
146 }
148 /* Get Record types
149 */
150 $this->RecordTypes = DNS::getDnsRecordTypes();
152 /* If there is at least one entry in this -> types, we have DNS enabled
153 */
154 if($this->dnsEntry['exists']){
155 $this->DNS_is_account = true;
156 }else{
157 $this->DNS_is_account = false;
158 }
159 }
161 /* Create a list of used mac and ip addresses.
163 ! We use this optically huge amount of code to fetch all
164 Mac and IP addresses, because a simple search for mac and IP
165 over the whole ldap server was 10 to 20 times slower.
166 */
167 $deps = array();
168 $ou = preg_replace("/,.*$/","",get_ou("systemsou"));
169 $a_ous = array(get_ou("serverou"),
170 get_ou("terminalou"),
171 get_ou("workstationou"),
172 get_ou("printerou"),
173 get_ou("phoneou"),
174 get_ou("componentou"));
176 $ldap = $this->config->get_ldap_link();
177 $ldap->cd($this->config->current['BASE']);
178 $ldap->search("(&(objectClass=organizationalUnit)(".$ou."))",array("dn"));
179 while($attrs = $ldap->fetch()){
180 foreach($a_ous as $allowed){
181 $deps[] = $allowed.$attrs['dn'];
182 }
183 }
185 foreach($deps as $dep){
186 $ldap->cd($dep);
187 $ldap->search("(|(macAddress=*)(ipHostNumber=*))",array("macAddress","ipHostNumber"));
188 while($attrs = $ldap->fetch()){
189 if(isset($attrs['ipHostNumber'][0])){
190 $this->used_ip_mac["ip:".$attrs['ipHostNumber'][0]] = "ip:".$attrs['ipHostNumber'][0];
191 }
192 if(isset($attrs['macAddress'][0])){
193 $this->used_ip_mac["mac:".$attrs['macAddress'][0]] = "mac:".$attrs['macAddress'][0];
194 }
195 }
196 }
198 /* Save initial ip and mac values, to be able
199 check if the used values are already in use */
200 $this->orig_ipHostNumber = $this->ipHostNumber;
201 $this->orig_macAddress = $this->macAddress;
203 /* Store initally account settings
204 */
205 $this->initially_was_account = $this->DNS_is_account;
207 if($this->DNS_is_account){
208 new log("view","unknown/".get_class($this),$this->dn);
209 }
210 }
213 function netmaskIsCoherent($idZone)
214 {
215 $netmask = DNS::FlipIp(str_replace(".in-addr.arpa","",DNS::getNameFromMix($idZone)));
216 if(!strstr($this->ipHostNumber, $netmask)){
217 return false;
218 }else{
219 return true;
220 }
221 }
224 function getVarsForSaving($attrs)
225 {
226 foreach($this->attributes as $attr){
227 if(!empty($this->$attr)){
228 $attrs[$attr] = $this->$attr;
229 }
230 }
231 return($attrs);
232 }
234 function execute()
235 {
236 /* Call parent execute */
237 $smarty= get_smarty();
239 $tmp = $this->plInfo();
240 foreach($tmp['plProvidedAcls'] as $name => $translation){
241 $smarty->assign($name."ACL",$this->getacl($name));
242 }
244 $display= "";
246 $smarty->assign("staticAddress", "");
248 /* Check for autonet button */
249 if (isset($_POST['autonet'])){
250 $d= new gosaSupportDaemon(TRUE, 0.5);
251 $res= $d->_send("<xml><header>gosa_network_completition</header><source>GOSA</source><target>GOSA</target><hostname>".$this->cn."</hostname></xml>", TRUE);
252 if (isset($res['XML']['IP'])){
253 $this->ipHostNumber= $res['XML']['IP'];
254 }
255 if (isset($res['XML']['MAC'])){
256 $this->macAddress= $res['XML']['MAC'];
257 }
258 }
261 /**********
262 * DHCP Handling
263 **********/
265 if(isset($_POST['dhcpEditOptions'])){
267 if(count($this->dhcpHostEntry) == 0){
268 $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
269 }else{
270 $this->dialog = new dhcpHost($this->dhcpHostEntry,TRUE);
271 }
272 $this->dialog->cn = $this->cn;
273 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
274 if(!empty($this->ipHostNumber)){
275 $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
276 }
277 }
279 if(isset($_POST['cancel_dhcp'])){
280 $this->dialog = FALSE;
281 }
283 if(isset($_POST['save_dhcp'])){
284 $this->dialog->save_object();
286 $msgs = $this->dialog->check(array());
287 if(count($msgs)){
288 foreach($msgs as $msg){
289 print_red($msg);
290 }
291 }else{
292 $this->dhcpHostEntry = $this->dialog->save();
293 $this->dialog = FALSE;
294 }
295 }
297 if(is_object($this->dialog)){
298 $this->dialog->save_object();
299 return($this->dialog->execute());
300 }
302 $smarty->assign("dhcpEnabled", $this->dhcpEnabled);
303 $smarty->assign("dhcp_is_Account",$this->dhcp_is_Account);
304 $smarty->assign("dhcpParentNode", $this->dhcpParentNode);
305 $smarty->assign("dhcpParentNodes",$this->dhcpParentNodes);
306 $smarty->assign("dhcpParentNodeCnt",count($this->dhcpParentNodes));
309 /**********
310 * DNS Handling
311 **********/
313 /* There is no dns available
314 */
315 $smarty->assign("DNS_is_account",$this->DNS_is_account);
316 $smarty->assign("DNSenabled",$this->DNSenabled);
317 if($this->DNSenabled == false){
319 /* Is IP address must ? */
320 # $smarty->assign("DNS_is_account",false);
321 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
323 /* Assign smarty all non DNs attributes */
324 foreach($this->attributes as $attr){
325 $smarty->assign($attr,$this->$attr);
326 }
327 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
329 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
330 }else{
331 # $smarty->assign("DNS_is_account",true);
333 /* Add new empty array to our record list */
334 if(isset($_POST['AddNewRecord'])){
335 $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>"");
336 }
338 /* propose_ip */
339 if(isset($_POST['propose_ip'])){
340 foreach($this->Zones as $key => $name){
341 if($name == $this->dnsEntry['zoneName']){
342 $net = DNS::FlipIp(str_replace(".in-addr.arpa","",DNS::getNameFromMix($key)));
343 $this->ipHostNumber = $this->generateRandomIp($net);
344 }
345 }
346 }
348 /* Handle all posts */
349 $only_once =true;
350 foreach($_POST as $name => $value){
352 /* Check if we have to delete a record entry */
353 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
355 /* Avoid performing this once again */
356 $only_once = false;
358 /* Extract id for specified entry */
359 $id = preg_replace("/RemoveRecord_/","",$name);
360 $id = preg_replace("/_.*$/","",$id);
362 /* Delete this record, mark edited entries to be able to delete them */
363 if(isset($this->dnsEntry['RECORDS'][$id])){
364 unset($this->dnsEntry['RECORDS'][$id]);
365 }
366 }
367 }
368 /* Assign smarty all non DNs attributes */
369 foreach($this->attributes as $attr){
370 $smarty->assign($attr,$this->$attr);
371 }
373 /* Assign smarty all DNS attributes */
374 foreach($this->DNSattributes as $attr){
375 $smarty->assign($attr,$this->dnsEntry[$attr]);
376 }
378 /* Assign all needed vars */
379 # $smarty->assign("DNSAccount",$this->DNS_is_account);
380 $smarty->assign("hide_dns_check_box",$this->hide_dns_check_box);
382 $smarty->assign("Zones",$this->Zones);
383 $smarty->assign("ZoneCnt",count($this->Zones));
384 $smarty->assign("ZoneKeys",($this->Zones));
385 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
387 /* Create zone array */
388 $idZones = array();
389 foreach($this->Zones as $id => $zone){
390 if($this->netmaskIsCoherent($id)) {
391 $idZones[$id] = $zone;
392 }else{
393 $idZones[$id] = $zone." ("._("Not matching").")";
394 }
395 }
396 $smarty->assign("Zones",$idZones);
397 $smarty->assign("ZoneKeys", $this->Zones);
399 $tmp = $this->generateRecordsList();
401 $changeStateForRecords = $tmp['changeStateForRecords'];
403 $smarty->assign("records",$tmp['str']);
404 $smarty->assign("changeStateForRecords",$changeStateForRecords);
405 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
407 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
408 }
409 return($display);
410 }
413 function remove_from_parent()
414 {
415 if($this->initially_was_account){
417 $ldap = $this->config->get_ldap_link();
419 $tmp = array();
420 $this->dnsEntry['exists'] = false;
421 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
423 /* Delete dns */
424 foreach($tmp['del'] as $dn => $del){
425 $ldap->cd($dn);
426 $ldap->rmdir_recursive($dn);
427 new log("remove","unknown/".get_class($this),$dn);
428 show_ldap_error($ldap->get_error(), sprintf(_("Could not remove dns extension for '%s'."),$dn));
429 }
430 }
431 }
434 /* Save data to object */
435 function save_object()
436 {
438 if(isset($_POST['network_tpl_posted'])){
440 /* Save all posted vars */
441 plugin::save_object();
443 /* Handle DHCP Posts*/
444 if($this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
445 foreach($this->dhcpAttributes as $attr){
446 if(isset($_POST[$attr])){
447 $this->$attr = $_POST[$attr];
448 }
449 }
450 if(isset($_POST['dhcp_is_Account'])){
451 $this->dhcp_is_Account = TRUE;
452 }else{
453 $this->dhcp_is_Account = FALSE;
454 }
455 }
457 /* Ge all non dns attributes (IP/MAC)*/
458 foreach($this->attributes as $attr){
459 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
460 $this->$attr = $_POST[$attr];
461 }
462 }
464 /* Check if DNS should be enabled / disabled */
465 if($this->DNS_is_account && $this->acl_is_removeable() && !isset($_POST['DNS_is_account'])){
466 $this->DNS_is_account = false;
467 }elseif(!$this->DNS_is_account && $this->acl_is_createable() && isset($_POST['DNS_is_account'])){
468 $this->DNS_is_account = true;
469 }
471 /* Get dns attributes */
472 if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
474 /* Check for posted record changes */
475 if(is_array($this->dnsEntry['RECORDS']) && $this->acl_is_writeable("Records")){
476 foreach($this->dnsEntry['RECORDS'] as $key => $value){
478 /* Check if type has changed */
479 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
480 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
481 }
482 /* Check if value has changed */
483 if(isset($_POST['RecordValue_'.$key])){
484 $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
485 }
486 }
487 }
488 /* Get all basic DNS attributes (TTL, Clas ..)*/
489 foreach($this->DNSattributes as $attr){
490 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
491 $this->dnsEntry[$attr] = $_POST[$attr];
492 }
493 }
496 }
497 if($this->hide_dns_check_box){
498 $this->DNS_is_account = true;
499 }
500 }
501 }
504 /* Check supplied data */
505 function check()
506 {
507 /* Call common method to give check the hook */
508 $message= plugin::check();
510 # if($this->dhcpEnabled && $this->dhcp_is_Account && $this->dhcpParentNode != "" && count($this->dhcpHostEntry) == 0){
511 # $message[] =_("You have not configured your dhcp settings yet.");
512 # }
514 /* Check if mac and ip are already used */
515 if(!empty($this->ipHostNumber) && $this->DNS_is_account &&
516 $this->ipHostNumber != $this->orig_ipHostNumber &&
517 in_array("ip:".$this->ipHostNumber,$this->used_ip_mac)){
518 $message[]= msgPool::duplicated(_("IP address"));
519 }
520 if(!empty($this->macAddress) && $this->dhcp_is_Account &&
521 $this->macAddress != $this->orig_macAddress &&
522 in_array("mac:".$this->macAddress,$this->used_ip_mac)){
523 $message[]= msgPool::duplicated(_("MAC address"));
524 }
526 /* Check if ip must be given
527 */
528 if(($this->IPisMust)||($this->DNS_is_account)){
529 if (empty($this->ipHostNumber)){
530 $message[]= msgPool::required(_("IP address"));
531 }
533 if (!tests::is_ip($this->ipHostNumber)){
534 $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.10");
535 }
536 }
538 /* Check if mac is empty
539 */
540 if ($this->macAddress == "" ){
541 $message[]= msgPool::required(_("IP address"));
542 }
543 if(!tests::is_mac($this->macAddress)){
544 $message[]= msgPool::invalid(_("MAC address"), "", "", "00:0C:7F:31:33:F1");
545 }
547 /* only perfrom this checks if this is a valid DNS account */
548 if($this->DNS_is_account){
550 $checkArray = array();
551 $onlyOnce = array();
553 // $onlyOnce['cNAMERecord'] = 0;
554 $tmp = array_flip($this->Zones);
555 $tmp2 = $tmp[$this->dnsEntry['zoneName']];
556 if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
557 $tmp2 = preg_replace("/^.*\//","",$tmp2);
558 $message[] =sprintf(_("The IP address '%s' is not part of the selected reverse zone '%s'!"),$this->ipHostNumber,$tmp2);
559 }
561 /* Walk through all entries and detect duplicates or mismatches
562 */
563 foreach($this->dnsEntry['RECORDS'] as $name => $values){
565 /* Count record values, to detect duplicate entries for a specific record
566 */
567 if(!isset($checkArray[$values['type']][$values['value']])){
568 $checkArray[$values['type']][$values['value']] = 0;
569 }else{
570 $message[] = sprintf(_("Record type '%s' is duplicated!"),$values['type']);
571 }
573 /* Check if given entries in $onlyOnce are used more than once
574 */
575 if(isset($onlyOnce[$values['type']])){
576 $onlyOnce[$values['type']] ++;
577 if($onlyOnce[$values['type']] > 1){
578 $message[] = sprintf(_("Uniq record type '%s' is duplicated!"),$values['type']);
579 }
580 }
582 /* Skip txt record ...
583 */
584 if($values['type'] == "tXTRecord") continue;
586 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
587 */
588 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
589 #TODO: Where's the problem here?
590 $message[]=sprintf(_("The IP address '%s' will is added as 'A Record', this will be done automatically, please remove the record."),
591 $this->ipHostNumber);
592 }
594 /* only lower-case is allowed in record entries ...
595 */
596 if($values['value'] != strtolower($values['value'])){
597 #TODO: What's in values['value']? Something for a propper message?
598 $message[] = sprintf(_("Only lowercase records are allowed, please check your '%ss'."),$values['type']);
599 }
600 }
601 }
602 return ($message);
603 }
606 /* Save to LDAP */
607 function save()
608 {
609 $ldap= $this->config->get_ldap_link();
611 $dn = $this->parent->dn;
613 /*******************/
614 /* IP-MAC HANDLING */
615 /*******************/
617 /* $dn was posted as parameter */
618 $this->dn = $dn;
620 /* Save DNS setting & ip/Mac*/
621 plugin::save();
623 /* Write back to ldap */
624 $ldap->cd($this->dn);
625 $this->cleanup();
626 $ldap->modify ($this->attrs);
628 /****************/
629 /* DHCP HANDLING */
630 /****************/
632 /* New entry */
633 if($this->dhcpEnabled){
635 if(count($this->dhcpHostEntry) == 0){
636 $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
637 $this->dialog->cn = $this->cn;
638 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
639 if(!empty($this->ipHostNumber)){
640 $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
641 }
642 $this->dialog->execute();
643 $this->dialog->save_object();
644 $this->dhcpHostEntry = $this->dialog->save();
645 if(count($this->dhcpHostEntry['dhcpOption']) == 0){
646 $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
647 }
648 }
650 if(count($this->dhcpHostEntry) == 0){
651 $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
652 $this->dialog->cn = $this->cn;
653 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
654 if(!empty($this->ipHostNumber)){
655 $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
656 }
657 $this->dialog->execute();
658 $this->dialog->save_object();
659 $this->dhcpHostEntry = $this->dialog->save();
660 if(count($this->dhcpHostEntry['dhcpOption']) == 0){
661 $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
662 }
663 }
665 /* Write mac address to dhcp settings */
666 if($this->dhcp_is_Account){
667 if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) ||
668 !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
669 $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
670 $this->dhcpHostEntry['MODIFIED'] = TRUE;
671 }
672 }
675 /* Unset dhcpStatements if this attribute is empty */
676 if(isset($this->dhcpHostEntry['dhcpStatements']) &&
677 ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
678 unset($this->dhcpHostEntry['dhcpStatements']);
679 }
681 /* DHCP removed */
682 if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
683 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
684 show_ldap_error($ldap->get_error(),_("Removing dhcp entry for this object failed."));
686 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
687 $tmp->handle_post_events("remove");
688 }
690 /* DHCP Added */
691 if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
692 $attrs = $this->dhcpHostEntry;
693 unset($attrs['MODIFIED']);
694 unset($attrs['dn']);
695 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
696 $res = $ldap->add($attrs);
698 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
699 $tmp->handle_post_events("add");
701 show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
702 }
704 /* DHCP still activated */
705 if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
707 /* DHCP node changed */
708 if(($this->initial_dhcpParentNode != $this->dhcpParentNode) ||
709 ($this->cn != $this->OrigCn)){
710 $attrs = $this->dhcpHostEntry;
711 $attrs['cn'] = $this->cn;
712 unset($attrs['dn']);
713 unset($attrs['MODIFIED']);
714 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
715 $res = $ldap->add($attrs);
717 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
718 $tmp->handle_post_events("modify");
720 show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
721 if($res){
722 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
723 show_ldap_error($ldap->get_error(),_("Removing old dhcp entry failed."));
724 }
725 }
727 /* SAME node but modified */
728 if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1 &&
729 $this->initial_dhcpParentNode == $this->dhcpParentNode){
730 $attrs = $this->dhcpHostEntry;
731 unset($attrs['dn']);
732 unset($attrs['MODIFIED']);
733 $ldap->cd($this->dhcpHostEntry['dn']);
734 $ldap->modify($attrs);
736 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
737 $tmp->handle_post_events("modify");
739 show_ldap_error($ldap->get_error(),_("Modifying dhcp entry failed."));
740 }
741 }
742 }
743 $this->dialog = FALSE;
745 /****************/
746 /* DNS HANDLING */
747 /****************/
749 /* If isn't DNS account but initially was DNS account
750 remove all DNS entries
751 */
752 if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
753 return;
754 }else{
756 /* Add ipHostNumber to aRecords
757 */
758 $backup_dnsEntry = $this->dnsEntry;
759 if(!empty($this->ipHostNumber)){
760 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
761 $ptr = $this->get_pTRRecord();
762 if(!empty($ptr)){
763 $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
764 }
765 }
767 /* Create diff and follow instructions
768 * If Account was disabled, remove account by setting exists to false
769 */
770 if((!$this->DNS_is_account)&&($this->initially_was_account)){
771 $this->dnsEntry['exists'] = false;
772 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
773 }else{
774 $this->dnsEntry['exists'] = $this->DNS_is_account;
775 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
776 }
778 /* move follwoing entries
779 */
780 foreach($tmp['move'] as $src => $dst){
781 $this->recursive_move($src,$dst);
782 }
784 /* Delete dns */
785 foreach($tmp['del'] as $dn => $del){
786 $ldap->cd($dn);
787 $ldap->rmdir_recursive($dn);
788 new log("modify","unknown/".get_class($this),$dn,array("*"),$ldap->get_error());
789 }
791 /* Add || Update new DNS entries
792 */
793 foreach($tmp['add'] as $dn => $attrs){
794 $ldap->cd($dn);
795 $ldap->cat($dn, array('dn'));
796 if(count($ldap->fetch())){
797 $ldap->cd($dn);
798 $ldap->modify ($attrs);
799 }else{
800 $ldap->cd($dn);
801 $ldap->add($attrs);
802 }
803 new log("modify","unknown/".get_class($this),$dn,array_keys($attrs),$ldap->get_error());
804 }
807 /* Display errors
808 */
809 if($ldap->get_error() != "Success"){
810 show_ldap_error($ldap->get_error(), sprintf(_("Saving of terminal/dns account with dn '%s' failed."),$this->dn));
811 }
813 $tmp2 = new servdns($this->config,$this->dn);
814 $tmp2->handle_post_events("modify");
816 $this->dnsEntry = $backup_dnsEntry;
817 }
818 }
820 /* Create html table with all used record types
821 */
822 function generateRecordsList()
823 {
824 $changeStateForRecords = "";
826 if(!$this->DNS_is_account) {
827 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
828 return(array("str" => $str, "changeStateForRecords"=> ""));
829 }
831 $str = "<table summary='' width='100%'>";
832 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
834 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
835 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
836 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
838 $str.=" <tr>".
839 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
840 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
841 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
842 "</tr>";
843 }
845 $str.= " <tr>".
846 " <td colspan=2 width='50%'></td><td>".
847 " <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
848 " </td>".
849 " </tr>".
850 "</table>";
851 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
852 return($ret);
853 }
856 /* Create a html select box which allows us to select different types of records
857 */
858 function generateRecordListBox($selected,$name)
859 {
860 $str = "<select name='".$name."' id='".$name."'>";
861 foreach($this->RecordTypes as $type => $value){
862 $use = "";
863 if($type == $selected){
864 $use = " selected ";
865 }
866 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
867 }
868 $str.="</select>";
869 return($str);
870 }
873 /* Return plugin informations for acl handling */
874 static function plInfo()
875 {
876 $tmp = array(
877 "plShortName" => _("DNS"),
878 "plDescription" => _("DNS settings"),
879 "plSelfModify" => FALSE,
880 "plDepends" => array(),
881 "plPriority" => 5,
882 "plSection" => array("administration"),
883 "plCategory" => array("workstation","terminal","phone","server","component","printer","winworkstation"),
885 "plProvidedAcls"=> array(
886 "ipHostNumber" => _("IP address"),
887 "macAddress" => _("MAC address"))
888 );
890 /* Hide all dns specific code, if dns is not available
891 */
892 $config = session::get('config');
893 foreach($config->data['TABS']['SERVERSERVICE'] as $tab){
894 if(preg_match("/^servdns$/",$tab['CLASS'])){
895 $tmp['plProvidedAcls']["Records"] = _("DNS records");
896 $tmp['plProvidedAcls']["zoneName"] = _("Zone name");
897 $tmp['plProvidedAcls']["dNSTTL"] = _("TTL");
898 break;
899 }
900 }
901 return($tmp);
902 }
905 function get_dhcp_host_entry()
906 {
907 $attrs = array();
908 $dn = $this->get_dhcp_host_entry_dn();
909 if($dn){
910 $ldap = $this->config->get_ldap_link();
911 $ldap->cd($this->config->current['BASE']);
912 $ldap->cat($dn,array("*"));
913 if($ldap->count()){
914 $attrs = $ldap->fetch();
915 foreach($attrs as $key => $value){
916 if(is_numeric($key) || ($key == "count")){
917 unset($attrs[$key]);
918 }
919 if(is_array($value) && isset($value['count'])){
920 unset($attrs[$key]['count']);
921 }
922 }
923 }
924 }
925 return($attrs);
926 }
929 function get_dhcp_host_entry_dn()
930 {
931 $ldap = $this->config->get_ldap_link();
932 $ldap->cd($this->config->current['BASE']);
933 $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
935 if($ldap->count()){
936 $attr = $ldap->fetch();
937 return($attr['dn']);
938 }else{
939 return("");
940 }
941 }
944 function get_dhcp_parent_node()
945 {
946 return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
947 }
950 function get_dhcp_parent_nodes()
951 {
952 $ldap = $this->config->get_ldap_link();
953 $ldap->cd($this->config->current['BASE']);
954 $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
956 $dhcp_dns = array();
957 while($attr = $ldap->fetch()){
958 $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
959 }
961 foreach($dhcp_dns as $key => $pri_dns){
962 $ldap->cat($pri_dns,array("cn"));
963 $tmp = $ldap->fetch();
964 if(isset($tmp['cn'][0])){
965 $dhcp_dns[$key] = $tmp['cn'][0];
966 }else{
967 unset($dhcp_dns[$key]);
968 }
969 }
971 $tmp = $tmp2 = array();
972 foreach($dhcp_dns as $dn => $cn){
973 $ldap->cd($dn);
974 $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
975 "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
976 while($attr = $ldap->fetch()){
977 $tmp[$attr['dn']] = $attr['cn'][0];
978 }
979 $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.") "));
980 }
981 return($tmp2);
982 }
985 /* this function returns the default ptr record entry */
986 function get_pTRRecord()
987 {
988 if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
989 $ldap = $this->config->get_ldap_link();
990 $ldap->cat(DNS::getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
991 $attrs = $ldap->fetch();
992 $tmp = array_flip($this->Zones);
993 $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
994 $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
995 $ptr = preg_replace("/^".normalizePreg(DNS::FlipIp($tmp))."\./","",$this->ipHostNumber);
996 return($ptr);
997 }else{
998 return(FALSE);
999 }
1000 }
1003 function generateRandomIP($net = "")
1004 {
1005 $str = $net;
1006 $cnt = 4;
1007 while(substr_count($str,".") < 3 && $cnt > 0){
1008 $str .= ".".rand(0,255);
1009 $str = preg_replace("/\.\.*/",".",$str);
1010 $str = trim($str,". ");
1011 $cnt --;
1012 }
1013 return($str);
1014 }
1017 function create_tree($arr,$base,$current = "")
1018 {
1019 $ret = array();
1020 foreach($arr as $r => $name){
1021 $base_part = str_replace($base,"",$r);
1022 if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
1023 $ret[$r] = $current.$name;
1024 $tmp = $this->create_tree($arr,$r,$current.". ");
1025 foreach($tmp as $sub_key => $sub_name){
1026 $ret[$sub_key] = $sub_name;
1027 }
1028 }
1029 }
1030 return($ret);
1031 }
1033 function force_dns()
1034 {
1035 if($this->DNSenabled){
1037 /* Only force DNS account, if we have at least on dns Zone */
1038 if(count($this->Zones)){
1039 $this->DNS_is_account = TRUE;
1040 $this->hide_dns_check_box = TRUE;
1041 }
1042 }
1043 }
1044 }
1046 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1047 ?>