dfddd446a55efec3b2212ebeb5673bdc38b6da58
1 <?php
3 class servdns extends goService
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 = FALSE;
12 var $attributes = array();
13 var $objectclasses = array("whatever");
15 var $RecordTypes = array();
16 var $Zones = array();
18 var $orig_dn = "";
20 var $initially_was_account;
22 /* ServerService tab vars */
23 var $conflicts = array("servdns");
24 var $DisplayName = "";
25 var $StatusFlag = "";
26 var $view_logged = FALSE;
28 var $dns_server_list = array("ENTRIES"=> array(),"FOR_LIST"=> array());
29 var $take_over_id = -1;
32 function servdns (&$config, $dn= NULL, $parent= NULL)
33 {
34 plugin::plugin ($config, $dn, $parent);
36 $this->DisplayName = _("DNS service");
38 $this->orig_dn = $dn;
40 /* Get record types for zones
41 */
42 $this->RecordTypes = DNS::getDnsRecordTypes(true);
44 /* Get all zone Informations
45 */
46 $this->Zones = DNS::getDNSZoneEntries($config,$dn);
48 /* If there is at least one entry in this -> types, we have DNS enabled
49 */
50 if(count($this->Zones) == 0){
51 $this->is_account = false;
52 $this->dns_server_list = $this->get_list_of_dns_servers();
53 }else{
54 $this->is_account = true;
55 }
56 $this->initially_was_account = $this->is_account;
57 }
60 function get_list_of_dns_servers()
61 {
62 $ret = array("ENTRIES"=> array(),"FOR_LIST"=> array());
63 $ldap = $this->config->get_ldap_link();
64 $ldap->cd($this->config->current['BASE']);
65 $ldap->search("(&(objectClass=dNSZone)(zoneName=*))",array("dn","zoneName"));
66 $dns = array();
67 while($attrs = $ldap->fetch()){
68 /* Skip own config */
69 if($this->dn != "new" && preg_match("/".normalizePreg($this->dn)."$/",$attrs['dn'])){
70 continue;
71 }
72 $dn = preg_replace("/^zoneName=[^,]+,/","",$attrs['dn']);
73 if(preg_match("/^cn=/",$dn) && !in_array($dn,$dns)){
74 $dns[] = $dn;
75 }
76 }
77 $i = 0;
78 foreach($dns as $dn){
79 $ldap->cat($dn,array('*'));
80 if($ldap->count()){
81 $i ++;
82 $attrs = $ldap->fetch();
83 $ret['ENTRIES'][$i] = $attrs;
84 $ret['FOR_LIST'][$i] = $attrs['cn'][0];
85 }
86 }
87 return($ret);
88 }
91 function get_dns_info_string($id)
92 {
93 $ret="";
94 $ldap = $this->config->get_ldap_link();
95 $ldap->cd($this->dns_server_list['ENTRIES'][$id]['dn']);
96 $ldap->search("(|(zoneName=*)(relativeDomainName=*))",array("dn"));
97 while($attrs = $ldap->fetch()){
98 $ret .= $attrs['dn']."\n";
99 }
100 return($ret);
101 }
104 function execute()
105 {
106 /* Call parent execute
107 */
108 plugin::execute();
110 if($this->is_account && !$this->view_logged){
111 $this->view_logged = TRUE;
112 new log("view","server/".get_class($this),$this->dn);
113 }
115 /* Fill templating stuff
116 */
117 $smarty= get_smarty();
118 $smarty->assign("dns_take_over",FALSE);
119 $smarty->assign("is_createable",$this->acl_is_createable());
120 $display= "";
123 $this->initially_was_account= $this->is_account;
124 /*****************/
125 /* Handle Take Over Actions
126 /*****************/
128 /* Give smarty the required informations */
129 $smarty->assign("dns_server_list", $this->dns_server_list['FOR_LIST']);
130 $smarty->assign("dns_server_list_cnt", count($this->dns_server_list['FOR_LIST']));
132 /* Take over requested, save id */
133 if(isset($_POST['take_over_src']) && isset($_POST['take_over'])){
134 $id = $_POST['take_over_src'];
135 if(isset($this->dns_server_list['ENTRIES'][$id])){
136 $this->take_over_id = $id;
137 }
138 }
140 /* Abort take over action */
141 if(isset($_POST['cancel_take_over'])){
142 $this->dialog =false;
143 $this->take_over_id = -1;
144 $this->dns_server_list = $this->get_list_of_dns_servers();
145 }
147 /* Display informartion about take over that will be started when saving this server
148 * and hide default dns output
149 */
150 if($this->take_over_id != -1){
151 $this->dialog = FALSE;
152 $id = $this->take_over_id;
153 $info = $this->get_dns_info_string($id);
154 $smarty->assign("dns_take_over",TRUE);
155 $smarty->assign("info",$info);
156 $warning = sprintf(_("You are going to migrate the DNS setup from server '%s'."),$this->dns_server_list['ENTRIES'][$id]['cn'][0]);
157 $warning2 = _("The migration will be startet when you save this system. To cancel this action, use the cancel button below.");
158 $smarty->assign("warning",$warning);
159 $smarty->assign("warning2",$warning2);
160 return($smarty->fetch(get_template_path('servdns.tpl', TRUE, dirname(__FILE__))));
161 }
164 /* Do we need to flip is_account state?
165 */
166 if (isset($_POST['modify_state'])){
167 $this->is_account= !$this->is_account;
168 }
170 if ($this->is_account){
171 $display= $this->show_disable_header(_("Remove DNS service"),
172 _("This server has DNS features enabled. You can disable them by clicking below."));
173 } else {
174 $display= $this->show_enable_header(_("Add DNS service"),
175 _("This server has DNS features disabled. You can enable them by clicking below."));
176 return ($display);
177 }
180 /* Edited or Added zone
181 */
182 if(isset($_POST['SaveZoneChanges'])){
183 $this->dialog->save_object();
185 /* Check for errors
186 */
187 if(count($this->dialog->check())){
188 foreach($this->dialog->check() as $msgs){
189 print_red($msgs);
190 }
191 }else{
192 /* add new/edited zone
193 */
194 $ret = $this->dialog->save();
195 if(!$this->dialog->isNew){
196 unset($this->Zones[$this->dialog->OldZoneName]);
197 }
198 $this->Zones[$ret['zoneName']] = $ret;
199 $this->dialog = FALSE;
200 }
201 }
203 /* Cancel zone edit / new
204 */
205 if(isset($_POST['CancelZoneChanges'])){
206 $this->dialog = FALSE;
207 }
209 /* Add empty new zone
210 */
211 if(isset($_POST['AddZone'])){
212 $this->dialog = new servdnseditZone($this->config,$this->dn);
213 }
215 /* Check for edit zone request
216 */
217 $once = false;
218 foreach( $_POST as $name => $value){
220 /* check all post for edit request
221 */
222 if(preg_match("/^editZone_/",$name)&&!$once){
223 $once =true;
224 $tmp = preg_replace("/^editZone_/","",$name);
225 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
226 $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$tmp]);
227 }
229 /* check posts for delete zone
230 */
231 if(preg_match("/^delZone_/",$name)&&!$once){
233 $once =true;
234 $tmp = preg_replace("/^delZone_/","",$name);
235 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
237 /* Initiate deletion
238 */
239 $this->RemoveZone($tmp);
240 }
241 }
243 if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
244 $id = base64_decode($_GET['id']);
245 if(isset($this->Zones[$id])){
246 $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$id]);
247 }
248 }
250 if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
251 $id = base64_decode($_GET['id']);
252 if(isset($this->Zones[$id])){
253 $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$id]);
254 }
255 }
257 /* Show dialog
258 */
259 if(is_object($this->dialog)){
260 $this->dialog->save_object();
261 $this->dialog->parent = $this;
262 return($this->dialog->execute());
263 }
265 /* Create Listbox with existing Zones
266 */
267 $ZoneList = new divSelectBox("dNSZones");
268 $ZoneList -> SetHeight(254);
270 /* Add entries to divlist
271 */
272 $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>";
273 if($this->acl_is_removeable()){
274 $editImg.= "<input type='image' src='images/edittrash.png' name='delZone_%s'>";
275 }
277 $link = "<a href='?plug=".$_GET['plug']."&act=edit&id=%s'>%s</a>";
278 foreach($this->Zones as $zone => $values ){
279 $ZoneList->AddEntry(array(
280 array("string" => sprintf($link,base64_encode($zone),($zone))),
281 array("string" => sprintf($link,base64_encode($zone),_("Reverse zone")." : ".($values['ReverseZone']))),
282 array("string" => _("TTL")." : ".$values['sOAttl']),
283 array("string" => _("Class")." : ".$values['dNSClass']),
284 array("string" =>str_replace("%s",base64_encode($zone),$editImg))
285 ));
286 }
288 /* Display tempalte
289 */
290 $smarty->assign("ZoneList",$ZoneList->DrawList());
291 $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE, dirname(__FILE__)));
292 return($display);
293 }
296 /* Delete specified zone
297 */
298 function RemoveZone($id)
299 {
300 $zones = $this->getUsedZoneNames();
302 if(isset($this->Zones[$id]['InitialReverseZone'])){
303 $rev = DNS::FlipIp($this->Zones[$id]['InitialReverseZone']);
304 }else{
305 $rev = DNS::FlipIp($this->Zones[$id]['ReverseZone']);
306 }
308 $zonename = "";
309 if(isset($this->Zones[$id]['InitialzoneName'])){
310 $zonename= $this->Zones[$id]['InitialzoneName'];
311 }
313 $used = array();
315 /* Add Records which use this zoneName
316 */
317 if(isset($zones[$zonename])){
318 $used = array_merge($used,$zones[$zonename]);
319 }
321 /* Add Records which uses this reverse zone
322 */
323 if(isset($zones[$rev.".in-addr.arpa"])){
324 $used = array_merge($used,$zones[$rev.".in-addr.arpa"]);
325 }
327 /* There are still entries using this configuration
328 * Abort deletion
329 */
330 if(count($used)){
331 $i = 2;
332 $str ="";
333 foreach($used as $dn){
334 if($i > 0 && !preg_match("/,relativeDomainName=/",$dn)){
335 $i --;
336 $name = preg_replace("/^[^=]+=([^,]*),.*$/","\\1",$dn);
337 $zone = preg_replace("/^.*zoneName=([^,]*),.*$/","\\1",$dn);
338 $str.= $name.".".$zone." ";
339 }
340 }
342 /* Only show 2 dns in the error message
343 */
344 if(count($used)> 2) {
345 $str .=" ... ";
346 }
347 print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
348 return(false);
349 }else{
350 unset($this->Zones[$id]);
351 return(true);
352 }
353 }
356 /* This funtion returns all used Zonenames
357 */
358 function getUsedZoneNames()
359 {
360 $ret = array();
361 $ldap = $this->config->get_ldap_link();
362 $ldap->cd($this->config->current['BASE']);
363 $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
364 while($attr = $ldap->fetch()){
365 $ret[$attr['zoneName'][0]][] = $attr['dn'];
366 }
367 return($ret);
368 }
371 /* Remove dns service
372 */
373 function remove_from_parent()
374 {
375 if($this->initially_was_account){
376 $bool = true;
377 $this->is_account = FALSE;
378 foreach($this->Zones as $key => $zone){
379 $bool= $bool & $this->RemoveZone($key);
380 }
382 if($bool){
383 $this->save();
384 }
385 return($bool);
386 }
387 }
390 /* Save to LDAP */
391 function save()
392 {
394 /* Take over handling
395 * - Create list of zones managed by source server
396 * - Copy ldap entries to destination server
397 * - Remove old zone entries from source
398 */
399 if($this->take_over_id != -1){
400 $del = array();
401 $id = $this->take_over_id;
402 $src = $this->dns_server_list['ENTRIES'][$id]['dn'];
403 $ldap = $this->config->get_ldap_link();
404 $ldap->ls("(objectClass=dnsZone)",$src,array('cn'));
405 while($attrs = $ldap->fetch()){
406 $src_zone = $attrs['dn'];
407 $dst_zone = preg_replace("/".normalizePreg($src)."$/",$this->dn,$src_zone);
408 $res = plugin::recursive_move($src_zone, $dst_zone);
410 if($res){
411 $del [] = $src_zone;
412 }
413 }
414 foreach($del as $src_zone){
415 $ldap->rmdir_recursive($src_zone);
416 }
417 return;
418 }
420 /* Save zone editor changes now */
421 foreach($this->Zones as $name => $zone){
422 if(isset($zone['zoneEditor'] ) && $zone['zoneEditor'] != NULL && is_object($zone['zoneEditor'])){
423 $zone['zoneEditor']->save();
424 unset($this->Zones[$name]['zoneEditor']);;
425 }
426 }
428 $ldap = $this->config->get_ldap_link();
429 $ldap->cd($this->config->current['BASE']);
431 /* Get differences
432 */
433 $old_dn = $this->orig_dn;
434 if($old_dn == "new"){
435 $old_dn = $this->dn;
436 }
438 /* Update dns to current object dn */
439 $tmp = DNS::getDNSZoneEntriesDiff($this->config,$this->Zones,$old_dn);
440 $tmp2 = array();
441 foreach($tmp as $key1 => $data1){
442 $tmp2[$key1] = array();
443 foreach($data1 as $key2 => $data2){
444 $tmp2[$key1][preg_replace("/".normalizePreg($old_dn)."$/",$this->dn,$key2)] = $data2;
445 }
446 }
447 $tmp = $tmp2;
449 /* Updated zone entries if reverser or forward name has changed
450 * Must be done before moving entries, else the given dn is invalid
451 */
452 if(isset($tmp['zoneUpdates'])){
453 foreach($tmp['zoneUpdates'] as $dn => $attrs){
454 $ldap->cd($dn);
455 $ldap->modify($attrs);
456 new log("modify","unknown/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
457 show_ldap_error($ldap->get_error(), sprintf(_("Updating of system server/dns with dn '%s' failed."),$this->dn));
458 }
459 }
461 /* Delete dns
462 */
463 foreach($tmp['del'] as $dn => $del){
465 $for = $del['InitialzoneName'];
466 $rev = DNS::FlipIp($del['InitialReverseZone']).".in-addr.arpa";
468 $ldap->cd($dn);
469 $ldap->rmdir_recursive($dn);
470 new log("remove","unknown/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
471 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/dns with dn '%s' failed."),$this->dn));
473 /* Handle Post events */
474 if(preg_match("/^zoneName=/",$dn)){
475 # $this->handle_post_events("remove",array("dn" => $dn,"zoneName" => $for));
476 # $this->handle_post_events("remove",array("dn" => $dn,"zoneName" => $rev));
477 }
478 }
480 /* move follwoing entries
481 */
482 foreach($tmp['move'] as $src => $dst){
483 $this->recursive_move($src,$dst);
484 }
486 /* Add || Update new DNS entries
487 */
488 foreach($tmp['add'] as $dn => $attrs){
489 $ldap->cd($dn);
490 $ldap->cat($dn, array('dn'));
491 if($ldap->fetch()){
492 $ldap->cd($dn);
493 $ldap->modify ($attrs);
494 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
496 /* Handle Post events */
497 if(preg_match("/^zoneName=/",$dn)){
498 # $this->handle_post_events("modify",array("dn" => $dn,"zoneName" => $attrs['zoneName']));
499 }
500 }else{
501 $ldap->cd($dn);
502 $ldap->add($attrs);
503 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
505 /* Handle Post events */
506 if(preg_match("/^zoneName=/",$dn)){
507 # $this->handle_post_events("add",array("dn" => $dn,"zoneName" => $attrs['zoneName']));
508 }
509 }
510 }
511 $this->handle_post_events("modify");
512 }
515 /* Directly save new status flag */
516 function setStatus($value)
517 {
518 if($value == "none") return;
519 if(!$this->initially_was_account) return;
520 if(empty($this->StatusFlag)) return;
521 $ldap = $this->config->get_ldap_link();
522 $ldap->cd($this->dn);
523 $ldap->cat($this->dn,array("objectClass"));
524 if($ldap->count()){
526 $tmp = $ldap->fetch();
527 for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
528 $attrs['objectClass'][] = $tmp['objectClass'][$i];
529 }
530 $flag = $this->StatusFlag;
531 $attrs[$flag] = $value;
532 $this->$flag = $value;
533 $ldap->modify($attrs);
534 show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for system server/dns with dn '%s' failed."),$this->dn));
535 $this->action_hook();
536 }
537 }
540 function getListEntry()
541 {
542 $fields = goService::getListEntry();
543 $fields['Message'] = _("DNS service");
544 $fields['AllowEdit'] = true;
545 return($fields);
546 }
549 /* Get updates for status flag */
550 function updateStatusState()
551 {
552 if(empty($this->StatusFlag)) return;
554 $attrs = array();
555 $flag = $this->StatusFlag;
556 $ldap = $this->config->get_ldap_link();
557 $ldap->cd($this->cn);
558 $ldap->cat($this->dn,array($flag));
559 if($ldap->count()){
560 $attrs = $ldap->fetch();
561 }
562 if(isset($attrs[$flag][0])){
563 $this->$flag = $attrs[$flag][0];
564 }
565 }
568 /* Return plugin informations for acl handling */
569 static function plInfo()
570 {
571 return (array(
572 "plShortName" => _("DNS service"),
573 "plDescription" => _("DNS service")." ("._("Services").")",
574 "plSelfModify" => FALSE,
575 "plDepends" => array(),
576 "plPriority" => 83,
577 "plSection" => array("administration"),
578 "plCategory" => array("server"),
580 "plProvidedAcls"=> array(
581 "zoneName" =>_("Zone name"),
582 "ReverseZone" =>_("Reverse zone"),
583 "sOAprimary" =>_("Primary dns server"),
584 "sOAmail" =>_("Mail address"),
585 "sOAserial" =>_("Serial"),
586 "sOArefresh" =>_("Refresh"),
587 "sOAretry" =>_("Retry"),
588 "sOAexpire" =>_("Expire"),
589 "sOAttl" =>_("TTL"),
590 "zoneRecords" =>_("Zone records"))
591 ));
592 }
594 }
595 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
596 ?>