1 <?php
3 class termDNS extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account = true;
7 var $autonet = false;
9 /* Basic informations
10 */
11 var $attributes = array("ipHostNumber","macAddress");
12 var $objectclasses = array("whatever");
14 var $ipHostNumber = ""; // IP address
15 var $macAddress = ""; // Mac address
16 var $cn = ""; // CN of currently edited device
17 var $OrigCn = ""; // Initial cn
18 var $IPisMust = false;
19 var $MACisMust = false;
21 /* DNS attributes
22 */
23 var $DNSattributes = array("dNSClass","zoneName","dNSTTL");
24 var $DNS_is_Account = false;
25 var $DNSinitially_was_account = false;
26 var $dnsEntry = array();
27 var $DNSenabled = false;
29 /* Terminal dns
30 */
31 function termDNS ($config, $dn,$objectClasses,$IPisMust = false)
32 {
33 /* We need to know which objectClasses are used, to store the ip/mac
34 * Because of different type of devices
35 */
36 $this->objectclasses = $objectClasses;
37 $this->IPisMust = $IPisMust;
39 plugin::plugin ($config, $dn);
41 if(isset($this->attrs['cn'][0])){
42 $this->OrigCn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
43 $this->cn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
44 }
46 /* Do we have autonet support? */
47 if (isset($this->config->data['MAIN']['AUTO_NETWORK_HOOK'])){
48 $this->autonet= true;
49 }
51 /* Hide all dns specific code, if dns is not available
52 */
53 $DNSenabled = false;
54 foreach($this->config->data['TABS']['SERVTABS'] as $tab){
55 if(preg_match("/^servdns$/",$tab['CLASS'])){
56 $this->DNSenabled = true;
57 }
58 }
59 if(!$this->DNSenabled){
60 $this->DNS_is_account = false;
61 return;
62 }
64 if($this->DNSenabled){
66 /* Get Zones
67 */
68 $this->Zones = getAvailableZones($config);
70 /* Get Entry
71 */
72 $this->dnsEntry = getDNSHostEntries($config,$this->OrigCn);
74 /* Remove A record which equals $this->ipHostNumber
75 */
76 foreach($this->dnsEntry['RECORDS'] as $key => $rec){
77 if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
78 unset($this->dnsEntry['RECORDS'][$key]);
79 }
80 }
82 /* Get Record types
83 */
84 $this->RecordTypes = getDnsRecordTypes();
86 /* If there is at least one entry in this -> types, we have DNS enabled
87 */
88 if($this->dnsEntry['exists']){
89 $this->DNS_is_account = true;
90 }else{
91 $this->DNS_is_account = false;
92 }
94 }
96 /* Store initally account settings
97 */
98 $this->DNSinitially_was_account = $this->DNS_is_account;
99 }
102 function netmaskIsCoherent($idZone)
103 {
104 $netmask = FlipIp(str_replace(".in-addr.arpa","",getNameFromMix($idZone)));
105 if(!strstr($this->ipHostNumber, $netmask)){
106 return false;
107 }else{
108 return true;
109 }
110 }
113 function getVarsForSaving($attrs)
114 {
115 foreach($this->attributes as $attr){
116 if(!empty($this->$attr)){
117 $attrs[$attr] = $this->$attr;
118 }
119 }
120 return($attrs);
121 }
123 function execute()
124 {
125 /* Call parent execute */
126 $smarty= get_smarty();
127 $display= "";
129 $smarty->assign("staticAddress", "");
130 $smarty->assign("autonet", $this->autonet);
132 /* Check for autonet button */
133 if ($this->autonet && isset($_POST['autonet'])){
134 $cmd= $this->config->data['MAIN']['AUTO_NETWORK_HOOK'];
135 if(!empty($cmd) && $this->cn != ""){
136 $res = shell_exec($cmd." ".$this->cn);
137 if(!$res){
138 print_red(sprintf(_("Can't execute specified AUTO_NETWORK_HOOK '%s'. Please check your gosa.conf."),$cmd));
139 } else {
140 $res= split(';', trim($res));
141 if (isset($res[0]) && $res[0] != ""){
142 $this->ipHostNumber= $res[0];
143 }
144 if (isset($res[1]) && $res[1] != ""){
145 $this->macAddress= $res[1];
146 }
147 }
148 }
149 }
151 /* There is no dns available
152 */
153 if($this->DNSenabled == false){
155 /* Is IP address must ? */
156 $smarty->assign("DNS_is_account",false);
157 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
159 /* Assign smarty all non DNs attributes */
160 foreach($this->attributes as $attr){
161 $smarty->assign($attr,$this->$attr);
162 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
163 }
164 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
166 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
167 return($display);
168 }else{
169 $smarty->assign("DNS_is_account",true);
170 }
172 /* Add new empty array to our record list */
173 if(isset($_POST['AddNewRecord'])){
174 $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>"");
175 }
177 /* Handle all posts */
178 $only_once =true;
179 foreach($_POST as $name => $value){
181 /* Check if we have to delete a record entry */
182 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
184 /* Avoid performing this once again */
185 $only_once = false;
187 /* Extract id for specified entry */
188 $id = preg_replace("/RemoveRecord_/","",$name);
189 $id = preg_replace("/_.*$/","",$id);
191 /* Delete this record, mark edited entries to be able to delete them */
192 if(isset($this->dnsEntry['RECORDS'][$id])){
193 unset($this->dnsEntry['RECORDS'][$id]);
194 }
195 }
196 }
198 /* Assign smarty all non DNs attributes */
199 foreach($this->attributes as $attr){
200 $smarty->assign($attr,$this->$attr);
201 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
202 }
204 /* Assign smarty all DNS attributes */
205 foreach($this->DNSattributes as $attr){
206 $smarty->assign($attr,$this->dnsEntry[$attr]);
207 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
208 }
210 /* Assign all needed vars */
211 $smarty->assign("DNSaccountACL",chkacl($this->acl,"termDNS"));
213 $smarty->assign("DNSAccount",$this->DNS_is_account);
214 $smarty->assign("Zones",$this->Zones);
215 $smarty->assign("ZoneKeys",($this->Zones));
216 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
218 /* Create zone array */
219 $idZones = array();
220 foreach($this->Zones as $id => $zone){
221 if($this->netmaskIsCoherent($id)) {
222 $idZones[$id] = $zone;
223 }else{
224 $idZones[$id] = $zone." ("._("Not matching").")";
225 }
226 }
227 $smarty->assign("Zones",$idZones);
228 $smarty->assign("ZoneKeys", $this->Zones);
230 $tmp = $this->generateRecordsList();
232 $changeStateForRecords = $tmp['changeStateForRecords'];
234 $smarty->assign("records",$tmp['str']);
235 $smarty->assign("changeStateForRecords",$changeStateForRecords);
236 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
238 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
239 return($display);
240 }
242 function remove_from_parent()
243 {
244 /*
245 $ldap = $this->config->get_ldap_link();
246 $ldap->cd($this->orig_dn);
247 $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("relativeDomainName","zoneName"));
248 while($attr = $ldap->fetch()){
249 $ldap->cd($attr['dn']);
250 $ldap->rmDir($attr['dn']);
251 show_ldap_error("Record:".$ldap->get_error(), _("Removing terminal from DNS object failed"));
252 }
253 */
254 }
256 /* Save data to object */
257 function save_object()
258 {
259 /* Save all posted vars */
260 plugin::save_object();
262 /* Get dns attributes */
263 if(($this->DNSenabled) && (isset($_POST['network_tpl_posted'])) && chkacl($this->acl,"termDNS") == ""){
265 /* Check for posted record changes */
266 if(is_array($this->dnsEntry['RECORDS'])){
267 foreach($this->dnsEntry['RECORDS'] as $key => $value){
269 /* Check if type has changed */
270 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
271 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
272 }
273 /* Check if value has changed */
274 if(isset($_POST['RecordValue_'.$key])){
275 $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
276 }
277 }
278 }
279 /* Get all basic DNS attributes (TTL, Clas ..)*/
280 foreach($this->DNSattributes as $attr){
281 if(isset($_POST[$attr])){
282 $this->dnsEntry[$attr] = $_POST[$attr];
283 }
284 }
286 /* Enable diable DNS */
287 if(isset($_POST['enableDNS'])){
288 $this->DNS_is_account = true;
289 }else{
290 $this->DNS_is_account = false;
291 }
292 }
293 }
296 /* Check supplied data */
297 function check()
298 {
299 /* Call common method to give check the hook */
300 $message= plugin::check();
302 /* Check if ip must be given
303 */
304 if(($this->IPisMust)||($this->DNS_is_account)){
306 /* Check if ip is empty
307 */
308 if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
309 $message[]= _("The required field 'IP-address' is not set.");
310 }
312 }
314 /* check if given ip is valid ip */
315 if ($this->ipHostNumber != "" && !is_ip($this->ipHostNumber)){
316 $message[]= _("Wrong IP format in field IP-address.");
317 }
319 /* Check if mac is empty
320 */
321 if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
322 $message[]= _("The required field 'MAC-address' is not set.");
323 }
325 /* Check if given mac is valid mac
326 */
327 if(!is_mac($this->macAddress)){
328 $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
329 }
331 /* only perfrom this checks if this is a valid DNS account */
332 if($this->DNS_is_account){
334 $checkArray = array();
335 $onlyOnce = array();
337 $tmp = array_flip($this->Zones);
338 $tmp2 = $tmp[$this->dnsEntry['zoneName']];
339 if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
340 $tmp2 = preg_replace("/^.*\//","",$tmp2);
341 $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
342 }
344 // There can be many CNAME records
345 //$onlyOnce['cNAMERecord'] = 0;
347 /* Walk through all entries and detect duplicates or mismatches
348 */
349 foreach($this->dnsEntry['RECORDS'] as $name => $values){
351 /* Count record values, to detect duplicate entries for a specific record
352 */
353 if(!isset($checkArray[$values['type']][$values['value']])){
354 $checkArray[$values['type']][$values['value']] = 0;
355 }else{
356 $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
357 }
359 /* Check if given entries in $onlyOnce are used more than once
360 */
361 if(isset($onlyOnce[$values['type']])){
362 $onlyOnce[$values['type']] ++;
363 if($onlyOnce[$values['type']] > 1){
364 $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
365 }
366 }
368 /* Skip txt record ...
369 */
370 if($values['type'] == "tXTRecord") continue;
372 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
373 */
374 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
375 $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."),
376 $this->ipHostNumber);
377 }
379 /* only lower-case is allowed in record entries ...
380 */
381 if($values['value'] != strtolower($values['value'])){
382 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
383 }
384 }
385 }
386 return ($message);
387 }
390 /* Save to LDAP */
391 function save($dn)
392 {
393 $ldap= $this->config->get_ldap_link();
395 /*******************/
396 /* IP-MAC HANDLING */
397 /*******************/
399 /* $dn was posted as parameter */
400 $this->dn = $dn;
402 /* Save DNS setting & ip/Mac*/
403 plugin::save();
405 /* Write back to ldap */
406 $ldap->cd($this->dn);
407 $this->cleanup();
408 $ldap->modify ($this->attrs);
410 /****************/
411 /* DNS HANDLING */
412 /****************/
414 /* If isn't DNS account but initially was DNS account
415 remove all DNS entries
416 */
417 if((!$this->DNSenabled) || (!$this->DNS_is_account && !$this->DNSinitially_was_account)){
418 return;
419 }else{
421 /* Add ipHostNumber to aRecords
422 */
423 if(!empty($this->ipHostNumber)){
424 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
425 }
427 /* Create diff and follow instructions
428 * If Account was disabled, remove account by setting exists to false
429 */
430 if((!$this->DNS_is_account)&&($this->DNSinitially_was_account)){
431 $this->dnsEntry['exists'] = false;
432 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
433 }else{
434 $this->dnsEntry['exists'] = $this->DNS_is_account;
435 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
436 }
438 /* move follwoing entries
439 */
440 foreach($tmp['move'] as $src => $dst){
441 $this->recursive_move($src,$dst);
442 }
444 /* Delete dns */
445 foreach($tmp['del'] as $dn => $del){
446 $ldap->cd($dn);
447 $ldap->rmdir_recursive($dn);
448 }
450 /* Add || Update new DNS entries
451 */
452 foreach($tmp['add'] as $dn => $attrs){
453 $ldap->cd($dn);
454 $ldap->cat($dn, array('dn'));
455 if(count($ldap->fetch())){
456 $ldap->cd($dn);
457 $ldap->modify ($attrs);
458 }else{
459 $ldap->cd($dn);
460 $ldap->add($attrs);
461 }
462 }
464 /* Display errors
465 */
466 if($ldap->get_error() != "Success"){
467 show_ldap_error("Record:".$ldap->get_error(), _("Saving terminal to DNS object failed"));
468 }
469 }
470 }
472 /* Create html table with all used record types
473 */
474 function generateRecordsList()
475 {
476 $changeStateForRecords = "";
478 if(!$this->DNS_is_account) {
479 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
480 return $str;
481 }
483 $str = "<table summary='' width='100%'>";
484 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
486 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
487 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
488 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
490 $str.=" <tr>".
491 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
492 " <td><input ".chkacl($this->acl,"termDNS")." type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
493 " <td><input ".chkacl($this->acl,"termDNS")." type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
494 "</tr>";
495 }
497 $str.= " <tr>".
498 " <td colspan=2 width='50%'></td><td>".
499 " <input type='submit' value='"._("Add")."' name='AddNewRecord' ".chkacl($this->acl,"termDNS")." >".
500 " </td>".
501 " </tr>".
502 "</table>";
503 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
504 return($ret);
505 }
508 /* Create a html select box which allows us to select different types of records
509 */
510 function generateRecordListBox($selected,$name)
511 {
512 $str = "<select ".chkacl($this->acl,"termDNS")." name='".$name."' id='".$name."'>";
513 foreach($this->RecordTypes as $type => $value){
514 $use = "";
515 if($type == $selected){
516 $use = " selected ";
517 }
518 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
519 }
520 $str.="</select>";
521 return($str);
522 }
523 }
525 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
526 ?>