1 <?php
3 class termDNS extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account = true;
7 var $autonet = false;
9 /* Basic informations
10 */
11 var $attributes = array("ipHostNumber","macAddress");
12 var $objectclasses = array("whatever");
14 var $ipHostNumber = ""; // IP address
15 var $macAddress = ""; // Mac address
16 var $cn = ""; // CN of currently edited device
17 var $OrigCn = ""; // Initial cn
18 var $IPisMust = false;
19 var $MACisMust = false;
21 /* DNS attributes
22 */
23 var $DNSattributes = array("dNSClass","zoneName","dNSTTL");
24 var $DNS_is_account = false;
25 var $initially_was_account = false;
26 var $dnsEntry = array();
27 var $DNSenabled = false;
29 /* Terminal dns
30 */
31 function termDNS ($config, $dn,$objectClasses,$IPisMust = false)
32 {
33 /* We need to know which objectClasses are used, to store the ip/mac
34 * Because of different type of devices
35 */
36 $this->objectclasses = $objectClasses;
37 $this->IPisMust = $IPisMust;
39 plugin::plugin ($config, $dn);
41 if(isset($this->attrs['cn'][0])){
42 $this->OrigCn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
43 $this->cn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
44 }
46 /* Do we have autonet support? */
47 if (isset($this->config->data['MAIN']['AUTO_NETWORK_HOOK'])){
48 $this->autonet= true;
49 }
51 /* Hide all dns specific code, if dns is not available
52 */
53 $DNSenabled = false;
54 foreach($this->config->data['TABS']['SERVERSERVICE'] as $tab){
55 if(preg_match("/^servdns$/",$tab['CLASS'])){
56 $this->DNSenabled = true;
57 }
58 }
59 if(!$this->DNSenabled){
60 $this->DNS_is_account = false;
61 return;
62 }
64 if($this->DNSenabled){
66 /* Get Zones
67 */
68 $this->Zones = getAvailableZones($config);
70 /* Get Entry
71 */
72 $this->dnsEntry = getDNSHostEntries($config,$this->OrigCn);
74 /* Remove A record which equals $this->ipHostNumber
75 */
76 foreach($this->dnsEntry['RECORDS'] as $key => $rec){
77 if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
78 unset($this->dnsEntry['RECORDS'][$key]);
79 }
80 }
82 /* Get Record types
83 */
84 $this->RecordTypes = getDnsRecordTypes();
86 /* If there is at least one entry in this -> types, we have DNS enabled
87 */
88 if($this->dnsEntry['exists']){
89 $this->DNS_is_account = true;
90 }else{
91 $this->DNS_is_account = false;
92 }
93 }
95 /* Store initally account settings
96 */
97 $this->initially_was_account = $this->DNS_is_account;
99 if($this->DNS_is_account){
100 new log("view","unknown/".get_class($this),$this->dn);
101 }
102 }
105 function netmaskIsCoherent($idZone)
106 {
107 $netmask = FlipIp(str_replace(".in-addr.arpa","",getNameFromMix($idZone)));
108 if(!strstr($this->ipHostNumber, $netmask)){
109 return false;
110 }else{
111 return true;
112 }
113 }
116 function getVarsForSaving($attrs)
117 {
118 foreach($this->attributes as $attr){
119 if(!empty($this->$attr)){
120 $attrs[$attr] = $this->$attr;
121 }
122 }
123 return($attrs);
124 }
126 function execute()
127 {
128 /* Call parent execute */
129 $smarty= get_smarty();
131 $tmp = $this->plInfo();
132 foreach($tmp['plProvidedAcls'] as $name => $translation){
133 $smarty->assign($name."ACL",$this->getacl($name));
134 }
136 $display= "";
138 $smarty->assign("staticAddress", "");
139 $smarty->assign("autonet", "");
141 /* Check for autonet button */
142 if ($this->autonet && isset($_POST['autonet'])){
143 $cmd= $this->config->data['MAIN']['AUTO_NETWORK_HOOK'];
144 if(!empty($cmd) && $this->cn != ""){
145 $res = shell_exec($cmd." ".$this->cn);
146 if(!$res){
147 print_red(sprintf(_("Can't execute specified AUTO_NETWORK_HOOK '%s'. Please check your gosa.conf."),$cmd));
148 } else {
149 $res= split(';', trim($res));
150 if (isset($res[0]) && $res[0] != ""){
151 $this->ipHostNumber= $res[0];
152 }
153 if (isset($res[1]) && $res[1] != ""){
154 $this->macAddress= $res[1];
155 }
156 }
157 }
158 }
160 /* There is no dns available
161 */
162 if($this->DNSenabled == false){
164 /* Is IP address must ? */
165 $smarty->assign("DNS_enabled",false);
166 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
168 /* Assign smarty all non DNs attributes */
169 foreach($this->attributes as $attr){
170 $smarty->assign($attr,$this->$attr);
171 }
172 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
174 if ($this->autonet){
175 $smarty->assign("autonet", "true");
176 } else {
177 $smarty->assign("autonet", "");
178 }
179 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
180 return($display);
181 }else{
182 $smarty->assign("DNS_enabled",true);
183 }
185 $accountACL = false;
186 if($this->DNS_is_account && $this->acl_is_removeable()){
187 $accountACL = true;
188 }elseif(!$this->DNS_is_account && $this->acl_is_createable()){
189 $accountACL = true;
190 }
191 $smarty->assign("accountACL",$accountACL);
193 /* Add new empty array to our record list */
194 if(isset($_POST['AddNewRecord'])){
195 $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>"");
196 }
198 /* Handle all posts */
199 $only_once =true;
200 foreach($_POST as $name => $value){
202 /* Check if we have to delete a record entry */
203 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
205 /* Avoid performing this once again */
206 $only_once = false;
208 /* Extract id for specified entry */
209 $id = preg_replace("/RemoveRecord_/","",$name);
210 $id = preg_replace("/_.*$/","",$id);
212 /* Delete this record, mark edited entries to be able to delete them */
213 if(isset($this->dnsEntry['RECORDS'][$id])){
214 unset($this->dnsEntry['RECORDS'][$id]);
215 }
216 }
217 }
219 /* Assign smarty all non DNs attributes */
220 foreach($this->attributes as $attr){
221 $smarty->assign($attr,$this->$attr);
222 }
224 /* Assign smarty all DNS attributes */
225 foreach($this->DNSattributes as $attr){
226 $smarty->assign($attr,$this->dnsEntry[$attr]);
227 }
229 /* Assign all needed vars */
230 $smarty->assign("DNS_is_account",$this->DNS_is_account);
231 $smarty->assign("Zones",$this->Zones);
232 $smarty->assign("ZoneKeys",($this->Zones));
233 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
235 /* Create zone array */
236 $idZones = array();
237 foreach($this->Zones as $id => $zone){
238 if($this->netmaskIsCoherent($id)) {
239 $idZones[$id] = $zone;
240 }else{
241 $idZones[$id] = $zone." ("._("Not matching").")";
242 }
243 }
244 $smarty->assign("Zones",$idZones);
245 $smarty->assign("ZoneKeys", $this->Zones);
247 $tmp = $this->generateRecordsList();
249 $changeStateForRecords = $tmp['changeStateForRecords'];
251 $smarty->assign("records",$tmp['str']);
252 $smarty->assign("changeStateForRecords",$changeStateForRecords);
253 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
255 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
256 return($display);
257 }
260 function remove_from_parent()
261 {
262 if($this->initially_was_account){
264 $ldap = $this->config->get_ldap_link();
266 $tmp = array();
267 $this->dnsEntry['exists'] = false;
268 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
270 /* Delete dns */
271 foreach($tmp['del'] as $dn => $del){
272 $ldap->cd($dn);
273 $ldap->rmdir_recursive($dn);
274 new log("remove","unknown/".get_class($this),$dn);
275 }
276 }
277 }
280 /* Save data to object */
281 function save_object()
282 {
283 /* Save all posted vars */
284 plugin::save_object();
286 if(isset($_POST['network_tpl_posted'])){
288 /* Ge all non dns attributes (IP/MAC)*/
289 foreach($this->attributes as $attr){
290 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
291 $this->$attr = $_POST[$attr];
292 }
293 }
295 /* Check if DNS should be enabled / disabled */
296 if($this->DNS_is_account && $this->acl_is_removeable() && !isset($_POST['DNS_is_account'])){
297 $this->DNS_is_account = false;
298 }elseif(!$this->DNS_is_account && $this->acl_is_createable() && isset($_POST['DNS_is_account'])){
299 $this->DNS_is_account = true;
300 }
302 /* Get dns attributes */
303 if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
305 /* Check for posted record changes */
306 if(is_array($this->dnsEntry['RECORDS']) && $this->acl_is_writeable("Records")){
307 foreach($this->dnsEntry['RECORDS'] as $key => $value){
309 /* Check if type has changed */
310 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
311 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
312 }
313 /* Check if value has changed */
314 if(isset($_POST['RecordValue_'.$key])){
315 $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
316 }
317 }
318 }
319 /* Get all basic DNS attributes (TTL, Clas ..)*/
320 foreach($this->DNSattributes as $attr){
321 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
322 $this->dnsEntry[$attr] = $_POST[$attr];
323 }
324 }
327 }
328 }
329 }
332 /* Check supplied data */
333 function check()
334 {
335 /* Call common method to give check the hook */
336 $message= plugin::check();
338 /* Check if ip must be given
339 */
340 if(($this->IPisMust)||($this->DNS_is_account)){
341 if (empty($this->ipHostNumber)){
342 $message[]= _("The required field 'IP-address' is not set.");
343 }
345 if (!is_ip($this->ipHostNumber)){
346 $message[]= _("Wrong IP format in field IP-address.");
347 }
348 }
350 /* Check if mac is empty
351 */
352 if ($this->macAddress == "" ){
353 $message[]= _("The required field 'MAC-address' is not set.");
354 }
355 if(!is_mac($this->macAddress)){
356 $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
357 }
359 /* only perfrom this checks if this is a valid DNS account */
360 if($this->DNS_is_account){
362 $checkArray = array();
363 $onlyOnce = array();
365 // $onlyOnce['cNAMERecord'] = 0;
366 $tmp = array_flip($this->Zones);
367 $tmp2 = $tmp[$this->dnsEntry['zoneName']];
368 if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
369 $tmp2 = preg_replace("/^.*\//","",$tmp2);
370 $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
371 }
373 /* Walk through all entries and detect duplicates or mismatches
374 */
375 foreach($this->dnsEntry['RECORDS'] as $name => $values){
377 /* Count record values, to detect duplicate entries for a specific record
378 */
379 if(!isset($checkArray[$values['type']][$values['value']])){
380 $checkArray[$values['type']][$values['value']] = 0;
381 }else{
382 $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
383 }
385 /* Check if given entries in $onlyOnce are used more than once
386 */
387 if(isset($onlyOnce[$values['type']])){
388 $onlyOnce[$values['type']] ++;
389 if($onlyOnce[$values['type']] > 1){
390 $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
391 }
392 }
394 /* Skip txt record ...
395 */
396 if($values['type'] == "tXTRecord") continue;
398 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
399 */
400 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
401 $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."),
402 $this->ipHostNumber);
403 }
405 /* only lower-case is allowed in record entries ...
406 */
407 if($values['value'] != strtolower($values['value'])){
408 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
409 }
410 }
411 }
412 return ($message);
413 }
416 /* Save to LDAP */
417 function save($dn)
418 {
419 $ldap= $this->config->get_ldap_link();
421 /*******************/
422 /* IP-MAC HANDLING */
423 /*******************/
425 /* $dn was posted as parameter */
426 $this->dn = $dn;
428 /* Save DNS setting & ip/Mac*/
429 plugin::save();
431 /* Write back to ldap */
432 $ldap->cd($this->dn);
433 $this->cleanup();
434 $ldap->modify ($this->attrs);
436 /****************/
437 /* DNS HANDLING */
438 /****************/
440 /* If isn't DNS account but initially was DNS account
441 remove all DNS entries
442 */
443 if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
444 return;
445 }else{
447 /* Add ipHostNumber to aRecords
448 */
449 $backup_dnsEntry = $this->dnsEntry;
450 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
452 /* Create diff and follow instructions
453 * If Account was disabled, remove account by setting exists to false
454 */
455 if((!$this->DNS_is_account)&&($this->initially_was_account)){
456 $this->dnsEntry['exists'] = false;
457 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
458 }else{
459 $this->dnsEntry['exists'] = $this->DNS_is_account;
460 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
461 }
463 /* move follwoing entries
464 */
465 foreach($tmp['move'] as $src => $dst){
466 $this->recursive_move($src,$dst);
467 }
469 /* Delete dns */
470 foreach($tmp['del'] as $dn => $del){
471 $ldap->cd($dn);
472 $ldap->rmdir_recursive($dn);
473 }
475 /* Add || Update new DNS entries
476 */
477 foreach($tmp['add'] as $dn => $attrs){
478 $ldap->cd($dn);
479 $ldap->cat($dn, array('dn'));
480 if(count($ldap->fetch())){
481 $ldap->cd($dn);
482 $ldap->modify ($attrs);
483 }else{
484 $ldap->cd($dn);
485 $ldap->add($attrs);
486 }
487 }
489 new log("modify","unknown/".get_class($this),$dn,array_keys($attrs),$ldap->get_error());
491 /* Display errors
492 */
493 if($ldap->get_error() != "Success"){
494 show_ldap_error($ldap->get_error(), sprintf(_("Saving of terminal/dns account with dn '%s' failed."),$this->dn));
495 }
497 $this->dnsEntry = $backup_dnsEntry;
498 }
499 }
501 /* Create html table with all used record types
502 */
503 function generateRecordsList()
504 {
505 $changeStateForRecords = "";
507 if(!$this->DNS_is_account) {
508 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
509 return $str;
510 }
512 $str = "<table summary='' width='100%'>";
513 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
515 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
516 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
517 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
519 $str.=" <tr>".
520 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
521 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
522 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
523 "</tr>";
524 }
526 $str.= " <tr>".
527 " <td colspan=2 width='50%'></td><td>".
528 " <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
529 " </td>".
530 " </tr>".
531 "</table>";
532 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
533 return($ret);
534 }
537 /* Create a html select box which allows us to select different types of records
538 */
539 function generateRecordListBox($selected,$name)
540 {
541 $str = "<select name='".$name."' id='".$name."'>";
542 foreach($this->RecordTypes as $type => $value){
543 $use = "";
544 if($type == $selected){
545 $use = " selected ";
546 }
547 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
548 }
549 $str.="</select>";
550 return($str);
551 }
554 /* Return plugin informations for acl handling */
555 function plInfo()
556 {
557 $tmp = array(
558 "plShortName" => _("DNS"),
559 "plDescription" => _("DNS settings"),
560 "plSelfModify" => FALSE,
561 "plDepends" => array(),
562 "plPriority" => 5,
563 "plSection" => array("administration"),
564 "plCategory" => array("workstation","terminal","phone","server","component","printer","winworkstation"),
566 "plProvidedAcls"=> array(
567 "ipHostNumber" => _("IP address"),
568 "macAddress" => _("MAC address"))
569 );
571 /* Hide all dns specific code, if dns is not available
572 */
573 $DNSenabled = false;
574 foreach($_SESSION['config']->data['TABS']['SERVERSERVICE'] as $tab){
575 if(preg_match("/^servdns$/",$tab['CLASS'])){
576 $tmp['plProvidedAcls']["Records"] = _("DNS records");
577 $tmp['plProvidedAcls']["zoneName"] = _("Zone name");
578 $tmp['plProvidedAcls']["dNSTTL"] = _("TTL");
579 }
580 }
581 return($tmp);
582 }
583 }
585 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
586 ?>