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;
12 var $autonet = false;
14 /* Basic informations
15 */
16 var $attributes = array("ipHostNumber","macAddress");
17 var $objectclasses = array("whatever");
19 var $ipHostNumber = ""; // IP address
20 var $macAddress = ""; // Mac address
21 var $cn = ""; // CN of currently edited device
22 var $OrigCn = ""; // Initial cn
23 var $IPisMust = false;
24 var $MACisMust = false;
26 /* DNS attributes
27 */
28 var $DNSattributes = array("dNSClass","zoneName","dNSTTL");
29 var $DNS_is_Account = false;
30 var $initially_was_account = false;
31 var $dnsEntry = array();
32 var $DNSenabled = false;
34 /* Terminal dns
35 */
36 function termDNS ($config, $dn,$objectClasses,$IPisMust = false)
37 {
38 /* We need to know which objectClasses are used, to store the ip/mac
39 * Because of different type of devices
40 */
41 $this->objectclasses = $objectClasses;
42 $this->IPisMust = $IPisMust;
44 plugin::plugin ($config, $dn);
46 if(isset($this->attrs['cn'][0])){
47 $this->OrigCn = $this->attrs['cn'][0];
48 $this->cn = $this->attrs['cn'][0];
49 }
51 /* Do we have autonet support? */
52 if (isset($this->config->data['MAIN']['AUTO_NETWORK_HOOK'])){
53 $this->autonet= true;
54 }
56 /* Hide all dns specific code, if dns is not available
57 */
58 $DNSenabled = false;
59 foreach($this->config->data['TABS']['SERVERSERVICE'] as $tab){
60 if(preg_match("/^servdns$/",$tab['CLASS'])){
61 $this->DNSenabled = true;
62 }
63 }
64 if(!$this->DNSenabled){
65 $this->DNS_is_account = false;
66 return;
67 }
69 if($this->DNSenabled){
71 /* Get Zones
72 */
73 $this->Zones = getAvailableZones($config);
75 /* Get Entry
76 */
77 $this->dnsEntry = getDNSHostEntries($config,$this->OrigCn);
79 /* Remove A record which equals $this->ipHostNumber
80 */
81 foreach($this->dnsEntry['RECORDS'] as $key => $rec){
82 if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
83 unset($this->dnsEntry['RECORDS'][$key]);
84 }
85 }
87 /* Get Record types
88 */
89 $this->RecordTypes = getDnsRecordTypes();
91 /* If there is at least one entry in this -> types, we have DNS enabled
92 */
93 if($this->dnsEntry['exists']){
94 $this->DNS_is_account = true;
95 }else{
96 $this->DNS_is_account = false;
97 }
99 }
101 /* Store initally account settings
102 */
103 $this->initially_was_account = $this->DNS_is_account;
104 }
107 function getVarsForSaving($attrs)
108 {
109 foreach($this->attributes as $attr){
110 if(!empty($this->$attr)){
111 $attrs[$attr] = $this->$attr;
112 }
113 }
114 return($attrs);
115 }
117 function execute()
118 {
119 /* Call parent execute */
120 $smarty= get_smarty();
122 $tmp = $this->plInfo();
123 foreach($tmp['plProvidedAcls'] as $name => $translation){
124 $smarty->assign($name."ACL",$this->getacl($name));
125 }
127 $display= "";
129 $smarty->assign("staticAddress", "");
130 $smarty->assign("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 }
163 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
165 if ($this->autonet){
166 $smarty->assign("autonet", "true");
167 } else {
168 $smarty->assign("autonet", "");
169 }
170 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
171 return($display);
172 }else{
173 $smarty->assign("DNS_is_account",true);
174 }
176 $accountACL = false;
177 if($this->DNSenabled && $this->acl_is_removeable()){
178 $accountACL = true;
179 }elseif(!$this->DNSenabled && $this->acl_is_createable()){
180 $accountACL = true;
181 }
182 $smarty->assign("accountACL",$accountACL);
184 /* Add new empty array to our record list */
185 if(isset($_POST['AddNewRecord'])){
186 $this->dnsEntry['RECORDS'][] =array("type"=>"aRecord","value"=>"");
187 }
189 /* Handle all posts */
190 $only_once =true;
191 foreach($_POST as $name => $value){
193 /* Check if we have to delete a record entry */
194 if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
196 /* Avoid performing this once again */
197 $only_once = false;
199 /* Extract id for specified entry */
200 $id = preg_replace("/RemoveRecord_/","",$name);
201 $id = preg_replace("/_.*$/","",$id);
203 /* Delete this record, mark edited entries to be able to delete them */
204 if(isset($this->dnsEntry['RECORDS'][$id])){
205 unset($this->dnsEntry['RECORDS'][$id]);
206 }
207 }
208 }
210 /* Assign smarty all non DNs attributes */
211 foreach($this->attributes as $attr){
212 $smarty->assign($attr,$this->$attr);
213 }
215 /* Assign smarty all DNS attributes */
216 foreach($this->DNSattributes as $attr){
217 $smarty->assign($attr,$this->dnsEntry[$attr]);
218 }
220 /* Assign all needed vars */
221 $smarty->assign("DNSAccount",$this->DNS_is_account);
222 $smarty->assign("Zones",$this->Zones);
223 $smarty->assign("ZoneKeys",($this->Zones));
224 $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
226 $tmp = $this->generateRecordsList();
228 $changeStateForRecords = $tmp['changeStateForRecords'];
230 $smarty->assign("records",$tmp['str']);
231 $smarty->assign("changeStateForRecords",$changeStateForRecords);
232 $smarty->assign("staticAddress","<font class=\"must\">*</font>");
234 $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
235 return($display);
236 }
238 function remove_from_parent()
239 {
240 /*
241 $ldap = $this->config->get_ldap_link();
242 $ldap->cd($this->orig_dn);
243 $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("relativeDomainName","zoneName"));
244 while($attr = $ldap->fetch()){
245 $ldap->cd($attr['dn']);
246 $ldap->rmDir($attr['dn']);
247 show_ldap_error($ldap->get_error(), sprintf(_("Removing of terminal/dns acoount with dn '%s' failed."),$this->dn));
248 }
249 */
250 }
252 /* Save data to object */
253 function save_object()
254 {
255 /* Save all posted vars */
256 plugin::save_object();
258 /* Ge all non dns attributes (IP/MAC)*/
259 foreach($this->attributes as $attr){
260 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
261 $this->$attr = $_POST[$attr];
262 }
263 }
265 /* Get dns attributes */
266 if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
268 /* Check for posted record changes */
269 if(is_array($this->dnsEntry['RECORDS']) && $this->acl_is_writeable("Records")){
270 foreach($this->dnsEntry['RECORDS'] as $key => $value){
272 /* Check if type has changed */
273 if(isset($_POST['RecordTypeSelectedFor_'.$key])){
274 $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
275 }
276 /* Check if value has changed */
277 if(isset($_POST['RecordValue_'.$key])){
278 $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
279 }
280 }
281 }
282 /* Get all basic DNS attributes (TTL, Clas ..)*/
283 foreach($this->DNSattributes as $attr){
284 if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
285 $this->dnsEntry[$attr] = $_POST[$attr];
286 }
287 }
289 if($this->DNSenabled && $this->acl_is_removeable() && !isset($_POST['enableDNS'])){
290 $this->DNS_is_account = false;
291 }elseif(!$this->DNSenabled && $this->acl_is_createable() && isset($_POST['enableDNS'])){
292 $this->DNS_is_account = true;
293 }
294 }
295 }
298 /* Check supplied data */
299 function check()
300 {
301 /* Call common method to give check the hook */
302 $message= plugin::check();
304 /* Check if ip must be given
305 */
306 if(($this->IPisMust)||($this->DNS_is_account)){
308 /* Check if ip is empty
309 */
310 if ($this->ipHostNumber == "" && $this->acl_is_writeable("ipHostNumber")){
311 $message[]= _("The required field 'IP-address' is not set.");
312 }
314 /* check if given ip is valid ipi
315 */
316 $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
317 if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
318 $message[]= _("Wrong IP format in field IP-address.");
319 }
320 }
322 /* Check if mac is empty
323 */
324 if ($this->macAddress == "" && $this->acl_is_writeable("macAddress")){
325 $message[]= _("The required field 'MAC-address' is not set.");
326 }
328 /* Check if given mac is valid mac
329 */
330 $tr = count(split(":",$this->macAddress));
331 if($tr!=6){
332 $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
333 }
335 /* only perfrom this checks if this is a valid DNS account */
336 if($this->DNS_is_account){
338 $checkArray = array();
339 $onlyOnce = array();
341 $onlyOnce['cNAMERecord'] = 0;
343 /* Walk through all entries and detect duplicates or mismatches
344 */
345 foreach($this->dnsEntry['RECORDS'] as $name => $values){
347 /* Count record values, to detect duplicate entries for a specific record
348 */
349 if(!isset($checkArray[$values['type']][$values['value']])){
350 $checkArray[$values['type']][$values['value']] = 0;
351 }else{
352 $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
353 }
355 /* Check if given entries in $onlyOnce are used more than once
356 */
357 if(isset($onlyOnce[$values['type']])){
358 $onlyOnce[$values['type']] ++;
359 if($onlyOnce[$values['type']] > 1){
360 $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
361 }
362 }
364 /* Skip txt record ...
365 */
366 if($values['type'] == "tXTRecord") continue;
368 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
369 */
370 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
371 $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."),
372 $this->ipHostNumber);
373 }
375 /* only lower-case is allowed in record entries ...
376 */
377 if($values['value'] != strtolower($values['value'])){
378 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
379 }
380 }
381 }
382 return ($message);
383 }
386 /* Save to LDAP */
387 function save($dn)
388 {
389 $ldap= $this->config->get_ldap_link();
391 /*******************/
392 /* IP-MAC HANDLING */
393 /*******************/
395 /* $dn was posted as parameter */
396 $this->dn = $dn;
398 /* Save DNS setting & ip/Mac*/
399 plugin::save();
401 /* Write back to ldap */
402 $ldap->cd($this->dn);
403 $this->cleanup();
404 $ldap->modify ($this->attrs);
406 /****************/
407 /* DNS HANDLING */
408 /****************/
410 /* If isn't DNS account but initially was DNS account
411 remove all DNS entries
412 */
413 if(!$this->DNSenabled){
414 return;
415 }else{
417 /* Add ipHostNumber to aRecords
418 */
419 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
421 /* Create diff and follow instructions
422 * If Account was disabled, remove account by setting exists to false
423 */
424 if((!$this->DNS_is_account)&&($this->initially_was_account)){
425 $this->dnsEntry['exists'] = false;
426 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
427 }else{
428 $this->dnsEntry['exists'] = $this->DNS_is_account;
429 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
430 }
432 /* move follwoing entries
433 */
434 foreach($tmp['move'] as $src => $dst){
435 $this->recursive_move($src,$dst);
436 }
438 /* Delete dns */
439 foreach($tmp['del'] as $dn => $del){
440 $ldap->cd($dn);
441 $ldap->rmdir_recursive($dn);
442 }
444 /* Add || Update new DNS entries
445 */
446 foreach($tmp['add'] as $dn => $attrs){
447 $ldap->cd($dn);
448 $ldap->cat($dn, array('dn'));
449 if(count($ldap->fetch())){
450 $ldap->cd($dn);
451 $ldap->modify ($attrs);
452 }else{
453 $ldap->cd($dn);
454 $ldap->add($attrs);
455 }
456 }
458 /* Display errors
459 */
460 if($ldap->get_error() != "Success"){
461 show_ldap_error($ldap->get_error(), sprintf(_("Saving of terminal/dns account with dn '%s' failed."),$this->dn));
462 }
463 }
464 }
466 /* Create html table with all used record types
467 */
468 function generateRecordsList()
469 {
470 $changeStateForRecords = "";
472 if(!$this->DNS_is_account) {
473 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
474 return $str;
475 }
477 $str = "<table summary='' width='100%'>";
478 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
480 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
481 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
482 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
484 $str.=" <tr>".
485 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
486 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
487 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
488 "</tr>";
489 }
491 $str.= " <tr>".
492 " <td colspan=2 width='50%'></td><td>".
493 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
494 " </td>".
495 " </tr>".
496 "</table>";
497 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
498 return($ret);
499 }
502 /* Create a html select box which allows us to select different types of records
503 */
504 function generateRecordListBox($selected,$name)
505 {
506 $str = "<select name='".$name."' id='".$name."'>";
507 foreach($this->RecordTypes as $type => $value){
508 $use = "";
509 if($type == $selected){
510 $use = " selected ";
511 }
512 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
513 }
514 $str.="</select>";
515 return($str);
516 }
519 /* Return plugin informations for acl handling */
520 function plInfo()
521 {
522 $tmp = array(
523 "plShortName" => _("DNS"),
524 "plDescription" => _("DNS settings"),
525 "plSelfModify" => FALSE,
526 "plDepends" => array(),
527 "plPriority" => 5,
528 "plSection" => array("administration"),
529 "plCategory" => array("workstation","terminal","phone","server","component","printer","winworkstation"),
531 "plProvidedAcls"=> array(
532 "ipHostNumber" => _("IP address"),
533 "macAddress" => _("MAC address"))
534 );
536 /* Hide all dns specific code, if dns is not available
537 */
538 $DNSenabled = false;
539 foreach($_SESSION['config']->data['TABS']['SERVERSERVICE'] as $tab){
540 if(preg_match("/^servdns$/",$tab['CLASS'])){
541 $tmp['plProvidedAcls']["Records"] = _("DNS records");
542 $tmp['plProvidedAcls']["zoneName"] = _("Zone name");
543 $tmp['plProvidedAcls']["dNSTTL"] = _("TTL");
544 }
545 }
546 return($tmp);
547 }
548 }
550 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
551 ?>