121b4318a4d3f2e93fda3f41e766e18c488cea20
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 }
137 function FlipIp($ip)
138 {
139 $tmp = array_reverse(split("\.",$ip));
140 $new = "";
141 foreach($tmp as $section){
142 $new .= $section.".";
143 }
144 return(preg_replace("/.$/","",$new));
145 }
147 function execute()
148 {
149 /* Call parent execute */
150 plugin::execute();
152 /* Fill templating stuff */
153 $smarty= get_smarty();
154 $display= "";
156 /* Do we need to flip is_account state? */
157 if (isset($_POST['modify_state'])){
158 $this->is_account= !$this->is_account;
159 }
161 /* Show tab dialog headers */
162 if ($this->is_account){
163 $display= $this->show_header(_("Remove DNS service"),
164 _("This server has DNS features enabled. You can disable them by clicking below."));
165 } else {
166 $display= $this->show_header(_("Add DNS service"),
167 _("This server has DNS features disabled. You can enable them by clicking below."));
168 return ($display);
169 }
171 /* Edited or Added zone hould be saved saved */
172 if(isset($_POST['SaveZoneChanges'])){
173 $this->dialog->save_object();
175 /* Check if noting went wrong */
176 if(count($this->dialog->check())){
177 foreach($this->dialog->check() as $msgs){
178 print_red($msgs);
179 }
180 }else{
182 /* add new/edited zone */
183 $ret = $this->dialog->save();
184 unset($this->Zones[$this->dialog->InitiallyZoneName]);
185 $this->Zones[$ret['zoneName']] = $ret;
186 $this->dialog = NULL;
187 }
188 }
190 /* Cancel zone edit / new */
191 if(isset($_POST['CancelZoneChanges'])){
192 $this->dialog = NULL;
193 }
195 /* Add empty new zone */
196 if(isset($_POST['AddZone'])){
197 $this->dialog = new servdnseditZone($this->config,$this->dn,$this->RecordTypes);
198 }
200 /* Check for edit zone request */
201 $once = false;
202 foreach( $_POST as $name => $value){
204 /* check all post for edit request */
205 if(preg_match("/^editZone_/",$name)&&!$once){
206 $once =true;
207 $tmp = preg_replace("/^editZone_/","",$name);
208 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
209 $this->dialog= new servdnseditZone($this->config,$this->dn,$this->RecordTypes,$this->Zones[$tmp]);
210 }
212 /* check posts for delete zone */
213 if(preg_match("/^delZone_/",$name)&&!$once){
214 $once =true;
215 $tmp = preg_replace("/^delZone_/","",$name);
216 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
217 unset($this->Zones[$tmp]);
218 }
219 }
221 /* Show dialog */
222 if($this->dialog!= NULL){
223 $this->dialog->save_object();
224 $this->dialog->parent = $this;
225 return($this->dialog->execute());
226 }
228 /* Create Listbox with existing Zones */
229 $ZoneList = new divSelectBox("dNSZones");
230 $ZoneList -> SetHeight(254);
232 /* Add entries to divlist*/
233 $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
234 <input type='image' src='images/edittrash.png' name='delZone_%s'>";
235 foreach($this->Zones as $zone => $values ){
236 $ZoneList->AddEntry(array(
237 array("string" => $zone),
238 array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
239 array("string" => _("TTL")." : ".$values['dNSTTL']),
240 array("string" => _("Class")." : ".$values['dNSClass']),
241 array("string" =>str_replace("%s",base64_encode($zone),$editImg))
242 ));
243 }
245 /* Display tempalte */
246 $smarty->assign("ZoneList",$ZoneList->DrawList());
247 $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
248 return($display);
249 }
251 /* Remove dns service */
252 function remove_from_parent()
253 {
254 $ldap = $this->config->get_ldap_link();
255 $ldap->cd($this->config->current['BASE']);
256 foreach($this->usedDNS as $dn){
257 $ldap->cd($dn);
258 $ldap->rmdir_recursive($dn);
259 }
260 show_ldap_error($ldap->get_error());
261 }
264 /* Save data to object */
265 function save_object()
266 {
267 }
270 /* Check supplied data */
271 function check()
272 {
273 $message= array();
274 return ($message);
275 }
278 /* Save to LDAP */
279 function save()
280 {
281 /* Ldap conenction / var initialization */
282 $ldap = $this->config->get_ldap_link();
283 $ldap->cd($this->config->current['BASE']);
284 $actions =array("update"=>array(),"add"=>array(),"delete"=>array());
286 /* Generate entries for all zones, and check if they must be updated deleted added */
287 foreach($this->Zones as $zone){
289 /* Get ldap syntax */
290 $tmp = $this->generate_LDAP_entries($zone);
292 /* Check if dn is new, or if entry was edited */
293 foreach($tmp as $key => $values){
294 if(isset($this->usedDNS[$key])){
295 $actions['update'][$key]=$values;
296 unset($this->usedDNS[$key]);
297 }else{
298 $actions['add'][$key] = $values;
299 }
300 }
301 }
303 /* Check which dns are not used anymore ...*/
304 foreach($this->usedDNS as $key => $values){
305 $actions['delete'][$key] = $values;
306 }
308 /* Remove deleted zones */
309 foreach($actions['delete'] as $dn => $attrs){
310 $ldap->cd($dn);
311 $ldap->rmdir_recursive($dn);
312 }
314 /* Add new zones */
315 foreach($actions['add'] as $dn => $attrs){
316 $ldap->cd($this->config->current['BASE']);
317 // $ldap->create_missing_trees($dn);
318 $ldap->cd($dn);
319 $ldap->add($attrs);
320 }
322 /* Update existing entries */
323 foreach($actions['update'] as $dn => $attrs){
324 $ldap->cd($dn);
325 $ldap->modify($attrs);
326 }
327 show_ldap_error($ldap->get_error());
328 }
331 /* This function generates ldap friendly output
332 of all changes for a single zone (reverse and forward)
333 */
334 function generate_LDAP_entries($zone)
335 {
336 $tmp = array();
337 $tmp['objectClass'] = array("top","dNSZone");
338 $tmp['dNSTTL'] = $zone['dNSTTL'];
339 $tmp['dNSClass'] = $zone['dNSClass'];
340 $tmp['relativeDomainName'] = "@";//$zone['relativeDomainName'];
342 $str = "";
343 foreach(array("sOAprimary","sOAmail","sOAserial","sOArefresh","sOAretry","sOAexpire","sOAttl") as $name){
344 $str .= $zone[$name]." ";
345 }
346 $tmp['sOARecord'] = $str;
349 /* Generate Record entries */
350 $arr = array("SigRecord","KeyRecord","aAAARecord","nSRecord","iaFSDBRecord","mInfoRecord","hInfoRecord","mXRecord","mDRecord","tXTRecord",
351 "LocRecord","nXTRecord","sRVRecord","nAPTRRecord","kXRecord","certRecord","a6Record","dSRecord","sSHFPRecord","rRSIGRecord","nSECRecord");
352 $aRecords = array();
353 foreach($arr as $ar){
354 if((isset($zone['Records']))&&(is_array($zone['Records']))){
355 foreach($zone['Records'] as $type){
356 if(($type['type'] == $ar)&&($type['status']!="deleted")){
357 $tmp[$ar][] = $type['value'];
358 }
359 }
360 }
361 }
363 /* Check if there are records removed,
364 if there are some removed records, the append an array
365 to ensure that these record types are deleted
366 */
367 if((isset($zone['Records']))&&(is_array($zone['Records']))){
368 foreach($zone['Records'] as $type){
369 if(isset($type['inittype'])){
370 if($type['type'] != $type['inittype']){
371 $tmp[$type['inittype']] = array();
372 }
373 }
374 }
375 }
376 /* generate forward entry */
377 $dn = "zoneName=".$zone['zoneName'].",".$this->dn;
378 $tmp2[$dn] = $tmp;
379 $tmp2[$dn]['zoneName'] = $zone['zoneName'];
381 /* generate reverse entry */
382 $dn = "zoneName=".$this->FlipIp($zone['ReverseZone']).".in-addr.arpa,".$this->dn;
383 $tmp2[$dn] = $tmp;
384 $tmp2[$dn]['tXTRecord'] ="zoneName=".$zone['zoneName'];
385 $tmp2[$dn]['zoneName'] = $this->FlipIp($zone['ReverseZone']).".in-addr.arpa";
387 return($tmp2);
388 }
392 }
394 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
395 ?>