Code

89d2f3f367ea5d8eee7d4e883ce540ff13a763bf
[gosa.git] / plugins / admin / systems / class_servDNS.inc
1 <?php
3 require_once("class_goService.inc");
5 class servdns extends goService
6 {
7   /* CLI vars */
8   var $cli_summary= "Manage server basic objects";
9   var $cli_description= "Some longer text\nfor help";
10   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
12   /* attribute list for save action */
13   var $ignore_account   = FALSE;
14   var $attributes       = array(); 
15   var $objectclasses    = array("whatever");
17   var $RecordTypes      = array();
18   var $Zones            = array();
19   var $dialog           = NULL;
21   var $orig_dn          = "";
23   var $initially_was_account;
24   
25   /* ServerService tab vars */
26   var $conflicts        = array("servdns");
27   var $DisplayName      = "";
28   var $StatusFlag       = "";
29   var $view_logged      = FALSE;
31   var $dns_server_list   = array("ENTRIES"=> array(),"FOR_LIST"=> array());
32   var $take_over_id       = -1;
35   function servdns ($config, $dn= NULL, $parent= NULL)
36   {
37     plugin::plugin ($config, $dn, $parent);
39     $this->DisplayName = _("DNS service");
41     $this->orig_dn = $dn;
43     /* Get record types for zones
44      */
45     $this->RecordTypes = getDnsRecordTypes(true);
47     /* Get all zone Informations
48      */
49     $this->Zones = getDNSZoneEntries($config,$dn);
50     
51     /* If there is at least one entry in this -> types, we have DNS enabled 
52      */
53     if(count($this->Zones) == 0){
54       $this->is_account = false;
55       $this->dns_server_list = $this->get_list_of_dns_servers();
56     }else{
57       $this->is_account = true;
58     }
59     $this->initially_was_account = $this->is_account;
60   }
63   function get_list_of_dns_servers()
64   {
65     $ret = array("ENTRIES"=> array(),"FOR_LIST"=> array());
66     $ldap = $this->config->get_ldap_link();
67     $ldap->cd($this->config->current['BASE']);
68     $ldap->search("(&(objectClass=dNSZone)(zoneName=*))",array("dn","zoneName"));
69     $dns = array();
70     while($attrs = $ldap->fetch()){
71       /* Skip own config */
72       if($this->dn != "new" && preg_match("/".normalizePreg($this->dn)."$/",$attrs['dn'])){
73         continue;
74       }
75       $dn = preg_replace("/^zoneName=[^,]+,/","",$attrs['dn']);
76       if(preg_match("/^cn=/",$dn) && !in_array($dn,$dns)){
77         $dns[] = $dn;
78       }
79     }
80     $i = 0;
81     foreach($dns as $dn){
82       $ldap->cat($dn,array('*'));
83       if($ldap->count()){
84         $i ++;
85         $attrs = $ldap->fetch();
86           $ret['ENTRIES'][$i]   = $attrs;
87           $ret['FOR_LIST'][$i] = $attrs['cn'][0];
88       }
89     }
90     return($ret);
91   }  
94   function get_dns_info_string($id)
95   {
96     $ret="";
97     $ldap = $this->config->get_ldap_link();
98     $ldap->cd($this->dns_server_list['ENTRIES'][$id]['dn']);
99     $ldap->search("(|(zoneName=*)(relativeDomainName=*))",array("dn"));
100     while($attrs = $ldap->fetch()){
101       $ret .= $attrs['dn']."\n";
102     }
103     return($ret);
104   }
107   function execute()
108   {
109     /* Call parent execute 
110      */
111     plugin::execute();
113     if($this->is_account && !$this->view_logged){
114       $this->view_logged = TRUE;
115       new log("view","server/".get_class($this),$this->dn);
116     }
118     /* Fill templating stuff 
119      */
120     $smarty= get_smarty();
121     $smarty->assign("dns_take_over",FALSE);
122     $smarty->assign("is_createable",$this->acl_is_createable());
123     $display= "";
126     $this->initially_was_account= $this->is_account;
127     /*****************/
128     /* Handle Take Over Actions
129     /*****************/
131     /* Give smarty the required informations */
132     $smarty->assign("dns_server_list", $this->dns_server_list['FOR_LIST']);
133     $smarty->assign("dns_server_list_cnt", count($this->dns_server_list['FOR_LIST']));
135     /* Take over requested, save id */
136     if(isset($_POST['take_over_src']) && isset($_POST['take_over'])){
137       $id = $_POST['take_over_src'];
138       if(isset($this->dns_server_list['ENTRIES'][$id])){
139         $this->take_over_id = $id;
140       }
141     }
143     /* Abort take over action */
144     if(isset($_POST['cancel_take_over'])){
145       $this->dialog =false;
146       $this->take_over_id = -1;
147       $this->dns_server_list = $this->get_list_of_dns_servers();
148     }
150     /* Display informartion about take over that will be started when saving this server
151      *  and hide default dns output
152      */
153     if($this->take_over_id != -1){
154       $this->dialog = FALSE;
155       $id = $this->take_over_id;
156       $info = $this->get_dns_info_string($id);
157       $smarty->assign("dns_take_over",TRUE);
158       $smarty->assign("info",$info);
159       $warning = sprintf(_("You are going to migrate the DNS setup from server '%s'."),$this->dns_server_list['ENTRIES'][$id]['cn'][0]);
160       $warning2 = _("The migration will be startet when you save this system. To cancel this action, use the cancel button below.");
161       $smarty->assign("warning",$warning);
162       $smarty->assign("warning2",$warning2);
163       return($smarty->fetch(get_template_path('servdns.tpl', TRUE)));
164     }
167     /* Do we need to flip is_account state? 
168      */
169     if (isset($_POST['modify_state'])){
170       $this->is_account= !$this->is_account;
171     }
173     if ($this->is_account){
174       $display= $this->show_disable_header(_("Remove DNS service"),
175           _("This server has DNS features enabled. You can disable them by clicking below."));
176     } else {
177       $display= $this->show_enable_header(_("Add DNS service"),
178           _("This server has DNS features disabled. You can enable them by clicking below."));
179       return ($display);
180     }
183     /* Edited or Added zone 
184      */
185     if(isset($_POST['SaveZoneChanges'])){
186       $this->dialog->save_object();
188       /* Check for errors  
189        */
190       if(count($this->dialog->check())){
191         foreach($this->dialog->check() as $msgs){
192           print_red($msgs); 
193         }
194       }else{
195         /* add new/edited zone 
196          */
197         $ret = $this->dialog->save();
198         if(!$this->dialog->isNew){
199           unset($this->Zones[$this->dialog->OldZoneName]);
200         }
201         $this->Zones[$ret['zoneName']] = $ret;
202         $this->dialog = NULL;
203       }
204     }
206     /* Cancel zone edit / new 
207      */
208     if(isset($_POST['CancelZoneChanges'])){
209       $this->dialog = NULL;
210     }
212     /* Add empty new zone 
213      */
214     if(isset($_POST['AddZone'])){
215       $this->dialog = new servdnseditZone($this->config,$this->dn);
216     }
218     /* Check for edit zone request 
219      */
220     $once = false;
221     foreach( $_POST as $name => $value){
222   
223       /* check all post for edit request 
224        */
225       if(preg_match("/^editZone_/",$name)&&!$once){
226         $once =true;
227         $tmp = preg_replace("/^editZone_/","",$name);
228         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
229         $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$tmp]);
230       }
232       /* check posts for delete zone 
233        */
234       if(preg_match("/^delZone_/",$name)&&!$once){
236         $once =true;
237         $tmp = preg_replace("/^delZone_/","",$name);
238         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
239      
240         /* Initiate deletion
241          */ 
242         $this->RemoveZone($tmp); 
243       }
244     }
246     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
247       $id = base64_decode($_GET['id']);
248       if(isset($this->Zones[$id])){
249          $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$id]);
250       }
251     }
253     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
254       $id = base64_decode($_GET['id']);
255       if(isset($this->Zones[$id])){
256          $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$id]);
257       }
258     }
260     /* Show dialog 
261      */
262     if($this->dialog!= NULL){
263       $this->dialog->save_object();
264       $this->dialog->parent = $this;
265       return($this->dialog->execute());
266     }
268     /* Create Listbox with existing Zones 
269      */
270     $ZoneList = new divSelectBox("dNSZones");
271     $ZoneList -> SetHeight(254);
273     /* Add entries to divlist
274      */
275     $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>";
276     if($this->acl_is_removeable()){
277       $editImg.= "<input type='image' src='images/edittrash.png' name='delZone_%s'>";
278     }
280     $link = "<a href='?plug=".$_GET['plug']."&act=edit&id=%s'>%s</a>";
281     foreach($this->Zones as $zone => $values ){
282       $ZoneList->AddEntry(array(
283             array("string" => sprintf($link,base64_encode($zone),($zone))),
284             array("string" => sprintf($link,base64_encode($zone),_("Reverse zone")." : ".($values['ReverseZone']))),
285             array("string" => _("TTL")." : ".$values['sOAttl']),
286             array("string" => _("Class")." : ".$values['dNSClass']),
287             array("string" =>str_replace("%s",base64_encode($zone),$editImg))
288             ));
289     }    
291     /* Display tempalte 
292      */
293     $smarty->assign("ZoneList",$ZoneList->DrawList());
294     $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
295     return($display);
296   }
299   /* Delete specified zone
300    */
301   function RemoveZone($id)
302   {
303     $zones =  $this->getUsedZoneNames();
305     if(isset($this->Zones[$id]['InitialReverseZone'])){
306       $rev = FlipIp($this->Zones[$id]['InitialReverseZone']);
307     }else{
308       $rev = FlipIp($this->Zones[$id]['ReverseZone']);
309     }
311     $zonename = "";
312     if(isset($this->Zones[$id]['InitialzoneName'])){
313       $zonename= $this->Zones[$id]['InitialzoneName'];
314     }
316     $used = array();
318     /* Add Records which use this zoneName
319      */
320     if(isset($zones[$zonename])){
321       $used = array_merge($used,$zones[$zonename]);
322     }
324     /* Add Records which uses this reverse zone
325      */
326     if(isset($zones[$rev.".in-addr.arpa"])){
327       $used = array_merge($used,$zones[$rev.".in-addr.arpa"]);
328     } 
330     /* There are still entries using this configuration
331      *  Abort deletion
332      */
333     if(count($used)){
334       $i = 2;
335       $str ="";
336       foreach($used as $dn){
337         if($i > 0 && !preg_match("/,relativeDomainName=/",$dn)){
338           $i --;
339           $name = preg_replace("/^[^=]+=([^,]*),.*$/","\\1",$dn);
340           $zone = preg_replace("/^.*zoneName=([^,]*),.*$/","\\1",$dn);
341           $str.= $name.".".$zone." ";
342         }
343       }
345       /*  Only show 2 dns in the error message 
346        */
347       if(count($used)> 2) {
348         $str .=" ... ";
349       }
350       print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
351       return(false);
352     }else{
353       unset($this->Zones[$id]);
354       return(true);
355     }
356   } 
359   /* This funtion returns all used Zonenames 
360    */
361   function getUsedZoneNames()
362   {
363     $ret = array();
364     $ldap = $this->config->get_ldap_link();
365     $ldap->cd($this->config->current['BASE']);
366     $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
367     while($attr = $ldap->fetch()){
368       $ret[$attr['zoneName'][0]][] = $attr['dn'];
369     }
370     return($ret);
371   }
374   /* Remove dns service 
375    */
376   function remove_from_parent()
377   {
378     if($this->initially_was_account){
379       $bool = true;
380       $this->is_account = FALSE;
381       foreach($this->Zones as $key => $zone){
382         $bool= $bool & $this->RemoveZone($key);
383       }
385       if($bool){
386         $this->save();
387       }
388       return($bool);
389     }
390   }
393   /* Save to LDAP */
394   function save()
395   {
397     /* Take over handling
398      * - Create list of zones managed by source server 
399      * - Copy ldap entries to destination server 
400      * - Remove old zone entries from source
401      */
402     if($this->take_over_id != -1){
403       $del = array();
404       $id = $this->take_over_id;
405       $src = $this->dns_server_list['ENTRIES'][$id]['dn'];
406       $ldap = $this->config->get_ldap_link(); 
407       $ldap->ls("(objectClass=dnsZone)",$src,array('cn'));
408       while($attrs = $ldap->fetch()){
409         $src_zone = $attrs['dn'];
410         $dst_zone = preg_replace("/".normalizePreg($src)."$/",$this->dn,$src_zone);
411         $res = plugin::recursive_move($src_zone, $dst_zone);
413         if($res){
414           $del [] = $src_zone;
415         }
416       }
417       foreach($del as $src_zone){
418         $ldap->rmdir_recursive($src_zone);
419       }
420       return;
421     }
424     $ldap = $this->config->get_ldap_link();
425     $ldap->cd($this->config->current['BASE']);  
426   
427     /* Get differences 
428      */
429     $old_dn = $this->orig_dn;
430     if($old_dn == "new"){
431       $old_dn = $this->dn;
432     }
434     $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$old_dn);
436     /* Update dns to current object dn */ 
437     $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$old_dn);
438     $tmp2 = array();
439     foreach($tmp as $key1 => $data1){
440       $tmp2[$key1] = array();
441       foreach($data1 as $key2 => $data2){
442         $tmp2[$key1][preg_replace("/".normalizePreg($old_dn)."$/",$this->dn,$key2)] = $data2;
443       }
444     }
445     $tmp = $tmp2;
447     /* Updated zone entries if reverser or forward name has changed  
448      * Must be done before moving entries, else the given dn is invalid
449      */
450     if(isset($tmp['zoneUpdates'])){
451       foreach($tmp['zoneUpdates'] as $dn => $attrs){
452         $ldap->cd($dn);
453         $ldap->modify($attrs);
454         new log("modfiy","unknown/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
455         show_ldap_error($ldap->get_error(), sprintf(_("Updating of system server/dns with dn '%s' failed."),$this->dn));
456       }
457     }
459     /* Delete dns 
460      */
461     foreach($tmp['del'] as $dn => $del){
463       $for = $del['InitialzoneName'];
464       $rev = FlipIp($del['InitialReverseZone']).".in-addr.arpa";
466       $ldap->cd($dn);
467       $ldap->rmdir_recursive($dn);
468       new log("remove","unknown/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
469       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/dns with dn '%s' failed."),$this->dn));
471       /* Handle Post events */
472       if(preg_match("/^zoneName=/",$dn)){
473         $this->handle_post_events("remove",array("dn" => $dn,"zoneName" => $for));
474         $this->handle_post_events("remove",array("dn" => $dn,"zoneName" => $rev));
475       }
476     }
478     /* move follwoing entries
479      */
480     foreach($tmp['move'] as $src => $dst){
481       $this->recursive_move($src,$dst);
482     }
484     /* Add || Update new DNS entries
485      */
486     foreach($tmp['add'] as $dn => $attrs){
487       $ldap->cd($dn);
488       $ldap->cat($dn, array('dn'));
489       if($ldap->fetch()){
490         $ldap->cd($dn);
491         $ldap->modify ($attrs);
492         show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
494         /* Handle Post events */
495         if(preg_match("/^zoneName=/",$dn)){
496           $this->handle_post_events("modify",array("dn" => $dn,"zoneName" => $attrs['zoneName']));
497         }
498       }else{
499         $ldap->cd($dn);
500         $ldap->add($attrs);
501         show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
503         /* Handle Post events */
504         if(preg_match("/^zoneName=/",$dn)){
505           $this->handle_post_events("add",array("dn" => $dn,"zoneName" => $attrs['zoneName']));
506         }
507       }
508     }
509   }
512   /* Directly save new status flag */
513   function setStatus($value)
514   {
515     if($value == "none") return;
516     if(!$this->initially_was_account) return;
517     if(empty($this->StatusFlag)) return;
518     $ldap = $this->config->get_ldap_link();
519     $ldap->cd($this->dn);
520     $ldap->cat($this->dn,array("objectClass"));
521     if($ldap->count()){
523       $tmp = $ldap->fetch();
524       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
525         $attrs['objectClass'][] = $tmp['objectClass'][$i];
526       }
527       $flag = $this->StatusFlag;
528       $attrs[$flag] = $value;
529       $this->$flag = $value;
530       $ldap->modify($attrs);
531       show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for system server/dns with dn '%s' failed."),$this->dn));
532       $this->action_hook();
533     }
534   }
537   function getListEntry()
538   {
539     $fields               = goService::getListEntry(); 
540     $fields['Message']    = _("DNS service");
541     $fields['AllowEdit']  = true;
542     return($fields);
543   }
546   /* Get updates for status flag */
547   function updateStatusState()
548   {
549     if(empty($this->StatusFlag)) return;
551     $attrs = array();
552     $flag = $this->StatusFlag;
553     $ldap = $this->config->get_ldap_link();
554     $ldap->cd($this->cn);
555     $ldap->cat($this->dn,array($flag));
556     if($ldap->count()){
557       $attrs = $ldap->fetch();
558     }
559     if(isset($attrs[$flag][0])){
560       $this->$flag = $attrs[$flag][0];
561     }
562   }
565   /* Return plugin informations for acl handling */
566   function plInfo()
567   {
568     return (array(
569           "plShortName"   => _("DNS service"),
570           "plDescription" => _("DNS service")." ("._("Services").")",
571           "plSelfModify"  => FALSE,
572           "plDepends"     => array(),
573           "plPriority"    => 83,
574           "plSection"     => array("administration"),
575           "plCategory"    => array("server"),
577           "plProvidedAcls"=> array(
578             "zoneName"      =>_("Zone name"),
579             "ReverseZone"   =>_("Reverse zone"),
580             "sOAprimary"    =>_("Primary dns server"),
581             "sOAmail"       =>_("Mail address"),
582             "sOAserial"     =>_("Serial"),
583             "sOArefresh"    =>_("Refresh"),
584             "sOAretry"      =>_("Retry"),
585             "sOAexpire"     =>_("Expire"),
586             "sOAttl"        =>_("TTL"),
587             "zoneRecords"   =>_("Zone records"))
588     ));
589   }
592 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
593 ?>