1 <?php
3 class termDNS 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 = TRUE;
13 /* Basic informations
14 */
15 var $attributes = array("ipHostNumber","macAddress");
16 var $objectclasses = array("whatever");
18 var $ipHostNumber = ""; // IP address
19 var $macAddress = ""; // Mac address
20 var $cn = ""; // CN of currently edited device
21 var $OrigCn = ""; // Initial cn
22 var $IPisMust = false;
23 var $MACisMust = false;
25 /* DNS attributes
26 */
27 var $DNSattributes = array("dNSClass","zoneName","dNSTTL");
28 var $DNS_is_Account = false;
29 var $DNSinitially_was_account = false;
30 var $dnsEntry = array();
31 var $DNSenabled = false;
33 /* Terminal dns
34 */
35 function termDNS ($config, $dn,$objectClasses,$IPisMust = false)
36 {
37 /* We need to know which objectClasses are used, to store the ip/mac
38 * Because of different type of devices
39 */
40 $this->objectclasses = $objectClasses;
41 $this->IPisMust = $IPisMust;
43 plugin::plugin ($config, $dn);
45 if(isset($this->attrs['cn'][0])){
46 $this->OrigCn = $this->attrs['cn'][0];
47 }
49 /* Hide all dns specific code, if dns is not available
50 */
51 $DNSenabled = false;
52 foreach($this->config->data['TABS']['SERVTABS'] as $tab){
53 if(preg_match("/^servdns$/",$tab['CLASS'])){
54 $this->DNSenabled = true;
55 }
56 }
57 if(!$this->DNSenabled){
58 $this->DNS_is_account = false;
59 return;
60 }
62 /* Get Zones
63 */
64 $this->Zones = getAvailableZones($config);
66 /* Get Entry
67 */
68 $this->dnsEntry = getDNSHostEntries($config,$this->OrigCn);
70 /* Remove A record which equals $this->ipHostNumber
71 */
72 foreach($this->dnsEntry['RECORDS'] as $key => $rec){
73 if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
74 unset($this->dnsEntry['RECORDS'][$key]);
75 }
76 }
78 /* Get Record types
79 */
80 $this->RecordTypes = getDnsRecordTypes();
82 /* If there is at least one entry in this -> types, we have DNS enabled
83 */
84 if($this->dnsEntry['exists']){
85 $this->DNS_is_account = true;
86 }else{
87 $this->DNS_is_account = false;
88 }
90 /* Store initally account settings
91 */
92 $this->DNSinitially_was_account = $this->DNS_is_account;
93 }
96 function getVarsForSaving($attrs)
97 {
98 foreach($this->attributes as $attr){
99 if(!empty($this->$attr)){
100 $attrs[$attr] = $this->$attr;
101 }
102 }
103 return($attrs);
104 }
106 function execute()
107 {
108 /* Call parent execute */
109 $smarty= get_smarty();
110 $display= "";
112 $smarty->assign("staticAddress", "");
114 /* There is no dns available
115 */
116 if($this->DNSenabled == false){
118 /* Is IP address must ? */
119 $smarty->assign("DNS_is_account",false);
120 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
122 /* Assign smarty all non DNs attributes */
123 foreach($this->attributes as $attr){
124 $smarty->assign($attr,$this->$attr);
125 }
126 $smarty->assign("staticAddress", "");
127 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
128 return($display);
129 }else{
130 $smarty->assign("DNS_is_account",true);
131 }
133 /* Add new empty array to our record list */
134 if(isset($_POST['AddNewRecord'])){
135 $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>"");
136 }
138 /* Handle all posts */
139 $only_once =true;
140 foreach($_POST as $name => $value){
142 /* Check if we have to delete a record entry */
143 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
145 /* Avoid performing this once again */
146 $only_once = false;
148 /* Extract id for specified entry */
149 $id = preg_replace("/RemoveRecord_/","",$name);
150 $id = preg_replace("/_.*$/","",$id);
152 /* Delete this record, mark edited entries to be able to delete them */
153 if(isset($this->dnsEntry['RECORDS'][$id])){
154 unset($this->dnsEntry['RECORDS'][$id]);
155 }
156 }
157 }
159 /* Assign smarty all non DNs attributes */
160 foreach($this->attributes as $attr){
161 $smarty->assign($attr,$this->$attr);
162 }
164 /* Assign smarty all DNS attributes */
165 foreach($this->DNSattributes as $attr){
166 $smarty->assign($attr,$this->dnsEntry[$attr]);
167 }
169 /* Assign all needed vars */
170 $smarty->assign("DNSAccount",$this->DNS_is_account);
171 $smarty->assign("Zones",$this->Zones);
172 $smarty->assign("ZoneKeys",($this->Zones));
173 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
175 $tmp = $this->generateRecordsList();
177 $changeStateForRecords = $tmp['changeStateForRecords'];
179 $smarty->assign("records",$tmp['str']);
180 $smarty->assign("changeStateForRecords",$changeStateForRecords);
181 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
183 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
184 return($display);
185 }
187 function remove_from_parent()
188 {
189 /*
190 $ldap = $this->config->get_ldap_link();
191 $ldap->cd($this->orig_dn);
192 $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("relativeDomainName","zoneName"));
193 while($attr = $ldap->fetch()){
194 $ldap->cd($attr['dn']);
195 $ldap->rmDir($attr['dn']);
196 }
197 */
198 }
200 /* Save data to object */
201 function save_object()
202 {
203 /* Save all posted vars */
204 plugin::save_object();
206 /* Ge all non dns attributes (IP/MAC)*/
207 foreach($this->attributes as $attr){
208 if(isset($_POST[$attr])){
209 $this->$attr = $_POST[$attr];
210 }
211 }
213 /* Get dns attributes */
214 if(isset($_POST['network_tpl_posted'])){
216 /* Check for posted record changes */
217 foreach($this->dnsEntry['RECORDS'] as $key => $value){
219 /* Check if type has changed */
220 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
221 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
222 }
223 /* Check if value has changed */
224 if(isset($_POST['RecordValue_'.$key])){
225 $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
226 }
227 }
229 /* Get all basic DNS attributes (TTL, Clas ..)*/
230 foreach($this->DNSattributes as $attr){
231 if(isset($_POST[$attr])){
232 $this->dnsEntry[$attr] = $_POST[$attr];
233 }
234 }
236 /* Enable diable DNS */
237 if(isset($_POST['enableDNS'])){
238 $this->DNS_is_account = true;
239 }else{
240 $this->DNS_is_account = false;
241 }
242 }
243 }
246 /* Check supplied data */
247 function check()
248 {
249 /* Call common method to give check the hook */
250 $message= plugin::check();
252 /* Check if ip must be given
253 */
254 if(($this->IPisMust)||($this->DNS_is_account)){
256 /* Check if ip is empty
257 */
258 if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
259 $message[]= _("The required field 'IP-address' is not set.");
260 }
262 /* check if given ip is valid ipi
263 */
264 $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
265 if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
266 $message[]= _("Wrong IP format in field IP-address.");
267 }
268 }
270 /* Check if mac is empty
271 */
272 if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
273 $message[]= _("The required field 'MAC-address' is not set.");
274 }
276 /* Check if given mac is valid mac
277 */
278 $tr = count(split(":",$this->macAddress));
279 if($tr!=6){
280 $message[]=(_("The given macaddress is invalid. There must be 6 1byte segments seperated by ':'."));
281 }
283 /* only perfrom this checks if this is a valid DNS account */
284 if($this->DNS_is_account){
286 $checkArray = array();
287 $onlyOnce = array();
289 $onlyOnce['cNAMERecord'] = 0;
291 /* Walk through all entries and detect duplicates or mismatches
292 */
293 foreach($this->dnsEntry['RECORDS'] as $name => $values){
295 /* Count record values, to detect duplicate entries for a specific record
296 */
297 if(!isset($checkArray[$values['type']][$values['value']])){
298 $checkArray[$values['type']][$values['value']] = 0;
299 }else{
300 $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
301 }
303 /* Check if given entries in $onlyOnce are used more than once
304 */
305 if(isset($onlyOnce[$values['type']])){
306 $onlyOnce[$values['type']] ++;
307 if($onlyOnce[$values['type']] > 1){
308 $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
309 }
310 }
312 /* Skip txt record ...
313 */
314 if($values['type'] == "tXTRecord") continue;
316 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
317 */
318 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
319 $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."),
320 $this->ipHostNumber);
321 }
323 /* only lower-case is allowed in record entries ...
324 */
325 if($values['value'] != strtolower($values['value'])){
326 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
327 }
328 }
329 }
330 return ($message);
331 }
334 /* Save to LDAP */
335 function save($dn)
336 {
337 $ldap= $this->config->get_ldap_link();
339 /*******************/
340 /* IP-MAC HANDLING */
341 /*******************/
343 /* $dn was posted as parameter */
344 $this->dn = $dn;
346 /* Save DNS setting & ip/Mac*/
347 plugin::save();
349 /* Write back to ldap */
350 $ldap->cd($this->dn);
351 $this->cleanup();
352 $ldap->modify ($this->attrs);
354 /****************/
355 /* DNS HANDLING */
356 /****************/
358 /* If isn't DNS account but initially was DNS account
359 remove all DNS entries
360 */
362 /* Add ipHostNumber to aRecords
363 */
364 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
366 /* Create diff and follow instructions
367 * If Account was disabled, remove account by setting exists to false
368 */
369 if((!$this->DNS_is_account)&&($this->DNSinitially_was_account)){
370 $this->dnsEntry['exists'] = false;
371 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
372 }else{
373 $this->dnsEntry['exists'] = $this->DNS_is_account;
374 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
375 }
377 /* move follwoing entries
378 */
379 foreach($tmp['move'] as $src => $dst){
380 $this->recursive_move($src,$dst);
381 }
383 /* Delete dns */
384 foreach($tmp['del'] as $dn => $del){
385 $ldap->cd($dn);
386 $ldap->rmdir_recursive($dn);
387 }
389 /* Add || Update new DNS entries
390 */
391 foreach($tmp['add'] as $dn => $attrs){
392 $ldap->cd($dn);
393 $ldap->cat($dn);
394 if(count($ldap->fetch())){
395 $ldap->cd($dn);
396 $ldap->modify ($attrs);
397 }else{
398 $ldap->cd($dn);
399 $ldap->add($attrs);
400 }
401 }
403 /* Display errors
404 */
405 if($ldap->get_error() != "Success"){
406 show_ldap_error("Record:".$ldap->get_error());
407 }
408 }
410 /* Create html table with all used record types
411 */
412 function generateRecordsList()
413 {
414 $changeStateForRecords = "";
416 if(!$this->DNS_is_account) {
417 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled>";
418 return $str;
419 }
421 $str = "<table summary='' width='100%'>";
422 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
424 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
425 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
426 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
428 $str.=" <tr>".
429 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
430 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
431 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
432 "</tr>";
433 }
435 $str.= " <tr>".
436 " <td colspan=2 width='50%'></td><td>".
437 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
438 " </td>".
439 " </tr>".
440 "</table>";
441 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
442 return($ret);
443 }
446 /* Create a html select box which allows us to select different types of records
447 */
448 function generateRecordListBox($selected,$name)
449 {
450 $str = "<select name='".$name."' id='".$name."'>";
451 foreach($this->RecordTypes as $type => $value){
452 $use = "";
453 if($type == $selected){
454 $use = " selected ";
455 }
456 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
457 }
458 $str.="</select>";
459 return($str);
460 }
461 }
463 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
464 ?>