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 = getDnsRecordTypes(true);
44 /* Get all zone Informations
45 */
46 $this->Zones = 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)));
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));
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 = FlipIp($this->Zones[$id]['InitialReverseZone']);
304 }else{
305 $rev = 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 }
421 $ldap = $this->config->get_ldap_link();
422 $ldap->cd($this->config->current['BASE']);
424 /* Get differences
425 */
426 $old_dn = $this->orig_dn;
427 if($old_dn == "new"){
428 $old_dn = $this->dn;
429 }
431 $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$old_dn);
433 /* Update dns to current object dn */
434 $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$old_dn);
435 $tmp2 = array();
436 foreach($tmp as $key1 => $data1){
437 $tmp2[$key1] = array();
438 foreach($data1 as $key2 => $data2){
439 $tmp2[$key1][preg_replace("/".normalizePreg($old_dn)."$/",$this->dn,$key2)] = $data2;
440 }
441 }
442 $tmp = $tmp2;
444 /* Updated zone entries if reverser or forward name has changed
445 * Must be done before moving entries, else the given dn is invalid
446 */
447 if(isset($tmp['zoneUpdates'])){
448 foreach($tmp['zoneUpdates'] as $dn => $attrs){
449 $ldap->cd($dn);
450 $ldap->modify($attrs);
451 new log("modfiy","unknown/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
452 show_ldap_error($ldap->get_error(), sprintf(_("Updating of system server/dns with dn '%s' failed."),$this->dn));
453 }
454 }
456 /* Delete dns
457 */
458 foreach($tmp['del'] as $dn => $del){
460 $for = $del['InitialzoneName'];
461 $rev = FlipIp($del['InitialReverseZone']).".in-addr.arpa";
463 $ldap->cd($dn);
464 $ldap->rmdir_recursive($dn);
465 new log("remove","unknown/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
466 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/dns with dn '%s' failed."),$this->dn));
468 /* Handle Post events */
469 if(preg_match("/^zoneName=/",$dn)){
470 $this->handle_post_events("remove",array("dn" => $dn,"zoneName" => $for));
471 $this->handle_post_events("remove",array("dn" => $dn,"zoneName" => $rev));
472 }
473 }
475 /* move follwoing entries
476 */
477 foreach($tmp['move'] as $src => $dst){
478 $this->recursive_move($src,$dst);
479 }
481 /* Add || Update new DNS entries
482 */
483 foreach($tmp['add'] as $dn => $attrs){
484 $ldap->cd($dn);
485 $ldap->cat($dn, array('dn'));
486 if($ldap->fetch()){
487 $ldap->cd($dn);
488 $ldap->modify ($attrs);
489 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
491 /* Handle Post events */
492 if(preg_match("/^zoneName=/",$dn)){
493 $this->handle_post_events("modify",array("dn" => $dn,"zoneName" => $attrs['zoneName']));
494 }
495 }else{
496 $ldap->cd($dn);
497 $ldap->add($attrs);
498 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
500 /* Handle Post events */
501 if(preg_match("/^zoneName=/",$dn)){
502 $this->handle_post_events("add",array("dn" => $dn,"zoneName" => $attrs['zoneName']));
503 }
504 }
505 }
506 }
509 /* Directly save new status flag */
510 function setStatus($value)
511 {
512 if($value == "none") return;
513 if(!$this->initially_was_account) return;
514 if(empty($this->StatusFlag)) return;
515 $ldap = $this->config->get_ldap_link();
516 $ldap->cd($this->dn);
517 $ldap->cat($this->dn,array("objectClass"));
518 if($ldap->count()){
520 $tmp = $ldap->fetch();
521 for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
522 $attrs['objectClass'][] = $tmp['objectClass'][$i];
523 }
524 $flag = $this->StatusFlag;
525 $attrs[$flag] = $value;
526 $this->$flag = $value;
527 $ldap->modify($attrs);
528 show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for system server/dns with dn '%s' failed."),$this->dn));
529 $this->action_hook();
530 }
531 }
534 function getListEntry()
535 {
536 $fields = goService::getListEntry();
537 $fields['Message'] = _("DNS service");
538 $fields['AllowEdit'] = true;
539 return($fields);
540 }
543 /* Get updates for status flag */
544 function updateStatusState()
545 {
546 if(empty($this->StatusFlag)) return;
548 $attrs = array();
549 $flag = $this->StatusFlag;
550 $ldap = $this->config->get_ldap_link();
551 $ldap->cd($this->cn);
552 $ldap->cat($this->dn,array($flag));
553 if($ldap->count()){
554 $attrs = $ldap->fetch();
555 }
556 if(isset($attrs[$flag][0])){
557 $this->$flag = $attrs[$flag][0];
558 }
559 }
562 /* Return plugin informations for acl handling */
563 function plInfo()
564 {
565 return (array(
566 "plShortName" => _("DNS service"),
567 "plDescription" => _("DNS service")." ("._("Services").")",
568 "plSelfModify" => FALSE,
569 "plDepends" => array(),
570 "plPriority" => 83,
571 "plSection" => array("administration"),
572 "plCategory" => array("server"),
574 "plProvidedAcls"=> array(
575 "zoneName" =>_("Zone name"),
576 "ReverseZone" =>_("Reverse zone"),
577 "sOAprimary" =>_("Primary dns server"),
578 "sOAmail" =>_("Mail address"),
579 "sOAserial" =>_("Serial"),
580 "sOArefresh" =>_("Refresh"),
581 "sOAretry" =>_("Retry"),
582 "sOAexpire" =>_("Expire"),
583 "sOAttl" =>_("TTL"),
584 "zoneRecords" =>_("Zone records"))
585 ));
586 }
588 }
589 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
590 ?>