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 $initially_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']['SERVERSERVICE'] 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 }
93 }
95 /* Store initally account settings
96 */
97 $this->initially_was_account = $this->DNS_is_account;
98 }
101 function netmaskIsCoherent($idZone)
102 {
103 $netmask = FlipIp(str_replace(".in-addr.arpa","",getNameFromMix($idZone)));
104 if(!strstr($this->ipHostNumber, $netmask)){
105 return false;
106 }else{
107 return true;
108 }
109 }
112 function getVarsForSaving($attrs)
113 {
114 foreach($this->attributes as $attr){
115 if(!empty($this->$attr)){
116 $attrs[$attr] = $this->$attr;
117 }
118 }
119 return($attrs);
120 }
122 function execute()
123 {
124 /* Call parent execute */
125 $smarty= get_smarty();
127 $tmp = $this->plInfo();
128 foreach($tmp['plProvidedAcls'] as $name => $translation){
129 $smarty->assign($name."ACL",$this->getacl($name));
130 }
132 $display= "";
134 $smarty->assign("staticAddress", "");
135 $smarty->assign("autonet", "");
137 /* Check for autonet button */
138 if ($this->autonet && isset($_POST['autonet'])){
139 $cmd= $this->config->data['MAIN']['AUTO_NETWORK_HOOK'];
140 if(!empty($cmd) && $this->cn != ""){
141 $res = shell_exec($cmd." ".$this->cn);
142 if(!$res){
143 print_red(sprintf(_("Can't execute specified AUTO_NETWORK_HOOK '%s'. Please check your gosa.conf."),$cmd));
144 } else {
145 $res= split(';', trim($res));
146 if (isset($res[0]) && $res[0] != ""){
147 $this->ipHostNumber= $res[0];
148 }
149 if (isset($res[1]) && $res[1] != ""){
150 $this->macAddress= $res[1];
151 }
152 }
153 }
154 }
156 /* There is no dns available
157 */
158 if($this->DNSenabled == false){
160 /* Is IP address must ? */
161 $smarty->assign("DNS_enabled",false);
162 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
164 /* Assign smarty all non DNs attributes */
165 foreach($this->attributes as $attr){
166 $smarty->assign($attr,$this->$attr);
167 }
168 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
170 if ($this->autonet){
171 $smarty->assign("autonet", "true");
172 } else {
173 $smarty->assign("autonet", "");
174 }
175 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
176 return($display);
177 }else{
178 $smarty->assign("DNS_enabled",true);
179 }
181 $accountACL = false;
182 if($this->DNS_is_account && $this->acl_is_removeable()){
183 $accountACL = true;
184 }elseif(!$this->DNS_is_account && $this->acl_is_createable()){
185 $accountACL = true;
186 }
187 $smarty->assign("accountACL",$accountACL);
189 /* Add new empty array to our record list */
190 if(isset($_POST['AddNewRecord'])){
191 $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>"");
192 }
194 /* Handle all posts */
195 $only_once =true;
196 foreach($_POST as $name => $value){
198 /* Check if we have to delete a record entry */
199 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
201 /* Avoid performing this once again */
202 $only_once = false;
204 /* Extract id for specified entry */
205 $id = preg_replace("/RemoveRecord_/","",$name);
206 $id = preg_replace("/_.*$/","",$id);
208 /* Delete this record, mark edited entries to be able to delete them */
209 if(isset($this->dnsEntry['RECORDS'][$id])){
210 unset($this->dnsEntry['RECORDS'][$id]);
211 }
212 }
213 }
215 /* Assign smarty all non DNs attributes */
216 foreach($this->attributes as $attr){
217 $smarty->assign($attr,$this->$attr);
218 }
220 /* Assign smarty all DNS attributes */
221 foreach($this->DNSattributes as $attr){
222 $smarty->assign($attr,$this->dnsEntry[$attr]);
223 }
225 /* Assign all needed vars */
226 $smarty->assign("DNS_is_account",$this->DNS_is_account);
227 $smarty->assign("Zones",$this->Zones);
228 $smarty->assign("ZoneKeys",($this->Zones));
229 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
231 /* Create zone array */
232 $idZones = array();
233 foreach($this->Zones as $id => $zone){
234 if($this->netmaskIsCoherent($id)) {
235 $idZones[$id] = $zone;
236 }else{
237 $idZones[$id] = $zone." ("._("Not matching").")";
238 }
239 }
240 $smarty->assign("Zones",$idZones);
241 $smarty->assign("ZoneKeys", $this->Zones);
243 $tmp = $this->generateRecordsList();
245 $changeStateForRecords = $tmp['changeStateForRecords'];
247 $smarty->assign("records",$tmp['str']);
248 $smarty->assign("changeStateForRecords",$changeStateForRecords);
249 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
251 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
252 return($display);
253 }
256 function remove_from_parent()
257 {
258 if($this->initially_was_account){
260 $ldap = $this->config->get_ldap_link();
262 $tmp = array();
263 $this->dnsEntry['exists'] = false;
264 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
266 /* Delete dns */
267 foreach($tmp['del'] as $dn => $del){
268 $ldap->cd($dn);
269 $ldap->rmdir_recursive($dn);
270 }
271 }
272 }
275 /* Save data to object */
276 function save_object()
277 {
278 /* Save all posted vars */
279 plugin::save_object();
281 if(isset($_POST['network_tpl_posted'])){
283 /* Ge all non dns attributes (IP/MAC)*/
284 foreach($this->attributes as $attr){
285 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
286 $this->$attr = $_POST[$attr];
287 }
288 }
290 /* Check if DNS should be enabled / disabled */
291 if($this->DNS_is_account && $this->acl_is_removeable() && !isset($_POST['DNS_is_account'])){
292 $this->DNS_is_account = false;
293 }elseif(!$this->DNS_is_account && $this->acl_is_createable() && isset($_POST['DNS_is_account'])){
294 $this->DNS_is_account = true;
295 }
297 /* Get dns attributes */
298 if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
300 /* Check for posted record changes */
301 if(is_array($this->dnsEntry['RECORDS']) && $this->acl_is_writeable("Records")){
302 foreach($this->dnsEntry['RECORDS'] as $key => $value){
304 /* Check if type has changed */
305 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
306 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
307 }
308 /* Check if value has changed */
309 if(isset($_POST['RecordValue_'.$key])){
310 $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
311 }
312 }
313 }
314 /* Get all basic DNS attributes (TTL, Clas ..)*/
315 foreach($this->DNSattributes as $attr){
316 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
317 $this->dnsEntry[$attr] = $_POST[$attr];
318 }
319 }
322 }
323 }
324 }
327 /* Check supplied data */
328 function check()
329 {
330 /* Call common method to give check the hook */
331 $message= plugin::check();
333 /* Check if ip must be given
334 */
335 if(($this->IPisMust)||($this->DNS_is_account)){
336 if (empty($this->ipHostNumber)){
337 $message[]= _("The required field 'IP-address' is not set.");
338 }
340 if (!is_ip($this->ipHostNumber)){
341 $message[]= _("Wrong IP format in field IP-address.");
342 }
343 }
345 /* Check if mac is empty
346 */
347 if ($this->macAddress == "" ){
348 $message[]= _("The required field 'MAC-address' is not set.");
349 }
350 if(!is_mac($this->macAddress)){
351 $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
352 }
354 /* only perfrom this checks if this is a valid DNS account */
355 if($this->DNS_is_account){
357 $checkArray = array();
358 $onlyOnce = array();
360 // $onlyOnce['cNAMERecord'] = 0;
361 $tmp = array_flip($this->Zones);
362 $tmp2 = $tmp[$this->dnsEntry['zoneName']];
363 if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
364 $tmp2 = preg_replace("/^.*\//","",$tmp2);
365 $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
366 }
368 /* Walk through all entries and detect duplicates or mismatches
369 */
370 foreach($this->dnsEntry['RECORDS'] as $name => $values){
372 /* Count record values, to detect duplicate entries for a specific record
373 */
374 if(!isset($checkArray[$values['type']][$values['value']])){
375 $checkArray[$values['type']][$values['value']] = 0;
376 }else{
377 $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
378 }
380 /* Check if given entries in $onlyOnce are used more than once
381 */
382 if(isset($onlyOnce[$values['type']])){
383 $onlyOnce[$values['type']] ++;
384 if($onlyOnce[$values['type']] > 1){
385 $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
386 }
387 }
389 /* Skip txt record ...
390 */
391 if($values['type'] == "tXTRecord") continue;
393 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
394 */
395 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
396 $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."),
397 $this->ipHostNumber);
398 }
400 /* only lower-case is allowed in record entries ...
401 */
402 if($values['value'] != strtolower($values['value'])){
403 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
404 }
405 }
406 }
407 return ($message);
408 }
411 /* Save to LDAP */
412 function save($dn)
413 {
414 $ldap= $this->config->get_ldap_link();
416 /*******************/
417 /* IP-MAC HANDLING */
418 /*******************/
420 /* $dn was posted as parameter */
421 $this->dn = $dn;
423 /* Save DNS setting & ip/Mac*/
424 plugin::save();
426 /* Write back to ldap */
427 $ldap->cd($this->dn);
428 $this->cleanup();
429 $ldap->modify ($this->attrs);
431 /****************/
432 /* DNS HANDLING */
433 /****************/
435 /* If isn't DNS account but initially was DNS account
436 remove all DNS entries
437 */
438 if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
439 return;
440 }else{
442 /* Add ipHostNumber to aRecords
443 */
444 $backup_dnsEntry = $this->dnsEntry;
445 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
447 /* Create diff and follow instructions
448 * If Account was disabled, remove account by setting exists to false
449 */
450 if((!$this->DNS_is_account)&&($this->initially_was_account)){
451 $this->dnsEntry['exists'] = false;
452 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
453 }else{
454 $this->dnsEntry['exists'] = $this->DNS_is_account;
455 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
456 }
458 /* move follwoing entries
459 */
460 foreach($tmp['move'] as $src => $dst){
461 $this->recursive_move($src,$dst);
462 }
464 /* Delete dns */
465 foreach($tmp['del'] as $dn => $del){
466 $ldap->cd($dn);
467 $ldap->rmdir_recursive($dn);
468 }
470 /* Add || Update new DNS entries
471 */
472 foreach($tmp['add'] as $dn => $attrs){
473 $ldap->cd($dn);
474 $ldap->cat($dn, array('dn'));
475 if(count($ldap->fetch())){
476 $ldap->cd($dn);
477 $ldap->modify ($attrs);
478 }else{
479 $ldap->cd($dn);
480 $ldap->add($attrs);
481 }
482 }
484 /* Display errors
485 */
486 if($ldap->get_error() != "Success"){
487 show_ldap_error($ldap->get_error(), sprintf(_("Saving of terminal/dns account with dn '%s' failed."),$this->dn));
488 }
490 $this->dnsEntry = $backup_dnsEntry;
491 }
492 }
494 /* Create html table with all used record types
495 */
496 function generateRecordsList()
497 {
498 $changeStateForRecords = "";
500 if(!$this->DNS_is_account) {
501 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
502 return $str;
503 }
505 $str = "<table summary='' width='100%'>";
506 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
508 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
509 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
510 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
512 $str.=" <tr>".
513 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
514 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
515 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
516 "</tr>";
517 }
519 $str.= " <tr>".
520 " <td colspan=2 width='50%'></td><td>".
521 " <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
522 " </td>".
523 " </tr>".
524 "</table>";
525 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
526 return($ret);
527 }
530 /* Create a html select box which allows us to select different types of records
531 */
532 function generateRecordListBox($selected,$name)
533 {
534 $str = "<select name='".$name."' id='".$name."'>";
535 foreach($this->RecordTypes as $type => $value){
536 $use = "";
537 if($type == $selected){
538 $use = " selected ";
539 }
540 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
541 }
542 $str.="</select>";
543 return($str);
544 }
547 /* Return plugin informations for acl handling */
548 function plInfo()
549 {
550 $tmp = array(
551 "plShortName" => _("DNS"),
552 "plDescription" => _("DNS settings"),
553 "plSelfModify" => FALSE,
554 "plDepends" => array(),
555 "plPriority" => 5,
556 "plSection" => array("administration"),
557 "plCategory" => array("workstation","terminal","phone","server","component","printer","winworkstation"),
559 "plProvidedAcls"=> array(
560 "ipHostNumber" => _("IP address"),
561 "macAddress" => _("MAC address"))
562 );
564 /* Hide all dns specific code, if dns is not available
565 */
566 $DNSenabled = false;
567 foreach($_SESSION['config']->data['TABS']['SERVERSERVICE'] as $tab){
568 if(preg_match("/^servdns$/",$tab['CLASS'])){
569 $tmp['plProvidedAcls']["Records"] = _("DNS records");
570 $tmp['plProvidedAcls']["zoneName"] = _("Zone name");
571 $tmp['plProvidedAcls']["dNSTTL"] = _("TTL");
572 }
573 }
574 return($tmp);
575 }
576 }
578 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
579 ?>