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 $initially_was_account;
23 /* ServerService tab vars */
24 var $conflicts = array("servdns");
25 var $DisplayName = "";
26 var $StatusFlag = "";
28 function servdns ($config, $dn= NULL, $parent= NULL)
29 {
30 plugin::plugin ($config, $dn, $parent);
32 $this->DisplayName = _("Domain name system service");
34 $this->orig_dn = $dn;
36 /* Get record types for zones
37 */
38 $this->RecordTypes = getDnsRecordTypes(true);
40 /* Get all zone Informations
41 */
42 $this->Zones = getDNSZoneEntries($config,$dn);
44 /* If there is at least one entry in this -> types, we have DNS enabled
45 */
46 if(count($this->Zones) == 0){
47 $this->is_account = false;
48 }else{
49 $this->is_account = true;
50 }
51 $this->initially_was_account = $this->is_account;
52 }
55 function execute()
56 {
57 /* Call parent execute
58 */
59 plugin::execute();
61 /* Fill templating stuff
62 */
63 $smarty= get_smarty();
64 $display= "";
66 /* Do we need to flip is_account state?
67 */
68 if (isset($_POST['modify_state'])){
69 $this->is_account= !$this->is_account;
70 }
72 if ($this->is_account){
73 $display= $this->show_disable_header(_("Remove DNS service"),
74 _("This server has DNS features enabled. You can disable them by clicking below."));
75 } else {
76 $display= $this->show_enable_header(_("Add DNS service"),
77 _("This server has DNS features disabled. You can enable them by clicking below."));
78 return ($display);
79 }
82 /* Edited or Added zone
83 */
84 if(isset($_POST['SaveZoneChanges'])){
85 $this->dialog->save_object();
87 /* Check for errors
88 */
89 if(count($this->dialog->check())){
90 foreach($this->dialog->check() as $msgs){
91 print_red($msgs);
92 }
93 }else{
94 /* add new/edited zone
95 */
96 $ret = $this->dialog->save();
97 if(!$this->dialog->isNew){
98 unset($this->Zones[$this->dialog->OldZoneName]);
99 }
100 $this->Zones[$ret['zoneName']] = $ret;
101 $this->dialog = NULL;
102 }
103 }
105 /* Cancel zone edit / new
106 */
107 if(isset($_POST['CancelZoneChanges'])){
108 $this->dialog = NULL;
109 }
111 /* Add empty new zone
112 */
113 if(isset($_POST['AddZone'])){
114 $this->dialog = new servdnseditZone($this->config,$this->dn);
115 }
117 /* Check for edit zone request
118 */
119 $once = false;
120 foreach( $_POST as $name => $value){
122 /* check all post for edit request
123 */
124 if(preg_match("/^editZone_/",$name)&&!$once){
125 $once =true;
126 $tmp = preg_replace("/^editZone_/","",$name);
127 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
128 $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$tmp]);
129 }
131 /* check posts for delete zone
132 */
133 if(preg_match("/^delZone_/",$name)&&!$once){
135 $once =true;
136 $tmp = preg_replace("/^delZone_/","",$name);
137 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
139 /* Initiate deletion
140 */
141 $this->RemoveZone($tmp);
142 }
143 }
145 /* Show dialog
146 */
147 if($this->dialog!= NULL){
148 $this->dialog->save_object();
149 $this->dialog->parent = $this;
150 return($this->dialog->execute());
151 }
153 /* Create Listbox with existing Zones
154 */
155 $ZoneList = new divSelectBox("dNSZones");
156 $ZoneList -> SetHeight(254);
158 /* Add entries to divlist
159 */
160 $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
161 <input type='image' src='images/edittrash.png' name='delZone_%s'>";
162 foreach($this->Zones as $zone => $values ){
163 $ZoneList->AddEntry(array(
164 array("string" => $zone),
165 array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
166 array("string" => _("TTL")." : ".$values['sOAttl']),
167 array("string" => _("Class")." : ".$values['dNSClass']),
168 array("string" =>str_replace("%s",base64_encode($zone),$editImg))
169 ));
170 }
172 /* Display tempalte
173 */
174 $smarty->assign("ZoneList",$ZoneList->DrawList());
175 $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
176 return($display);
177 }
180 /* Delete specified zone
181 */
182 function RemoveZone($id)
183 {
184 $zones = $this->getUsedZoneNames();
186 if(isset($this->Zones[$id]['InitialReverseZone'])){
187 $rev = FlipIp($this->Zones[$id]['InitialReverseZone']);
188 }else{
189 $rev = FlipIp($this->Zones[$id]['ReverseZone']);
190 }
192 $zonename = "";
193 if(isset($this->Zones[$id]['InitialzoneName'])){
194 $zonename= $this->Zones[$id]['InitialzoneName'];
195 }
197 $used = array();
199 /* Add Records which use this zoneName
200 */
201 if(isset($zones[$zonename])){
202 $used = array_merge($used,$zones[$zonename]);
203 }
205 /* Add Records which uses this reverse zone
206 */
207 if(isset($zones[$rev.".in-addr.arpa"])){
208 $used = array_merge($used,$zones[$rev.".in-addr.arpa"]);
209 }
211 /* There are still entries using this configuration
212 * Abort deletion
213 */
214 if(count($used)){
215 $i = 2;
216 $str ="";
217 foreach($used as $dn){
218 if($i > 0 ){
219 $i --;
220 $str.=$dn." ";
221 }
222 }
224 /* Only show 2 dns in the error message
225 */
226 if(count($used)> 2) {
227 $str .=" ... ";
228 }
229 print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
230 return(false);
231 }else{
232 unset($this->Zones[$id]);
233 return(true);
234 }
235 }
238 /* This funtion returns all used Zonenames
239 */
240 function getUsedZoneNames()
241 {
242 $ret = array();
243 $ldap = $this->config->get_ldap_link();
244 $ldap->cd($this->config->current['BASE']);
245 $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
246 while($attr = $ldap->fetch()){
247 $ret[$attr['zoneName'][0]][] = $attr['dn'];
248 }
249 return($ret);
250 }
253 /* Remove dns service
254 */
255 function remove_from_parent()
256 {
257 if($this->initially_was_account){
258 $bool = true;
259 foreach($this->Zones as $key => $zone){
260 $bool= $bool & $this->RemoveZone($key);
261 }
263 if($bool){
264 $this->save();
265 }
266 return($bool);
267 }
268 }
271 /* Save to LDAP */
272 function save()
273 {
274 $ldap = $this->config->get_ldap_link();
275 $ldap->cd($this->config->current['BASE']);
277 /* Get differences
278 */
279 $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$this->orig_dn);
281 /* Updated zone entries if reverser or forward name has changed
282 * Must be done before moving entries, else the given dn is invalid
283 */
284 if(isset($tmp['zoneUpdates'])){
285 foreach($tmp['zoneUpdates'] as $dn => $attrs){
286 $ldap->cd($dn);
287 $ldap->modify($attrs);
288 show_ldap_error($ldap->get_error(), sprintf(_("Updating of system server/dns with dn '%s' failed."),$this->dn));
289 }
290 }
292 /* Delete dns
293 */
294 foreach($tmp['del'] as $dn => $del){
295 $ldap->cd($dn);
296 $ldap->rmdir_recursive($dn);
297 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/dns with dn '%s' failed."),$this->dn));
298 }
300 /* move follwoing entries
301 */
302 foreach($tmp['move'] as $src => $dst){
303 $this->recursive_move($src,$dst);
304 }
306 /* Add || Update new DNS entries
307 */
308 foreach($tmp['add'] as $dn => $attrs){
309 $ldap->cd($dn);
310 $ldap->cat($dn, array('dn'));
311 if(count($ldap->fetch())){
312 $ldap->cd($dn);
313 $ldap->modify ($attrs);
314 }else{
315 $ldap->cd($dn);
316 $ldap->add($attrs);
317 }
318 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
319 }
320 }
323 /* Directly save new status flag */
324 function setStatus($value)
325 {
326 if($value == "none") return;
327 if(!$this->initially_was_account) return;
328 if(empty($this->StatusFlag)) return;
329 $ldap = $this->config->get_ldap_link();
330 $ldap->cd($this->dn);
331 $ldap->cat($this->dn,array("objectClass"));
332 if($ldap->count()){
334 $tmp = $ldap->fetch();
335 for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
336 $attrs['objectClass'][] = $tmp['objectClass'][$i];
337 }
338 $flag = $this->StatusFlag;
339 $attrs[$flag] = $value;
340 $this->$flag = $value;
341 $ldap->modify($attrs);
342 show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for system server/dns with dn '%s' failed."),$this->dn));
343 $this->action_hook();
344 }
345 }
348 function getListEntry()
349 {
350 $this->updateStatusState();
351 $flag = $this->StatusFlag;
352 if(empty($flag)){
353 $fields['Status'] = "";
354 }else{
355 $fields['Status'] = $this->$flag;
356 }
357 $fields['Message'] = _("DNS service");
358 $fields['AllowStart'] = true;
359 $fields['AllowStop'] = true;
360 $fields['AllowRestart'] = true;
361 $fields['AllowRemove']= true;
362 $fields['AllowEdit'] = true;
363 return($fields);
364 }
367 /* Get updates for status flag */
368 function updateStatusState()
369 {
370 if(empty($this->StatusFlag)) return;
372 $attrs = array();
373 $flag = $this->StatusFlag;
374 $ldap = $this->config->get_ldap_link();
375 $ldap->cd($this->cn);
376 $ldap->cat($this->dn,array($flag));
377 if($ldap->count()){
378 $attrs = $ldap->fetch();
379 }
380 if(isset($attrs[$flag][0])){
381 $this->$flag = $attrs[$flag][0];
382 }
383 }
386 /* Return plugin informations for acl handling */
387 function plInfo()
388 {
389 return (array(
390 "plShortName" => _("DNS"),
391 "plDescription" => _("DNS service"),
392 "plSelfModify" => FALSE,
393 "plDepends" => array(),
394 "plPriority" => 0,
395 "plSection" => array("administration"),
396 "plCategory" => array("server"),
398 "plProvidedAcls"=> array(
399 "zoneName" =>_("Zone name"),
400 "ReverseZone" =>_("Reverse zone"),
401 "sOAprimary" =>_("Primary dns server"),
402 "sOAmail" =>_("Mail address"),
403 "sOAserial" =>_("Serial"),
404 "sOArefresh" =>_("Refresh"),
405 "sOAretry" =>_("Retry"),
406 "sOAexpire" =>_("Expire"),
407 "sOAttl" =>_("TTL"),
408 "zoneRecords" =>_("Zone records"))
409 ));
410 }
412 }
413 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
414 ?>