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 }
313 }
316 /* check if given ip is valid ip */
317 if ($this->ipHostNumber != "" && !is_ip($this->ipHostNumber)){
318 $message[]= _("Wrong IP format in field IP-address.");
319 }
321 /* Check if mac is empty
322 */
323 if ($this->macAddress == "" && $this->acl_is_writeable("macAddress")){
324 $message[]= _("The required field 'MAC-address' is not set.");
325 }
327 /* Check if given mac is valid mac
328 */
329 $tr = count(split(":",$this->macAddress));
330 if(!is_mac($this->macAddress)){
331 $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
332 }
334 /* only perfrom this checks if this is a valid DNS account */
335 if($this->DNS_is_account){
337 $checkArray = array();
338 $onlyOnce = array();
340 $onlyOnce['cNAMERecord'] = 0;
342 /* Walk through all entries and detect duplicates or mismatches
343 */
344 foreach($this->dnsEntry['RECORDS'] as $name => $values){
346 /* Count record values, to detect duplicate entries for a specific record
347 */
348 if(!isset($checkArray[$values['type']][$values['value']])){
349 $checkArray[$values['type']][$values['value']] = 0;
350 }else{
351 $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
352 }
354 /* Check if given entries in $onlyOnce are used more than once
355 */
356 if(isset($onlyOnce[$values['type']])){
357 $onlyOnce[$values['type']] ++;
358 if($onlyOnce[$values['type']] > 1){
359 $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
360 }
361 }
363 /* Skip txt record ...
364 */
365 if($values['type'] == "tXTRecord") continue;
367 /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress
368 */
369 if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
370 $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."),
371 $this->ipHostNumber);
372 }
374 /* only lower-case is allowed in record entries ...
375 */
376 if($values['value'] != strtolower($values['value'])){
377 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
378 }
379 }
380 }
381 return ($message);
382 }
385 /* Save to LDAP */
386 function save($dn)
387 {
388 $ldap= $this->config->get_ldap_link();
390 /*******************/
391 /* IP-MAC HANDLING */
392 /*******************/
394 /* $dn was posted as parameter */
395 $this->dn = $dn;
397 /* Save DNS setting & ip/Mac*/
398 plugin::save();
400 /* Write back to ldap */
401 $ldap->cd($this->dn);
402 $this->cleanup();
403 $ldap->modify ($this->attrs);
405 /****************/
406 /* DNS HANDLING */
407 /****************/
409 /* If isn't DNS account but initially was DNS account
410 remove all DNS entries
411 */
412 if(!$this->DNSenabled){
413 return;
414 }else{
416 /* Add ipHostNumber to aRecords
417 */
418 $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
420 /* Create diff and follow instructions
421 * If Account was disabled, remove account by setting exists to false
422 */
423 if((!$this->DNS_is_account)&&($this->initially_was_account)){
424 $this->dnsEntry['exists'] = false;
425 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
426 }else{
427 $this->dnsEntry['exists'] = $this->DNS_is_account;
428 $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
429 }
431 /* move follwoing entries
432 */
433 foreach($tmp['move'] as $src => $dst){
434 $this->recursive_move($src,$dst);
435 }
437 /* Delete dns */
438 foreach($tmp['del'] as $dn => $del){
439 $ldap->cd($dn);
440 $ldap->rmdir_recursive($dn);
441 }
443 /* Add || Update new DNS entries
444 */
445 foreach($tmp['add'] as $dn => $attrs){
446 $ldap->cd($dn);
447 $ldap->cat($dn, array('dn'));
448 if(count($ldap->fetch())){
449 $ldap->cd($dn);
450 $ldap->modify ($attrs);
451 }else{
452 $ldap->cd($dn);
453 $ldap->add($attrs);
454 }
455 }
457 /* Display errors
458 */
459 if($ldap->get_error() != "Success"){
460 show_ldap_error($ldap->get_error(), sprintf(_("Saving of terminal/dns account with dn '%s' failed."),$this->dn));
461 }
462 }
463 }
465 /* Create html table with all used record types
466 */
467 function generateRecordsList()
468 {
469 $changeStateForRecords = "";
471 if(!$this->DNS_is_account) {
472 $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
473 return $str;
474 }
476 $str = "<table summary='' width='100%'>";
477 foreach($this->dnsEntry['RECORDS'] as $key => $entry){
479 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
480 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
481 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
483 $str.=" <tr>".
484 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
485 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
486 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
487 "</tr>";
488 }
490 $str.= " <tr>".
491 " <td colspan=2 width='50%'></td><td>".
492 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
493 " </td>".
494 " </tr>".
495 "</table>";
496 $ret = array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
497 return($ret);
498 }
501 /* Create a html select box which allows us to select different types of records
502 */
503 function generateRecordListBox($selected,$name)
504 {
505 $str = "<select name='".$name."' id='".$name."'>";
506 foreach($this->RecordTypes as $type => $value){
507 $use = "";
508 if($type == $selected){
509 $use = " selected ";
510 }
511 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
512 }
513 $str.="</select>";
514 return($str);
515 }
518 /* Return plugin informations for acl handling */
519 function plInfo()
520 {
521 $tmp = array(
522 "plShortName" => _("DNS"),
523 "plDescription" => _("DNS settings"),
524 "plSelfModify" => FALSE,
525 "plDepends" => array(),
526 "plPriority" => 5,
527 "plSection" => array("administration"),
528 "plCategory" => array("workstation","terminal","phone","server","component","printer","winworkstation"),
530 "plProvidedAcls"=> array(
531 "ipHostNumber" => _("IP address"),
532 "macAddress" => _("MAC address"))
533 );
535 /* Hide all dns specific code, if dns is not available
536 */
537 $DNSenabled = false;
538 foreach($_SESSION['config']->data['TABS']['SERVERSERVICE'] as $tab){
539 if(preg_match("/^servdns$/",$tab['CLASS'])){
540 $tmp['plProvidedAcls']["Records"] = _("DNS records");
541 $tmp['plProvidedAcls']["zoneName"] = _("Zone name");
542 $tmp['plProvidedAcls']["dNSTTL"] = _("TTL");
543 }
544 }
545 return($tmp);
546 }
547 }
549 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
550 ?>