1 <?php
3 class termDNS extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage server basic objects";
7 var $cli_description= "Some longer text\nfor help";
8 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account= TRUE;
12 var $DNSattributes = array("dNSClass","zoneName","dNSTTL");
13 var $attributes= array("ipHostNumber","macAddress");
14 var $objectclasses= array("whatever");
16 var $ipHostNumber =""; // IP address
17 var $macAddress =""; // Mac address
18 var $cn =""; // CN of currently edited device
20 var $Zones = array(); // All Available Zones like array("3.2.1"=>"MyServer.de")
21 var $RecordTypes= array(); // Possible record types
23 var $dNSClass = "IN"; // dNSClass name
24 var $zoneName = ""; // Used ZoneName
25 var $dNSTTL = 7200; // TTL settings for the created entries
27 /* Used records */
28 var $types = array();
29 var $DNSinitially_was_account = false;
31 var $orig_dn ="";
33 var $IPisMust = false;
34 var $MACisMust= false;
36 var $found = false;
38 function termDNS ($config, $dn,$objectClasses,$IPisMust = false)
39 {
40 /* We need to know which objectClasses are used, to store the ip/mac*/
41 $this->objectclasses= $objectClasses;
42 plugin::plugin ($config, $dn);
44 $this->orig_dn= $dn;
46 $this->IPisMust = $IPisMust;
48 /* Hide all dns specific code, if dns is not available
49 */
50 $found = false;
51 foreach($this->config->data['TABS']['SERVTABS'] as $tab){
52 if(preg_match("/^servdns$/",$tab['CLASS'])){
53 $found = true;
54 }
55 }
56 $this->found = $found;
57 if(!$this->found){
58 $this->DNS_is_account = false;
59 return;
60 }
63 /* All types with required attrs */
64 $this->RecordTypes['aRecord'] = "aRecord"; // ok
65 $this->RecordTypes['mDRecord'] = "mDRecord"; // ok
66 $this->RecordTypes['mXRecord'] = "mXRecord"; // ok
67 $this->RecordTypes['nSRecord'] = "nSRecord"; // ok
68 $this->RecordTypes['pTRRecord'] = "relativeDomainName";// ok
69 $this->RecordTypes['hInfoRecord'] = "hInfoRecord"; // ok
70 $this->RecordTypes['mInfoRecord'] = "mInfoRecord"; // ok
71 $this->RecordTypes['cNAMERecord'] = "relativeDomainName";// ok
72 $this->RecordTypes['tXTRecord'] = "tXTRecord"; // ok
73 $this->RecordTypes['aFSDBRecord'] = "aFSDBRecord"; // ok
74 $this->RecordTypes['SigRecord'] = "SigRecord"; // ok
75 $this->RecordTypes['KeyRecord'] = "KeyRecord"; // ok
76 $this->RecordTypes['aAAARecord'] = "aAAARecord"; // ok
77 $this->RecordTypes['LocRecord'] = "LocRecord"; // ok
78 $this->RecordTypes['nXTRecord'] = "nXTRecord"; // ok
79 $this->RecordTypes['sRVRecord'] = "sRVRecord"; // ok
80 $this->RecordTypes['nAPTRRecord'] = "nAPTRRecord"; // ok
81 $this->RecordTypes['kXRecord'] = "kXRecord"; // ok
82 $this->RecordTypes['certRecord'] = "certRecord"; // ok
83 $this->RecordTypes['a6Record'] = "a6Record"; // ok
84 $this->RecordTypes['dSRecord'] = "dSRecord"; // ok
85 $this->RecordTypes['sSHFPRecord'] = "sSHFPRecord"; // ok
86 $this->RecordTypes['rRSIGRecord'] = "rRSIGRecord"; // ok
87 $this->RecordTypes['nSECRecord'] = "nSECRecord"; // ok
89 /* Get all available zones */
90 if(empty($this->cn)&&(isset($this->attrs['cn'][0]))){
91 $this->cn = $this->attrs['cn'][0];
92 }
93 $this->Zones = $this->get_Zones();
94 $types = array();
96 /* Get all records */
97 $ldap = $this->config->get_ldap_link();
98 $ldap->cd($this->dn);
99 $ldap->ls("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",$this->dn,array("*"));
101 $found = false;
103 while($attrs = $ldap->fetch()){
104 /* If relative domainname == cn
105 * Try to read dnsclass / TTl / zone
106 */
108 if($attrs['relativeDomainName'][0] == $this->cn){
109 /* Get class */
110 if(isset($attrs['dNSClass'][0])){
111 $this->dNSClass = $attrs['dNSClass'][0];
112 $found = true;
113 }
114 /* Get Zone*/
115 if(isset($attrs['zoneName'][0])){
116 $this->zoneName = $attrs['zoneName'][0];
117 $found = true;
118 }
119 /* Get ttl */
120 if(isset($attrs['dNSTTL'][0])){
121 $this->dNSTTL = $attrs['dNSTTL'][0];
122 $found = true;
123 }
124 }
126 /* Create list with all used records */
127 foreach($this->RecordTypes as $name => $value){
129 /* If there is a record attribute */
130 if(isset($attrs[$name])){
133 /* get all entries */
134 for($i = 0 ; $i < $attrs[$value]['count']; $i ++){
135 if(($value == "aRecord")&&($this->ipHostNumber==$attrs[$value][$i])){
136 continue;
137 }
138 $types[] =array("type"=>$name,"inittype"=>$name,"value"=>$attrs[$value][$i],"status"=>"edited","dn"=>$attrs['dn']);
139 }
140 }
141 }
142 }
144 /* If there is at least one entry in this -> types, we have DNS enabled */
145 $this->types = $types;
146 if((count($this->types) == 0) &&($found == false)){
147 $this->DNS_is_account = false;
148 }else{
149 $this->DNS_is_account = true;
150 }
152 /* Store initally account settings */
153 $this->DNSinitially_was_account = $this->DNS_is_account;
154 }
156 function execute()
157 {
158 /* Call parent execute */
159 $smarty= get_smarty();
160 $display= "";
162 $smarty->assign("staticAddress", "");
164 /* There is no dns available
165 */
166 if($this->found == false){
168 $smarty->assign("DNS_is_account",false);
169 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
171 /* Assign smarty all non DNs attributes */
172 foreach($this->attributes as $attr){
173 $smarty->assign($attr,$this->$attr);
174 }
175 $smarty->assign("staticAddress", "");
176 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
177 return($display);
178 }else{
179 $smarty->assign("DNS_is_account",true);
180 }
182 /* Add new empty array with status new, to our record list */
183 if(isset($_POST['AddNewRecord'])){
184 $this->types[] =array("type"=>"aRecord","value"=>"","status"=>"new");
185 }
187 /* Handle all posts */
188 $only_once =true;
189 foreach($_POST as $name => $value){
191 /* Check if we have to delete a record entry */
192 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
194 /* Avoid performing this once again */
195 $only_once = false;
197 /* Extract id for specified entry */
198 $id = preg_replace("/RemoveRecord_/","",$name);
199 $id = preg_replace("/_.*$/","",$id);
201 /* Delete this record, mark edited entries to be able to delete them */
202 if(isset($this->types[$id])){
203 if($this->types[$id]['status'] == "edited"){
204 $this->types[$id]['status'] = "deleted";
205 }else{
206 unset($this->types[$id]);
207 }
208 }
209 }
210 }
212 /* Assign smarty all non DNs attributes */
213 foreach($this->attributes as $attr){
214 $smarty->assign($attr,$this->$attr);
215 }
217 /* Assign smarty all DNS attributes */
218 foreach($this->DNSattributes as $attr){
219 $smarty->assign($attr,$this->$attr);
220 }
222 /* Assign all needed vars */
223 $smarty->assign("DNSAccount",$this->DNS_is_account);
224 $smarty->assign("Zones",$this->Zones);
225 $smarty->assign("ZoneKeys",($this->Zones));
226 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
227 $changeStateForRecords ="";
228 $smarty->assign("records",$this->generateRecordsList(&$changeStateForRecords));
229 $smarty->assign("changeStateForRecords",$changeStateForRecords);
230 // $smarty->assign("dNSClasses",array("IN"=>"IN"));
231 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
232 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
233 return($display);
234 }
236 function remove_from_parent()
237 {
238 $ldap = $this->config->get_ldap_link();
239 $ldap->cd($this->orig_dn);
240 $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("relativeDomainName","zoneName"));
241 while($attr = $ldap->fetch()){
242 $ldap->cd($attr['dn']);
243 $ldap->rmDir($attr['dn']);
244 }
245 }
247 /* Save data to object */
248 function save_object()
249 {
250 /* Save all posted vars */
251 plugin::save_object();
253 /* Ge all non dns attributes (IP/MAC)*/
254 foreach($this->attributes as $attr){
255 if(isset($_POST[$attr])){
256 $this->$attr = $_POST[$attr];
257 }
258 }
260 /* Get dns attributes */
261 if(isset($_POST['network_tpl_posted'])){
263 /* Check for posted record changes */
264 foreach($this->types as $key => $value){
266 /* Check if type has changed */
267 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
268 $this->types[$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
269 }
270 /* Check if value has changed */
271 if(isset($_POST['RecordValue_'.$key])){
272 $this->types[$key]['value'] = $_POST['RecordValue_'.$key];
273 }
274 }
276 /* Get all basic DNS attributes (TTL, Clas ..)*/
277 foreach($this->DNSattributes as $attr){
278 if(isset($_POST[$attr])){
279 $this->$attr = $_POST[$attr];
280 }
281 }
283 /* Enable diable DNS */
284 if(isset($_POST['enableDNS'])){
285 $this->DNS_is_account = true;
286 }else{
287 $this->DNS_is_account = false;
288 }
289 }
290 }
293 /* Check supplied data */
294 function check()
295 {
296 $message= array();
299 if(($this->IPisMust)||($this->DNS_is_account)){
300 /* Check if ip is empty */
301 if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
302 $message[]= _("The required field 'IP-address' is not set.");
303 }
305 /* check if given ip is valid ip*/
306 $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
307 if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
308 $message[]= _("Wrong IP format in field IP-address.");
309 }
310 }
312 /* Check if mac is empty */
313 if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
314 $message[]= _("The required field 'MAC-address' is not set.");
315 }
317 /* Check if given mac is valid mac */
318 $tr = count(split(":",$this->macAddress));
319 if($tr!=6){
320 $message[]=(_("The given macaddress is invalid. There must be 6 1byte segments seperated by ':'."));
321 }
323 /* only perfrom this checks if this is a valid DNS account */
324 if($this->DNS_is_account){
326 $checkArray = array();
327 $onlyOnce = array();
328 $onlyOnce['cNAMERecord'] = 0;
330 foreach($this->types as $name => $values){
332 if(!isset($checkArray[$values['type']][$values['value']])){
333 $checkArray[$values['type']][$values['value']] = 0;
334 }else{
335 $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
336 }
338 if(isset($onlyOnce[$values['type']])){
339 $onlyOnce[$values['type']] ++;
340 if($onlyOnce[$values['type']] > 1){
341 $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
342 }
343 }
345 if($values['type'] == "tXTRecord") continue;
347 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress */
348 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)&&($values['status']!="deleted")){
349 $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."),
350 $this->ipHostNumber);
351 }
353 /* only lower-case is allowed in record entries ... */
354 if($values['value'] != strtolower($values['value'])){
355 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
356 }
357 }
358 }
359 return ($message);
360 }
363 /* Save to LDAP */
364 function save($dn)
365 {
366 $ldap= $this->config->get_ldap_link();
368 /*******************/
369 /* IP-MAC HANDLING */
370 /*******************/
372 /* $dn was posted as parameter */
373 $this->dn = $dn;
375 /* Save DNS setting & ip/Mac*/
376 plugin::save();
378 /* Write back to ldap */
379 $ldap->cd($this->dn);
380 $this->cleanup();
381 $ldap->modify ($this->attrs);
384 /****************/
385 /* DNS HANDLING */
386 /****************/
388 /* If isn't DNS account but initially was DNS account
389 remove all DNS entries
390 */
391 if(!$this->DNS_is_account){
392 if($this->DNSinitially_was_account){
393 $tmp = array();
395 $ldap->ls("(&(objectClass=dNSZone)(!(relativeDomainName=@)))",$this->dn,array("relativeDomainName"));
396 while($attrs = $ldap->fetch()){
397 $dn=$attrs['dn'];
398 $ldap->cd($dn);
399 $ldap->rmdir_recursive($dn);
400 }
401 }
402 }else{
404 /* DNS is enabled, check what we have to do */
405 $delete = array();
407 /* Generate a list of new ldap entries,
408 & $delete contains all dns which should be deleted
409 */
410 $entries = $this->generate_LDAP_entries(&$delete);
412 /* Delete dns */
413 foreach($delete as $dn => $del){
414 $ldap->cd($dn);
415 $ldap->rmDir($dn);
416 }
418 /* Add || Update new DNS entries */
419 foreach($entries as $dn => $attrs){
420 $ldap->cd($dn);
421 $ldap->cat($dn);
423 if(count($ldap->fetch())){
424 $ldap->cd($dn);
425 // $this->cleanup();
426 $ldap->modify ($attrs);
428 }else{
429 $ldap->cd($dn);
430 $ldap->add($attrs);
431 }
432 }
433 }
434 if($ldap->get_error() != "Success"){
435 show_ldap_error($ldap->get_error());
436 }
438 }
440 /* Create html table with all used record types
441 */
442 function generateRecordsList($changeStateForRecords)
443 {
444 $changeStateForRecords = "";
446 if(!$this->DNS_is_account) {
447 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled>";
448 return $str;
449 }
451 $str = "<table summary='' width='100%'>";
452 foreach($this->types as $key => $entry){
453 if($entry['status'] == "deleted") continue;
455 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
456 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
457 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
459 $str.=" <tr>".
460 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
461 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
462 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
463 "</tr>";
464 }
466 $str.= " <tr>".
467 " <td colspan=2 width='50%'></td><td>".
468 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
469 " </td>".
470 " </tr>".
471 "</table>";
472 return($str);
473 }
475 /* Create a html select box which allows us to select different types of records */
476 function generateRecordListBox($selected,$name)
477 {
478 $str = "<select name='".$name."' id='".$name."'>";
479 foreach($this->RecordTypes as $type => $value){
480 $use = "";
481 if($type == $selected){
482 $use = " selected ";
483 }
484 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
485 }
486 $str.="</select>";
487 return($str);
488 }
490 /* return all Zone names */
491 function get_Zones()
492 {
493 $ret = array();
494 $ldap = $this->config->get_ldap_link();
495 $ldap-> cd ($this->config->current['BASE']);
496 $ldap->search("(&(objectClass=dNSZone)(sOARecord=*))",array("*"));
498 while($at = $ldap->fetch()){
499 if(preg_match("/\.in\-addr\.arpa/",$at['zoneName'][0])){
500 $name = preg_replace("/^zoneName=/","",$at['tXTRecord'][0]);
501 $ret[$name]['addr']= $at['zoneName'][0];
502 }else{
503 $name = $at['zoneName'][0];
504 $ret[$name]['name']= $at['zoneName'][0];
505 }
506 }
508 $tmp =array();
509 foreach($ret as $name => $entry){
510 if((isset($entry['addr']))&&(isset($entry['name']))){
511 $tmp[$entry['addr']]=$entry['name'];
512 }
513 }
514 $ret = $tmp;
515 return($ret);
516 }
518 /* this is used to generate ldap friendly output of our
519 dns configuration
520 */
521 function generate_LDAP_entries($delete)
522 {
523 $entries = array();
524 $delete = array();
526 /* Generate Main Entry */
527 $dn = "relativeDomainName=".$this->cn.",".$this->dn;
528 $entries[$dn]['dNSClass'] = $this->dNSClass;
529 $entries[$dn]['zoneName'] = $this->zoneName;
530 $entries[$dn]['dNSTTL'] = $this->dNSTTL;
531 $entries[$dn]['relativeDomainName'] = $this->cn;
533 /* Generate cNAMERecord */
534 $aRecords = array();
535 foreach($this->types as $type){
536 if($type['type'] == "cNAMERecord"){
538 $Cdn = "relativeDomainName=".$type['value'].",".$this->dn;
539 if($type['status']=="deleted"){
540 $delete [$type['dn']] = $Cdn;
541 }else{
542 $entries[$Cdn] = $entries[$dn];
543 $entries[$Cdn]['relativeDomainName'] = $type['value'];
544 $entries[$Cdn]['cNAMERecord'] = $this->cn.".".$this->zoneName;
545 }
546 }
547 }
549 /* Generate tXTRecord */
550 $aRecords = array();
551 foreach($this->types as $type){
552 if(($type['type'] == "tXTRecord")&&($type['status']!="deleted")){
553 $entries[$dn]['tXTRecord'][] = $type['value'];
554 }
555 }
557 /* Generate mDRecord */
558 $aRecords = array();
559 foreach($this->types as $type){
560 if(($type['type'] == "mDRecord")&&($type['status']!="deleted")){
561 $entries[$dn]['mDRecord'][] = $type['value'];
562 }
563 }
565 /* Generate mXRecord */
566 $aRecords = array();
567 foreach($this->types as $type){
568 if(($type['type'] == "mXRecord")&&($type['status']!="deleted")){
569 $entries[$dn]['mXRecord'][] = $type['value'];
570 }
571 }
573 /* Generate hInfoRecord */
574 $aRecords = array();
575 foreach($this->types as $type){
576 if(($type['type'] == "hInfoRecord")&&($type['status']!="deleted")){
577 $entries[$dn]['hInfoRecord'][] = $type['value'];
578 }
579 }
581 /* Generate mInfoRecord */
582 $aRecords = array();
583 foreach($this->types as $type){
584 if(($type['type'] == "mInfoRecord")&&($type['status']!="deleted")){
585 $entries[$dn]['mInfoRecord'][] = $type['value'];
586 }
587 }
589 /* Generate aFSDBRecord */
590 $aRecords = array();
591 foreach($this->types as $type){
592 if(($type['type'] == "aFSDBRecord")&&($type['status']!="deleted")){
593 $entries[$dn]['aFSDBRecord'][] = $type['value'];
594 }
595 }
597 /* Generate some attrs */
598 $arr = array("SigRecord","KeyRecord","aAAARecord","nSRecord",
599 "LocRecord","nXTRecord","sRVRecord","nAPTRRecord","kXRecord","certRecord","a6Record","dSRecord","sSHFPRecord","rRSIGRecord","nSECRecord");
600 $aRecords = array();
601 foreach($arr as $ar){
602 foreach($this->types as $type){
603 if(($type['type'] == $ar)&&($type['status']!="deleted")){
604 $entries[$dn][$ar][] = $type['value'];
605 }
606 }
607 }
610 /* Generate A Records (IP Address relation) */
611 $aRecords = array();
612 foreach($this->types as $type){
613 if(($type['type'] == "aRecord")&&($type['status']!="deleted")){
614 $aRecords[] = $type['value'];
615 }
616 }
617 if(count($aRecords)){
619 /* Add ipHostNumber as default aRecord */
620 $aRecords[] = $this->ipHostNumber;
622 $dn = "relativeDomainName=".$this->cn.",".$this->dn;
623 foreach($aRecords as $rec){
624 $entries[$dn]['aRecord'][] = $rec;
625 }
626 }
628 /* Generate pTRRecord Records */
629 foreach($this->types as $type){
630 if($type['type'] == "pTRRecord"){
631 $PTRdn= "relativeDomainName=".$type['value'].",".$this->dn;
632 if($type['status']=="deleted"){
633 $delete [$type['dn']] = $PTRdn;
634 }else{
635 $zones = array_flip($this->Zones);
636 $zone = $zones[$this->zoneName];
637 $entries[$PTRdn]['relativeDomainName'] = $type['value'];
638 $entries[$PTRdn]['pTRRecord'] = $this->cn.".".$this->zoneName;
639 $entries[$PTRdn]['zoneName'] = $zone;
640 }
641 }
642 }
644 /* add ObjectClasses */
645 foreach($entries as $key => $entry ){
646 $entries[$key]['objectClass']=array("top","dNSZone");
647 $entries[$key] = array_reverse($entries[$key]);
648 }
650 /* Check if record type has changed, and if we need to delete this record attribute from ldap entry */
651 foreach($this->types as $type){
652 if(isset($type['inittype'])){
653 if(!isset($entries[$dn][$type['inittype']])){
654 $entries[$dn][$type['inittype']] = array();
655 }
656 }
657 }
659 return($entries);
660 }
661 }
663 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
664 ?>