a876012c44dc99e782d5cc963aa125498dc8c839
1 <?php
3 class servdns 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 = FALSE;
12 var $attributes = array();
13 var $objectclasses = array("whatever");
15 var $RecordTypes = array();
17 var $Zones = array();
18 var $dialog = NULL;
20 var $usedDNS = array();
22 var $orig_dn = "";
24 var $DNSinitially_was_account;
27 function servdns ($config, $dn= NULL)
28 {
29 plugin::plugin ($config, $dn);
31 $this->orig_dn = $dn;
33 /* All types with required attrs */
34 $this->RecordTypes['aRecord'] = "aRecord"; // ok
35 $this->RecordTypes['mDRecord'] = "mDRecord"; // ok
36 $this->RecordTypes['mXRecord'] = "mXRecord"; // ok
37 $this->RecordTypes['nSRecord'] = "nSRecord"; // ok
38 $this->RecordTypes['hInfoRecord'] = "hInfoRecord"; // ok
39 $this->RecordTypes['mInfoRecord'] = "mInfoRecord"; // ok
40 // $this->RecordTypes['tXTRecord'] = "tXTRecord"; // ok
41 $this->RecordTypes['aFSDBRecord'] = "aFSDBRecord"; // ok
42 $this->RecordTypes['SigRecord'] = "SigRecord"; // ok
43 $this->RecordTypes['KeyRecord'] = "KeyRecord"; // ok
44 $this->RecordTypes['aAAARecord'] = "aAAARecord"; // ok
45 $this->RecordTypes['LocRecord'] = "LocRecord"; // ok
46 $this->RecordTypes['nXTRecord'] = "nXTRecord"; // ok
47 $this->RecordTypes['sRVRecord'] = "sRVRecord"; // ok
48 $this->RecordTypes['nAPTRRecord'] = "nAPTRRecord"; // ok
49 $this->RecordTypes['kXRecord'] = "kXRecord"; // ok
50 $this->RecordTypes['certRecord'] = "certRecord"; // ok
51 $this->RecordTypes['a6Record'] = "a6Record"; // ok
52 $this->RecordTypes['dSRecord'] = "dSRecord"; // ok
53 $this->RecordTypes['sSHFPRecord'] = "sSHFPRecord"; // ok
54 $this->RecordTypes['rRSIGRecord'] = "rRSIGRecord"; // ok
55 $this->RecordTypes['nSECRecord'] = "nSECRecord"; // ok
57 $types = array();
59 /* Get all records */
60 $ldap = $this->config->get_ldap_link();
61 $ldap->cd($this->dn);
62 $ldap->ls("(&(objectClass=dNSZone)(relativeDomainName=@))",$dn,array("*"));
64 while($attrs = $ldap->fetch()){
66 /* If relative domainname
67 * Try to read dnsclass / TTl / zone
68 */
69 $this->usedDNS[$attrs['dn']] = $attrs['dn'];
70 if((isset($attrs['tXTRecord'][0]))&&(preg_match("/zoneName\=/",$attrs['tXTRecord'][0]))){
71 $zoneName= preg_replace("/zoneName\=/","",$attrs['tXTRecord'][0]);
72 $z = preg_replace("/\.in\-addr\.arpa/","",$attrs['zoneName'][0]);
74 $z = $this->FlipIp($z);
76 $types[$zoneName]['ReverseZone'] = $z;
77 $types[$zoneName]['ReverseDN'] = $attrs['dn'];
78 }else{
80 /* Generate SOA entry
81 */
82 if(isset($attrs['sOARecord'][0])){
83 $tmp = split("\ ",$attrs['sOARecord'][0]) ;
84 $tmp2 = array();
85 $ar = array("0"=>"sOAprimary","1"=>"sOAmail","2"=>"sOAserial","3"=>"sOArefresh","4"=>"sOAretry","5"=>"sOAexpire","6"=>"sOAttl");
87 /* Assign soa vars */
88 foreach($ar as $key => $name){
89 if(isset($tmp[$key])){
90 $types[$attrs['zoneName'][0]][$name] = $tmp[$key];
91 }else{
92 $types[$attrs['zoneName'][0]][$name] = "";
93 }
94 }
95 }
97 /* Set dns Class
98 */
99 if(isset($attrs['dNSClass'][0])){
100 $types[$attrs['zoneName'][0]]['dNSClass'] = $attrs['dNSClass'][0];
101 }
103 /* Set zone Name
104 */
105 if(isset($attrs['zoneName'][0])){
106 $types[$attrs['zoneName'][0]]['zoneName'] = $attrs['zoneName'][0];
107 }
109 /* Create list with all used records
110 */
111 foreach($this->RecordTypes as $name => $value){
113 /* If there is a record attribute
114 */
115 if(isset($attrs[$name])){
117 /* get all entries
118 */
119 for($i = 0 ; $i < $attrs[$value]['count']; $i ++){
120 $types[$attrs['zoneName'][0]]['Records'][] =array("type" =>$name,
121 "inittype" =>$name,
122 "value" =>$attrs[$value][$i],
123 "status" =>"edited",
124 "dn" =>$attrs['dn']);
125 }
126 }
127 }
128 }
129 }
131 /* If there is at least one entry in this -> types, we have DNS enabled
132 */
133 $this->Zones = $types;
134 if(count($this->Zones) == 0){
135 $this->is_account = false;
136 }else{
137 $this->is_account = true;
138 }
140 /* Store initally account settings
141 */
142 $this->DNSinitially_was_account = $this->is_account;
143 }
146 /* this is used to flip the ip address for example
147 12.3.45 -> 54.3.12
148 Because some entries (like zones) are store like that 54.3.12.in-addr.arpa
149 but we want to display 12.3.45.
150 */
151 function FlipIp($ip)
152 {
153 $tmp = array_reverse(split("\.",$ip));
154 $new = "";
155 foreach($tmp as $section){
156 $new .= $section.".";
157 }
158 return(preg_replace("/.$/","",$new));
159 }
161 function execute()
162 {
163 /* Call parent execute
164 */
165 plugin::execute();
167 /* Fill templating stuff
168 */
169 $smarty= get_smarty();
170 $display= "";
172 /* Do we need to flip is_account state?
173 */
174 if (isset($_POST['modify_state'])){
175 $this->is_account= !$this->is_account;
176 }
178 /* Show tab dialog headers
179 */
180 if ($this->is_account){
181 $display= $this->show_header(_("Remove DNS service"),
182 _("This server has DNS features enabled. You can disable them by clicking below."));
183 } else {
184 $display= $this->show_header(_("Add DNS service"),
185 _("This server has DNS features disabled. You can enable them by clicking below."));
186 return ($display);
187 }
189 /* Edited or Added zone hould be saved saved
190 */
191 if(isset($_POST['SaveZoneChanges'])){
192 $this->dialog->save_object();
194 /* Check if noting went wrong
195 */
196 if(count($this->dialog->check())){
197 foreach($this->dialog->check() as $msgs){
198 print_red($msgs);
199 }
200 }else{
202 /* add new/edited zone
203 */
204 $ret = $this->dialog->save();
205 $ret['InitialzoneName']= $this->dialog->InitiallyZoneName;
206 $ret['InitialreverseZone']= $this->dialog->InitiallyReverseZone;
207 unset($this->Zones[$this->dialog->InitiallyZoneName]);
208 $this->Zones[$ret['zoneName']] = $ret;
209 $this->dialog = NULL;
210 }
211 }
213 /* Cancel zone edit / new
214 */
215 if(isset($_POST['CancelZoneChanges'])){
216 $this->dialog = NULL;
217 }
219 /* Add empty new zone
220 */
221 if(isset($_POST['AddZone'])){
222 $this->dialog = new servdnseditZone($this->config,$this->dn,$this->RecordTypes);
223 }
225 /* Check for edit zone request
226 */
227 $once = false;
228 foreach( $_POST as $name => $value){
230 /* check all post for edit request
231 */
232 if(preg_match("/^editZone_/",$name)&&!$once){
233 $once =true;
234 $tmp = preg_replace("/^editZone_/","",$name);
235 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
236 $this->dialog= new servdnseditZone($this->config,$this->dn,$this->RecordTypes,$this->Zones[$tmp]);
237 }
239 /* check posts for delete zone
240 */
241 if(preg_match("/^delZone_/",$name)&&!$once){
242 $once =true;
243 $tmp = preg_replace("/^delZone_/","",$name);
244 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
246 $zones = $this->getUsedZoneNames();
247 $rev = $this->Zones[$tmp]['ReverseZone'];
248 $res = array_merge(($zones[$tmp]),($zones[$rev.".in-addr.arpa"]));
250 if(count($res)){
251 $i = 2;
252 $str ="";
253 foreach($res as $dn){
254 if($i > 0 ){
255 $i --;
256 $str.=$dn." ";
257 }
258 }
259 if(count($res)> 2) $str .=" ... ";
260 print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
261 }else{
262 unset($this->Zones[$tmp]);
263 }
264 }
265 }
267 /* Show dialog
268 */
269 if($this->dialog!= NULL){
270 $this->dialog->save_object();
271 $this->dialog->parent = $this;
272 return($this->dialog->execute());
273 }
275 /* Create Listbox with existing Zones
276 */
277 $ZoneList = new divSelectBox("dNSZones");
278 $ZoneList -> SetHeight(254);
280 /* Add entries to divlist
281 */
282 $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
283 <input type='image' src='images/edittrash.png' name='delZone_%s'>";
284 foreach($this->Zones as $zone => $values ){
285 $ZoneList->AddEntry(array(
286 array("string" => $zone),
287 array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
288 array("string" => _("TTL")." : ".$values['sOAttl']),
289 array("string" => _("Class")." : ".$values['dNSClass']),
290 array("string" =>str_replace("%s",base64_encode($zone),$editImg))
291 ));
292 }
294 /* Display tempalte
295 */
296 $smarty->assign("ZoneList",$ZoneList->DrawList());
297 $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
298 return($display);
299 }
302 /* This funtion returns all used Zonenames
303 */
304 function getUsedZoneNames()
305 {
306 $ret = array();
307 $ldap = $this->config->get_ldap_link();
308 $ldap->cd($this->config->current['BASE']);
309 $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName","tXTRecord"));
310 while($attr = $ldap->fetch()){
311 if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
312 $ret[$attr['zoneName'][0]][] = $attr['dn'];
313 }else{
314 $ret[$attr['zoneName'][0]][] = $attr['dn'];
315 }
316 }
317 return($ret);
318 }
321 /* Remove dns service
322 */
323 function remove_from_parent()
324 {
325 if(!$this->DNSinitially_was_account){
326 return;
327 }
329 $ldap = $this->config->get_ldap_link();
330 $ldap->cd($this->config->current['BASE']);
331 foreach($this->usedDNS as $dn){
332 $ldap->cd($dn);
333 $ldap->rmdir_recursive($dn);
334 }
336 $ldap = $this->config->get_ldap_link();
337 $ldap->cd($this->orig_dn);
338 $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(relativeDomainName=@))",array("relativeDomainName","zoneName"));
339 while($attr = $ldap->fetch()){
340 $ldap->cd($attr['dn']);
341 $ldap->rmDir($attr['dn']);
342 }
345 show_ldap_error($ldap->get_error());
346 }
349 /* Save data to object */
350 function save_object()
351 {
352 }
355 /* Check supplied data */
356 function check()
357 {
358 $message= array();
359 return ($message);
360 }
363 /* Save to LDAP */
364 function save()
365 {
366 /* Ldap conenction / var initialization
367 */
368 $ldap = $this->config->get_ldap_link();
369 $ldap->cd($this->config->current['BASE']);
370 $actions =array("update"=>array(),"add"=>array(),"delete"=>array());
372 /* Generate entries for all zones, and check if they must be updated deleted added
373 */
374 foreach($this->Zones as $key => $zone){
375 if(isset($zone['InitialzoneName']) && ($zone['zoneName'] != $zone['InitialzoneName'])){
376 $dst = "zoneName=".$zone['zoneName'].",".$this->dn;
377 $src = "zoneName=".$zone['InitialzoneName'].",".$this->dn;
378 $this->recursive_move($src,$dst);
380 $tmp['zoneName'] = $zone['zoneName'];
381 $ldap->search ("(&(objectClass=dNSZone)(zoneName=".$zone['InitialzoneName']."))",array("relativeDomainName"));
382 while($attrs = $ldap->fetch()){
383 $ldap->cd($attrs['dn']);
384 $ldap->modify($tmp);
385 show_ldap_error($ldap->get_error());
386 }
387 }
388 if(isset($zone['InitialreverseZone']) &&($zone['InitialreverseZone'] != $zone['ReverseZone'])){
389 $dst = "zoneName=".$zone['ReverseZone'].",".$this->dn;
390 $src = "zoneName=".$zone['InitialreverseZone'].",".$this->dn;
391 $this->recursive_move($src,$dst);
393 $tmp['zoneName'] = $zone['zoneName'];
394 $ldap->search ("(&(objectClass=dNSZone)(zoneName=".$zone['InitialzoneName']."))",array("relativeDomainName"));
395 while($attrs = $ldap->fetch()){
396 $ldap->cd($attrs['dn']);
397 $ldap->modify($tmp);
398 show_ldap_error($ldap->get_error());
399 }
400 }
403 /* Get ldap syntax
404 */
405 $tmp = $this->generate_LDAP_entries($zone);
407 /* Check if dn is new, or if entry was edited
408 */
409 foreach($tmp as $key => $values){
410 if(isset($this->usedDNS[$key])){
411 $actions['update'][$key]=$values;
412 unset($this->usedDNS[$key]);
413 }else{
414 $actions['add'][$key] = $values;
415 }
416 }
417 }
419 /* Check which dns are not used anymore ...
420 */
421 foreach($this->usedDNS as $key => $values){
422 $actions['delete'][$key] = $values;
423 }
425 /* Remove deleted zones
426 */
427 foreach($actions['delete'] as $dn => $attrs){
428 $ldap->cd($dn);
429 $ldap->rmdir_recursive($dn);
430 }
432 /* Add new zones
433 */
434 foreach($actions['add'] as $dn => $attrs){
435 $ldap->cd($this->config->current['BASE']);
436 $ldap->cd($dn);
437 $ldap->add($attrs);
438 }
440 /* Update existing entries
441 */
442 foreach($actions['update'] as $dn => $attrs){
443 $ldap->cd($dn);
444 $ldap->modify ($attrs);
446 }
448 show_ldap_error($ldap->get_error());
449 }
452 /* This function generates ldap friendly output
453 of all changes for a single zone (reverse and forward)
454 */
455 function generate_LDAP_entries($zone)
456 {
457 $tmp = array();
458 $tmp['objectClass'] = array("top","dNSZone");
459 $tmp['dNSClass'] = "IN";//$zone['dNSClass'];
460 $tmp['relativeDomainName'] = "@";//$zone['relativeDomainName'];
462 $str = "";
463 foreach(array("sOAprimary","sOAmail","sOAserial","sOArefresh","sOAretry","sOAexpire","sOAttl") as $name){
464 $str .= $zone[$name]." ";
465 }
466 $tmp['sOARecord'] = $str;
469 /* Generate Record entries
470 */
471 $arr = array("aRecord","SigRecord","KeyRecord","aAAARecord","nSRecord","iaFSDBRecord","mInfoRecord","hInfoRecord","mXRecord","mDRecord","tXTRecord",
472 "LocRecord","nXTRecord","sRVRecord","nAPTRRecord","kXRecord","certRecord","a6Record","dSRecord","sSHFPRecord","rRSIGRecord","nSECRecord");
473 $aRecords = array();
474 foreach($arr as $ar){
475 if((isset($zone['Records']))&&(is_array($zone['Records']))){
476 foreach($zone['Records'] as $type){
477 if(($type['type'] == $ar)&&($type['status']!="deleted")){
478 $tmp[$ar][] = $type['value'];
479 }
480 }
481 }
482 }
483 /* Check if there are records removed,
484 if there are some removed records, then append an array
485 to ensure that these record types are deleted
486 */
487 if((isset($zone['Records']))&&(is_array($zone['Records']))){
488 foreach($zone['Records'] as $type){
489 if((isset($type['inittype']))&&($type['inittype']!="")){
490 if($type['type'] != $type['inittype']){
491 $tmp[$type['inittype']] = array();
492 }
493 }
494 }
495 }
497 /* generate forward entry
498 */
499 $dn = "zoneName=".$zone['zoneName'].",".$this->dn;
500 $tmp2[$dn] = $tmp;
501 $tmp2[$dn]['zoneName'] = $zone['zoneName'];
503 /* generate reverse entry
504 */
505 $dn = "zoneName=".$this->FlipIp($zone['ReverseZone']).".in-addr.arpa,".$this->dn;
506 $tmp2[$dn] = $tmp;
507 $tmp2[$dn]['tXTRecord'] ="zoneName=".$zone['zoneName'];
508 $tmp2[$dn]['zoneName'] = $this->FlipIp($zone['ReverseZone']).".in-addr.arpa";
510 return($tmp2);
511 }
512 }
513 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
514 ?>