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 }
53 $this->initially_was_account = $this->is_account;
54 }
57 function execute()
58 {
59 /* Call parent execute
60 */
61 plugin::execute();
63 /* Fill templating stuff
64 */
65 $smarty= get_smarty();
66 $display= "";
68 /* Do we need to flip is_account state?
69 */
70 if (isset($_POST['modify_state'])){
71 $this->is_account= !$this->is_account;
72 }
74 if ($this->is_account){
75 $display= $this->show_disable_header(_("Remove DNS service"),
76 _("This server has DNS features enabled. You can disable them by clicking below."));
77 } else {
78 $display= $this->show_enable_header(_("Add DNS service"),
79 _("This server has DNS features disabled. You can enable them by clicking below."));
80 return ($display);
81 }
84 /* Edited or Added zone
85 */
86 if(isset($_POST['SaveZoneChanges'])){
87 $this->dialog->save_object();
89 /* Check for errors
90 */
91 if(count($this->dialog->check())){
92 foreach($this->dialog->check() as $msgs){
93 print_red($msgs);
94 }
95 }else{
96 /* add new/edited zone
97 */
98 $ret = $this->dialog->save();
99 if(!$this->dialog->isNew){
100 unset($this->Zones[$this->dialog->OldZoneName]);
101 }
102 $this->Zones[$ret['zoneName']] = $ret;
103 $this->dialog = NULL;
104 }
105 }
107 /* Cancel zone edit / new
108 */
109 if(isset($_POST['CancelZoneChanges'])){
110 $this->dialog = NULL;
111 }
113 /* Add empty new zone
114 */
115 if(isset($_POST['AddZone'])){
116 $this->dialog = new servdnseditZone($this->config,$this->dn);
117 }
119 /* Check for edit zone request
120 */
121 $once = false;
122 foreach( $_POST as $name => $value){
124 /* check all post for edit request
125 */
126 if(preg_match("/^editZone_/",$name)&&!$once){
127 $once =true;
128 $tmp = preg_replace("/^editZone_/","",$name);
129 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
130 $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$tmp]);
131 }
133 /* check posts for delete zone
134 */
135 if(preg_match("/^delZone_/",$name)&&!$once){
137 $once =true;
138 $tmp = preg_replace("/^delZone_/","",$name);
139 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
141 /* Initiate deletion
142 */
143 $this->RemoveZone($tmp);
144 }
145 }
147 /* Show dialog
148 */
149 if($this->dialog!= NULL){
150 $this->dialog->save_object();
151 $this->dialog->parent = $this;
152 return($this->dialog->execute());
153 }
155 /* Create Listbox with existing Zones
156 */
157 $ZoneList = new divSelectBox("dNSZones");
158 $ZoneList -> SetHeight(254);
160 /* Add entries to divlist
161 */
162 $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
163 <input type='image' src='images/edittrash.png' name='delZone_%s'>";
164 foreach($this->Zones as $zone => $values ){
165 $ZoneList->AddEntry(array(
166 array("string" => $zone),
167 array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
168 array("string" => _("TTL")." : ".$values['sOAttl']),
169 array("string" => _("Class")." : ".$values['dNSClass']),
170 array("string" =>str_replace("%s",base64_encode($zone),$editImg))
171 ));
172 }
174 /* Display tempalte
175 */
176 $smarty->assign("ZoneList",$ZoneList->DrawList());
177 $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
178 return($display);
179 }
182 /* Delete specified zone
183 */
184 function RemoveZone($id)
185 {
186 $zones = $this->getUsedZoneNames();
188 if(isset($this->Zones[$id]['InitialReverseZone'])){
189 $rev = FlipIp($this->Zones[$id]['InitialReverseZone']);
190 }else{
191 $rev = FlipIp($this->Zones[$id]['ReverseZone']);
192 }
194 $zonename = "";
195 if(isset($this->Zones[$id]['InitialzoneName'])){
196 $zonename= $this->Zones[$id]['InitialzoneName'];
197 }
199 $used = array();
201 /* Add Records which use this zoneName
202 */
203 if(isset($zones[$zonename])){
204 $used = array_merge($used,$zones[$zonename]);
205 }
207 /* Add Records which uses this reverse zone
208 */
209 if(isset($zones[$rev.".in-addr.arpa"])){
210 $used = array_merge($used,$zones[$rev.".in-addr.arpa"]);
211 }
213 /* There are still entries using this configuration
214 * Abort deletion
215 */
216 if(count($used)){
217 $i = 2;
218 $str ="";
219 foreach($used as $dn){
220 if($i > 0 && !preg_match("/,relativeDomainName=/",$dn)){
221 $i --;
222 $name = preg_replace("/^[^=]+=([^,]*),.*$/","\\1",$dn);
223 $zone = preg_replace("/^.*zoneName=([^,]*),.*$/","\\1",$dn);
224 $str.= $name.".".$zone." ";
225 }
226 }
228 /* Only show 2 dns in the error message
229 */
230 if(count($used)> 2) {
231 $str .=" ... ";
232 }
233 print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
234 return(false);
235 }else{
236 unset($this->Zones[$id]);
237 return(true);
238 }
239 }
242 /* This funtion returns all used Zonenames
243 */
244 function getUsedZoneNames()
245 {
246 $ret = array();
247 $ldap = $this->config->get_ldap_link();
248 $ldap->cd($this->config->current['BASE']);
249 $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
250 while($attr = $ldap->fetch()){
251 $ret[$attr['zoneName'][0]][] = $attr['dn'];
252 }
253 return($ret);
254 }
257 /* Remove dns service
258 */
259 function remove_from_parent()
260 {
261 if($this->initially_was_account){
262 $bool = true;
263 foreach($this->Zones as $key => $zone){
264 $bool= $bool & $this->RemoveZone($key);
265 }
267 if($bool){
268 $this->save();
269 }
270 return($bool);
271 }
272 }
275 /* Save to LDAP */
276 function save()
277 {
278 $ldap = $this->config->get_ldap_link();
279 $ldap->cd($this->config->current['BASE']);
281 /* Get differences
282 */
283 $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$this->orig_dn);
285 /* Updated zone entries if reverser or forward name has changed
286 * Must be done before moving entries, else the given dn is invalid
287 */
288 if(isset($tmp['zoneUpdates'])){
289 foreach($tmp['zoneUpdates'] as $dn => $attrs){
290 $ldap->cd($dn);
291 $ldap->modify($attrs);
292 show_ldap_error($ldap->get_error(), sprintf(_("Updating of system server/dns with dn '%s' failed."),$this->dn));
293 }
294 }
296 /* Delete dns
297 */
298 foreach($tmp['del'] as $dn => $del){
299 $ldap->cd($dn);
300 $ldap->rmdir_recursive($dn);
301 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/dns with dn '%s' failed."),$this->dn));
302 }
304 /* move follwoing entries
305 */
306 foreach($tmp['move'] as $src => $dst){
307 $this->recursive_move($src,$dst);
308 }
310 /* Add || Update new DNS entries
311 */
312 foreach($tmp['add'] as $dn => $attrs){
313 $ldap->cd($dn);
314 $ldap->cat($dn, array('dn'));
315 if(count($ldap->fetch())){
316 $ldap->cd($dn);
317 $ldap->modify ($attrs);
318 }else{
319 $ldap->cd($dn);
320 $ldap->add($attrs);
321 }
322 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
323 }
324 }
327 /* Directly save new status flag */
328 function setStatus($value)
329 {
330 if($value == "none") return;
331 if(!$this->initially_was_account) return;
332 if(empty($this->StatusFlag)) return;
333 $ldap = $this->config->get_ldap_link();
334 $ldap->cd($this->dn);
335 $ldap->cat($this->dn,array("objectClass"));
336 if($ldap->count()){
338 $tmp = $ldap->fetch();
339 for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
340 $attrs['objectClass'][] = $tmp['objectClass'][$i];
341 }
342 $flag = $this->StatusFlag;
343 $attrs[$flag] = $value;
344 $this->$flag = $value;
345 $ldap->modify($attrs);
346 show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for system server/dns with dn '%s' failed."),$this->dn));
347 $this->action_hook();
348 }
349 }
352 function getListEntry()
353 {
354 $fields = goService::getListEntry();
355 $fields['Message'] = _("DNS service");
356 $fields['AllowEdit'] = true;
357 return($fields);
358 }
361 /* Get updates for status flag */
362 function updateStatusState()
363 {
364 if(empty($this->StatusFlag)) return;
366 $attrs = array();
367 $flag = $this->StatusFlag;
368 $ldap = $this->config->get_ldap_link();
369 $ldap->cd($this->cn);
370 $ldap->cat($this->dn,array($flag));
371 if($ldap->count()){
372 $attrs = $ldap->fetch();
373 }
374 if(isset($attrs[$flag][0])){
375 $this->$flag = $attrs[$flag][0];
376 }
377 }
380 /* Return plugin informations for acl handling */
381 function plInfo()
382 {
383 return (array(
384 "plShortName" => _("DNS service"),
385 "plDescription" => _("DNS service")." ("._("Services").")",
386 "plSelfModify" => FALSE,
387 "plDepends" => array(),
388 "plPriority" => 83,
389 "plSection" => array("administration"),
390 "plCategory" => array("server"),
392 "plProvidedAcls"=> array(
393 "zoneName" =>_("Zone name"),
394 "ReverseZone" =>_("Reverse zone"),
395 "sOAprimary" =>_("Primary dns server"),
396 "sOAmail" =>_("Mail address"),
397 "sOAserial" =>_("Serial"),
398 "sOArefresh" =>_("Refresh"),
399 "sOAretry" =>_("Retry"),
400 "sOAexpire" =>_("Expire"),
401 "sOAttl" =>_("TTL"),
402 "zoneRecords" =>_("Zone records"))
403 ));
404 }
406 }
407 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
408 ?>