Code

37471e7cc92c70795c666c40d7f6b277b98d9138
[gosa.git] / plugins / admin / systems / class_servDNS.inc
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= TRUE;
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];
52     $types = array();
54     /* Get all records */
55     $ldap = $this->config->get_ldap_link();
56     $ldap->cd($this->dn);
57     $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@))",array("*"));
59     while($attrs = $ldap->fetch()){
60       /* If relative domainname == cn
61        * Try to read dnsclass / TTl / zone
62        */
63       $this->usedDNS[$attrs['dn']] = $attrs;
64       if((isset($attrs['tXTRecord'][0]))&&(preg_match("/zoneName\=/",$attrs['tXTRecord'][0]))){
65         $zoneName= preg_replace("/zoneName\=/","",$attrs['tXTRecord'][0]);  
66         $types[$zoneName]['ReverseZone']  = preg_replace("/\.in\-addr\.arpa/","",$attrs['zoneName'][0]);
67         $types[$zoneName]['ReverseDN']    = $attrs['dn']; 
68       }else{
70         /* Generate SOA entry */
71         if(isset($attrs['sOARecord'][0])){
72           $tmp = split("\ ",$attrs['sOARecord'][0]) ;
73           $tmp2 = array();
74           $ar = array("0"=>"sOAprimary","1"=>"sOAmail","2"=>"sOAserial","3"=>"sOArefresh","4"=>"sOAretry","5"=>"sOAexpire","6"=>"sOAttl");
76           /* Assign soa vars */
77           foreach($ar as $key => $name){
78             if(isset($tmp[$key])){
79               $types[$attrs['zoneName'][0]][$name] = $tmp[$key];
80             }else{
81               $types[$attrs['zoneName'][0]][$name] = "";
82             }
83           }
84         }
86         /* Set TTL value */
87         if(isset($attrs['dNSTTL'][0])){
88           $types[$attrs['zoneName'][0]]['dNSTTL'] = $attrs['dNSTTL'][0];
89         }
91         /* Set dns Class*/
92         if(isset($attrs['dNSClass'][0])){
93           $types[$attrs['zoneName'][0]]['dNSClass'] = $attrs['dNSClass'][0];
94         }
96         /* Set zone Name */
97         if(isset($attrs['zoneName'][0])){
98           $types[$attrs['zoneName'][0]]['zoneName'] = $attrs['zoneName'][0];
99         }
101         /* Set status */
102 //        $types[$attrs['zoneName'][0]]['status']   ="edited"; 
103 //        $types[$attrs['zoneName'][0]]['Reversestatus']   ="edited"; 
104 //        $types[$attrs['zoneName'][0]]['ReverseDN']= $attrs['dn']; 
106         /* Create list with all used records */
107         foreach($this->RecordTypes as $name => $value){
109           /* If there is a record attribute  */
110           if(isset($attrs[$name])){
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 execute()
138   {
139     /* Call parent execute */
140     plugin::execute();
142     /* Fill templating stuff */
143     $smarty= get_smarty();
144     $display= "";
146     /* Do we need to flip is_account state? */
147     if (isset($_POST['modify_state'])){
148       $this->is_account= !$this->is_account;
149     }
151     /* Show tab dialog headers */
152     if ($this->is_account){
153       $display= $this->show_header(_("Remove DNS service"),
154           _("This server has DNS features enabled. You can disable them by clicking below."));
155     } else {
156       $display= $this->show_header(_("Add DNS service"),
157           _("This server has DNS features disabled. You can enable them by clicking below."));
158       return ($display);
159     }
161     if(isset($_POST['SaveZoneChanges'])){
162       $this->dialog->save_object();
163     
164       if(count($this->dialog->check())){
165         foreach($this->dialog->check() as $msgs){
166           print_red($msgs); 
167         }
168       }else{
170         $ret = $this->dialog->save();
171         unset($this->Zones[$this->dialog->InitiallyZoneName]);
172         $this->Zones[$ret['zoneName']]                  = $ret;
174         /*
175         if($this->dialog->isNew == true){
176           $this->Zones[$ret['zoneName']] = $ret;
177           $this->Zones[$ret['zoneName']] = "new";
178         }else{
180           if(!$this->dialog->isNew){
181     
182             $ForStatus = $this->Zones[$this->dialog->InitiallyZoneName]['status'];
183             $RevStatus = $this->Zones[$this->dialog->InitiallyZoneName]['Reversestatus'];
184   
185             if($ret['zoneName'] != $this->dialog->InitiallyZoneName){
186               if($this->Zones[$this->dialog->InitiallyZoneName]['status'] != "new"){
187                 $this->Zones[$this->dialog->InitiallyZoneName]['status'] = "deleted";
188               }else{
189                 unset($this->Zones[$this->dialog->InitiallyZoneName]);
190               }
191               $this->Zones[$ret['zoneName']]                  = $ret;
192               $this->Zones[$ret['zoneName']]['status']        = "new";
193               $this->Zones[$ret['zoneName']]['Reversestatus'] = $RevStatus;
195             }elseif($ret['ReverseZone'] != $this->dialog->InitiallyReverseZone){
196               if($this->Zones[$this->dialog->InitiallyZoneName]['Reversestatus'] != "new"){
197                 $this->Zones[$this->dialog->InitiallyZoneName]['Reversestatus'] = "deleted";
198               }else{
199                 unset($this->Zones[$this->dialog->InitiallyZoneName]);
200               }
201               $this->Zones[$ret['zoneName']]                  = $ret;
202               $this->Zones[$ret['zoneName']]['Reversestatus'] = "new";
203               $this->Zones[$ret['zoneName']]['status']        = $ForStatus;
205             }else{
206               $this->Zones[$ret['zoneName']]                  = $ret;
207               $this->Zones[$ret['zoneName']]['status']        = $ForStatus;
208               $this->Zones[$ret['zoneName']]['Reversestatus'] = $RevStatus;
209             }
210           }
211         }
212       */
213         $this->dialog = NULL;
214       }
215     }
216     if(isset($_POST['CancelZoneChanges'])){
217       $this->dialog = NULL;
218     }
219     /* Add empty new zone */
220     if(isset($_POST['AddZone'])){
221       $this->dialog = new servdnseditZone($this->config,$this->dn,$this->RecordTypes);
222     }
224     $once = false;
225     foreach( $_POST as $name => $value){
226       if(preg_match("/^editZone_/",$name)&&!$once){
227         $once =true;
228         $tmp = preg_replace("/^editZone_/","",$name);
229         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
230         $this->dialog= new servdnseditZone($this->config,$this->dn,$this->RecordTypes,$this->Zones[$tmp]);
231       }
232     }
234     if($this->dialog!= NULL){
235       $this->dialog->save_object();
236       $this->dialog->parent = $this;
237       return($this->dialog->execute());
238     }
240     /* Create Listbox with existing Zones */
241     $ZoneList = new divSelectBox("dNSZones");
242     $ZoneList -> SetHeight(254);
244     /* Add entries to divlist*/
245     $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>";
246     foreach($this->Zones as $zone => $values ){
247       $ZoneList->AddEntry(array(
248                                 array("string" => $zone),
249                                 array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
250                                 array("string" => _("TTL")." : ".$values['dNSTTL']),
251                                 array("string" => _("Class")." : ".$values['dNSClass']),
252                                 array("string" =>str_replace("%s",base64_encode($zone),$editImg))
253                           ));
254     }    
256     /* Display tempalte */
257     $smarty->assign("ZoneList",$ZoneList->DrawList());
258     $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
259     return($display);
260   }
262   function remove_from_parent()
263   {
264   }
267   /* Save data to object */
268   function save_object()
269   {
270   }
273   /* Check supplied data */
274   function check()
275   {
276     $message= array();
277     return ($message);
278   }
281   /* Save to LDAP */
282   function save()
283   {
284     $ldap = $this->config->get_ldap_link();
285     $ldap->cd($this->config->current['BASE']);
287     $actions =array("update"=>array(),"add"=>array(),"delete"=>array());
289     foreach($this->Zones as $zone){
290       $tmp = $this->generate_LDAP_entries($zone);
291       foreach($tmp as $key => $values){
292         if(isset($this->usedDNS[$key])){
293           $actions['update'][$key]=$values;
294           unset($this->usedDNS[$key]);
295         }else{
296           $actions['add'][$key] = $values;
297         }
298       }
299     }
300     foreach($this->usedDNS as $key => $values){
301       $actions['delete'][$key] = $values;
302     }
304     foreach($actions['delete'] as $dn => $attrs){
305       $ldap->cd($dn);
306       $ldap->rmDir($dn);
307     }
309     foreach($actions['add'] as $dn => $attrs){
310       $ldap->cd($this->config->current['BASE']);
311 //      $ldap->create_missing_trees($dn);
312       $ldap->cd($dn);
313       $ldap->add($attrs);
314     }
316     foreach($actions['update'] as $dn => $attrs){
317       $ldap->cd($dn);
318       $ldap->modify($attrs);
319     }
320     show_ldap_error($ldap->get_error());
321   }
324   function generate_LDAP_entries($zone)
325   {
326     $tmp = array();
327     $tmp['objectClass']           = array("top","dNSZone");
328     $tmp['dNSTTL']                = $zone['dNSTTL']; 
329     $tmp['dNSClass']              = $zone['dNSClass']; 
330     $tmp['relativeDomainName']    = "@";//$zone['relativeDomainName']; 
332     $str = "";
333     foreach(array("sOAprimary","sOAmail","sOAserial","sOArefresh","sOAretry","sOAexpire","sOAttl") as $name){
334       $str .= $zone[$name]." "; 
335     }
336     $tmp['sOARecord'] = $str;
339     /* Generate some attrs  */
340     $arr = array("SigRecord","KeyRecord","aAAARecord","nSRecord","iaFSDBRecord","mInfoRecord","hInfoRecord","mXRecord","mDRecord","tXTRecord",
341         "LocRecord","nXTRecord","sRVRecord","nAPTRRecord","kXRecord","certRecord","a6Record","dSRecord","sSHFPRecord","rRSIGRecord","nSECRecord");
342     $aRecords = array();
343     foreach($arr as $ar){
344       foreach($zone['Records'] as $type){
345         if(($type['type'] == $ar)&&($type['status']!="deleted")){
346           $tmp[$ar][] = $type['value'];
347         }
348       }
349     }
350     if(is_array($zone['Records'])){
351       foreach($zone['Records'] as $type){
352         if(isset($type['inittype'])){
353           if($type['type'] != $type['inittype']){
354             $tmp[$type['inittype']] = array();
355           }
356         }
357       }
358     }
359     $dn = "zoneName=".$zone['zoneName'].",".$this->dn; 
360     $tmp2[$dn] = $tmp;
361     $tmp2[$dn]['zoneName'] = $zone['zoneName'];
362     $dn = "zoneName=".$zone['ReverseZone'].".in-addr.arpa,".$this->dn;
363     $tmp2[$dn] = $tmp;
364     $tmp2[$dn]['tXTRecord'] ="zoneName=".$zone['zoneName'];
365     $tmp2[$dn]['zoneName'] = $zone['ReverseZone'].".in-addr.arpa";
367     return($tmp2);
368   }
374 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
375 ?>