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();
16 var $Zones = array();
17 var $dialog = NULL;
19 var $orig_dn = "";
21 var $DNSinitially_was_account;
24 function servdns ($config, $dn= NULL)
25 {
26 plugin::plugin ($config, $dn);
28 $this->orig_dn = $dn;
30 /* Get record types for zones
31 */
32 $this->RecordTypes = getDnsRecordTypes(true);
34 /* Get all zone Informations
35 */
36 $this->Zones = getDNSZoneEntries($config,$dn);
38 /* If there is at least one entry in this -> types, we have DNS enabled
39 */
40 if(count($this->Zones) == 0){
41 $this->is_account = false;
42 }else{
43 $this->is_account = true;
44 }
45 $this->DNSinitially_was_account = $this->is_account;
46 }
49 function execute()
50 {
51 /* Call parent execute
52 */
53 plugin::execute();
55 /* Fill templating stuff
56 */
57 $smarty= get_smarty();
58 $display= "";
60 /* Do we need to flip is_account state?
61 */
62 if (isset($_POST['modify_state'])){
63 $this->is_account= !$this->is_account;
64 }
66 if ($this->is_account){
67 $display= $this->show_header(_("Remove DNS service"),
68 _("This server has DNS features enabled. You can disable them by clicking below."));
69 } else {
70 $display= $this->show_header(_("Add DNS service"),
71 _("This server has DNS features disabled. You can enable them by clicking below."));
72 return ($display);
73 }
76 /* Edited or Added zone
77 */
78 if(isset($_POST['SaveZoneChanges'])){
79 $this->dialog->save_object();
81 /* Check for errors
82 */
83 if(count($this->dialog->check())){
84 foreach($this->dialog->check() as $msgs){
85 print_red($msgs);
86 }
87 }else{
88 /* add new/edited zone
89 */
90 $ret = $this->dialog->save();
91 if(!$this->dialog->isNew){
92 unset($this->Zones[$this->dialog->OldZoneName]);
93 }
94 $this->Zones[$ret['zoneName']] = $ret;
95 $this->dialog = NULL;
96 }
97 }
99 /* Cancel zone edit / new
100 */
101 if(isset($_POST['CancelZoneChanges'])){
102 $this->dialog = NULL;
103 }
105 /* Add empty new zone
106 */
107 if(isset($_POST['AddZone'])){
108 $this->dialog = new servdnseditZone($this->config,$this->dn);
109 }
111 /* Check for edit zone request
112 */
113 $once = false;
114 foreach( $_POST as $name => $value){
116 /* check all post for edit request
117 */
118 if(preg_match("/^editZone_/",$name)&&!$once){
119 $once =true;
120 $tmp = preg_replace("/^editZone_/","",$name);
121 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
122 $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$tmp]);
123 }
125 /* check posts for delete zone
126 */
127 if(preg_match("/^delZone_/",$name)&&!$once){
129 $once =true;
130 $tmp = preg_replace("/^delZone_/","",$name);
131 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
133 /* Initiate deletion
134 */
135 $this->RemoveZone($tmp);
136 }
137 }
139 /* Show dialog
140 */
141 if($this->dialog!= NULL){
142 $this->dialog->save_object();
143 $this->dialog->parent = $this;
144 return($this->dialog->execute());
145 }
147 /* Create Listbox with existing Zones
148 */
149 $ZoneList = new divSelectBox("dNSZones");
150 $ZoneList -> SetHeight(254);
152 /* Add entries to divlist
153 */
154 $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
155 <input type='image' src='images/edittrash.png' name='delZone_%s'>";
156 foreach($this->Zones as $zone => $values ){
157 $ZoneList->AddEntry(array(
158 array("string" => $zone),
159 array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
160 array("string" => _("TTL")." : ".$values['sOAttl']),
161 array("string" => _("Class")." : ".$values['dNSClass']),
162 array("string" =>str_replace("%s",base64_encode($zone),$editImg))
163 ));
164 }
166 /* Display tempalte
167 */
168 $smarty->assign("ZoneList",$ZoneList->DrawList());
169 $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
170 return($display);
171 }
174 /* Delete specified zone
175 */
176 function RemoveZone($id)
177 {
178 $zones = $this->getUsedZoneNames();
180 if(isset($this->Zones[$id]['InitialReverseZone'])){
181 $rev = FlipIp($this->Zones[$id]['InitialReverseZone']);
182 }else{
183 $rev = FlipIp($this->Zones[$id]['ReverseZone']);
184 }
186 $zonename = "";
187 if(isset($this->Zones[$id]['InitialzoneName'])){
188 $zonename= $this->Zones[$id]['InitialzoneName'];
189 }
191 $used = array();
193 /* Add Records which use this zoneName
194 */
195 if(isset($zones[$zonename])){
196 $used = array_merge($used,$zones[$zonename]);
197 }
199 /* Add Records which uses this reverse zone
200 */
201 if(isset($zones[$rev.".in-addr.arpa"])){
202 $used = array_merge($used,$zones[$rev.".in-addr.arpa"]);
203 }
205 /* There are still entries using this configuration
206 * Abort deletion
207 */
208 if(count($used)){
209 $i = 2;
210 $str ="";
211 foreach($used as $dn){
212 if($i > 0 ){
213 $i --;
214 $str.=$dn." ";
215 }
216 }
218 /* Only show 2 dns in the error message
219 */
220 if(count($used)> 2) {
221 $str .=" ... ";
222 }
223 print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
225 }else{
226 unset($this->Zones[$id]);
227 }
228 }
231 /* This funtion returns all used Zonenames
232 */
233 function getUsedZoneNames()
234 {
235 $ret = array();
236 $ldap = $this->config->get_ldap_link();
237 $ldap->cd($this->config->current['BASE']);
238 $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
239 while($attr = $ldap->fetch()){
240 $ret[$attr['zoneName'][0]][] = $attr['dn'];
241 }
242 return($ret);
243 }
246 /* Remove dns service
247 */
248 function remove_from_parent()
249 {
250 if(!$this->DNSinitially_was_account){
251 return;
252 }
253 print_red("Can't remove dns yet. returning without remove.");
254 return;
255 $ldap = $this->config->get_ldap_link();
256 $ldap->ls("(&(objectClass=dNSZone)(zoneName=*)(relativeDomainName=@))",$this->orig_dn,array("relativeDomainName","zoneName"));
257 while($attr = $ldap->fetch()){
258 $ldap->cd($attr['dn']);
259 $ldap->rmDir($attr['dn']);
260 }
261 show_ldap_error($ldap->get_error(), _("Removing DNS service failed"));
262 }
265 /* Save to LDAP */
266 function save()
267 {
268 $ldap = $this->config->get_ldap_link();
269 $ldap->cd($this->config->current['BASE']);
271 /* Get differences
272 */
273 $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$this->orig_dn);
275 /* Updated zone entries if reverser or forward name has changed
276 * Must be done before moving entries, else the given dn is invalid
277 */
278 if(isset($tmp['zoneUpdates'])){
279 foreach($tmp['zoneUpdates'] as $dn => $attrs){
280 $ldap->cd($dn);
281 $ldap->modify($attrs);
282 show_ldap_error("Zone:".$ldap->get_error(), _("Updating DNS service failed"));
283 }
284 }
286 /* Delete dns
287 */
288 foreach($tmp['del'] as $dn => $del){
289 $ldap->cd($dn);
290 $ldap->rmdir_recursive($dn);
291 show_ldap_error($ldap->get_error(), _("Removing DNS entries failed"));
292 }
294 /* move follwoing entries
295 */
296 foreach($tmp['move'] as $src => $dst){
297 $this->recursive_move($src,$dst);
298 }
300 /* Add || Update new DNS entries
301 */
302 foreach($tmp['add'] as $dn => $attrs){
303 $ldap->cd($dn);
304 $ldap->cat($dn, array('dn'));
305 if(count($ldap->fetch())){
306 $ldap->cd($dn);
307 $ldap->modify ($attrs);
308 }else{
309 $ldap->cd($dn);
310 $ldap->add($attrs);
311 }
312 show_ldap_error($ldap->get_error(), _("Saving DNS entries failed"));
313 }
314 }
315 }
316 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
317 ?>