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 ){
221 $i --;
222 $str.=$dn." ";
223 }
224 }
226 /* Only show 2 dns in the error message
227 */
228 if(count($used)> 2) {
229 $str .=" ... ";
230 }
231 print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
232 return(false);
233 }else{
234 unset($this->Zones[$id]);
235 return(true);
236 }
237 }
240 /* This funtion returns all used Zonenames
241 */
242 function getUsedZoneNames()
243 {
244 $ret = array();
245 $ldap = $this->config->get_ldap_link();
246 $ldap->cd($this->config->current['BASE']);
247 $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
248 while($attr = $ldap->fetch()){
249 $ret[$attr['zoneName'][0]][] = $attr['dn'];
250 }
251 return($ret);
252 }
255 /* Remove dns service
256 */
257 function remove_from_parent()
258 {
259 if($this->initially_was_account){
260 $bool = true;
261 foreach($this->Zones as $key => $zone){
262 $bool= $bool & $this->RemoveZone($key);
263 }
265 if($bool){
266 $this->save();
267 }
268 return($bool);
269 }
270 }
273 /* Save to LDAP */
274 function save()
275 {
276 $ldap = $this->config->get_ldap_link();
277 $ldap->cd($this->config->current['BASE']);
279 /* Get differences
280 */
281 $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$this->orig_dn);
283 /* Updated zone entries if reverser or forward name has changed
284 * Must be done before moving entries, else the given dn is invalid
285 */
286 if(isset($tmp['zoneUpdates'])){
287 foreach($tmp['zoneUpdates'] as $dn => $attrs){
288 $ldap->cd($dn);
289 $ldap->modify($attrs);
290 show_ldap_error($ldap->get_error(), sprintf(_("Updating of system server/dns with dn '%s' failed."),$this->dn));
291 }
292 }
294 /* Delete dns
295 */
296 foreach($tmp['del'] as $dn => $del){
297 $ldap->cd($dn);
298 $ldap->rmdir_recursive($dn);
299 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/dns with dn '%s' failed."),$this->dn));
300 }
302 /* move follwoing entries
303 */
304 foreach($tmp['move'] as $src => $dst){
305 $this->recursive_move($src,$dst);
306 }
308 /* Add || Update new DNS entries
309 */
310 foreach($tmp['add'] as $dn => $attrs){
311 $ldap->cd($dn);
312 $ldap->cat($dn, array('dn'));
313 if(count($ldap->fetch())){
314 $ldap->cd($dn);
315 $ldap->modify ($attrs);
316 }else{
317 $ldap->cd($dn);
318 $ldap->add($attrs);
319 }
320 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
321 }
322 }
325 /* Directly save new status flag */
326 function setStatus($value)
327 {
328 if($value == "none") return;
329 if(!$this->initially_was_account) return;
330 if(empty($this->StatusFlag)) return;
331 $ldap = $this->config->get_ldap_link();
332 $ldap->cd($this->dn);
333 $ldap->cat($this->dn,array("objectClass"));
334 if($ldap->count()){
336 $tmp = $ldap->fetch();
337 for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
338 $attrs['objectClass'][] = $tmp['objectClass'][$i];
339 }
340 $flag = $this->StatusFlag;
341 $attrs[$flag] = $value;
342 $this->$flag = $value;
343 $ldap->modify($attrs);
344 show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for system server/dns with dn '%s' failed."),$this->dn));
345 $this->action_hook();
346 }
347 }
350 function getListEntry()
351 {
352 $fields = goService::getListEntry();
353 $fields['Message'] = _("DNS service");
354 $fields['AllowEdit'] = true;
355 return($fields);
356 }
359 /* Get updates for status flag */
360 function updateStatusState()
361 {
362 if(empty($this->StatusFlag)) return;
364 $attrs = array();
365 $flag = $this->StatusFlag;
366 $ldap = $this->config->get_ldap_link();
367 $ldap->cd($this->cn);
368 $ldap->cat($this->dn,array($flag));
369 if($ldap->count()){
370 $attrs = $ldap->fetch();
371 }
372 if(isset($attrs[$flag][0])){
373 $this->$flag = $attrs[$flag][0];
374 }
375 }
378 /* Return plugin informations for acl handling */
379 function plInfo()
380 {
381 return (array(
382 "plShortName" => _("DNS service"),
383 "plDescription" => _("DNS service")." ("._("Services").")",
384 "plSelfModify" => FALSE,
385 "plDepends" => array(),
386 "plPriority" => 83,
387 "plSection" => array("administration"),
388 "plCategory" => array("server"),
390 "plProvidedAcls"=> array(
391 "zoneName" =>_("Zone name"),
392 "ReverseZone" =>_("Reverse zone"),
393 "sOAprimary" =>_("Primary dns server"),
394 "sOAmail" =>_("Mail address"),
395 "sOAserial" =>_("Serial"),
396 "sOArefresh" =>_("Refresh"),
397 "sOAretry" =>_("Retry"),
398 "sOAexpire" =>_("Expire"),
399 "sOAttl" =>_("TTL"),
400 "zoneRecords" =>_("Zone records"))
401 ));
402 }
404 }
405 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
406 ?>