1 <?php
3 require_once("class_goService.inc");
5 class servdns extends goService
6 {
7 /* CLI vars */
8 var $cli_summary= "Manage server basic objects";
9 var $cli_description= "Some longer text\nfor help";
10 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
12 /* attribute list for save action */
13 var $ignore_account = FALSE;
14 var $attributes = array();
15 var $objectclasses = array("whatever");
17 var $RecordTypes = array();
18 var $Zones = array();
19 var $dialog = NULL;
21 var $orig_dn = "";
23 var $initially_was_account;
25 /* ServerService tab vars */
26 var $conflicts = array("servdns");
27 var $DisplayName = "";
28 var $StatusFlag = "";
30 function servdns ($config, $dn= NULL, $parent= NULL)
31 {
32 plugin::plugin ($config, $dn, $parent);
34 $this->DisplayName = _("DNS service");
36 $this->orig_dn = $dn;
38 /* Get record types for zones
39 */
40 $this->RecordTypes = getDnsRecordTypes(true);
42 /* Get all zone Informations
43 */
44 $this->Zones = getDNSZoneEntries($config,$dn);
46 /* If there is at least one entry in this -> types, we have DNS enabled
47 */
48 if(count($this->Zones) == 0){
49 $this->is_account = false;
50 }else{
51 $this->is_account = true;
52 }
54 if($this->is_account){
55 @log::log("view","unknown/".get_class($this),$this->dn);
56 }
58 $this->initially_was_account = $this->is_account;
59 }
62 function execute()
63 {
64 /* Call parent execute
65 */
66 plugin::execute();
68 /* Fill templating stuff
69 */
70 $smarty= get_smarty();
71 $display= "";
73 /* Do we need to flip is_account state?
74 */
75 if (isset($_POST['modify_state'])){
76 $this->is_account= !$this->is_account;
77 }
79 if ($this->is_account){
80 $display= $this->show_disable_header(_("Remove DNS service"),
81 _("This server has DNS features enabled. You can disable them by clicking below."));
82 } else {
83 $display= $this->show_enable_header(_("Add DNS service"),
84 _("This server has DNS features disabled. You can enable them by clicking below."));
85 return ($display);
86 }
89 /* Edited or Added zone
90 */
91 if(isset($_POST['SaveZoneChanges'])){
92 $this->dialog->save_object();
94 /* Check for errors
95 */
96 if(count($this->dialog->check())){
97 foreach($this->dialog->check() as $msgs){
98 print_red($msgs);
99 }
100 }else{
101 /* add new/edited zone
102 */
103 $ret = $this->dialog->save();
104 if(!$this->dialog->isNew){
105 unset($this->Zones[$this->dialog->OldZoneName]);
106 }
107 $this->Zones[$ret['zoneName']] = $ret;
108 $this->dialog = NULL;
109 }
110 }
112 /* Cancel zone edit / new
113 */
114 if(isset($_POST['CancelZoneChanges'])){
115 $this->dialog = NULL;
116 }
118 /* Add empty new zone
119 */
120 if(isset($_POST['AddZone'])){
121 $this->dialog = new servdnseditZone($this->config,$this->dn);
122 }
124 /* Check for edit zone request
125 */
126 $once = false;
127 foreach( $_POST as $name => $value){
129 /* check all post for edit request
130 */
131 if(preg_match("/^editZone_/",$name)&&!$once){
132 $once =true;
133 $tmp = preg_replace("/^editZone_/","",$name);
134 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
135 $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$tmp]);
136 }
138 /* check posts for delete zone
139 */
140 if(preg_match("/^delZone_/",$name)&&!$once){
142 $once =true;
143 $tmp = preg_replace("/^delZone_/","",$name);
144 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
146 /* Initiate deletion
147 */
148 $this->RemoveZone($tmp);
149 }
150 }
152 /* Show dialog
153 */
154 if($this->dialog!= NULL){
155 $this->dialog->save_object();
156 $this->dialog->parent = $this;
157 return($this->dialog->execute());
158 }
160 /* Create Listbox with existing Zones
161 */
162 $ZoneList = new divSelectBox("dNSZones");
163 $ZoneList -> SetHeight(254);
165 /* Add entries to divlist
166 */
167 $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
168 <input type='image' src='images/edittrash.png' name='delZone_%s'>";
169 foreach($this->Zones as $zone => $values ){
170 $ZoneList->AddEntry(array(
171 array("string" => $zone),
172 array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
173 array("string" => _("TTL")." : ".$values['sOAttl']),
174 array("string" => _("Class")." : ".$values['dNSClass']),
175 array("string" =>str_replace("%s",base64_encode($zone),$editImg))
176 ));
177 }
179 /* Display tempalte
180 */
181 $smarty->assign("ZoneList",$ZoneList->DrawList());
182 $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
183 return($display);
184 }
187 /* Delete specified zone
188 */
189 function RemoveZone($id)
190 {
191 $zones = $this->getUsedZoneNames();
193 if(isset($this->Zones[$id]['InitialReverseZone'])){
194 $rev = FlipIp($this->Zones[$id]['InitialReverseZone']);
195 }else{
196 $rev = FlipIp($this->Zones[$id]['ReverseZone']);
197 }
199 $zonename = "";
200 if(isset($this->Zones[$id]['InitialzoneName'])){
201 $zonename= $this->Zones[$id]['InitialzoneName'];
202 }
204 $used = array();
206 /* Add Records which use this zoneName
207 */
208 if(isset($zones[$zonename])){
209 $used = array_merge($used,$zones[$zonename]);
210 }
212 /* Add Records which uses this reverse zone
213 */
214 if(isset($zones[$rev.".in-addr.arpa"])){
215 $used = array_merge($used,$zones[$rev.".in-addr.arpa"]);
216 }
218 /* There are still entries using this configuration
219 * Abort deletion
220 */
221 if(count($used)){
222 $i = 2;
223 $str ="";
224 foreach($used as $dn){
225 if($i > 0 && !preg_match("/,relativeDomainName=/",$dn)){
226 $i --;
227 $name = preg_replace("/^[^=]+=([^,]*),.*$/","\\1",$dn);
228 $zone = preg_replace("/^.*zoneName=([^,]*),.*$/","\\1",$dn);
229 $str.= $name.".".$zone." ";
230 }
231 }
233 /* Only show 2 dns in the error message
234 */
235 if(count($used)> 2) {
236 $str .=" ... ";
237 }
238 print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
239 return(false);
240 }else{
241 unset($this->Zones[$id]);
242 return(true);
243 }
244 }
247 /* This funtion returns all used Zonenames
248 */
249 function getUsedZoneNames()
250 {
251 $ret = array();
252 $ldap = $this->config->get_ldap_link();
253 $ldap->cd($this->config->current['BASE']);
254 $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
255 while($attr = $ldap->fetch()){
256 $ret[$attr['zoneName'][0]][] = $attr['dn'];
257 }
258 return($ret);
259 }
262 /* Remove dns service
263 */
264 function remove_from_parent()
265 {
266 if($this->initially_was_account){
267 $bool = true;
268 foreach($this->Zones as $key => $zone){
269 $bool= $bool & $this->RemoveZone($key);
270 }
272 if($bool){
273 $this->save();
274 }
275 return($bool);
276 }
277 }
280 /* Save to LDAP */
281 function save()
282 {
283 $ldap = $this->config->get_ldap_link();
284 $ldap->cd($this->config->current['BASE']);
286 /* Get differences
287 */
288 $old_dn = $this->orig_dn;
289 if($old_dn == "new"){
290 $old_dn = $this->dn;
291 }
293 $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$old_dn);
295 /* Updated zone entries if reverser or forward name has changed
296 * Must be done before moving entries, else the given dn is invalid
297 */
298 if(isset($tmp['zoneUpdates'])){
299 foreach($tmp['zoneUpdates'] as $dn => $attrs){
300 $ldap->cd($dn);
301 $ldap->modify($attrs);
302 @log::log("modfiy","unknown/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
303 show_ldap_error($ldap->get_error(), sprintf(_("Updating of system server/dns with dn '%s' failed."),$this->dn));
304 }
305 }
307 /* Delete dns
308 */
309 foreach($tmp['del'] as $dn => $del){
310 $ldap->cd($dn);
311 $ldap->rmdir_recursive($dn);
312 @log::log("remove","unknown/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
313 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/dns with dn '%s' failed."),$this->dn));
314 }
316 /* move follwoing entries
317 */
318 foreach($tmp['move'] as $src => $dst){
319 $this->recursive_move($src,$dst);
320 }
322 /* Add || Update new DNS entries
323 */
324 foreach($tmp['add'] as $dn => $attrs){
325 $ldap->cd($dn);
326 $ldap->cat($dn, array('dn'));
327 if(count($ldap->fetch())){
328 $ldap->cd($dn);
329 $ldap->modify ($attrs);
330 }else{
331 $ldap->cd($dn);
332 $ldap->add($attrs);
333 }
334 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
335 }
336 }
339 /* Directly save new status flag */
340 function setStatus($value)
341 {
342 if($value == "none") return;
343 if(!$this->initially_was_account) return;
344 if(empty($this->StatusFlag)) return;
345 $ldap = $this->config->get_ldap_link();
346 $ldap->cd($this->dn);
347 $ldap->cat($this->dn,array("objectClass"));
348 if($ldap->count()){
350 $tmp = $ldap->fetch();
351 for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
352 $attrs['objectClass'][] = $tmp['objectClass'][$i];
353 }
354 $flag = $this->StatusFlag;
355 $attrs[$flag] = $value;
356 $this->$flag = $value;
357 $ldap->modify($attrs);
358 show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for system server/dns with dn '%s' failed."),$this->dn));
359 $this->action_hook();
360 }
361 }
364 function getListEntry()
365 {
366 $fields = goService::getListEntry();
367 $fields['Message'] = _("DNS service");
368 $fields['AllowEdit'] = true;
369 return($fields);
370 }
373 /* Get updates for status flag */
374 function updateStatusState()
375 {
376 if(empty($this->StatusFlag)) return;
378 $attrs = array();
379 $flag = $this->StatusFlag;
380 $ldap = $this->config->get_ldap_link();
381 $ldap->cd($this->cn);
382 $ldap->cat($this->dn,array($flag));
383 if($ldap->count()){
384 $attrs = $ldap->fetch();
385 }
386 if(isset($attrs[$flag][0])){
387 $this->$flag = $attrs[$flag][0];
388 }
389 }
392 /* Return plugin informations for acl handling */
393 function plInfo()
394 {
395 return (array(
396 "plShortName" => _("DNS service"),
397 "plDescription" => _("DNS service")." ("._("Services").")",
398 "plSelfModify" => FALSE,
399 "plDepends" => array(),
400 "plPriority" => 83,
401 "plSection" => array("administration"),
402 "plCategory" => array("server"),
404 "plProvidedAcls"=> array(
405 "zoneName" =>_("Zone name"),
406 "ReverseZone" =>_("Reverse zone"),
407 "sOAprimary" =>_("Primary dns server"),
408 "sOAmail" =>_("Mail address"),
409 "sOAserial" =>_("Serial"),
410 "sOArefresh" =>_("Refresh"),
411 "sOAretry" =>_("Retry"),
412 "sOAexpire" =>_("Expire"),
413 "sOAttl" =>_("TTL"),
414 "zoneRecords" =>_("Zone records"))
415 ));
416 }
418 }
419 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
420 ?>