6039a29bb9557f9090b251e64208bb1cd5ee833e
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();
21 function servdns ($config, $dn= NULL)
22 {
23 plugin::plugin ($config, $dn);
25 /* All types with required attrs */
26 $this->RecordTypes['aRecord'] = "aRecord"; // ok
27 $this->RecordTypes['mDRecord'] = "mDRecord"; // ok
28 $this->RecordTypes['mXRecord'] = "mXRecord"; // ok
29 $this->RecordTypes['nSRecord'] = "nSRecord"; // ok
30 $this->RecordTypes['pTRRecord'] = "relativeDomainName";// ok
31 $this->RecordTypes['hInfoRecord'] = "hInfoRecord"; // ok
32 $this->RecordTypes['mInfoRecord'] = "mInfoRecord"; // ok
33 $this->RecordTypes['tXTRecord'] = "tXTRecord"; // ok
34 $this->RecordTypes['aFSDBRecord'] = "aFSDBRecord"; // ok
35 $this->RecordTypes['SigRecord'] = "SigRecord"; // ok
36 $this->RecordTypes['KeyRecord'] = "KeyRecord"; // ok
37 $this->RecordTypes['aAAARecord'] = "aAAARecord"; // ok
38 $this->RecordTypes['LocRecord'] = "LocRecord"; // ok
39 $this->RecordTypes['nXTRecord'] = "nXTRecord"; // ok
40 $this->RecordTypes['sRVRecord'] = "sRVRecord"; // ok
41 $this->RecordTypes['nAPTRRecord'] = "nAPTRRecord"; // ok
42 $this->RecordTypes['kXRecord'] = "kXRecord"; // ok
43 $this->RecordTypes['certRecord'] = "certRecord"; // ok
44 $this->RecordTypes['a6Record'] = "a6Record"; // ok
45 $this->RecordTypes['dSRecord'] = "dSRecord"; // ok
46 $this->RecordTypes['sSHFPRecord'] = "sSHFPRecord"; // ok
47 $this->RecordTypes['rRSIGRecord'] = "rRSIGRecord"; // ok
48 $this->RecordTypes['nSECRecord'] = "nSECRecord"; // ok
50 $this->cn = $this->attrs['cn'][0];
51 $types = array();
53 /* Get all records */
54 $ldap = $this->config->get_ldap_link();
55 $ldap->cd($this->dn);
56 $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@))",array("*"));
58 while($attrs = $ldap->fetch()){
59 /* If relative domainname == cn
60 * Try to read dnsclass / TTl / zone
61 */
62 $this->usedDNS[$attrs['dn']] = $attrs['dn'];
63 if((isset($attrs['tXTRecord'][0]))&&(preg_match("/zoneName\=/",$attrs['tXTRecord'][0]))){
64 $zoneName= preg_replace("/zoneName\=/","",$attrs['tXTRecord'][0]);
65 $z = preg_replace("/\.in\-addr\.arpa/","",$attrs['zoneName'][0]);
67 $z = $this->FlipIp($z);
69 $types[$zoneName]['ReverseZone'] = $z;
70 $types[$zoneName]['ReverseDN'] = $attrs['dn'];
71 }else{
73 /* Generate SOA entry */
74 if(isset($attrs['sOARecord'][0])){
75 $tmp = split("\ ",$attrs['sOARecord'][0]) ;
76 $tmp2 = array();
77 $ar = array("0"=>"sOAprimary","1"=>"sOAmail","2"=>"sOAserial","3"=>"sOArefresh","4"=>"sOAretry","5"=>"sOAexpire","6"=>"sOAttl");
79 /* Assign soa vars */
80 foreach($ar as $key => $name){
81 if(isset($tmp[$key])){
82 $types[$attrs['zoneName'][0]][$name] = $tmp[$key];
83 }else{
84 $types[$attrs['zoneName'][0]][$name] = "";
85 }
86 }
87 }
89 /* Set TTL value */
90 if(isset($attrs['dNSTTL'][0])){
91 $types[$attrs['zoneName'][0]]['dNSTTL'] = $attrs['dNSTTL'][0];
92 }
94 /* Set dns Class*/
95 if(isset($attrs['dNSClass'][0])){
96 $types[$attrs['zoneName'][0]]['dNSClass'] = $attrs['dNSClass'][0];
97 }
99 /* Set zone Name */
100 if(isset($attrs['zoneName'][0])){
101 $types[$attrs['zoneName'][0]]['zoneName'] = $attrs['zoneName'][0];
102 }
104 /* Create list with all used records */
105 foreach($this->RecordTypes as $name => $value){
107 /* If there is a record attribute */
108 if(isset($attrs[$name])){
110 $types[$attrs['zoneName'][0]]['Records']=array();
112 /* get all entries */
113 for($i = 0 ; $i < $attrs[$value]['count']; $i ++){
114 $types[$attrs['zoneName'][0]]['Records'][] =array("type" =>$name,
115 "inittype" =>$name,
116 "value" =>$attrs[$value][$i],
117 "status" =>"edited",
118 "dn" =>$attrs['dn']);
119 }
120 }
121 }
122 }
123 }
125 /* If there is at least one entry in this -> types, we have DNS enabled */
126 $this->Zones = $types;
127 if(count($this->Zones) == 0){
128 $this->is_account = false;
129 }else{
130 $this->is_account = true;
131 }
133 /* Store initally account settings */
134 $this->DNSinitially_was_account = $this->is_account;
135 }
138 /* this is used to flip the ip address for example
139 12.3.45 -> 54.3.12
140 Because some entries (like zones) are store like that 54.3.12.in-addr.arpa
141 but we want to display 12.3.45.
142 */
143 function FlipIp($ip)
144 {
145 $tmp = array_reverse(split("\.",$ip));
146 $new = "";
147 foreach($tmp as $section){
148 $new .= $section.".";
149 }
150 return(preg_replace("/.$/","",$new));
151 }
153 function execute()
154 {
155 /* Call parent execute */
156 plugin::execute();
158 /* Fill templating stuff */
159 $smarty= get_smarty();
160 $display= "";
162 /* Do we need to flip is_account state? */
163 if (isset($_POST['modify_state'])){
164 $this->is_account= !$this->is_account;
165 }
167 /* Show tab dialog headers */
168 if ($this->is_account){
169 $display= $this->show_header(_("Remove DNS service"),
170 _("This server has DNS features enabled. You can disable them by clicking below."));
171 } else {
172 $display= $this->show_header(_("Add DNS service"),
173 _("This server has DNS features disabled. You can enable them by clicking below."));
174 return ($display);
175 }
177 /* Edited or Added zone hould be saved saved */
178 if(isset($_POST['SaveZoneChanges'])){
179 $this->dialog->save_object();
181 /* Check if noting went wrong */
182 if(count($this->dialog->check())){
183 foreach($this->dialog->check() as $msgs){
184 print_red($msgs);
185 }
186 }else{
188 /* add new/edited zone */
189 $ret = $this->dialog->save();
190 unset($this->Zones[$this->dialog->InitiallyZoneName]);
191 $this->Zones[$ret['zoneName']] = $ret;
192 $this->dialog = NULL;
193 }
194 }
196 /* Cancel zone edit / new */
197 if(isset($_POST['CancelZoneChanges'])){
198 $this->dialog = NULL;
199 }
201 /* Add empty new zone */
202 if(isset($_POST['AddZone'])){
203 $this->dialog = new servdnseditZone($this->config,$this->dn,$this->RecordTypes);
204 }
206 /* Check for edit zone request */
207 $once = false;
208 foreach( $_POST as $name => $value){
210 /* check all post for edit request */
211 if(preg_match("/^editZone_/",$name)&&!$once){
212 $once =true;
213 $tmp = preg_replace("/^editZone_/","",$name);
214 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
215 $this->dialog= new servdnseditZone($this->config,$this->dn,$this->RecordTypes,$this->Zones[$tmp]);
216 }
218 /* check posts for delete zone */
219 if(preg_match("/^delZone_/",$name)&&!$once){
222 $once =true;
223 $tmp = preg_replace("/^delZone_/","",$name);
224 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
226 $zones = $this->getUsedZoneNames();
227 $rev = $this->Zones[$tmp]['ReverseZone'];
228 $res = array_merge(($zones[$tmp]),($zones[$rev.".in-addr.arpa"]));
230 if(count($res)){
231 $i = 2;
232 $str ="";
233 foreach($res as $dn){
234 if($i > 0 ){
235 $i --;
236 $str.=$dn." ";
237 }
238 }
239 if(count($res)> 2) $str .=" ... ";
240 print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
241 }else{
242 unset($this->Zones[$tmp]);
243 }
244 }
245 }
247 /* Show dialog */
248 if($this->dialog!= NULL){
249 $this->dialog->save_object();
250 $this->dialog->parent = $this;
251 return($this->dialog->execute());
252 }
254 /* Create Listbox with existing Zones */
255 $ZoneList = new divSelectBox("dNSZones");
256 $ZoneList -> SetHeight(254);
258 /* Add entries to divlist*/
259 $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
260 <input type='image' src='images/edittrash.png' name='delZone_%s'>";
261 foreach($this->Zones as $zone => $values ){
262 $ZoneList->AddEntry(array(
263 array("string" => $zone),
264 array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
265 array("string" => _("TTL")." : ".$values['dNSTTL']),
266 array("string" => _("Class")." : ".$values['dNSClass']),
267 array("string" =>str_replace("%s",base64_encode($zone),$editImg))
268 ));
269 }
271 /* Display tempalte */
272 $smarty->assign("ZoneList",$ZoneList->DrawList());
273 $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
274 return($display);
275 }
278 /* This funtion returns all used Zonenames */
279 function getUsedZoneNames()
280 {
281 $ret = array();
282 $ldap = $this->config->get_ldap_link();
283 $ldap->cd($this->config->current['BASE']);
284 $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName","tXTRecord"));
285 while($attr = $ldap->fetch()){
286 if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
287 $ret[$attr['zoneName'][0]][] = $attr['dn'];
288 }else{
289 $ret[$attr['zoneName'][0]][] = $attr['dn'];
290 }
291 }
292 return($ret);
293 }
296 /* Remove dns service */
297 function remove_from_parent()
298 {
299 $ldap = $this->config->get_ldap_link();
300 $ldap->cd($this->config->current['BASE']);
301 foreach($this->usedDNS as $dn){
302 $ldap->cd($dn);
303 $ldap->rmdir_recursive($dn);
304 }
305 show_ldap_error($ldap->get_error());
306 }
309 /* Save data to object */
310 function save_object()
311 {
312 }
315 /* Check supplied data */
316 function check()
317 {
318 $message= array();
319 return ($message);
320 }
323 /* Save to LDAP */
324 function save()
325 {
326 /* Ldap conenction / var initialization */
327 $ldap = $this->config->get_ldap_link();
328 $ldap->cd($this->config->current['BASE']);
329 $actions =array("update"=>array(),"add"=>array(),"delete"=>array());
331 /* Generate entries for all zones, and check if they must be updated deleted added */
332 foreach($this->Zones as $zone){
334 /* Get ldap syntax */
335 $tmp = $this->generate_LDAP_entries($zone);
337 /* Check if dn is new, or if entry was edited */
338 foreach($tmp as $key => $values){
339 if(isset($this->usedDNS[$key])){
340 $actions['update'][$key]=$values;
341 unset($this->usedDNS[$key]);
342 }else{
343 $actions['add'][$key] = $values;
344 }
345 }
346 }
348 /* Check which dns are not used anymore ...*/
349 foreach($this->usedDNS as $key => $values){
350 $actions['delete'][$key] = $values;
351 }
353 /* Remove deleted zones */
354 foreach($actions['delete'] as $dn => $attrs){
355 $ldap->cd($dn);
356 $ldap->rmdir_recursive($dn);
357 }
359 /* Add new zones */
360 foreach($actions['add'] as $dn => $attrs){
361 $ldap->cd($this->config->current['BASE']);
362 // $ldap->create_missing_trees($dn);
363 $ldap->cd($dn);
364 $ldap->add($attrs);
365 }
367 /* Update existing entries */
368 foreach($actions['update'] as $dn => $attrs){
369 $ldap->cd($dn);
370 $ldap->modify($attrs);
371 }
372 show_ldap_error($ldap->get_error());
373 }
376 /* This function generates ldap friendly output
377 of all changes for a single zone (reverse and forward)
378 */
379 function generate_LDAP_entries($zone)
380 {
381 $tmp = array();
382 $tmp['objectClass'] = array("top","dNSZone");
383 $tmp['dNSTTL'] = $zone['dNSTTL'];
384 $tmp['dNSClass'] = $zone['dNSClass'];
385 $tmp['relativeDomainName'] = "@";//$zone['relativeDomainName'];
387 $str = "";
388 foreach(array("sOAprimary","sOAmail","sOAserial","sOArefresh","sOAretry","sOAexpire","sOAttl") as $name){
389 $str .= $zone[$name]." ";
390 }
391 $tmp['sOARecord'] = $str;
394 /* Generate Record entries */
395 $arr = array("SigRecord","KeyRecord","aAAARecord","nSRecord","iaFSDBRecord","mInfoRecord","hInfoRecord","mXRecord","mDRecord","tXTRecord",
396 "LocRecord","nXTRecord","sRVRecord","nAPTRRecord","kXRecord","certRecord","a6Record","dSRecord","sSHFPRecord","rRSIGRecord","nSECRecord");
397 $aRecords = array();
398 foreach($arr as $ar){
399 if((isset($zone['Records']))&&(is_array($zone['Records']))){
400 foreach($zone['Records'] as $type){
401 if(($type['type'] == $ar)&&($type['status']!="deleted")){
402 $tmp[$ar][] = $type['value'];
403 }
404 }
405 }
406 }
408 /* Check if there are records removed,
409 if there are some removed records, the append an array
410 to ensure that these record types are deleted
411 */
412 if((isset($zone['Records']))&&(is_array($zone['Records']))){
413 foreach($zone['Records'] as $type){
414 if(isset($type['inittype'])){
415 if($type['type'] != $type['inittype']){
416 $tmp[$type['inittype']] = array();
417 }
418 }
419 }
420 }
421 /* generate forward entry */
422 $dn = "zoneName=".$zone['zoneName'].",".$this->dn;
423 $tmp2[$dn] = $tmp;
424 $tmp2[$dn]['zoneName'] = $zone['zoneName'];
426 /* generate reverse entry */
427 $dn = "zoneName=".$this->FlipIp($zone['ReverseZone']).".in-addr.arpa,".$this->dn;
428 $tmp2[$dn] = $tmp;
429 $tmp2[$dn]['tXTRecord'] ="zoneName=".$zone['zoneName'];
430 $tmp2[$dn]['zoneName'] = $this->FlipIp($zone['ReverseZone']).".in-addr.arpa";
432 return($tmp2);
433 }
437 }
439 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
440 ?>