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