e3059b2250b463dd2acc710d191000331ea4e16d
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 /* Call parent execute */
261 $smarty= get_smarty();
262 $smarty->assign("autonetACL",$this->acl_is_writeable("macAddress") && $this->acl_is_writeable("ipHostNumber")?"rw":"");
263 $smarty->assign("MACisMust",$this->MACisMust);
265 $tmp = $this->plInfo();
266 foreach($tmp['plProvidedAcls'] as $name => $translation){
267 $smarty->assign($name."ACL",$this->getacl($name));
268 }
270 $display= "";
272 /**********
273 * Additional ipHostNumber handling
274 **********/
276 /* Add a new one */
277 if($this->acl_is_writeable("ipHostNumber")){
278 foreach($_POST as $name => $value){
279 if(preg_match("/^additionalHostNumbers_add/",$name)){
280 $this->additionalHostNumbers[] = "";
281 break;
282 }
284 /* Delete given entry */
285 if(preg_match("/^additionalHostNumbers_del_/",$name)){
286 $id = preg_replace("/^^additionalHostNumbers_del_([0-9]*)_.*/","\\1",$name);
287 if(isset($this->additionalHostNumbers[$id])){
288 unset($this->additionalHostNumbers[$id]);
289 $this->additionalHostNumbers = array_values($this->additionalHostNumbers);
290 }
291 break;
292 }
293 }
294 }
296 $smarty->assign("additionalHostNumbers",$this->additionalHostNumbers);
297 $smarty->assign("staticAddress", "");
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 if(class_available("gosaSupportDaemon")){
306 $d= new gosaSupportDaemon(TRUE, 0.5);
307 $res= $d->_send("<xml><header>gosa_network_completition</header>".
308 "<source>GOSA</source><target>GOSA</target><hostname>".$this->cn."</hostname></xml>", TRUE);
309 if (isset($res['XML']['IP']) && $this->acl_is_writeable("ipHostNumber")){
310 $this->ipHostNumber= $res['XML']['IP'];
311 }
312 if (isset($res['XML']['MAC']) && $this->acl_is_writeable("macAddress")){
313 $this->macAddress= $res['XML']['MAC'];
314 }
315 }
316 }
319 /**********
320 * DHCP Handling
321 **********/
323 if(isset($_POST['dhcpEditOptions']) && $this->acl_is_readable("dhcpSetup")){
324 if(count($this->dhcpHostEntry) == 0){
325 $this->dialog = new dhcpHost($this->parent,$this->dhcpParentNode,TRUE);
326 }else{
327 $this->dialog = new dhcpHost($this->parent,$this->dhcpHostEntry,TRUE);
328 }
329 $this->dialog->cn = $this->cn;
330 $this->dialog->read_only = !$this->acl_is_writeable("dhcpSetup");
331 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
332 if(!empty($this->ipHostNumber)){
333 $this->dialog->statements->set('fixed-address', $this->ipHostNumber);
334 }
335 }
337 if(isset($_POST['cancel_dhcp'])){
338 $this->dialog = FALSE;
339 }
341 if(isset($_POST['save_dhcp']) && $this->acl_is_writeable("dhcpSetup") && is_object($this->dialog)){
342 $this->dialog->save_object();
343 $msgs = $this->dialog->check(array());
344 if(count($msgs)){
345 foreach($msgs as $msg){
346 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
347 }
348 }else{
349 $this->dhcpHostEntry = $this->dialog->save();
350 $this->dialog = FALSE;
351 }
352 }
354 if(is_object($this->dialog)){
355 $this->dialog->save_object();
356 return($this->dialog->execute());
357 }
359 $smarty->assign("dhcpEnabled", $this->dhcpEnabled && $this->acl_is_readable("dhcpSetup"));
360 $smarty->assign("dhcp_is_Account",$this->dhcp_is_Account);
361 $smarty->assign("dhcpParentNode", $this->dhcpParentNode);
362 $smarty->assign("dhcpParentNodes",$this->dhcpParentNodes);
363 $smarty->assign("dhcpParentNodeCnt",count($this->dhcpParentNodes));
365 /**********
366 * DNS Handling
367 **********/
369 /* There is no dns available
370 */
371 $smarty->assign("DNS_is_account",$this->DNS_is_account);
372 $smarty->assign("DNSenabled",$this->DNSenabled && $this->acl_is_readable("dnsSetup"));
374 if($this->DNSenabled == false){
376 /* Is IP address must ? */
377 # $smarty->assign("DNS_is_account",false);
378 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
380 /* Assign smarty all non DNs attributes */
381 foreach($this->attributes as $attr){
382 $smarty->assign($attr,$this->$attr);
383 }
384 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
386 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
387 }else{
388 # $smarty->assign("DNS_is_account",true);
390 /* Add new empty array to our record list */
391 if(isset($_POST['AddNewRecord']) && $this->acl_is_writeable("dnsSetup")){
392 $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>"");
393 }
395 /* propose_ip */
396 if(isset($_POST['propose_ip']) && $this->acl_is_writeable("ipHostNumber")){
397 foreach($this->Zones as $key => $name){
398 if($name == $this->dnsEntry['zoneName']){
399 $net = DNS::FlipIp(str_replace(".in-addr.arpa.","",DNS::getNameFromMix($key)));
400 $this->ipHostNumber = $this->generateRandomIP($net);
401 }
402 }
403 }
405 /* Handle all posts */
406 $only_once =true;
407 if($this->acl_is_writeable("dnsSetup")){
408 foreach($_POST as $name => $value){
410 /* Check if we have to delete a record entry */
411 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
413 /* Avoid performing this once again */
414 $only_once = false;
416 /* Extract id for specified entry */
417 $id = preg_replace("/RemoveRecord_/","",$name);
418 $id = preg_replace("/_.*$/","",$id);
420 /* Delete this record, mark edited entries to be able to delete them */
421 if(isset($this->dnsEntry['RECORDS'][$id])){
422 unset($this->dnsEntry['RECORDS'][$id]);
423 }
424 }
425 }
426 }
427 /* Assign smarty all non DNs attributes */
428 foreach($this->attributes as $attr){
429 $smarty->assign($attr,$this->$attr);
430 }
432 /* Assign smarty all DNS attributes */
433 foreach($this->DNSattributes as $attr){
434 $smarty->assign($attr,htmlentities($this->dnsEntry[$attr]));
435 }
437 /* Assign all needed vars */
438 # $smarty->assign("DNSAccount",$this->DNS_is_account);
439 $smarty->assign("hide_dns_check_box",$this->hide_dns_check_box);
441 $smarty->assign("Zones",$this->Zones);
442 $smarty->assign("ZoneCnt",count($this->Zones));
443 $smarty->assign("ZoneKeys",($this->Zones));
444 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
446 /* Create zone array */
447 $idZones = array();
448 foreach($this->Zones as $id => $zone){
449 if($this->netmaskIsCoherent($id)) {
450 $idZones[$id] = $zone;
451 }else{
452 $idZones[$id] = $zone." ("._("Not matching").")";
453 }
454 }
455 $smarty->assign("Zones",$idZones);
456 $smarty->assign("ZoneKeys", $this->Zones);
458 $tmp = $this->generateRecordsList();
460 $changeStateForRecords = $tmp['changeStateForRecords'];
462 $smarty->assign("records",$tmp['str']);
463 $smarty->assign("changeStateForRecords",$changeStateForRecords);
464 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
466 $smarty->assign("autonetACL",$this->acl_is_writeable("macAddress") && $this->acl_is_writeable("ipHostNumber")?"rw":"");
468 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
469 }
471 return($display);
472 }
475 function remove_from_parent()
476 {
477 if($this->DNS_is_account){
479 $ldap = $this->config->get_ldap_link();
481 $tmp = array();
482 $this->dnsEntry['exists'] = false;
483 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
485 /* Delete dns */
486 foreach($tmp['del'] as $dn => $del){
487 $ldap->cd($dn);
488 $ldap->rmdir_recursive($dn);
489 new log("remove","unknown/".get_class($this),$dn);
490 if (!$ldap->success()){
491 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_DEL, get_class()));
492 }
493 }
494 }
495 }
498 /* Save data to object */
499 function save_object()
500 {
502 if(isset($_POST['network_tpl_posted'])){
504 /* Save all posted vars */
505 plugin::save_object();
507 /******
508 Additional IP Host Numbers
509 ******/
511 /* Get posts for all additionally added ipHostNumbers */
512 if($this->acl_is_writeable("ipHostNumber")){
513 foreach($this->additionalHostNumbers as $id => $value){
514 if(isset($_POST['additionalHostNumbers_'.$id])){
515 $this->additionalHostNumbers[$id] = get_post('additionalHostNumbers_'.$id);
516 }
517 }
518 }
521 /******
522 DHCP posts
523 ******/
525 if($this->acl_is_writeable("dhcpSetup") && $this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
526 foreach($this->dhcpAttributes as $attr){
527 if(isset($_POST[$attr])){
528 $this->$attr = $_POST[$attr];
529 }
530 }
531 if(isset($_POST['dhcp_is_Account'])){
532 $this->dhcp_is_Account = TRUE;
533 }else{
534 $this->dhcp_is_Account = FALSE;
535 }
536 }
538 /* Ge all non dns attributes (IP/MAC)*/
539 foreach($this->attributes as $attr){
540 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
541 $this->$attr = $_POST[$attr];
542 }
543 }
546 /******
547 DNS posts
548 ******/
550 /* Check if DNS should be enabled / disabled
551 * -skip this, if the dns account is enforced.
552 */
553 if(!$this->hide_dns_check_box){
554 if($this->DNS_is_account && $this->acl_is_writeable("dnsSetup") && !isset($_POST['DNS_is_account'])){
555 $this->DNS_is_account = false;
556 }elseif(!$this->DNS_is_account && $this->acl_is_writeable("dnsSetup") && isset($_POST['DNS_is_account'])){
557 $this->DNS_is_account = true;
558 }
559 }
561 /* Get dns attributes */
562 if(($this->DNSenabled) && (isset($_POST['network_tpl_posted'])) && $this->acl_is_writeable("dnsSetup")){
564 /* Check for posted record changes */
565 if(is_array($this->dnsEntry['RECORDS'])){
566 foreach($this->dnsEntry['RECORDS'] as $key => $value){
568 /* Check if type has changed */
569 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
570 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
571 }
572 /* Check if value has changed */
573 if(isset($_POST['RecordValue_'.$key])){
574 $this->dnsEntry['RECORDS'][$key]['value'] = get_post('RecordValue_'.$key);
575 }
576 }
577 }
579 /* Get all basic DNS attributes (TTL, Clas ..)*/
580 foreach($this->DNSattributes as $attr){
581 if(isset($_POST[$attr])){
582 $this->dnsEntry[$attr] = get_post($attr);
583 }
584 }
585 }
586 if($this->hide_dns_check_box){
587 $this->DNS_is_account = true;
588 }
589 }
590 }
593 /* Check supplied data */
594 function check()
595 {
596 /* Call common method to give check the hook */
597 $message= plugin::check();
599 /******
600 check additional IP Host Numbers
601 ******/
603 foreach($this->additionalHostNumbers as $id => $value){
604 if(!tests::is_ip($value)){
605 $message[]= msgPool::invalid(sprintf(_("IP address %s"),($id +2)), "", "", "192.168.1.10");
606 }
607 }
610 /* Check if mac and ip are already used */
611 if(!empty($this->ipHostNumber) && $this->DNS_is_account &&
612 $this->ipHostNumber != $this->orig_ipHostNumber &&
613 in_array("ip:".$this->ipHostNumber,$this->used_ip_mac)){
614 $message[]= msgPool::duplicated(_("IP address"));
615 }
616 if(!empty($this->macAddress) && $this->dhcp_is_Account &&
617 $this->macAddress != $this->orig_macAddress &&
618 in_array("mac:".$this->macAddress,$this->used_ip_mac)){
619 $message[]= msgPool::duplicated(_("MAC address"));
620 }
622 /* Check if ip must be given
623 */
624 if(($this->IPisMust)||($this->DNS_is_account)){
625 if (empty($this->ipHostNumber)){
626 $message[]= msgPool::required(_("IP address"));
627 }elseif (!tests::is_ip($this->ipHostNumber)){
628 $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.10");
629 }
630 }
632 /* Check if mac is empty
633 */
634 if($this->MACisMust || $this->dhcp_is_Account){
635 if ($this->macAddress == "" ){
636 $message[]= msgPool::required(_("MAC address"));
637 }elseif(!tests::is_mac($this->macAddress)){
638 $message[]= msgPool::invalid(_("MAC address"), "", "", "00:0C:7F:31:33:F1");
639 }
640 }
642 /* only perfrom this checks if this is a valid DNS account */
643 if($this->DNS_is_account){
645 $checkArray = array();
646 $onlyOnce = array();
648 // $onlyOnce['cNAMERecord'] = 0;
649 $tmp = array_flip($this->Zones);
650 $tmp2 = $tmp[$this->dnsEntry['zoneName']];
651 if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
652 $tmp2 = preg_replace("/^.*\//","",$tmp2);
653 $message[] =sprintf(_("The IP address '%s' is not part of the selected reverse zone '%s'!"),$this->ipHostNumber,$tmp2);
654 }
656 /* Walk through all entries and detect duplicates or mismatches
657 */
658 foreach($this->dnsEntry['RECORDS'] as $name => $values){
660 /* Count record values, to detect duplicate entries for a specific record
661 */
662 if(!isset($checkArray[$values['type']][$values['value']])){
663 $checkArray[$values['type']][$values['value']] = 0;
664 }else{
665 $message[] = sprintf(_("Record type '%s' is duplicated!"),$values['type']);
666 }
668 /* Check if given entries in $onlyOnce are used more than once
669 */
670 if(isset($onlyOnce[$values['type']])){
671 $onlyOnce[$values['type']] ++;
672 if($onlyOnce[$values['type']] > 1){
673 $message[] = sprintf(_("Uniq record type '%s' is duplicated!"),$values['type']);
674 }
675 }
677 /* Skip txt record ...
678 */
679 if($values['type'] == "tXTRecord") continue;
681 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
682 */
683 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
684 #TODO: Where's the problem here?
685 $message[]=sprintf(_("The IP address '%s' will be added as 'A Record', this will be done automatically, please remove the record."),
686 $this->ipHostNumber);
687 }
689 /* only lower-case is allowed in record entries ...
690 */
691 if($values['value'] != strtolower($values['value'])){
692 #TODO: What's in values['value']? Something for a propper message?
693 $message[] = sprintf(_("Only lowercase records are allowed, please check your '%ss'."),$values['type']);
694 }
695 }
696 }
697 return ($message);
698 }
701 /* Save to LDAP */
702 function save()
703 {
704 $ldap= $this->config->get_ldap_link();
706 $dn = $this->parent->dn;
708 /*******************/
709 /* IP-MAC HANDLING */
710 /*******************/
712 /* $dn was posted as parameter */
713 $this->dn = $dn;
715 /* Save DNS setting & ip/Mac*/
716 plugin::save();
718 /* Add all additional ipHostNumbers now
719 */
720 if(!empty($this->ipHostNumber)){
721 $this->attrs['ipHostNumber'] = array($this->ipHostNumber);
722 }
723 foreach($this->additionalHostNumbers as $value){
724 $this->attrs['ipHostNumber'][] = $value;
725 }
727 /* Do not add the objectClass ipHost if no ip address is given */
728 if(!isset($this->attrs['ipHostNumber'])){
729 $this->attrs['objectClass'] = array_remove_entries(array("ipHost"),$this->attrs['objectClass']);
730 }
732 /* Write back to ldap */
733 $ldap->cd($this->dn);
734 $this->cleanup();
735 $ldap->modify ($this->attrs);
737 /****************/
738 /* DHCP HANDLING */
739 /****************/
741 /* New entry */
742 if($this->dhcpEnabled && $this->acl_is_writeable("dhcpSetup")) {
744 if(count($this->dhcpHostEntry) == 0){
745 $this->dialog = new dhcpHost($this->parent,$this->dhcpParentNode,TRUE);
746 $this->dialog->cn = $this->cn;
747 $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
748 if(!empty($this->ipHostNumber)){
749 $this->dialog->statements->set('fixed-address', $this->ipHostNumber);
750 }
751 $this->dialog->execute();
752 $this->dialog->save_object();
753 $this->dhcpHostEntry = $this->dialog->save();
754 if(count($this->dhcpHostEntry['dhcpOption']) == 0){
755 $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
756 }
757 }else{
759 // Updated Host-Name entry
760 foreach($this->dhcpHostEntry['dhcpOption'] as $key => $entry){
761 if(preg_match("/^host-name/", $entry)){
762 $this->dhcpHostEntry['dhcpOption'][$key] = "host-name ".$this->cn;
763 break;
764 }
765 }
766 }
768 /* Write mac address to dhcp settings */
769 if($this->dhcp_is_Account){
770 if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) ||
771 !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
772 $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
773 $this->dhcpHostEntry['MODIFIED'] = TRUE;
774 }
775 }
777 /* Updated IP host number */
778 if($this->dhcp_is_Account && $this->dhcpHostEntry['dhcpStatements']){
779 foreach($this->dhcpHostEntry['dhcpStatements'] as $id => $value){
780 if(preg_match("/^fixed-address/",$value)){
781 $this->dhcpHostEntry['dhcpStatements'][$id] = "fixed-address ".$this->ipHostNumber;
782 $this->dhcpHostEntry['MODIFIED'] = TRUE;
783 }
784 }
785 }
787 /* Unset dhcpStatements if this attribute is empty */
788 if(isset($this->dhcpHostEntry['dhcpStatements']) &&
789 ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
790 unset($this->dhcpHostEntry['dhcpStatements']);
791 }
793 /* DHCP removed */
794 if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
795 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
796 if (!$ldap->success()){
797 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_DEL, get_class()));
798 }
800 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
801 $tmp->handle_post_events("remove");
802 }
804 /* DHCP Added */
805 if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
806 $attrs = $this->dhcpHostEntry;
807 unset($attrs['MODIFIED']);
808 unset($attrs['dn']);
809 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
810 $res = $ldap->add($attrs);
812 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
813 $tmp->handle_post_events("add");
815 if (!$ldap->success()){
816 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), "cn=".$this->cn.",".$this->dhcpParentNode, LDAP_ADD, get_class()));
817 }
818 }
820 /* DHCP still activated */
821 if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
823 /* DHCP node changed */
824 if(($this->initial_dhcpParentNode != $this->dhcpParentNode) ||
825 ($this->cn != $this->OrigCn)){
826 $attrs = $this->dhcpHostEntry;
827 $attrs[$this->namingAttr] = $this->cn;
828 unset($attrs['dn']);
829 unset($attrs['MODIFIED']);
830 $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
831 $res = $ldap->add($attrs);
833 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
834 $tmp->handle_post_events("modify");
836 if (!$ldap->success()){
837 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), "cn=".$this->cn.",".$this->dhcpParentNode, LDAP_ADD, get_class()));
838 }
839 if($res){
840 $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
841 if (!$ldap->success()){
842 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_DEL, get_class()));
843 }
844 }
845 }
847 /* SAME node but modified */
848 if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1 &&
849 $this->initial_dhcpParentNode == $this->dhcpParentNode){
850 $attrs = $this->dhcpHostEntry;
851 unset($attrs['dn']);
852 unset($attrs['MODIFIED']);
853 $ldap->cd($this->dhcpHostEntry['dn']);
854 $ldap->modify($attrs);
856 $tmp = new servdhcp($this->config,$this->dhcpParentNode);
857 $tmp->handle_post_events("modify");
859 if (!$ldap->success()){
860 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dhcpHostEntry['dn'], LDAP_MOD, get_class()));
861 }
862 }
863 }
864 }
865 $this->dialog = FALSE;
867 /****************/
868 /* DNS HANDLING */
869 /****************/
871 /* If isn't DNS account but initially was DNS account
872 remove all DNS entries
873 */
874 if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
875 return;
876 }elseif($this->acl_is_writeable("dnsSetup")){
878 /* Add ipHostNumber to aRecords
879 */
880 $backup_dnsEntry = $this->dnsEntry;
881 if(!empty($this->ipHostNumber)){
882 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
883 $ptr = $this->get_pTRRecord();
884 if(!empty($ptr)){
885 $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
886 }
887 }
889 /* Create diff and follow instructions
890 * If Account was disabled, remove account by setting exists to false
891 */
892 if((!$this->DNS_is_account)&&($this->initially_was_account)){
893 $this->dnsEntry['exists'] = false;
894 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
895 }else{
896 $this->dnsEntry['exists'] = $this->DNS_is_account;
897 $tmp = DNS::getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
898 }
900 /* Update SOA record if needed
901 */
902 $this->updateSOARecord();
904 /* move follwoing entries
905 */
906 foreach($tmp['move'] as $src => $dst){
907 $this->recursive_move($src,$dst);
908 }
910 /* Delete dns */
911 foreach($tmp['del'] as $dn => $del){
912 $ldap->cd($dn);
913 $ldap->rmdir_recursive($dn);
914 new log("modify","unknown/".get_class($this),$dn,array("*"),$ldap->get_error());
915 }
917 /* Add || Update new DNS entries
918 */
919 foreach($tmp['add'] as $dn => $attrs){
920 $ldap->cd($dn);
921 $ldap->cat($dn, array('dn'));
922 if(count($ldap->fetch())){
923 $ldap->cd($dn);
924 $ldap->modify ($attrs);
925 }else{
926 $ldap->cd($dn);
927 $ldap->add($attrs);
928 }
929 new log("modify","unknown/".get_class($this),$dn,array_keys($attrs),$ldap->get_error());
930 }
933 /* Display errors
934 */
935 if (!$ldap->success()){
936 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, 0, get_class()));
937 }
939 $tmp2 = new servdns($this->config,$this->dn);
940 $tmp2->handle_post_events("modify");
942 $this->dnsEntry = $backup_dnsEntry;
943 }
944 }
946 /* Create html table with all used record types
947 */
948 function generateRecordsList()
949 {
950 $changeStateForRecords = "";
951 $str = "<table summary='' width='100%'>";
952 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
954 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
955 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
956 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
958 $str.=" <tr>".
959 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
960 " <td><input type='text' value=\"".htmlentities($entry['value'])."\" name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
961 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
962 "</tr>";
963 }
965 $str.= " <tr>".
966 " <td colspan=2 width='50%'></td><td>".
967 " <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
968 " </td>".
969 " </tr>".
970 "</table>";
971 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
973 return($ret);
974 }
977 /* Create a html select box which allows us to select different types of records
978 */
979 function generateRecordListBox($selected,$name)
980 {
981 $str = "<select name='".$name."' id='".$name."'>";
982 foreach($this->RecordTypes as $type => $value){
983 $use = "";
984 if($type == $selected){
985 $use = " selected ";
986 }
987 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
988 }
989 $str.="</select>";
990 return($str);
991 }
994 /* Return plugin informations for acl handling */
995 static function plInfo()
996 {
997 $tmp = array(
998 "plShortName" => _("DNS"),
999 "plDescription" => _("DNS settings"),
1000 "plSelfModify" => FALSE,
1001 "plDepends" => array(),
1002 "plPriority" => 5,
1003 "plSection" => array("administration"),
1004 "plCategory" => array("workstation","terminal","phone","server","component","printer","winworkstation"),
1006 "plProvidedAcls"=> array(
1007 "ipHostNumber" => _("IP address"),
1008 "macAddress" => _("MAC address"))
1009 );
1011 /* Hide all dns/dhcp configurations if not available
1012 */
1013 if(class_available("servdns")){
1014 $tmp['plProvidedAcls']["dnsSetup"] = _("DNS configuration");
1015 }
1016 if(class_available("servdhcp")){
1017 $tmp['plProvidedAcls']["dhcpSetup"] = _("DHCP configuration");
1018 }
1019 return($tmp);
1020 }
1023 function get_dhcp_host_entry()
1024 {
1025 $attrs = array();
1026 $dn = $this->get_dhcp_host_entry_dn();
1027 if($dn){
1028 $ldap = $this->config->get_ldap_link();
1029 $ldap->cd($this->config->current['BASE']);
1030 $ldap->cat($dn,array("*"));
1031 if($ldap->count()){
1032 $attrs = $ldap->fetch();
1033 foreach($attrs as $key => $value){
1034 if(is_numeric($key) || ($key == "count")){
1035 unset($attrs[$key]);
1036 }
1037 if(is_array($value) && isset($value['count'])){
1038 unset($attrs[$key]['count']);
1039 }
1040 }
1041 }
1042 }
1043 return($attrs);
1044 }
1047 function get_dhcp_host_entry_dn()
1048 {
1049 $ldap = $this->config->get_ldap_link();
1050 $ldap->cd($this->config->current['BASE']);
1051 $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("uid","cn","dn"));
1053 if($ldap->count()){
1054 $attr = $ldap->fetch();
1055 return($attr['dn']);
1056 }else{
1057 return("");
1058 }
1059 }
1062 function get_dhcp_parent_node()
1063 {
1064 return(preg_replace("/^cn=".preg_quote($this->cn, '/').",/","",$this->get_dhcp_host_entry_dn()));
1065 }
1068 function get_dhcp_parent_nodes()
1069 {
1070 $ldap = $this->config->get_ldap_link();
1071 $ldap->cd($this->config->current['BASE']);
1072 $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
1074 $dhcp_dns = array();
1075 while($attr = $ldap->fetch()){
1076 $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
1077 }
1079 foreach($dhcp_dns as $key => $pri_dns){
1080 $ldap->cat($pri_dns,array("cn"));
1081 $tmp = $ldap->fetch();
1082 if(isset($tmp['cn'][0])){
1083 $dhcp_dns[$key] = $tmp['cn'][0];
1084 }else{
1085 unset($dhcp_dns[$key]);
1086 }
1087 }
1089 $tmp = $tmp2 = array();
1090 foreach($dhcp_dns as $dn => $cn){
1091 $ldap->cd($dn);
1092 $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
1093 "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
1094 while($attr = $ldap->fetch()){
1095 $tmp[$attr['dn']] = $attr['cn'][0];
1096 }
1097 $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.") "));
1098 }
1099 return($tmp2);
1100 }
1103 /* this function returns the default ptr record entry */
1104 function get_pTRRecord()
1105 {
1106 if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
1107 $ldap = $this->config->get_ldap_link();
1108 $ldap->cat(DNS::getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
1109 $attrs = $ldap->fetch();
1110 $tmp = array_flip($this->Zones);
1111 $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
1112 $tmp = trim(preg_replace("/\.in-addr.arpa\.$/","",$tmp));
1113 $ptr = preg_replace("/^".preg_quote(DNS::FlipIp($tmp), '/')."\./","",$this->ipHostNumber);
1114 return($ptr);
1115 }else{
1116 return(FALSE);
1117 }
1118 }
1121 function generateRandomIP($net = "")
1122 {
1123 $str = $net;
1124 $cnt = 4;
1126 // first gather all IPs
1127 $ldap = $this->config->get_ldap_link();
1128 $ocs =
1129 "(objectClass=goFonHardware)".
1130 "(objectClass=goServer)".
1131 "(objectClass=GOhard)".
1132 "(objectClass=gotoTerminal)".
1133 "(objectClass=gotoWorkstation)".
1134 "(objectClass=gotoPrinter)".
1135 "(objectClass=ipHost)";
1136 $list = array();
1137 $ldap->search("(&(|{$ocs})(ipHostNumber=*))",array("ipHostNumber"));
1138 while($attrs = $ldap->fetch()){
1139 if (preg_match("/^$net\./", $attrs['ipHostNumber'][0])) {
1140 $list[] = $attrs['ipHostNumber'][0];
1141 }
1142 }
1144 // Set starting ip.
1145 $ip_data = preg_split("/\./",$net);
1146 for($i=0;$i<4;$i++){
1147 if(!isset($ip_data[$i])) $ip_data[$i] = 0;
1148 }
1150 // Search the next free and valid ip.
1151 while(in_array(implode(".",$ip_data),$list) || $ip_data[3] <= 1){
1152 $ip_data[3] ++ ;
1153 if($ip_data[3] > 255){
1154 $ip_data[3] = 1 ;
1155 $ip_data[2] ++ ;
1156 }
1157 if($ip_data[2] > 255){
1158 $ip_data[2] = 1 ;
1159 $ip_data[1] ++ ;
1160 }
1161 if($ip_data[1] > 255){
1162 $ip_data[1] = 1 ;
1163 $ip_data[0] ++ ;
1164 }
1165 if($ip_data[0] > 255) break;
1166 }
1168 return(implode(".",$ip_data));
1169 }
1172 function create_tree($arr,$base,$current = "")
1173 {
1174 $ret = array();
1175 foreach($arr as $r => $name){
1176 $base_part = str_replace($base,"",$r);
1177 if(preg_match("/^[a-z]*=".preg_quote($name, '/')."(|,)$/i",$base_part)){
1178 $ret[$r] = $current.$name;
1179 $tmp = $this->create_tree($arr,$r,$current.". ");
1180 foreach($tmp as $sub_key => $sub_name){
1181 $ret[$sub_key] = $sub_name;
1182 }
1183 }
1184 }
1185 return($ret);
1186 }
1188 function force_dns()
1189 {
1190 if($this->DNSenabled){
1192 /* Only force DNS account, if we have at least on dns Zone */
1193 if(count($this->Zones)){
1194 $this->DNS_is_account = TRUE;
1195 $this->hide_dns_check_box = TRUE;
1196 }
1197 }
1198 }
1200 function updateSOARecord()
1201 {
1202 $oldEntries = DNS::getDNSHostEntries($this->config,$this->OrigCn);
1203 $newEntries = $this->dnsEntry;
1204 if(array_differs($newEntries['RECORDS'],$oldEntries['RECORDS'])){
1205 DNS::touchDNSZone($this->config,$this->dnsEntry['zoneName']);
1206 }
1207 }
1208 }
1210 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1211 ?>