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